diff --git a/.cargo/config.toml b/.cargo/config.toml index 19d47a2..807bc56 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,7 +1,6 @@ [alias] version-dev="workspaces version --no-git-commit --force tinywasm*" dev="run -- -l debug run" - test-mvp="test --package tinywasm --test test-mvp --release -- --enable " test-2="test --package tinywasm --test test-two --release -- --enable " test-wast="test --package tinywasm --test test-wast -- --enable " diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 92d56f4..9061eb8 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -16,14 +16,21 @@ jobs: with: submodules: true - - name: Install stable Rust toolchain - run: rustup update stable + - name: Install stable Rust toolchain & Binaryen + run: | + rustup update stable + rustup update nightly + rustup target add wasm32-unknown-unknown + sudo apt-get install -y binaryen wabt + + - name: Build wasm + run: ./examples/rust/build.sh - name: Build (stable) - run: cargo +stable build --workspace --exclude wasm-testsuite + run: cargo +stable build --workspace - name: Run tests (stable) - run: cargo +stable test --workspace --exclude wasm-testsuite + run: cargo +stable test --workspace - name: Run MVP testsuite run: cargo +stable test-mvp @@ -37,14 +44,20 @@ jobs: with: submodules: true - - name: Install nightly Rust toolchain - run: rustup update nightly + - name: Install nightly Rust toolchain & Binaryen + run: | + rustup update nightly + rustup target add wasm32-unknown-unknown + sudo apt-get install -y binaryen wabt + + - name: Build wasm + run: ./examples/rust/build.sh - name: Build (nightly, no default features) - run: cargo +nightly build --workspace --exclude wasm-testsuite --exclude rust-wasm-examples --no-default-features + run: cargo +nightly build --workspace --no-default-features - name: Run tests (nightly, no default features) - run: cargo +nightly test --workspace --exclude wasm-testsuite --exclude rust-wasm-examples --no-default-features + run: cargo +nightly test --workspace --no-default-features - name: Run MVP testsuite (nightly) run: cargo +nightly test-mvp diff --git a/.gitignore b/.gitignore index f5dcc83..d2fbb51 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ /target notes.md examples/rust/out/* +examples/rust/target +examples/rust/Cargo.lock examples/wast/* +perf.* +flamegraph.svg diff --git a/.vscode/settings.json b/.vscode/settings.json index 4f9768f..51f36b4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,9 @@ { "search.exclude": { "**/wasm-testsuite/data": true - } + }, + "rust-analyzer.linkedProjects": [ + "./Cargo.toml", + "./examples/rust/Cargo.toml" + ] } \ No newline at end of file diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 8705e5a..d3816d0 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -1 +1,29 @@ # TinyWasm's Architecture + +TinyWasm follows the general Runtime Structure described in the [WebAssembly Specification](https://webassembly.github.io/spec/core/exec/runtime.html). +Some key differences are: + +- Values are stored without their type, (as `u64`), and the type is inferred from the instruction that uses them. This is possible because the instructions are validated before execution and the type of each value can be inferred from the instruction. +- TinyWasm has a explicit stack for values, labels and frames. This is mostly for simplicity in the implementation, but also allows for some optimizations. +- Floats always use a canonical NaN representation, the spec allows for multiple NaN representations. +- TinyWasm uses a custom bytecode format (see [Bytecode Format](#bytecode-format) for more details) +- Global state in the `Store` can be addressed from module instances other than the owning module. This is to allow more efficient access to imports and exports. Ownership is still enforced implicitly by requiring a reference to the instance to access it which can not be changed using the WebAssembly instructions. +- The `Store` is not thread-safe. This is to allow for more efficient access to the `Store` and its contents. When later adding support for threads, a `Mutex` can be used to make it thread-safe but the overhead of requiring a lock for every access is not necessary for single-threaded applications. +- TinyWasm is architectured to allow for a JIT compiler to be added later. Functions are stored as FunctionInstances which can contain either a `WasmFunction` or a `HostFunction`. A third variant `JitFunction` could be added later to store a pointer to the compiled function. This would allow for the JIT to be used transparently without changing the rest of the runtime. +- TinyWasm is designed to be used in `no_std` environments. The `std` feature is enabled by default, but can be disabled to remove the dependency on `std` and `std::io`. This is done by disabling the `std` and `parser` features. The `logging` feature can also be disabled to remove the dependency on `log`. This is not recommended, since `libm` is not as performant as the compiler's math intrinsics, especially on wasm32 targets, but can be useful for resource-constrained devices or other environments where `std` is not available such as OS kernels. +- Call Frames are executed in a loop instead of recursively. This allows the use of a single stack for all frames and makes it easier to pause execution and resume it later, or to step through the code one instruction at a time. +- While other interpreters convert `locals` to be register-based when parsing the function body, TinyWasm keeps them in a stack. This is mostly for simplicity in the implementation, but performance is still comparable or better than other interpreters. + +## Bytecode Format + +To improve performance and reduce code size, instructions are encoded as enum variants instead of opcodes. +This allows preprocessing the bytecode into a more compact format, which can be loaded directly into memory and executed without decoding later. This can skip the decoding step entirely on resource-constrained devices where memory is limited. See this [blog post](https://wasmer.io/posts/improving-with-zero-copy-deserialization) by Wasmer +for more details which inspired this design. + +Some instructions are split into multiple variants to reduce the size of the enum (e.g. `br_table` and `br_label`). +Additionally, label instructions contain offsets relative to the current instruction to make branching faster and easier to implement. +Also, `End` instructions are split into `End` and `EndBlock`. + +See [instructions.rs](./crates/types/src/instructions.rs) for the full list of instructions. + +This is a area that can still be improved. While being able to load pre-processes bytecode directly into memory is nice, in-place decoding could achieve similar speeds, see [A fast in-place interpreter for WebAssembly](https://arxiv.org/abs/2205.01183). diff --git a/BENCHMARKS.md b/BENCHMARKS.md new file mode 100644 index 0000000..78f05d7 --- /dev/null +++ b/BENCHMARKS.md @@ -0,0 +1,84 @@ +# Benchmark results + +All benchmarks are run on a Ryzen 7 5800X with 32GB of RAM, running Linux 6.6. +WebAssembly files are optimized using [wasm-opt](https://github.com/WebAssembly/binaryen), +and the benchmark code is available in the `benches` folder. + +These are mainly preliminary benchmarks, and I will be adding more in the future that are also looking into memory usage and other metrics. + +## WebAssembly Settings + +All WebAssembly files are compiled with the following settings: + +- `opt-level` is set to 3, `lto` is set to `thin`, `codegen-units` is set to 1. +- `reference-types`, `bulk-memory`, `mutable-globals` proposals are enabled. + +## Runtime Settings + +All runtimes are compiled with the following settings: + +- `unsafe` features are enabled. +- `opt-level` is set to 3, `lto` is set to `thin`, `codegen-units` is set to 1. + +## Versions + +- `tinywasm`: `0.4.0` +- `wasmi`: `0.31.0` +- `wasmer`: `4.2.0` + +## Results + +| Benchmark | Native | TinyWasm | Wasmi | Wasmer (Single Pass) | +| ------------ | ------ | -------- | -------- | -------------------- | +| `fib` | 6ns | 44.76µs | 48.96µs | 52µs | +| `fib-rec` | 284ns | 25.565ms | 5.11ms | 0.50ms | +| `argon2id` | 0.52ms | 110.08ms | 44.408ms | 4.76ms | +| `selfhosted` | 45µs | 2.18ms | 4.25ms | 258.87ms | + +### Fib + +The first benchmark is a simple optimized Fibonacci function, which is a good way to show the overhead of calling functions and parsing the bytecode. +TinyWasm is slightly faster than Wasmi here, but that's probably because of the overhead of parsing the bytecode, as TinyWasm uses a custom bytecode to pre-process the WebAssembly bytecode. + +### Fib-Rec + +This benchmark is a recursive Fibonacci function, which highlights some of the issues with the current implementation of TinyWasm's Call Stack. +TinyWasm is a lot slower here, but that's because there's currently no way to reuse the same Call Frame for recursive calls, so a new Call Frame is allocated for every call. This is not a problem for most programs, and the upcoming `tail-call` proposal will make this a lot easier to implement. + +### Argon2id + +This benchmark runs the Argon2id hashing algorithm, with 2 iterations, 1KB of memory, and 1 parallel lane. +I had to decrease the memory usage from the default to 1KB, because especially the interpreters were struggling to finish in a reasonable amount of time. +This is where `simd` instructions would be really useful, and it also highlights some of the issues with the current implementation of TinyWasm's Value Stack and Memory Instances. + +### Selfhosted + +This benchmark runs TinyWasm itself in the VM, and parses and executes the `print.wasm` example from the `examples` folder. +This is a good way to show some of TinyWasm's strengths - the code is quite large at 702KB and Wasmer struggles massively with it, even with the Single Pass compiler. I think it's a decent real-world performance benchmark, but it definitely favors TinyWasm a bit. + +Wasmer also offers a pre-parsed module format, so keep in mind that this number could be a bit lower if that was used (but probably still on the same order of magnitude). This number seems so high that I'm not sure if I'm doing something wrong, so I will be looking into this in the future. + +### Conclusion + +After profiling and fixing some low-hanging fruits, I found the biggest bottleneck to be Vector operations, especially for the Value Stack, and having shared access to Memory Instances using RefCell. These are the two areas I will be focusing on improving in the future, trying out Arena Allocation and other data structures to improve performance. Additionally, typed FuncHandles have a significant overhead over the untyped ones, so I will be looking into improving that as well. Still, I'm quite happy with the results, especially considering the use of standard Rust data structures. + +# Running benchmarks + +Benchmarks are run using [Criterion.rs](https://github.com/bheisler/criterion.rs). To run a benchmark, use the following command: + +```sh +$ cargo bench --bench +``` + +# Profiling + +To profile a benchmark, use the following command: + +```sh +$ cargo flamegraph --bench -- --bench +``` + +This will generate a flamegraph in `flamegraph.svg` and a `perf.data` file. +You can use [hotspot](https://github.com/KDAB/hotspot) to analyze the `perf.data` file. +Since a lot of functions are inlined, you probably want to remove the `#[inline]` attribute from the functions you care about. +Note that this will make the benchmark considerably slower, 2-10x slower in some cases. diff --git a/Cargo.lock b/Cargo.lock index b145eff..e147db6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,7 +8,7 @@ version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" dependencies = [ - "gimli", + "gimli 0.28.1", ] [[package]] @@ -52,6 +52,18 @@ dependencies = [ "libc", ] +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + +[[package]] +name = "anstyle" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2faccea4cc4ab4a667ce676a30e8ec13922a692c99bb8f5b11f1502c72e04220" + [[package]] name = "argh" version = "0.1.12" @@ -83,6 +95,24 @@ dependencies = [ "serde", ] +[[package]] +name = "argon2" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072" +dependencies = [ + "base64ct", + "blake2", + "cpufeatures", + "password-hash", +] + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "autocfg" version = "1.1.0" @@ -104,6 +134,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base64ct" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" + [[package]] name = "bitflags" version = "1.3.2" @@ -128,6 +164,15 @@ dependencies = [ "wyz", ] +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -159,7 +204,18 @@ version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627" dependencies = [ - "bytecheck_derive", + "bytecheck_derive 0.6.11", + "ptr_meta", + "simdutf8", +] + +[[package]] +name = "bytecheck" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41502630fe304ce54cbb2f8389e017784dee2b0328147779fcbe43b9db06d35d" +dependencies = [ + "bytecheck_derive 0.7.0", "ptr_meta", "simdutf8", ] @@ -175,11 +231,22 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "bytecheck_derive" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eda88c587085bc07dc201ab9df871bd9baa5e07f7754b745e4d7194b43ac1eda" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "bytemuck" -version = "1.14.0" +version = "1.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" +checksum = "ed2490600f404f2b94c167e31d3ed1d5f3c225a0f3b80230053b3e0b7b962bd9" [[package]] name = "byteorder" @@ -193,6 +260,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cc" version = "1.0.83" @@ -210,9 +283,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.32" +version = "0.4.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41daef31d7a747c5c847246f36de49ced6f7403b4cdabc807a97b5cc184cda7a" +checksum = "9f13690e35a5e4ace198e7beea2895d29f3a9cc55015fcebe6336bd2010af9eb" dependencies = [ "android-tzdata", "iana-time-zone", @@ -222,6 +295,58 @@ dependencies = [ "windows-targets 0.52.0", ] +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "clap" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" +dependencies = [ + "anstyle", + "clap_lex", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + [[package]] name = "color-eyre" version = "0.6.2" @@ -313,6 +438,19 @@ dependencies = [ "libc", ] +[[package]] +name = "corosensei" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80128832c58ea9cbd041d2a759ec449224487b2c1e400453d99d244eead87a8e" +dependencies = [ + "autocfg", + "cfg-if", + "libc", + "scopeguard", + "windows-sys 0.33.0", +] + [[package]] name = "cpufeatures" version = "0.2.12" @@ -322,6 +460,89 @@ dependencies = [ "libc", ] +[[package]] +name = "cranelift-bforest" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2ab4512dfd3a6f4be184403a195f76e81a8a9f9e6c898e19d2dc3ce20e0115" +dependencies = [ + "cranelift-entity", +] + +[[package]] +name = "cranelift-codegen" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98b022ed2a5913a38839dfbafe6cf135342661293b08049843362df4301261dc" +dependencies = [ + "arrayvec", + "bumpalo", + "cranelift-bforest", + "cranelift-codegen-meta", + "cranelift-codegen-shared", + "cranelift-egraph", + "cranelift-entity", + "cranelift-isle", + "gimli 0.26.2", + "log", + "regalloc2", + "smallvec", + "target-lexicon", +] + +[[package]] +name = "cranelift-codegen-meta" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "639307b45434ad112a98f8300c0f0ab085cbefcd767efcdef9ef19d4c0756e74" +dependencies = [ + "cranelift-codegen-shared", +] + +[[package]] +name = "cranelift-codegen-shared" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "278e52e29c53fcf32431ef08406c295699a70306d05a0715c5b1bf50e33a9ab7" + +[[package]] +name = "cranelift-egraph" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624b54323b06e675293939311943ba82d323bb340468ce1889be5da7932c8d73" +dependencies = [ + "cranelift-entity", + "fxhash", + "hashbrown 0.12.3", + "indexmap", + "log", + "smallvec", +] + +[[package]] +name = "cranelift-entity" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a59bcbca89c3f1b70b93ab3cbba5e5e0cbf3e63dadb23c7525cb142e21a9d4c" + +[[package]] +name = "cranelift-frontend" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d70abacb8cfef3dc8ff7e8836e9c1d70f7967dfdac824a4cd5e30223415aca6" +dependencies = [ + "cranelift-codegen", + "log", + "smallvec", + "target-lexicon", +] + +[[package]] +name = "cranelift-isle" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "393bc73c451830ff8dbb3a07f61843d6cb41a084f9996319917c0b291ed785bb" + [[package]] name = "crc32fast" version = "1.3.2" @@ -331,6 +552,82 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "criterion" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" +dependencies = [ + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "is-terminal", + "itertools", + "num-traits", + "once_cell", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-queue" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "crypto-common" version = "0.1.6" @@ -341,6 +638,64 @@ dependencies = [ "typenum", ] +[[package]] +name = "darling" +version = "0.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da01daa5f6d41c91358398e8db4dde38e292378da1f28300b59ef4732b879454" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f44f6238b948a3c6c3073cdf53bb0c2d5e024ee27e0f35bfe9d556a12395808a" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "darling_macro" +version = "0.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d2d88bd93979b1feb760a6b5c531ac5ba06bd63e74894c377af02faee07b9cd" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "dashmap" +version = "5.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +dependencies = [ + "cfg-if", + "hashbrown 0.14.3", + "lock_api", + "once_cell", + "parking_lot_core", +] + +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "digest" version = "0.10.7" @@ -349,6 +704,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -381,6 +737,12 @@ dependencies = [ "libloading", ] +[[package]] +name = "downcast-rs" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" + [[package]] name = "dwrote" version = "0.11.0" @@ -393,6 +755,79 @@ dependencies = [ "wio", ] +[[package]] +name = "dynasm" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "add9a102807b524ec050363f09e06f1504214b0e1c7797f64261c891022dce8b" +dependencies = [ + "bitflags 1.3.2", + "byteorder", + "lazy_static", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "dynasmrt" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64fba5a42bd76a17cad4bfa00de168ee1cbfa06a5e8ce992ae880218c05641a9" +dependencies = [ + "byteorder", + "dynasm", + "memmap2 0.5.10", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "enum-iterator" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eeac5c5edb79e4e39fe8439ef35207780a11f69c52cbe424ce3dfad4cb78de6" +dependencies = [ + "enum-iterator-derive", +] + +[[package]] +name = "enum-iterator-derive" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c134c37760b27a871ba422106eedbb8247da973a09e82558bf26d619c882b159" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "enumset" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "226c0da7462c13fb57e5cc9e0dc8f0635e7d27f276a3a7fd30054647f669007d" +dependencies = [ + "enumset_derive", +] + +[[package]] +name = "enumset_derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08b6c6ab82d70f08844964ba10c7babb716de2ecaeab9be5717918a5177d3af" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "env_logger" version = "0.10.2" @@ -426,6 +861,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + [[package]] name = "fdeflate" version = "0.3.4" @@ -451,6 +892,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bad48618fdb549078c333a7a8528acb57af271d0433bdecd523eb620628364e" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "font-kit" version = "0.11.0" @@ -491,6 +938,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + [[package]] name = "freetype" version = "0.7.1" @@ -518,6 +974,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -549,6 +1014,17 @@ dependencies = [ "weezl", ] +[[package]] +name = "gimli" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" +dependencies = [ + "fallible-iterator", + "indexmap", + "stable_deref_trait", +] + [[package]] name = "gimli" version = "0.28.1" @@ -568,6 +1044,16 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "half" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc52e53916c08643f1b56ec082790d1e86a32e58dc5268f897f313fbae7b4872" +dependencies = [ + "cfg-if", + "crunchy", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -577,6 +1063,12 @@ dependencies = [ "ahash", ] +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" + [[package]] name = "hermit-abi" version = "0.3.4" @@ -612,6 +1104,22 @@ dependencies = [ "cc", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "image" version = "0.24.8" @@ -632,6 +1140,16 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + [[package]] name = "indexmap-nostd" version = "0.4.0" @@ -649,6 +1167,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.10" @@ -721,18 +1248,64 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +[[package]] +name = "lock_api" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "mach" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" +dependencies = [ + "libc", +] + [[package]] name = "memchr" version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +[[package]] +name = "memmap2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" +dependencies = [ + "libc", +] + +[[package]] +name = "memmap2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d28bba84adfe6646737845bc5ebbfa2c08424eb1c37e94a1fd2a82adb56a872" +dependencies = [ + "libc", +] + +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -743,6 +1316,12 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "more-asserts" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7843ec2de400bcbc6a6328c958dc38e5359da6e93e72e37bc5246bf1ae776389" + [[package]] name = "num-traits" version = "0.2.17" @@ -767,6 +1346,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "oorandom" +version = "11.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" + [[package]] name = "owo-colors" version = "3.5.0" @@ -779,6 +1364,36 @@ version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f" +[[package]] +name = "parking_lot_core" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.48.5", +] + +[[package]] +name = "password-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" +dependencies = [ + "base64ct", + "rand_core", + "subtle", +] + +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + [[package]] name = "pathfinder_geometry" version = "0.5.1" @@ -798,6 +1413,12 @@ dependencies = [ "rustc_version", ] +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "pin-project-lite" version = "0.2.13" @@ -870,13 +1491,37 @@ dependencies = [ ] [[package]] -name = "pretty_env_logger" -version = "0.5.0" +name = "pretty_env_logger" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c" +dependencies = [ + "env_logger", + "log", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "env_logger", - "log", + "proc-macro2", + "quote", + "version_check", ] [[package]] @@ -923,6 +1568,32 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "rayon" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7237101a77a10773db45d62004a272517633fbcc3df19d96455ede1122e051" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -943,6 +1614,18 @@ dependencies = [ "thiserror", ] +[[package]] +name = "regalloc2" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300d4fbfb40c1c66a78ba3ddd41c1110247cf52f97b87d0f2fc9209bd49b030c" +dependencies = [ + "fxhash", + "log", + "slice-group-by", + "smallvec", +] + [[package]] name = "regex" version = "1.10.3" @@ -957,9 +1640,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.4" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b7fa1134405e2ec9353fd416b17f8dacd46c473d7d3fd1cf202706a14eb792a" +checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" dependencies = [ "aho-corasick", "memchr", @@ -972,13 +1655,25 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +[[package]] +name = "region" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76e189c2369884dce920945e2ddf79b3dff49e071a167dd1817fa9c4c00d512e" +dependencies = [ + "bitflags 1.3.2", + "libc", + "mach", + "winapi", +] + [[package]] name = "rend" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2571463863a6bd50c32f94402933f03457a3fbaf697a707c5be741e459f08fd" dependencies = [ - "bytecheck", + "bytecheck 0.6.11", ] [[package]] @@ -988,9 +1683,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "527a97cdfef66f65998b5f3b637c26f5a5ec09cc52a3f9932313ac645f4190f5" dependencies = [ "bitvec", - "bytecheck", + "bytecheck 0.6.11", "bytes", - "hashbrown", + "hashbrown 0.12.3", + "indexmap", "ptr_meta", "rend", "rkyv_derive", @@ -1045,13 +1741,6 @@ dependencies = [ "walkdir", ] -[[package]] -name = "rust-wasm-examples" -version = "0.0.0" -dependencies = [ - "tinywasm", -] - [[package]] name = "rustc-demangle" version = "0.1.23" @@ -1095,12 +1784,24 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "seahash" version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" +[[package]] +name = "self_cell" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58bf37232d3bb9a2c4e641ca2a11d83b5062066f88df7fed36c28772046d65ba" + [[package]] name = "semver" version = "1.0.21" @@ -1109,18 +1810,29 @@ checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" [[package]] name = "serde" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" +checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" dependencies = [ "serde_derive", ] +[[package]] +name = "serde-wasm-bindgen" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b4c031cd0d9014307d82b8abf653c0290fbdaeb4c02d00c63cf52f728628bf" +dependencies = [ + "js-sys", + "serde", + "wasm-bindgen", +] + [[package]] name = "serde_derive" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" +checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" dependencies = [ "proc-macro2", "quote", @@ -1129,9 +1841,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.111" +version = "1.0.113" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" +checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79" dependencies = [ "itoa", "ryu", @@ -1158,6 +1870,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shared-buffer" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6c99835bad52957e7aa241d3975ed17c1e5f8c92026377d117a606f36b84b16" +dependencies = [ + "bytes", + "memmap2 0.6.2", +] + [[package]] name = "simd-adler32" version = "0.3.7" @@ -1170,6 +1892,36 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +[[package]] +name = "slice-group-by" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "826167069c09b99d56f31e9ae5c99049e932a98c9dc2dac47645b08dbbf76ba7" + +[[package]] +name = "smallvec" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "syn" version = "1.0.109" @@ -1198,6 +1950,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "target-lexicon" +version = "0.12.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae" + [[package]] name = "termcolor" version = "1.4.1" @@ -1237,6 +1995,16 @@ dependencies = [ "once_cell", ] +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1254,7 +2022,7 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tinywasm" -version = "0.3.0" +version = "0.4.0" dependencies = [ "eyre", "libm", @@ -1267,24 +2035,24 @@ dependencies = [ "tinywasm-parser", "tinywasm-types", "wasm-testsuite", - "wast", + "wast 70.0.2", ] [[package]] name = "tinywasm-cli" -version = "0.3.0" +version = "0.4.0" dependencies = [ "argh", "color-eyre", "log", "pretty_env_logger", "tinywasm", - "wast", + "wast 70.0.2", ] [[package]] name = "tinywasm-parser" -version = "0.3.0" +version = "0.4.0" dependencies = [ "log", "tinywasm-types", @@ -1295,14 +2063,20 @@ dependencies = [ name = "tinywasm-root" version = "0.0.0" dependencies = [ + "argon2", "color-eyre", + "criterion", "tinywasm", + "wasmer", + "wasmi", + "wat", ] [[package]] name = "tinywasm-types" -version = "0.3.0" +version = "0.4.0" dependencies = [ + "bytecheck 0.7.0", "log", "rkyv", ] @@ -1314,9 +2088,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "tracing-core" version = "0.1.32" @@ -1360,18 +2146,44 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-width" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +[[package]] +name = "url" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + [[package]] name = "uuid" version = "1.7.0" @@ -1462,9 +2274,18 @@ checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" [[package]] name = "wasm-encoder" -version = "0.39.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "111495d6204760238512f57a9af162f45086504da332af210f2f75dd80b34f1d" +checksum = "1ba64e81215916eaeb48fee292f29401d69235d62d8b8fd92a7b2844ec5ae5f7" +dependencies = [ + "leb128", +] + +[[package]] +name = "wasm-encoder" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e09bca7d6388637d27fb5edbeab11f56bfabcef8743c55ae34370e1e5030a071" dependencies = [ "leb128", ] @@ -1476,6 +2297,197 @@ dependencies = [ "rust-embed", ] +[[package]] +name = "wasmer" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5467c7a23f9be04d5691590bea509dbea27e5ba5810d0020bef908456a495f33" +dependencies = [ + "bytes", + "cfg-if", + "derivative", + "indexmap", + "js-sys", + "more-asserts", + "rustc-demangle", + "serde", + "serde-wasm-bindgen", + "shared-buffer", + "target-lexicon", + "thiserror", + "wasm-bindgen", + "wasmer-compiler", + "wasmer-compiler-cranelift", + "wasmer-compiler-singlepass", + "wasmer-derive", + "wasmer-types", + "wasmer-vm", + "wat", + "winapi", +] + +[[package]] +name = "wasmer-compiler" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "510ad01a668d774f3a103a7c219bbc0970be93e8f1b27e2fdb48d1f4ccd1deff" +dependencies = [ + "backtrace", + "bytes", + "cfg-if", + "enum-iterator", + "enumset", + "lazy_static", + "leb128", + "memmap2 0.5.10", + "more-asserts", + "region", + "rkyv", + "self_cell", + "shared-buffer", + "smallvec", + "thiserror", + "wasmer-types", + "wasmer-vm", + "wasmparser", + "winapi", +] + +[[package]] +name = "wasmer-compiler-cranelift" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54bf93078990d83960d798de3c5935bddaba771fc2fefb9ed6bab9c0bbdea5c1" +dependencies = [ + "cranelift-codegen", + "cranelift-entity", + "cranelift-frontend", + "gimli 0.26.2", + "more-asserts", + "rayon", + "smallvec", + "target-lexicon", + "tracing", + "wasmer-compiler", + "wasmer-types", +] + +[[package]] +name = "wasmer-compiler-singlepass" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f4d6359d66a8bcefac26d48fcb0f3f0882bdf122b52121a1ae21f918706e040" +dependencies = [ + "byteorder", + "dynasm", + "dynasmrt", + "enumset", + "gimli 0.26.2", + "lazy_static", + "more-asserts", + "rayon", + "smallvec", + "wasmer-compiler", + "wasmer-types", +] + +[[package]] +name = "wasmer-derive" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b374fd34d97b1c091d8675f9bc472df52dc6787d139d3762d42c7dc84813a9b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "wasmer-types" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0caf1c87937b52aba8e9f920a278e1beda282f7439612c0b48f51a58e7a87bab" +dependencies = [ + "bytecheck 0.6.11", + "enum-iterator", + "enumset", + "indexmap", + "more-asserts", + "rkyv", + "target-lexicon", + "thiserror", +] + +[[package]] +name = "wasmer-vm" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58315c25492bc72a33f47a7d7fb0869a0106fc0164ec051e349a9e1eddba9a01" +dependencies = [ + "backtrace", + "cc", + "cfg-if", + "corosensei", + "crossbeam-queue", + "dashmap", + "derivative", + "enum-iterator", + "fnv", + "indexmap", + "lazy_static", + "libc", + "mach", + "memoffset", + "more-asserts", + "region", + "scopeguard", + "thiserror", + "wasmer-types", + "winapi", +] + +[[package]] +name = "wasmi" +version = "0.31.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8281d1d660cdf54c76a3efa9ddd0c270cada1383a995db3ccb43d166456c7" +dependencies = [ + "smallvec", + "spin", + "wasmi_arena", + "wasmi_core", + "wasmparser-nostd", +] + +[[package]] +name = "wasmi_arena" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "104a7f73be44570cac297b3035d76b169d6599637631cf37a1703326a0727073" + +[[package]] +name = "wasmi_core" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf1a7db34bff95b85c261002720c00c3a6168256dcb93041d3fa2054d19856a" +dependencies = [ + "downcast-rs", + "libm", + "num-traits", + "paste", +] + +[[package]] +name = "wasmparser" +version = "0.95.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ea896273ea99b15132414be1da01ab0d8836415083298ecaffbe308eaac87a" +dependencies = [ + "indexmap", + "url", +] + [[package]] name = "wasmparser-nostd" version = "0.100.1" @@ -1487,14 +2499,36 @@ dependencies = [ [[package]] name = "wast" -version = "70.0.0" +version = "64.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a259b226fd6910225aa7baeba82f9d9933b6d00f2ce1b49b80fa4214328237cc" +dependencies = [ + "leb128", + "memchr", + "unicode-width", + "wasm-encoder 0.32.0", +] + +[[package]] +name = "wast" +version = "70.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ee4bc54bbe1c6924160b9f75e374a1d07532e7580eb632c0ee6cdd109bb217e" +checksum = "a3d5061300042ff5065123dae1e27d00c03f567d34a2937c8472255148a216dc" dependencies = [ + "bumpalo", "leb128", "memchr", "unicode-width", - "wasm-encoder", + "wasm-encoder 0.41.0", +] + +[[package]] +name = "wat" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53253d920ab413fca1c7dc2161d601c79b4fdf631d0ba51dd4343bf9b556c3f6" +dependencies = [ + "wast 64.0.0", ] [[package]] @@ -1553,6 +2587,19 @@ dependencies = [ "windows-targets 0.52.0", ] +[[package]] +name = "windows-sys" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43dbb096663629518eb1dfa72d80243ca5a6aca764cae62a2df70af760a9be75" +dependencies = [ + "windows_aarch64_msvc 0.33.0", + "windows_i686_gnu 0.33.0", + "windows_i686_msvc 0.33.0", + "windows_x86_64_gnu 0.33.0", + "windows_x86_64_msvc 0.33.0", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -1613,6 +2660,12 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" +[[package]] +name = "windows_aarch64_msvc" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd761fd3eb9ab8cc1ed81e56e567f02dd82c4c837e48ac3b2181b9ffc5060807" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" @@ -1625,6 +2678,12 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" +[[package]] +name = "windows_i686_gnu" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab0cf703a96bab2dc0c02c0fa748491294bf9b7feb27e1f4f96340f208ada0e" + [[package]] name = "windows_i686_gnu" version = "0.48.5" @@ -1637,6 +2696,12 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" +[[package]] +name = "windows_i686_msvc" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cfdbe89cc9ad7ce618ba34abc34bbb6c36d99e96cae2245b7943cd75ee773d0" + [[package]] name = "windows_i686_msvc" version = "0.48.5" @@ -1649,6 +2714,12 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" +[[package]] +name = "windows_x86_64_gnu" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4dd9b0c0e9ece7bb22e84d70d01b71c6d6248b81a3c60d11869451b4cb24784" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" @@ -1673,6 +2744,12 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" +[[package]] +name = "windows_x86_64_msvc" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff1e4aa646495048ec7f3ffddc411e1d829c026a2ec62b39da15c1055e406eaa" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" diff --git a/Cargo.toml b/Cargo.toml index ee9a9df..49b73a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members=["crates/*", "examples/rust"] +members=["crates/*"] resolver="2" [profile.wasm] @@ -10,7 +10,7 @@ panic="abort" inherits="release" [workspace.package] -version="0.3.0" +version="0.4.0" edition="2021" license="MIT OR Apache-2.0" authors=["Henry Gressmann "] @@ -21,6 +21,35 @@ name="tinywasm-root" publish=false edition="2021" +[[example]] +name="wasm-rust" +test=false + +[[bench]] +name="selfhosted" +harness=false + +[[bench]] +name="fibonacci" +harness=false + + +[[bench]] +name="argon2id" +harness=false + +[profile.bench] +opt-level=3 +lto="thin" +codegen-units=1 +debug=true + [dev-dependencies] color-eyre="0.6" -tinywasm={path="crates/tinywasm"} +criterion={version="0.5", features=["html_reports"]} + +tinywasm={path="crates/tinywasm", features=["unsafe"]} +wat={version="1.0"} +wasmi={version="0.31", features=["std"]} +wasmer={version="4.2", features=["cranelift", "singlepass"]} +argon2={version="0.5"} diff --git a/README.md b/README.md index 5055889..da4d738 100644 --- a/README.md +++ b/README.md @@ -10,40 +10,47 @@ [![docs.rs](https://img.shields.io/docsrs/tinywasm?logo=rust)](https://docs.rs/tinywasm) [![Crates.io](https://img.shields.io/crates/v/tinywasm.svg?logo=rust)](https://crates.io/crates/tinywasm) [![Crates.io](https://img.shields.io/crates/l/tinywasm.svg)](./LICENSE-APACHE) -# Status +## Why TinyWasm? -TinyWasm, starting from version `0.3.0`, passes all the WebAssembly 1.0 tests in the [WebAssembly Test Suite](https://github.com/WebAssembly/testsuite). The 2.0 tests are in progress (notably `simd` and `bulk-memory-operations` are not implemented yet). This is enough to run most WebAssembly programs, including TinyWasm itself compiled to WebAssembly (see [examples/wasm-rust.rs](./examples/wasm-rust.rs)). +- **Tiny**: TinyWasm is designed to be as small as possible without significantly compromising performance or functionality. +- **Portable**: TinyWasm runs on any platform that LLVM supports, including WebAssembly itself, with minimal external dependencies. +- **Lightweight**: TinyWasm is easy to integrate and has a low call overhead, making it suitable for scripting and embedding. -Some APIs to interact with the runtime are not yet exposed, and the existing ones are subject to change, but the core functionality is mostly complete. -Results of the tests can be found [here](https://github.com/explodingcamera/tinywasm/tree/main/crates/tinywasm/tests/generated). +## Status -TinyWasm is not designed for performance, but rather for size and portability. However, it is still reasonably fast. -There are a couple of low-hanging fruits on the performance side, but they are not a priority at the moment. +As of version `0.3.0`, TinyWasm successfully passes all the WebAssembly 1.0 tests in the [WebAssembly Test Suite](https://github.com/WebAssembly/testsuite). Work on the 2.0 tests is ongoing. This achievement ensures that TinyWasm can run most WebAssembly programs, including versions of TinyWasm itself compiled to WebAssembly (see [examples/wasm-rust.rs](./examples/wasm-rust.rs)). The results of the testsuite are available [here](https://github.com/explodingcamera/tinywasm/tree/main/crates/tinywasm/tests/generated). + +The API is still unstable and may change at any time, so don't use it in production _yet_. Note that TinyWasm isn't primarily designed for high performance; its focus lies more on simplicity, size, and portability. More details on its performance aspects can be found in [BENCHMARKS.md](./BENCHMARKS.md). ## Supported Proposals -- [**Mutable Globals**](https://github.com/WebAssembly/mutable-global/blob/master/proposals/mutable-global/Overview.md) - **Fully implemented** -- [**Multi-value**](https://github.com/WebAssembly/spec/blob/master/proposals/multi-value/Overview.md) - **Fully implemented** -- [**Sign-extension operators**](https://github.com/WebAssembly/spec/blob/master/proposals/sign-extension-ops/Overview.md) - **Fully implemented** -- [**Reference Types**](https://github.com/WebAssembly/reference-types/blob/master/proposals/reference-types/Overview.md) - **_Partially implemented_** -- [**Multiple Memories**](https://github.com/WebAssembly/multi-memory/blob/master/proposals/multi-memory/Overview.md) - **_Partially implemented_** (not tested yet) -- [**Memory64**](https://github.com/WebAssembly/memory64/blob/master/proposals/memory64/Overview.md) - **_Partially implemented_** (only 32-bit addressing is supported at the moment, but larger memories can be created) +| Proposal | Implementation Status | Version | +| -------------------------------------------------------------------------------------------------------------------------- | --------------------- | ------- | +| [**Mutable Globals**](https://github.com/WebAssembly/mutable-global/blob/master/proposals/mutable-global/Overview.md) | Fully implemented | 0.2.0 | +| [**Multi-value**](https://github.com/WebAssembly/spec/blob/master/proposals/multi-value/Overview.md) | Fully implemented | 0.2.0 | +| [**Sign-extension operators**](https://github.com/WebAssembly/spec/blob/master/proposals/sign-extension-ops/Overview.md) | Fully implemented | 0.2.0 | +| [**Bulk Memory Operations**](https://github.com/WebAssembly/spec/blob/master/proposals/bulk-memory-operations/Overview.md) | Fully implemented | 0.4.0 | +| [**Reference Types**](https://github.com/WebAssembly/reference-types/blob/master/proposals/reference-types/Overview.md) | Partially implemented | N/A | +| [**Multiple Memories**](https://github.com/WebAssembly/multi-memory/blob/master/proposals/multi-memory/Overview.md) | Partially implemented | N/A | +| [**Memory64**](https://github.com/WebAssembly/memory64/blob/master/proposals/memory64/Overview.md) | Partially implemented | N/A | ## Usage TinyWasm can be used through the `tinywasm-cli` CLI tool or as a library in your Rust project. Documentation can be found [here](https://docs.rs/tinywasm). -### CLI +### Library ```sh -$ cargo install tinywasm-cli -$ tinywasm-cli --help +$ cargo add tinywasm ``` -### Library +### CLI + +The CLI is mainly available for testing purposes, but can also be used to run WebAssembly programs. ```sh -$ cargo add tinywasm +$ cargo install tinywasm-cli +$ tinywasm-cli --help ``` ## Feature Flags @@ -54,15 +61,15 @@ $ cargo add tinywasm Enables logging using the `log` crate. This is enabled by default. - **`parser`**\ Enables the `tinywasm-parser` crate. This is enabled by default. +- **`archive`**\ + Enables pre-parsing of archives. This is enabled by default. +- **`unsafe`**\ + Uses `unsafe` code to improve performance, particularly in Memory access With all these features disabled, TinyWasm only depends on `core`, `alloc` and `libm` and can be used in `no_std` environments. -Since `libm` is not as performant as the compiler's built-in math intrinsics, it is recommended to use the `std` feature if possible (at least [for now](https://github.com/rust-lang/rfcs/issues/2505)). - -## Performance - -> Benchmarks are coming soon. +Since `libm` is not as performant as the compiler's math intrinsics, it is recommended to use the `std` feature if possible (at least [for now](https://github.com/rust-lang/rfcs/issues/2505)), especially on wasm32 targets. -# 📄 License +## License Licensed under either of [Apache License, Version 2.0](./LICENSE-APACHE) or [MIT license](./LICENSE-MIT) at your option. diff --git a/benches/argon2id.rs b/benches/argon2id.rs new file mode 100644 index 0000000..4504812 --- /dev/null +++ b/benches/argon2id.rs @@ -0,0 +1,60 @@ +mod util; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use util::wasm_to_twasm; + +fn run_tinywasm(twasm: &[u8], params: (i32, i32, i32), name: &str) { + let (mut store, instance) = util::tinywasm(twasm); + let argon2 = instance.exported_func::<(i32, i32, i32), i32>(&store, name).expect("exported_func"); + argon2.call(&mut store, params).expect("call"); +} + +fn run_wasmi(wasm: &[u8], params: (i32, i32, i32), name: &str) { + let (module, mut store, linker) = util::wasmi(wasm); + let instance = linker.instantiate(&mut store, &module).expect("instantiate").start(&mut store).expect("start"); + let argon2 = instance.get_typed_func::<(i32, i32, i32), i32>(&mut store, name).expect("get_typed_func"); + argon2.call(&mut store, params).expect("call"); +} + +fn run_wasmer(wasm: &[u8], params: (i32, i32, i32), name: &str) { + use wasmer::Value; + let (mut store, instance) = util::wasmer(wasm); + let argon2 = instance.exports.get_function(name).expect("get_function"); + argon2.call(&mut store, &[Value::I32(params.0), Value::I32(params.1), Value::I32(params.2)]).expect("call"); +} + +fn run_native(params: (i32, i32, i32)) { + fn run_native(m_cost: i32, t_cost: i32, p_cost: i32) { + let password = b"password"; + let salt = b"some random salt"; + + let params = argon2::Params::new(m_cost as u32, t_cost as u32, p_cost as u32, None).unwrap(); + let argon = argon2::Argon2::new(argon2::Algorithm::Argon2id, argon2::Version::V0x13, params); + + let mut hash = [0u8; 32]; + argon.hash_password_into(password, salt, &mut hash).unwrap(); + } + run_native(params.0, params.1, params.2) +} + +const ARGON2ID: &[u8] = include_bytes!("../examples/rust/out/argon2id.wasm"); +fn criterion_benchmark(c: &mut Criterion) { + let twasm = wasm_to_twasm(ARGON2ID); + let params = (1000, 2, 1); + + let mut group = c.benchmark_group("argon2id"); + group.measurement_time(std::time::Duration::from_secs(7)); + group.sample_size(10); + + group.bench_function("native", |b| b.iter(|| run_native(black_box(params)))); + group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(params), "argon2id"))); + group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&ARGON2ID, black_box(params), "argon2id"))); + group.bench_function("wasmer", |b| b.iter(|| run_wasmer(&ARGON2ID, black_box(params), "argon2id"))); +} + +criterion_group!( + name = benches; + config = Criterion::default().significance_level(0.1); + targets = criterion_benchmark +); + +criterion_main!(benches); diff --git a/benches/fibonacci.rs b/benches/fibonacci.rs new file mode 100644 index 0000000..1d1134a --- /dev/null +++ b/benches/fibonacci.rs @@ -0,0 +1,76 @@ +mod util; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use util::wasm_to_twasm; + +fn run_tinywasm(twasm: &[u8], iterations: i32, name: &str) { + let (mut store, instance) = util::tinywasm(twasm); + let fib = instance.exported_func::(&store, name).expect("exported_func"); + fib.call(&mut store, iterations).expect("call"); +} + +fn run_wasmi(wasm: &[u8], iterations: i32, name: &str) { + let (module, mut store, linker) = util::wasmi(wasm); + let instance = linker.instantiate(&mut store, &module).expect("instantiate").start(&mut store).expect("start"); + let fib = instance.get_typed_func::(&mut store, name).expect("get_typed_func"); + fib.call(&mut store, iterations).expect("call"); +} + +fn run_wasmer(wasm: &[u8], iterations: i32, name: &str) { + use wasmer::*; + let engine: Engine = wasmer::Singlepass::default().into(); + let mut store = Store::default(); + let import_object = imports! {}; + let module = wasmer::Module::from_binary(&engine, &wasm).expect("wasmer::Module::from_binary"); + let instance = Instance::new(&mut store, &module, &import_object).expect("Instance::new"); + let fib = instance.exports.get_typed_function::(&mut store, name).expect("get_function"); + fib.call(&mut store, iterations).expect("call"); +} + +fn run_native(n: i32) -> i32 { + let mut sum = 0; + let mut last = 0; + let mut curr = 1; + for _i in 1..n { + sum = last + curr; + last = curr; + curr = sum; + } + sum +} + +fn run_native_recursive(n: i32) -> i32 { + if n <= 1 { + return n; + } + run_native_recursive(n - 1) + run_native_recursive(n - 2) +} + +const FIBONACCI: &[u8] = include_bytes!("../examples/rust/out/fibonacci.wasm"); +fn criterion_benchmark(c: &mut Criterion) { + let twasm = wasm_to_twasm(FIBONACCI); + + { + let mut group = c.benchmark_group("fibonacci"); + group.bench_function("native", |b| b.iter(|| run_native(black_box(60)))); + group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(60), "fibonacci"))); + group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&FIBONACCI, black_box(60), "fibonacci"))); + group.bench_function("wasmer", |b| b.iter(|| run_wasmer(&FIBONACCI, black_box(60), "fibonacci"))); + } + + { + let mut group = c.benchmark_group("fibonacci-recursive"); + group.measurement_time(std::time::Duration::from_secs(5)); + group.bench_function("native", |b| b.iter(|| run_native_recursive(black_box(26)))); + group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(26), "fibonacci_recursive"))); + group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&FIBONACCI, black_box(26), "fibonacci_recursive"))); + group.bench_function("wasmer", |b| b.iter(|| run_wasmer(&FIBONACCI, black_box(26), "fibonacci_recursive"))); + } +} + +criterion_group!( + name = benches; + config = Criterion::default().significance_level(0.1); + targets = criterion_benchmark +); + +criterion_main!(benches); diff --git a/benches/selfhosted.rs b/benches/selfhosted.rs new file mode 100644 index 0000000..57146e7 --- /dev/null +++ b/benches/selfhosted.rs @@ -0,0 +1,72 @@ +mod util; +use criterion::{criterion_group, criterion_main, Criterion}; + +use crate::util::twasm_to_module; + +fn run_native() { + use tinywasm::*; + let module = tinywasm::Module::parse_bytes(include_bytes!("../examples/rust/out/print.wasm")).expect("parse"); + let mut store = Store::default(); + let mut imports = Imports::default(); + imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _: i32| Ok(()))).expect("define"); + let instance = ModuleInstance::instantiate(&mut store, module, Some(imports)).expect("instantiate"); + let hello = instance.exported_func::<(i32, i32), ()>(&store, "add_and_print").expect("exported_func"); + hello.call(&mut store, (2, 3)).expect("call"); +} + +fn run_tinywasm(twasm: &[u8]) { + use tinywasm::*; + let module = twasm_to_module(twasm); + let mut store = Store::default(); + let mut imports = Imports::default(); + imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _: i32| Ok(()))).expect("define"); + let instance = ModuleInstance::instantiate(&mut store, module, Some(imports)).expect("instantiate"); + let hello = instance.exported_func::<(), ()>(&store, "hello").expect("exported_func"); + hello.call(&mut store, ()).expect("call"); +} + +fn run_wasmi(wasm: &[u8]) { + use wasmi::*; + let engine = Engine::default(); + let module = wasmi::Module::new(&engine, wasm).expect("wasmi::Module::new"); + let mut store = Store::new(&engine, ()); + let mut linker = >::new(&engine); + linker.define("env", "printi32", Func::wrap(&mut store, |_: Caller<'_, ()>, _: i32| {})).expect("define"); + let instance = linker.instantiate(&mut store, &module).expect("instantiate").start(&mut store).expect("start"); + let hello = instance.get_typed_func::<(), ()>(&mut store, "hello").expect("get_typed_func"); + hello.call(&mut store, ()).expect("call"); +} + +fn run_wasmer(wasm: &[u8]) { + use wasmer::*; + let engine = wasmer::Engine::default(); + let mut store = Store::default(); + let import_object = imports! { + "env" => { + "printi32" => Function::new_typed(&mut store, |_: i32| {}), + }, + }; + let module = wasmer::Module::from_binary(&engine, &wasm).expect("wasmer::Module::from_binary"); + let instance = Instance::new(&mut store, &module, &import_object).expect("Instance::new"); + let hello = instance.exports.get_function("hello").expect("get_function"); + hello.call(&mut store, &[]).expect("call"); +} + +const TINYWASM: &[u8] = include_bytes!("../examples/rust/out/tinywasm.wasm"); +fn criterion_benchmark(c: &mut Criterion) { + let twasm = util::wasm_to_twasm(TINYWASM); + + let mut group = c.benchmark_group("selfhosted"); + group.bench_function("native", |b| b.iter(|| run_native())); + group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm))); + group.bench_function("wasmi", |b| b.iter(|| run_wasmi(TINYWASM))); + group.bench_function("wasmer", |b| b.iter(|| run_wasmer(TINYWASM))); +} + +criterion_group!( + name = benches; + config = Criterion::default().sample_size(100).measurement_time(std::time::Duration::from_secs(5)).significance_level(0.1); + targets = criterion_benchmark +); + +criterion_main!(benches); diff --git a/benches/util/mod.rs b/benches/util/mod.rs new file mode 100644 index 0000000..03e2075 --- /dev/null +++ b/benches/util/mod.rs @@ -0,0 +1,42 @@ +#![allow(dead_code)] + +use tinywasm::{self, parser::Parser, types::TinyWasmModule}; + +pub fn wasm_to_twasm(wasm: &[u8]) -> Vec { + let parser = Parser::new(); + let res = parser.parse_module_bytes(wasm).expect("parse_module_bytes"); + res.serialize_twasm().to_vec() +} + +#[inline] +pub fn twasm_to_module(twasm: &[u8]) -> tinywasm::Module { + unsafe { TinyWasmModule::from_twasm_unchecked(&twasm) }.into() +} + +pub fn tinywasm(twasm: &[u8]) -> (tinywasm::Store, tinywasm::ModuleInstance) { + use tinywasm::*; + let module = twasm_to_module(twasm); + let mut store = Store::default(); + let imports = Imports::default(); + let instance = ModuleInstance::instantiate(&mut store, module, Some(imports)).expect("instantiate"); + (store, instance) +} + +pub fn wasmi(wasm: &[u8]) -> (wasmi::Module, wasmi::Store<()>, wasmi::Linker<()>) { + use wasmi::*; + let engine = Engine::default(); + let module = wasmi::Module::new(&engine, wasm).expect("wasmi::Module::new"); + let store = Store::new(&engine, ()); + let linker = >::new(&engine); + (module, store, linker) +} + +pub fn wasmer(wasm: &[u8]) -> (wasmer::Store, wasmer::Instance) { + use wasmer::*; + let compiler = Singlepass::default(); + let mut store = Store::new(compiler); + let import_object = imports! {}; + let module = Module::new(&store, wasm).expect("wasmer::Module::new"); + let instance = Instance::new(&mut store, &module, &import_object).expect("Instance::new"); + (store, instance) +} diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 9cde64e..11914ef 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -14,7 +14,7 @@ path="src/bin.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tinywasm={version="0.3.0-alpha.0", path="../tinywasm", features=["std", "parser"]} +tinywasm={version="0.4.0", path="../tinywasm", features=["std", "parser"]} argh="0.1" color-eyre={version="0.6", default-features=false} log="0.4" diff --git a/crates/cli/README.md b/crates/cli/README.md new file mode 100644 index 0000000..1f7a1bb --- /dev/null +++ b/crates/cli/README.md @@ -0,0 +1,10 @@ +# `tinywasm-cli` + +The `tinywasm-cli` crate contains the command line interface for the `tinywasm` project. See [`tinywasm`](https://crates.io/crates/tinywasm) for more information. + +## Usage + +```bash +$ cargo install tinywasm-cli +$ tinywasm-cli --help +``` diff --git a/crates/cli/src/bin.rs b/crates/cli/src/bin.rs index 1c166cf..34d4bcd 100644 --- a/crates/cli/src/bin.rs +++ b/crates/cli/src/bin.rs @@ -112,7 +112,7 @@ fn run(module: Module, func: Option, args: Vec) -> Result<()> let instance = module.instantiate(&mut store, None)?; if let Some(func) = func { - let func = instance.exported_func_by_name(&store, &func)?; + let func = instance.exported_func_untyped(&store, &func)?; let res = func.call(&mut store, &args)?; info!("{res:?}"); } diff --git a/crates/parser/Cargo.toml b/crates/parser/Cargo.toml index 1592f85..0fee8e1 100644 --- a/crates/parser/Cargo.toml +++ b/crates/parser/Cargo.toml @@ -11,9 +11,9 @@ repository.workspace=true # fork of wasmparser with no_std support, see https://github.com/bytecodealliance/wasmtime/issues/3495 wasmparser={version="0.100", package="wasmparser-nostd", default-features=false} log={version="0.4", optional=true} -tinywasm-types={version="0.3.0-alpha.0", path="../types"} +tinywasm-types={version="0.4.0", path="../types", default-features=false} [features] default=["std", "logging"] logging=["log"] -std=[] +std=["tinywasm-types/std"] diff --git a/crates/parser/README.md b/crates/parser/README.md index 6cf2234..8ac7a30 100644 --- a/crates/parser/README.md +++ b/crates/parser/README.md @@ -1,6 +1,6 @@ # `tinywasm-parser` -This crate provides a parser that can parse WebAssembly modules into a TinyWasm module. It is based on +This crate provides a parser that can parse WebAssembly modules into a TinyWasm module. It is based on [`wasmparser_nostd`](https://crates.io/crates/wasmparser_nostd) and used by [`tinywasm`](https://crates.io/crates/tinywasm). ## Features @@ -11,11 +11,11 @@ This crate provides a parser that can parse WebAssembly modules into a TinyWasm ## Usage ```rust -use tinywasm_parser::{Parser, TinyWasmModule}; +use tinywasm_parser::Parser; let bytes = include_bytes!("./file.wasm"); let parser = Parser::new(); -let module: TinyWasmModule = parser.parse_module_bytes(bytes).unwrap(); -let mudule: TinyWasmModule = parser.parse_module_file("path/to/file.wasm").unwrap(); -let module: TinyWasmModule = parser.parse_module_stream(&mut stream).unwrap(); +let module = parser.parse_module_bytes(bytes).unwrap(); +let mudule = parser.parse_module_file("path/to/file.wasm").unwrap(); +let module = parser.parse_module_stream(&mut stream).unwrap(); ``` diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index 835842f..7c10d62 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -107,7 +107,6 @@ pub(crate) fn convert_module_tables Result> { let table_type = table_types.into_iter().map(|table| convert_module_table(table?)).collect::>>()?; - Ok(table_type) } @@ -332,6 +331,12 @@ pub fn process_operators<'a>( GlobalSet { global_index } => Instruction::GlobalSet(global_index), MemorySize { mem, mem_byte } => Instruction::MemorySize(mem, mem_byte), MemoryGrow { mem, mem_byte } => Instruction::MemoryGrow(mem, mem_byte), + + MemoryCopy { dst_mem, src_mem } => Instruction::MemoryCopy(src_mem, dst_mem), + MemoryFill { mem } => Instruction::MemoryFill(mem), + MemoryInit { data_index, mem } => Instruction::MemoryInit(data_index, mem), + DataDrop { data_index } => Instruction::DataDrop(data_index), + I32Const { value } => Instruction::I32Const(value), I64Const { value } => Instruction::I64Const(value), F32Const { value } => Instruction::F32Const(f32::from_bits(value.bits())), diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index edcd280..c608232 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -25,7 +25,7 @@ mod module; use alloc::{string::ToString, vec::Vec}; pub use error::*; use module::ModuleReader; -use tinywasm_types::WasmFunction; +use tinywasm_types::{TypedWasmFunction, WasmFunction}; use wasmparser::Validator; pub use tinywasm_types::TinyWasmModule; @@ -116,19 +116,13 @@ impl TryFrom for TinyWasmModule { .code .into_iter() .zip(code_type_addrs) - .map(|(f, ty_idx)| { - ( - ty_idx, - WasmFunction { - instructions: f.body, - locals: f.locals, - ty: reader - .func_types - .get(ty_idx as usize) - .expect("No func type for func, this is a bug") - .clone(), - }, - ) + .map(|(f, ty_idx)| TypedWasmFunction { + type_addr: ty_idx, + wasm_function: WasmFunction { + instructions: f.body, + locals: f.locals, + ty: reader.func_types.get(ty_idx as usize).expect("No func type for func, this is a bug").clone(), + }, }) .collect::>(); diff --git a/crates/parser/src/module.rs b/crates/parser/src/module.rs index e0fb0cc..f5c01ac 100644 --- a/crates/parser/src/module.rs +++ b/crates/parser/src/module.rs @@ -5,7 +5,7 @@ use core::fmt::Debug; use tinywasm_types::{Data, Element, Export, FuncType, Global, Import, Instruction, MemoryType, TableType, ValType}; use wasmparser::{Payload, Validator}; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone)] pub struct CodeSection { pub locals: Box<[ValType]>, pub body: Box<[Instruction]>, @@ -112,7 +112,6 @@ impl ModuleReader { if !self.table_types.is_empty() { return Err(ParseError::DuplicateSection("Table section".into())); } - debug!("Found table section"); validator.table_section(&reader)?; self.table_types = conversion::convert_module_tables(reader)?; @@ -140,6 +139,13 @@ impl ModuleReader { validator.data_section(&reader)?; self.data = conversion::convert_module_data_sections(reader)?; } + DataCountSection { count, range } => { + debug!("Found data count section"); + if !self.data.is_empty() { + return Err(ParseError::DuplicateSection("Data count section".into())); + } + validator.data_count_section(count, &range)?; + } CodeSectionStart { count, range, .. } => { debug!("Found code section ({} functions)", count); if !self.code.is_empty() { diff --git a/crates/tinywasm/Cargo.toml b/crates/tinywasm/Cargo.toml index f1a3242..ad58b9e 100644 --- a/crates/tinywasm/Cargo.toml +++ b/crates/tinywasm/Cargo.toml @@ -14,8 +14,8 @@ path="src/lib.rs" [dependencies] log={version="0.4", optional=true} -tinywasm-parser={version="0.3.0-alpha.0", path="../parser", default-features=false, optional=true} -tinywasm-types={version="0.3.0-alpha.0", path="../types", default-features=false} +tinywasm-parser={version="0.4.0", path="../parser", default-features=false, optional=true} +tinywasm-types={version="0.4.0", path="../types", default-features=false} libm={version="0.2", default-features=false} [dev-dependencies] @@ -29,10 +29,12 @@ plotters={version="0.3"} pretty_env_logger="0.5" [features] -default=["std", "parser", "logging"] -logging=["log", "tinywasm-types/logging", "tinywasm-parser?/logging"] +default=["std", "parser", "logging", "archive"] +logging=["log", "tinywasm-parser?/logging", "tinywasm-types/logging"] std=["tinywasm-parser?/std", "tinywasm-types/std"] parser=["tinywasm-parser"] +unsafe=["tinywasm-types/unsafe"] +archive=["tinywasm-types/archive"] [[test]] name="generate-charts" diff --git a/crates/tinywasm/src/export.rs b/crates/tinywasm/src/export.rs deleted file mode 100644 index 8b13789..0000000 --- a/crates/tinywasm/src/export.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/crates/tinywasm/src/func.rs b/crates/tinywasm/src/func.rs index 8433236..2088494 100644 --- a/crates/tinywasm/src/func.rs +++ b/crates/tinywasm/src/func.rs @@ -1,17 +1,17 @@ -use crate::log; +use crate::{log, runtime::RawWasmValue, unlikely, Function}; use alloc::{boxed::Box, format, string::String, string::ToString, vec, vec::Vec}; -use tinywasm_types::{FuncAddr, FuncType, ValType, WasmValue}; +use tinywasm_types::{FuncType, ModuleInstanceAddr, ValType, WasmValue}; use crate::{ runtime::{CallFrame, Stack}, - Error, FuncContext, ModuleInstance, Result, Store, + Error, FuncContext, Result, Store, }; #[derive(Debug)] /// A function handle pub struct FuncHandle { - pub(crate) module: ModuleInstance, - pub(crate) addr: FuncAddr, + pub(crate) module_addr: ModuleInstanceAddr, + pub(crate) addr: u32, pub(crate) ty: FuncType, /// The name of the function, if it has one @@ -22,19 +22,13 @@ impl FuncHandle { /// Call a function (Invocation) /// /// See + #[inline] pub fn call(&self, store: &mut Store, params: &[WasmValue]) -> Result> { - let mut stack = Stack::default(); - - // 1. Assert: funcs[func_addr] exists - // 2. let func_inst be the functiuon instance funcs[func_addr] - let func_inst = store.get_func(self.addr as usize)?.clone(); - // 3. Let func_ty be the function type let func_ty = &self.ty; // 4. If the length of the provided argument values is different from the number of expected arguments, then fail - if func_ty.params.len() != params.len() { - log::info!("func_ty.params: {:?}", func_ty.params); + if unlikely(func_ty.params.len() != params.len()) { return Err(Error::Other(format!( "param count mismatch: expected {}, got {}", func_ty.params.len(), @@ -43,31 +37,34 @@ impl FuncHandle { } // 5. For each value type and the corresponding value, check if types match - for (i, (ty, param)) in func_ty.params.iter().zip(params).enumerate() { + if !unlikely(func_ty.params.iter().zip(params).enumerate().all(|(i, (ty, param))| { if ty != ¶m.val_type() { - return Err(Error::Other(format!( - "param type mismatch at index {}: expected {:?}, got {:?}", - i, ty, param - ))); + log::error!("param type mismatch at index {}: expected {:?}, got {:?}", i, ty, param); + false + } else { + true } + })) { + return Err(Error::Other("Type mismatch".into())); } - let locals = match &func_inst.func { - crate::Function::Host(h) => { - let func = h.func.clone(); - let ctx = FuncContext { store, module: &self.module }; + let func_inst = store.get_func(self.addr as usize)?; + let wasm_func = match &func_inst.func { + Function::Host(host_func) => { + let func = &host_func.clone().func; + let ctx = FuncContext { store, module_addr: self.module_addr }; return (func)(ctx, params); } - crate::Function::Wasm(ref f) => f.locals.to_vec(), + Function::Wasm(wasm_func) => wasm_func, }; // 6. Let f be the dummy frame - log::debug!("locals: {:?}", locals); - let call_frame = CallFrame::new(func_inst, params, locals); + let call_frame = + CallFrame::new(wasm_func.clone(), func_inst.owner, params.iter().map(|v| RawWasmValue::from(*v))); // 7. Push the frame f to the call stack // & 8. Push the values to the stack (Not needed since the call frame owns the values) - stack.call_stack.push(call_frame)?; + let mut stack = Stack::new(call_frame); // 9. Invoke the function instance let runtime = store.runtime(); @@ -100,7 +97,7 @@ pub trait IntoWasmValueTuple { } pub trait FromWasmValueTuple { - fn from_wasm_value_tuple(values: Vec) -> Result + fn from_wasm_value_tuple(values: &[WasmValue]) -> Result where Self: Sized; } @@ -115,7 +112,7 @@ impl FuncHandleTyped { let result = self.func.call(store, &wasm_values)?; // Convert the Vec back to R - R::from_wasm_value_tuple(result) + R::from_wasm_value_tuple(&result) } } macro_rules! impl_into_wasm_value_tuple { @@ -125,6 +122,7 @@ macro_rules! impl_into_wasm_value_tuple { $($T: Into),* { #[allow(non_snake_case)] + #[inline] fn into_wasm_value_tuple(self) -> Vec { let ($($T,)*) = self; vec![$($T.into(),)*] @@ -136,6 +134,7 @@ macro_rules! impl_into_wasm_value_tuple { macro_rules! impl_into_wasm_value_tuple_single { ($T:ident) => { impl IntoWasmValueTuple for $T { + #[inline] fn into_wasm_value_tuple(self) -> Vec { vec![self.into()] } @@ -164,14 +163,15 @@ macro_rules! impl_from_wasm_value_tuple { where $($T: TryFrom),* { - fn from_wasm_value_tuple(values: Vec) -> Result { + #[inline] + fn from_wasm_value_tuple(values: &[WasmValue]) -> Result { #[allow(unused_variables, unused_mut)] - let mut iter = values.into_iter(); + let mut iter = values.iter(); Ok(( $( $T::try_from( - iter.next() + *iter.next() .ok_or(Error::Other("Not enough values in WasmValue vector".to_string()))? ) .map_err(|e| Error::Other(format!("FromWasmValueTuple: Could not convert WasmValue to expected type: {:?}", e, @@ -186,10 +186,11 @@ macro_rules! impl_from_wasm_value_tuple { macro_rules! impl_from_wasm_value_tuple_single { ($T:ident) => { impl FromWasmValueTuple for $T { - fn from_wasm_value_tuple(values: Vec) -> Result { + #[inline] + fn from_wasm_value_tuple(values: &[WasmValue]) -> Result { #[allow(unused_variables, unused_mut)] - let mut iter = values.into_iter(); - $T::try_from(iter.next().ok_or(Error::Other("Not enough values in WasmValue vector".to_string()))?) + let mut iter = values.iter(); + $T::try_from(*iter.next().ok_or(Error::Other("Not enough values in WasmValue vector".to_string()))?) .map_err(|e| { Error::Other(format!( "FromWasmValueTupleSingle: Could not convert WasmValue to expected type: {:?}", @@ -254,6 +255,7 @@ macro_rules! impl_val_types_from_tuple { where $($t: ToValType,)+ { + #[inline] fn val_types() -> Box<[ValType]> { Box::new([$($t::to_val_type(),)+]) } @@ -262,6 +264,7 @@ macro_rules! impl_val_types_from_tuple { } impl ValTypesFromTuple for () { + #[inline] fn val_types() -> Box<[ValType]> { Box::new([]) } @@ -271,6 +274,7 @@ impl ValTypesFromTuple for T1 where T1: ToValType, { + #[inline] fn val_types() -> Box<[ValType]> { Box::new([T1::to_val_type()]) } diff --git a/crates/tinywasm/src/imports.rs b/crates/tinywasm/src/imports.rs index a640081..e273838 100644 --- a/crates/tinywasm/src/imports.rs +++ b/crates/tinywasm/src/imports.rs @@ -7,9 +7,10 @@ use crate::{ log, LinkingError, Result, }; use alloc::{ + boxed::Box, collections::BTreeMap, + rc::Rc, string::{String, ToString}, - sync::Arc, vec::Vec, }; use tinywasm_types::*; @@ -18,10 +19,10 @@ use tinywasm_types::*; #[derive(Debug, Clone)] pub enum Function { /// A host function - Host(HostFunction), + Host(Rc), /// A function defined in WebAssembly - Wasm(WasmFunction), + Wasm(Rc), } impl Function { @@ -34,7 +35,6 @@ impl Function { } /// A host function -#[derive(Clone)] pub struct HostFunction { pub(crate) ty: tinywasm_types::FuncType, pub(crate) func: HostFuncInner, @@ -52,30 +52,39 @@ impl HostFunction { } } -pub(crate) type HostFuncInner = - Arc, &[WasmValue]) -> Result> + 'static + Send + Sync>; +pub(crate) type HostFuncInner = Box, &[WasmValue]) -> Result>>; /// The context of a host-function call #[derive(Debug)] pub struct FuncContext<'a> { pub(crate) store: &'a mut crate::Store, - pub(crate) module: &'a crate::ModuleInstance, + pub(crate) module_addr: ModuleInstanceAddr, } impl FuncContext<'_> { - /// Get a mutable reference to the store - pub fn store_mut(&mut self) -> &mut crate::Store { + /// Get a reference to the store + pub fn store(&self) -> &crate::Store { self.store } - /// Get a reference to the store - pub fn store(&self) -> &crate::Store { + /// Get a mutable reference to the store + pub fn store_mut(&mut self) -> &mut crate::Store { self.store } /// Get a reference to the module instance - pub fn module(&self) -> &crate::ModuleInstance { - self.module + pub fn module(&self) -> crate::ModuleInstance { + self.store.get_module_instance_raw(self.module_addr) + } + + /// Get a reference to an exported memory + pub fn exported_memory(&mut self, name: &str) -> Result> { + self.module().exported_memory(self.store, name) + } + + /// Get a reference to an exported memory + pub fn exported_memory_mut(&mut self, name: &str) -> Result> { + self.module().exported_memory_mut(self.store, name) } } @@ -134,31 +143,28 @@ impl Extern { /// Create a new function import pub fn func( ty: &tinywasm_types::FuncType, - func: impl Fn(FuncContext<'_>, &[WasmValue]) -> Result> + 'static + Send + Sync, + func: impl Fn(FuncContext<'_>, &[WasmValue]) -> Result> + 'static, ) -> Self { - let inner_func = move |ctx: FuncContext<'_>, args: &[WasmValue]| { - let args = args.to_vec(); - func(ctx, &args) - }; - - Self::Function(Function::Host(HostFunction { func: Arc::new(inner_func), ty: ty.clone() })) + Self::Function(Function::Host(Rc::new(HostFunction { func: Box::new(func), ty: ty.clone() }))) } /// Create a new typed function import - pub fn typed_func(func: impl Fn(FuncContext<'_>, P) -> Result + 'static + Send + Sync) -> Self + // TODO: currently, this is slower than `Extern::func` because of the type conversions. + // we should be able to optimize this and make it even faster than `Extern::func`. + pub fn typed_func(func: impl Fn(FuncContext<'_>, P) -> Result + 'static) -> Self where P: FromWasmValueTuple + ValTypesFromTuple, R: IntoWasmValueTuple + ValTypesFromTuple + Debug, { let inner_func = move |ctx: FuncContext<'_>, args: &[WasmValue]| -> Result> { - let args = P::from_wasm_value_tuple(args.to_vec())?; + let args = P::from_wasm_value_tuple(args)?; let result = func(ctx, args)?; - Ok(result.into_wasm_value_tuple()) + Ok(result.into_wasm_value_tuple().to_vec()) }; let ty = tinywasm_types::FuncType { params: P::val_types(), results: R::val_types() }; - Self::Function(Function::Host(HostFunction { func: Arc::new(inner_func), ty })) + Self::Function(Function::Host(Rc::new(HostFunction { func: Box::new(inner_func), ty }))) } pub(crate) fn kind(&self) -> ExternalKind { @@ -250,6 +256,13 @@ impl Imports { Imports { values: BTreeMap::new(), modules: BTreeMap::new() } } + /// Merge two import sets + pub fn merge(mut self, other: Self) -> Self { + self.values.extend(other.values); + self.modules.extend(other.modules); + self + } + /// Link a module /// /// This will automatically link all imported values on instantiation @@ -273,24 +286,19 @@ impl Imports { if let Some(v) = self.values.get(&name) { return Some(ResolvedExtern::Extern(v.clone())); } - if let Some(addr) = self.modules.get(&name.module) { let instance = store.get_module_instance(*addr)?; - return Some(ResolvedExtern::Store(instance.export(&import.name)?)); + return Some(ResolvedExtern::Store(instance.export_addr(&import.name)?)); } None } - fn compare_types(import: &Import, actual: &T, expected: &T) -> Result<()> - where - T: Debug + PartialEq, - { + fn compare_types(import: &Import, actual: &T, expected: &T) -> Result<()> { if expected != actual { log::error!("failed to link import {}, expected {:?}, got {:?}", import.name, expected, actual); return Err(LinkingError::incompatible_import_type(import).into()); } - Ok(()) } @@ -320,22 +328,20 @@ impl Imports { ) -> Result<()> { Self::compare_types(import, &expected.arch, &actual.arch)?; - if actual.page_count_initial > expected.page_count_initial { - if let Some(real_size) = real_size { - if actual.page_count_initial > real_size as u64 { - return Err(LinkingError::incompatible_import_type(import).into()); - } - } else { - return Err(LinkingError::incompatible_import_type(import).into()); - } + if actual.page_count_initial > expected.page_count_initial + && real_size.map_or(true, |size| actual.page_count_initial > size as u64) + { + return Err(LinkingError::incompatible_import_type(import).into()); } - match (expected.page_count_max, actual.page_count_max) { - (None, Some(_)) => return Err(LinkingError::incompatible_import_type(import).into()), - (Some(expected_max), Some(actual_max)) if actual_max < expected_max => { - return Err(LinkingError::incompatible_import_type(import).into()) + if expected.page_count_max.is_none() && actual.page_count_max.is_some() { + return Err(LinkingError::incompatible_import_type(import).into()); + } + + if let (Some(expected_max), Some(actual_max)) = (expected.page_count_max, actual.page_count_max) { + if actual_max < expected_max { + return Err(LinkingError::incompatible_import_type(import).into()); } - _ => {} } Ok(()) @@ -375,7 +381,7 @@ impl Imports { .ok_or_else(|| LinkingError::incompatible_import_type(import))?; Self::compare_types(import, extern_func.ty(), import_func_type)?; - imports.funcs.push(store.add_func(extern_func, *ty, idx)?); + imports.funcs.push(store.add_func(extern_func, idx)?); } _ => return Err(LinkingError::incompatible_import_type(import).into()), }, @@ -398,7 +404,7 @@ impl Imports { Self::compare_table_types(import, &table.borrow().kind, ty)?; imports.tables.push(table_addr); } - (ExternVal::Mem(memory_addr), ImportKind::Memory(ty)) => { + (ExternVal::Memory(memory_addr), ImportKind::Memory(ty)) => { let mem = store.get_mem(memory_addr as usize)?; let (size, kind) = { let mem = mem.borrow(); diff --git a/crates/tinywasm/src/instance.rs b/crates/tinywasm/src/instance.rs index d750cd5..cb12f1b 100644 --- a/crates/tinywasm/src/instance.rs +++ b/crates/tinywasm/src/instance.rs @@ -1,21 +1,18 @@ -use alloc::{boxed::Box, format, string::ToString, sync::Arc}; -use tinywasm_types::{ - DataAddr, ElemAddr, Export, ExternVal, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, - ModuleInstanceAddr, TableAddr, -}; +use alloc::{boxed::Box, format, rc::Rc, string::ToString}; +use tinywasm_types::*; use crate::{ func::{FromWasmValueTuple, IntoWasmValueTuple}, - log, Error, FuncHandle, FuncHandleTyped, Imports, Module, Result, Store, + log, Error, FuncHandle, FuncHandleTyped, Imports, MemoryRef, MemoryRefMut, Module, Result, Store, }; /// An instanciated WebAssembly module /// -/// Backed by an Arc, so cloning is cheap +/// Backed by an Rc, so cloning is cheap /// /// See #[derive(Debug, Clone)] -pub struct ModuleInstance(Arc); +pub struct ModuleInstance(Rc); #[allow(dead_code)] #[derive(Debug)] @@ -45,6 +42,10 @@ impl ModuleInstance { self.0 = other.0; } + pub(crate) fn swap_with(&mut self, other_addr: ModuleInstanceAddr, store: &mut Store) { + self.swap(store.get_module_instance_raw(other_addr)) + } + /// Get the module instance's address pub fn id(&self) -> ModuleInstanceAddr { self.0.idx @@ -72,7 +73,7 @@ impl ModuleInstance { let global_addrs = store.init_globals(addrs.globals, data.globals.into(), &addrs.funcs, idx)?; let (elem_addrs, elem_trapped) = - store.init_elements(&addrs.tables, &addrs.funcs, &global_addrs, data.elements.into(), idx)?; + store.init_elements(&addrs.tables, &addrs.funcs, &global_addrs, &data.elements, idx)?; let (data_addrs, data_trapped) = store.init_datas(&addrs.memories, data.data.into(), idx)?; let instance = ModuleInstanceInner { @@ -106,7 +107,7 @@ impl ModuleInstance { } /// Get a export by name - pub fn export(&self, name: &str) -> Option { + pub fn export_addr(&self, name: &str) -> Option { let exports = self.0.exports.iter().find(|e| e.name == name.into())?; let kind = exports.kind.clone(); let addr = match kind { @@ -123,13 +124,8 @@ impl ModuleInstance { &self.0.func_addrs } - /// Get the module's function types - pub fn func_tys(&self) -> &[FuncType] { - &self.0.types - } - pub(crate) fn new(inner: ModuleInstanceInner) -> Self { - Self(Arc::new(inner)) + Self(Rc::new(inner)) } pub(crate) fn func_ty(&self, addr: FuncAddr) -> &FuncType { @@ -151,6 +147,11 @@ impl ModuleInstance { *self.0.mem_addrs.get(addr as usize).expect("No mem addr for mem, this is a bug") } + // resolve a data address to the global store address + pub(crate) fn resolve_data_addr(&self, addr: DataAddr) -> MemAddr { + *self.0.data_addrs.get(addr as usize).expect("No data addr for data, this is a bug") + } + // resolve a memory address to the global store address pub(crate) fn resolve_elem_addr(&self, addr: ElemAddr) -> ElemAddr { *self.0.elem_addrs.get(addr as usize).expect("No elem addr for elem, this is a bug") @@ -162,12 +163,12 @@ impl ModuleInstance { } /// Get an exported function by name - pub fn exported_func_by_name(&self, store: &Store, name: &str) -> Result { + pub fn exported_func_untyped(&self, store: &Store, name: &str) -> Result { if self.0.store_id != store.id() { return Err(Error::InvalidStore); } - let export = self.export(name).ok_or_else(|| Error::Other(format!("Export not found: {}", name)))?; + let export = self.export_addr(name).ok_or_else(|| Error::Other(format!("Export not found: {}", name)))?; let ExternVal::Func(func_addr) = export else { return Err(Error::Other(format!("Export is not a function: {}", name))); }; @@ -175,24 +176,57 @@ impl ModuleInstance { let func_inst = store.get_func(func_addr as usize)?; let ty = func_inst.func.ty(); - Ok(FuncHandle { addr: func_addr, module: self.clone(), name: Some(name.to_string()), ty: ty.clone() }) + Ok(FuncHandle { addr: func_addr, module_addr: self.id(), name: Some(name.to_string()), ty: ty.clone() }) } /// Get a typed exported function by name - pub fn typed_func(&self, store: &Store, name: &str) -> Result> + pub fn exported_func(&self, store: &Store, name: &str) -> Result> where P: IntoWasmValueTuple, R: FromWasmValueTuple, { - let func = self.exported_func_by_name(store, name)?; + let func = self.exported_func_untyped(store, name)?; Ok(FuncHandleTyped { func, marker: core::marker::PhantomData }) } + /// Get an exported memory by name + pub fn exported_memory<'a>(&self, store: &'a mut Store, name: &str) -> Result> { + let export = self.export_addr(name).ok_or_else(|| Error::Other(format!("Export not found: {}", name)))?; + let ExternVal::Memory(mem_addr) = export else { + return Err(Error::Other(format!("Export is not a memory: {}", name))); + }; + let mem = self.memory(store, mem_addr)?; + Ok(mem) + } + + /// Get an exported memory by name + pub fn exported_memory_mut<'a>(&self, store: &'a mut Store, name: &str) -> Result> { + let export = self.export_addr(name).ok_or_else(|| Error::Other(format!("Export not found: {}", name)))?; + let ExternVal::Memory(mem_addr) = export else { + return Err(Error::Other(format!("Export is not a memory: {}", name))); + }; + let mem = self.memory_mut(store, mem_addr)?; + Ok(mem) + } + + /// Get a memory by address + pub fn memory<'a>(&self, store: &'a mut Store, addr: MemAddr) -> Result> { + let addr = self.resolve_mem_addr(addr); + let mem = store.get_mem(addr as usize)?; + Ok(MemoryRef { instance: mem.borrow() }) + } + + /// Get a memory by address (mutable) + pub fn memory_mut<'a>(&self, store: &'a mut Store, addr: MemAddr) -> Result> { + let addr = self.resolve_mem_addr(addr); + let mem = store.get_mem(addr as usize)?; + Ok(MemoryRefMut { instance: mem.borrow_mut() }) + } + /// Get the start function of the module /// /// Returns None if the module has no start function /// If no start function is specified, also checks for a _start function in the exports - /// (which is not part of the spec, but used by llvm) /// /// See pub fn start_func(&self, store: &Store) -> Result> { @@ -204,7 +238,7 @@ impl ModuleInstance { Some(func_index) => func_index, None => { // alternatively, check for a _start function in the exports - let Some(ExternVal::Func(func_addr)) = self.export("_start") else { + let Some(ExternVal::Func(func_addr)) = self.export_addr("_start") else { return Ok(None); }; @@ -216,7 +250,7 @@ impl ModuleInstance { let func_inst = store.get_func(*func_addr as usize)?; let ty = func_inst.func.ty(); - Ok(Some(FuncHandle { module: self.clone(), addr: *func_addr, ty: ty.clone(), name: None })) + Ok(Some(FuncHandle { module_addr: self.id(), addr: *func_addr, ty: ty.clone(), name: None })) } /// Invoke the start function of the module diff --git a/crates/tinywasm/src/lib.rs b/crates/tinywasm/src/lib.rs index 36270a7..79b111c 100644 --- a/crates/tinywasm/src/lib.rs +++ b/crates/tinywasm/src/lib.rs @@ -1,11 +1,11 @@ #![no_std] -#![forbid(unsafe_code)] #![doc(test( no_crate_inject, attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_assignments, unused_variables)) ))] #![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)] #![cfg_attr(nightly, feature(error_in_core))] +#![cfg_attr(not(feature = "unsafe"), deny(unsafe_code))] //! A tiny WebAssembly Runtime written in Rust //! @@ -51,13 +51,15 @@ //! // Get a typed handle to the exported "add" function //! // Alternatively, you can use `instance.get_func` to get an untyped handle //! // that takes and returns [`WasmValue`]s -//! let func = instance.typed_func::<(i32, i32), i32>(&mut store, "add")?; +//! let func = instance.exported_func::<(i32, i32), i32>(&mut store, "add")?; //! let res = func.call(&mut store, (1, 2))?; //! //! assert_eq!(res, 3); //! # Ok::<(), tinywasm::Error>(()) //! ``` //! +//! For more examples, see the [`examples`](https://github.com/explodingcamera/tinywasm/tree/main/examples) directory. +//! //! ## Imports //! //! To provide imports to a module, you can use the [`Imports`] struct. @@ -99,6 +101,9 @@ pub use module::Module; mod instance; pub use instance::ModuleInstance; +mod reference; +pub use reference::*; + mod func; pub use func::{FuncHandle, FuncHandleTyped}; @@ -119,3 +124,13 @@ pub mod parser { pub mod types { pub use tinywasm_types::*; } + +#[cold] +pub(crate) fn cold() {} + +pub(crate) fn unlikely(b: bool) -> bool { + if b { + cold(); + } + b +} diff --git a/crates/tinywasm/src/reference.rs b/crates/tinywasm/src/reference.rs new file mode 100644 index 0000000..a34e30b --- /dev/null +++ b/crates/tinywasm/src/reference.rs @@ -0,0 +1,163 @@ +use core::{ + cell::{Ref, RefCell, RefMut}, + ffi::CStr, +}; + +use crate::{GlobalInstance, MemoryInstance, Result}; +use alloc::{ + ffi::CString, + rc::Rc, + string::{String, ToString}, + vec::Vec, +}; +use tinywasm_types::WasmValue; + +// This module essentially contains the public APIs to interact with the data stored in the store + +/// A reference to a memory instance +#[derive(Debug)] +pub struct MemoryRef<'a> { + pub(crate) instance: Ref<'a, MemoryInstance>, +} + +/// A borrowed reference to a memory instance +#[derive(Debug)] +pub struct MemoryRefMut<'a> { + pub(crate) instance: RefMut<'a, MemoryInstance>, +} + +impl<'a> MemoryRefLoad for MemoryRef<'a> { + /// Load a slice of memory + fn load(&self, offset: usize, len: usize) -> Result<&[u8]> { + self.instance.load(offset, 0, len) + } +} + +impl<'a> MemoryRefLoad for MemoryRefMut<'a> { + /// Load a slice of memory + fn load(&self, offset: usize, len: usize) -> Result<&[u8]> { + self.instance.load(offset, 0, len) + } +} + +impl MemoryRef<'_> { + /// Load a slice of memory + pub fn load(&self, offset: usize, len: usize) -> Result<&[u8]> { + self.instance.load(offset, 0, len) + } + + /// Load a slice of memory as a vector + pub fn load_vec(&self, offset: usize, len: usize) -> Result> { + self.load(offset, len).map(|x| x.to_vec()) + } +} + +impl MemoryRefMut<'_> { + /// Load a slice of memory + pub fn load(&self, offset: usize, len: usize) -> Result<&[u8]> { + self.instance.load(offset, 0, len) + } + + /// Load a slice of memory as a vector + pub fn load_vec(&self, offset: usize, len: usize) -> Result> { + self.load(offset, len).map(|x| x.to_vec()) + } + + /// Grow the memory by the given number of pages + pub fn grow(&mut self, delta_pages: i32) -> Option { + self.instance.grow(delta_pages) + } + + /// Get the current size of the memory in pages + pub fn page_count(&mut self) -> usize { + self.instance.page_count() + } + + /// Copy a slice of memory to another place in memory + pub fn copy_within(&mut self, src: usize, dst: usize, len: usize) -> Result<()> { + self.instance.copy_within(src, dst, len) + } + + /// Fill a slice of memory with a value + pub fn fill(&mut self, offset: usize, len: usize, val: u8) -> Result<()> { + self.instance.fill(offset, len, val) + } + + /// Store a slice of memory + pub fn store(&mut self, offset: usize, len: usize, data: &[u8]) -> Result<()> { + self.instance.store(offset, 0, data, len) + } +} + +#[doc(hidden)] +pub trait MemoryRefLoad { + fn load(&self, offset: usize, len: usize) -> Result<&[u8]>; + fn load_vec(&self, offset: usize, len: usize) -> Result> { + self.load(offset, len).map(|x| x.to_vec()) + } +} + +/// Convenience methods for loading strings from memory +pub trait MemoryStringExt: MemoryRefLoad { + /// Load a C-style string from memory + fn load_cstr(&self, offset: usize, len: usize) -> Result<&CStr> { + let bytes = self.load(offset, len)?; + CStr::from_bytes_with_nul(bytes).map_err(|_| crate::Error::Other("Invalid C-style string".to_string())) + } + + /// Load a C-style string from memory, stopping at the first nul byte + fn load_cstr_until_nul(&self, offset: usize, max_len: usize) -> Result<&CStr> { + let bytes = self.load(offset, max_len)?; + CStr::from_bytes_until_nul(bytes).map_err(|_| crate::Error::Other("Invalid C-style string".to_string())) + } + + /// Load a UTF-8 string from memory + fn load_string(&self, offset: usize, len: usize) -> Result { + let bytes = self.load(offset, len)?; + String::from_utf8(bytes.to_vec()).map_err(|_| crate::Error::Other("Invalid UTF-8 string".to_string())) + } + + /// Load a C-style string from memory + fn load_cstring(&self, offset: usize, len: usize) -> Result { + Ok(CString::from(self.load_cstr(offset, len)?)) + } + + /// Load a C-style string from memory, stopping at the first nul byte + fn load_cstring_until_nul(&self, offset: usize, max_len: usize) -> Result { + Ok(CString::from(self.load_cstr_until_nul(offset, max_len)?)) + } + + /// Load a JavaScript-style utf-16 string from memory + fn load_js_string(&self, offset: usize, len: usize) -> Result { + let bytes = self.load(offset, len)?; + let mut string = String::new(); + for i in 0..(len / 2) { + let c = u16::from_le_bytes([bytes[i * 2], bytes[i * 2 + 1]]); + string.push( + char::from_u32(c as u32).ok_or_else(|| crate::Error::Other("Invalid UTF-16 string".to_string()))?, + ); + } + Ok(string) + } +} + +impl MemoryStringExt for MemoryRef<'_> {} +impl MemoryStringExt for MemoryRefMut<'_> {} + +/// A reference to a global instance +#[derive(Debug, Clone)] +pub struct GlobalRef { + pub(crate) instance: Rc>, +} + +impl GlobalRef { + /// Get the value of the global + pub fn get(&self) -> WasmValue { + self.instance.borrow().get() + } + + /// Set the value of the global + pub fn set(&self, val: WasmValue) -> Result<()> { + self.instance.borrow_mut().set(val) + } +} diff --git a/crates/tinywasm/src/runtime/interpreter/macros.rs b/crates/tinywasm/src/runtime/interpreter/macros.rs index 909acb3..a769b12 100644 --- a/crates/tinywasm/src/runtime/interpreter/macros.rs +++ b/crates/tinywasm/src/runtime/interpreter/macros.rs @@ -14,14 +14,14 @@ macro_rules! mem_load { // TODO: there could be a lot of performance improvements here let mem_idx = $module.resolve_mem_addr($arg.mem_addr); let mem = $store.get_mem(mem_idx as usize)?; + let mem_ref = mem.borrow_mut(); let addr = $stack.values.pop()?.raw_value(); - let addr = $arg.offset.checked_add(addr).ok_or_else(|| { Error::Trap(crate::Trap::MemoryOutOfBounds { offset: $arg.offset as usize, len: core::mem::size_of::<$load_type>(), - max: mem.borrow().max_pages(), + max: mem_ref.max_pages(), }) })?; @@ -29,18 +29,14 @@ macro_rules! mem_load { Error::Trap(crate::Trap::MemoryOutOfBounds { offset: $arg.offset as usize, len: core::mem::size_of::<$load_type>(), - max: mem.borrow().max_pages(), + max: mem_ref.max_pages(), }) })?; - let val: [u8; core::mem::size_of::<$load_type>()] = { - let mem = mem.borrow_mut(); - let val = mem.load(addr, $arg.align as usize, core::mem::size_of::<$load_type>())?; - val.try_into().expect("slice with incorrect length") - }; - - let loaded_value = <$load_type>::from_le_bytes(val); - $stack.values.push((loaded_value as $target_type).into()); + const LEN: usize = core::mem::size_of::<$load_type>(); + let val = mem_ref.load_as::(addr, $arg.align as usize)?; + // let loaded_value = mem_ref.load_as::<$load_type>(addr, $arg.align as usize)?; + $stack.values.push((val as $target_type).into()); }}; } @@ -63,7 +59,7 @@ macro_rules! mem_store { let val = val as $store_type; let val = val.to_le_bytes(); - mem.borrow_mut().store(($arg.offset + addr) as usize, $arg.align as usize, &val)?; + mem.borrow_mut().store(($arg.offset + addr) as usize, $arg.align as usize, &val, val.len())?; }}; } @@ -146,13 +142,8 @@ macro_rules! comp { }}; ($op:tt, $intermediate:ty, $to:ty, $stack:ident) => {{ - let [a, b] = $stack.values.pop_n_const::<2>()?; - let a: $intermediate = a.into(); - let b: $intermediate = b.into(); - - // Cast to unsigned type before comparison - let a = a as $to; - let b = b as $to; + let b = $stack.values.pop_t::<$intermediate>()? as $to; + let a = $stack.values.pop_t::<$intermediate>()? as $to; $stack.values.push(((a $op b) as i32).into()); }}; } @@ -160,7 +151,7 @@ macro_rules! comp { /// Compare a value on the stack to zero macro_rules! comp_zero { ($op:tt, $ty:ty, $stack:ident) => {{ - let a: $ty = $stack.values.pop()?.into(); + let a = $stack.values.pop_t::<$ty>()?; $stack.values.push(((a $op 0) as i32).into()); }}; } @@ -173,20 +164,14 @@ macro_rules! arithmetic { // also allow operators such as +, - ($op:tt, $ty:ty, $stack:ident) => {{ - let [a, b] = $stack.values.pop_n_const::<2>()?; - let a: $ty = a.into(); - let b: $ty = b.into(); + let b: $ty = $stack.values.pop_t()?; + let a: $ty = $stack.values.pop_t()?; $stack.values.push((a $op b).into()); }}; ($op:ident, $intermediate:ty, $to:ty, $stack:ident) => {{ - let [a, b] = $stack.values.pop_n_const::<2>()?; - let a: $to = a.into(); - let b: $to = b.into(); - - let a = a as $intermediate; - let b = b as $intermediate; - + let b = $stack.values.pop_t::<$to>()? as $intermediate; + let a = $stack.values.pop_t::<$to>()? as $intermediate; let result = a.$op(b); $stack.values.push((result as $to).into()); }}; @@ -215,19 +200,14 @@ macro_rules! checked_int_arithmetic { }}; ($op:ident, $from:ty, $to:ty, $stack:ident) => {{ - let [a, b] = $stack.values.pop_n_const::<2>()?; - let a: $from = a.into(); - let b: $from = b.into(); + let b = $stack.values.pop_t::<$from>()? as $to; + let a = $stack.values.pop_t::<$from>()? as $to; - let a_casted: $to = a as $to; - let b_casted: $to = b as $to; - - if b_casted == 0 { + if b == 0 { return Err(Error::Trap(crate::Trap::DivisionByZero)); } - let result = a_casted.$op(b_casted).ok_or_else(|| Error::Trap(crate::Trap::IntegerOverflow))?; - + let result = a.$op(b).ok_or_else(|| Error::Trap(crate::Trap::IntegerOverflow))?; // Cast back to original type if different $stack.values.push((result as $from).into()); }}; diff --git a/crates/tinywasm/src/runtime/interpreter/mod.rs b/crates/tinywasm/src/runtime/interpreter/mod.rs index db013a0..79abbf3 100644 --- a/crates/tinywasm/src/runtime/interpreter/mod.rs +++ b/crates/tinywasm/src/runtime/interpreter/mod.rs @@ -1,13 +1,13 @@ use super::{InterpreterRuntime, Stack}; -use crate::log; +use crate::{cold, log, unlikely}; use crate::{ - log::debug, - runtime::{BlockType, CallFrame, LabelArgs, LabelFrame}, + runtime::{BlockType, CallFrame, LabelFrame}, Error, FuncContext, ModuleInstance, Result, Store, Trap, }; +use alloc::format; use alloc::{string::ToString, vec::Vec}; use core::ops::{BitAnd, BitOr, BitXor, Neg}; -use tinywasm_types::{ElementKind, Instruction, ValType}; +use tinywasm_types::{ElementKind, ValType}; #[cfg(not(feature = "std"))] mod no_std_floats; @@ -23,51 +23,29 @@ use macros::*; use traits::*; impl InterpreterRuntime { + // #[inline(always)] // a small 2-3% performance improvement in some cases pub(crate) fn exec(&self, store: &mut Store, stack: &mut Stack) -> Result<()> { // The current call frame, gets updated inside of exec_one let mut cf = stack.call_stack.pop()?; - let mut func_inst = cf.func_instance.clone(); - let mut wasm_func = func_inst.assert_wasm().expect("exec expected wasm function"); - // The function to execute, gets updated from ExecResult::Call - let mut instrs = &wasm_func.instructions; - - let mut current_module = store.get_module_instance(func_inst.owner).unwrap().clone(); + let mut current_module = store.get_module_instance_raw(cf.func_instance.1); - while let Some(instr) = instrs.get(cf.instr_ptr) { - match exec_one(&mut cf, instr, instrs, stack, store, ¤t_module)? { + loop { + match exec_one(&mut cf, stack, store, ¤t_module)? { // Continue execution at the new top of the call stack ExecResult::Call => { cf = stack.call_stack.pop()?; - func_inst = cf.func_instance.clone(); - wasm_func = func_inst.assert_wasm().expect("exec expected wasm function"); - instrs = &wasm_func.instructions; - - if cf.func_instance.owner != current_module.id() { - current_module.swap( - store - .get_module_instance(cf.func_instance.owner) - .unwrap_or_else(|| { - panic!( - "exec expected module instance {} to exist for function", - cf.func_instance.owner - ) - }) - .clone(), - ); + if cf.func_instance.1 != current_module.id() { + current_module.swap_with(cf.func_instance.1, store); } - - continue; } // return from the function ExecResult::Return => return Ok(()), // continue to the next instruction and increment the instruction pointer - ExecResult::Ok => { - cf.instr_ptr += 1; - } + ExecResult::Ok => cf.instr_ptr += 1, // trap the program ExecResult::Trap(trap) => { @@ -79,12 +57,6 @@ impl InterpreterRuntime { } } } - - debug!("end of exec"); - debug!("stack: {:?}", stack.values); - debug!("insts: {:?}", instrs); - debug!("instr_ptr: {}", cf.instr_ptr); - Err(Error::FuncDidNotReturn) } } @@ -115,21 +87,22 @@ macro_rules! break_to { /// Run a single step of the interpreter /// A seperate function is used so later, we can more easily implement /// a step-by-step debugger (using generators once they're stable?) -#[inline] -fn exec_one( - cf: &mut CallFrame, - instr: &Instruction, - instrs: &[Instruction], - stack: &mut Stack, - store: &mut Store, - module: &ModuleInstance, -) -> Result { - debug!("ptr: {} instr: {:?}", cf.instr_ptr, instr); +#[inline(always)] // this improves performance by more than 20% in some cases +fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &ModuleInstance) -> Result { + let instrs = &cf.func_instance.0.instructions; + if unlikely(cf.instr_ptr >= instrs.len() || instrs.is_empty()) { + cold(); + log::error!("instr_ptr out of bounds: {} >= {}", cf.instr_ptr, instrs.len()); + return Err(Error::Other(format!("instr_ptr out of bounds: {} >= {}", cf.instr_ptr, instrs.len()))); + } use tinywasm_types::Instruction::*; - match instr { + match &instrs[cf.instr_ptr] { Nop => { /* do nothing */ } - Unreachable => return Ok(ExecResult::Trap(crate::Trap::Unreachable)), // we don't need to include the call frame here because it's already on the stack + Unreachable => { + cold(); + return Ok(ExecResult::Trap(crate::Trap::Unreachable)); + } // we don't need to include the call frame here because it's already on the stack Drop => stack.values.pop().map(|_| ())?, Select( @@ -151,19 +124,19 @@ fn exec_one( let func_idx = module.resolve_func_addr(*v); let func_inst = store.get_func(func_idx as usize)?.clone(); - let (locals, ty) = match &func_inst.func { - crate::Function::Wasm(ref f) => (f.locals.to_vec(), f.ty.clone()), + let wasm_func = match &func_inst.func { + crate::Function::Wasm(wasm_func) => wasm_func.clone(), crate::Function::Host(host_func) => { - let func = host_func.func.clone(); + let func = &host_func.func; let params = stack.values.pop_params(&host_func.ty.params)?; - let res = (func)(FuncContext { store, module }, ¶ms)?; + let res = (func)(FuncContext { store, module_addr: module.id() }, ¶ms)?; stack.values.extend_from_typed(&res); return Ok(ExecResult::Ok); } }; - let params = stack.values.pop_n_rev(ty.params.len())?; - let call_frame = CallFrame::new_raw(func_inst, ¶ms, locals); + let params = stack.values.pop_n_rev(wasm_func.ty.params.len())?; + let call_frame = CallFrame::new(wasm_func, func_inst.owner, params); // push the call frame cf.instr_ptr += 1; // skip the call instruction @@ -190,35 +163,37 @@ fn exec_one( }; let func_inst = store.get_func(func_ref as usize)?.clone(); - let func_ty = func_inst.func.ty(); - - log::info!("type_addr: {}", type_addr); - log::info!("types: {:?}", module.func_tys()); let call_ty = module.func_ty(*type_addr); - log::info!("call_indirect: current fn owner: {:?}", module.id()); - log::info!("call_indirect: func owner: {:?}", func_inst.owner); - - if func_ty != call_ty { - log::error!("indirect call type mismatch: {:?} != {:?}", func_ty, call_ty); - return Err( - Trap::IndirectCallTypeMismatch { actual: func_ty.clone(), expected: call_ty.clone() }.into() - ); - } - - let locals = match &func_inst.func { - crate::Function::Wasm(ref f) => f.locals.to_vec(), + let wasm_func = match func_inst.func { + crate::Function::Wasm(ref f) => f.clone(), crate::Function::Host(host_func) => { - let func = host_func.func.clone(); - let params = stack.values.pop_params(&func_ty.params)?; - let res = (func)(FuncContext { store, module }, ¶ms)?; + if unlikely(host_func.ty != *call_ty) { + log::error!("indirect call type mismatch: {:?} != {:?}", host_func.ty, call_ty); + return Err(Trap::IndirectCallTypeMismatch { + actual: host_func.ty.clone(), + expected: call_ty.clone(), + } + .into()); + } + + let host_func = host_func.clone(); + let params = stack.values.pop_params(&host_func.ty.params)?; + let res = (host_func.func)(FuncContext { store, module_addr: module.id() }, ¶ms)?; stack.values.extend_from_typed(&res); return Ok(ExecResult::Ok); } }; - let params = stack.values.pop_n_rev(func_ty.params.len())?; - let call_frame = CallFrame::new_raw(func_inst, ¶ms, locals); + if unlikely(wasm_func.ty != *call_ty) { + log::error!("indirect call type mismatch: {:?} != {:?}", wasm_func.ty, call_ty); + return Err( + Trap::IndirectCallTypeMismatch { actual: wasm_func.ty.clone(), expected: call_ty.clone() }.into() + ); + } + + let params = stack.values.pop_n_rev(wasm_func.ty.params.len())?; + let call_frame = CallFrame::new(wasm_func, func_inst.owner, params); // push the call frame cf.instr_ptr += 1; // skip the call instruction @@ -232,15 +207,15 @@ fn exec_one( If(args, else_offset, end_offset) => { // truthy value is on the top of the stack, so enter the then block if stack.values.pop_t::()? != 0 { - log::trace!("entering then"); cf.enter_label( - LabelFrame { - instr_ptr: cf.instr_ptr, - end_instr_ptr: cf.instr_ptr + *end_offset, - stack_ptr: stack.values.len(), // - params, - args: LabelArgs::new(*args, module)?, - ty: BlockType::If, - }, + LabelFrame::new( + cf.instr_ptr, + cf.instr_ptr + *end_offset, + stack.values.len(), // - params, + BlockType::If, + args, + module, + ), &mut stack.values, ); return Ok(ExecResult::Ok); @@ -248,46 +223,45 @@ fn exec_one( // falsy value is on the top of the stack if let Some(else_offset) = else_offset { - log::debug!("entering else at {}", cf.instr_ptr + *else_offset); - cf.enter_label( - LabelFrame { - instr_ptr: cf.instr_ptr + *else_offset, - end_instr_ptr: cf.instr_ptr + *end_offset, - stack_ptr: stack.values.len(), // - params, - args: LabelArgs::new(*args, module)?, - ty: BlockType::Else, - }, - &mut stack.values, + let label = LabelFrame::new( + cf.instr_ptr + *else_offset, + cf.instr_ptr + *end_offset, + stack.values.len(), // - params, + BlockType::Else, + args, + module, ); cf.instr_ptr += *else_offset; + cf.enter_label(label, &mut stack.values); } else { cf.instr_ptr += *end_offset; } } Loop(args, end_offset) => { - // let params = stack.values.pop_block_params(*args, &module)?; cf.enter_label( - LabelFrame { - instr_ptr: cf.instr_ptr, - end_instr_ptr: cf.instr_ptr + *end_offset, - stack_ptr: stack.values.len(), // - params, - args: LabelArgs::new(*args, module)?, - ty: BlockType::Loop, - }, + LabelFrame::new( + cf.instr_ptr, + cf.instr_ptr + *end_offset, + stack.values.len(), // - params, + BlockType::Loop, + args, + module, + ), &mut stack.values, ); } Block(args, end_offset) => { cf.enter_label( - LabelFrame { - instr_ptr: cf.instr_ptr, - end_instr_ptr: cf.instr_ptr + *end_offset, - stack_ptr: stack.values.len(), //- params, - args: LabelArgs::new(*args, module)?, - ty: BlockType::Block, - }, + LabelFrame::new( + cf.instr_ptr, + cf.instr_ptr + *end_offset, + stack.values.len(), // - params, + BlockType::Block, + args, + module, + ), &mut stack.values, ); } @@ -297,11 +271,14 @@ fn exec_one( .iter() .map(|i| match i { BrLabel(l) => Ok(*l), - _ => panic!("Expected BrLabel, this should have been validated by the parser"), + _ => { + cold(); + panic!("Expected BrLabel, this should have been validated by the parser") + } }) .collect::>>()?; - if instr.len() != *len { + if unlikely(instr.len() != *len) { panic!( "Expected {} BrLabel instructions, got {}, this should have been validated by the parser", len, @@ -341,10 +318,11 @@ fn exec_one( // We're essentially using else as a EndBlockFrame instruction for if blocks Else(end_offset) => { let Some(block) = cf.labels.pop() else { + cold(); panic!("else: no label to end, this should have been validated by the parser"); }; - let res_count = block.args.results; + let res_count = block.results; stack.values.truncate_keep(block.stack_ptr, res_count); cf.instr_ptr += *end_offset; } @@ -352,9 +330,10 @@ fn exec_one( EndBlockFrame => { // remove the label from the label stack let Some(block) = cf.labels.pop() else { + cold(); panic!("end: no label to end, this should have been validated by the parser"); }; - stack.values.truncate_keep(block.stack_ptr, block.args.results) + stack.values.truncate_keep(block.stack_ptr, block.results) } LocalGet(local_index) => stack.values.push(cf.get_local(*local_index as usize)), @@ -379,7 +358,8 @@ fn exec_one( MemorySize(addr, byte) => { if *byte != 0 { - unimplemented!("memory.size with byte != 0"); + cold(); + return Err(Error::UnsupportedFeature("memory.size with byte != 0".to_string())); } let mem_idx = module.resolve_mem_addr(*addr); @@ -389,6 +369,7 @@ fn exec_one( MemoryGrow(addr, byte) => { if *byte != 0 { + cold(); return Err(Error::UnsupportedFeature("memory.grow with byte != 0".to_string())); } @@ -407,6 +388,69 @@ fn exec_one( } } + // Bulk memory operations + MemoryCopy(from, to) => { + let size = stack.values.pop_t::()?; + let src = stack.values.pop_t::()?; + let dst = stack.values.pop_t::()?; + + let mem = store.get_mem(module.resolve_mem_addr(*from) as usize)?; + let mut mem = mem.borrow_mut(); + + if from == to { + // copy within the same memory + mem.copy_within(dst as usize, src as usize, size as usize)?; + } else { + // copy between two memories + let mem2 = store.get_mem(module.resolve_mem_addr(*to) as usize)?; + let mut mem2 = mem2.borrow_mut(); + mem2.copy_from_slice(dst as usize, mem.load(src as usize, 0, size as usize)?)?; + } + } + + MemoryFill(addr) => { + let size = stack.values.pop_t::()?; + let val = stack.values.pop_t::()?; + let dst = stack.values.pop_t::()?; + + let mem = store.get_mem(module.resolve_mem_addr(*addr) as usize)?; + let mut mem = mem.borrow_mut(); + mem.fill(dst as usize, size as usize, val as u8)?; + } + + MemoryInit(data_index, mem_index) => { + let size = stack.values.pop_t::()? as usize; + let offset = stack.values.pop_t::()? as usize; + let dst = stack.values.pop_t::()? as usize; + + let data_idx = module.resolve_data_addr(*data_index); + let Some(ref data) = store.get_data(data_idx as usize)?.data else { + cold(); + return Err(Trap::MemoryOutOfBounds { offset: 0, len: 0, max: 0 }.into()); + }; + + let mem_idx = module.resolve_mem_addr(*mem_index); + let mem = store.get_mem(mem_idx as usize)?; + + let data_len = data.len(); + if offset + size > data_len { + cold(); + return Err(Trap::MemoryOutOfBounds { offset, len: size, max: data_len }.into()); + } + + let mut mem = mem.borrow_mut(); + let data = &data[offset..(offset + size)]; + + // mem.store checks bounds + mem.store(dst, 0, data, size)?; + } + + DataDrop(data_index) => { + let data_idx = module.resolve_data_addr(*data_index); + let data = store.get_data_mut(data_idx as usize)?; + data.drop(); + } + I32Store(arg) => mem_store!(i32, arg, stack, store, module), I64Store(arg) => mem_store!(i64, arg, stack, store, module), F32Store(arg) => mem_store!(f32, arg, stack, store, module), @@ -612,7 +656,7 @@ fn exec_one( let elem_idx = module.resolve_elem_addr(*elem_index); let elem = store.get_elem(elem_idx as usize)?; - if elem.kind != ElementKind::Passive { + if let ElementKind::Passive = elem.kind { return Err(Trap::TableOutOfBounds { offset: 0, len: 0, max: 0 }.into()); } @@ -633,6 +677,7 @@ fn exec_one( I64TruncSatF64U => arithmetic_single!(trunc, f64, u64, stack), i => { + cold(); log::error!("unimplemented instruction: {:?}", i); return Err(Error::UnsupportedFeature(alloc::format!("unimplemented instruction: {:?}", i))); } diff --git a/crates/tinywasm/src/runtime/interpreter/no_std_floats.rs b/crates/tinywasm/src/runtime/interpreter/no_std_floats.rs index 095fe9f..5620249 100644 --- a/crates/tinywasm/src/runtime/interpreter/no_std_floats.rs +++ b/crates/tinywasm/src/runtime/interpreter/no_std_floats.rs @@ -10,67 +10,83 @@ pub(super) trait FExt { } impl FExt for f64 { + #[inline] fn round(self) -> Self { libm::round(self) } + #[inline] fn abs(self) -> Self { libm::fabs(self) } + #[inline] fn signum(self) -> Self { libm::copysign(1.0, self) } + #[inline] fn ceil(self) -> Self { libm::ceil(self) } + #[inline] fn floor(self) -> Self { libm::floor(self) } + #[inline] fn trunc(self) -> Self { libm::trunc(self) } + #[inline] fn sqrt(self) -> Self { libm::sqrt(self) } + #[inline] fn copysign(self, other: Self) -> Self { libm::copysign(self, other) } } impl FExt for f32 { + #[inline] fn round(self) -> Self { libm::roundf(self) } + #[inline] fn abs(self) -> Self { libm::fabsf(self) } + #[inline] fn signum(self) -> Self { libm::copysignf(1.0, self) } + #[inline] fn ceil(self) -> Self { libm::ceilf(self) } + #[inline] fn floor(self) -> Self { libm::floorf(self) } + #[inline] fn trunc(self) -> Self { libm::truncf(self) } + #[inline] fn sqrt(self) -> Self { libm::sqrtf(self) } + #[inline] fn copysign(self, other: Self) -> Self { libm::copysignf(self, other) } diff --git a/crates/tinywasm/src/runtime/stack.rs b/crates/tinywasm/src/runtime/stack.rs index 07d9316..31c41f0 100644 --- a/crates/tinywasm/src/runtime/stack.rs +++ b/crates/tinywasm/src/runtime/stack.rs @@ -3,12 +3,18 @@ mod call_stack; mod value_stack; use self::{call_stack::CallStack, value_stack::ValueStack}; -pub(crate) use blocks::{BlockType, LabelArgs, LabelFrame}; +pub(crate) use blocks::{BlockType, LabelFrame}; pub(crate) use call_stack::CallFrame; /// A WebAssembly Stack -#[derive(Debug, Default)] +#[derive(Debug)] pub struct Stack { pub(crate) values: ValueStack, pub(crate) call_stack: CallStack, } + +impl Stack { + pub(crate) fn new(call_frame: CallFrame) -> Self { + Self { values: ValueStack::default(), call_stack: CallStack::new(call_frame) } + } +} diff --git a/crates/tinywasm/src/runtime/stack/blocks.rs b/crates/tinywasm/src/runtime/stack/blocks.rs index 76252b5..f302e59 100644 --- a/crates/tinywasm/src/runtime/stack/blocks.rs +++ b/crates/tinywasm/src/runtime/stack/blocks.rs @@ -1,12 +1,17 @@ use alloc::vec::Vec; use tinywasm_types::BlockArgs; -use crate::{ModuleInstance, Result}; +use crate::{unlikely, ModuleInstance}; -#[derive(Debug, Clone, Default)] -pub(crate) struct Labels(Vec); +#[derive(Debug, Clone)] +pub(crate) struct Labels(Vec); // TODO: maybe Box<[LabelFrame]> by analyzing the lable count when parsing the module? impl Labels { + pub(crate) fn new() -> Self { + // this is somehow a lot faster than Vec::with_capacity(128) or even using Default::default() in the benchmarks + Self(Vec::new()) + } + pub(crate) fn len(&self) -> usize { self.0.len() } @@ -19,12 +24,12 @@ impl Labels { #[inline] /// get the label at the given index, where 0 is the top of the stack pub(crate) fn get_relative_to_top(&self, index: usize) -> Option<&LabelFrame> { - let len = self.0.len(); - if index >= len { + // the vast majority of wasm functions don't use break to return + if unlikely(index >= self.0.len()) { return None; } - self.0.get(self.0.len() - index - 1) + Some(&self.0[self.0.len() - index - 1]) } #[inline] @@ -48,10 +53,34 @@ pub(crate) struct LabelFrame { // position of the stack pointer when the block was entered pub(crate) stack_ptr: usize, - pub(crate) args: LabelArgs, + pub(crate) results: usize, + pub(crate) params: usize, pub(crate) ty: BlockType, } +impl LabelFrame { + #[inline] + pub(crate) fn new( + instr_ptr: usize, + end_instr_ptr: usize, + stack_ptr: usize, + ty: BlockType, + args: &BlockArgs, + module: &ModuleInstance, + ) -> Self { + let (params, results) = match args { + BlockArgs::Empty => (0, 0), + BlockArgs::Type(_) => (0, 1), + BlockArgs::FuncType(t) => { + let ty = module.func_ty(*t); + (ty.params.len(), ty.results.len()) + } + }; + + Self { instr_ptr, end_instr_ptr, stack_ptr, results, params, ty } + } +} + #[derive(Debug, Copy, Clone)] #[allow(dead_code)] pub(crate) enum BlockType { @@ -60,21 +89,3 @@ pub(crate) enum BlockType { Else, Block, } - -#[derive(Debug, Clone, Default)] -pub(crate) struct LabelArgs { - pub(crate) params: usize, - pub(crate) results: usize, -} - -impl LabelArgs { - pub(crate) fn new(args: BlockArgs, module: &ModuleInstance) -> Result { - Ok(match args { - BlockArgs::Empty => LabelArgs { params: 0, results: 0 }, - BlockArgs::Type(_) => LabelArgs { params: 0, results: 1 }, - BlockArgs::FuncType(t) => { - LabelArgs { params: module.func_ty(t).params.len(), results: module.func_ty(t).results.len() } - } - }) - } -} diff --git a/crates/tinywasm/src/runtime/stack/call_stack.rs b/crates/tinywasm/src/runtime/stack/call_stack.rs index 20de728..dcbfcac 100644 --- a/crates/tinywasm/src/runtime/stack/call_stack.rs +++ b/crates/tinywasm/src/runtime/stack/call_stack.rs @@ -1,10 +1,10 @@ -use crate::log; +use crate::unlikely; use crate::{ runtime::{BlockType, RawWasmValue}, - Error, FunctionInstance, Result, Trap, + Error, Result, Trap, }; use alloc::{boxed::Box, rc::Rc, vec::Vec}; -use tinywasm_types::{ValType, WasmValue}; +use tinywasm_types::{ModuleInstanceAddr, WasmFunction}; use super::{blocks::Labels, LabelFrame}; @@ -15,40 +15,34 @@ const CALL_STACK_MAX_SIZE: usize = 1024; #[derive(Debug)] pub(crate) struct CallStack { stack: Vec, - top: usize, } -impl Default for CallStack { - fn default() -> Self { - Self { stack: Vec::with_capacity(CALL_STACK_SIZE), top: 0 } +impl CallStack { + #[inline] + pub(crate) fn new(initial_frame: CallFrame) -> Self { + let mut stack = Self { stack: Vec::with_capacity(CALL_STACK_SIZE) }; + stack.push(initial_frame).unwrap(); + stack } -} -impl CallStack { + #[inline] pub(crate) fn is_empty(&self) -> bool { - self.top == 0 + self.stack.is_empty() } + #[inline] pub(crate) fn pop(&mut self) -> Result { - assert!(self.top <= self.stack.len()); - if self.top == 0 { - return Err(Error::CallStackEmpty); + match self.stack.pop() { + Some(frame) => Ok(frame), + None => Err(Error::CallStackEmpty), } - - self.top -= 1; - Ok(self.stack.pop().unwrap()) } #[inline] pub(crate) fn push(&mut self, call_frame: CallFrame) -> Result<()> { - assert!(self.top <= self.stack.len(), "stack is too small"); - - log::debug!("stack size: {}", self.stack.len()); - if self.stack.len() >= CALL_STACK_MAX_SIZE { + if unlikely(self.stack.len() >= CALL_STACK_MAX_SIZE) { return Err(Trap::CallStackOverflow.into()); } - - self.top += 1; self.stack.push(call_frame); Ok(()) } @@ -58,19 +52,16 @@ impl CallStack { pub(crate) struct CallFrame { pub(crate) instr_ptr: usize, // pub(crate) module: ModuleInstanceAddr, - pub(crate) func_instance: Rc, - + pub(crate) func_instance: (Rc, ModuleInstanceAddr), pub(crate) labels: Labels, pub(crate) locals: Box<[RawWasmValue]>, - pub(crate) local_count: usize, } impl CallFrame { - #[inline] /// Push a new label to the label stack and ensure the stack has the correct values pub(crate) fn enter_label(&mut self, label_frame: LabelFrame, stack: &mut super::ValueStack) { - if label_frame.args.params > 0 { - stack.extend_from_within((label_frame.stack_ptr - label_frame.args.params)..label_frame.stack_ptr); + if label_frame.params > 0 { + stack.extend_from_within((label_frame.stack_ptr - label_frame.params)..label_frame.stack_ptr); } self.labels.push(label_frame); @@ -78,9 +69,7 @@ impl CallFrame { /// Break to a block at the given index (relative to the current frame) /// Returns `None` if there is no block at the given index (e.g. if we need to return, this is handled by the caller) - #[inline] pub(crate) fn break_to(&mut self, break_to_relative: u32, value_stack: &mut super::ValueStack) -> Option<()> { - log::debug!("break_to_relative: {}", break_to_relative); let break_to = self.labels.get_relative_to_top(break_to_relative as usize)?; // instr_ptr points to the label instruction, but the next step @@ -88,18 +77,23 @@ impl CallFrame { match break_to.ty { BlockType::Loop => { // this is a loop, so we want to jump back to the start of the loop - // We also want to push the params to the stack - value_stack.break_to(break_to.stack_ptr, break_to.args.params); - self.instr_ptr = break_to.instr_ptr; - // we also want to trim the label stack to the loop (but not including the loop) - self.labels.truncate(self.labels.len() - break_to_relative as usize); + // We also want to push the params to the stack + value_stack.break_to(break_to.stack_ptr, break_to.params); + + // check if we're breaking to the loop + if break_to_relative != 0 { + // we also want to trim the label stack to the loop (but not including the loop) + self.labels.truncate(self.labels.len() - break_to_relative as usize); + return Some(()); + } } + BlockType::Block | BlockType::If | BlockType::Else => { // this is a block, so we want to jump to the next instruction after the block ends // We also want to push the block's results to the stack - value_stack.break_to(break_to.stack_ptr, break_to.args.results); + value_stack.break_to(break_to.stack_ptr, break_to.results); // (the inst_ptr will be incremented by 1 before the next instruction is executed) self.instr_ptr = break_to.end_instr_ptr; @@ -112,45 +106,32 @@ impl CallFrame { Some(()) } - pub(crate) fn new_raw( - func_instance_ptr: Rc, - params: &[RawWasmValue], - local_types: Vec, - ) -> Self { - let mut locals = Vec::with_capacity(local_types.len() + params.len()); - locals.extend(params.iter().cloned()); - locals.extend(local_types.iter().map(|_| RawWasmValue::default())); - - Self { - instr_ptr: 0, - func_instance: func_instance_ptr, - local_count: locals.len(), - locals: locals.into_boxed_slice(), - labels: Labels::default(), - } - } - + // TODO: perf: a lot of time is spent here + #[inline(always)] // about 10% faster with this pub(crate) fn new( - func_instance_ptr: Rc, - params: &[WasmValue], - local_types: Vec, + wasm_func_inst: Rc, + owner: ModuleInstanceAddr, + params: impl Iterator + ExactSizeIterator, ) -> Self { - CallFrame::new_raw( - func_instance_ptr, - ¶ms.iter().map(|v| RawWasmValue::from(*v)).collect::>(), - local_types, - ) + let locals = { + let local_types = &wasm_func_inst.locals; + let total_size = local_types.len() + params.len(); + let mut locals = Vec::with_capacity(total_size); + locals.extend(params); + locals.resize_with(total_size, RawWasmValue::default); + locals.into_boxed_slice() + }; + + Self { instr_ptr: 0, func_instance: (wasm_func_inst, owner), locals, labels: Labels::new() } } #[inline] pub(crate) fn set_local(&mut self, local_index: usize, value: RawWasmValue) { - assert!(local_index < self.local_count, "Invalid local index"); self.locals[local_index] = value; } #[inline] pub(crate) fn get_local(&self, local_index: usize) -> RawWasmValue { - assert!(local_index < self.local_count, "Invalid local index"); self.locals[local_index] } } diff --git a/crates/tinywasm/src/runtime/stack/value_stack.rs b/crates/tinywasm/src/runtime/stack/value_stack.rs index bb4e398..9b8f82d 100644 --- a/crates/tinywasm/src/runtime/stack/value_stack.rs +++ b/crates/tinywasm/src/runtime/stack/value_stack.rs @@ -1,29 +1,26 @@ use core::ops::Range; -use crate::log; -use crate::{runtime::RawWasmValue, Error, Result}; +use crate::{cold, runtime::RawWasmValue, unlikely, Error, Result}; use alloc::vec::Vec; use tinywasm_types::{ValType, WasmValue}; -// minimum stack size -pub(crate) const STACK_SIZE: usize = 1024; +pub(crate) const MIN_VALUE_STACK_SIZE: usize = 1024; +// pub(crate) const MAX_VALUE_STACK_SIZE: usize = 1024 * 1024; #[derive(Debug)] pub(crate) struct ValueStack { stack: Vec, - top: usize, } impl Default for ValueStack { fn default() -> Self { - Self { stack: Vec::with_capacity(STACK_SIZE), top: 0 } + Self { stack: Vec::with_capacity(MIN_VALUE_STACK_SIZE) } } } impl ValueStack { #[inline] pub(crate) fn extend_from_within(&mut self, range: Range) { - self.top += range.len(); self.stack.extend_from_within(range); } @@ -33,105 +30,94 @@ impl ValueStack { return; } - self.top += values.len(); self.stack.extend(values.iter().map(|v| RawWasmValue::from(*v))); } #[inline] pub(crate) fn len(&self) -> usize { - assert!(self.top <= self.stack.len()); - self.top + self.stack.len() } pub(crate) fn truncate_keep(&mut self, n: usize, end_keep: usize) { let total_to_keep = n + end_keep; - assert!(self.top >= total_to_keep, "Total to keep should be less than or equal to self.top"); + let len = self.stack.len(); + assert!(len >= total_to_keep, "Total to keep should be less than or equal to self.top"); - let current_size = self.stack.len(); - if current_size <= total_to_keep { + if len <= total_to_keep { return; // No need to truncate if the current size is already less than or equal to total_to_keep } - let items_to_remove = current_size - total_to_keep; - let remove_start_index = self.top - items_to_remove - end_keep; - let remove_end_index = self.top - end_keep; - + let items_to_remove = len - total_to_keep; + let remove_start_index = len - items_to_remove - end_keep; + let remove_end_index = len - end_keep; self.stack.drain(remove_start_index..remove_end_index); - self.top = total_to_keep; // Update top to reflect the new size } #[inline] pub(crate) fn push(&mut self, value: RawWasmValue) { - self.top += 1; self.stack.push(value); } #[inline] pub(crate) fn last(&self) -> Result<&RawWasmValue> { - self.stack.last().ok_or(Error::StackUnderflow) + match self.stack.last() { + Some(v) => Ok(v), + None => { + cold(); + Err(Error::StackUnderflow) + } + } } #[inline] pub(crate) fn pop_t>(&mut self) -> Result { - self.top -= 1; - Ok(self.stack.pop().ok_or(Error::StackUnderflow)?.into()) + match self.stack.pop() { + Some(v) => Ok(v.into()), + None => { + cold(); + Err(Error::StackUnderflow) + } + } } #[inline] pub(crate) fn pop(&mut self) -> Result { - self.top -= 1; - self.stack.pop().ok_or(Error::StackUnderflow) + match self.stack.pop() { + Some(v) => Ok(v), + None => { + cold(); + Err(Error::StackUnderflow) + } + } } #[inline] pub(crate) fn pop_params(&mut self, types: &[ValType]) -> Result> { - log::info!("pop_params: types={:?}", types); - log::info!("stack={:?}", self.stack); - - let mut res = Vec::with_capacity(types.len()); - for ty in types.iter() { - res.push(self.pop()?.attach_type(*ty)); - } - + let res = self.pop_n_rev(types.len())?.zip(types.iter()).map(|(v, ty)| v.attach_type(*ty)).collect(); Ok(res) } + #[inline] pub(crate) fn break_to(&mut self, new_stack_size: usize, result_count: usize) { - assert!(self.top >= result_count); - self.stack.copy_within((self.top - result_count)..self.top, new_stack_size); - self.top = new_stack_size + result_count; - self.stack.truncate(self.top); + self.stack.drain(new_stack_size..(self.stack.len() - result_count)); } #[inline] pub(crate) fn last_n(&self, n: usize) -> Result<&[RawWasmValue]> { - if self.top < n { + let len = self.stack.len(); + if unlikely(len < n) { return Err(Error::StackUnderflow); } - Ok(&self.stack[self.top - n..self.top]) + Ok(&self.stack[len - n..len]) } #[inline] - pub(crate) fn pop_n_rev(&mut self, n: usize) -> Result> { - if self.top < n { + pub(crate) fn pop_n_rev(&mut self, n: usize) -> Result> { + let len = self.stack.len(); + if unlikely(len < n) { return Err(Error::StackUnderflow); } - self.top -= n; - let res = self.stack.drain(self.top..).collect::>(); - Ok(res) - } - - #[inline] - pub(crate) fn pop_n_const(&mut self) -> Result<[RawWasmValue; N]> { - if self.top < N { - return Err(Error::StackUnderflow); - } - self.top -= N; - let mut res = [RawWasmValue::default(); N]; - for i in res.iter_mut().rev() { - *i = self.stack.pop().ok_or(Error::InvalidStore)?; - } - + let res = self.stack.drain((len - n)..); Ok(res) } } diff --git a/crates/tinywasm/src/runtime/value.rs b/crates/tinywasm/src/runtime/value.rs index 7230830..5341361 100644 --- a/crates/tinywasm/src/runtime/value.rs +++ b/crates/tinywasm/src/runtime/value.rs @@ -8,6 +8,7 @@ use tinywasm_types::{ValType, WasmValue}; /// /// See [`WasmValue`] for the public representation. #[derive(Clone, Copy, Default, PartialEq, Eq)] +#[repr(transparent)] pub struct RawWasmValue(u64); impl Debug for RawWasmValue { @@ -17,6 +18,7 @@ impl Debug for RawWasmValue { } impl RawWasmValue { + #[inline(always)] pub fn raw_value(&self) -> u64 { self.0 } diff --git a/crates/tinywasm/src/store/data.rs b/crates/tinywasm/src/store/data.rs new file mode 100644 index 0000000..efbb858 --- /dev/null +++ b/crates/tinywasm/src/store/data.rs @@ -0,0 +1,27 @@ +use alloc::vec::Vec; +use tinywasm_types::*; + +/// A WebAssembly Data Instance +/// +/// See +#[derive(Debug)] +pub(crate) struct DataInstance { + pub(crate) data: Option>, + pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances +} + +impl DataInstance { + pub(crate) fn new(data: Option>, owner: ModuleInstanceAddr) -> Self { + Self { data, _owner: owner } + } + + pub(crate) fn drop(&mut self) -> Option<()> { + match self.data { + None => None, + Some(_) => { + let _ = self.data.take(); + Some(()) + } + } + } +} diff --git a/crates/tinywasm/src/store/element.rs b/crates/tinywasm/src/store/element.rs new file mode 100644 index 0000000..6563dff --- /dev/null +++ b/crates/tinywasm/src/store/element.rs @@ -0,0 +1,19 @@ +use crate::TableElement; +use alloc::vec::Vec; +use tinywasm_types::*; + +/// A WebAssembly Element Instance +/// +/// See +#[derive(Debug)] +pub(crate) struct ElementInstance { + pub(crate) kind: ElementKind, + pub(crate) items: Option>, // none is the element was dropped + _owner: ModuleInstanceAddr, // index into store.module_instances +} + +impl ElementInstance { + pub(crate) fn new(kind: ElementKind, owner: ModuleInstanceAddr, items: Option>) -> Self { + Self { kind, _owner: owner, items } + } +} diff --git a/crates/tinywasm/src/store/function.rs b/crates/tinywasm/src/store/function.rs new file mode 100644 index 0000000..7508d00 --- /dev/null +++ b/crates/tinywasm/src/store/function.rs @@ -0,0 +1,11 @@ +use crate::Function; +use tinywasm_types::*; + +#[derive(Debug, Clone)] +/// A WebAssembly Function Instance +/// +/// See +pub(crate) struct FunctionInstance { + pub(crate) func: Function, + pub(crate) owner: ModuleInstanceAddr, // index into store.module_instances, none for host functions +} diff --git a/crates/tinywasm/src/store/global.rs b/crates/tinywasm/src/store/global.rs new file mode 100644 index 0000000..298a31e --- /dev/null +++ b/crates/tinywasm/src/store/global.rs @@ -0,0 +1,41 @@ +use alloc::{format, string::ToString}; +use tinywasm_types::*; + +use crate::{runtime::RawWasmValue, Error, Result}; + +/// A WebAssembly Global Instance +/// +/// See +#[derive(Debug)] +pub(crate) struct GlobalInstance { + pub(crate) value: RawWasmValue, + pub(crate) ty: GlobalType, + pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances +} + +impl GlobalInstance { + pub(crate) fn new(ty: GlobalType, value: RawWasmValue, owner: ModuleInstanceAddr) -> Self { + Self { ty, value, _owner: owner } + } + + pub(crate) fn get(&self) -> WasmValue { + self.value.attach_type(self.ty.ty) + } + + pub(crate) fn set(&mut self, val: WasmValue) -> Result<()> { + if val.val_type() != self.ty.ty { + return Err(Error::Other(format!( + "global type mismatch: expected {:?}, got {:?}", + self.ty.ty, + val.val_type() + ))); + } + + if !self.ty.mutable { + return Err(Error::Other("global is immutable".to_string())); + } + + self.value = val.into(); + Ok(()) + } +} diff --git a/crates/tinywasm/src/store/memory.rs b/crates/tinywasm/src/store/memory.rs new file mode 100644 index 0000000..9b527d3 --- /dev/null +++ b/crates/tinywasm/src/store/memory.rs @@ -0,0 +1,296 @@ +use alloc::vec; +use alloc::vec::Vec; +use tinywasm_types::{MemoryType, ModuleInstanceAddr}; + +use crate::{cold, unlikely, Error, Result}; + +pub(crate) const PAGE_SIZE: usize = 65536; +pub(crate) const MAX_PAGES: usize = 65536; +pub(crate) const MAX_SIZE: u64 = PAGE_SIZE as u64 * MAX_PAGES as u64; + +/// A WebAssembly Memory Instance +/// +/// See +#[derive(Debug)] +pub(crate) struct MemoryInstance { + pub(crate) kind: MemoryType, + pub(crate) data: Vec, + pub(crate) page_count: usize, + pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances +} + +impl MemoryInstance { + pub(crate) fn new(kind: MemoryType, owner: ModuleInstanceAddr) -> Self { + assert!(kind.page_count_initial <= kind.page_count_max.unwrap_or(MAX_PAGES as u64)); + log::debug!("initializing memory with {} pages", kind.page_count_initial); + + Self { + kind, + data: vec![0; PAGE_SIZE * kind.page_count_initial as usize], + page_count: kind.page_count_initial as usize, + _owner: owner, + } + } + + pub(crate) fn store(&mut self, addr: usize, _align: usize, data: &[u8], len: usize) -> Result<()> { + let Some(end) = addr.checked_add(len) else { + cold(); + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { + offset: addr, + len: data.len(), + max: self.data.len(), + })); + }; + + if unlikely(end > self.data.len() || end < addr) { + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { + offset: addr, + len: data.len(), + max: self.data.len(), + })); + } + + // WebAssembly doesn't require alignment for stores + #[cfg(not(feature = "unsafe"))] + self.data[addr..end].copy_from_slice(data); + + #[cfg(feature = "unsafe")] + // SAFETY: we checked that `end` is in bounds above, this is the same as `copy_from_slice` + // src must is for reads of count * size_of::() bytes. + // dst must is for writes of count * size_of::() bytes. + // Both src and dst are properly aligned. + // The region of memory beginning at src does not overlap with the region of memory beginning at dst with the same size. + unsafe { + core::ptr::copy_nonoverlapping(data.as_ptr(), self.data[addr..end].as_mut_ptr(), len); + } + + Ok(()) + } + + pub(crate) fn max_pages(&self) -> usize { + self.kind.page_count_max.unwrap_or(MAX_PAGES as u64) as usize + } + + pub(crate) fn load(&self, addr: usize, _align: usize, len: usize) -> Result<&[u8]> { + let Some(end) = addr.checked_add(len) else { + cold(); + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len, max: self.data.len() })); + }; + + if unlikely(end > self.data.len() || end < addr) { + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len, max: self.data.len() })); + } + + Ok(&self.data[addr..end]) + } + + // this is a workaround since we can't use generic const expressions yet (https://github.com/rust-lang/rust/issues/76560) + pub(crate) fn load_as>(&self, addr: usize, _align: usize) -> Result { + let Some(end) = addr.checked_add(SIZE) else { + cold(); + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len: SIZE, max: self.max_pages() })); + }; + + if unlikely(end > self.data.len()) { + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len: SIZE, max: self.data.len() })); + } + + #[cfg(feature = "unsafe")] + // WebAssembly doesn't require alignment for loads + // SAFETY: we checked that `end` is in bounds above. All types that implement `Into` are valid + // to load from unaligned addresses. + let val = unsafe { core::ptr::read_unaligned(self.data[addr..end].as_ptr() as *const T) }; + + #[cfg(not(feature = "unsafe"))] + let val = T::from_le_bytes(self.data[addr..end].try_into().expect("slice size mismatch")); + + Ok(val) + } + + pub(crate) fn page_count(&self) -> usize { + self.page_count + } + + pub(crate) fn fill(&mut self, addr: usize, len: usize, val: u8) -> Result<()> { + let end = addr + .checked_add(len) + .ok_or_else(|| Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len, max: self.data.len() }))?; + if unlikely(end > self.data.len()) { + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len, max: self.data.len() })); + } + + self.data[addr..end].fill(val); + Ok(()) + } + + pub(crate) fn copy_from_slice(&mut self, dst: usize, src: &[u8]) -> Result<()> { + let end = dst.checked_add(src.len()).ok_or_else(|| { + Error::Trap(crate::Trap::MemoryOutOfBounds { offset: dst, len: src.len(), max: self.data.len() }) + })?; + if unlikely(end > self.data.len()) { + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { + offset: dst, + len: src.len(), + max: self.data.len(), + })); + } + + self.data[dst..end].copy_from_slice(src); + Ok(()) + } + + pub(crate) fn copy_within(&mut self, dst: usize, src: usize, len: usize) -> Result<()> { + // Calculate the end of the source slice + let src_end = src + .checked_add(len) + .ok_or_else(|| Error::Trap(crate::Trap::MemoryOutOfBounds { offset: src, len, max: self.data.len() }))?; + if src_end > self.data.len() { + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { offset: src, len, max: self.data.len() })); + } + + // Calculate the end of the destination slice + let dst_end = dst + .checked_add(len) + .ok_or_else(|| Error::Trap(crate::Trap::MemoryOutOfBounds { offset: dst, len, max: self.data.len() }))?; + if dst_end > self.data.len() { + return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { offset: dst, len, max: self.data.len() })); + } + + // Perform the copy + self.data.copy_within(src..src_end, dst); + Ok(()) + } + + pub(crate) fn grow(&mut self, pages_delta: i32) -> Option { + let current_pages = self.page_count(); + let new_pages = current_pages as i64 + pages_delta as i64; + + if new_pages < 0 || new_pages > MAX_PAGES as i64 { + return None; + } + + if new_pages as usize > self.max_pages() { + log::info!("memory size out of bounds: {}", new_pages); + return None; + } + + let new_size = new_pages as usize * PAGE_SIZE; + if new_size as u64 > MAX_SIZE { + return None; + } + + // Zero initialize the new pages + self.data.resize(new_size, 0); + self.page_count = new_pages as usize; + + log::debug!("memory was {} pages", current_pages); + log::debug!("memory grown by {} pages", pages_delta); + log::debug!("memory grown to {} pages", self.page_count); + + Some(current_pages.try_into().expect("memory size out of bounds, this should have been caught earlier")) + } +} + +#[allow(unsafe_code)] +/// A trait for types that can be loaded from memory +/// +/// # Safety +/// Only implemented for primitive types, unsafe to not allow it for other types. +/// Only actually unsafe to implement if the `unsafe` feature is enabled since there might be +/// UB for loading things things like packed structs +pub(crate) unsafe trait MemLoadable: Sized + Copy { + /// Load a value from memory + fn from_le_bytes(bytes: [u8; T]) -> Self; + /// Load a value from memory + fn from_be_bytes(bytes: [u8; T]) -> Self; +} + +macro_rules! impl_mem_loadable_for_primitive { + ($($type:ty, $size:expr),*) => { + $( + #[allow(unsafe_code)] + unsafe impl MemLoadable<$size> for $type { + fn from_le_bytes(bytes: [u8; $size]) -> Self { + <$type>::from_le_bytes(bytes) + } + + fn from_be_bytes(bytes: [u8; $size]) -> Self { + <$type>::from_be_bytes(bytes) + } + } + )* + } +} + +impl_mem_loadable_for_primitive!( + u8, 1, i8, 1, u16, 2, i16, 2, u32, 4, i32, 4, f32, 4, u64, 8, i64, 8, f64, 8, u128, 16, i128, 16 +); + +#[cfg(test)] +mod memory_instance_tests { + use super::*; + use tinywasm_types::{MemoryArch, MemoryType, ModuleInstanceAddr}; + + fn create_test_memory() -> MemoryInstance { + let kind = MemoryType { arch: MemoryArch::I32, page_count_initial: 1, page_count_max: Some(2) }; + let owner = ModuleInstanceAddr::default(); + MemoryInstance::new(kind, owner) + } + + #[test] + fn test_memory_store_and_load() { + let mut memory = create_test_memory(); + let data_to_store = [1, 2, 3, 4]; + assert!(memory.store(0, 0, &data_to_store, data_to_store.len()).is_ok()); + let loaded_data = memory.load(0, 0, data_to_store.len()).unwrap(); + assert_eq!(loaded_data, &data_to_store); + } + + #[test] + fn test_memory_store_out_of_bounds() { + let mut memory = create_test_memory(); + let data_to_store = [1, 2, 3, 4]; + assert!(memory.store(memory.data.len(), 0, &data_to_store, data_to_store.len()).is_err()); + } + + #[test] + fn test_memory_fill() { + let mut memory = create_test_memory(); + assert!(memory.fill(0, 10, 42).is_ok()); + assert_eq!(&memory.data[0..10], &[42; 10]); + } + + #[test] + fn test_memory_fill_out_of_bounds() { + let mut memory = create_test_memory(); + assert!(memory.fill(memory.data.len(), 10, 42).is_err()); + } + + #[test] + fn test_memory_copy_within() { + let mut memory = create_test_memory(); + memory.fill(0, 10, 1).unwrap(); + assert!(memory.copy_within(10, 0, 10).is_ok()); + assert_eq!(&memory.data[10..20], &[1; 10]); + } + + #[test] + fn test_memory_copy_within_out_of_bounds() { + let mut memory = create_test_memory(); + assert!(memory.copy_within(memory.data.len(), 0, 10).is_err()); + } + + #[test] + fn test_memory_grow() { + let mut memory = create_test_memory(); + let original_pages = memory.page_count(); + assert_eq!(memory.grow(1), Some(original_pages as i32)); + assert_eq!(memory.page_count(), original_pages + 1); + } + + #[test] + fn test_memory_grow_out_of_bounds() { + let mut memory = create_test_memory(); + assert!(memory.grow(MAX_PAGES as i32 + 1).is_none()); + } +} diff --git a/crates/tinywasm/src/store.rs b/crates/tinywasm/src/store/mod.rs similarity index 56% rename from crates/tinywasm/src/store.rs rename to crates/tinywasm/src/store/mod.rs index d281b5c..c8c5d8a 100644 --- a/crates/tinywasm/src/store.rs +++ b/crates/tinywasm/src/store/mod.rs @@ -1,5 +1,5 @@ use crate::log; -use alloc::{boxed::Box, format, rc::Rc, string::ToString, vec, vec::Vec}; +use alloc::{boxed::Box, format, rc::Rc, string::ToString, vec::Vec}; use core::{ cell::RefCell, sync::atomic::{AtomicUsize, Ordering}, @@ -11,6 +11,14 @@ use crate::{ Error, Function, ModuleInstance, Result, Trap, }; +mod data; +mod element; +mod function; +mod global; +mod memory; +mod table; +pub(crate) use {data::*, element::*, function::*, global::*, memory::*, table::*}; + // global store id counter static STORE_ID: AtomicUsize = AtomicUsize::new(0); @@ -46,10 +54,13 @@ impl Store { /// Get a module instance by the internal id pub fn get_module_instance(&self, addr: ModuleInstanceAddr) -> Option<&ModuleInstance> { - log::debug!("existing module instances: {:?}", self.module_instances.len()); self.module_instances.get(addr as usize) } + pub(crate) fn get_module_instance_raw(&self, addr: ModuleInstanceAddr) -> ModuleInstance { + self.module_instances[addr as usize].clone() + } + /// Create a new store with the given runtime pub(crate) fn runtime(&self) -> runtime::InterpreterRuntime { match self.runtime { @@ -84,7 +95,7 @@ impl Default for Store { /// Data should only be addressable by the module that owns it /// See pub(crate) struct StoreData { - pub(crate) funcs: Vec>, + pub(crate) funcs: Vec, pub(crate) tables: Vec>>, pub(crate) memories: Vec>>, pub(crate) globals: Vec>>, @@ -112,18 +123,14 @@ impl Store { /// Add functions to the store, returning their addresses in the store pub(crate) fn init_funcs( &mut self, - funcs: Vec<(u32, WasmFunction)>, + funcs: Vec, idx: ModuleInstanceAddr, ) -> Result> { let func_count = self.data.funcs.len(); let mut func_addrs = Vec::with_capacity(func_count); - for (i, (type_idx, func)) in funcs.into_iter().enumerate() { - self.data.funcs.push(Rc::new(FunctionInstance { - func: Function::Wasm(func), - _type_idx: type_idx, - owner: idx, - })); + for (i, func) in funcs.into_iter().enumerate() { + self.data.funcs.push(FunctionInstance { func: Function::Wasm(Rc::new(func.wasm_function)), owner: idx }); func_addrs.push((i + func_count) as FuncAddr); } @@ -193,12 +200,11 @@ impl Store { let addr = globals.get(*addr as usize).copied().ok_or_else(|| { Error::Other(format!("global {} not found. This should have been caught by the validator", addr)) })?; - let global = self.data.globals[addr as usize].clone(); let val = i64::from(global.borrow().value); - log::error!("global: {}", val); + + // check if the global is actually a null reference if val < 0 { - // the global is actually a null reference None } else { Some(val as u32) @@ -217,12 +223,12 @@ impl Store { table_addrs: &[TableAddr], func_addrs: &[FuncAddr], global_addrs: &[Addr], - elements: Vec, + elements: &[Element], idx: ModuleInstanceAddr, ) -> Result<(Box<[Addr]>, Option)> { let elem_count = self.data.elements.len(); let mut elem_addrs = Vec::with_capacity(elem_count); - for (i, element) in elements.into_iter().enumerate() { + for (i, element) in elements.iter().enumerate() { let init = element .items .iter() @@ -285,38 +291,38 @@ impl Store { let data_count = self.data.datas.len(); let mut data_addrs = Vec::with_capacity(data_count); for (i, data) in datas.into_iter().enumerate() { - use tinywasm_types::DataKind::*; - match data.kind { - Active { mem: mem_addr, offset } => { - // a. Assert: memidx == 0 - if mem_addr != 0 { - return Err(Error::UnsupportedFeature("data segments for non-zero memories".to_string())); - } + let data_val = + match data.kind { + tinywasm_types::DataKind::Active { mem: mem_addr, offset } => { + // a. Assert: memidx == 0 + if mem_addr != 0 { + return Err(Error::UnsupportedFeature("data segments for non-zero memories".to_string())); + } - let mem_addr = mem_addrs - .get(mem_addr as usize) - .copied() - .ok_or_else(|| Error::Other(format!("memory {} not found for data segment {}", mem_addr, i)))?; + let mem_addr = mem_addrs.get(mem_addr as usize).copied().ok_or_else(|| { + Error::Other(format!("memory {} not found for data segment {}", mem_addr, i)) + })?; - let offset = self.eval_i32_const(&offset)?; + let offset = self.eval_i32_const(&offset)?; - let mem = - self.data.memories.get_mut(mem_addr as usize).ok_or_else(|| { + let mem = self.data.memories.get_mut(mem_addr as usize).ok_or_else(|| { Error::Other(format!("memory {} not found for data segment {}", mem_addr, i)) })?; - // See comment for active element sections in the function above why we need to do this here - if let Err(Error::Trap(trap)) = mem.borrow_mut().store(offset as usize, 0, &data.data) { - return Ok((data_addrs.into_boxed_slice(), Some(trap))); - } + // See comment for active element sections in the function above why we need to do this here + if let Err(Error::Trap(trap)) = + mem.borrow_mut().store(offset as usize, 0, &data.data, data.data.len()) + { + return Ok((data_addrs.into_boxed_slice(), Some(trap))); + } - // drop the data - continue; - } - Passive => {} - } + // drop the data + None + } + tinywasm_types::DataKind::Passive => Some(data.data.to_vec()), + }; - self.data.datas.push(DataInstance::new(data.data.to_vec(), idx)); + self.data.datas.push(DataInstance::new(data_val, idx)); data_addrs.push((i + data_count) as Addr); } @@ -342,8 +348,8 @@ impl Store { Ok(self.data.memories.len() as MemAddr - 1) } - pub(crate) fn add_func(&mut self, func: Function, type_idx: TypeAddr, idx: ModuleInstanceAddr) -> Result { - self.data.funcs.push(Rc::new(FunctionInstance { func, _type_idx: type_idx, owner: idx })); + pub(crate) fn add_func(&mut self, func: Function, idx: ModuleInstanceAddr) -> Result { + self.data.funcs.push(FunctionInstance { func, owner: idx }); Ok(self.data.funcs.len() as FuncAddr - 1) } @@ -395,7 +401,7 @@ impl Store { } /// Get the function at the actual index in the store - pub(crate) fn get_func(&self, addr: usize) -> Result<&Rc> { + pub(crate) fn get_func(&self, addr: usize) -> Result<&FunctionInstance> { self.data.funcs.get(addr).ok_or_else(|| Error::Other(format!("function {} not found", addr))) } @@ -409,6 +415,16 @@ impl Store { self.data.tables.get(addr).ok_or_else(|| Error::Other(format!("table {} not found", addr))) } + /// Get the data at the actual index in the store + pub(crate) fn get_data(&self, addr: usize) -> Result<&DataInstance> { + self.data.datas.get(addr).ok_or_else(|| Error::Other(format!("table {} not found", addr))) + } + + /// Get the data at the actual index in the store + pub(crate) fn get_data_mut(&mut self, addr: usize) -> Result<&mut DataInstance> { + self.data.datas.get_mut(addr).ok_or_else(|| Error::Other(format!("table {} not found", addr))) + } + /// Get the element at the actual index in the store pub(crate) fn get_elem(&self, addr: usize) -> Result<&ElementInstance> { self.data.elements.get(addr).ok_or_else(|| Error::Other(format!("element {} not found", addr))) @@ -436,289 +452,3 @@ impl Store { .map(|global| global.borrow_mut().value = value) } } - -#[derive(Debug)] -/// A WebAssembly Function Instance -/// -/// See -pub(crate) struct FunctionInstance { - pub(crate) func: Function, - pub(crate) _type_idx: TypeAddr, - pub(crate) owner: ModuleInstanceAddr, // index into store.module_instances, none for host functions -} - -// TODO: check if this actually helps -#[inline(always)] -#[cold] -const fn cold() {} - -impl FunctionInstance { - pub(crate) fn assert_wasm(&self) -> Result<&WasmFunction> { - match &self.func { - Function::Wasm(w) => Ok(w), - Function::Host(_) => { - cold(); - Err(Error::Other("expected wasm function".to_string())) - } - } - } -} - -#[derive(Debug, Clone, Copy)] -pub(crate) enum TableElement { - Uninitialized, - Initialized(Addr), -} - -impl From> for TableElement { - fn from(addr: Option) -> Self { - match addr { - None => TableElement::Uninitialized, - Some(addr) => TableElement::Initialized(addr), - } - } -} - -impl TableElement { - pub(crate) fn addr(&self) -> Option { - match self { - TableElement::Uninitialized => None, - TableElement::Initialized(addr) => Some(*addr), - } - } - - pub(crate) fn map Addr>(self, f: F) -> Self { - match self { - TableElement::Uninitialized => TableElement::Uninitialized, - TableElement::Initialized(addr) => TableElement::Initialized(f(addr)), - } - } -} - -const MAX_TABLE_SIZE: u32 = 10000000; - -/// A WebAssembly Table Instance -/// -/// See -#[derive(Debug)] -pub(crate) struct TableInstance { - pub(crate) elements: Vec, - pub(crate) kind: TableType, - pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances -} - -impl TableInstance { - pub(crate) fn new(kind: TableType, owner: ModuleInstanceAddr) -> Self { - Self { elements: vec![TableElement::Uninitialized; kind.size_initial as usize], kind, _owner: owner } - } - - pub(crate) fn get_wasm_val(&self, addr: usize) -> Result { - let val = self.get(addr)?.addr(); - - Ok(match self.kind.element_type { - ValType::RefFunc => val.map(WasmValue::RefFunc).unwrap_or(WasmValue::RefNull(ValType::RefFunc)), - ValType::RefExtern => val.map(WasmValue::RefExtern).unwrap_or(WasmValue::RefNull(ValType::RefExtern)), - _ => unimplemented!("unsupported table type: {:?}", self.kind.element_type), - }) - } - - pub(crate) fn get(&self, addr: usize) -> Result<&TableElement> { - self.elements.get(addr).ok_or_else(|| Error::Trap(Trap::UndefinedElement { index: addr })) - } - - pub(crate) fn set(&mut self, table_idx: usize, value: Addr) -> Result<()> { - self.grow_to_fit(table_idx + 1).map(|_| self.elements[table_idx] = TableElement::Initialized(value)) - } - - pub(crate) fn grow_to_fit(&mut self, new_size: usize) -> Result<()> { - if new_size > self.elements.len() { - if new_size > self.kind.size_max.unwrap_or(MAX_TABLE_SIZE) as usize { - return Err(crate::Trap::TableOutOfBounds { offset: new_size, len: 1, max: self.elements.len() }.into()); - } - - self.elements.resize(new_size, TableElement::Uninitialized); - } - Ok(()) - } - - pub(crate) fn size(&self) -> i32 { - self.elements.len() as i32 - } - - fn resolve_func_ref(&self, func_addrs: &[u32], addr: Addr) -> Addr { - if self.kind.element_type != ValType::RefFunc { - return addr; - } - - *func_addrs - .get(addr as usize) - .expect("error initializing table: function not found. This should have been caught by the validator") - } - - // Initialize the table with the given elements - pub(crate) fn init_raw(&mut self, offset: i32, init: &[TableElement]) -> Result<()> { - let offset = offset as usize; - let end = offset.checked_add(init.len()).ok_or_else(|| { - Error::Trap(crate::Trap::TableOutOfBounds { offset, len: init.len(), max: self.elements.len() }) - })?; - - if end > self.elements.len() || end < offset { - return Err(crate::Trap::TableOutOfBounds { offset, len: init.len(), max: self.elements.len() }.into()); - } - - self.elements[offset..end].copy_from_slice(init); - log::debug!("table: {:?}", self.elements); - Ok(()) - } - - // Initialize the table with the given elements (resolves function references) - pub(crate) fn init(&mut self, func_addrs: &[u32], offset: i32, init: &[TableElement]) -> Result<()> { - let init = init.iter().map(|item| item.map(|addr| self.resolve_func_ref(func_addrs, addr))).collect::>(); - - self.init_raw(offset, &init) - } -} - -pub(crate) const PAGE_SIZE: usize = 65536; -pub(crate) const MAX_PAGES: usize = 65536; -pub(crate) const MAX_SIZE: u64 = PAGE_SIZE as u64 * MAX_PAGES as u64; - -/// A WebAssembly Memory Instance -/// -/// See -#[derive(Debug)] -pub(crate) struct MemoryInstance { - pub(crate) kind: MemoryType, - pub(crate) data: Vec, - pub(crate) page_count: usize, - pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances -} - -impl MemoryInstance { - pub(crate) fn new(kind: MemoryType, owner: ModuleInstanceAddr) -> Self { - assert!(kind.page_count_initial <= kind.page_count_max.unwrap_or(MAX_PAGES as u64)); - log::debug!("initializing memory with {} pages", kind.page_count_initial); - - Self { - kind, - data: vec![0; PAGE_SIZE * kind.page_count_initial as usize], - page_count: kind.page_count_initial as usize, - _owner: owner, - } - } - - pub(crate) fn store(&mut self, addr: usize, _align: usize, data: &[u8]) -> Result<()> { - let end = addr.checked_add(data.len()).ok_or_else(|| { - Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len: data.len(), max: self.data.len() }) - })?; - - if end > self.data.len() || end < addr { - return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { - offset: addr, - len: data.len(), - max: self.data.len(), - })); - } - - // WebAssembly doesn't require alignment for stores - self.data[addr..end].copy_from_slice(data); - Ok(()) - } - - pub(crate) fn max_pages(&self) -> usize { - self.kind.page_count_max.unwrap_or(MAX_PAGES as u64) as usize - } - - pub(crate) fn load(&self, addr: usize, _align: usize, len: usize) -> Result<&[u8]> { - let end = addr - .checked_add(len) - .ok_or_else(|| Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len, max: self.max_pages() }))?; - - if end > self.data.len() { - return Err(Error::Trap(crate::Trap::MemoryOutOfBounds { offset: addr, len, max: self.data.len() })); - } - - // WebAssembly doesn't require alignment for loads - Ok(&self.data[addr..end]) - } - - pub(crate) fn page_count(&self) -> usize { - self.page_count - } - - pub(crate) fn grow(&mut self, delta: i32) -> Option { - let current_pages = self.page_count(); - let new_pages = current_pages as i64 + delta as i64; - - if new_pages < 0 || new_pages > MAX_PAGES as i64 { - return None; - } - - if new_pages as usize > self.max_pages() { - log::info!("memory size out of bounds: {}", new_pages); - return None; - } - - let new_size = new_pages as usize * PAGE_SIZE; - if new_size as u64 > MAX_SIZE { - return None; - } - - // Zero initialize the new pages - self.data.resize(new_size, 0); - self.page_count = new_pages as usize; - - log::debug!("memory was {} pages", current_pages); - log::debug!("memory grown by {} pages", delta); - log::debug!("memory grown to {} pages", self.page_count); - - Some(current_pages.try_into().expect("memory size out of bounds, this should have been caught earlier")) - } -} - -/// A WebAssembly Global Instance -/// -/// See -#[derive(Debug)] -pub(crate) struct GlobalInstance { - pub(crate) value: RawWasmValue, - pub(crate) ty: GlobalType, - pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances -} - -impl GlobalInstance { - pub(crate) fn new(ty: GlobalType, value: RawWasmValue, owner: ModuleInstanceAddr) -> Self { - Self { ty, value, _owner: owner } - } -} - -/// A WebAssembly Element Instance -/// -/// See -#[derive(Debug)] -pub(crate) struct ElementInstance { - pub(crate) kind: ElementKind, - pub(crate) items: Option>, // none is the element was dropped - _owner: ModuleInstanceAddr, // index into store.module_instances -} - -impl ElementInstance { - pub(crate) fn new(kind: ElementKind, owner: ModuleInstanceAddr, items: Option>) -> Self { - Self { kind, _owner: owner, items } - } -} - -/// A WebAssembly Data Instance -/// -/// See -#[derive(Debug)] -pub(crate) struct DataInstance { - pub(crate) _data: Vec, - pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances -} - -impl DataInstance { - pub(crate) fn new(data: Vec, owner: ModuleInstanceAddr) -> Self { - Self { _data: data, _owner: owner } - } -} diff --git a/crates/tinywasm/src/store/table.rs b/crates/tinywasm/src/store/table.rs new file mode 100644 index 0000000..ea520b8 --- /dev/null +++ b/crates/tinywasm/src/store/table.rs @@ -0,0 +1,119 @@ +use crate::log; +use crate::{Error, Result, Trap}; +use alloc::{vec, vec::Vec}; +use tinywasm_types::*; + +const MAX_TABLE_SIZE: u32 = 10000000; + +/// A WebAssembly Table Instance +/// +/// See +#[derive(Debug)] +pub(crate) struct TableInstance { + pub(crate) elements: Vec, + pub(crate) kind: TableType, + pub(crate) _owner: ModuleInstanceAddr, // index into store.module_instances +} + +impl TableInstance { + pub(crate) fn new(kind: TableType, owner: ModuleInstanceAddr) -> Self { + Self { elements: vec![TableElement::Uninitialized; kind.size_initial as usize], kind, _owner: owner } + } + + pub(crate) fn get_wasm_val(&self, addr: usize) -> Result { + let val = self.get(addr)?.addr(); + + Ok(match self.kind.element_type { + ValType::RefFunc => val.map(WasmValue::RefFunc).unwrap_or(WasmValue::RefNull(ValType::RefFunc)), + ValType::RefExtern => val.map(WasmValue::RefExtern).unwrap_or(WasmValue::RefNull(ValType::RefExtern)), + _ => Err(Error::UnsupportedFeature("non-ref table".into()))?, + }) + } + + pub(crate) fn get(&self, addr: usize) -> Result<&TableElement> { + self.elements.get(addr).ok_or_else(|| Error::Trap(Trap::UndefinedElement { index: addr })) + } + + pub(crate) fn set(&mut self, table_idx: usize, value: Addr) -> Result<()> { + self.grow_to_fit(table_idx + 1).map(|_| self.elements[table_idx] = TableElement::Initialized(value)) + } + + pub(crate) fn grow_to_fit(&mut self, new_size: usize) -> Result<()> { + if new_size > self.elements.len() { + if new_size > self.kind.size_max.unwrap_or(MAX_TABLE_SIZE) as usize { + return Err(crate::Trap::TableOutOfBounds { offset: new_size, len: 1, max: self.elements.len() }.into()); + } + + self.elements.resize(new_size, TableElement::Uninitialized); + } + Ok(()) + } + + pub(crate) fn size(&self) -> i32 { + self.elements.len() as i32 + } + + fn resolve_func_ref(&self, func_addrs: &[u32], addr: Addr) -> Addr { + if self.kind.element_type != ValType::RefFunc { + return addr; + } + + *func_addrs + .get(addr as usize) + .expect("error initializing table: function not found. This should have been caught by the validator") + } + + // Initialize the table with the given elements + pub(crate) fn init_raw(&mut self, offset: i32, init: &[TableElement]) -> Result<()> { + let offset = offset as usize; + let end = offset.checked_add(init.len()).ok_or_else(|| { + Error::Trap(crate::Trap::TableOutOfBounds { offset, len: init.len(), max: self.elements.len() }) + })?; + + if end > self.elements.len() || end < offset { + return Err(crate::Trap::TableOutOfBounds { offset, len: init.len(), max: self.elements.len() }.into()); + } + + self.elements[offset..end].copy_from_slice(init); + log::debug!("table: {:?}", self.elements); + Ok(()) + } + + // Initialize the table with the given elements (resolves function references) + pub(crate) fn init(&mut self, func_addrs: &[u32], offset: i32, init: &[TableElement]) -> Result<()> { + let init = init.iter().map(|item| item.map(|addr| self.resolve_func_ref(func_addrs, addr))).collect::>(); + + self.init_raw(offset, &init) + } +} + +#[derive(Debug, Clone, Copy)] +pub(crate) enum TableElement { + Uninitialized, + Initialized(TableAddr), +} + +impl From> for TableElement { + fn from(addr: Option) -> Self { + match addr { + None => TableElement::Uninitialized, + Some(addr) => TableElement::Initialized(addr), + } + } +} + +impl TableElement { + pub(crate) fn addr(&self) -> Option { + match self { + TableElement::Uninitialized => None, + TableElement::Initialized(addr) => Some(*addr), + } + } + + pub(crate) fn map Addr>(self, f: F) -> Self { + match self { + TableElement::Uninitialized => TableElement::Uninitialized, + TableElement::Initialized(addr) => TableElement::Initialized(f(addr)), + } + } +} diff --git a/crates/tinywasm/tests/charts/progress.rs b/crates/tinywasm/tests/charts/progress.rs index 0bc66a6..1ecc09c 100644 --- a/crates/tinywasm/tests/charts/progress.rs +++ b/crates/tinywasm/tests/charts/progress.rs @@ -6,7 +6,7 @@ use std::path::Path; const FONT: &str = "Victor Mono"; -pub fn create_progress_chart(csv_path: &Path, output_path: &Path) -> Result<()> { +pub fn create_progress_chart(name: &str, csv_path: &Path, output_path: &Path) -> Result<()> { let file = File::open(csv_path)?; let reader = io::BufReader::new(file); @@ -41,7 +41,7 @@ pub fn create_progress_chart(csv_path: &Path, output_path: &Path) -> Result<()> .y_label_area_size(70) .margin(10) .margin_top(20) - .caption("MVP TESTSUITE", (FONT, 30.0, FontStyle::Bold)) + .caption(name, (FONT, 30.0, FontStyle::Bold)) .build_cartesian_2d((0..(versions.len() - 1) as u32).into_segmented(), 0..max_tests)?; chart diff --git a/crates/tinywasm/tests/generate-charts.rs b/crates/tinywasm/tests/generate-charts.rs index e8e323d..ec48703 100644 --- a/crates/tinywasm/tests/generate-charts.rs +++ b/crates/tinywasm/tests/generate-charts.rs @@ -11,13 +11,21 @@ fn generate_charts() -> Result<()> { return Ok(()); } - // Create a line chart charts::create_progress_chart( + "WebAssembly 1.0 Test Suite", std::path::Path::new("./tests/generated/mvp.csv"), std::path::Path::new("./tests/generated/progress-mvp.svg"), )?; println!("created progress chart: ./tests/generated/progress-mvp.svg"); + charts::create_progress_chart( + "WebAssembly 2.0 Test Suite", + std::path::Path::new("./tests/generated/2.0.csv"), + std::path::Path::new("./tests/generated/progress-2.0.svg"), + )?; + + println!("created progress chart: ./tests/generated/progress-2.0.svg"); + Ok(()) } diff --git a/crates/tinywasm/tests/generated/2.0.csv b/crates/tinywasm/tests/generated/2.0.csv index 0d233ee..fbf18ae 100644 --- a/crates/tinywasm/tests/generated/2.0.csv +++ b/crates/tinywasm/tests/generated/2.0.csv @@ -1,2 +1,2 @@ 0.3.0,26722,1161,[{"name":"address.wast","passed":260,"failed":0},{"name":"align.wast","passed":156,"failed":0},{"name":"binary-leb128.wast","passed":91,"failed":0},{"name":"binary.wast","passed":112,"failed":0},{"name":"block.wast","passed":223,"failed":0},{"name":"br.wast","passed":97,"failed":0},{"name":"br_if.wast","passed":118,"failed":0},{"name":"br_table.wast","passed":174,"failed":0},{"name":"bulk.wast","passed":8,"failed":109},{"name":"call.wast","passed":91,"failed":0},{"name":"call_indirect.wast","passed":170,"failed":0},{"name":"comments.wast","passed":8,"failed":0},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":619,"failed":0},{"name":"custom.wast","passed":11,"failed":0},{"name":"data.wast","passed":61,"failed":0},{"name":"elem.wast","passed":98,"failed":1},{"name":"endianness.wast","passed":69,"failed":0},{"name":"exports.wast","passed":96,"failed":0},{"name":"f32.wast","passed":2514,"failed":0},{"name":"f32_bitwise.wast","passed":364,"failed":0},{"name":"f32_cmp.wast","passed":2407,"failed":0},{"name":"f64.wast","passed":2514,"failed":0},{"name":"f64_bitwise.wast","passed":364,"failed":0},{"name":"f64_cmp.wast","passed":2407,"failed":0},{"name":"fac.wast","passed":8,"failed":0},{"name":"float_exprs.wast","passed":900,"failed":0},{"name":"float_literals.wast","passed":163,"failed":0},{"name":"float_memory.wast","passed":90,"failed":0},{"name":"float_misc.wast","passed":441,"failed":0},{"name":"forward.wast","passed":5,"failed":0},{"name":"func.wast","passed":172,"failed":0},{"name":"func_ptrs.wast","passed":36,"failed":0},{"name":"global.wast","passed":110,"failed":0},{"name":"i32.wast","passed":460,"failed":0},{"name":"i64.wast","passed":416,"failed":0},{"name":"if.wast","passed":241,"failed":0},{"name":"imports.wast","passed":171,"failed":12},{"name":"inline-module.wast","passed":1,"failed":0},{"name":"int_exprs.wast","passed":108,"failed":0},{"name":"int_literals.wast","passed":51,"failed":0},{"name":"labels.wast","passed":29,"failed":0},{"name":"left-to-right.wast","passed":96,"failed":0},{"name":"linking.wast","passed":132,"failed":0},{"name":"load.wast","passed":97,"failed":0},{"name":"local_get.wast","passed":36,"failed":0},{"name":"local_set.wast","passed":53,"failed":0},{"name":"local_tee.wast","passed":97,"failed":0},{"name":"loop.wast","passed":120,"failed":0},{"name":"memory.wast","passed":79,"failed":0},{"name":"memory_copy.wast","passed":3928,"failed":522},{"name":"memory_fill.wast","passed":64,"failed":36},{"name":"memory_grow.wast","passed":96,"failed":0},{"name":"memory_init.wast","passed":177,"failed":63},{"name":"memory_redundancy.wast","passed":8,"failed":0},{"name":"memory_size.wast","passed":42,"failed":0},{"name":"memory_trap.wast","passed":182,"failed":0},{"name":"names.wast","passed":486,"failed":0},{"name":"nop.wast","passed":88,"failed":0},{"name":"ref_func.wast","passed":9,"failed":8},{"name":"ref_is_null.wast","passed":4,"failed":12},{"name":"ref_null.wast","passed":1,"failed":2},{"name":"return.wast","passed":84,"failed":0},{"name":"select.wast","passed":148,"failed":0},{"name":"skip-stack-guard-page.wast","passed":11,"failed":0},{"name":"stack.wast","passed":7,"failed":0},{"name":"start.wast","passed":20,"failed":0},{"name":"store.wast","passed":68,"failed":0},{"name":"switch.wast","passed":28,"failed":0},{"name":"table-sub.wast","passed":2,"failed":0},{"name":"table.wast","passed":19,"failed":0},{"name":"table_copy.wast","passed":1613,"failed":115},{"name":"table_fill.wast","passed":23,"failed":22},{"name":"table_get.wast","passed":10,"failed":6},{"name":"table_grow.wast","passed":21,"failed":29},{"name":"table_init.wast","passed":594,"failed":186},{"name":"table_set.wast","passed":19,"failed":7},{"name":"table_size.wast","passed":8,"failed":31},{"name":"token.wast","passed":58,"failed":0},{"name":"traps.wast","passed":36,"failed":0},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":64,"failed":0},{"name":"unreached-invalid.wast","passed":118,"failed":0},{"name":"unreached-valid.wast","passed":7,"failed":0},{"name":"unwind.wast","passed":50,"failed":0},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":176,"failed":0}] -0.4.0-alpha.0,26841,1042,[{"name":"address.wast","passed":260,"failed":0},{"name":"align.wast","passed":156,"failed":0},{"name":"binary-leb128.wast","passed":91,"failed":0},{"name":"binary.wast","passed":112,"failed":0},{"name":"block.wast","passed":223,"failed":0},{"name":"br.wast","passed":97,"failed":0},{"name":"br_if.wast","passed":118,"failed":0},{"name":"br_table.wast","passed":174,"failed":0},{"name":"bulk.wast","passed":8,"failed":109},{"name":"call.wast","passed":91,"failed":0},{"name":"call_indirect.wast","passed":170,"failed":0},{"name":"comments.wast","passed":8,"failed":0},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":619,"failed":0},{"name":"custom.wast","passed":11,"failed":0},{"name":"data.wast","passed":61,"failed":0},{"name":"elem.wast","passed":99,"failed":0},{"name":"endianness.wast","passed":69,"failed":0},{"name":"exports.wast","passed":96,"failed":0},{"name":"f32.wast","passed":2514,"failed":0},{"name":"f32_bitwise.wast","passed":364,"failed":0},{"name":"f32_cmp.wast","passed":2407,"failed":0},{"name":"f64.wast","passed":2514,"failed":0},{"name":"f64_bitwise.wast","passed":364,"failed":0},{"name":"f64_cmp.wast","passed":2407,"failed":0},{"name":"fac.wast","passed":8,"failed":0},{"name":"float_exprs.wast","passed":900,"failed":0},{"name":"float_literals.wast","passed":163,"failed":0},{"name":"float_memory.wast","passed":90,"failed":0},{"name":"float_misc.wast","passed":441,"failed":0},{"name":"forward.wast","passed":5,"failed":0},{"name":"func.wast","passed":172,"failed":0},{"name":"func_ptrs.wast","passed":36,"failed":0},{"name":"global.wast","passed":110,"failed":0},{"name":"i32.wast","passed":460,"failed":0},{"name":"i64.wast","passed":416,"failed":0},{"name":"if.wast","passed":241,"failed":0},{"name":"imports.wast","passed":183,"failed":0},{"name":"inline-module.wast","passed":1,"failed":0},{"name":"int_exprs.wast","passed":108,"failed":0},{"name":"int_literals.wast","passed":51,"failed":0},{"name":"labels.wast","passed":29,"failed":0},{"name":"left-to-right.wast","passed":96,"failed":0},{"name":"linking.wast","passed":132,"failed":0},{"name":"load.wast","passed":97,"failed":0},{"name":"local_get.wast","passed":36,"failed":0},{"name":"local_set.wast","passed":53,"failed":0},{"name":"local_tee.wast","passed":97,"failed":0},{"name":"loop.wast","passed":120,"failed":0},{"name":"memory.wast","passed":79,"failed":0},{"name":"memory_copy.wast","passed":3928,"failed":522},{"name":"memory_fill.wast","passed":64,"failed":36},{"name":"memory_grow.wast","passed":96,"failed":0},{"name":"memory_init.wast","passed":177,"failed":63},{"name":"memory_redundancy.wast","passed":8,"failed":0},{"name":"memory_size.wast","passed":42,"failed":0},{"name":"memory_trap.wast","passed":182,"failed":0},{"name":"names.wast","passed":486,"failed":0},{"name":"nop.wast","passed":88,"failed":0},{"name":"ref_func.wast","passed":9,"failed":8},{"name":"ref_is_null.wast","passed":4,"failed":12},{"name":"ref_null.wast","passed":1,"failed":2},{"name":"return.wast","passed":84,"failed":0},{"name":"select.wast","passed":148,"failed":0},{"name":"skip-stack-guard-page.wast","passed":11,"failed":0},{"name":"stack.wast","passed":7,"failed":0},{"name":"start.wast","passed":20,"failed":0},{"name":"store.wast","passed":68,"failed":0},{"name":"switch.wast","passed":28,"failed":0},{"name":"table-sub.wast","passed":2,"failed":0},{"name":"table.wast","passed":19,"failed":0},{"name":"table_copy.wast","passed":1613,"failed":115},{"name":"table_fill.wast","passed":23,"failed":22},{"name":"table_get.wast","passed":10,"failed":6},{"name":"table_grow.wast","passed":21,"failed":29},{"name":"table_init.wast","passed":700,"failed":80},{"name":"table_set.wast","passed":19,"failed":7},{"name":"table_size.wast","passed":8,"failed":31},{"name":"token.wast","passed":58,"failed":0},{"name":"traps.wast","passed":36,"failed":0},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":64,"failed":0},{"name":"unreached-invalid.wast","passed":118,"failed":0},{"name":"unreached-valid.wast","passed":7,"failed":0},{"name":"unwind.wast","passed":50,"failed":0},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":176,"failed":0}] +0.4.0-alpha.0,27549,334,[{"name":"address.wast","passed":260,"failed":0},{"name":"align.wast","passed":156,"failed":0},{"name":"binary-leb128.wast","passed":91,"failed":0},{"name":"binary.wast","passed":112,"failed":0},{"name":"block.wast","passed":223,"failed":0},{"name":"br.wast","passed":97,"failed":0},{"name":"br_if.wast","passed":118,"failed":0},{"name":"br_table.wast","passed":174,"failed":0},{"name":"bulk.wast","passed":75,"failed":42},{"name":"call.wast","passed":91,"failed":0},{"name":"call_indirect.wast","passed":170,"failed":0},{"name":"comments.wast","passed":8,"failed":0},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":619,"failed":0},{"name":"custom.wast","passed":11,"failed":0},{"name":"data.wast","passed":61,"failed":0},{"name":"elem.wast","passed":99,"failed":0},{"name":"endianness.wast","passed":69,"failed":0},{"name":"exports.wast","passed":96,"failed":0},{"name":"f32.wast","passed":2514,"failed":0},{"name":"f32_bitwise.wast","passed":364,"failed":0},{"name":"f32_cmp.wast","passed":2407,"failed":0},{"name":"f64.wast","passed":2514,"failed":0},{"name":"f64_bitwise.wast","passed":364,"failed":0},{"name":"f64_cmp.wast","passed":2407,"failed":0},{"name":"fac.wast","passed":8,"failed":0},{"name":"float_exprs.wast","passed":900,"failed":0},{"name":"float_literals.wast","passed":163,"failed":0},{"name":"float_memory.wast","passed":90,"failed":0},{"name":"float_misc.wast","passed":441,"failed":0},{"name":"forward.wast","passed":5,"failed":0},{"name":"func.wast","passed":172,"failed":0},{"name":"func_ptrs.wast","passed":36,"failed":0},{"name":"global.wast","passed":110,"failed":0},{"name":"i32.wast","passed":460,"failed":0},{"name":"i64.wast","passed":416,"failed":0},{"name":"if.wast","passed":241,"failed":0},{"name":"imports.wast","passed":183,"failed":0},{"name":"inline-module.wast","passed":1,"failed":0},{"name":"int_exprs.wast","passed":108,"failed":0},{"name":"int_literals.wast","passed":51,"failed":0},{"name":"labels.wast","passed":29,"failed":0},{"name":"left-to-right.wast","passed":96,"failed":0},{"name":"linking.wast","passed":132,"failed":0},{"name":"load.wast","passed":97,"failed":0},{"name":"local_get.wast","passed":36,"failed":0},{"name":"local_set.wast","passed":53,"failed":0},{"name":"local_tee.wast","passed":97,"failed":0},{"name":"loop.wast","passed":120,"failed":0},{"name":"memory.wast","passed":79,"failed":0},{"name":"memory_copy.wast","passed":4450,"failed":0},{"name":"memory_fill.wast","passed":100,"failed":0},{"name":"memory_grow.wast","passed":96,"failed":0},{"name":"memory_init.wast","passed":240,"failed":0},{"name":"memory_redundancy.wast","passed":8,"failed":0},{"name":"memory_size.wast","passed":42,"failed":0},{"name":"memory_trap.wast","passed":182,"failed":0},{"name":"names.wast","passed":486,"failed":0},{"name":"nop.wast","passed":88,"failed":0},{"name":"ref_func.wast","passed":9,"failed":8},{"name":"ref_is_null.wast","passed":4,"failed":12},{"name":"ref_null.wast","passed":1,"failed":2},{"name":"return.wast","passed":84,"failed":0},{"name":"select.wast","passed":148,"failed":0},{"name":"skip-stack-guard-page.wast","passed":11,"failed":0},{"name":"stack.wast","passed":7,"failed":0},{"name":"start.wast","passed":20,"failed":0},{"name":"store.wast","passed":68,"failed":0},{"name":"switch.wast","passed":28,"failed":0},{"name":"table-sub.wast","passed":2,"failed":0},{"name":"table.wast","passed":19,"failed":0},{"name":"table_copy.wast","passed":1613,"failed":115},{"name":"table_fill.wast","passed":23,"failed":22},{"name":"table_get.wast","passed":10,"failed":6},{"name":"table_grow.wast","passed":21,"failed":29},{"name":"table_init.wast","passed":720,"failed":60},{"name":"table_set.wast","passed":19,"failed":7},{"name":"table_size.wast","passed":8,"failed":31},{"name":"token.wast","passed":58,"failed":0},{"name":"traps.wast","passed":36,"failed":0},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":64,"failed":0},{"name":"unreached-invalid.wast","passed":118,"failed":0},{"name":"unreached-valid.wast","passed":7,"failed":0},{"name":"unwind.wast","passed":50,"failed":0},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":176,"failed":0}] diff --git a/crates/tinywasm/tests/generated/README.md b/crates/tinywasm/tests/generated/README.md new file mode 100644 index 0000000..40acee5 --- /dev/null +++ b/crates/tinywasm/tests/generated/README.md @@ -0,0 +1,7 @@ +# WebAssembly 1.0 Test Results (out of 20254 tests) + +![](./progress-mvp.svg) + +# WebAssembly 2.0 Test Results (out of 27883 tests) + +![](./progress-2.0.svg) diff --git a/crates/tinywasm/tests/generated/progress-2.0.svg b/crates/tinywasm/tests/generated/progress-2.0.svg new file mode 100644 index 0000000..c869fcb --- /dev/null +++ b/crates/tinywasm/tests/generated/progress-2.0.svg @@ -0,0 +1,54 @@ + + + +WebAssembly 2.0 Test Suite + + +Tests Passed + + +TinyWasm Version + + + + + + + + + +0 + + + +5000 + + + +10000 + + + +15000 + + + +20000 + + + +25000 + + + + +v0.3.0 (26722) + + + +v0.4.0-alpha.0 (27464) + + + + + diff --git a/crates/tinywasm/tests/generated/progress-mvp.svg b/crates/tinywasm/tests/generated/progress-mvp.svg index c1200f5..b8a3e35 100644 --- a/crates/tinywasm/tests/generated/progress-mvp.svg +++ b/crates/tinywasm/tests/generated/progress-mvp.svg @@ -1,7 +1,7 @@ -MVP TESTSUITE +WebAssembly 1.0 Test Suite Tests Passed @@ -56,9 +56,9 @@ v0.2.0 (19344) v0.3.0 (20254) + + - - diff --git a/crates/tinywasm/tests/test-wast.rs b/crates/tinywasm/tests/test-wast.rs index a50a612..1d3fbe3 100644 --- a/crates/tinywasm/tests/test-wast.rs +++ b/crates/tinywasm/tests/test-wast.rs @@ -13,8 +13,8 @@ fn main() -> Result<()> { } if args.len() < 3 { - bail!("usage: cargo test-wast "); - } + bail!("usage: cargo test-wast ") + }; // cwd for relative paths, absolute paths are kept as-is let cwd = std::env::current_dir()?; diff --git a/crates/tinywasm/tests/testsuite/run.rs b/crates/tinywasm/tests/testsuite/run.rs index 79d9acc..125a9d6 100644 --- a/crates/tinywasm/tests/testsuite/run.rs +++ b/crates/tinywasm/tests/testsuite/run.rs @@ -137,7 +137,7 @@ impl TestSuite { for (name, addr) in modules { log::debug!("registering module: {}", name); - imports.link_module(&name, *addr)?; + imports.link_module(name, *addr)?; } Ok(imports) @@ -199,7 +199,7 @@ impl TestSuite { QuoteWat::QuoteModule(_, quoted_wat) => { let wat = quoted_wat .iter() - .map(|(_, s)| std::str::from_utf8(&s).expect("failed to convert wast to utf8")) + .map(|(_, s)| std::str::from_utf8(s).expect("failed to convert wast to utf8")) .collect::>() .join("\n"); @@ -413,7 +413,7 @@ impl TestSuite { AssertReturn { span, exec, results } => { info!("AssertReturn: {:?}", exec); - let expected = convert_wastret(results)?; + let expected = convert_wastret(results.into_iter())?; let invoke = match match exec { wast::WastExecute::Wat(_) => Err(eyre!("wat not supported")), @@ -428,7 +428,7 @@ impl TestSuite { continue; }; - let module_global = match match module.export(global) { + let module_global = match match module.export_addr(global) { Some(ExternVal::Global(addr)) => { store.get_global_val(addr as usize).map_err(|_| eyre!("failed to get global")) } @@ -444,7 +444,7 @@ impl TestSuite { continue; } }; - let expected = expected.get(0).expect("expected global value"); + let expected = expected.first().expect("expected global value"); let module_global = module_global.attach_type(expected.val_type()); if !module_global.eq_loose(expected) { diff --git a/crates/tinywasm/tests/testsuite/util.rs b/crates/tinywasm/tests/testsuite/util.rs index b74eec5..1b91e1e 100644 --- a/crates/tinywasm/tests/testsuite/util.rs +++ b/crates/tinywasm/tests/testsuite/util.rs @@ -25,7 +25,7 @@ pub fn exec_fn_instance( return Err(tinywasm::Error::Other("no instance found".to_string())); }; - let func = instance.exported_func_by_name(store, name)?; + let func = instance.exported_func_untyped(store, name)?; func.call(store, args) } @@ -42,7 +42,7 @@ pub fn exec_fn( let mut store = tinywasm::Store::new(); let module = tinywasm::Module::from(module); let instance = module.instantiate(&mut store, imports)?; - instance.exported_func_by_name(&store, name)?.call(&mut store, args) + instance.exported_func_untyped(&store, name)?.call(&mut store, args) } pub fn catch_unwind_silent R, R>(f: F) -> std::thread::Result { @@ -62,8 +62,8 @@ pub fn convert_wastargs(args: Vec) -> Result) -> Result> { - args.into_iter().map(|a| wastret2tinywasmvalue(a)).collect() +pub fn convert_wastret<'a>(args: impl Iterator>) -> Result> { + args.map(|a| wastret2tinywasmvalue(a)).collect() } fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result { diff --git a/crates/types/Cargo.toml b/crates/types/Cargo.toml index bc6769a..76e5679 100644 --- a/crates/types/Cargo.toml +++ b/crates/types/Cargo.toml @@ -9,10 +9,12 @@ repository.workspace=true [dependencies] log={version="0.4", optional=true} -rkyv={version="0.7", optional=true, default-features=false, features=["size_32"]} +rkyv={version="0.7", optional=true, default-features=false, features=["size_32", "validation"]} +bytecheck={version="0.7", optional=true} [features] -default=["std", "logging"] -std=["rkyv/std"] -serialize=["dep:rkyv", "dep:log"] +default=["std", "logging", "archive", "unsafe"] +std=["rkyv?/std"] +archive=["dep:rkyv", "dep:bytecheck"] logging=["dep:log"] +unsafe=[] diff --git a/crates/types/README.md b/crates/types/README.md index f2d048b..5a4431e 100644 --- a/crates/types/README.md +++ b/crates/types/README.md @@ -1,3 +1,3 @@ -# `tinywasm_types` +# `tinywasm-types` -This crate contains the types used by the [`tinywasm`](https://crates.io/crates/tinywasm) crate. It is also used by the [`tinywasm_parser`](https://crates.io/crates/tinywasm_parser) crate to parse WebAssembly binaries. +This crate contains the types used by the [`tinywasm`](https://crates.io/crates/tinywasm) crate. It is also used by the [`tinywasm-parser`](https://crates.io/crates/tinywasm-parser) crate to parse WebAssembly binaries. diff --git a/crates/types/src/archive.rs b/crates/types/src/archive.rs new file mode 100644 index 0000000..bbd2206 --- /dev/null +++ b/crates/types/src/archive.rs @@ -0,0 +1,113 @@ +use core::fmt::{Display, Formatter}; + +use crate::TinyWasmModule; +use rkyv::{ + check_archived_root, + ser::{serializers::AllocSerializer, Serializer}, + Deserialize, +}; + +// 16 bytes +const TWASM_MAGIC_PREFIX: &[u8; 4] = b"TWAS"; +const TWASM_VERSION: &[u8; 2] = b"01"; + +#[rustfmt::skip] +const TWASM_MAGIC: [u8; 16] = [ TWASM_MAGIC_PREFIX[0], TWASM_MAGIC_PREFIX[1], TWASM_MAGIC_PREFIX[2], TWASM_MAGIC_PREFIX[3], TWASM_VERSION[0], TWASM_VERSION[1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + +pub use rkyv::AlignedVec; + +fn validate_magic(wasm: &[u8]) -> Result { + if wasm.len() < TWASM_MAGIC.len() || &wasm[..TWASM_MAGIC_PREFIX.len()] != TWASM_MAGIC_PREFIX { + return Err(TwasmError::InvalidMagic); + } + if &wasm[TWASM_MAGIC_PREFIX.len()..TWASM_MAGIC_PREFIX.len() + TWASM_VERSION.len()] != TWASM_VERSION { + return Err(TwasmError::InvalidVersion); + } + if wasm[TWASM_MAGIC_PREFIX.len() + TWASM_VERSION.len()..TWASM_MAGIC.len()] != [0; 10] { + return Err(TwasmError::InvalidPadding); + } + + Ok(TWASM_MAGIC.len()) +} + +#[derive(Debug)] +pub enum TwasmError { + InvalidMagic, + InvalidVersion, + InvalidPadding, + InvalidArchive, +} + +impl Display for TwasmError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match self { + TwasmError::InvalidMagic => write!(f, "Invalid twasm: invalid magic number"), + TwasmError::InvalidVersion => write!(f, "Invalid twasm: invalid version"), + TwasmError::InvalidPadding => write!(f, "Invalid twasm: invalid padding"), + TwasmError::InvalidArchive => write!(f, "Invalid twasm: invalid archive"), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for TwasmError {} + +impl TinyWasmModule { + /// Creates a TinyWasmModule from a slice of bytes. + pub fn from_twasm(wasm: &[u8]) -> Result { + let len = validate_magic(wasm)?; + let root = check_archived_root::(&wasm[len..]).map_err(|_e| { + crate::log::error!("Invalid archive: {}", _e); + TwasmError::InvalidArchive + })?; + + Ok(root.deserialize(&mut rkyv::Infallible).unwrap()) + } + + #[cfg(feature = "unsafe")] + #[allow(unsafe_code)] + /// Creates a TinyWasmModule from a slice of bytes. + /// + /// # Safety + /// This function is only safe to call if the bytes have been created by + /// a trusted source. Otherwise, it may cause undefined behavior. + pub unsafe fn from_twasm_unchecked(wasm: &[u8]) -> Self { + let len = validate_magic(wasm).unwrap(); + rkyv::archived_root::(&wasm[len..]).deserialize(&mut rkyv::Infallible).unwrap() + } + + /// Serializes the TinyWasmModule into a vector of bytes. + /// AlignedVec can be deferenced as a slice of bytes and + /// implements io::Write when the `std` feature is enabled. + pub fn serialize_twasm(&self) -> rkyv::AlignedVec { + let mut serializer = AllocSerializer::<0>::default(); + serializer.pad(TWASM_MAGIC.len()).unwrap(); + serializer.serialize_value(self).unwrap(); + let mut out = serializer.into_serializer().into_inner(); + out[..TWASM_MAGIC.len()].copy_from_slice(&TWASM_MAGIC); + out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_serialize() { + let wasm = TinyWasmModule::default(); + let twasm = wasm.serialize_twasm(); + let wasm2 = TinyWasmModule::from_twasm(&twasm).unwrap(); + assert_eq!(wasm, wasm2); + } + + #[cfg(feature = "unsafe")] + #[test] + fn test_serialize_unchecked() { + let wasm = TinyWasmModule::default(); + let twasm = wasm.serialize_twasm(); + #[allow(unsafe_code)] + let wasm2 = unsafe { TinyWasmModule::from_twasm_unchecked(&twasm) }; + assert_eq!(wasm, wasm2); + } +} diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index d5de50a..0e2eafe 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -1,8 +1,10 @@ -use crate::{ElemAddr, MemAddr}; +use crate::{DataAddr, ElemAddr, MemAddr}; use super::{FuncAddr, GlobalAddr, LabelAddr, LocalAddr, TableAddr, TypeAddr, ValType}; #[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum BlockArgs { Empty, Type(ValType), @@ -10,7 +12,9 @@ pub enum BlockArgs { } /// Represents a memory immediate in a WebAssembly memory instruction. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct MemoryArg { pub mem_addr: MemAddr, pub align: u8, @@ -24,6 +28,8 @@ type EndOffset = usize; type ElseOffset = usize; #[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum ConstInstruction { I32Const(i32), I64Const(i64), @@ -43,10 +49,12 @@ pub enum ConstInstruction { /// * `br_table` stores the jump lables in the following `br_label` instructions to keep this enum small. /// * Lables/Blocks: we store the label end offset in the instruction itself and /// have seperate EndBlockFrame and EndFunc instructions to mark the end of a block or function. -/// This makes it easier to implement the label stack (we call it BlockFrameStack) iteratively. +/// This makes it easier to implement the label stack iteratively. /// /// See #[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum Instruction { // Custom Instructions BrLabel(LabelAddr), @@ -266,4 +274,10 @@ pub enum Instruction { TableGrow(TableAddr), TableSize(TableAddr), TableFill(TableAddr), + + // Bulk Memory Instructions + MemoryInit(MemAddr, DataAddr), + MemoryCopy(MemAddr, MemAddr), + MemoryFill(MemAddr), + DataDrop(DataAddr), } diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index d0d854c..205ec5a 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -1,17 +1,21 @@ -#![no_std] -#![forbid(unsafe_code)] #![doc(test( no_crate_inject, attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_assignments, unused_variables)) ))] #![warn(missing_debug_implementations, rust_2018_idioms, unreachable_pub)] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(not(feature = "unsafe"), forbid(unsafe_code))] +#![cfg_attr(feature = "unsafe", deny(unused_unsafe))] //! Types used by [`tinywasm`](https://docs.rs/tinywasm) and [`tinywasm_parser`](https://docs.rs/tinywasm_parser). extern crate alloc; +use alloc::boxed::Box; +use core::{fmt::Debug, ops::Range}; // log for logging (optional). #[cfg(feature = "logging")] +#[allow(clippy::single_component_path_imports)] use log; #[cfg(not(feature = "logging"))] @@ -22,27 +26,29 @@ pub(crate) mod log { } mod instructions; -use core::{fmt::Debug, ops::Range}; - -use alloc::boxed::Box; +mod value; pub use instructions::*; +pub use value::*; + +#[cfg(feature = "archive")] +pub mod archive; /// A TinyWasm WebAssembly Module /// /// This is the internal representation of a WebAssembly module in TinyWasm. /// TinyWasmModules are validated before being created, so they are guaranteed to be valid (as long as they were created by TinyWasm). /// This means you should not trust a TinyWasmModule created by a third party to be valid. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct TinyWasmModule { /// The version of the WebAssembly module. pub version: Option, - /// The start function of the WebAssembly module. pub start_func: Option, /// The functions of the WebAssembly module. - pub funcs: Box<[(u32, WasmFunction)]>, - + pub funcs: Box<[TypedWasmFunction]>, /// The types of the WebAssembly module. pub func_types: Box<[FuncType]>, @@ -68,217 +74,12 @@ pub struct TinyWasmModule { pub elements: Box<[Element]>, } -/// A WebAssembly value. -/// -/// See -#[derive(Clone, PartialEq, Copy)] -pub enum WasmValue { - // Num types - /// A 32-bit integer. - I32(i32), - /// A 64-bit integer. - I64(i64), - /// A 32-bit float. - F32(f32), - /// A 64-bit float. - F64(f64), - - RefExtern(ExternAddr), - RefFunc(FuncAddr), - RefNull(ValType), -} - -impl WasmValue { - pub fn const_instr(&self) -> ConstInstruction { - match self { - Self::I32(i) => ConstInstruction::I32Const(*i), - Self::I64(i) => ConstInstruction::I64Const(*i), - Self::F32(i) => ConstInstruction::F32Const(*i), - Self::F64(i) => ConstInstruction::F64Const(*i), - - Self::RefFunc(i) => ConstInstruction::RefFunc(*i), - Self::RefNull(ty) => ConstInstruction::RefNull(*ty), - - // Self::RefExtern(addr) => ConstInstruction::RefExtern(*addr), - _ => unimplemented!("no const_instr for {:?}", self), - } - } - - /// Get the default value for a given type. - pub fn default_for(ty: ValType) -> Self { - match ty { - ValType::I32 => Self::I32(0), - ValType::I64 => Self::I64(0), - ValType::F32 => Self::F32(0.0), - ValType::F64 => Self::F64(0.0), - ValType::RefFunc => Self::RefNull(ValType::RefFunc), - ValType::RefExtern => Self::RefNull(ValType::RefExtern), - } - } - - pub fn eq_loose(&self, other: &Self) -> bool { - match (self, other) { - (Self::I32(a), Self::I32(b)) => a == b, - (Self::I64(a), Self::I64(b)) => a == b, - (Self::RefNull(v), Self::RefNull(v2)) => v == v2, - (Self::RefExtern(addr), Self::RefExtern(addr2)) => addr == addr2, - (Self::RefFunc(addr), Self::RefFunc(addr2)) => addr == addr2, - (Self::F32(a), Self::F32(b)) => { - if a.is_nan() && b.is_nan() { - true // Both are NaN, treat them as equal - } else { - a.to_bits() == b.to_bits() - } - } - (Self::F64(a), Self::F64(b)) => { - if a.is_nan() && b.is_nan() { - true // Both are NaN, treat them as equal - } else { - a.to_bits() == b.to_bits() - } - } - _ => false, - } - } -} - -impl From for WasmValue { - fn from(i: i32) -> Self { - Self::I32(i) - } -} - -impl From for WasmValue { - fn from(i: i64) -> Self { - Self::I64(i) - } -} - -impl From for WasmValue { - fn from(i: f32) -> Self { - Self::F32(i) - } -} - -impl From for WasmValue { - fn from(i: f64) -> Self { - Self::F64(i) - } -} - -impl TryFrom for i32 { - type Error = (); - - fn try_from(value: WasmValue) -> Result { - match value { - WasmValue::I32(i) => Ok(i), - _ => { - log::error!("i32: try_from failed: {:?}", value); - Err(()) - } - } - } -} - -impl TryFrom for i64 { - type Error = (); - - fn try_from(value: WasmValue) -> Result { - match value { - WasmValue::I64(i) => Ok(i), - _ => { - log::error!("i64: try_from failed: {:?}", value); - Err(()) - } - } - } -} - -impl TryFrom for f32 { - type Error = (); - - fn try_from(value: WasmValue) -> Result { - match value { - WasmValue::F32(i) => Ok(i), - _ => { - log::error!("f32: try_from failed: {:?}", value); - Err(()) - } - } - } -} - -impl TryFrom for f64 { - type Error = (); - - fn try_from(value: WasmValue) -> Result { - match value { - WasmValue::F64(i) => Ok(i), - _ => { - log::error!("f64: try_from failed: {:?}", value); - Err(()) - } - } - } -} - -impl Debug for WasmValue { - fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result { - match self { - WasmValue::I32(i) => write!(f, "i32({})", i), - WasmValue::I64(i) => write!(f, "i64({})", i), - WasmValue::F32(i) => write!(f, "f32({})", i), - WasmValue::F64(i) => write!(f, "f64({})", i), - WasmValue::RefExtern(addr) => write!(f, "ref.extern({:?})", addr), - WasmValue::RefFunc(addr) => write!(f, "ref.func({:?})", addr), - WasmValue::RefNull(ty) => write!(f, "ref.null({:?})", ty), - // WasmValue::V128(i) => write!(f, "v128({})", i), - } - } -} - -impl WasmValue { - /// Get the type of a [`WasmValue`] - pub fn val_type(&self) -> ValType { - match self { - Self::I32(_) => ValType::I32, - Self::I64(_) => ValType::I64, - Self::F32(_) => ValType::F32, - Self::F64(_) => ValType::F64, - Self::RefExtern(_) => ValType::RefExtern, - Self::RefFunc(_) => ValType::RefFunc, - Self::RefNull(ty) => *ty, - } - } -} - -/// Type of a WebAssembly value. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum ValType { - /// A 32-bit integer. - I32, - /// A 64-bit integer. - I64, - /// A 32-bit float. - F32, - /// A 64-bit float. - F64, - /// A reference to a function. - RefFunc, - /// A reference to an external value. - RefExtern, -} - -impl ValType { - pub fn default_value(&self) -> WasmValue { - WasmValue::default_for(*self) - } -} - /// A WebAssembly External Kind. /// /// See #[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum ExternalKind { /// A WebAssembly Function. Func, @@ -316,25 +117,27 @@ pub type ModuleInstanceAddr = Addr; pub enum ExternVal { Func(FuncAddr), Table(TableAddr), - Mem(MemAddr), + Memory(MemAddr), Global(GlobalAddr), } impl ExternVal { + #[inline] pub fn kind(&self) -> ExternalKind { match self { Self::Func(_) => ExternalKind::Func, Self::Table(_) => ExternalKind::Table, - Self::Mem(_) => ExternalKind::Memory, + Self::Memory(_) => ExternalKind::Memory, Self::Global(_) => ExternalKind::Global, } } + #[inline] pub fn new(kind: ExternalKind, addr: Addr) -> Self { match kind { ExternalKind::Func => Self::Func(addr), ExternalKind::Table => Self::Table(addr), - ExternalKind::Memory => Self::Mem(addr), + ExternalKind::Memory => Self::Memory(addr), ExternalKind::Global => Self::Global(addr), } } @@ -344,6 +147,8 @@ impl ExternVal { /// /// See #[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct FuncType { pub params: Box<[ValType]>, pub results: Box<[ValType]>, @@ -351,20 +156,33 @@ pub struct FuncType { impl FuncType { /// Get the number of parameters of a function type. + #[inline] pub fn empty() -> Self { Self { params: Box::new([]), results: Box::new([]) } } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct WasmFunction { pub instructions: Box<[Instruction]>, pub locals: Box<[ValType]>, pub ty: FuncType, } +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] +pub struct TypedWasmFunction { + pub type_addr: u32, + pub wasm_function: WasmFunction, +} + /// A WebAssembly Module Export -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct Export { /// The name of the export. pub name: Box, @@ -374,19 +192,25 @@ pub struct Export { pub index: u32, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct Global { pub ty: GlobalType, pub init: ConstInstruction, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct GlobalType { pub mutable: bool, pub ty: ValType, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct TableType { pub element_type: ValType, pub size_initial: u32, @@ -403,10 +227,12 @@ impl TableType { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] /// Represents a memory's type. -#[derive(Copy, PartialEq, Eq, Hash)] +#[derive(Copy)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct MemoryType { pub arch: MemoryArch, pub page_count_initial: u64, @@ -417,30 +243,28 @@ impl MemoryType { pub fn new_32(page_count_initial: u64, page_count_max: Option) -> Self { Self { arch: MemoryArch::I32, page_count_initial, page_count_max } } - - // pub fn new_64(page_count_initial: u64, page_count_max: Option) -> Self { - // Self { - // arch: MemoryArch::I64, - // page_count_initial, - // page_count_max, - // } - // } } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum MemoryArch { I32, I64, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct Import { pub module: Box, pub name: Box, pub kind: ImportKind, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum ImportKind { Function(TypeAddr), Table(TableType), @@ -449,6 +273,7 @@ pub enum ImportKind { } impl From<&ImportKind> for ExternalKind { + #[inline] fn from(kind: &ImportKind) -> Self { match kind { ImportKind::Function(_) => Self::Func, @@ -459,20 +284,26 @@ impl From<&ImportKind> for ExternalKind { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct Data { pub data: Box<[u8]>, pub range: Range, pub kind: DataKind, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum DataKind { Active { mem: MemAddr, offset: ConstInstruction }, Passive, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub struct Element { pub kind: ElementKind, pub items: Box<[ElementItem]>, @@ -480,14 +311,18 @@ pub struct Element { pub ty: ValType, } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum ElementKind { Passive, Active { table: TableAddr, offset: ConstInstruction }, Declared, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] pub enum ElementItem { Func(FuncAddr), Expr(ConstInstruction), diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs new file mode 100644 index 0000000..e46092b --- /dev/null +++ b/crates/types/src/value.rs @@ -0,0 +1,175 @@ +use core::fmt::Debug; + +use crate::{ConstInstruction, ExternAddr, FuncAddr}; + +/// A WebAssembly value. +/// +/// See +#[derive(Clone, Copy)] +pub enum WasmValue { + // Num types + /// A 32-bit integer. + I32(i32), + /// A 64-bit integer. + I64(i64), + /// A 32-bit float. + F32(f32), + /// A 64-bit float. + F64(f64), + + RefExtern(ExternAddr), + RefFunc(FuncAddr), + RefNull(ValType), +} + +impl WasmValue { + #[inline] + pub fn const_instr(&self) -> ConstInstruction { + match self { + Self::I32(i) => ConstInstruction::I32Const(*i), + Self::I64(i) => ConstInstruction::I64Const(*i), + Self::F32(i) => ConstInstruction::F32Const(*i), + Self::F64(i) => ConstInstruction::F64Const(*i), + + Self::RefFunc(i) => ConstInstruction::RefFunc(*i), + Self::RefNull(ty) => ConstInstruction::RefNull(*ty), + + // Self::RefExtern(addr) => ConstInstruction::RefExtern(*addr), + _ => unimplemented!("no const_instr for {:?}", self), + } + } + + /// Get the default value for a given type. + #[inline] + pub fn default_for(ty: ValType) -> Self { + match ty { + ValType::I32 => Self::I32(0), + ValType::I64 => Self::I64(0), + ValType::F32 => Self::F32(0.0), + ValType::F64 => Self::F64(0.0), + ValType::RefFunc => Self::RefNull(ValType::RefFunc), + ValType::RefExtern => Self::RefNull(ValType::RefExtern), + } + } + + #[inline] + pub fn eq_loose(&self, other: &Self) -> bool { + match (self, other) { + (Self::I32(a), Self::I32(b)) => a == b, + (Self::I64(a), Self::I64(b)) => a == b, + (Self::RefNull(v), Self::RefNull(v2)) => v == v2, + (Self::RefExtern(addr), Self::RefExtern(addr2)) => addr == addr2, + (Self::RefFunc(addr), Self::RefFunc(addr2)) => addr == addr2, + (Self::F32(a), Self::F32(b)) => { + if a.is_nan() && b.is_nan() { + true // Both are NaN, treat them as equal + } else { + a.to_bits() == b.to_bits() + } + } + (Self::F64(a), Self::F64(b)) => { + if a.is_nan() && b.is_nan() { + true // Both are NaN, treat them as equal + } else { + a.to_bits() == b.to_bits() + } + } + _ => false, + } + } +} + +#[cold] +fn cold() {} + +impl Debug for WasmValue { + fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result { + match self { + WasmValue::I32(i) => write!(f, "i32({})", i), + WasmValue::I64(i) => write!(f, "i64({})", i), + WasmValue::F32(i) => write!(f, "f32({})", i), + WasmValue::F64(i) => write!(f, "f64({})", i), + WasmValue::RefExtern(addr) => write!(f, "ref.extern({:?})", addr), + WasmValue::RefFunc(addr) => write!(f, "ref.func({:?})", addr), + WasmValue::RefNull(ty) => write!(f, "ref.null({:?})", ty), + } + } +} + +impl WasmValue { + /// Get the type of a [`WasmValue`] + #[inline] + pub fn val_type(&self) -> ValType { + match self { + Self::I32(_) => ValType::I32, + Self::I64(_) => ValType::I64, + Self::F32(_) => ValType::F32, + Self::F64(_) => ValType::F64, + Self::RefExtern(_) => ValType::RefExtern, + Self::RefFunc(_) => ValType::RefFunc, + Self::RefNull(ty) => *ty, + } + } +} + +/// Type of a WebAssembly value. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] +#[cfg_attr(feature = "archive", archive(check_bytes))] +pub enum ValType { + /// A 32-bit integer. + I32, + /// A 64-bit integer. + I64, + /// A 32-bit float. + F32, + /// A 64-bit float. + F64, + /// A reference to a function. + RefFunc, + /// A reference to an external value. + RefExtern, +} + +impl ValType { + #[inline] + pub fn default_value(&self) -> WasmValue { + WasmValue::default_for(*self) + } +} + +macro_rules! impl_conversion_for_wasmvalue { + ($($t:ty => $variant:ident),*) => { + $( + // Implementing From<$t> for WasmValue + impl From<$t> for WasmValue { + #[inline] + fn from(i: $t) -> Self { + Self::$variant(i) + } + } + + // Implementing TryFrom for $t + impl TryFrom for $t { + type Error = (); + + #[inline] + fn try_from(value: WasmValue) -> Result { + if let WasmValue::$variant(i) = value { + Ok(i) + } else { + cold(); + Err(()) + } + } + } + )* + } +} + +impl_conversion_for_wasmvalue! { + i32 => I32, + i64 => I64, + f32 => F32, + f64 => F64 +} diff --git a/examples/README.md b/examples/README.md deleted file mode 100644 index ce47073..0000000 --- a/examples/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Examples - -## WasmRust - -These are examples using WebAssembly generated from Rust code. -To run these, you first need to build the Rust code into WebAssembly, since the wasm files are not included in the repository to keep it small. -This requires the `wasm32-unknown-unknown` target and `wasm-opt` to be installed (available via Binaryen). - -```bash -$ ./examples/rust/build.sh -``` - -Then you can run the examples: - -```bash -$ cargo run --example wasm-rust -``` - -Where `` is one of the following: - -- `hello`: A simple example that prints a number to the console. -- `tinywasm`: Runs `hello` using TinyWasm - inside of TinyWasm itself! diff --git a/examples/archive.rs b/examples/archive.rs new file mode 100644 index 0000000..7c93205 --- /dev/null +++ b/examples/archive.rs @@ -0,0 +1,29 @@ +use color_eyre::eyre::Result; +use tinywasm::{parser::Parser, types::TinyWasmModule, Module, Store}; + +const WASM: &str = r#" +(module + (func $add (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.add) + (export "add" (func $add))) +"#; + +fn main() -> Result<()> { + let wasm = wat::parse_str(WASM).expect("failed to parse wat"); + let module = Parser::default().parse_module_bytes(&wasm)?; + let twasm = module.serialize_twasm(); + + // now, you could e.g. write twasm to a file called `add.twasm` + // and load it later in a different program + + let module: Module = TinyWasmModule::from_twasm(&twasm)?.into(); + let mut store = Store::default(); + let instance = module.instantiate(&mut store, None)?; + let add = instance.exported_func::<(i32, i32), i32>(&store, "add")?; + + assert_eq!(add.call(&mut store, (1, 2))?, 3); + + Ok(()) +} diff --git a/examples/linking.rs b/examples/linking.rs new file mode 100644 index 0000000..f278266 --- /dev/null +++ b/examples/linking.rs @@ -0,0 +1,41 @@ +use color_eyre::eyre::Result; +use tinywasm::{Module, Store}; + +const WASM_ADD: &str = r#" +(module + (func $add (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.add) + (export "add" (func $add))) +"#; + +const WASM_IMPORT: &str = r#" +(module + (import "adder" "add" (func $add (param i32 i32) (result i32))) + (func $main (result i32) + i32.const 1 + i32.const 2 + call $add) + (export "main" (func $main)) +) +"#; + +fn main() -> Result<()> { + let wasm_add = wat::parse_str(WASM_ADD).expect("failed to parse wat"); + let wasm_import = wat::parse_str(WASM_IMPORT).expect("failed to parse wat"); + + let add_module = Module::parse_bytes(&wasm_add)?; + let import_module = Module::parse_bytes(&wasm_import)?; + + let mut store = Store::default(); + let add_instance = add_module.instantiate(&mut store, None)?; + + let mut imports = tinywasm::Imports::new(); + imports.link_module("adder", add_instance.id())?; + let import_instance = import_module.instantiate(&mut store, Some(imports))?; + + let main = import_instance.exported_func::<(), i32>(&store, "main")?; + assert_eq!(main.call(&mut store, ())?, 3); + Ok(()) +} diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml index 9ff80dc..f3f475e 100644 --- a/examples/rust/Cargo.toml +++ b/examples/rust/Cargo.toml @@ -1,5 +1,8 @@ cargo-features=["per-package-target"] +# treat this as an independent package +[workspace] + [package] publish=false name="rust-wasm-examples" @@ -7,12 +10,32 @@ forced-target="wasm32-unknown-unknown" edition="2021" [dependencies] -tinywasm={path="../../crates/tinywasm", features=["parser", "std"]} +tinywasm={path="../../crates/tinywasm", features=["parser", "std", "unsafe"]} +argon2={version="0.5"} [[bin]] name="hello" path="src/hello.rs" +[[bin]] +name="print" +path="src/print.rs" + [[bin]] name="tinywasm" path="src/tinywasm.rs" + +[[bin]] +name="fibonacci" +path="src/fibonacci.rs" + +[[bin]] +name="argon2id" +path="src/argon2id.rs" + +[profile.wasm] +opt-level=3 +lto="thin" +codegen-units=1 +panic="abort" +inherits="release" diff --git a/examples/rust/README.md b/examples/rust/README.md index 8ecac52..1b6be2f 100644 --- a/examples/rust/README.md +++ b/examples/rust/README.md @@ -1 +1,4 @@ -# Examples using Rust compiled to WebAssembly +# WebAssembly Rust Examples + +This is a seperate crate that generates WebAssembly from Rust code. +It is used by the `wasm-rust` example. diff --git a/examples/rust/build.sh b/examples/rust/build.sh index 2c8069a..e6d3b0d 100755 --- a/examples/rust/build.sh +++ b/examples/rust/build.sh @@ -1,16 +1,21 @@ #!/usr/bin/env bash cd "$(dirname "$0")" -bins=("hello" "tinywasm") +bins=("hello" "fibonacci" "print" "tinywasm" "argon2id") exclude_wat=("tinywasm") -out_dir="../../target/wasm32-unknown-unknown/wasm" +out_dir="./target/wasm32-unknown-unknown/wasm" dest_dir="out" +features="+reference-types,+bulk-memory,+mutable-globals" + +# ensure out dir exists +mkdir -p "$dest_dir" + for bin in "${bins[@]}"; do - cargo build --target wasm32-unknown-unknown --package rust-wasm-examples --profile=wasm --bin "$bin" + RUSTFLAGS="-C target-feature=$features -C panic=abort" cargo build --target wasm32-unknown-unknown --package rust-wasm-examples --profile=wasm --bin "$bin" cp "$out_dir/$bin.wasm" "$dest_dir/" - wasm-opt "$dest_dir/$bin.wasm" -o "$dest_dir/$bin.wasm" -O --intrinsic-lowering -O + wasm-opt "$dest_dir/$bin.wasm" -o "$dest_dir/$bin.wasm" -Oz --enable-bulk-memory --enable-multivalue --enable-reference-types --enable-mutable-globals if [[ ! " ${exclude_wat[@]} " =~ " $bin " ]]; then wasm2wat "$dest_dir/$bin.wasm" -o "$dest_dir/$bin.wat" diff --git a/examples/rust/src/argon2id.rs b/examples/rust/src/argon2id.rs new file mode 100644 index 0000000..01ea7ca --- /dev/null +++ b/examples/rust/src/argon2id.rs @@ -0,0 +1,14 @@ +#![no_main] + +#[no_mangle] +pub extern "C" fn argon2id(m_cost: i32, t_cost: i32, p_cost: i32) -> i32 { + let password = b"password"; + let salt = b"some random salt"; + + let params = argon2::Params::new(m_cost as u32, t_cost as u32, p_cost as u32, None).unwrap(); + let argon = argon2::Argon2::new(argon2::Algorithm::Argon2id, argon2::Version::V0x13, params); + + let mut hash = [0u8; 32]; + argon.hash_password_into(password, salt, &mut hash).unwrap(); + hash[0] as i32 +} diff --git a/examples/rust/src/fibonacci.rs b/examples/rust/src/fibonacci.rs new file mode 100644 index 0000000..8de496d --- /dev/null +++ b/examples/rust/src/fibonacci.rs @@ -0,0 +1,23 @@ +#![no_main] +#![allow(non_snake_case)] + +#[no_mangle] +pub extern "C" fn fibonacci(n: i32) -> i32 { + let mut sum = 0; + let mut last = 0; + let mut curr = 1; + for _i in 1..n { + sum = last + curr; + last = curr; + curr = sum; + } + sum +} + +#[no_mangle] +pub extern "C" fn fibonacci_recursive(n: i32) -> i32 { + if n <= 1 { + return n; + } + fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2) +} diff --git a/examples/rust/src/hello.rs b/examples/rust/src/hello.rs index 34f3c7f..c8e2ac3 100644 --- a/examples/rust/src/hello.rs +++ b/examples/rust/src/hello.rs @@ -1,18 +1,28 @@ -#![no_std] #![no_main] -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - core::arch::wasm32::unreachable() -} - #[link(wasm_import_module = "env")] extern "C" { - fn printi32(x: i32); + fn print_utf8(location: i64, len: i32); +} + +const ARG: &[u8] = &[0u8; 100]; + +#[no_mangle] +pub unsafe extern "C" fn arg_ptr() -> i32 { + ARG.as_ptr() as i32 } #[no_mangle] -pub unsafe extern "C" fn add_and_print(lh: i32, rh: i32) { - printi32(lh + rh); +pub unsafe extern "C" fn arg_size() -> i32 { + ARG.len() as i32 +} + +#[no_mangle] +pub unsafe extern "C" fn hello(len: i32) { + let arg = core::str::from_utf8(&ARG[0..len as usize]).unwrap(); + let res = format!("Hello, {}!", arg).as_bytes().to_vec(); + + let len = res.len() as i32; + let ptr = res.leak().as_ptr() as i64; + print_utf8(ptr, len); } diff --git a/examples/rust/src/print.rs b/examples/rust/src/print.rs new file mode 100644 index 0000000..d04daa3 --- /dev/null +++ b/examples/rust/src/print.rs @@ -0,0 +1,11 @@ +#![no_main] + +#[link(wasm_import_module = "env")] +extern "C" { + fn printi32(x: i32); +} + +#[no_mangle] +pub unsafe extern "C" fn add_and_print(lh: i32, rh: i32) { + printi32(lh + rh); +} diff --git a/examples/rust/src/tinywasm.rs b/examples/rust/src/tinywasm.rs index 0f18ab9..08c8135 100644 --- a/examples/rust/src/tinywasm.rs +++ b/examples/rust/src/tinywasm.rs @@ -12,7 +12,7 @@ pub extern "C" fn hello() { } fn run() -> tinywasm::Result<()> { - let module = tinywasm::Module::parse_bytes(include_bytes!("../out/hello.wasm"))?; + let module = tinywasm::Module::parse_bytes(include_bytes!("../out/print.wasm"))?; let mut store = tinywasm::Store::default(); let mut imports = tinywasm::Imports::new(); @@ -26,7 +26,7 @@ fn run() -> tinywasm::Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.typed_func::<(i32, i32), ()>(&mut store, "add_and_print")?; + let add_and_print = instance.exported_func::<(i32, i32), ()>(&mut store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) } diff --git a/examples/rust/src/tinywasm_no_std.rs b/examples/rust/src/tinywasm_no_std.rs new file mode 100644 index 0000000..9f2b28c --- /dev/null +++ b/examples/rust/src/tinywasm_no_std.rs @@ -0,0 +1,33 @@ +#![no_main] +#![no_std] +use tinywasm::{Extern, FuncContext}; + +#[link(wasm_import_module = "env")] +extern "C" { + fn printi32(x: i32); +} + +#[no_mangle] +pub extern "C" fn hello() { + let _ = run(); +} + +fn run() -> tinywasm::Result<()> { + let module = tinywasm::Module::parse_bytes(include_bytes!("../out/print.wasm"))?; + let mut store = tinywasm::Store::default(); + let mut imports = tinywasm::Imports::new(); + + imports.define( + "env", + "printi32", + Extern::typed_func(|_: FuncContext<'_>, v: i32| { + unsafe { printi32(v) } + Ok(()) + }), + )?; + let instance = module.instantiate(&mut store, Some(imports))?; + + let add_and_print = instance.exported_func::<(i32, i32), ()>(&mut store, "add_and_print")?; + add_and_print.call(&mut store, (1, 2))?; + Ok(()) +} diff --git a/examples/simple.rs b/examples/simple.rs new file mode 100644 index 0000000..6f79c0f --- /dev/null +++ b/examples/simple.rs @@ -0,0 +1,22 @@ +use color_eyre::eyre::Result; +use tinywasm::{Module, Store}; + +const WASM: &str = r#" +(module + (func $add (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.add) + (export "add" (func $add))) +"#; + +fn main() -> Result<()> { + let wasm = wat::parse_str(WASM).expect("failed to parse wat"); + let module = Module::parse_bytes(&wasm)?; + let mut store = Store::default(); + let instance = module.instantiate(&mut store, None)?; + let add = instance.exported_func::<(i32, i32), i32>(&store, "add")?; + + assert_eq!(add.call(&mut store, (1, 2))?, 3); + Ok(()) +} diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index 3b8877e..b57a1da 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -1,18 +1,38 @@ use color_eyre::eyre::Result; -use tinywasm::{Extern, FuncContext, Imports, Module, Store}; +use tinywasm::{Extern, FuncContext, Imports, MemoryStringExt, Module, Store}; +/// Examples of using WebAssembly compiled from Rust with tinywasm. +/// +/// These examples are meant to be run with `cargo run --example wasm-rust `. +/// For example, `cargo run --example wasm-rust hello`. +/// +/// To run these, you first need to compile the Rust examples to WebAssembly: +/// +/// ```sh +/// ./examples/rust/build.sh +/// ``` +/// +/// This requires the `wasm32-unknown-unknown` target, `binaryen` and `wabt` to be installed. +/// `rustup target add wasm32-unknown-unknown`. +/// https://github.com/WebAssembly/wabt +/// https://github.com/WebAssembly/binaryen +/// fn main() -> Result<()> { let args = std::env::args().collect::>(); if args.len() < 2 { println!("Usage: cargo run --example wasm-rust "); println!("Available examples:"); println!(" hello"); - println!(" tinywasm"); + println!(" printi32"); + println!(" fibonacci - calculate fibonacci(30)"); + println!(" tinywasm - run printi32 inside of tinywasm inside of itself"); return Ok(()); } match args[1].as_str() { "hello" => hello()?, + "printi32" => printi32()?, + "fibonacci" => fibonacci()?, "tinywasm" => tinywasm()?, _ => {} } @@ -22,7 +42,7 @@ fn main() -> Result<()> { fn tinywasm() -> Result<()> { const TINYWASM: &[u8] = include_bytes!("./rust/out/tinywasm.wasm"); - let module = Module::parse_bytes(&TINYWASM)?; + let module = Module::parse_bytes(TINYWASM)?; let mut store = Store::default(); let mut imports = Imports::new(); @@ -36,7 +56,7 @@ fn tinywasm() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let hello = instance.typed_func::<(), ()>(&mut store, "hello")?; + let hello = instance.exported_func::<(), ()>(&store, "hello")?; hello.call(&mut store, ())?; Ok(()) @@ -44,7 +64,37 @@ fn tinywasm() -> Result<()> { fn hello() -> Result<()> { const HELLO_WASM: &[u8] = include_bytes!("./rust/out/hello.wasm"); - let module = Module::parse_bytes(&HELLO_WASM)?; + let module = Module::parse_bytes(HELLO_WASM)?; + let mut store = Store::default(); + + let mut imports = Imports::new(); + imports.define( + "env", + "print_utf8", + Extern::typed_func(|mut ctx: FuncContext<'_>, args: (i64, i32)| { + let mem = ctx.exported_memory("memory")?; + let ptr = args.0 as usize; + let len = args.1 as usize; + let string = mem.load_string(ptr, len)?; + println!("{}", string); + Ok(()) + }), + )?; + + let instance = module.instantiate(&mut store, Some(imports))?; + let arg_ptr = instance.exported_func::<(), i32>(&store, "arg_ptr")?.call(&mut store, ())?; + let arg = b"world"; + + instance.exported_memory_mut(&mut store, "memory")?.store(arg_ptr as usize, arg.len(), arg)?; + let hello = instance.exported_func::(&store, "hello")?; + hello.call(&mut store, arg.len() as i32)?; + + Ok(()) +} + +fn printi32() -> Result<()> { + const HELLO_WASM: &[u8] = include_bytes!("./rust/out/print.wasm"); + let module = Module::parse_bytes(HELLO_WASM)?; let mut store = Store::default(); let mut imports = Imports::new(); @@ -58,8 +108,22 @@ fn hello() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.typed_func::<(i32, i32), ()>(&mut store, "add_and_print")?; + let add_and_print = instance.exported_func::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) } + +fn fibonacci() -> Result<()> { + const FIBONACCI_WASM: &[u8] = include_bytes!("./rust/out/fibonacci.wasm"); + let module = Module::parse_bytes(FIBONACCI_WASM)?; + let mut store = Store::default(); + + let instance = module.instantiate(&mut store, None)?; + let fibonacci = instance.exported_func::(&store, "fibonacci")?; + let n = 30; + let result = fibonacci.call(&mut store, n)?; + println!("fibonacci({}) = {}", n, result); + + Ok(()) +} diff --git a/examples/wasm/add.wasm b/examples/wasm/add.wasm new file mode 100644 index 0000000..92e3432 Binary files /dev/null and b/examples/wasm/add.wasm differ