-
Notifications
You must be signed in to change notification settings - Fork 16
Compile rust code with lind-wasm and enable shared memory #577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5d961eb
Add: Compile rust code with lind-wasm and enable shared memory
yzhang71 ec9cc77
Add: steps to compile rust with lind-wasm
yzhang71 e216369
Mod: steps to compile rust with lind-wasm
yzhang71 002b73d
Mod: steps to compile rust with lind-wasm
yzhang71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # Compiling Rust Code with `lind-glibc` (Shared Memory Enabled) | ||
|
|
||
| To compile Rust programs against **`lind-glibc`**, you must configure Rust to: | ||
|
|
||
| 0. Use the nightly Rust toolchain (because `-Z build-std` is a nightly-only feature) | ||
| 1. Use the custom `config.toml` configuration file | ||
| 2. Use the custom `wasip1-clang` linker wrapper | ||
| 3. Rebuild the Rust standard library (`std`) with these features enabled | ||
|
|
||
| --- | ||
| ## 1. Cargo Configuration (`.cargo/config.toml`) | ||
|
|
||
| Create or modify `.cargo/config.toml` as follows (based on | ||
| `scripts/rust/config.toml.template`): | ||
|
|
||
| ```toml | ||
| [build] | ||
| # Compile all Rust code for WASI Preview 1 | ||
| target = "wasm32-wasip1" | ||
|
|
||
| [target.wasm32-wasip1] | ||
| # Use lind’s custom clang wrapper for glibc-based WASI linking | ||
| linker = "/home/lind-wasm/scripts/wasip1-clang.sh" | ||
|
|
||
| rustflags = [ | ||
| # Do not use Rust’s built-in self-contained WASI linker | ||
| "-C", "link-self-contained=no", | ||
|
|
||
| # Enable WebAssembly features required for shared memory | ||
| # - atomics: required for multi-threading | ||
| # - bulk-memory: required for memory.copy / memory.fill | ||
| # - crt-static: ensure static runtime linking | ||
| "-C", "target-feature=+crt-static,+atomics,+bulk-memory", | ||
|
|
||
| # Import and export the linear memory so the runtime can control it | ||
| "-C", "link-arg=-Wl,--import-memory", | ||
| "-C", "link-arg=-Wl,--export-memory", | ||
|
|
||
| # Enable shared linear memory (threads proposal) | ||
| "-C", "link-arg=-Wl,--shared-memory", | ||
|
|
||
| # Set maximum memory size (64 MiB) | ||
| "-C", "link-arg=-Wl,--max-memory=67108864", | ||
|
|
||
| # Export stack symbols required by lind runtime | ||
| "-C", "link-arg=-Wl,--export=__stack_pointer", | ||
| "-C", "link-arg=-Wl,--export=__stack_low", | ||
| ] | ||
| ``` | ||
|
|
||
| ### Why this is necessary | ||
|
|
||
| * Shared memory requires `atomics + bulk-memory` | ||
| * These features must be enabled for both your crate and `std` | ||
| * Rust’s prebuilt `std` does *not* include these features by default | ||
| * Therefore, `std` must be rebuilt explicitly | ||
|
|
||
| --- | ||
|
|
||
| ## 2. Custom Linker Wrapper (`scripts/wasip1-clang.sh`) | ||
|
|
||
| The following script replaces Cargo’s default linker and ensures that: | ||
|
|
||
| * `lind-glibc` is used instead of WASI libc | ||
| * The correct `crt1.o` startup object is injected | ||
| * `pthread` and glibc are linked properly | ||
| * All Rust-provided linker arguments are preserved | ||
|
|
||
| ### Linker Script | ||
|
|
||
| ```bash | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| # Path to lind-glibc sysroot | ||
| SYSROOT=/home/lind-wasm/src/glibc/sysroot | ||
| LIBDIR="$SYSROOT/lib/wasm32-wasi" | ||
| CRT1="$LIBDIR/crt1.o" | ||
|
|
||
| # Sanity checks (fail early with a clear message) | ||
| [ -r "$CRT1" ] || { echo "Missing $CRT1"; exit 1; } | ||
| [ -d "$LIBDIR" ] || { echo "Missing $LIBDIR"; exit 1; } | ||
|
|
||
| # Base clang invocation | ||
| cmd=( | ||
| clang | ||
| --target=wasm32-unknown-wasip1 | ||
| --sysroot="$SYSROOT" | ||
| -nostartfiles # Prevent clang from injecting its own crt1.o | ||
| ) | ||
|
|
||
| # Forward all arguments from rustc unchanged | ||
| cmd+=("$@") | ||
|
|
||
| # Inject lind-glibc startup object and libraries | ||
| cmd+=( | ||
| "$CRT1" | ||
| -L"$LIBDIR" | ||
| -lc # lind-glibc | ||
| -pthread # enable pthread support | ||
| ) | ||
|
|
||
| # Print the final command for debugging (stderr keeps rustc output clean) | ||
| echo "[clang wrapper exec]" "${cmd[@]}" 1>&2 | ||
|
|
||
| # Execute the linker | ||
| exec "${cmd[@]}" | ||
| ``` | ||
|
|
||
| ### Why this script exists | ||
|
|
||
| Rust’s default WASI linker: | ||
|
|
||
| * Uses WASI-libc instead of glibc | ||
| * Does not support lind’s threading and memory model | ||
| * Cannot inject a custom `crt1.o` | ||
|
|
||
| This wrapper ensures full control over startup, libc, and threading behavior. | ||
| --- | ||
|
|
||
| ## 3. Build Rust with a Custom `std` | ||
|
|
||
| After configuring Cargo, compile your Rust project using **nightly** and rebuild the standard library: | ||
|
|
||
| ```bash | ||
| cargo build -Z build-std=std,panic_abort | ||
| ``` | ||
|
|
||
| Alternatively, you can add the following to `.cargo/config.toml`, which achieves the same effect and is often easier to avoid missing during builds: | ||
|
|
||
| ```toml | ||
| [unstable] | ||
| build-std = ["std", "panic_abort"] | ||
| ``` | ||
|
|
||
| This ensures that `std` is always rebuilt with the required features whenever you run `cargo build`. | ||
|
|
||
| ### What this does | ||
|
|
||
| * Forces Rust to rebuild `std` for `wasm32-wasip1` | ||
| * Applies your `rustflags` to `std` itself | ||
| * Enables atomics + bulk-memory inside `libstd` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [build] | ||
| target = "wasm32-wasip1" | ||
|
|
||
| [target.wasm32-wasip1] | ||
| linker = "/home/lind-wasm/scripts/wasip1-clang.sh" | ||
| rustflags = [ | ||
| "-C", "link-self-contained=no", | ||
|
|
||
| # Enable atomics + bulk-memory + keep crt-static | ||
| "-C", "target-feature=+crt-static,+atomics,+bulk-memory", | ||
|
|
||
| # Tell wasm-ld to use shared memory | ||
| "-C", "link-arg=-Wl,--import-memory", | ||
| "-C", "link-arg=-Wl,--export-memory", | ||
| "-C", "link-arg=-Wl,--shared-memory", | ||
| "-C", "link-arg=-Wl,--max-memory=67108864", | ||
| "-C", "link-arg=-Wl,--export=__stack_pointer", | ||
| "-C", "link-arg=-Wl,--export=__stack_low", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| SYSROOT=/home/lind-wasm/src/glibc/sysroot | ||
| LIBDIR="$SYSROOT/lib/wasm32-wasi" | ||
| CRT1="$LIBDIR/crt1.o" | ||
|
|
||
| # Sanity checks (fail early with a clear message) | ||
| [ -r "$CRT1" ] || { echo "Missing $CRT1"; exit 1; } | ||
| [ -d "$LIBDIR" ] || { echo "Missing $LIBDIR"; exit 1; } | ||
|
|
||
| # Build the actual clang command we will exec | ||
| cmd=( | ||
| clang | ||
| --target=wasm32-unknown-wasip1 | ||
| --sysroot="$SYSROOT" | ||
| -nostartfiles # prevent clang from looking for its own crt1.o | ||
| ) | ||
|
|
||
| # Forward all rustc-provided args | ||
| cmd+=("$@") | ||
|
|
||
| # Inject our startup object and libraries **after** user objects | ||
| cmd+=( | ||
| "$CRT1" | ||
| -L"$LIBDIR" | ||
| -lc | ||
| -pthread | ||
| ) | ||
|
|
||
| # Show the exact command we will run (to STDERR so rustc keeps its stdout clean) | ||
| echo "[clang wrapper exec]" "${cmd[@]}" 1>&2 | ||
|
|
||
| # Run it | ||
| exec "${cmd[@]}" | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
Adding this in the .cargo/config.toml also does the same, and is perhaps easier to not miss during build