- Execute Linux/Mac
syscall
s 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
npm install libsys
Compiles on Linux, Mac and Windows (in WSL process).
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);
- 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.
syscall
— Executes system call with varied type arguments, returns 32-bit resultsyscall64
— Executes system call with varied type arguments, return 64-bit resultsyscall_N
— Executes system call with Nnumber
arguments, returns 32-bit resultsyscall64_N
— Executes system call with Nnumber
arguments, returns 64-bit resultgetAddressArrayBuffer
— Returns 64-bit address ofArrayBuffer
getAddressTypedArray
— Returns 64-bit address ofTypedArray
(includingUint8Array
, etc..)getAddressBuffer
— Returns 64-bit address of Node'sBuffer
getAddress
— Returns 64-bit address of any buffer typeframe
— CreatesArrayBuffer
in the specified memory locationcall
— Calls machine code at specified address with up to 10 arguments, returns 32-bit resultcall64
— Calls machine code at specified address with up to 10 arguments, returns 64-bit resultcall_0
— Call machine code with no argumentscall_1
call64_0
call64_1
jumper
sigaction
- Executessigaction
system callcmpxchg8
- Compare and exchange value at memory locationcmpxchg16
- Compare and exchange value at memory locationcmpxchg32
- Compare and exchange value at memory location
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 thatoffset
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(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(command: number, ...args: TArg[]): [number, number];
Same as syscall
, but returns 64-bit result.
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(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(ab: ArrayBuffer): [number, number];
getAddressTypedArray(ta: TypedArray | Uint8Array): [number, number];
getAddressBuffer(buf: Buffer): [number, number];
Return memory address of Buffer
's data contents.
getAddress(buffer: Buffer | ArrayBuffer | TypedArray): [number, number];
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(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.
Same as call
but ruturns a 64-bit [number, number]
.
call_0(address: Targ): number;
Call machine code at address
without any arguments using architecture specific calling conventions. Returns a 32-bit result.
Unlicense - public domain.