diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index af5ad77684..45b0ddbaea 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -10,6 +10,21 @@ dependencies = [ "regex", ] +[[package]] +name = "addr2line" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b6a2d3371669ab3ca9797670853d61402b03d0b4b9ebf33d677dfa720203072" +dependencies = [ + "gimli 0.22.0", +] + +[[package]] +name = "adler" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" + [[package]] name = "adler32" version = "1.0.4" @@ -310,26 +325,18 @@ checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" [[package]] name = "backtrace" -version = "0.3.46" +version = "0.3.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1e692897359247cc6bb902933361652380af0f1b7651ae5c5013407f30e109e" +checksum = "46254cf2fdcdf1badb5934448c1bcbe046a56537b3987d96c51a7afc5d03f293" dependencies = [ - "backtrace-sys", + "addr2line", "cfg-if", "libc", + "miniz_oxide 0.4.0", + "object 0.20.0", "rustc-demangle", ] -[[package]] -name = "backtrace-sys" -version = "0.1.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7de8aba10a69c8e8d7622c5710229485ec32e9d55fdad160ea559c086fdcd118" -dependencies = [ - "cc", - "libc", -] - [[package]] name = "base58" version = "0.1.0" @@ -816,25 +823,23 @@ checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" [[package]] name = "cranelift-bforest" -version = "0.63.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4425bb6c3f3d2f581c650f1a1fdd3196a975490149cf59bea9d34c3bea79eda" +version = "0.66.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.63.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d166b289fd30062ee6de86284750fc3fe5d037c6b864b3326ce153239b0626e1" +version = "0.66.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "byteorder", "cranelift-bforest", "cranelift-codegen-meta", "cranelift-codegen-shared", "cranelift-entity", - "gimli", + "gimli 0.21.0", "log", "regalloc", "serde", @@ -845,9 +850,8 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.63.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02c9fb2306a36d41c5facd4bf3400bc6c157185c43a96eaaa503471c34c5144b" +version = "0.66.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -855,24 +859,21 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.63.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44e0cfe9b1f97d9f836bca551618106c7d53b93b579029ecd38e73daa7eb689e" +version = "0.66.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" [[package]] name = "cranelift-entity" -version = "0.63.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "926a73c432e5ba9c891171ff50b75e7d992cd76cd271f0a0a0ba199138077472" +version = "0.66.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "serde", ] [[package]] name = "cranelift-frontend" -version = "0.63.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e45f82e3446dd1ebb8c2c2f6a6b0e6cd6cd52965c7e5f7b1b35e9a9ace31ccde" +version = "0.66.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "cranelift-codegen", "log", @@ -882,9 +883,8 @@ dependencies = [ [[package]] name = "cranelift-native" -version = "0.63.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "488b5d481bb0996a143e55a9d1739ef425efa20d4a5e5e98c859a8573c9ead9a" +version = "0.66.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "cranelift-codegen", "raw-cpuid", @@ -893,9 +893,8 @@ dependencies = [ [[package]] name = "cranelift-wasm" -version = "0.63.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00aa8dde71fd9fdb1958e7b0ef8f524c1560e2c6165e4ea54bc302b40551c161" +version = "0.66.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "cranelift-codegen", "cranelift-entity", @@ -903,7 +902,7 @@ dependencies = [ "log", "serde", "thiserror", - "wasmparser 0.51.4", + "wasmparser 0.59.0", ] [[package]] @@ -1408,21 +1407,6 @@ dependencies = [ "futures 0.3.5", ] -[[package]] -name = "faerie" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfef65b0e94693295c5d2fe2506f0ee6f43465342d4b5331659936aee8b16084" -dependencies = [ - "goblin", - "indexmap", - "log", - "scroll", - "string-interner", - "target-lexicon", - "thiserror", -] - [[package]] name = "failure" version = "0.1.7" @@ -1526,7 +1510,7 @@ dependencies = [ "crc32fast", "libc", "libz-sys", - "miniz_oxide", + "miniz_oxide 0.3.6", ] [[package]] @@ -2026,18 +2010,21 @@ dependencies = [ [[package]] name = "gimli" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81dd6190aad0f05ddbbf3245c54ed14ca4aa6dd32f22312b70d8f168c3e3e633" +checksum = "bcc8e0c9bce37868955864dbecd2b1ab2bdf967e6f28066d65aaac620444b65c" dependencies = [ - "arrayvec 0.5.1", - "byteorder", "fallible-iterator", "indexmap", - "smallvec 1.4.1", "stable_deref_trait", ] +[[package]] +name = "gimli" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" + [[package]] name = "glob" version = "0.2.11" @@ -2076,17 +2063,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "goblin" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3081214398d39e4bd7f2c1975f0488ed04614ffdd976c6fc7a0708278552c0da" -dependencies = [ - "log", - "plain", - "scroll", -] - [[package]] name = "h2" version = "0.1.26" @@ -2441,6 +2417,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "076f042c5b7b98f31d205f1249267e12a6518c1481e9dae9764af19b707d2292" dependencies = [ "autocfg 1.0.0", + "serde", ] [[package]] @@ -3236,9 +3213,9 @@ dependencies = [ [[package]] name = "mach" -version = "0.2.3" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86dd2487cdfea56def77b88438a2c915fb45113c5319bfe7e14306ca4cd0b0e1" +checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" dependencies = [ "libc", ] @@ -3327,6 +3304,15 @@ dependencies = [ "adler32", ] +[[package]] +name = "miniz_oxide" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be0f75932c1f6cfae3c04000e40114adf955636e19040f9c0a2c380702aa1c7f" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.6.21" @@ -4021,11 +4007,19 @@ dependencies = [ [[package]] name = "object" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5666bbb90bc4d1e5bdcb26c0afda1822d25928341e9384ab187a9b37ab69e36" +checksum = "9cbca9424c482ee628fa549d9c812e2cd22f1180b9222c9200fdfa6eb31aecb2" + +[[package]] +name = "object" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" dependencies = [ - "target-lexicon", + "crc32fast", + "indexmap", + "wasmparser 0.57.0", ] [[package]] @@ -5253,12 +5247,6 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" -[[package]] -name = "plain" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" - [[package]] name = "platforms" version = "0.2.1" @@ -5849,9 +5837,9 @@ dependencies = [ [[package]] name = "regalloc" -version = "0.0.21" +version = "0.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b27b256b41986ac5141b37b8bbba85d314fbf546c182eb255af6720e07e4f804" +checksum = "3598bed0895fe0f72a9e0b00ef9e3a3c8af978a8401b2f2046dec5927de6364a" dependencies = [ "log", "rustc-hash", @@ -5887,9 +5875,9 @@ checksum = "7fe5bd57d1d7414c6b5ed48563a2c855d995ff777729dcd91c369ec7fea395ae" [[package]] name = "region" -version = "2.1.2" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "448e868c6e4cfddfa49b6a72c95906c04e8547465e9536575b95c70a4044f856" +checksum = "877e54ea2adcd70d80e9179344c97f93ef0dffd6b03e1f4529e6e83ab2fa9ae0" dependencies = [ "bitflags", "libc", @@ -6615,8 +6603,8 @@ dependencies = [ "sp-runtime-interface", "sp-wasm-interface", "substrate-wasmtime", - "substrate-wasmtime-runtime", "wasmtime-environ", + "wasmtime-runtime", ] [[package]] @@ -8343,15 +8331,6 @@ dependencies = [ "bytes 0.4.12", ] -[[package]] -name = "string-interner" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd710eadff449a1531351b0e43eb81ea404336fa2f56c777427ab0e32a4cf183" -dependencies = [ - "serde", -] - [[package]] name = "strsim" version = "0.8.0" @@ -8660,93 +8639,28 @@ version = "1.0.6" [[package]] name = "substrate-wasmtime" -version = "0.16.0-threadsafe.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bd62264edc1a5f3ef44d86fb0c11c9fb142894b9a2da034f34afae482080d7a" +version = "0.19.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "anyhow", "backtrace", "cfg-if", "lazy_static", "libc", + "log", "region", "rustc-demangle", - "substrate-wasmtime-jit", - "substrate-wasmtime-profiling", - "substrate-wasmtime-runtime", + "smallvec 1.4.1", "target-lexicon", - "wasmparser 0.52.2", + "wasmparser 0.59.0", "wasmtime-environ", + "wasmtime-jit", + "wasmtime-profiling", + "wasmtime-runtime", "wat", "winapi 0.3.8", ] -[[package]] -name = "substrate-wasmtime-jit" -version = "0.16.0-threadsafe.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ce43c159d4f3ef6b19641e1ae045847fd202d8e2cc74df7ccb2b6475e069d4a" -dependencies = [ - "anyhow", - "cfg-if", - "cranelift-codegen", - "cranelift-entity", - "cranelift-frontend", - "cranelift-native", - "cranelift-wasm", - "gimli", - "log", - "more-asserts", - "region", - "substrate-wasmtime-profiling", - "substrate-wasmtime-runtime", - "target-lexicon", - "thiserror", - "wasmparser 0.52.2", - "wasmtime-debug", - "wasmtime-environ", - "winapi 0.3.8", -] - -[[package]] -name = "substrate-wasmtime-profiling" -version = "0.16.0-threadsafe.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c77f0ce539b5a09a54dc80a1cf0c7cd7e694df11029354fe50a2d5fe889bdb97" -dependencies = [ - "anyhow", - "cfg-if", - "gimli", - "lazy_static", - "libc", - "object", - "scroll", - "serde", - "substrate-wasmtime-runtime", - "target-lexicon", - "wasmtime-environ", -] - -[[package]] -name = "substrate-wasmtime-runtime" -version = "0.16.0-threadsafe.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46516af0a64a7d9b652c5aa7436b6ce13edfa54435a66ef177fc02d2283e2dc2" -dependencies = [ - "backtrace", - "cc", - "cfg-if", - "indexmap", - "lazy_static", - "libc", - "memoffset", - "more-asserts", - "region", - "thiserror", - "wasmtime-environ", - "winapi 0.3.8", -] - [[package]] name = "subtle" version = "1.0.0" @@ -9785,43 +9699,43 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.51.4" +version = "0.57.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aeb1956b19469d1c5e63e459d29e7b5aa0f558d9f16fcef09736f8a265e6c10a" +checksum = "32fddd575d477c6e9702484139cf9f23dcd554b06d185ed0f56c857dd3a47aa6" [[package]] name = "wasmparser" -version = "0.52.2" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "733954023c0b39602439e60a65126fd31b003196d3a1e8e4531b055165a79b31" +checksum = "a950e6a618f62147fd514ff445b2a0b53120d382751960797f85f058c7eda9b9" [[package]] name = "wasmtime-debug" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d39ba645aee700b29ff0093028b4123556dd142a74973f04ed6225eedb40e77d" +version = "0.19.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "anyhow", - "faerie", - "gimli", + "gimli 0.21.0", "more-asserts", + "object 0.20.0", "target-lexicon", "thiserror", - "wasmparser 0.51.4", + "wasmparser 0.59.0", "wasmtime-environ", ] [[package]] name = "wasmtime-environ" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed54fd9d64dfeeee7c285fd126174a6b5e6d4efc7e5a1566fdb635e60ff6a74e" +version = "0.19.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" dependencies = [ "anyhow", "base64 0.12.3", "bincode", + "cfg-if", "cranelift-codegen", "cranelift-entity", + "cranelift-frontend", "cranelift-wasm", "directories", "errno", @@ -9835,25 +9749,104 @@ dependencies = [ "sha2 0.8.1", "thiserror", "toml", - "wasmparser 0.51.4", + "wasmparser 0.59.0", "winapi 0.3.8", "zstd", ] +[[package]] +name = "wasmtime-jit" +version = "0.19.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" +dependencies = [ + "anyhow", + "cfg-if", + "cranelift-codegen", + "cranelift-entity", + "cranelift-frontend", + "cranelift-native", + "cranelift-wasm", + "gimli 0.21.0", + "log", + "more-asserts", + "object 0.20.0", + "region", + "target-lexicon", + "thiserror", + "wasmparser 0.59.0", + "wasmtime-debug", + "wasmtime-environ", + "wasmtime-obj", + "wasmtime-profiling", + "wasmtime-runtime", + "winapi 0.3.8", +] + +[[package]] +name = "wasmtime-obj" +version = "0.19.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" +dependencies = [ + "anyhow", + "more-asserts", + "object 0.20.0", + "target-lexicon", + "wasmtime-debug", + "wasmtime-environ", +] + +[[package]] +name = "wasmtime-profiling" +version = "0.19.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" +dependencies = [ + "anyhow", + "cfg-if", + "gimli 0.21.0", + "lazy_static", + "libc", + "object 0.19.0", + "scroll", + "serde", + "target-lexicon", + "wasmtime-environ", + "wasmtime-runtime", +] + +[[package]] +name = "wasmtime-runtime" +version = "0.19.0" +source = "git+https://github.com/paritytech/wasmtime?branch=update-upstream#f744c4e564b40a4cfce6a7090f093ec1726c68e9" +dependencies = [ + "backtrace", + "cc", + "cfg-if", + "indexmap", + "lazy_static", + "libc", + "log", + "memoffset", + "more-asserts", + "region", + "thiserror", + "wasmtime-environ", + "winapi 0.3.8", +] + [[package]] name = "wast" -version = "13.0.0" +version = "21.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b20abd8b4a26f7e0d4dd5e357e90a3d555ec190e94472c9b2b27c5b9777f9ae" +checksum = "0b1844f66a2bc8526d71690104c0e78a8e59ffa1597b7245769d174ebb91deb5" dependencies = [ "leb128", ] [[package]] name = "wat" -version = "1.0.14" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51a615830ee3e7200b505c441fec09aac2f114deae69df52f215cb828ba112c4" +checksum = "ce85d72b74242c340e9e3492cfb602652d7bb324c3172dd441b5577e39a2e18c" dependencies = [ "wast", ] diff --git a/substrate/client/executor/wasmtime/Cargo.toml b/substrate/client/executor/wasmtime/Cargo.toml index d29b0e34f0..e4ee979407 100644 --- a/substrate/client/executor/wasmtime/Cargo.toml +++ b/substrate/client/executor/wasmtime/Cargo.toml @@ -21,11 +21,12 @@ sp-wasm-interface = { version = "2.0.0-rc5", path = "../../../primitives/wasm-in sp-runtime-interface = { version = "2.0.0-rc5", path = "../../../primitives/runtime-interface" } sp-core = { version = "2.0.0-rc5", path = "../../../primitives/core" } sp-allocator = { version = "2.0.0-rc5", path = "../../../primitives/allocator" } -wasmtime = { package = "substrate-wasmtime", version = "0.16.0-threadsafe.4" } -wasmtime-runtime = { package = "substrate-wasmtime-runtime", version = "0.16.0-threadsafe.4" } -wasmtime-environ = "0.16" -cranelift-wasm = "0.63" -cranelift-codegen = "0.63" +wasmtime = { package = "substrate-wasmtime", git = "https://github.com/paritytech/wasmtime", branch = "update-upstream" } +wasmtime-runtime = { git = "https://github.com/paritytech/wasmtime", branch = "update-upstream" } +wasmtime-environ = { git = "https://github.com/paritytech/wasmtime", branch = "update-upstream" } +cranelift-wasm = { git = "https://github.com/paritytech/wasmtime", branch = "update-upstream" } +cranelift-codegen = { git = "https://github.com/paritytech/wasmtime", branch = "update-upstream" } + [dev-dependencies] assert_matches = "1.3.0" diff --git a/substrate/client/executor/wasmtime/src/host.rs b/substrate/client/executor/wasmtime/src/host.rs index 8c481e95c4..eeb7cb9271 100644 --- a/substrate/client/executor/wasmtime/src/host.rs +++ b/substrate/client/executor/wasmtime/src/host.rs @@ -308,6 +308,7 @@ impl<'a> Sandbox for HostContext<'a> { .ok_or_else(|| "dispatch_thunk_id is out of bounds")? .funcref() .ok_or_else(|| "dispatch_thunk_idx should be a funcref")? + .ok_or_else(|| "dispatch_thunk_idx should point to actual func")? .clone(); SupervisorFuncRef(func_ref) }; diff --git a/substrate/client/executor/wasmtime/src/imports.rs b/substrate/client/executor/wasmtime/src/imports.rs index 36752d72fa..41498e2b0f 100644 --- a/substrate/client/executor/wasmtime/src/imports.rs +++ b/substrate/client/executor/wasmtime/src/imports.rs @@ -22,7 +22,7 @@ use sp_wasm_interface::{Function, Value, ValueType}; use std::any::Any; use wasmtime::{ Extern, ExternType, Func, FuncType, ImportType, Limits, Memory, MemoryType, Module, - Trap, Val, + Trap, Val, Store, }; pub struct Imports { @@ -35,6 +35,7 @@ pub struct Imports { /// Goes over all imports of a module and prepares a vector of `Extern`s that can be used for /// instantiation of the module. Returns an error if there are imports that cannot be satisfied. pub fn resolve_imports( + store: &Store, module: &Module, host_functions: &[&'static dyn Function], heap_pages: u32, @@ -54,10 +55,10 @@ pub fn resolve_imports( let resolved = match import_ty.name() { "memory" => { memory_import_index = Some(externs.len()); - resolve_memory_import(module, &import_ty, heap_pages)? + resolve_memory_import(store, &import_ty, heap_pages)? } _ => resolve_func_import( - module, + store, &import_ty, host_functions, allow_missing_func_imports, @@ -72,7 +73,7 @@ pub fn resolve_imports( } fn resolve_memory_import( - module: &Module, + store: &Store, import_ty: &ImportType, heap_pages: u32, ) -> Result { @@ -105,12 +106,12 @@ fn resolve_memory_import( } let memory_ty = MemoryType::new(Limits::new(initial, requested_memory_ty.limits().max())); - let memory = Memory::new(module.store(), memory_ty); + let memory = Memory::new(store, memory_ty); Ok(Extern::Memory(memory)) } fn resolve_func_import( - module: &Module, + store: &Store, import_ty: &ImportType, host_functions: &[&'static dyn Function], allow_missing_func_imports: bool, @@ -132,7 +133,7 @@ fn resolve_func_import( { Some(host_func) => host_func, None if allow_missing_func_imports => { - return Ok(MissingHostFuncHandler::new(import_ty).into_extern(module, &func_ty)); + return Ok(MissingHostFuncHandler::new(import_ty).into_extern(store, &func_ty)); } None => { return Err(WasmError::Other(format!( @@ -150,7 +151,7 @@ fn resolve_func_import( ))); } - Ok(HostFuncHandler::new(*host_func).into_extern(module)) + Ok(HostFuncHandler::new(*host_func).into_extern(store)) } /// Returns `true` if `lhs` and `rhs` represent the same signature. @@ -223,10 +224,10 @@ impl HostFuncHandler { } } - fn into_extern(self, module: &Module) -> Extern { + fn into_extern(self, store: &Store) -> Extern { let host_func = self.host_func; let func_ty = wasmtime_func_sig(self.host_func); - let func = Func::new(module.store(), func_ty, + let func = Func::new(store, func_ty, move |_, params, result| { call_static(host_func, params, result) } @@ -249,9 +250,9 @@ impl MissingHostFuncHandler { } } - fn into_extern(self, wasmtime_module: &Module, func_ty: &FuncType) -> Extern { + fn into_extern(self, store: &Store, func_ty: &FuncType) -> Extern { let Self { module, name } = self; - let func = Func::new(wasmtime_module.store(), func_ty.clone(), + let func = Func::new(store, func_ty.clone(), move |_, _, _| Err(Trap::new(format!( "call to a missing function {}:{}", module, name diff --git a/substrate/client/executor/wasmtime/src/instance_wrapper.rs b/substrate/client/executor/wasmtime/src/instance_wrapper.rs index 9026b8054e..d31193688b 100644 --- a/substrate/client/executor/wasmtime/src/instance_wrapper.rs +++ b/substrate/client/executor/wasmtime/src/instance_wrapper.rs @@ -28,7 +28,7 @@ use sc_executor_common::{ util::{WasmModuleInfo, DataSegmentsSnapshot}, }; use sp_wasm_interface::{Pointer, WordSize, Value}; -use wasmtime::{Store, Instance, Module, Memory, Table, Val, Func, Extern, Global}; +use wasmtime::{Engine, Instance, Module, Memory, Table, Val, Func, Extern, Global, Store}; mod globals_snapshot; @@ -42,8 +42,8 @@ pub struct ModuleWrapper { } impl ModuleWrapper { - pub fn new(store: &Store, code: &[u8]) -> Result { - let module = Module::new(&store, code) + pub fn new(engine: &Engine, code: &[u8]) -> Result { + let module = Module::new(engine, code) .map_err(|e| Error::from(format!("cannot create module: {}", e)))?; let module_info = WasmModuleInfo::new(code) @@ -121,8 +121,8 @@ fn extern_func(extern_: &Extern) -> Option<&Func> { impl InstanceWrapper { /// Create a new instance wrapper from the given wasm module. - pub fn new(module_wrapper: &ModuleWrapper, imports: &Imports, heap_pages: u32) -> Result { - let instance = Instance::new(&module_wrapper.module, &imports.externs) + pub fn new(store: &Store, module_wrapper: &ModuleWrapper, imports: &Imports, heap_pages: u32) -> Result { + let instance = Instance::new(store, &module_wrapper.module, &imports.externs) .map_err(|e| Error::from(format!("cannot instantiate: {}", e)))?; let memory = match imports.memory_import_index { diff --git a/substrate/client/executor/wasmtime/src/instance_wrapper/globals_snapshot.rs b/substrate/client/executor/wasmtime/src/instance_wrapper/globals_snapshot.rs index 01d82451fc..dd99d63ae2 100644 --- a/substrate/client/executor/wasmtime/src/instance_wrapper/globals_snapshot.rs +++ b/substrate/client/executor/wasmtime/src/instance_wrapper/globals_snapshot.rs @@ -40,7 +40,7 @@ impl GlobalsSnapshot { pub fn take(instance_wrapper: &InstanceWrapper) -> Result { // EVIL: // Usage of an undocumented function. - let handle = unsafe { instance_wrapper.instance.handle().clone() }; + let handle = instance_wrapper.instance.handle().clone().handle; let mut preserved_mut_globals = vec![]; @@ -75,7 +75,7 @@ impl GlobalsSnapshot { /// /// This instance must be the same that was used for creation of this snapshot. pub fn apply(&self, instance_wrapper: &InstanceWrapper) -> Result<()> { - if instance_wrapper.instance.handle() != &self.handle { + if instance_wrapper.instance.handle().handle != self.handle { return Err(Error::from("unexpected instance handle".to_string())); } diff --git a/substrate/client/executor/wasmtime/src/runtime.rs b/substrate/client/executor/wasmtime/src/runtime.rs index a2ad3bada4..365770b3fa 100644 --- a/substrate/client/executor/wasmtime/src/runtime.rs +++ b/substrate/client/executor/wasmtime/src/runtime.rs @@ -39,13 +39,17 @@ pub struct WasmtimeRuntime { heap_pages: u32, allow_missing_func_imports: bool, host_functions: Vec<&'static dyn Function>, + engine: Engine, } impl WasmModule for WasmtimeRuntime { fn new_instance(&self) -> Result> { + let store = Store::new(&self.engine); + // Scan all imports, find the matching host functions, and create stubs that adapt arguments // and results. let imports = resolve_imports( + &store, self.module_wrapper.module(), &self.host_functions, self.heap_pages, @@ -53,11 +57,12 @@ impl WasmModule for WasmtimeRuntime { )?; let instance_wrapper = - InstanceWrapper::new(&self.module_wrapper, &imports, self.heap_pages)?; + InstanceWrapper::new(&store, &self.module_wrapper, &imports, self.heap_pages)?; let heap_base = instance_wrapper.extract_heap_base()?; let globals_snapshot = GlobalsSnapshot::take(&instance_wrapper)?; Ok(Box::new(WasmtimeInstance { + store, instance_wrapper: Rc::new(instance_wrapper), module_wrapper: Arc::clone(&self.module_wrapper), imports, @@ -71,6 +76,7 @@ impl WasmModule for WasmtimeRuntime { /// A `WasmInstance` implementation that reuses compiled module and spawns instances /// to execute the compiled code. pub struct WasmtimeInstance { + store: Store, module_wrapper: Arc, instance_wrapper: Rc, globals_snapshot: GlobalsSnapshot, @@ -106,7 +112,7 @@ impl WasmInstance for WasmtimeInstance { } fn get_global_const(&self, name: &str) -> Result> { - let instance = InstanceWrapper::new(&self.module_wrapper, &self.imports, self.heap_pages)?; + let instance = InstanceWrapper::new(&self.store, &self.module_wrapper, &self.imports, self.heap_pages)?; instance.get_global_val(name) } } @@ -124,9 +130,8 @@ pub fn create_runtime( config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); let engine = Engine::new(&config); - let store = Store::new(&engine); - let module_wrapper = ModuleWrapper::new(&store, code) + let module_wrapper = ModuleWrapper::new(&engine, code) .map_err(|e| WasmError::Other(format!("cannot create module: {}", e)))?; Ok(WasmtimeRuntime { @@ -134,6 +139,7 @@ pub fn create_runtime( heap_pages: heap_pages as u32, allow_missing_func_imports, host_functions, + engine, }) }