Giter Site home page Giter Site logo

streamich / libsys Goto Github PK

View Code? Open in Web Editor NEW
42.0 2.0 4.0 222 KB

POSIX/Linux/Mac system calls and low level operations for Node.js

Home Page: https://www.npmjs.com/package/libsys

License: The Unlicense

Python 0.43% C++ 41.55% JavaScript 33.08% C 24.72% Dockerfile 0.22%

libsys's Introduction

libsys

  • Execute Linux/Mac syscalls from Node.js
  • Get memory address of Buffer, ArrayBuffer, TypedArray
  • Call arbitrary machine code from Node.js
  • Create machine code trampolines that will call Node.js functions

Installation

npm install libsys

Compiles on Linux, Mac and Windows (in WSL process).

Usage

Print Hello world to console

const libsys = require('libsys');

const STDOUT = 1;
const isMac = process.platform === 'darwin';
const SYS_write = isMac ? 0x2000004 : 1;
const buf = Buffer.from('Hello world\n');

libsys.syscall(SYS_write, STDOUT, buf, buf.length);

More goodies

  • libjs — POSIX command implementation (libc in JavaScript).
  • bamboo — Node.js clone in pure JavaScript.
  • jskernel — proposal of Node in kernel space.
  • Assembler.js — X86_64 assembler in JavaScript.

Reference

  • syscall — Executes system call with varied type arguments, returns 32-bit result
  • syscall64 — Executes system call with varied type arguments, return 64-bit result
  • syscall_N — Executes system call with N number arguments, returns 32-bit result
  • syscall64_N — Executes system call with N number arguments, returns 64-bit result
  • getAddressArrayBuffer — Returns 64-bit address of ArrayBuffer
  • getAddressTypedArray — Returns 64-bit address of TypedArray (including Uint8Array, etc..)
  • getAddressBuffer — Returns 64-bit address of Node's Buffer
  • getAddress — Returns 64-bit address of any buffer type
  • frame — Creates ArrayBuffer in the specified memory location
  • call — Calls machine code at specified address with up to 10 arguments, returns 32-bit result
  • call64 — Calls machine code at specified address with up to 10 arguments, returns 64-bit result
  • call_0 — Call machine code with no arguments
  • call_1
  • call64_0
  • call64_1
  • jumper
  • sigaction - Executes sigaction system call
  • cmpxchg8 - Compare and exchange value at memory location
  • cmpxchg16 - Compare and exchange value at memory location
  • cmpxchg32 - Compare and exchange value at memory location

Arguments

Different JavaScript types can be used as Targ argument in some functions. Here is how they are converted to 64-bit integers:

type Targ = number | [number, number] | [number, number, number] | string | ArrayBuffer | TypedArray | Buffer;
  • number is treated as 32-bit integer and gets extended to 64-bit integer;
  • [number, number] treated as a [lo, hi] tuple of two 32-bit integers, which are combined into 64-bit integer;
  • [number, number, number] treated as a [lo, hi, offset] tuple, same as above with the difference that offset is added to the resulting 64-bit integer;
  • string gets converted to C null-terminated string and 64-bit pointer created to the beginning of that string;
  • ArrayBuffer, TypedArray, Buffer 64-bit pointer to the beginning of data contents of those objects is created;

syscall

syscall(command: number, ...args: Targ[]): number;

syscall accepts up to 6 command arguments args. See discussion on Arguments above to see how JavaScript objects are converted to 64-bit integers.

syscall returns a number which is the result returned by the kernel, negative numbers usually represent an error.

syscall64

syscall64(command: number, ...args: TArg[]): [number, number];

Same as syscall, but returns 64-bit result.

syscall_0

syscall_0(command: number): number;
syscall_1(command: number, arg1: number): number;
syscall_2(command: number, arg1: number, arg2: number): number;
syscall_3(command: number, arg1: number, arg2: number, arg3: number): number;
syscall_4(command: number, arg1: number, arg2: number, arg3: number, arg4: number): number;
syscall_5(command: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number): number;
syscall_6(command: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number): number;

Executes system calls and returns 32-bit result. Expects all arguments to be of type number.

syscall64_0

syscall64_0(command: number): [number, number];
syscall64_1(command: number, arg1: number): [number, number];
syscall64_2(command: number, arg1: number, arg2: number): [number, number];
syscall64_3(command: number, arg1: number, arg2: number, arg3: number): [number, number];
syscall64_4(command: number, arg1: number, arg2: number, arg3: number, arg4: number): [number, number];
syscall64_5(command: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number): [number, number];
syscall64_6(command: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number): [number, number];

Executes system calls and returns 64-bit result. Expects all arguments to be of type number.

getAddressArrayBuffer

getAddressArrayBuffer(ab: ArrayBuffer): [number, number];

getAddressTypedArray

getAddressTypedArray(ta: TypedArray | Uint8Array): [number, number];

getAddressBuffer

getAddressBuffer(buf: Buffer): [number, number];

Return memory address of Buffer's data contents.

getAddress

getAddress(buffer: Buffer | ArrayBuffer | TypedArray): [number, number];

frame

frame(addresss: Targ, size: number): ArrayBuffer;

frame returns an ArrayBuffer object of size size that is mapped to memory location specified in addr argument.

call

call(address: Targ, offset?: number, arguments?: Targ[]);

Execute machine code at specified memory address address. The memory address is converted to function pointer and called using your architecture calling conventions. offest is added to the address, but defaults to 0.

Up to 10 arguments can be supplied.

call64

Same as call but ruturns a 64-bit [number, number].

call_0

call_0(address: Targ): number;

Call machine code at address without any arguments using architecture specific calling conventions. Returns a 32-bit result.

License

Unlicense - public domain.

libsys's People

Contributors

renovate-bot avatar renovate[bot] avatar streamich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

libsys's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Detected dependencies

dockerfile
Dockerfile
  • ubuntu cosmic-20190719
npm
package.json
  • nan ^2.14.0
  • @types/node 20.10.3
  • ass-js 2.1.1
  • jest 29.7.0
  • node-addon-api 7.0.0
  • node-gyp 10.0.1
  • node >= 4.4.3
travis
.travis.yml
  • node 10
  • node 8

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

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.