npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

static-buffer

v1.0.1

Published

StaticBuffer, just like Buffer but you can execute machine code in it.

Downloads

23

Readme

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;