Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/audit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
security-events: write
uses: google/osv-scanner-action/.github/workflows/osv-scanner-reusable-pr.yml@a345acffa64b0eaede81a3d9aae6141214d9c8fc # v2.6.0
with:
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04
upload-sarif: false
scan-args: --lockfile=./Cargo.lock

Expand All @@ -41,6 +41,6 @@ jobs:
security-events: write
uses: google/osv-scanner-action/.github/workflows/osv-scanner-reusable.yml@a345acffa64b0eaede81a3d9aae6141214d9c8fc # v2.6.0
with:
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04
fail-on-vuln: false
scan-args: --lockfile=./Cargo.lock
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:

jobs:
publish:
runs-on: ubuntu-latest
runs-on: ubuntu-26.04
environment: release
permissions:
id-token: write
Expand Down
58 changes: 36 additions & 22 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,47 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-latest
- os: ubuntu-26.04
rust: stable
name: "Linux x86 (stable)"
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
- os: ubuntu-26.04
rust: nightly
name: "Linux x86 (nightly)"
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
- os: ubuntu-26.04
rust: nightly
name: "Linux x86 (nightly tail calls)"
target: x86_64-unknown-linux-gnu
args: "--features tinywasm/nightly-tail-calls"
- os: ubuntu-latest
- os: ubuntu-26.04
rust: stable
name: "Linux x86 (stable, no default features)"
target: x86_64-unknown-linux-gnu
args: "--no-default-features"
- os: ubuntu-latest
- os: ubuntu-26.04
rust: nightly
name: "Linux x86 (nightly, no default features)"
target: x86_64-unknown-linux-gnu
args: "--no-default-features"
- os: ubuntu-latest
- os: ubuntu-26.04
rust: nightly
name: "Linux x86 (nightly tail calls, no default features)"
target: x86_64-unknown-linux-gnu
args: "--no-default-features --features tinywasm/nightly-tail-calls"
- os: macos-14
- os: macos-26
rust: stable
name: "macOS arm64 (Apple M1)"
- os: ubuntu-latest
target: aarch64-apple-darwin
- os: windows-2025
rust: stable
name: "Windows x86_64"
target: x86_64-pc-windows-msvc
- os: ubuntu-26.04-arm
rust: stable
name: "Linux arm64"
target: aarch64-unknown-linux-gnu
- os: ubuntu-latest
- os: ubuntu-26.04-arm
rust: stable
name: "Linux armv7"
target: armv7-unknown-linux-gnueabihf
Expand All @@ -63,22 +74,25 @@ jobs:
uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
rustflags: ""
build-warnings: warn
components: rustfmt, clippy
if: matrix.target == ''

- name: Install armv7 tools
run: sudo apt-get update && sudo apt-get install -y gcc-arm-linux-gnueabihf qemu-user
if: matrix.target == 'armv7-unknown-linux-gnueabihf'

- name: Run tests
run: cargo test --workspace ${{ matrix.args }} && cargo test --workspace ${{ matrix.args }} --examples
if: matrix.target == ''
run: cargo test --workspace --target ${{ matrix.target }} ${{ matrix.args }}
env:
CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER: arm-linux-gnueabihf-gcc
CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_RUNNER: qemu-arm -L /usr/arm-linux-gnueabihf

- name: Run clippy
run: cargo clippy --workspace ${{ matrix.args }}
if: matrix.target == ''
- name: Run examples
run: cargo test --workspace --target ${{ matrix.target }} ${{ matrix.args }} --examples
if: matrix.target != 'armv7-unknown-linux-gnueabihf'

- name: Run tests (${{ matrix.target }})
uses: houseabsolute/actions-rust-cross@21b0f18dc621b25bfae556ff2791fca4173121e8 # v1.0.8
with:
command: test
target: ${{ matrix.target }}
toolchain: ${{ matrix.rust }}
if: matrix.target != ''
- name: Run clippy
run: cargo clippy --workspace --target ${{ matrix.target }} ${{ matrix.args }}
if: matrix.target != 'armv7-unknown-linux-gnueabihf'
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added WebAssembly threads support, including atomic wait/notify and cross-store shared memory with `MemoryShared`.
- Added new `Memory::data` and `Memory::data_mut` methods for direct access to linear memory.
- Added several internal methods to support a future c-api crate

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ["cfg(rust_analyzer)"] }

[dev-dependencies]
anyhow.workspace = true
tinywasm = { path = "crates/tinywasm", features = ["state"] }
tinywasm = { path = "crates/tinywasm", features = ["send", "state"] }
tinywasm-wasi.workspace = true
wat.workspace = true

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ See the [examples](./examples) directory and [documentation](https://docs.rs/tin
## Cargo Features

- **`full`:** Enables `archive`, `debug`, `parallel-parser`, `parser`, `state`, and `validate`. Enabled by default.
- **`std`:** Enables `std` and parsing from files and streams. Enabled by default.
- **`std`:** Enables parsing from files and streams, shared memory handles, and blocking atomic wait/notify across stores. Enabled by default.
- **`parser`:** Enables `tinywasm-parser` and top-level parse helpers. Enabled by default.
- **`validate`:** Enables WebAssembly validation while parsing. Enabled by default and configurable through [`ParserOptions`](https://docs.rs/tinywasm/latest/tinywasm/parser/struct.ParserOptions.html).
- **`parallel-parser`:** Parallelizes function parsing when `std` is enabled. Enabled by default.
Expand Down Expand Up @@ -104,8 +104,8 @@ TinyWasm generally implements non-JavaScript core proposals at [phase 4](https:/
| [**Garbage Collection**](https://github.com/WebAssembly/gc) | 🟢 | 0.11.0 |
| [**Exception Handling**](https://github.com/WebAssembly/exception-handling) | 🟢 | 0.11.0 |
| [**Compact Import Section**](https://github.com/WebAssembly/compact-import-section) | 🟢 | 0.11.0 |
| [**Threads**](https://github.com/WebAssembly/threads) | 🚧 | next |
| [**Stack Switching**](https://github.com/WebAssembly/stack-switching) | 🌑 | - |
| [**Threads**](https://github.com/WebAssembly/threads) | 🌑 | - |

**Legend**\
🌑 -- not available\
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/src/testsuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ impl TestSuite {
use std::fs::OpenOptions;
use std::io::Write;

let mut file = OpenOptions::new().create(true).append(true).read(true).open(path)?;
if std::env::var("CI").is_ok_and(|value| value == "true") {
return Ok(());
}

let mut file = OpenOptions::new().create(true).read(true).write(true).open(path)?;
let last_line = BufReader::new(&file).lines().last().transpose()?;

if let Some(last) = last_line
Expand Down
16 changes: 12 additions & 4 deletions crates/cli/src/wast_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use std::time::Duration;
use anyhow::{Context, Result, anyhow, bail};
use log::{debug, error};
use tinywasm::types::{MemoryType, RefType, RefValue, TableType, WasmType, WasmValue};
use tinywasm::{ExecProgress, Global, HostFunction, Imports, Memory, Module, ModuleInstance, Store, Table};
use tinywasm::{
ExecProgress, Global, HostFunction, Imports, Memory, MemoryShared, Module, ModuleInstance, Store, Table,
};
use wast::QuoteWat;
use wast::core::{AbstractHeapType, NanPattern};

Expand All @@ -17,7 +19,7 @@ const TEST_MAX_SUSPENSIONS: u32 = 1000;
// Older suites classify encodings that wasmparser accepts with the latest feature set as malformed.
const ACCEPTED_MALFORMED_MESSAGES: &[&str] =
&["integer representation too long", "zero byte expected", "zero flag expected"];
const ACCEPTED_INVALID_MESSAGES: &[&str] = &["multiple memories"];
const ACCEPTED_INVALID_MESSAGES: &[&str] = &["multiple memories", "multiple tables"];

macro_rules! float_value {
($pattern:expr, $float:ty, $variant:ident) => {
Expand Down Expand Up @@ -172,6 +174,8 @@ impl WastRunner {
let table64 = Table::try_new(store, TableType::new64(RefType::FUNCREF, 10, Some(20)), RefValue::Null.into())?;
let memory =
Memory::try_new(store, MemoryType::default().with_page_count_initial(1).with_page_count_max(Some(2)))?;
let shared_memory =
MemoryShared::try_new(MemoryType::default().with_page_count_initial(1).with_page_count_max(Some(2)))?;
let global_i32 =
Global::try_new(store, tinywasm::types::GlobalType::new(WasmType::I32, false), WasmValue::I32(666))?;
let global_i64 =
Expand All @@ -183,6 +187,7 @@ impl WastRunner {

imports
.define("spectest", "memory", memory)
.define("spectest", "shared_memory", shared_memory)
.define("spectest", "table", table)
.define("spectest", "table64", table64)
.define("spectest", "global_i32", global_i32)
Expand Down Expand Up @@ -344,11 +349,14 @@ impl WastRunner {
);
continue;
};
if !message.starts_with(trap.message()) && !trap.message().starts_with(message) {
// The core test suite's "call stack exhausted" text denotes stack exhaustion,
// not which internal stack reaches its configured limit first. Function-entry
// operand reservation can exhaust a value lane before the call-frame stack.
if !matches!(trap, tinywasm::Trap::CallStackOverflow | tinywasm::Trap::ValueStackOverflow) {
test_group.add_result(
&format!("AssertExhaustion({i})"),
span.linecol_in(wast_raw),
Err(anyhow!("expected trap: {}, got: {}", message, trap.message())),
Err(anyhow!("expected stack exhaustion ({message}), got: {}", trap.message())),
);
continue;
}
Expand Down
36 changes: 19 additions & 17 deletions crates/parser/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> MemoryTyp
memory.maximum,
memory.page_size_log2.map(|x| 1 << x),
)
.with_shared(memory.shared)
}

pub(crate) fn convert_module_globals(
Expand Down Expand Up @@ -197,34 +198,35 @@ pub(crate) fn convert_module_code(
}

#[cfg(feature = "validate")]
let (body, data, uses_local_memory, validator_allocs, reader_allocs) = match validator {
let (body, data, uses_local_memory, max_stack, validator_allocs, reader_allocs) = match validator {
Some(validator) => {
let (body, data, uses_local_memory, validator_allocs, reader_allocs) = process_operators_and_validate(
validator,
func,
(local_types, local_addr_map),
metadata,
context,
reader_allocs,
options,
)?;
(body, data, uses_local_memory, Some(validator_allocs), reader_allocs)
let (body, data, uses_local_memory, max_stack, validator_allocs, reader_allocs) =
process_operators_and_validate(
validator,
func,
(local_types, local_addr_map),
metadata,
context,
reader_allocs,
options,
)?;
(body, data, uses_local_memory, max_stack, Some(validator_allocs), reader_allocs)
}
None => {
let (body, data, uses_local_memory, reader_allocs) =
let (body, data, uses_local_memory, max_stack, reader_allocs) =
process_operators(func, (local_types, local_addr_map), metadata, context, reader_allocs, options)?;
(body, data, uses_local_memory, None, reader_allocs)
(body, data, uses_local_memory, max_stack, None, reader_allocs)
}
};
#[cfg(not(feature = "validate"))]
let (body, data, uses_local_memory, validator_allocs, reader_allocs) = {
let (body, data, uses_local_memory, max_stack, validator_allocs, reader_allocs) = {
let _ = validator;
let (body, data, uses_local_memory, reader_allocs) =
let (body, data, uses_local_memory, max_stack, reader_allocs) =
process_operators(func, (local_types, local_addr_map), metadata, context, reader_allocs, options)?;
(body, data, uses_local_memory, None, reader_allocs)
(body, data, uses_local_memory, max_stack, None, reader_allocs)
};
Ok((
FunctionCode { instructions: body, data: data.finish(), locals: local_counts, uses_local_memory },
FunctionCode { instructions: body, data: data.finish(), locals: local_counts, max_stack, uses_local_memory },
validator_allocs,
reader_allocs,
))
Expand Down
1 change: 0 additions & 1 deletion crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ impl Parser {
#[cfg(feature = "validate")]
{
let features = WasmFeatures::WASM3
.difference(WasmFeatures::THREADS)
.union(WasmFeatures::CUSTOM_PAGE_SIZES)
.union(WasmFeatures::WIDE_ARITHMETIC)
.union(WasmFeatures::COMPACT_IMPORTS);
Expand Down
31 changes: 31 additions & 0 deletions crates/parser/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pub(crate) mod visit {
// and fallback opcode. Instructions without a family use plain emission.
macro_rules! lowering_ops {
() => {};
(atomic $op:ident $inputs:tt => $outputs:tt {
$($visit:ident => $width:literal),* $(,)?
} $($rest:tt)*) => {
$(lowering_ops!(@atomic $op $inputs => $outputs $visit $width);)*
lowering_ops!($($rest)*);
};
($kind:ident $inputs:tt => $outputs:tt {
$($visit:ident $(($($arg:ident: $ty:ty),+))? => $instr:ident $([$family:ident($($rule_arg:expr),*)])?),* $(,)?
} $($rest:tt)*) => {
Expand Down Expand Up @@ -104,6 +110,30 @@ pub(crate) mod visit {
(@memory_arg $memory_arg_idx:ident, $lane:ident) => {
tinywasm_types::MemoryLaneArg { memory_arg_idx: $memory_arg_idx, lane: $lane }
};
(@atomic $op:ident [$($input:ident),*] => [$($output:ident),*] $visit:ident $width:literal) => {
fn $visit(&mut self, memarg: wasmparser::MemArg) -> Self::Output {
if memarg.align != ($width as u32).trailing_zeros() as u8 {
return Err(crate::ParseError::Other("invalid atomic alignment".into()));
}
let address = self.metadata.memory_size(memarg.memory)?;
self.mark_memory(memarg.memory);
let memory = self.push128(tinywasm_types::Operand128::<tinywasm_types::MemoryOperand>::new(
memarg.offset, memarg.memory,
))?;
let is_64 = lowering_ops!(@atomic_is64 [$($input),*] [$($output),*]);
let arg = tinywasm_types::AtomicArg::new(
memory, tinywasm_types::AtomicWidth::from_bytes($width), is_64, tinywasm_types::AtomicOp::$op,
);
self.emit(
&[$(lowering_ops!(@size $input, address)),*],
&[$(lowering_ops!(@size $output, address)),*],
tinywasm_types::Instruction::Atomic(arg),
)
}
};
(@atomic_is64 [Addr, S64 $(, S64)*] $outputs:tt) => { true };
(@atomic_is64 [Addr] [S64]) => { true };
(@atomic_is64 $inputs:tt $outputs:tt) => { false };
(@global $inputs:tt => $outputs:tt $($operator:tt)*) => {
lowering_ops!(@resolved global_size $inputs => $outputs $($operator)*);
};
Expand Down Expand Up @@ -203,6 +233,7 @@ pub(crate) mod visit {
(@@function_references $($rest:tt)* ) => {};
(@@gc $($rest:tt)* ) => {};
(@@exceptions $($rest:tt)* ) => {};
(@@threads $($rest:tt)* ) => {};

(@@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*)) => {
fn $visit(&mut self $($(,_: $argty)*)?) -> Self::Output {
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub(crate) struct FunctionCode {
pub instructions: Vec<Instruction>,
pub data: WasmFunctionData,
pub locals: ValueCounts,
pub max_stack: ValueCounts,
pub uses_local_memory: bool,
}

Expand Down Expand Up @@ -553,6 +554,7 @@ impl<'a> ModuleReader<'a> {
locals: code.locals,
params,
results,
max_stack: code.max_stack,
}))
})
.collect::<Result<_>>()?;
Expand Down
Loading