tomaka / redshirt

🧑‍🔬 Operating system
GNU General Public License v3.0
1.43k stars 37 forks source link

Post-threads messages passing rework brainstorming #863

Open tomaka opened 3 years ago

tomaka commented 3 years ago

At the time of writing this issue, the WebAssembly threads proposal is still a work in progress. The Rust compiler has some support for atomic_notify and atomic_wait, but it requires compiling the standard library with a specific flag. The wasmi interpreter doesn't support these instructions.

However, let's assume that everything is ready and notify/wait are supported, and memory sharing is possible.


A message sender and its receiver should actually share memory in order to write messages directly to the target without copying said messages.

The details of sharing memory are still a bit uncertain, and it is unclear to me whether it will be possible to add memories at runtime.

Whether it is the case or not, each interface registered or used should result in one memory object being attached to the VM. This memory object would be shared with the other side of the interface, and contains the messages being sent and the responses. In order to avoid collisions in the memory allocators, the sender must allocate the buffer containing the responses.

Contrary to what might be the intuitive thing to do, we shouldn't make programs send messages to each other directly, as the kernel is responsible for multiplexing the messages and ensuring a fair distribution.

A ring buffer would be used in order to send messages, and another one for receiving responses. These two ring buffers are not tied to a specific interface, and thus can be in any memory (most likely the main one). Sending a message would be done by writing the message in the appropriate memory, adding an element in the ring buffer, and doing atomic_notify to notify the kernel of the message. Waiting for a response would be done with atomic_wait to wait for the receiving ring buffer to contain elements. Sending a messages needs to indicate the location of the ring buffer that will receive the response.

One unresolved question is how to rework the interface message requests to fit this scheme.

tomaka commented 3 years ago

There are two possible attack vectors that I can think of that we should somehow defend against: