diff --git a/Cargo.lock b/Cargo.lock index 166146005c8d3..da130640e3c81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,20 +14,11 @@ dependencies = [ [[package]] name = "addr2line" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7" -dependencies = [ - "gimli 0.23.0", -] - -[[package]] -name = "addr2line" -version = "0.15.1" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03345e98af8f3d786b6d9f656ccfa6ac316d954e92bc4841f0bba20789d5fb5a" +checksum = "3e61f2b7f93d2c7d2b08263acaa4a363b3e276806c68af6134c44f523bf1aacd" dependencies = [ - "gimli 0.24.0", + "gimli 0.25.0", ] [[package]] @@ -415,15 +406,16 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "backtrace" -version = "0.3.56" +version = "0.3.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d117600f438b1707d4e4ae15d3595657288f8235a0eb593e80ecc98ab34e1bc" +checksum = "e7a905d892734eea339e896738c14b9afce22b5318f64b951e70bf3844419b01" dependencies = [ - "addr2line 0.14.1", + "addr2line", + "cc", "cfg-if 1.0.0", "libc", "miniz_oxide", - "object 0.23.0", + "object 0.26.0", "rustc-demangle", ] @@ -1067,11 +1059,11 @@ dependencies = [ [[package]] name = "cranelift-bforest" -version = "0.74.0" +version = "0.76.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8ca3560686e7c9c7ed7e0fe77469f2410ba5d7781b1acaa9adc8d8deea28e3e" +checksum = "7e6bea67967505247f54fa2c85cf4f6e0e31c4e5692c9b70e4ae58e339067333" dependencies = [ - "cranelift-entity 0.74.0", + "cranelift-entity 0.76.0", ] [[package]] @@ -1095,15 +1087,15 @@ dependencies = [ [[package]] name = "cranelift-codegen" -version = "0.74.0" +version = "0.76.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf9bf1ffffb6ce3d2e5ebc83549bd2436426c99b31cc550d521364cbe35d276" +checksum = "48194035d2752bdd5bdae429e3ab88676e95f52a2b1355a5d4e809f9e39b1d74" dependencies = [ - "cranelift-bforest 0.74.0", - "cranelift-codegen-meta 0.74.0", - "cranelift-codegen-shared 0.74.0", - "cranelift-entity 0.74.0", - "gimli 0.24.0", + "cranelift-bforest 0.76.0", + "cranelift-codegen-meta 0.76.0", + "cranelift-codegen-shared 0.76.0", + "cranelift-entity 0.76.0", + "gimli 0.25.0", "log 0.4.14", "regalloc", "serde", @@ -1123,12 +1115,12 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.74.0" +version = "0.76.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cc21936a5a6d07e23849ffe83e5c1f6f50305c074f4b2970ca50c13bf55b821" +checksum = "976efb22fcab4f2cd6bd4e9913764616a54d895c1a23530128d04e03633c555f" dependencies = [ - "cranelift-codegen-shared 0.74.0", - "cranelift-entity 0.74.0", + "cranelift-codegen-shared 0.76.0", + "cranelift-entity 0.76.0", ] [[package]] @@ -1139,9 +1131,9 @@ checksum = "6759012d6d19c4caec95793f052613e9d4113e925e7f14154defbac0f1d4c938" [[package]] name = "cranelift-codegen-shared" -version = "0.74.0" +version = "0.76.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca5b6ffaa87560bebe69a5446449da18090b126037920b0c1c6d5945f72faf6b" +checksum = "9dabb5fe66e04d4652e434195b45ae65b5c8172d520247b8f66d8df42b2b45dc" dependencies = [ "serde", ] @@ -1157,9 +1149,9 @@ dependencies = [ [[package]] name = "cranelift-entity" -version = "0.74.0" +version = "0.76.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d6b4a8bef04f82e4296782646f733c641d09497df2fabf791323fefaa44c64c" +checksum = "3329733e4d4b8e91c809efcaa4faee80bf66f20164e3dd16d707346bd3494799" dependencies = [ "serde", ] @@ -1178,11 +1170,11 @@ dependencies = [ [[package]] name = "cranelift-frontend" -version = "0.74.0" +version = "0.76.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c31b783b351f966fce33e3c03498cb116d16d97a8f9978164a60920bd0d3a99c" +checksum = "279afcc0d3e651b773f94837c3d581177b348c8d69e928104b2e9fccb226f921" dependencies = [ - "cranelift-codegen 0.74.0", + "cranelift-codegen 0.76.0", "log 0.4.14", "smallvec 1.6.1", "target-lexicon 0.12.0", @@ -1190,29 +1182,30 @@ dependencies = [ [[package]] name = "cranelift-native" -version = "0.74.0" +version = "0.76.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a77c88d3dd48021ff1e37e978a00098524abd3513444ae252c08d37b310b3d2a" +checksum = "4c04d1fe6a5abb5bb0edc78baa8ef238370fb8e389cc88b6d153f7c3e9680425" dependencies = [ - "cranelift-codegen 0.74.0", + "cranelift-codegen 0.76.0", + "libc", "target-lexicon 0.12.0", ] [[package]] name = "cranelift-wasm" -version = "0.74.0" +version = "0.76.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edb6d408e2da77cdbbd65466298d44c86ae71c1785d2ab0d8657753cdb4d9d89" +checksum = "e0d260ad44f6fd2c91f7f5097191a2a9e3edcbb36df1fb787b600dad5ea148ec" dependencies = [ - "cranelift-codegen 0.74.0", - "cranelift-entity 0.74.0", - "cranelift-frontend 0.74.0", + "cranelift-codegen 0.76.0", + "cranelift-entity 0.76.0", + "cranelift-frontend 0.76.0", "itertools 0.10.0", "log 0.4.14", "serde", "smallvec 1.6.1", "thiserror", - "wasmparser 0.78.2", + "wasmparser 0.79.0", ] [[package]] @@ -2469,15 +2462,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" - -[[package]] -name = "gimli" -version = "0.24.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189" +checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" dependencies = [ "fallible-iterator", "indexmap", @@ -4111,9 +4098,9 @@ checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" [[package]] name = "memchr" -version = "2.3.4" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" [[package]] name = "memmap" @@ -4882,9 +4869,9 @@ checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" [[package]] name = "nom" -version = "6.2.1" +version = "6.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c5c51b9083a3c620fa67a2a635d1ce7d95b897e957d6b28ff9a5da960a103a6" +checksum = "e7413f999671bd4745a7b624bd370a569fb6bc574b23c83a3c5ed2e453f3d5e2" dependencies = [ "bitvec 0.19.5", "funty", @@ -4996,18 +4983,13 @@ dependencies = [ [[package]] name = "object" -version = "0.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4" - -[[package]] -name = "object" -version = "0.24.0" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a5b3dd1c072ee7963717671d1ca129f1048fda25edea6b752bfc71ac8854170" +checksum = "c55827317fb4c08822499848a14237d2874d6f139828893017237e7ab93eb386" dependencies = [ "crc32fast", "indexmap", + "memchr", ] [[package]] @@ -8016,7 +7998,6 @@ dependencies = [ "sc-allocator", "sc-executor-common", "sc-runtime-test", - "scoped-tls", "sp-core", "sp-io", "sp-runtime-interface", @@ -11463,15 +11444,15 @@ checksum = "87cc2fe6350834b4e528ba0901e7aa405d78b89dc1fa3145359eb4de0e323fcf" [[package]] name = "wasmparser" -version = "0.78.2" +version = "0.79.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52144d4c78e5cf8b055ceab8e5fa22814ce4315d6002ad32cfd914f37c12fd65" +checksum = "5b5894be15a559c85779254700e1d35f02f843b5a69152e5c82c626d9fd66c0e" [[package]] name = "wasmtime" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b310b9d20fcf59385761d1ade7a3ef06aecc380e3d3172035b919eaf7465d9f7" +checksum = "8bbb8a082a8ef50f7eeb8b82dda9709ef1e68963ea3c94e45581644dd4041835" dependencies = [ "anyhow", "backtrace", @@ -11489,7 +11470,7 @@ dependencies = [ "serde", "smallvec 1.6.1", "target-lexicon 0.12.0", - "wasmparser 0.78.2", + "wasmparser 0.79.0", "wasmtime-cache", "wasmtime-environ", "wasmtime-jit", @@ -11500,9 +11481,9 @@ dependencies = [ [[package]] name = "wasmtime-cache" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d14d500d5c3dc5f5c097158feee123d64b3097f0d836a2a27dff9c761c73c843" +checksum = "d73391579ca7f24573138ef768b73b2aed5f9d542385c64979b65d60d0912399" dependencies = [ "anyhow", "base64 0.13.0", @@ -11521,78 +11502,78 @@ dependencies = [ [[package]] name = "wasmtime-cranelift" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c525b39f062eada7db3c1298287b96dcb6e472b9f6b22501300b28d9fa7582f6" +checksum = "81c6f5ae9205382345c7cd7454932a906186836999a2161c385e38a15f52e1fe" dependencies = [ - "cranelift-codegen 0.74.0", - "cranelift-entity 0.74.0", - "cranelift-frontend 0.74.0", + "cranelift-codegen 0.76.0", + "cranelift-entity 0.76.0", + "cranelift-frontend 0.76.0", "cranelift-wasm", "target-lexicon 0.12.0", - "wasmparser 0.78.2", + "wasmparser 0.79.0", "wasmtime-environ", ] [[package]] name = "wasmtime-debug" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5d2a763e7a6fc734218e0e463196762a4f409c483063d81e0e85f96343b2e0a" +checksum = "c69e08f55e12f15f50b1b533bc3626723e7224254a065de6576934c86258c9e8" dependencies = [ "anyhow", - "gimli 0.24.0", + "gimli 0.25.0", "more-asserts", - "object 0.24.0", + "object 0.26.0", "target-lexicon 0.12.0", "thiserror", - "wasmparser 0.78.2", + "wasmparser 0.79.0", "wasmtime-environ", ] [[package]] name = "wasmtime-environ" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f64d0c2d881c31b0d65c1f2695e022d71eb60b9fbdd336aacca28208b58eac90" +checksum = "005d93174040af37fb8625f891cd9827afdad314261f7ec4ee61ec497d6e9d3c" dependencies = [ "cfg-if 1.0.0", - "cranelift-codegen 0.74.0", - "cranelift-entity 0.74.0", + "cranelift-codegen 0.76.0", + "cranelift-entity 0.76.0", "cranelift-wasm", - "gimli 0.24.0", + "gimli 0.25.0", "indexmap", "log 0.4.14", "more-asserts", "serde", "thiserror", - "wasmparser 0.78.2", + "wasmparser 0.79.0", ] [[package]] name = "wasmtime-jit" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d4539ea734422b7c868107e2187d7746d8affbcaa71916d72639f53757ad707" +checksum = "d0bf1dfb213a35d8f21aefae40e597fe72778a907011ffdff7affb029a02af9a" dependencies = [ - "addr2line 0.15.1", + "addr2line", "anyhow", "cfg-if 1.0.0", - "cranelift-codegen 0.74.0", - "cranelift-entity 0.74.0", - "cranelift-frontend 0.74.0", + "cranelift-codegen 0.76.0", + "cranelift-entity 0.76.0", + "cranelift-frontend 0.76.0", "cranelift-native", "cranelift-wasm", - "gimli 0.24.0", + "gimli 0.25.0", "log 0.4.14", "more-asserts", - "object 0.24.0", + "object 0.26.0", "rayon", "region", "serde", "target-lexicon 0.12.0", "thiserror", - "wasmparser 0.78.2", + "wasmparser 0.79.0", "wasmtime-cranelift", "wasmtime-debug", "wasmtime-environ", @@ -11604,13 +11585,13 @@ dependencies = [ [[package]] name = "wasmtime-obj" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e1a8ff85246d091828e2225af521a6208ed28c997bb5c39eb697366dc2e2f2b" +checksum = "d231491878e710c68015228c9f9fc5955fe5c96dbf1485c15f7bed55b622c83c" dependencies = [ "anyhow", "more-asserts", - "object 0.24.0", + "object 0.26.0", "target-lexicon 0.12.0", "wasmtime-debug", "wasmtime-environ", @@ -11618,9 +11599,9 @@ dependencies = [ [[package]] name = "wasmtime-profiling" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e24364d522dcd67c897c8fffc42e5bdfc57207bbb6d7eeade0da9d4a7d70105b" +checksum = "21486cfb5255c2069666c1f116f9e949d4e35c9a494f11112fa407879e42198d" dependencies = [ "anyhow", "cfg-if 1.0.0", @@ -11634,9 +11615,9 @@ dependencies = [ [[package]] name = "wasmtime-runtime" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c51e57976e8a19a18a18e002c6eb12e5769554204238e47ff155fda1809ef0f7" +checksum = "d7ddfdf32e0a20d81f48be9dacd31612bc61de5a174d1356fef806d300f507de" dependencies = [ "anyhow", "backtrace", @@ -11885,18 +11866,18 @@ dependencies = [ [[package]] name = "zstd" -version = "0.6.1+zstd.1.4.9" +version = "0.9.0+zstd.1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5de55e77f798f205d8561b8fe2ef57abfb6e0ff2abe7fd3c089e119cdb5631a3" +checksum = "07749a5dc2cb6b36661290245e350f15ec3bbb304e493db54a1d354480522ccd" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "3.0.1+zstd.1.4.9" +version = "4.1.1+zstd.1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1387cabcd938127b30ce78c4bf00b30387dddf704e3f0881dbc4ff62b5566f8c" +checksum = "c91c90f2c593b003603e5e0493c837088df4469da25aafff8bce42ba48caf079" dependencies = [ "libc", "zstd-sys", @@ -11904,9 +11885,9 @@ dependencies = [ [[package]] name = "zstd-sys" -version = "1.4.20+zstd.1.4.9" +version = "1.6.1+zstd.1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebd5b733d7cf2d9447e2c3e76a5589b4f5e5ae065c22a2bc0b023cbc331b6c8e" +checksum = "615120c7a2431d16cf1cf979e7fc31ba7a5b5e5707b29c8a99e5dbf8a8392a33" dependencies = [ "cc", "libc", diff --git a/client/executor/common/src/runtime_blob/globals_snapshot.rs b/client/executor/common/src/runtime_blob/globals_snapshot.rs index 6a29ff8bae365..a25fa6f9fd639 100644 --- a/client/executor/common/src/runtime_blob/globals_snapshot.rs +++ b/client/executor/common/src/runtime_blob/globals_snapshot.rs @@ -34,14 +34,14 @@ pub trait InstanceGlobals { /// Get a handle to a global by it's export name. /// /// The requested export is must exist in the exported list, and it should be a mutable global. - fn get_global(&self, export_name: &str) -> Self::Global; + fn get_global(&mut self, export_name: &str) -> Self::Global; /// Get the current value of the global. - fn get_global_value(&self, global: &Self::Global) -> sp_wasm_interface::Value; + fn get_global_value(&mut self, global: &Self::Global) -> sp_wasm_interface::Value; /// Update the current value of the global. /// /// The global behind the handle is guaranteed to be mutable and the value to be the same type /// as the global. - fn set_global_value(&self, global: &Self::Global, value: sp_wasm_interface::Value); + fn set_global_value(&mut self, global: &Self::Global, value: sp_wasm_interface::Value); } /// A set of exposed mutable globals. @@ -79,7 +79,10 @@ impl GlobalsSnapshot { /// /// This function panics if the instance doesn't correspond to the module from which the /// [`ExposedMutableGlobalsSet`] was collected. - pub fn take(mutable_globals: &ExposedMutableGlobalsSet, instance: &Instance) -> Self + pub fn take( + mutable_globals: &ExposedMutableGlobalsSet, + instance: &mut Instance, + ) -> Self where Instance: InstanceGlobals, { @@ -98,7 +101,7 @@ impl GlobalsSnapshot { /// Apply the snapshot to the given instance. /// /// This instance must be the same that was used for creation of this snapshot. - pub fn apply(&self, instance: &Instance) + pub fn apply(&self, instance: &mut Instance) where Instance: InstanceGlobals, { diff --git a/client/executor/common/src/util.rs b/client/executor/common/src/util.rs index 3ea29540f98ee..ffbeb8c7ab533 100644 --- a/client/executor/common/src/util.rs +++ b/client/executor/common/src/util.rs @@ -233,7 +233,7 @@ pub mod wasmer { let range = checked_range(dest_addr.into(), source.len(), destination.len()) .ok_or_else(|| Error::Other("memory write is out of bounds".into()))?; - &mut destination[range].copy_from_slice(source); + destination[range].copy_from_slice(source); Ok(()) } } diff --git a/client/executor/common/src/wasm_runtime.rs b/client/executor/common/src/wasm_runtime.rs index eb73909d9234f..1e9f1225518a3 100644 --- a/client/executor/common/src/wasm_runtime.rs +++ b/client/executor/common/src/wasm_runtime.rs @@ -78,21 +78,21 @@ pub trait WasmInstance: Send { /// Before execution, instance is reset. /// /// Returns the encoded result on success. - fn call(&self, method: InvokeMethod, data: &[u8]) -> Result, Error>; + fn call(&mut self, method: InvokeMethod, data: &[u8]) -> Result, Error>; /// Call an exported method on this WASM instance. /// /// Before execution, instance is reset. /// /// Returns the encoded result on success. - fn call_export(&self, method: &str, data: &[u8]) -> Result, Error> { + fn call_export(&mut self, method: &str, data: &[u8]) -> Result, Error> { self.call(method.into(), data) } /// Get the value from a global with the given `name`. /// /// This method is only suitable for getting immutable globals. - fn get_global_const(&self, name: &str) -> Result, Error>; + fn get_global_const(&mut self, name: &str) -> Result, Error>; /// **Testing Only**. This function returns the base address of the linear memory. /// diff --git a/client/executor/src/integration_tests/linux.rs b/client/executor/src/integration_tests/linux.rs index 7e0696973dc77..38e57707e9e6b 100644 --- a/client/executor/src/integration_tests/linux.rs +++ b/client/executor/src/integration_tests/linux.rs @@ -40,7 +40,7 @@ fn memory_consumption_compiled() { let runtime = mk_test_runtime(WasmExecutionMethod::Compiled, 1024); - let instance = runtime.new_instance().unwrap(); + let mut instance = runtime.new_instance().unwrap(); let heap_base = instance .get_global_const("__heap_base") .expect("`__heap_base` is valid") diff --git a/client/executor/src/integration_tests/mod.rs b/client/executor/src/integration_tests/mod.rs index dabead4799dc8..fe964f47ba374 100644 --- a/client/executor/src/integration_tests/mod.rs +++ b/client/executor/src/integration_tests/mod.rs @@ -467,7 +467,7 @@ test_wasm_execution!(returns_mutable_static); fn returns_mutable_static(wasm_method: WasmExecutionMethod) { let runtime = mk_test_runtime(wasm_method, 1024); - let instance = runtime.new_instance().unwrap(); + let mut instance = runtime.new_instance().unwrap(); let res = instance.call_export("returns_mutable_static", &[0]).unwrap(); assert_eq!(33, u64::decode(&mut &res[..]).unwrap()); @@ -482,7 +482,7 @@ test_wasm_execution!(returns_mutable_static_bss); fn returns_mutable_static_bss(wasm_method: WasmExecutionMethod) { let runtime = mk_test_runtime(wasm_method, 1024); - let instance = runtime.new_instance().unwrap(); + let mut instance = runtime.new_instance().unwrap(); let res = instance.call_export("returns_mutable_static_bss", &[0]).unwrap(); assert_eq!(1, u64::decode(&mut &res[..]).unwrap()); @@ -508,7 +508,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) { const REQUIRED_MEMORY_PAGES: u64 = 32; let runtime = mk_test_runtime(wasm_method, REQUIRED_MEMORY_PAGES); - let instance = runtime.new_instance().unwrap(); + let mut instance = runtime.new_instance().unwrap(); // On the first invocation we allocate approx. 768KB (75%) of stack and then trap. let res = instance.call_export("allocates_huge_stack_array", &true.encode()); @@ -522,7 +522,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) { test_wasm_execution!(interpreted_only heap_is_reset_between_calls); fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) { let runtime = mk_test_runtime(wasm_method, 1024); - let instance = runtime.new_instance().unwrap(); + let mut instance = runtime.new_instance().unwrap(); let heap_base = instance .get_global_const("__heap_base") diff --git a/client/executor/src/native_executor.rs b/client/executor/src/native_executor.rs index 77b1ec7abf4f2..f24c52925ac5f 100644 --- a/client/executor/src/native_executor.rs +++ b/client/executor/src/native_executor.rs @@ -163,7 +163,7 @@ impl WasmExecutor { where F: FnOnce( AssertUnwindSafe<&Arc>, - AssertUnwindSafe<&dyn WasmInstance>, + AssertUnwindSafe<&mut dyn WasmInstance>, Option<&RuntimeVersion>, AssertUnwindSafe<&mut dyn Externalities>, ) -> Result>, @@ -217,7 +217,7 @@ impl WasmExecutor { .new_instance() .map_err(|e| format!("Failed to create instance: {:?}", e))?; - let instance = AssertUnwindSafe(instance); + let mut instance = AssertUnwindSafe(instance); let mut ext = AssertUnwindSafe(ext); let module = AssertUnwindSafe(module); @@ -283,7 +283,7 @@ impl CodeExecutor for WasmExecutor { runtime_code, ext, false, - |module, instance, _onchain_version, mut ext| { + |module, mut instance, _onchain_version, mut ext| { with_externalities_safe(&mut **ext, move || { preregister_builtin_ext(module.clone()); instance.call_export(method, data).map(NativeOrEncoded::Encoded) @@ -438,7 +438,7 @@ impl RuntimeSpawn for RuntimeInstanceSpawn { // pool of instances should be used. // // https://github.com/paritytech/substrate/issues/7354 - let instance = + let mut instance = module.new_instance().expect("Failed to create new instance from module"); instance @@ -525,7 +525,7 @@ impl CodeExecutor for NativeElseWasmExecut runtime_code, ext, false, - |module, instance, onchain_version, mut ext| { + |module, mut instance, onchain_version, mut ext| { let onchain_version = onchain_version.ok_or_else(|| Error::ApiError("Unknown version".into()))?; diff --git a/client/executor/src/wasm_runtime.rs b/client/executor/src/wasm_runtime.rs index b11e3958dbc81..b3a981d9e0821 100644 --- a/client/executor/src/wasm_runtime.rs +++ b/client/executor/src/wasm_runtime.rs @@ -76,7 +76,7 @@ impl VersionedRuntime { where F: FnOnce( &Arc, - &dyn WasmInstance, + &mut dyn WasmInstance, Option<&RuntimeVersion>, &mut dyn Externalities, ) -> Result, @@ -90,12 +90,12 @@ impl VersionedRuntime { match instance { Some((index, mut locked)) => { - let (instance, new_inst) = locked + let (mut instance, new_inst) = locked .take() .map(|r| Ok((r, false))) .unwrap_or_else(|| self.module.new_instance().map(|i| (i, true)))?; - let result = f(&self.module, &*instance, self.version.as_ref(), ext); + let result = f(&self.module, &mut *instance, self.version.as_ref(), ext); if let Err(e) = &result { if new_inst { log::warn!( @@ -129,9 +129,9 @@ impl VersionedRuntime { log::warn!(target: "wasm-runtime", "Ran out of free WASM instances"); // Allocate a new instance - let instance = self.module.new_instance()?; + let mut instance = self.module.new_instance()?; - f(&self.module, &*instance, self.version.as_ref(), ext) + f(&self.module, &mut *instance, self.version.as_ref(), ext) }, } } @@ -213,7 +213,7 @@ impl RuntimeCache { where F: FnOnce( &Arc, - &dyn WasmInstance, + &mut dyn WasmInstance, Option<&RuntimeVersion>, &mut dyn Externalities, ) -> Result, diff --git a/client/executor/wasmi/src/lib.rs b/client/executor/wasmi/src/lib.rs index 6052662fa7ccf..ceab07c2f71cb 100644 --- a/client/executor/wasmi/src/lib.rs +++ b/client/executor/wasmi/src/lib.rs @@ -736,7 +736,7 @@ pub struct WasmiInstance { unsafe impl Send for WasmiInstance {} impl WasmInstance for WasmiInstance { - fn call(&self, method: InvokeMethod, data: &[u8]) -> Result, Error> { + fn call(&mut self, method: InvokeMethod, data: &[u8]) -> Result, Error> { // We reuse a single wasm instance for multiple calls and a previous call (if any) // altered the state. Therefore, we need to restore the instance to original state. @@ -767,7 +767,7 @@ impl WasmInstance for WasmiInstance { ) } - fn get_global_const(&self, name: &str) -> Result, Error> { + fn get_global_const(&mut self, name: &str) -> Result, Error> { match self.instance.export_by_name(name) { Some(global) => Ok(Some( global diff --git a/client/executor/wasmtime/Cargo.toml b/client/executor/wasmtime/Cargo.toml index 3158cdecc3263..edf1747524263 100644 --- a/client/executor/wasmtime/Cargo.toml +++ b/client/executor/wasmtime/Cargo.toml @@ -16,7 +16,6 @@ targets = ["x86_64-unknown-linux-gnu"] libc = "0.2.90" cfg-if = "1.0" log = "0.4.8" -scoped-tls = "1.0" parity-wasm = "0.42.0" codec = { package = "parity-scale-codec", version = "2.0.0" } sc-executor-common = { version = "0.10.0-dev", path = "../common" } @@ -24,10 +23,7 @@ sp-wasm-interface = { version = "4.0.0-dev", path = "../../../primitives/wasm-in sp-runtime-interface = { version = "4.0.0-dev", path = "../../../primitives/runtime-interface" } sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" } sc-allocator = { version = "4.0.0-dev", path = "../../allocator" } -wasmtime = { version = "0.27.0", default-features = false, features = [ - "cache", - "parallel-compilation", -] } +wasmtime = { version = "0.29.0", default-features = false, features = ["cache", "parallel-compilation"] } [dev-dependencies] sc-runtime-test = { version = "2.0.0", path = "../runtime-test" } diff --git a/client/executor/wasmtime/src/host.rs b/client/executor/wasmtime/src/host.rs index 8453ec3954354..4edb9f9c423f0 100644 --- a/client/executor/wasmtime/src/host.rs +++ b/client/executor/wasmtime/src/host.rs @@ -19,7 +19,7 @@ //! This module defines `HostState` and `HostContext` structs which provide logic and state //! required for execution of host. -use crate::instance_wrapper::InstanceWrapper; +use crate::{instance_wrapper::InstanceWrapper, runtime::StoreData}; use codec::{Decode, Encode}; use log::trace; use sc_allocator::FreeingBumpHeapAllocator; @@ -31,7 +31,7 @@ use sc_executor_common::{ use sp_core::sandbox as sandbox_primitives; use sp_wasm_interface::{FunctionContext, MemoryId, Pointer, Sandbox, WordSize}; use std::{cell::RefCell, rc::Rc}; -use wasmtime::{Func, Val}; +use wasmtime::{Caller, Func, Val}; /// The state required to construct a HostContext context. The context only lasts for one host /// call, whereas the state is maintained for the duration of a Wasm runtime call, which may make @@ -64,45 +64,67 @@ impl HostState { } /// Materialize `HostContext` that can be used to invoke a substrate host `dyn Function`. - pub fn materialize<'a>(&'a self) -> HostContext<'a> { - HostContext(self) + pub(crate) fn materialize<'a, 'b, 'c>( + &'a self, + caller: &'b mut Caller<'c, StoreData>, + ) -> HostContext<'a, 'b, 'c> { + HostContext { host_state: self, caller } } } /// A `HostContext` implements `FunctionContext` for making host calls from a Wasmtime /// runtime. The `HostContext` exists only for the lifetime of the call and borrows state from /// a longer-living `HostState`. -pub struct HostContext<'a>(&'a HostState); +pub(crate) struct HostContext<'a, 'b, 'c> { + host_state: &'a HostState, + caller: &'b mut Caller<'c, StoreData>, +} -impl<'a> std::ops::Deref for HostContext<'a> { +impl<'a, 'b, 'c> std::ops::Deref for HostContext<'a, 'b, 'c> { type Target = HostState; fn deref(&self) -> &HostState { - self.0 + self.host_state } } -impl<'a> sp_wasm_interface::FunctionContext for HostContext<'a> { +impl<'a, 'b, 'c> sp_wasm_interface::FunctionContext for HostContext<'a, 'b, 'c> { fn read_memory_into( &self, address: Pointer, dest: &mut [u8], ) -> sp_wasm_interface::Result<()> { - self.instance.read_memory_into(address, dest).map_err(|e| e.to_string()) + let ctx = &self.caller; + self.host_state + .instance + .read_memory_into(ctx, address, dest) + .map_err(|e| e.to_string()) } fn write_memory(&mut self, address: Pointer, data: &[u8]) -> sp_wasm_interface::Result<()> { - self.instance.write_memory_from(address, data).map_err(|e| e.to_string()) + let ctx = &mut self.caller; + self.host_state + .instance + .write_memory_from(ctx, address, data) + .map_err(|e| e.to_string()) } fn allocate_memory(&mut self, size: WordSize) -> sp_wasm_interface::Result> { - self.instance - .allocate(&mut *self.allocator.borrow_mut(), size) + let ctx = &mut self.caller; + let allocator = &self.host_state.allocator; + + self.host_state + .instance + .allocate(ctx, &mut *allocator.borrow_mut(), size) .map_err(|e| e.to_string()) } fn deallocate_memory(&mut self, ptr: Pointer) -> sp_wasm_interface::Result<()> { - self.instance - .deallocate(&mut *self.allocator.borrow_mut(), ptr) + let ctx = &mut self.caller; + let allocator = &self.host_state.allocator; + + self.host_state + .instance + .deallocate(ctx, &mut *allocator.borrow_mut(), ptr) .map_err(|e| e.to_string()) } @@ -111,7 +133,7 @@ impl<'a> sp_wasm_interface::FunctionContext for HostContext<'a> { } } -impl<'a> Sandbox for HostContext<'a> { +impl<'a, 'b, 'c> Sandbox for HostContext<'a, 'b, 'c> { fn memory_get( &mut self, memory_id: MemoryId, @@ -129,7 +151,8 @@ impl<'a> Sandbox for HostContext<'a> { Ok(buffer) => buffer, }; - if let Err(_) = self.instance.write_memory_from(buf_ptr, &buffer) { + let instance = self.instance.clone(); + if let Err(_) = instance.write_memory_from(&mut self.caller, buf_ptr, &buffer) { return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS) } @@ -148,7 +171,7 @@ impl<'a> Sandbox for HostContext<'a> { let len = val_len as usize; - let buffer = match self.instance.read_memory(val_ptr, len) { + let buffer = match self.instance.read_memory(&self.caller, val_ptr, len) { Err(_) => return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), Ok(buffer) => buffer, }; @@ -241,12 +264,14 @@ impl<'a> Sandbox for HostContext<'a> { ) -> sp_wasm_interface::Result { // Extract a dispatch thunk from the instance's table by the specified index. let dispatch_thunk = { + let ctx = &mut self.caller; let table_item = self + .host_state .instance .table() .as_ref() .ok_or_else(|| "Runtime doesn't have a table; sandbox is unavailable")? - .get(dispatch_thunk_id); + .get(ctx, dispatch_thunk_id); table_item .ok_or_else(|| "dispatch_thunk_id is out of bounds")? @@ -295,12 +320,12 @@ impl<'a> Sandbox for HostContext<'a> { } } -struct SandboxContext<'a, 'b> { - host_context: &'a mut HostContext<'b>, +struct SandboxContext<'a, 'b, 'c, 'd> { + host_context: &'a mut HostContext<'b, 'c, 'd>, dispatch_thunk: Func, } -impl<'a, 'b> sandbox::SandboxContext for SandboxContext<'a, 'b> { +impl<'a, 'b, 'c, 'd> sandbox::SandboxContext for SandboxContext<'a, 'b, 'c, 'd> { fn invoke( &mut self, invoke_args_ptr: Pointer, @@ -308,12 +333,16 @@ impl<'a, 'b> sandbox::SandboxContext for SandboxContext<'a, 'b> { state: u32, func_idx: SupervisorFuncIndex, ) -> Result { - let result = self.dispatch_thunk.call(&[ - Val::I32(u32::from(invoke_args_ptr) as i32), - Val::I32(invoke_args_len as i32), - Val::I32(state as i32), - Val::I32(usize::from(func_idx) as i32), - ]); + let result = self.dispatch_thunk.call( + &mut self.host_context.caller, + &[ + Val::I32(u32::from(invoke_args_ptr) as i32), + Val::I32(invoke_args_len as i32), + Val::I32(state as i32), + Val::I32(usize::from(func_idx) as i32), + ], + ); + match result { Ok(ret_vals) => { let ret_val = if ret_vals.len() != 1 { diff --git a/client/executor/wasmtime/src/imports.rs b/client/executor/wasmtime/src/imports.rs index b27fb944bc030..a9ef6e1f58a70 100644 --- a/client/executor/wasmtime/src/imports.rs +++ b/client/executor/wasmtime/src/imports.rs @@ -16,12 +16,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{state_holder, util}; +use crate::{ + runtime::{Store, StoreData}, + util, +}; use sc_executor_common::error::WasmError; use sp_wasm_interface::{Function, ValueType}; use std::any::Any; use wasmtime::{ - Extern, ExternType, Func, FuncType, ImportType, Limits, Memory, MemoryType, Module, Store, + Caller, Extern, ExternType, Func, FuncType, ImportType, Limits, Memory, MemoryType, Module, Trap, Val, }; @@ -34,8 +37,8 @@ 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, +pub(crate) fn resolve_imports( + store: &mut Store, module: &Module, host_functions: &[&'static dyn Function], heap_pages: u32, @@ -78,7 +81,7 @@ fn import_name<'a, 'b: 'a>(import: &'a ImportType<'b>) -> Result<&'a str, WasmEr } fn resolve_memory_import( - store: &Store, + store: &mut Store, import_ty: &ImportType, heap_pages: u32, ) -> Result { @@ -117,7 +120,7 @@ fn resolve_memory_import( } fn resolve_func_import( - store: &Store, + store: &mut Store, import_ty: &ImportType, host_functions: &[&'static dyn Function], allow_missing_func_imports: bool, @@ -162,19 +165,27 @@ struct HostFuncHandler { host_func: &'static dyn Function, } -fn call_static( +fn call_static<'a>( static_func: &'static dyn Function, wasmtime_params: &[Val], wasmtime_results: &mut [Val], + mut caller: Caller<'a, StoreData>, ) -> Result<(), wasmtime::Trap> { - let unwind_result = state_holder::with_context(|host_ctx| { - let mut host_ctx = host_ctx.expect( - "host functions can be called only from wasm instance; - wasm instance is always called initializing context; - therefore host_ctx cannot be None; - qed - ", - ); + let unwind_result = { + let host_state = caller + .data() + .host_state() + .expect( + "host functions can be called only from wasm instance; + wasm instance is always called initializing context; + therefore host_ctx cannot be None; + qed + ", + ) + .clone(); + + let mut host_ctx = host_state.materialize(&mut caller); + // `from_wasmtime_val` panics if it encounters a value that doesn't fit into the values // available in substrate. // @@ -185,7 +196,7 @@ fn call_static( std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { static_func.execute(&mut host_ctx, &mut params) })) - }); + }; let execution_result = match unwind_result { Ok(execution_result) => execution_result, @@ -219,11 +230,11 @@ impl HostFuncHandler { Self { host_func } } - fn into_extern(self, store: &Store) -> Extern { + fn into_extern(self, store: &mut Store) -> Extern { let host_func = self.host_func; let func_ty = wasmtime_func_sig(self.host_func); - let func = Func::new(store, func_ty, move |_, params, result| { - call_static(host_func, params, result) + let func = Func::new(store, func_ty, move |caller, params, result| { + call_static(host_func, params, result, caller) }); Extern::Func(func) } @@ -243,7 +254,7 @@ impl MissingHostFuncHandler { }) } - fn into_extern(self, store: &Store, func_ty: &FuncType) -> Extern { + fn into_extern(self, store: &mut Store, func_ty: &FuncType) -> Extern { let Self { module, name } = self; let func = Func::new(store, func_ty.clone(), move |_, _, _| { Err(Trap::new(format!("call to a missing function {}:{}", module, name))) diff --git a/client/executor/wasmtime/src/instance_wrapper.rs b/client/executor/wasmtime/src/instance_wrapper.rs index f66d62f673d90..ccfbb912b9a69 100644 --- a/client/executor/wasmtime/src/instance_wrapper.rs +++ b/client/executor/wasmtime/src/instance_wrapper.rs @@ -19,20 +19,18 @@ //! Defines data and logic needed for interaction with an WebAssembly instance of a substrate //! runtime module. -use crate::{ - imports::Imports, - util::{from_wasmtime_val, into_wasmtime_val}, -}; +use crate::imports::Imports; use sc_executor_common::{ error::{Error, Result}, - runtime_blob, util::checked_range, wasm_runtime::InvokeMethod, }; use sp_wasm_interface::{Pointer, Value, WordSize}; -use std::{marker, slice}; -use wasmtime::{Extern, Func, Global, Instance, Memory, Module, Store, Table, Val}; +use std::marker; +use wasmtime::{ + AsContext, AsContextMut, Extern, Func, Global, Instance, Memory, Module, Table, Val, +}; /// Invoked entrypoint format. pub enum EntryPointType { @@ -58,7 +56,12 @@ pub struct EntryPoint { impl EntryPoint { /// Call this entry point. - pub fn call(&self, data_ptr: Pointer, data_len: WordSize) -> Result { + pub fn call( + &self, + ctx: impl AsContextMut, + data_ptr: Pointer, + data_len: WordSize, + ) -> Result { let data_ptr = u32::from(data_ptr); let data_len = u32::from(data_len); @@ -68,15 +71,18 @@ impl EntryPoint { match self.call_type { EntryPointType::Direct { ref entrypoint } => - entrypoint.call((data_ptr, data_len)).map_err(handle_trap), + entrypoint.call(ctx, (data_ptr, data_len)).map_err(handle_trap), EntryPointType::Wrapped { func, ref dispatcher } => - dispatcher.call((func, data_ptr, data_len)).map_err(handle_trap), + dispatcher.call(ctx, (func, data_ptr, data_len)).map_err(handle_trap), } } - pub fn direct(func: wasmtime::Func) -> std::result::Result { + pub fn direct( + func: wasmtime::Func, + ctx: impl AsContext, + ) -> std::result::Result { let entrypoint = func - .typed::<(u32, u32), u64>() + .typed::<(u32, u32), u64, _>(ctx) .map_err(|_| "Invalid signature for direct entry point")? .clone(); Ok(Self { call_type: EntryPointType::Direct { entrypoint } }) @@ -85,9 +91,10 @@ impl EntryPoint { pub fn wrapped( dispatcher: wasmtime::Func, func: u32, + ctx: impl AsContext, ) -> std::result::Result { let dispatcher = dispatcher - .typed::<(u32, u32, u32), u64>() + .typed::<(u32, u32, u32), u64, _>(ctx) .map_err(|_| "Invalid signature for wrapped entry point")? .clone(); Ok(Self { call_type: EntryPointType::Wrapped { func, dispatcher } }) @@ -144,8 +151,13 @@ fn extern_func(extern_: &Extern) -> Option<&Func> { impl InstanceWrapper { /// Create a new instance wrapper from the given wasm module. - pub fn new(store: &Store, module: &Module, imports: &Imports, heap_pages: u32) -> Result { - let instance = Instance::new(store, module, &imports.externs) + pub fn new( + module: &Module, + imports: &Imports, + heap_pages: u32, + mut ctx: impl AsContextMut, + ) -> Result { + let instance = Instance::new(&mut ctx, module, &imports.externs) .map_err(|e| Error::from(format!("cannot instantiate: {}", e)))?; let memory = match imports.memory_import_index { @@ -153,51 +165,55 @@ impl InstanceWrapper { .expect("only memory can be at the `memory_idx`; qed") .clone(), None => { - let memory = get_linear_memory(&instance)?; - if !memory.grow(heap_pages).is_ok() { - return Err("failed to increase the linear memory size".into()) + let memory = get_linear_memory(&instance, &mut ctx)?; + if !memory.grow(&mut ctx, heap_pages).is_ok() { + return Err("failed top increase the linear memory size".into()) } memory }, }; - Ok(Self { - table: get_table(&instance), - instance, - memory, - _not_send_nor_sync: marker::PhantomData, - }) + let table = get_table(&instance, ctx); + + Ok(Self { table, instance, memory, _not_send_nor_sync: marker::PhantomData }) } /// Resolves a substrate entrypoint by the given name. /// /// An entrypoint must have a signature `(i32, i32) -> i64`, otherwise this function will return /// an error. - pub fn resolve_entrypoint(&self, method: InvokeMethod) -> Result { + pub fn resolve_entrypoint( + &self, + method: InvokeMethod, + mut ctx: impl AsContextMut, + ) -> Result { Ok(match method { InvokeMethod::Export(method) => { // Resolve the requested method and verify that it has a proper signature. - let export = self.instance.get_export(method).ok_or_else(|| { + let export = self.instance.get_export(&mut ctx, method).ok_or_else(|| { Error::from(format!("Exported method {} is not found", method)) })?; let func = extern_func(&export) .ok_or_else(|| Error::from(format!("Export {} is not a function", method)))? .clone(); - EntryPoint::direct(func).map_err(|_| { + EntryPoint::direct(func, ctx).map_err(|_| { Error::from(format!("Exported function '{}' has invalid signature.", method)) })? }, InvokeMethod::Table(func_ref) => { - let table = - self.instance.get_table("__indirect_function_table").ok_or(Error::NoTable)?; - let val = table.get(func_ref).ok_or(Error::NoTableEntryWithIndex(func_ref))?; + let table = self + .instance + .get_table(&mut ctx, "__indirect_function_table") + .ok_or(Error::NoTable)?; + let val = + table.get(&mut ctx, func_ref).ok_or(Error::NoTableEntryWithIndex(func_ref))?; let func = val .funcref() .ok_or(Error::TableElementIsNotAFunction(func_ref))? .ok_or(Error::FunctionRefIsNull(func_ref))? .clone(); - EntryPoint::direct(func).map_err(|_| { + EntryPoint::direct(func, ctx).map_err(|_| { Error::from(format!( "Function @{} in exported table has invalid signature for direct call.", func_ref, @@ -205,10 +221,12 @@ impl InstanceWrapper { })? }, InvokeMethod::TableWithWrapper { dispatcher_ref, func } => { - let table = - self.instance.get_table("__indirect_function_table").ok_or(Error::NoTable)?; + let table = self + .instance + .get_table(&mut ctx, "__indirect_function_table") + .ok_or(Error::NoTable)?; let val = table - .get(dispatcher_ref) + .get(&mut ctx, dispatcher_ref) .ok_or(Error::NoTableEntryWithIndex(dispatcher_ref))?; let dispatcher = val .funcref() @@ -216,7 +234,7 @@ impl InstanceWrapper { .ok_or(Error::FunctionRefIsNull(dispatcher_ref))? .clone(); - EntryPoint::wrapped(dispatcher, func).map_err(|_| { + EntryPoint::wrapped(dispatcher, func, ctx).map_err(|_| { Error::from(format!( "Function @{} in exported table has invalid signature for wrapped call.", dispatcher_ref, @@ -234,17 +252,17 @@ impl InstanceWrapper { /// Reads `__heap_base: i32` global variable and returns it. /// /// If it doesn't exist, not a global or of not i32 type returns an error. - pub fn extract_heap_base(&self) -> Result { + pub fn extract_heap_base(&self, mut ctx: impl AsContextMut) -> Result { let heap_base_export = self .instance - .get_export("__heap_base") + .get_export(&mut ctx, "__heap_base") .ok_or_else(|| Error::from("__heap_base is not found"))?; let heap_base_global = extern_global(&heap_base_export) .ok_or_else(|| Error::from("__heap_base is not a global"))?; let heap_base = heap_base_global - .get() + .get(&mut ctx) .i32() .ok_or_else(|| Error::from("__heap_base is not a i32"))?; @@ -252,15 +270,15 @@ impl InstanceWrapper { } /// Get the value from a global with the given `name`. - pub fn get_global_val(&self, name: &str) -> Result> { - let global = match self.instance.get_export(name) { + pub fn get_global_val(&self, mut ctx: impl AsContextMut, name: &str) -> Result> { + let global = match self.instance.get_export(&mut ctx, name) { Some(global) => global, None => return Ok(None), }; let global = extern_global(&global).ok_or_else(|| format!("`{}` is not a global", name))?; - match global.get() { + match global.get(ctx) { Val::I32(val) => Ok(Some(Value::I32(val))), Val::I64(val) => Ok(Some(Value::I64(val))), Val::F32(val) => Ok(Some(Value::F32(val))), @@ -268,12 +286,17 @@ impl InstanceWrapper { _ => Err("Unknown value type".into()), } } + + /// Get a global with the given `name`. + pub fn get_global(&self, ctx: impl AsContextMut, name: &str) -> Option { + self.instance.get_global(ctx, name) + } } /// Extract linear memory instance from the given instance. -fn get_linear_memory(instance: &Instance) -> Result { +fn get_linear_memory(instance: &Instance, ctx: impl AsContextMut) -> Result { let memory_export = instance - .get_export("memory") + .get_export(ctx, "memory") .ok_or_else(|| Error::from("memory is not exported under `memory` name"))?; let memory = extern_memory(&memory_export) @@ -284,9 +307,9 @@ fn get_linear_memory(instance: &Instance) -> Result { } /// Extract the table from the given instance if any. -fn get_table(instance: &Instance) -> Option { +fn get_table(instance: &Instance, ctx: impl AsContextMut) -> Option
{ instance - .get_export("__indirect_function_table") + .get_export(ctx, "__indirect_function_table") .as_ref() .and_then(extern_table) .cloned() @@ -297,12 +320,17 @@ impl InstanceWrapper { /// Read data from a slice of memory into a newly allocated buffer. /// /// Returns an error if the read would go out of the memory bounds. - pub fn read_memory(&self, source_addr: Pointer, size: usize) -> Result> { - let range = checked_range(source_addr.into(), size, self.memory.data_size()) + pub fn read_memory( + &self, + ctx: impl AsContext, + source_addr: Pointer, + size: usize, + ) -> Result> { + let range = checked_range(source_addr.into(), size, self.memory.data_size(&ctx)) .ok_or_else(|| Error::Other("memory read is out of bounds".into()))?; let mut buffer = vec![0; range.len()]; - self.read_memory_into(source_addr, &mut buffer)?; + self.read_memory_into(ctx, source_addr, &mut buffer)?; Ok(buffer) } @@ -310,33 +338,35 @@ impl InstanceWrapper { /// Read data from the instance memory into a slice. /// /// Returns an error if the read would go out of the memory bounds. - pub fn read_memory_into(&self, source_addr: Pointer, dest: &mut [u8]) -> Result<()> { - unsafe { - // This should be safe since we don't grow up memory while caching this reference and - // we give up the reference before returning from this function. - let memory = self.memory_as_slice(); - - let range = checked_range(source_addr.into(), dest.len(), memory.len()) - .ok_or_else(|| Error::Other("memory read is out of bounds".into()))?; - dest.copy_from_slice(&memory[range]); - Ok(()) - } + pub fn read_memory_into( + &self, + ctx: impl AsContext, + address: Pointer, + dest: &mut [u8], + ) -> Result<()> { + let memory = self.memory.data(ctx.as_context()); + + let range = checked_range(address.into(), dest.len(), memory.len()) + .ok_or_else(|| Error::Other("memory read is out of bounds".into()))?; + dest.copy_from_slice(&memory[range]); + Ok(()) } /// Write data to the instance memory from a slice. /// /// Returns an error if the write would go out of the memory bounds. - pub fn write_memory_from(&self, dest_addr: Pointer, data: &[u8]) -> Result<()> { - unsafe { - // This should be safe since we don't grow up memory while caching this reference and - // we give up the reference before returning from this function. - let memory = self.memory_as_slice_mut(); - - let range = checked_range(dest_addr.into(), data.len(), memory.len()) - .ok_or_else(|| Error::Other("memory write is out of bounds".into()))?; - memory[range].copy_from_slice(data); - Ok(()) - } + pub fn write_memory_from( + &self, + mut ctx: impl AsContextMut, + address: Pointer, + data: &[u8], + ) -> Result<()> { + let memory = self.memory.data_mut(ctx.as_context_mut()); + + let range = checked_range(address.into(), data.len(), memory.len()) + .ok_or_else(|| Error::Other("memory write is out of bounds".into()))?; + memory[range].copy_from_slice(data); + Ok(()) } /// Allocate some memory of the given size. Returns pointer to the allocated memory region. @@ -345,16 +375,13 @@ impl InstanceWrapper { /// to get more details. pub fn allocate( &self, + mut ctx: impl AsContextMut, allocator: &mut sc_allocator::FreeingBumpHeapAllocator, size: WordSize, ) -> Result> { - unsafe { - // This should be safe since we don't grow up memory while caching this reference and - // we give up the reference before returning from this function. - let memory = self.memory_as_slice_mut(); + let memory = self.memory.data_mut(ctx.as_context_mut()); - allocator.allocate(memory, size).map_err(Into::into) - } + allocator.allocate(memory, size).map_err(Into::into) } /// Deallocate the memory pointed by the given pointer. @@ -362,64 +389,25 @@ impl InstanceWrapper { /// Returns `Err` in case the given memory region cannot be deallocated. pub fn deallocate( &self, + mut ctx: impl AsContextMut, allocator: &mut sc_allocator::FreeingBumpHeapAllocator, ptr: Pointer, ) -> Result<()> { - unsafe { - // This should be safe since we don't grow up memory while caching this reference and - // we give up the reference before returning from this function. - let memory = self.memory_as_slice_mut(); + let memory = self.memory.data_mut(ctx.as_context_mut()); - allocator.deallocate(memory, ptr).map_err(Into::into) - } - } - - /// Returns linear memory of the wasm instance as a slice. - /// - /// # Safety - /// - /// Wasmtime doesn't provide comprehensive documentation about the exact behavior of the data - /// pointer. If a dynamic style heap is used the base pointer of the heap can change. Since - /// growing, we cannot guarantee the lifetime of the returned slice reference. - unsafe fn memory_as_slice(&self) -> &[u8] { - let ptr = self.memory.data_ptr() as *const _; - let len = self.memory.data_size(); - - if len == 0 { - &[] - } else { - slice::from_raw_parts(ptr, len) - } - } - - /// Returns linear memory of the wasm instance as a slice. - /// - /// # Safety - /// - /// See `[memory_as_slice]`. In addition to those requirements, since a mutable reference is - /// returned it must be ensured that only one mutable and no shared references to memory exists - /// at the same time. - unsafe fn memory_as_slice_mut(&self) -> &mut [u8] { - let ptr = self.memory.data_ptr(); - let len = self.memory.data_size(); - - if len == 0 { - &mut [] - } else { - slice::from_raw_parts_mut(ptr, len) - } + allocator.deallocate(memory, ptr).map_err(Into::into) } /// Returns the pointer to the first byte of the linear memory for this instance. - pub fn base_ptr(&self) -> *const u8 { - self.memory.data_ptr() + pub fn base_ptr(&self, ctx: impl AsContext) -> *const u8 { + self.memory.data_ptr(ctx) } /// Removes physical backing from the allocated linear memory. This leads to returning the /// memory back to the system. While the memory is zeroed this is considered as a side-effect /// and is not relied upon. Thus this function acts as a hint. - pub fn decommit(&self) { - if self.memory.data_size() == 0 { + pub fn decommit(&self, ctx: impl AsContext) { + if self.memory.data_size(&ctx) == 0 { return } @@ -428,8 +416,8 @@ impl InstanceWrapper { use std::sync::Once; unsafe { - let ptr = self.memory.data_ptr(); - let len = self.memory.data_size(); + let ptr = self.memory.data_ptr(&ctx); + let len = self.memory.data_size(ctx); // Linux handles MADV_DONTNEED reliably. The result is that the given area // is unmapped and will be zeroed on the next pagefault. @@ -447,23 +435,3 @@ impl InstanceWrapper { } } } - -impl runtime_blob::InstanceGlobals for InstanceWrapper { - type Global = wasmtime::Global; - - fn get_global(&self, export_name: &str) -> Self::Global { - self.instance - .get_global(export_name) - .expect("get_global is guaranteed to be called with an export name of a global; qed") - } - - fn get_global_value(&self, global: &Self::Global) -> Value { - from_wasmtime_val(global.get()) - } - - fn set_global_value(&self, global: &Self::Global, value: Value) { - global.set(into_wasmtime_val(value)).expect( - "the value is guaranteed to be of the same value; the global is guaranteed to be mutable; qed", - ); - } -} diff --git a/client/executor/wasmtime/src/lib.rs b/client/executor/wasmtime/src/lib.rs index 62b0b205f6de6..8d7f93fecb307 100644 --- a/client/executor/wasmtime/src/lib.rs +++ b/client/executor/wasmtime/src/lib.rs @@ -21,7 +21,6 @@ mod host; mod imports; mod instance_wrapper; mod runtime; -mod state_holder; mod util; #[cfg(test)] diff --git a/client/executor/wasmtime/src/runtime.rs b/client/executor/wasmtime/src/runtime.rs index f6878ec5ee6e1..006f102926ecb 100644 --- a/client/executor/wasmtime/src/runtime.rs +++ b/client/executor/wasmtime/src/runtime.rs @@ -22,13 +22,15 @@ use crate::{ host::HostState, imports::{resolve_imports, Imports}, instance_wrapper::{EntryPoint, InstanceWrapper}, - state_holder, + util, }; use sc_allocator::FreeingBumpHeapAllocator; use sc_executor_common::{ error::{Result, WasmError}, - runtime_blob::{DataSegmentsSnapshot, ExposedMutableGlobalsSet, GlobalsSnapshot, RuntimeBlob}, + runtime_blob::{ + self, DataSegmentsSnapshot, ExposedMutableGlobalsSet, GlobalsSnapshot, RuntimeBlob, + }, wasm_runtime::{InvokeMethod, WasmInstance, WasmModule}, }; use sp_runtime_interface::unpack_ptr_and_len; @@ -38,7 +40,24 @@ use std::{ rc::Rc, sync::Arc, }; -use wasmtime::{Engine, Store}; +use wasmtime::{AsContext, AsContextMut, Engine, StoreLimits}; + +pub(crate) struct StoreData { + /// The limits we aply to the store. We need to store it here to return a reference to this + /// object when we have the limits enabled. + limits: StoreLimits, + /// This will only be set when we call into the runtime. + host_state: Option>, +} + +impl StoreData { + /// Returns a reference to the host state. + pub fn host_state(&self) -> Option<&Rc> { + self.host_state.as_ref() + } +} + +pub(crate) type Store = wasmtime::Store; enum Strategy { FastInstanceReuse { @@ -46,6 +65,7 @@ enum Strategy { globals_snapshot: GlobalsSnapshot, data_segments_snapshot: Arc, heap_base: u32, + store: Store, }, RecreateInstance(InstanceCreator), } @@ -58,8 +78,33 @@ struct InstanceCreator { } impl InstanceCreator { - fn instantiate(&self) -> Result { - InstanceWrapper::new(&self.store, &*self.module, &*self.imports, self.heap_pages) + fn instantiate(&mut self) -> Result { + InstanceWrapper::new(&*self.module, &*self.imports, self.heap_pages, &mut self.store) + } +} + +struct InstanceGlobals<'a, C> { + ctx: &'a mut C, + instance: &'a InstanceWrapper, +} + +impl<'a, C: AsContextMut> runtime_blob::InstanceGlobals for InstanceGlobals<'a, C> { + type Global = wasmtime::Global; + + fn get_global(&mut self, export_name: &str) -> Self::Global { + self.instance + .get_global(&mut self.ctx, export_name) + .expect("get_global is guaranteed to be called with an export name of a global; qed") + } + + fn get_global_value(&mut self, global: &Self::Global) -> Value { + util::from_wasmtime_val(global.get(&mut self.ctx)) + } + + fn set_global_value(&mut self, global: &Self::Global, value: Value) { + global.set(&mut self.ctx, util::into_wasmtime_val(value)).expect( + "the value is guaranteed to be of the same value; the global is guaranteed to be mutable; qed", + ); } } @@ -82,19 +127,25 @@ pub struct WasmtimeRuntime { impl WasmtimeRuntime { /// Creates the store respecting the set limits. fn new_store(&self) -> Store { - match self.config.max_memory_pages { - Some(max_memory_pages) => Store::new_with_limits( - &self.engine, - wasmtime::StoreLimitsBuilder::new().memory_pages(max_memory_pages).build(), - ), - None => Store::new(&self.engine), + let limits = if let Some(max_memory_pages) = self.config.max_memory_pages { + wasmtime::StoreLimitsBuilder::new().memory_pages(max_memory_pages).build() + } else { + Default::default() + }; + + let mut store = Store::new(&self.engine, StoreData { limits, host_state: None }); + + if self.config.max_memory_pages.is_some() { + store.limiter(|s| &mut s.limits); } + + store } } impl WasmModule for WasmtimeRuntime { fn new_instance(&self) -> Result> { - let store = self.new_store(); + let mut store = self.new_store(); // Scan all imports, find the matching host functions, and create stubs that adapt arguments // and results. @@ -103,7 +154,7 @@ impl WasmModule for WasmtimeRuntime { // However, I am not sure if that's a good idea since it would be pushing our luck // further by assuming that `Store` not only `Send` but also `Sync`. let imports = resolve_imports( - &store, + &mut store, &self.module, &self.host_functions, self.config.heap_pages, @@ -112,21 +163,24 @@ impl WasmModule for WasmtimeRuntime { let strategy = if let Some(ref snapshot_data) = self.snapshot_data { let instance_wrapper = - InstanceWrapper::new(&store, &self.module, &imports, self.config.heap_pages)?; - let heap_base = instance_wrapper.extract_heap_base()?; + InstanceWrapper::new(&self.module, &imports, self.config.heap_pages, &mut store)?; + let heap_base = instance_wrapper.extract_heap_base(&mut store)?; // This function panics if the instance was created from a runtime blob different from // which the mutable globals were collected. Here, it is easy to see that there is only // a single runtime blob and thus it's the same that was used for both creating the // instance and collecting the mutable globals. - let globals_snapshot = - GlobalsSnapshot::take(&snapshot_data.mutable_globals, &instance_wrapper); + let globals_snapshot = GlobalsSnapshot::take( + &snapshot_data.mutable_globals, + &mut InstanceGlobals { ctx: &mut store, instance: &instance_wrapper }, + ); Strategy::FastInstanceReuse { instance_wrapper: Rc::new(instance_wrapper), globals_snapshot, data_segments_snapshot: snapshot_data.data_segments_snapshot.clone(), heap_base, + store, } } else { Strategy::RecreateInstance(InstanceCreator { @@ -152,48 +206,63 @@ pub struct WasmtimeInstance { unsafe impl Send for WasmtimeInstance {} impl WasmInstance for WasmtimeInstance { - fn call(&self, method: InvokeMethod, data: &[u8]) -> Result> { - match &self.strategy { + fn call(&mut self, method: InvokeMethod, data: &[u8]) -> Result> { + match &mut self.strategy { Strategy::FastInstanceReuse { instance_wrapper, globals_snapshot, data_segments_snapshot, heap_base, + ref mut store, } => { - let entrypoint = instance_wrapper.resolve_entrypoint(method)?; + let entrypoint = instance_wrapper.resolve_entrypoint(method, &mut *store)?; data_segments_snapshot.apply(|offset, contents| { - instance_wrapper.write_memory_from(Pointer::new(offset), contents) + instance_wrapper.write_memory_from(&mut *store, Pointer::new(offset), contents) })?; - globals_snapshot.apply(&**instance_wrapper); + globals_snapshot + .apply(&mut InstanceGlobals { ctx: &mut *store, instance: &*instance_wrapper }); let allocator = FreeingBumpHeapAllocator::new(*heap_base); - let result = - perform_call(data, Rc::clone(&instance_wrapper), entrypoint, allocator); + let result = perform_call( + &mut *store, + data, + instance_wrapper.clone(), + entrypoint, + allocator, + ); // Signal to the OS that we are done with the linear memory and that it can be // reclaimed. - instance_wrapper.decommit(); + instance_wrapper.decommit(&store); result }, - Strategy::RecreateInstance(instance_creator) => { + Strategy::RecreateInstance(ref mut instance_creator) => { let instance_wrapper = instance_creator.instantiate()?; - let heap_base = instance_wrapper.extract_heap_base()?; - let entrypoint = instance_wrapper.resolve_entrypoint(method)?; + let heap_base = instance_wrapper.extract_heap_base(&mut instance_creator.store)?; + let entrypoint = + instance_wrapper.resolve_entrypoint(method, &mut instance_creator.store)?; let allocator = FreeingBumpHeapAllocator::new(heap_base); - perform_call(data, Rc::new(instance_wrapper), entrypoint, allocator) + perform_call( + &mut instance_creator.store, + data, + Rc::new(instance_wrapper), + entrypoint, + allocator, + ) }, } } - fn get_global_const(&self, name: &str) -> Result> { - match &self.strategy { - Strategy::FastInstanceReuse { instance_wrapper, .. } => - instance_wrapper.get_global_val(name), - Strategy::RecreateInstance(instance_creator) => - instance_creator.instantiate()?.get_global_val(name), + fn get_global_const(&mut self, name: &str) -> Result> { + match &mut self.strategy { + Strategy::FastInstanceReuse { instance_wrapper, ref mut store, .. } => + instance_wrapper.get_global_val(&mut *store, name), + Strategy::RecreateInstance(ref mut instance_creator) => instance_creator + .instantiate()? + .get_global_val(&mut instance_creator.store, name), } } @@ -204,8 +273,8 @@ impl WasmInstance for WasmtimeInstance { // associated with it. None }, - Strategy::FastInstanceReuse { instance_wrapper, .. } => - Some(instance_wrapper.base_ptr()), + Strategy::FastInstanceReuse { instance_wrapper, store, .. } => + Some(instance_wrapper.base_ptr(&store)), } } } @@ -536,40 +605,50 @@ pub fn prepare_runtime_artifact( } fn perform_call( + mut ctx: impl AsContextMut, data: &[u8], instance_wrapper: Rc, entrypoint: EntryPoint, mut allocator: FreeingBumpHeapAllocator, ) -> Result> { - let (data_ptr, data_len) = inject_input_data(&instance_wrapper, &mut allocator, data)?; + let (data_ptr, data_len) = + inject_input_data(&mut ctx, &instance_wrapper, &mut allocator, data)?; let host_state = HostState::new(allocator, instance_wrapper.clone()); - let ret = state_holder::with_initialized_state(&host_state, || -> Result<_> { - Ok(unpack_ptr_and_len(entrypoint.call(data_ptr, data_len)?)) - }); + + // Set the host state before calling into wasm. + ctx.as_context_mut().data_mut().host_state = Some(Rc::new(host_state)); + + let ret = entrypoint.call(&mut ctx, data_ptr, data_len).map(unpack_ptr_and_len); + + // Reset the host state + ctx.as_context_mut().data_mut().host_state = None; + let (output_ptr, output_len) = ret?; - let output = extract_output_data(&instance_wrapper, output_ptr, output_len)?; + let output = extract_output_data(ctx, &instance_wrapper, output_ptr, output_len)?; Ok(output) } fn inject_input_data( + mut ctx: impl AsContextMut, instance: &InstanceWrapper, allocator: &mut FreeingBumpHeapAllocator, data: &[u8], ) -> Result<(Pointer, WordSize)> { let data_len = data.len() as WordSize; - let data_ptr = instance.allocate(allocator, data_len)?; - instance.write_memory_from(data_ptr, data)?; + let data_ptr = instance.allocate(&mut ctx, allocator, data_len)?; + instance.write_memory_from(ctx, data_ptr, data)?; Ok((data_ptr, data_len)) } fn extract_output_data( + ctx: impl AsContext, instance: &InstanceWrapper, output_ptr: u32, output_len: u32, ) -> Result> { let mut output = vec![0; output_len as usize]; - instance.read_memory_into(Pointer::new(output_ptr), &mut output)?; + instance.read_memory_into(ctx, Pointer::new(output_ptr), &mut output)?; Ok(output) } diff --git a/client/executor/wasmtime/src/state_holder.rs b/client/executor/wasmtime/src/state_holder.rs deleted file mode 100644 index 0e2684cd25130..0000000000000 --- a/client/executor/wasmtime/src/state_holder.rs +++ /dev/null @@ -1,45 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -use crate::host::{HostContext, HostState}; - -scoped_tls::scoped_thread_local!(static HOST_STATE: HostState); - -/// Provide `HostState` for the runtime method call and execute the given function `f`. -/// -/// During the execution of the provided function `with_context` will be callable. -pub fn with_initialized_state(s: &HostState, f: F) -> R -where - F: FnOnce() -> R, -{ - HOST_STATE.set(s, f) -} - -/// Create a `HostContext` from the contained `HostState` and execute the given function `f`. -/// -/// This function is only callable within closure passed to `init_state`. Otherwise, the passed -/// context will be `None`. -pub fn with_context(f: F) -> R -where - F: FnOnce(Option) -> R, -{ - if !HOST_STATE.is_set() { - return f(None) - } - HOST_STATE.with(|state| f(Some(state.materialize()))) -} diff --git a/client/executor/wasmtime/src/tests.rs b/client/executor/wasmtime/src/tests.rs index 366352d7f5c39..2a8bcc0b01b04 100644 --- a/client/executor/wasmtime/src/tests.rs +++ b/client/executor/wasmtime/src/tests.rs @@ -116,7 +116,7 @@ fn test_nan_canonicalization() { builder.build() }; - let instance = runtime.new_instance().expect("failed to instantiate a runtime"); + let mut instance = runtime.new_instance().expect("failed to instantiate a runtime"); /// A NaN with canonical payload bits. const CANONICAL_NAN_BITS: u32 = 0x7fc00000; @@ -159,7 +159,7 @@ fn test_stack_depth_reaching() { builder.deterministic_stack(true); builder.build() }; - let instance = runtime.new_instance().expect("failed to instantiate a runtime"); + let mut instance = runtime.new_instance().expect("failed to instantiate a runtime"); let err = instance.call_export("test-many-locals", &[]).unwrap_err(); @@ -180,7 +180,7 @@ fn test_max_memory_pages() { builder.max_memory_pages(max_memory_pages); builder.build() }; - let instance = runtime.new_instance()?; + let mut instance = runtime.new_instance()?; let _ = instance.call_export("main", &[])?; Ok(()) } diff --git a/primitives/maybe-compressed-blob/Cargo.toml b/primitives/maybe-compressed-blob/Cargo.toml index 8d47c89ea8ebe..6994ccf5486ba 100644 --- a/primitives/maybe-compressed-blob/Cargo.toml +++ b/primitives/maybe-compressed-blob/Cargo.toml @@ -11,4 +11,4 @@ documentation = "https://docs.rs/sp-maybe-compressed-blob" readme = "README.md" [dependencies] -zstd = { version = "0.6.0", default-features = false } +zstd = { version = "0.9.0", default-features = false } diff --git a/primitives/state-machine/Cargo.toml b/primitives/state-machine/Cargo.toml index 457bbac5d2640..b62a24acd1244 100644 --- a/primitives/state-machine/Cargo.toml +++ b/primitives/state-machine/Cargo.toml @@ -35,7 +35,7 @@ tracing = { version = "0.1.22", optional = true } hex-literal = "0.3.1" sp-runtime = { version = "4.0.0-dev", path = "../runtime" } pretty_assertions = "0.6.1" -rand = { version = "0.7.2" } +rand = "0.7.2" [features] default = ["std"]