Coder Social home page Coder Social logo

static-buffer's Introduction

StaticArrayBuffer and StaticBuffer

Hello World Example

Print "Hello World" to console, we do it by executing write system call to 1 file descriptor which is STDOUT:

var StaticBuffer = require('static-buffer/buffer').StaticBuffer;

var sbuf = StaticBuffer.from([
    0x48, 0xc7, 0xc0, 1, 0, 0, 0,       // mov    $0x1,%rax         # System call `1` -- SYS_write
    0x48, 0xc7, 0xc7, 1, 0, 0, 0,       // mov    $0x1,%rdi         # File descriptor `1` -- STDOUT
    0x48, 0x8d, 0x35, 10, 0, 0, 0,      // lea    0x1(%rip),%rsi    # Data address
    0x48, 0xc7, 0xc2, 13, 0, 0, 0,      // mov    $13,%rdx          # Number of bytes to write -- 13
    0x0f, 0x05,                         // syscall                  # Execute the system call.
    0xc3,                               // retq                     # Return
    0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello_
    0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21, // World!
    0x0A, 0                             // \n\0
], 'rwe'); // r - readable, w - writable, e - executable

sbuf.call([], 0); // Hello World!

How it works

StaticArrayBuffer and StaticBuffer extend ArrayBuffer and Buffer classes and provide such extra functionality:

  1. Allocate executable memory.
  2. Execute the machine code in StaticBuffer using .call() method.
  3. Change protection to backing memory.
  4. Promise that actual data in memory will never be moved by runtime.

Memory protection flags:

  • 'r' - readable
  • 'w' - writable
  • 'e' - executable

Usage

var StaticArrayBuffer = require('static-buffer/arraybuffer').StaticArrayBuffer;
var StaticBuffer = require('static-buffer/buffer').StaticArrayBuffer;

StaticArrayBuffer inherits from ArrayBuffer and provides the following extra functionality:

var sab = new StaticArrayBuffer(1024, 'rwe');
StaticArrayBuffer.isStaticArrayBuffer(sab); // true

// Call mathine code stored in `StaticArrayBuffer` at offset `offset`,
// providing `number[]` arguments to that machine code using standard
// calling conventions supported by your system, currently supports 
// up to 10 arguments.
var offset = 0;
var args = [1, 2, 3];
sab.call(args, offset);

// Change protection of the memory block.
sab.setProtection('rw');

// Get the actual address pointer of the memory that backs the `StaticArrayBuffer`.
offset = 0;
var addr = sab.getAddress(offset); // [number, number]

// Free the memory of the `StaticArrayBuffer`, after that your `StaticArrayBuffer`
// should not be used.
sab.free();
sab = null;

StaticBuffer inherits from Buffer and is backed by StaticArrayBuffer instead of ArrayBuffer that Buffer uses. It provides the following extra functionality:

var sbuf = new StaticBuffer(1024, 'rwe');
// or
var offset = 10, len = 10;
sbuf = new StaticBuffer(sab, offset, len);
// or use StaticBuffer.from() syntax

StaticBuffer.isStaticBuffer(sbuf); // true

sbuf.call([], 0);
sbuf.getAddress(0);

sbuf.buffer.free();
sbuf = null;

static-buffer's People

Stargazers

 avatar

Watchers

 avatar  avatar

static-buffer's Issues

libsys.malloc is not a function

Trying to run some samples of ass-js, came to this libsys.malloc is not a function at:

var ab = libsys.malloc(addr, size);

Sample trying to be executed:

require('libsys/shim')
const {X64} = require('ass-js')
const {StaticBuffer} = require('static-buffer/buffer')

const asm = X64({main: 'hello_world_app'})
asm._('db', 'Hello World!\n')
asm._('mov', ['rax', 1])
asm._('mov', ['rdi', 1])
asm._('lea', ['rsi', asm._('rip').disp(-34)])
asm._('mov', ['rdx', 13])
asm._('syscall')
asm._('ret')

const bin = asm.compile()
StaticBuffer.from(bin, 'rwe').call([], 13)

At some point, I just find out about libsys shim, so I imported it at the first line of my code...
Without that import, I face libsys undefined and Cannot read property 'syscall64' of undefined

OS Version:

DISTRIB_ID=ManjaroLinux
DISTRIB_RELEASE=19.0.2
DISTRIB_CODENAME=Kyria
DISTRIB_DESCRIPTION="Manjaro Linux"

Node Version:

v10.17.0

NPM Version:

6.11.3

May I did something wrong?
Any tips?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.