Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down Expand Up @@ -310,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 }}
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions psm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
82 changes: 82 additions & 0 deletions psm/src/arch/wasm32_wasip3.s
Original file line number Diff line number Diff line change
@@ -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
Loading