From e00e10e97168b3cad2bf927b67a87448ab2fe90c Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues <16357187+eduardomourar@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:00:22 +0000 Subject: [PATCH 1/2] chore: fix unable to install windows rust toolchain --- .github/workflows/test.yml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dfa8eef..03a3d3f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -99,7 +99,7 @@ jobs: runs-on: windows-latest timeout-minutes: 10 env: - RUSTUP_TOOLCHAIN: ${{ matrix.rust_toolchain }}-${{ matrix.rust_target }} + RUSTUP_TOOLCHAIN: ${{ matrix.rust_toolchain }}-x86_64-pc-windows-gnu strategy: fail-fast: false matrix: @@ -110,7 +110,19 @@ jobs: manifest: ["psm/Cargo.toml", "Cargo.toml"] steps: - uses: actions/checkout@v7 - - run: rustup install ${{ env.RUSTUP_TOOLCHAIN }} --profile minimal + - run: rustup install ${{ env.RUSTUP_TOOLCHAIN }} --profile minimal --target ${{ matrix.rust_target }} + - name: Install i686 MinGW + if: matrix.rust_target == 'i686-pc-windows-gnu' + id: msys2 + uses: msys2/setup-msys2@v2 + with: + msystem: MINGW32 + install: mingw-w64-i686-gcc + update: false + - name: Add i686 MinGW to PATH + if: matrix.rust_target == 'i686-pc-windows-gnu' + shell: bash + run: echo "${{ steps.msys2.outputs.msys2-location }}/mingw32/bin" >> "$GITHUB_PATH" - run: cargo test --target ${{ matrix.rust_target }} --manifest-path=${{ matrix.manifest }} env: CFLAGS: "-Werror -Wundef" From 28e414554a869e86cf8daf02d2ead2b6648f87d6 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues <16357187+eduardomourar@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:55:29 +0000 Subject: [PATCH 2/2] psm: support stack switching on wasm32-wasip3 --- .github/workflows/test.yml | 26 +++++++++--- README.md | 6 +++ psm/build.rs | 5 +++ psm/src/arch/wasm32_wasip3.s | 82 ++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 psm/src/arch/wasm32_wasip3.s diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 03a3d3f..3600b2e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -322,19 +322,33 @@ jobs: cargo build --target ${{ matrix.rust_target }} --manifest-path ${{ matrix.manifest }} wasm-test: - name: Test stacker on WASM + name: Test stacker on ${{ matrix.rust_target }} runs-on: ubuntu-latest timeout-minutes: 10 + strategy: + fail-fast: false + matrix: + include: + - rust_target: wasm32-wasip1 + runner_var: CARGO_TARGET_WASM32_WASIP1_RUNNER + wasmtime_flags: "" + - rust_target: wasm32-wasip2 + runner_var: CARGO_TARGET_WASM32_WASIP2_RUNNER + wasmtime_flags: "" + - rust_target: wasm32-wasip3 + runner_var: CARGO_TARGET_WASM32_WASIP3_RUNNER + wasmtime_flags: "-W component-model-async=y -S p3" env: RUSTUP_TOOLCHAIN: nightly + WASMTIME_VERSION: v51.0.0 steps: - uses: actions/checkout@v7 - - run: rustup install ${{ env.RUSTUP_TOOLCHAIN }} --profile minimal --target wasm32-wasip1 + - run: rustup install ${{ env.RUSTUP_TOOLCHAIN }} --profile minimal --target ${{ matrix.rust_target }} - run: | - curl -Lf https://github.com/bytecodealliance/wasmtime/releases/download/v24.0.0/wasmtime-v24.0.0-x86_64-linux.tar.xz | tar xJf - -C ${{ runner.tool_cache }} - echo "${{ runner.tool_cache }}/wasmtime-v24.0.0-x86_64-linux" >> $GITHUB_PATH - echo "CARGO_TARGET_WASM32_WASIP1_RUNNER=wasmtime run --" >> $GITHUB_ENV - - run: cargo test --target wasm32-wasip1 --all -- --nocapture + curl -Lf https://github.com/bytecodealliance/wasmtime/releases/download/${{ env.WASMTIME_VERSION }}/wasmtime-${{ env.WASMTIME_VERSION }}-x86_64-linux.tar.xz | tar xJf - -C ${{ runner.tool_cache }} + echo "${{ runner.tool_cache }}/wasmtime-${{ env.WASMTIME_VERSION }}-x86_64-linux" >> $GITHUB_PATH + echo "${{ matrix.runner_var }}=wasmtime run ${{ matrix.wasmtime_flags }} --" >> $GITHUB_ENV + - run: cargo test --target ${{ matrix.rust_target }} --all -- --nocapture miri-test: name: Test stacker with Miri on ${{ matrix.os }} diff --git a/README.md b/README.md index d280fa1..51985a5 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,12 @@ On all unsupported platforms this library is a noop. It should compile and run, but it won't actually grow the stack and code will continue to hit the guard pages typically in place. +`wasm32-wasip3`'s `--cooperative-threading` link mode replaces the classic +`__stack_pointer` global with a component-model task-context slot. psm reaches +it through wasi-libc's `__wasm_get_stack_pointer`/`__wasm_set_stack_pointer` +imports instead, so this target is fully supported, including real stack +switching. + # License This project is licensed under either of diff --git a/psm/build.rs b/psm/build.rs index f27b53b..d135693 100644 --- a/psm/build.rs +++ b/psm/build.rs @@ -50,6 +50,11 @@ fn find_assembly( ("sparc", _, _, _) => Some(("src/arch/sparc_sysv.s", true)), ("riscv32", _, _, _) => Some(("src/arch/riscv.s", true)), ("riscv64", _, _, _) => Some(("src/arch/riscv64.s", true)), + // `--cooperative-threading` (see README.md) replaces the classic `__stack_pointer` + // global `wasm32.s` reads and writes with a component-model task-context slot, reached + // through wasi-libc's `__wasm_get_stack_pointer`/`__wasm_set_stack_pointer` imports + // instead, so this target gets its own source using those. + ("wasm32", _, "wasi", "p3") => Some(("src/arch/wasm32_wasip3.s", true)), ("wasm32", _, _, _) => Some(("src/arch/wasm32.o", true)), ("loongarch64", _, _, _) => Some(("src/arch/loongarch64.s", true)), _ => None, diff --git a/psm/src/arch/wasm32_wasip3.s b/psm/src/arch/wasm32_wasip3.s new file mode 100644 index 0000000..ed50b43 --- /dev/null +++ b/psm/src/arch/wasm32_wasip3.s @@ -0,0 +1,82 @@ +#include "psm.h" +#include "gnu_stack_note.s" + +# `wasm32-wasip3` links with `--cooperative-threading`, under which the shadow stack +# pointer is not a `__stack_pointer` WebAssembly global at all (see README.md): it +# lives in a component-model task-context slot, and `rust-lld` rejects any object +# that references a classic `__stack_pointer` global outright. wasi-libc's +# `crt1-*.o` gets and sets that slot through a pair of imported functions, +# `__wasm_get_stack_pointer`/`__wasm_set_stack_pointer` (module `env`), which +# `wit-component` recognizes by name and rewrites into the real task-context +# accessors when a core module becomes a component. This file imports and calls +# those same functions in place of every `global.get`/`global.set __stack_pointer` +# `wasm32.s` has, so `on_stack`/`replace_stack` work the same way here as there. +# +# Generated the same way `wasm32.o` is (see that file's own header), from this source +# instead: +# +# clang -E -x assembler-with-cpp psm/src/arch/wasm32_wasip3.s | llvm-mc -o psm/src/arch/wasm32_wasip3.o --arch=wasm32 -filetype=obj +# +# where `clang`/`llvm-mc` need to be a relatively recent LLVM (confirmed with the one +# shipped in a WASI SDK release, since `cpp` alone does not speak this file's GAS-style +# `#`-comments the way `-x assembler-with-cpp` does). + +.globl __wasm_get_stack_pointer +.type __wasm_get_stack_pointer,@function +.functype __wasm_get_stack_pointer () -> (i32) +.import_module __wasm_get_stack_pointer, env +.import_name __wasm_get_stack_pointer, __wasm_get_stack_pointer + +.globl __wasm_set_stack_pointer +.type __wasm_set_stack_pointer,@function +.functype __wasm_set_stack_pointer (i32) -> () +.import_module __wasm_set_stack_pointer, env +.import_name __wasm_set_stack_pointer, __wasm_set_stack_pointer + +.globl rust_psm_stack_direction +.type rust_psm_stack_direction,@function +rust_psm_stack_direction: +.functype rust_psm_stack_direction () -> (i32) + i32.const STACK_DIRECTION_DESCENDING + end_function + +.globl rust_psm_stack_pointer +.type rust_psm_stack_pointer,@function +rust_psm_stack_pointer: +.functype rust_psm_stack_pointer () -> (i32) + call __wasm_get_stack_pointer + end_function + +.globl rust_psm_on_stack +.type rust_psm_on_stack,@function +rust_psm_on_stack: +.functype rust_psm_on_stack (i32, i32, i32, i32) -> () + # get our new stack argument, then save the old stack + # pointer into that local + local.get 3 + call __wasm_get_stack_pointer + local.set 3 + call __wasm_set_stack_pointer + + # Call our indirect function specified + local.get 0 + local.get 1 + local.get 2 + call_indirect (i32, i32) -> () + + # restore the stack pointer before returning + local.get 3 + call __wasm_set_stack_pointer + end_function + +.globl rust_psm_replace_stack +.type rust_psm_replace_stack,@function +rust_psm_replace_stack: +.functype rust_psm_replace_stack (i32, i32, i32) -> () + local.get 2 + call __wasm_set_stack_pointer + local.get 0 + local.get 1 + call_indirect (i32) -> () + unreachable + end_function