tanishiking / scala-wasm

Experimental WasmGC backend for Scala.js | merging into the upstream Scala.js repo https://github.com/scala-js/scala-js/pull/4988
39 stars 3 forks source link

Make scala-wasm suitable for standalone Wasm VMs #117

Open tanishiking opened 4 months ago

tanishiking commented 4 months ago

Why

Currently, the Wasm modules generated by scala-wasm depend on helper functions in JavaScript, making it possible to run them on JavaScript engines like V8, but not on standalone Wasm runtimes such as wasmtime or wasmedge.

While WebAssembly was initially designed to run in web browsers, it has recently been adopted in various environments due to its portability, fast loading and execution times, and security benefits.

For example, WebAssembly is being used in:

(I personally believe the significant benefit of compiling Scala to WebAssembly lies in the ability to run it in these environments (as well as the Component Model))

However, to leverage WebAssembly in these environments, the binaries need to be executable on standalone Wasm runtimes, which is not possible with the current state of the generated binaries.

How

To achieve this, two major tasks are required (and the latter will likely be a significant challenge):

State of WasmGC support on standalone Wasm runtimes

tanishiking commented 3 months ago

WASI exchange data via Wasm linear memory

First of all, WASI (both preview 1 and preview 2) rexchange data between the Wasm module and the host environment via Wasm linear memory. For example, the fd_write function requires us to place some data on linear memory and pass the addresses and length as arguments. That means, we would need to allocate memory on WebAssembly linear memory, and put/get data on those allocated memory regions.

Direct ByteBuffer to allocate memory?

We've talked a bit about using (Direct)ByteBuffer to allocate off-heap memory on WebAssembly linear memory. Emulating a Direct ByteBuffer in pure WebAssembly would involve using WebAssembly's linear memory, which seems reasonable.

However, it relies on a JVM's garbage collector to handle memory deallocation, and I don't think there's a way to hook into when objects are garbage collected by the WasmGC and free memory in a similar way.

We would need to find a workaround to support Direct ByteBuffer in pure WebAssembly at some point if we want to support it, but at this point, Direct Bytebuffer seems too much for supporting WASI 🤔

Our own Memory Allocator (for wasm linear memory)

Instead of supporting Direct ByteBuffer for linear memory allocation, I'm thinking about having a simple linear memory allocator (we anyway need an allocator for Direct ByteBuffer though), something like:

withScopedMemoryAllocator { allocator =>
  val ptr = allocator.alloc(byteSize)
  ptr.writeBytes(bytes)
  ...
} // when we exit the scope, the allocated memory will be "free"ed.

We can start with a simple memory allocation algorithm considering the those allocated memory should be short-lived only for exchanging data between wasm module and host environment (in WASI context).

tarsa commented 3 months ago

note that java 22 adds support for explicitly deallocated foreign (i.e. off-heap) memory: https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/foreign/package-summary.html . you could partially implement that api, throwing exceptions when user wants implicitly deallocated memory. otoh, that api is maybe too fresh and complex to replicate is straight away and your simple api would be best for purposes of writing glue code.

tanishiking commented 3 months ago

Thanks @tarsa I didn't know about that new API, and it gave me an inspiration!

tarsa commented 3 months ago

note that graalvm guys are working on [GR-18218] Full support for the Foreign Function & Memory API in Native Image (oracle/graal issue#8113). after some iterations, their design probably could be a good inspiration to implement that api in scala-wasm and/or scala-native.

edit: i've edited the reference to be unclickable, to prevent looking at gpl-licensed code.

(added later) p.s. probably my wording was misleading. by their design i've meant the abstract mechanisms (ideas) and their configuration, not the graalvm-specific gpl-encumbered implementation.

sjrd commented 3 months ago

We cannot look at OpenJDK nor Graal nor any other GPL code. All we can look at is the public JavaDoc.

tarsa commented 3 months ago

We cannot look at OpenJDK nor Graal nor any other GPL code. All we can look at is the public JavaDoc.

ok, i've edited the link to be unclickable.

the native-image configuration mechanisms are probably descibed not only in javadocs, but in manuals (and other documentation) too. i guess you should be allowed to look at graalvm manuals without any license-related problems.