Skip to content

allow Memory.buffer to be a SharedArrayBuffer #950

Closed
@bpowers

Description

@bpowers

Some background: I'm working on Browsix, a tool for running multiple C, C++, Go and node.js programs as processes in the browser. We do this for C programs by compiling with a (fork) of Emscripten and running the compiled "binary" in a Web Worker, where our fork mainly replaces src/library_syscalls.js with an implementation that calls into a kernel (library) that runs on the main browser thread.

Our preferred method for system calls works like this: When the process/Web Worker starts up, the backing buffer for the heap is initialized as a SharedArrayBuffer, and a reference is sent to the kernel (over a message sent with postMessage), along with an offset into that SharedArrayBuffer reserved as an address for Atomics.wait. When the program issues future system calls, the runtime sends a message (with postMessage) to the kernel with the system call number and arguments (integers or "pointer" offsets into the heap), and Atomics.wait() on the pre-arranged request. The kernel processes the system call, places results in the SharedArrayBuffer, and Atomics.wake()s the process.

This works great with asm.js code emitted from Emscripten (and is much faster than our first approach, which relied on the Emterpreter to save and restore the stack), but I don't think is possible with the current WebAssembly spec (I would love to be wrong!).

My proposal is to add an additional optional argument to the options object that WebAssembly.Memory takes, a boolean "isShared". When this argument is present and true, the buffer backing the Memory is created as a SharedArrayBuffer.

I've implemented this in v8 here: bpowers/v8@bda41b7 and so far in my limited testing seems to do the trick -- I am able to run small C programs compiled with a modified emscripten/binaryen as Browsix processes in Web Workers.

Thoughts/suggestions?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions