From cc4cc2af5efdb31e4cf2359b1bd70c5446626820 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 1 Feb 2023 12:55:03 -0700 Subject: [PATCH 1/2] use main module's `cabi_realloc` instead of `memory.grow` This depends on https://github.com/bytecodealliance/wasm-tools/pull/900, which will polyfill an implementation if the main module does not export it. Signed-off-by: Joel Dice --- src/lib.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 973ff6f7..2878b517 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,15 @@ #![allow(unused_variables)] // TODO: remove this when more things are implemented use crate::bindings::{ - wasi_clocks, wasi_default_clocks, wasi_exit, wasi_filesystem, wasi_logging, wasi_poll, - wasi_random, wasi_stderr, wasi_tcp, + wasi_clocks, wasi_default_clocks, wasi_exit, wasi_filesystem, wasi_poll, wasi_random, + wasi_stderr, wasi_tcp, }; use core::arch::wasm32; use core::cell::{Cell, RefCell, UnsafeCell}; use core::cmp::min; use core::ffi::c_void; use core::hint::black_box; -use core::mem::{align_of, forget, replace, size_of, ManuallyDrop, MaybeUninit}; +use core::mem::{self, align_of, forget, replace, size_of, ManuallyDrop, MaybeUninit}; use core::ptr::{self, null_mut}; use core::slice; use wasi::*; @@ -149,7 +149,7 @@ fn align_to(ptr: usize, align: usize) -> usize { /// traps when it runs out of data. This means that the total size of /// arguments/env/etc coming into a component is bounded by the current 64k /// (ish) limit. That's just an implementation limit though which can be lifted -/// by dynamically calling `memory.grow` as necessary for more data. +/// by dynamically calling the main module's allocator as necessary for more data. #[no_mangle] pub unsafe extern "C" fn cabi_export_realloc( old_ptr: *mut u8, @@ -2265,11 +2265,25 @@ impl State { #[cold] fn new() -> &'static RefCell { - let grew = wasm32::memory_grow(0, 1); - if grew == usize::MAX { - unreachable!(); + #[link(wasm_import_module = "__main_module__")] + extern "C" { + fn cabi_realloc( + old_ptr: *mut u8, + old_len: usize, + align: usize, + new_len: usize, + ) -> *mut u8; } - let ret = (grew * PAGE_SIZE) as *mut RefCell; + + let ret = unsafe { + cabi_realloc( + ptr::null_mut(), + 0, + mem::align_of::>(), + mem::size_of::>(), + ) as *mut RefCell + }; + let ret = unsafe { ret.write(RefCell::new(State { magic1: MAGIC, From b44310f90c1dd3549df3f5434ef3617f24a2f2d2 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 1 Feb 2023 14:10:33 -0700 Subject: [PATCH 2/2] update `wit-component` to pull in https://github.com/bytecodealliance/wasm-tools/pull/900 Signed-off-by: Joel Dice --- Cargo.lock | 70 +++++++++++++++++++++++++++------ test-programs/macros/Cargo.toml | 3 +- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a36f97a..68bcf52c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1194,7 +1194,7 @@ name = "test-programs-macros" version = "0.0.0" dependencies = [ "quote", - "wit-component", + "wit-component 0.4.4", ] [[package]] @@ -1495,6 +1495,14 @@ dependencies = [ "leb128", ] +[[package]] +name = "wasm-encoder" +version = "0.22.0" +source = "git+https://github.com/bytecodealliance/wasm-tools?rev=5ba052b9862466258f7e4d153823520ae8dabd88#5ba052b9862466258f7e4d153823520ae8dabd88" +dependencies = [ + "leb128", +] + [[package]] name = "wasmparser" version = "0.92.0" @@ -1524,6 +1532,15 @@ dependencies = [ "url", ] +[[package]] +name = "wasmparser" +version = "0.99.0" +source = "git+https://github.com/bytecodealliance/wasm-tools?rev=5ba052b9862466258f7e4d153823520ae8dabd88#5ba052b9862466258f7e4d153823520ae8dabd88" +dependencies = [ + "indexmap", + "url", +] + [[package]] name = "wasmprinter" version = "0.2.49" @@ -1531,7 +1548,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27c13dff901f9354fa9a6a877152d9c5642513645985635c9b83bcca99e40ea1" dependencies = [ "anyhow", - "wasmparser 0.99.0", + "wasmparser 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1605,7 +1622,7 @@ dependencies = [ "syn", "wasmtime-component-util", "wasmtime-wit-bindgen", - "wit-parser", + "wit-parser 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1753,7 +1770,7 @@ source = "git+https://github.com/bytecodealliance/wasmtime#b58a197d33f044193c3d6 dependencies = [ "anyhow", "heck", - "wit-parser", + "wit-parser 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1765,7 +1782,7 @@ dependencies = [ "leb128", "memchr", "unicode-width", - "wasm-encoder 0.22.0", + "wasm-encoder 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1882,8 +1899,8 @@ version = "0.3.0" source = "git+https://github.com/bytecodealliance/wit-bindgen#98c2b1e4cb10d3757114b7524d112511a4c7459e" dependencies = [ "anyhow", - "wit-component", - "wit-parser", + "wit-component 0.4.3", + "wit-parser 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1894,7 +1911,7 @@ dependencies = [ "heck", "wit-bindgen-core", "wit-bindgen-gen-rust-lib", - "wit-component", + "wit-component 0.4.3", ] [[package]] @@ -1925,7 +1942,7 @@ dependencies = [ "syn", "wit-bindgen-core", "wit-bindgen-gen-guest-rust", - "wit-component", + "wit-component 0.4.3", ] [[package]] @@ -1939,9 +1956,24 @@ dependencies = [ "indexmap", "log", "url", - "wasm-encoder 0.22.0", - "wasmparser 0.99.0", - "wit-parser", + "wasm-encoder 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "wasmparser 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)", + "wit-parser 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wit-component" +version = "0.4.4" +source = "git+https://github.com/bytecodealliance/wasm-tools?rev=5ba052b9862466258f7e4d153823520ae8dabd88#5ba052b9862466258f7e4d153823520ae8dabd88" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "url", + "wasm-encoder 0.22.0 (git+https://github.com/bytecodealliance/wasm-tools?rev=5ba052b9862466258f7e4d153823520ae8dabd88)", + "wasmparser 0.99.0 (git+https://github.com/bytecodealliance/wasm-tools?rev=5ba052b9862466258f7e4d153823520ae8dabd88)", + "wit-parser 0.4.1 (git+https://github.com/bytecodealliance/wasm-tools?rev=5ba052b9862466258f7e4d153823520ae8dabd88)", ] [[package]] @@ -1959,6 +1991,20 @@ dependencies = [ "url", ] +[[package]] +name = "wit-parser" +version = "0.4.1" +source = "git+https://github.com/bytecodealliance/wasm-tools?rev=5ba052b9862466258f7e4d153823520ae8dabd88#5ba052b9862466258f7e4d153823520ae8dabd88" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "pulldown-cmark", + "unicode-xid", + "url", +] + [[package]] name = "zstd" version = "0.11.2+zstd.1.5.2" diff --git a/test-programs/macros/Cargo.toml b/test-programs/macros/Cargo.toml index 5578689b..17cf18e4 100644 --- a/test-programs/macros/Cargo.toml +++ b/test-programs/macros/Cargo.toml @@ -13,4 +13,5 @@ test = false quote = "1.0" [build-dependencies] -wit-component = "0.4.2" +# TODO: switch to release once https://github.com/bytecodealliance/wasm-tools/pull/900 has been released: +wit-component = { git = "https://github.com/bytecodealliance/wasm-tools", rev = "5ba052b9862466258f7e4d153823520ae8dabd88" }