Skip to content

Rust Compilation for Lind-Wasm Support #302

@Yaxuan-w

Description

@Yaxuan-w

To compile rustc into lind-wasm, our first step is to ensure regular Rust programs can run successfully within the lind-wasm runtime. This issue is to track the progress of compiling Rust code into the lind-wasm.

Current Approach

Because Rust uses the LLVM backend, and lind-wasm relies heavily on custom build settings during both object generation and linking, we use similar strategies in Rust:

Thread support during compilation:

Just like in the C compilation process for lind-wasm, we need to enable -pthread at the compilation stage to ensure correct threading behavior by wasm32-wasip1-threads. In rust the way to do so, is to enable the '+atomics' feature in unison with the wasm32-wasip1-threads target, which we have configured.

Custom memory and stack behavior during linking:

At the linking stage, lind-wasm requires specific flags to manage memory and stack behavior in wasmtime:

  • --export-memory
  • --import-memory
  • --max-memory
  • --export=__stack_pointer
  • --export=__stack_low

Though import-memory and export-memory are somewhat redundant, --import-memory tells the linker that the wasm module will import memory from the host (lind-wasm runtime) and --export memory tells the linker that the wasm module must make it's memory accessible to the host. For our tests, we will be keeping both flags as LLVM 16 allows and requires both for wasi-threads. See issue: WebAssembly/WASI#502

In addition, we will also run unit tests without the --import-memory to see if it is truly required for successful compilation and execution with wasi and wasi-threads support.

To implement this in Rust, we configure those in .cargo/config.toml:

  • A target of wasm32-wasip1-threads
  • A custom linker that invokes our modified wasm-ld with lind-wasm-specific flags
  • Include the tokio crate in the list of dependencies with the following features enabled to test async functions: "rt", "time", "macros", "io-util", "sync"

Current Progress

With help from Sankalp @ssannkkallpp , we successfully compiled and ran:

  • A Rust Hello World program
  • A synchronized write test
  • Simple rust programs with multiple I/O operations and computations performed in sequence without blocking the runtime as a whole
  • Simple asynchronous rust programs with tokio

These ran correctly under lind-wasm without errors and the asynchronous processes run without blocking the runtime.

--import-memory Conflict

We noticed that using both --import-memory and --export-memory leads to a conflict. Dropping the --import-memory flag doesn't appear to impact current functionality. Rust tests run fine without it. C programs also behave correctly without --import-memory.

Further discussion is needed to determine whether --import-memory is fundamentally incompatible with our setup or if it might be required in specific cases.

Next Steps

@ssannkkallpp will add more Rust-based test cases to improve coverage. Once stabilized, a PR will be submitted with working examples and build changes. We also need to discuss how to integrate Rust compilation into our current CI/CD pipeline.

@ssannkkallpp will be experimenting with more C programs and tests by compiling for wasm32 without the --import-memory flag to ensure no regression.

Metadata

Metadata

Assignees

Labels

buildIssues related to building and running programs into the lind-wasm

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions