diff --git a/Cargo.lock b/Cargo.lock index c2d0bab1432..0c7e4ec10ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1356,9 +1356,6 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" -dependencies = [ - "ahash 0.8.11", -] [[package]] name = "heck" @@ -1784,12 +1781,6 @@ dependencies = [ "uuid", ] -[[package]] -name = "multi-stash" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "685a9ac4b61f4e728e1d2c6a7844609c16527aeb5e6c865915c08e619c16410f" - [[package]] name = "nanorand" version = "0.7.0" @@ -1849,17 +1840,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" -[[package]] -name = "num-derive" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.76", -] - [[package]] name = "num-format" version = "0.4.4" @@ -2318,6 +2298,7 @@ dependencies = [ "radix-substate-store-interface", "radix-transactions", "radix-wasm-instrument", + "radix-wasmi", "sbor", "strum", "syn 1.0.109", @@ -2325,7 +2306,6 @@ dependencies = [ "wabt", "walkdir", "wasm-benchmarks-lib", - "wasmi", "wasmparser 0.107.0", ] @@ -2578,6 +2558,24 @@ dependencies = [ "wasmprinter", ] +[[package]] +name = "radix-wasmi" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59bf4a0498ed223122e726f9360e9889c2bb6eb6898069f4b6f9960b067a742d" +dependencies = [ + "radix-wasmi-arena", + "spin 0.9.8", + "wasmi_core", + "wasmparser-nostd", +] + +[[package]] +name = "radix-wasmi-arena" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a03d7e7816ade40c6ecedff7515198906d644527771bd8fb5a335094353a15a" + [[package]] name = "rand" version = "0.4.6" @@ -3229,17 +3227,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9091b6114800a5f2141aee1d1b9d6ca3592ac062dc5decb3764ec5895a47b4eb" -[[package]] -name = "string-interner" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c6a0d765f5807e98a091107bae0a56ea3799f66a5de47b2c84c94a39c09974e" -dependencies = [ - "cfg-if", - "hashbrown 0.14.3", - "serde", -] - [[package]] name = "strsim" version = "0.10.0" @@ -3754,39 +3741,11 @@ dependencies = [ "cxx-build", ] -[[package]] -name = "wasmi" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbaac6e702fa7b52258e5ac90d6e20a40afb37a1fbe7c645d0903ee42c5f85f4" -dependencies = [ - "arrayvec", - "multi-stash", - "num-derive", - "num-traits", - "smallvec", - "spin 0.9.8", - "wasmi_collections", - "wasmi_core", - "wasmparser-nostd", -] - -[[package]] -name = "wasmi_collections" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ff59e30e550a509cc689ec638e5042be4d78ec9f6dd8a71fd02ee28776a74fd" -dependencies = [ - "ahash 0.8.11", - "hashbrown 0.14.3", - "string-interner", -] - [[package]] name = "wasmi_core" -version = "0.35.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e10c674add0f92f47bf8ad57c55ee3ac1762a0d9baf07535e27e22b758a916" +checksum = "d21fc8d8dafc2df566ebcba7d48a2df7b37386d6afa64237cdf9415c8d7d98ec" dependencies = [ "downcast-rs", "libm", @@ -3816,9 +3775,9 @@ dependencies = [ [[package]] name = "wasmparser-nostd" -version = "0.100.2" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5a015fe95f3504a94bb1462c717aae75253e39b9dd6c3fb1062c934535c64aa" +checksum = "9c37f310b5a62bfd5ae7c0f1d8e6f98af16a5d6d84ba764e9c36439ec14e318b" dependencies = [ "indexmap-nostd", ] diff --git a/Cargo.toml b/Cargo.toml index 7496b48b167..4ef531fe525 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -127,7 +127,7 @@ temp-env = { version = "0.2.0" } # Used in radix-clis trybuild = { version = "1.0.85" } wabt = { version = "0.10.0" } walkdir = { version = "2.3.3", default-features = false } -wasmi = { version = "0.35.0" } +radix-wasmi = { version = "1.0.0" } wasm-opt = { version = "0.114.1" } wasmparser = { version = "0.107.0", default-features = false } extend = { version = "1.2.0" } diff --git a/examples/everything/Cargo.lock b/examples/everything/Cargo.lock index 99cd65ec076..280d999bdb8 100644 --- a/examples/everything/Cargo.lock +++ b/examples/everything/Cargo.lock @@ -2,18 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "ahash" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" -dependencies = [ - "cfg-if", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "aliasable" version = "0.1.3" @@ -42,12 +30,6 @@ version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" -[[package]] -name = "arrayvec" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" - [[package]] name = "autocfg" version = "1.1.0" @@ -499,9 +481,6 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" -dependencies = [ - "ahash", -] [[package]] name = "heck" @@ -672,12 +651,6 @@ dependencies = [ "uuid", ] -[[package]] -name = "multi-stash" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "685a9ac4b61f4e728e1d2c6a7844609c16527aeb5e6c865915c08e619c16410f" - [[package]] name = "num-bigint" version = "0.4.3" @@ -689,17 +662,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "num-derive" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.76", -] - [[package]] name = "num-integer" version = "0.1.45" @@ -919,12 +881,12 @@ dependencies = [ "radix-substate-store-interface", "radix-transactions", "radix-wasm-instrument", + "radix-wasmi", "sbor", "strum", "syn 1.0.109", "tempfile", "walkdir", - "wasmi", "wasmparser 0.107.0", ] @@ -1062,6 +1024,24 @@ dependencies = [ "wasmprinter", ] +[[package]] +name = "radix-wasmi" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59bf4a0498ed223122e726f9360e9889c2bb6eb6898069f4b6f9960b067a742d" +dependencies = [ + "radix-wasmi-arena", + "spin", + "wasmi_core", + "wasmparser-nostd", +] + +[[package]] +name = "radix-wasmi-arena" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a03d7e7816ade40c6ecedff7515198906d644527771bd8fb5a335094353a15a" + [[package]] name = "rand_core" version = "0.6.4" @@ -1413,17 +1393,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" -[[package]] -name = "string-interner" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c6a0d765f5807e98a091107bae0a56ea3799f66a5de47b2c84c94a39c09974e" -dependencies = [ - "cfg-if", - "hashbrown 0.14.3", - "serde", -] - [[package]] name = "strum" version = "0.24.1" @@ -1701,39 +1670,11 @@ dependencies = [ "cxx-build", ] -[[package]] -name = "wasmi" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbaac6e702fa7b52258e5ac90d6e20a40afb37a1fbe7c645d0903ee42c5f85f4" -dependencies = [ - "arrayvec", - "multi-stash", - "num-derive", - "num-traits", - "smallvec", - "spin", - "wasmi_collections", - "wasmi_core", - "wasmparser-nostd", -] - -[[package]] -name = "wasmi_collections" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ff59e30e550a509cc689ec638e5042be4d78ec9f6dd8a71fd02ee28776a74fd" -dependencies = [ - "ahash", - "hashbrown 0.14.3", - "string-interner", -] - [[package]] name = "wasmi_core" -version = "0.35.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e10c674add0f92f47bf8ad57c55ee3ac1762a0d9baf07535e27e22b758a916" +checksum = "d21fc8d8dafc2df566ebcba7d48a2df7b37386d6afa64237cdf9415c8d7d98ec" dependencies = [ "downcast-rs", "libm", @@ -1763,9 +1704,9 @@ dependencies = [ [[package]] name = "wasmparser-nostd" -version = "0.100.2" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5a015fe95f3504a94bb1462c717aae75253e39b9dd6c3fb1062c934535c64aa" +checksum = "9c37f310b5a62bfd5ae7c0f1d8e6f98af16a5d6d84ba764e9c36439ec14e318b" dependencies = [ "indexmap-nostd", ] @@ -1959,26 +1900,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "zerocopy" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.76", -] - [[package]] name = "zeroize" version = "1.8.1" diff --git a/examples/hello-world/Cargo.lock b/examples/hello-world/Cargo.lock index f1e3980cfac..7c5af476627 100644 --- a/examples/hello-world/Cargo.lock +++ b/examples/hello-world/Cargo.lock @@ -2,18 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "ahash" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" -dependencies = [ - "cfg-if", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "aliasable" version = "0.1.3" @@ -42,12 +30,6 @@ version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" -[[package]] -name = "arrayvec" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" - [[package]] name = "autocfg" version = "1.1.0" @@ -491,9 +473,6 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" -dependencies = [ - "ahash", -] [[package]] name = "heck" @@ -672,12 +651,6 @@ dependencies = [ "uuid", ] -[[package]] -name = "multi-stash" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "685a9ac4b61f4e728e1d2c6a7844609c16527aeb5e6c865915c08e619c16410f" - [[package]] name = "num-bigint" version = "0.4.3" @@ -689,17 +662,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "num-derive" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.76", -] - [[package]] name = "num-integer" version = "0.1.45" @@ -919,12 +881,12 @@ dependencies = [ "radix-substate-store-interface", "radix-transactions", "radix-wasm-instrument", + "radix-wasmi", "sbor", "strum", "syn 1.0.109", "tempfile", "walkdir", - "wasmi", "wasmparser 0.107.0", ] @@ -1062,6 +1024,24 @@ dependencies = [ "wasmprinter", ] +[[package]] +name = "radix-wasmi" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59bf4a0498ed223122e726f9360e9889c2bb6eb6898069f4b6f9960b067a742d" +dependencies = [ + "radix-wasmi-arena", + "spin", + "wasmi_core", + "wasmparser-nostd", +] + +[[package]] +name = "radix-wasmi-arena" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a03d7e7816ade40c6ecedff7515198906d644527771bd8fb5a335094353a15a" + [[package]] name = "rand_core" version = "0.6.4" @@ -1413,17 +1393,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" -[[package]] -name = "string-interner" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c6a0d765f5807e98a091107bae0a56ea3799f66a5de47b2c84c94a39c09974e" -dependencies = [ - "cfg-if", - "hashbrown 0.14.3", - "serde", -] - [[package]] name = "strum" version = "0.24.1" @@ -1701,39 +1670,11 @@ dependencies = [ "cxx-build", ] -[[package]] -name = "wasmi" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbaac6e702fa7b52258e5ac90d6e20a40afb37a1fbe7c645d0903ee42c5f85f4" -dependencies = [ - "arrayvec", - "multi-stash", - "num-derive", - "num-traits", - "smallvec", - "spin", - "wasmi_collections", - "wasmi_core", - "wasmparser-nostd", -] - -[[package]] -name = "wasmi_collections" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ff59e30e550a509cc689ec638e5042be4d78ec9f6dd8a71fd02ee28776a74fd" -dependencies = [ - "ahash", - "hashbrown 0.14.3", - "string-interner", -] - [[package]] name = "wasmi_core" -version = "0.35.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e10c674add0f92f47bf8ad57c55ee3ac1762a0d9baf07535e27e22b758a916" +checksum = "d21fc8d8dafc2df566ebcba7d48a2df7b37386d6afa64237cdf9415c8d7d98ec" dependencies = [ "downcast-rs", "libm", @@ -1763,9 +1704,9 @@ dependencies = [ [[package]] name = "wasmparser-nostd" -version = "0.100.2" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5a015fe95f3504a94bb1462c717aae75253e39b9dd6c3fb1062c934535c64aa" +checksum = "9c37f310b5a62bfd5ae7c0f1d8e6f98af16a5d6d84ba764e9c36439ec14e318b" dependencies = [ "indexmap-nostd", ] @@ -1959,26 +1900,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "zerocopy" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.76", -] - [[package]] name = "zeroize" version = "1.8.1" diff --git a/radix-clis/assets/template/Cargo.lock_template b/radix-clis/assets/template/Cargo.lock_template index 51c677ce95c..90ce59dea05 100644 --- a/radix-clis/assets/template/Cargo.lock_template +++ b/radix-clis/assets/template/Cargo.lock_template @@ -2,18 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "ahash" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" -dependencies = [ - "cfg-if", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "aliasable" version = "0.1.3" @@ -42,12 +30,6 @@ version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" -[[package]] -name = "arrayvec" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" - [[package]] name = "autocfg" version = "1.1.0" @@ -491,9 +473,6 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" -dependencies = [ - "ahash", -] [[package]] name = "heck" @@ -664,12 +643,6 @@ dependencies = [ "uuid", ] -[[package]] -name = "multi-stash" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "685a9ac4b61f4e728e1d2c6a7844609c16527aeb5e6c865915c08e619c16410f" - [[package]] name = "num-bigint" version = "0.4.3" @@ -681,17 +654,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "num-derive" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.76", -] - [[package]] name = "num-integer" version = "0.1.45" @@ -911,12 +873,12 @@ dependencies = [ "radix-substate-store-interface", "radix-transactions", "radix-wasm-instrument", + "radix-wasmi", "sbor", "strum", "syn 1.0.109", "tempfile", "walkdir", - "wasmi", "wasmparser 0.107.0", ] @@ -1054,6 +1016,24 @@ dependencies = [ "wasmprinter", ] +[[package]] +name = "radix-wasmi" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59bf4a0498ed223122e726f9360e9889c2bb6eb6898069f4b6f9960b067a742d" +dependencies = [ + "radix-wasmi-arena", + "spin", + "wasmi_core", + "wasmparser-nostd", +] + +[[package]] +name = "radix-wasmi-arena" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a03d7e7816ade40c6ecedff7515198906d644527771bd8fb5a335094353a15a" + [[package]] name = "rand_core" version = "0.6.4" @@ -1405,17 +1385,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" -[[package]] -name = "string-interner" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c6a0d765f5807e98a091107bae0a56ea3799f66a5de47b2c84c94a39c09974e" -dependencies = [ - "cfg-if", - "hashbrown 0.14.3", - "serde", -] - [[package]] name = "strum" version = "0.24.1" @@ -1693,39 +1662,11 @@ dependencies = [ "cxx-build", ] -[[package]] -name = "wasmi" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbaac6e702fa7b52258e5ac90d6e20a40afb37a1fbe7c645d0903ee42c5f85f4" -dependencies = [ - "arrayvec", - "multi-stash", - "num-derive", - "num-traits", - "smallvec", - "spin", - "wasmi_collections", - "wasmi_core", - "wasmparser-nostd", -] - -[[package]] -name = "wasmi_collections" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ff59e30e550a509cc689ec638e5042be4d78ec9f6dd8a71fd02ee28776a74fd" -dependencies = [ - "ahash", - "hashbrown 0.14.3", - "string-interner", -] - [[package]] name = "wasmi_core" -version = "0.35.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e10c674add0f92f47bf8ad57c55ee3ac1762a0d9baf07535e27e22b758a916" +checksum = "d21fc8d8dafc2df566ebcba7d48a2df7b37386d6afa64237cdf9415c8d7d98ec" dependencies = [ "downcast-rs", "libm", @@ -1755,9 +1696,9 @@ dependencies = [ [[package]] name = "wasmparser-nostd" -version = "0.100.2" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5a015fe95f3504a94bb1462c717aae75253e39b9dd6c3fb1062c934535c64aa" +checksum = "9c37f310b5a62bfd5ae7c0f1d8e6f98af16a5d6d84ba764e9c36439ec14e318b" dependencies = [ "indexmap-nostd", ] @@ -1951,26 +1892,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "zerocopy" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.76", -] - [[package]] name = "zeroize" version = "1.8.1" diff --git a/radix-engine-tests/tests/vm/system_wasm_buffers.rs b/radix-engine-tests/tests/vm/system_wasm_buffers.rs index d441a53e5fa..2a4e855d8b7 100644 --- a/radix-engine-tests/tests/vm/system_wasm_buffers.rs +++ b/radix-engine-tests/tests/vm/system_wasm_buffers.rs @@ -246,7 +246,7 @@ fn test_wasm_buffer_read_memory_instruction_trap() { // This error is really nasty and we should somehow prevent it from occurring. // Especially that we know that transaction will fail for smaller sizes... RuntimeError::VmError(VmError::Wasm(WasmRuntimeError::ExecutionError(message))) => { - message == "Error { kind: TrapCode(UnreachableCodeReached) }" + message.contains("UnreachableCodeReached") } _ => false, }); @@ -265,7 +265,7 @@ fn test_wasm_buffer_read_memory_instruction_trap() { // Assert receipt.expect_specific_failure(|e| match e { RuntimeError::VmError(VmError::Wasm(WasmRuntimeError::ExecutionError(message))) => { - message == "Error { kind: TrapCode(UnreachableCodeReached) }" + message.contains("UnreachableCodeReached") } _ => false, }); diff --git a/radix-engine/Cargo.toml b/radix-engine/Cargo.toml index 448ee5e3310..68ca79e4fa9 100644 --- a/radix-engine/Cargo.toml +++ b/radix-engine/Cargo.toml @@ -45,7 +45,7 @@ radix-wasm-instrument = { workspace = true, features = ["ignore_custom_section" # WASM execution # - Wasmi is a WASM interpreter that supports WebAssembly MVP -wasmi = { workspace = true, features = ["no-hash-maps"] } +radix-wasmi = { workspace = true } lazy_static = { workspace = true } [dev-dependencies] @@ -60,7 +60,7 @@ harness = false [features] # You should enable either `std` or `alloc` default = ["std", "moka"] -std = ["sbor/std", "radix-native-sdk/std", "radix-transactions/std", "radix-blueprint-schema-init/std", "radix-engine-interface/std", "radix-substate-store-interface/std", "radix-common-derive/std", "radix-rust/std", "radix-wasm-instrument/std", "wasmi/std" ] +std = ["sbor/std", "radix-native-sdk/std", "radix-transactions/std", "radix-blueprint-schema-init/std", "radix-engine-interface/std", "radix-substate-store-interface/std", "radix-common-derive/std", "radix-rust/std", "radix-wasm-instrument/std", "radix-wasmi/std" ] alloc = ["sbor/alloc", "radix-native-sdk/alloc", "radix-transactions/alloc", "radix-blueprint-schema-init/alloc", "radix-engine-interface/alloc", "radix-substate-store-interface/alloc", "radix-common-derive/alloc", "radix-rust/alloc", "lru?/hashbrown"] # Enables heap memory and CPU cycles resource tracing - available only for Linux OS on x86 arch. diff --git a/radix-engine/benches/wasm_benchmarks.rs b/radix-engine/benches/wasm_benchmarks.rs index 247f464415b..34d05b1b884 100644 --- a/radix-engine/benches/wasm_benchmarks.rs +++ b/radix-engine/benches/wasm_benchmarks.rs @@ -1,3 +1,5 @@ +extern crate radix_wasmi as wasmi; + use criterion::{black_box, criterion_group, criterion_main, Criterion}; use radix_common::math::{traits::*, Decimal, PreciseDecimal}; use std::process::Command; @@ -66,7 +68,7 @@ macro_rules! wasmi_native { mut caller: wasmi::Caller<'_, HostState>, a_ptr: u32, b_ptr: u32, - c_ptr: u32) -> Result { + c_ptr: u32) -> Result { let memory = match caller.get_export("memory") { Some(wasmi::Extern::Memory(memory)) => memory, _ => panic!("Failed to find memory export"), @@ -115,7 +117,7 @@ fn wasmi_get_instance( code: &[u8], ) -> wasmi::Instance { let module = wasmi::Module::new(&engine, code).unwrap(); - let mut linker = >::new(engine); + let mut linker = >::new(); macro_rules! linker_define { ($name:tt) => { diff --git a/radix-engine/src/vm/wasm/prepare.rs b/radix-engine/src/vm/wasm/prepare.rs index 6f1c77211f3..a6af9e31d28 100644 --- a/radix-engine/src/vm/wasm/prepare.rs +++ b/radix-engine/src/vm/wasm/prepare.rs @@ -1259,12 +1259,9 @@ impl WasmModule { // Because the offset can be an `InitExpr` that requires evaluation against an WASM instance, // we're using the `wasmi` logic as a shortcut. let code = self.module.bytes(); - WasmiModule::new(&code[..]) - .map_err(|_| PrepareError::NotCompilable)? - .instantiate() - .map_err(|e| PrepareError::NotInstantiatable { - reason: format!("{:?}", e), - })?; + WasmiModule::new(&code[..]).map_err(|e| PrepareError::NotInstantiatable { + reason: format!("{:?}", e), + })?; Ok(self) } diff --git a/radix-engine/src/vm/wasm/wasmi.rs b/radix-engine/src/vm/wasm/wasmi.rs index 29d1753893a..80071a06148 100644 --- a/radix-engine/src/vm/wasm/wasmi.rs +++ b/radix-engine/src/vm/wasm/wasmi.rs @@ -1,3 +1,5 @@ +extern crate radix_wasmi as wasmi; + use crate::errors::InvokeError; use crate::internal_prelude::*; #[cfg(feature = "coverage")] @@ -8,33 +10,67 @@ use crate::vm::wasm::traits::*; use crate::vm::wasm::WasmEngine; use radix_engine_interface::api::actor_api::EventFlags; use radix_engine_interface::blueprints::package::CodeHash; +use sbor::rust::mem::transmute; use sbor::rust::mem::MaybeUninit; #[cfg(not(feature = "fuzzing"))] use sbor::rust::sync::Arc; -use wasmi::core::HostError; +use wasmi::core::Value; +use wasmi::core::{HostError, Trap}; use wasmi::errors::InstantiationError; use wasmi::*; +type FakeHostState = FakeWasmiInstanceEnv; type HostState = WasmiInstanceEnv; -/// A `WasmiModule` defines a compiled WASM module +/// A `WasmiModule` defines a parsed WASM module "template" Instance (with imports already defined) +/// and Store, which keeps user data. +/// "Template" (Store, Instance) tuple are cached together, and never to be invoked. +/// Upon instantiation Instance and Store are cloned, so the state is not shared between instances. +/// It is safe to clone an `Instance` and a `Store`, since they don't use pointers, but `Arena` +/// allocator. `Instance` is owned by `Store`, it is basically some offset within `Store`'s vector +/// of `Instance`s. So after clone we receive the same `Store`, where we are able to set different +/// data, more specifically a `runtime_ptr`. +/// Also, it is correctly `Send + Sync` (under the assumption that the data in the Store is set to +/// a valid value upon invocation , because this is the thing which is cached in the +/// ScryptoInterpreter caches. pub struct WasmiModule { - module: Module, + template_store: Store, + template_instance: Instance, #[allow(dead_code)] code_size_bytes: usize, } -/// A `WasmiModule` defines -/// - an instantiated WASM module -/// - a Store , which keeps user data -/// - a Memory - linear memory reference to the Store pub struct WasmiInstance { store: Store, instance: Instance, memory: Memory, } -/// This is to construct a `Store` +/// This is to construct a stub `Store`, which is a part of +/// `WasmiModule` struct and serves as a placeholder for the real `Store`. +/// The real store is set (prior being transumted) when the `WasmiModule` is being instantiated. +/// In fact the only difference between a stub and real Store is the `Send + Sync` manually +/// implemented for the former one, which is required by `WasmiModule` cache (for `std` +/// configuration) but shall not be implemented for the latter one to prevent sharing it between +/// the threads since pointer might point to volatile data. +#[derive(Clone)] +pub struct FakeWasmiInstanceEnv { + #[allow(dead_code)] + runtime_ptr: MaybeUninit<*mut Box>, +} + +impl FakeWasmiInstanceEnv { + pub fn new() -> Self { + Self { + runtime_ptr: MaybeUninit::uninit(), + } + } +} + +unsafe impl Send for FakeWasmiInstanceEnv {} +unsafe impl Sync for FakeWasmiInstanceEnv {} + +/// This is to construct a real `Store pub struct WasmiInstanceEnv { runtime_ptr: MaybeUninit<*mut Box>, } @@ -929,6 +965,7 @@ macro_rules! linker_define { #[derive(Debug)] pub enum WasmiInstantiationError { + ValidationError(Error), CompilationError(Error), PreInstantiationError(Error), InstantiationError(InstantiationError), @@ -936,25 +973,20 @@ pub enum WasmiInstantiationError { impl WasmiModule { pub fn new(code: &[u8]) -> Result { - let mut config = wasmi::Config::default(); - - // In order to speed compilation we deliberately - // - use LazyTranslation compilation mode - // - compiling without WASM validation (Module::new_checked()) - // (for more details see: https://github.com/wasmi-labs/wasmi/releases/tag/v0.32.0) - // - // It is assumed that WASM code passed here is already WASM validated, - // so above combination should be fine. - config.compilation_mode(wasmi::CompilationMode::LazyTranslation); - let engine = Engine::new(&config); - - let module = unsafe { - Module::new_unchecked(&engine, code) - .map_err(WasmiInstantiationError::CompilationError)? - }; + let engine = Engine::default(); + let mut store = Store::new(&engine, WasmiInstanceEnv::new()); + + let module = + Module::new(&engine, code).map_err(WasmiInstantiationError::ValidationError)?; + + let instance = Self::host_funcs_set(&module, &mut store) + .map_err(WasmiInstantiationError::PreInstantiationError)? + .ensure_no_start(store.as_context_mut()) + .map_err(WasmiInstantiationError::InstantiationError)?; Ok(Self { - module, + template_store: unsafe { transmute(store) }, + template_instance: instance, code_size_bytes: code.len(), }) } @@ -965,8 +997,8 @@ impl WasmiModule { |caller: Caller<'_, HostState>, buffer_id: BufferId, destination_ptr: u32| - -> Result<(), Error> { - consume_buffer(caller, buffer_id, destination_ptr).map_err(|e| Error::host(e)) + -> Result<(), Trap> { + consume_buffer(caller, buffer_id, destination_ptr).map_err(|e| e.into()) }, ); @@ -979,7 +1011,7 @@ impl WasmiModule { ident_len: u32, args_ptr: u32, args_len: u32| - -> Result { + -> Result { call_method( caller, receiver_ptr, @@ -989,7 +1021,7 @@ impl WasmiModule { args_ptr, args_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1003,7 +1035,7 @@ impl WasmiModule { ident_len: u32, args_ptr: u32, args_len: u32| - -> Result { + -> Result { call_module_method( caller, receiver_ptr, @@ -1014,7 +1046,7 @@ impl WasmiModule { args_ptr, args_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1027,7 +1059,7 @@ impl WasmiModule { ident_len: u32, args_ptr: u32, args_len: u32| - -> Result { + -> Result { call_direct_method( caller, receiver_ptr, @@ -1037,7 +1069,7 @@ impl WasmiModule { args_ptr, args_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1052,7 +1084,7 @@ impl WasmiModule { ident_len: u32, args_ptr: u32, args_len: u32| - -> Result { + -> Result { call_function( caller, package_address_ptr, @@ -1064,7 +1096,7 @@ impl WasmiModule { args_ptr, args_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1075,7 +1107,7 @@ impl WasmiModule { blueprint_name_len: u32, object_states_ptr: u32, object_states_len: u32| - -> Result { + -> Result { new_object( caller, blueprint_name_ptr, @@ -1083,7 +1115,7 @@ impl WasmiModule { object_states_ptr, object_states_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1092,8 +1124,8 @@ impl WasmiModule { |caller: Caller<'_, HostState>, schema_ptr: u32, schema_len: u32| - -> Result { - new_key_value_store(caller, schema_ptr, schema_len).map_err(|e| Error::host(e)) + -> Result { + new_key_value_store(caller, schema_ptr, schema_len).map_err(|e| e.into()) }, ); @@ -1104,7 +1136,7 @@ impl WasmiModule { package_address_len: u32, blueprint_name_ptr: u32, blueprint_name_len: u32| - -> Result { + -> Result { allocate_global_address( caller, package_address_ptr, @@ -1112,7 +1144,7 @@ impl WasmiModule { blueprint_name_ptr, blueprint_name_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1121,58 +1153,57 @@ impl WasmiModule { |caller: Caller<'_, HostState>, node_id_ptr: u32, node_id_len: u32| - -> Result { - get_reservation_address(caller, node_id_ptr, node_id_len) - .map_err(|e| Error::host(e)) + -> Result { + get_reservation_address(caller, node_id_ptr, node_id_len).map_err(|e| e.into()) }, ); let host_execution_cost_unit_limit = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - execution_cost_unit_limit(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + execution_cost_unit_limit(caller).map_err(|e| e.into()) }, ); let host_execution_cost_unit_price = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - execution_cost_unit_price(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + execution_cost_unit_price(caller).map_err(|e| e.into()) }, ); let host_finalization_cost_unit_limit = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - finalization_cost_unit_limit(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + finalization_cost_unit_limit(caller).map_err(|e| e.into()) }, ); let host_finalization_cost_unit_price = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - finalization_cost_unit_price(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + finalization_cost_unit_price(caller).map_err(|e| e.into()) }, ); let host_usd_price = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - usd_price(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + usd_price(caller).map_err(|e| e.into()) }, ); let host_tip_percentage = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - tip_percentage(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + tip_percentage(caller).map_err(|e| e.into()) }, ); let host_fee_balance = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - fee_balance(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + fee_balance(caller).map_err(|e| e.into()) }, ); @@ -1185,7 +1216,7 @@ impl WasmiModule { modules_len: u32, address_ptr: u32, address_len: u32| - -> Result { + -> Result { globalize_object( caller, obj_ptr, @@ -1195,7 +1226,7 @@ impl WasmiModule { address_ptr, address_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1208,7 +1239,7 @@ impl WasmiModule { package_address_len: u32, blueprint_name_ptr: u32, blueprint_name_len: u32| - -> Result { + -> Result { instance_of( caller, object_id_ptr, @@ -1218,7 +1249,7 @@ impl WasmiModule { blueprint_name_ptr, blueprint_name_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1227,8 +1258,8 @@ impl WasmiModule { |caller: Caller<'_, HostState>, object_id_ptr: u32, object_id_len: u32| - -> Result { - blueprint_id(caller, object_id_ptr, object_id_len).map_err(|e| Error::host(e)) + -> Result { + blueprint_id(caller, object_id_ptr, object_id_len).map_err(|e| e.into()) }, ); @@ -1237,8 +1268,8 @@ impl WasmiModule { |caller: Caller<'_, HostState>, object_id_ptr: u32, object_id_len: u32| - -> Result { - get_outer_object(caller, object_id_ptr, object_id_len).map_err(|e| Error::host(e)) + -> Result { + get_outer_object(caller, object_id_ptr, object_id_len).map_err(|e| e.into()) }, ); @@ -1250,7 +1281,7 @@ impl WasmiModule { offset_ptr: u32, offset_len: u32, mutable: u32| - -> Result { + -> Result { lock_key_value_store_entry( caller, node_id_ptr, @@ -1259,14 +1290,14 @@ impl WasmiModule { offset_len, mutable, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); let host_key_value_entry_get = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, handle: u32| -> Result { - key_value_entry_get(caller, handle).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, handle: u32| -> Result { + key_value_entry_get(caller, handle).map_err(|e| e.into()) }, ); @@ -1276,23 +1307,22 @@ impl WasmiModule { handle: u32, buffer_ptr: u32, buffer_len: u32| - -> Result<(), Error> { - key_value_entry_set(caller, handle, buffer_ptr, buffer_len) - .map_err(|e| Error::host(e)) + -> Result<(), Trap> { + key_value_entry_set(caller, handle, buffer_ptr, buffer_len).map_err(|e| e.into()) }, ); let host_key_value_entry_remove = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, handle: u32| -> Result { - key_value_entry_remove(caller, handle).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, handle: u32| -> Result { + key_value_entry_remove(caller, handle).map_err(|e| e.into()) }, ); let host_unlock_key_value_entry = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, handle: u32| -> Result<(), Error> { - unlock_key_value_entry(caller, handle).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, handle: u32| -> Result<(), Trap> { + unlock_key_value_entry(caller, handle).map_err(|e| e.into()) }, ); @@ -1303,9 +1333,9 @@ impl WasmiModule { node_id_len: u32, key_ptr: u32, key_len: u32| - -> Result { + -> Result { key_value_store_remove(caller, node_id_ptr, node_id_len, key_ptr, key_len) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1315,15 +1345,15 @@ impl WasmiModule { object_handle: u32, field: u32, lock_flags: u32| - -> Result { - lock_field(caller, object_handle, field, lock_flags).map_err(|e| Error::host(e)) + -> Result { + lock_field(caller, object_handle, field, lock_flags).map_err(|e| e.into()) }, ); let host_field_lock_read = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, handle: u32| -> Result { - field_lock_read(caller, handle).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, handle: u32| -> Result { + field_lock_read(caller, handle).map_err(|e| e.into()) }, ); @@ -1333,43 +1363,43 @@ impl WasmiModule { handle: u32, data_ptr: u32, data_len: u32| - -> Result<(), Error> { - field_lock_write(caller, handle, data_ptr, data_len).map_err(|e| Error::host(e)) + -> Result<(), Trap> { + field_lock_write(caller, handle, data_ptr, data_len).map_err(|e| e.into()) }, ); let host_field_lock_release = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, handle: u32| -> Result<(), Error> { - field_lock_release(caller, handle).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, handle: u32| -> Result<(), Trap> { + field_lock_release(caller, handle).map_err(|e| e.into()) }, ); let host_actor_get_node_id = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, handle: u32| -> Result { - actor_get_node_id(caller, handle).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, handle: u32| -> Result { + actor_get_node_id(caller, handle).map_err(|e| e.into()) }, ); let host_get_package_address = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - get_package_address(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + get_package_address(caller).map_err(|e| e.into()) }, ); let host_get_blueprint_name = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - get_blueprint_name(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + get_blueprint_name(caller).map_err(|e| e.into()) }, ); let host_consume_wasm_execution_units = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, n: u64| -> Result<(), Error> { - consume_wasm_execution_units(caller, n).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, n: u64| -> Result<(), Trap> { + consume_wasm_execution_units(caller, n).map_err(|e| e.into()) }, ); @@ -1381,7 +1411,7 @@ impl WasmiModule { event_data_ptr: u32, event_data_len: u32, flags: u32| - -> Result<(), Error> { + -> Result<(), Trap> { emit_event( caller, event_name_ptr, @@ -1390,7 +1420,7 @@ impl WasmiModule { event_data_len, flags, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1401,9 +1431,9 @@ impl WasmiModule { level_len: u32, message_ptr: u32, message_len: u32| - -> Result<(), Error> { + -> Result<(), Trap> { emit_log(caller, level_ptr, level_len, message_ptr, message_len) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1412,8 +1442,8 @@ impl WasmiModule { |caller: Caller<'_, HostState>, message_ptr: u32, message_len: u32| - -> Result<(), Error> { - panic(caller, message_ptr, message_len).map_err(|e| Error::host(e)) + -> Result<(), Trap> { + panic(caller, message_ptr, message_len).map_err(|e| e.into()) }, ); @@ -1422,22 +1452,22 @@ impl WasmiModule { |caller: Caller<'_, HostState>, address_ptr: u32, address_len: u32| - -> Result { - bech32_encode_address(caller, address_ptr, address_len).map_err(|e| Error::host(e)) + -> Result { + bech32_encode_address(caller, address_ptr, address_len).map_err(|e| e.into()) }, ); let host_get_transaction_hash = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - get_transaction_hash(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + get_transaction_hash(caller).map_err(|e| e.into()) }, ); let host_generate_ruid = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - generate_ruid(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + generate_ruid(caller).map_err(|e| e.into()) }, ); @@ -1450,7 +1480,7 @@ impl WasmiModule { public_key_len: u32, signature_ptr: u32, signature_len: u32| - -> Result { + -> Result { bls12381_v1_verify( caller, message_ptr, @@ -1460,7 +1490,7 @@ impl WasmiModule { signature_ptr, signature_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1471,7 +1501,7 @@ impl WasmiModule { pub_keys_and_msgs_len: u32, signature_ptr: u32, signature_len: u32| - -> Result { + -> Result { bls12381_v1_aggregate_verify( caller, pub_keys_and_msgs_ptr, @@ -1479,7 +1509,7 @@ impl WasmiModule { signature_ptr, signature_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1492,7 +1522,7 @@ impl WasmiModule { public_keys_len: u32, signature_ptr: u32, signature_len: u32| - -> Result { + -> Result { bls12381_v1_fast_aggregate_verify( caller, message_ptr, @@ -1502,7 +1532,7 @@ impl WasmiModule { signature_ptr, signature_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); @@ -1511,23 +1541,23 @@ impl WasmiModule { |caller: Caller<'_, HostState>, signatures_ptr: u32, signatures_len: u32| - -> Result { + -> Result { bls12381_g2_signature_aggregate(caller, signatures_ptr, signatures_len) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); let host_keccak256_hash = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, data_ptr: u32, data_len: u32| -> Result { - keccak256_hash(caller, data_ptr, data_len).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, data_ptr: u32, data_len: u32| -> Result { + keccak256_hash(caller, data_ptr, data_len).map_err(|e| e.into()) }, ); let host_blake2b_256_hash = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>, data_ptr: u32, data_len: u32| -> Result { - blake2b_256_hash(caller, data_ptr, data_len).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>, data_ptr: u32, data_len: u32| -> Result { + blake2b_256_hash(caller, data_ptr, data_len).map_err(|e| e.into()) }, ); @@ -1540,7 +1570,7 @@ impl WasmiModule { public_key_len: u32, signature_ptr: u32, signature_len: u32| - -> Result { + -> Result { ed25519_verify( caller, message_ptr, @@ -1550,7 +1580,7 @@ impl WasmiModule { signature_ptr, signature_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); let host_secp2561k1_ecdsa_verify = Func::wrap( @@ -1562,7 +1592,7 @@ impl WasmiModule { public_key_len: u32, signature_ptr: u32, signature_len: u32| - -> Result { + -> Result { secp256k1_ecdsa_verify( caller, message_ptr, @@ -1572,7 +1602,7 @@ impl WasmiModule { signature_ptr, signature_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); let host_secp2561k1_ecdsa_verify_and_key_recover = Func::wrap( @@ -1582,7 +1612,7 @@ impl WasmiModule { message_len: u32, signature_ptr: u32, signature_len: u32| - -> Result { + -> Result { secp256k1_ecdsa_verify_and_key_recover( caller, message_ptr, @@ -1590,11 +1620,11 @@ impl WasmiModule { signature_ptr, signature_len, ) - .map_err(|e| Error::host(e)) + .map_err(|e| e.into()) }, ); - let mut linker = >::new(module.engine()); + let mut linker = >::new(); linker_define!(linker, BUFFER_CONSUME_FUNCTION_NAME, host_consume_buffer); linker_define!(linker, OBJECT_CALL_FUNCTION_NAME, host_call_method); @@ -1804,8 +1834,8 @@ impl WasmiModule { |caller: Caller<'_, HostState>, memory_offs: u32, data_len: u32| - -> Result<(), Error> { - test_host_read_memory(caller, memory_offs, data_len).map_err(|e| Error::host(e)) + -> Result<(), Trap> { + test_host_read_memory(caller, memory_offs, data_len).map_err(|e| e.into()) }, ); let host_write_memory = Func::wrap( @@ -1813,15 +1843,14 @@ impl WasmiModule { |caller: Caller<'_, HostState>, memory_offs: u32, data_len: u32| - -> Result<(), Error> { - test_host_write_memory(caller, memory_offs, data_len) - .map_err(|e| Error::host(e)) + -> Result<(), Trap> { + test_host_write_memory(caller, memory_offs, data_len).map_err(|e| e.into()) }, ); let host_check_memory_is_clean = Func::wrap( store.as_context_mut(), - |caller: Caller<'_, HostState>| -> Result { - test_host_check_memory_is_clean(caller).map_err(|e| Error::host(e)) + |caller: Caller<'_, HostState>| -> Result { + test_host_check_memory_is_clean(caller).map_err(|e| e.into()) }, ); linker_define!(linker, "test_host_read_memory", host_read_memory); @@ -1836,28 +1865,19 @@ impl WasmiModule { linker.instantiate(store.as_context_mut(), &module) } - pub fn instantiate(&self) -> Result { - let mut store = Store::new(self.module.engine(), WasmiInstanceEnv::new()); - - let instance = Self::host_funcs_set(&self.module, &mut store) - .map_err(WasmiInstantiationError::PreInstantiationError)? - .ensure_no_start(store.as_context_mut()) - .map_err(WasmiInstantiationError::InstantiationError)?; - + fn instantiate(&self) -> WasmiInstance { + let instance = self.template_instance.clone(); + let mut store = self.template_store.clone(); let memory = match instance.get_export(store.as_context_mut(), EXPORT_MEMORY) { Some(Extern::Memory(memory)) => memory, _ => panic!("Failed to find memory export"), }; - Ok(WasmiInstance { + WasmiInstance { instance, - store, + store: unsafe { transmute(store) }, memory, - }) - } - - fn instantiate_unchecked(&self) -> WasmiInstance { - self.instantiate().expect("Failed to instantiate") + } } } @@ -1923,10 +1943,15 @@ impl HostError for InvokeError {} impl From for InvokeError { fn from(err: Error) -> Self { let e_str = format!("{:?}", err); - if let Some(invoke_err) = err.downcast::>() { - invoke_err.clone() - } else { - InvokeError::SelfError(WasmRuntimeError::ExecutionError(e_str)) + match err { + Error::Trap(trap) => { + if let Some(invoke_err) = trap.downcast_ref::>() { + invoke_err.clone() + } else { + InvokeError::SelfError(WasmRuntimeError::ExecutionError(e_str)) + } + } + _ => InvokeError::SelfError(WasmRuntimeError::ExecutionError(e_str)), } } } @@ -1950,11 +1975,11 @@ impl WasmInstance for WasmiInstance { } let func = self.get_export_func(func_name).unwrap(); - let input: Vec = args + let input: Vec = args .into_iter() - .map(|buffer| Val::I64(buffer.as_i64())) + .map(|buffer| Value::I64(buffer.as_i64())) .collect(); - let mut ret = [Val::I64(0)]; + let mut ret = [Value::I64(0)]; let call_result = func .call(self.store.as_context_mut(), &input, &mut ret) @@ -1964,8 +1989,8 @@ impl WasmInstance for WasmiInstance { }); let result = match call_result { - Ok(_) => match ret[0] { - Val::I64(ret) => read_slice( + Ok(_) => match i64::try_from(ret[0]) { + Ok(ret) => read_slice( self.store.as_context_mut(), self.memory, Slice::transmute_i64(ret), @@ -1982,18 +2007,15 @@ impl WasmInstance for WasmiInstance { String::from_utf8(runtime.buffer_consume(blueprint_buffer.id()).unwrap()) .unwrap(); - let mut ret = [Val::I64(0)]; + let mut ret = [Value::I64(0)]; dump_coverage .call(self.store.as_context_mut(), &[], &mut ret) .unwrap(); - let coverage_data = match ret[0] { - Val::I64(ret) => read_slice( - self.store.as_context_mut(), - self.memory, - Slice::transmute_i64(ret), - ), - _ => Err(InvokeError::SelfError(WasmRuntimeError::InvalidWasmPointer)), - } + let coverage_data = read_slice( + self.store.as_context_mut(), + self.memory, + Slice::transmute_i64(i64::try_from(ret[0]).unwrap()), + ) .unwrap(); save_coverage_data(&blueprint_name, &coverage_data); } @@ -2058,17 +2080,17 @@ impl WasmEngine for WasmiEngine { #[cfg(not(feature = "moka"))] { if let Some(cached_module) = self.modules_cache.borrow_mut().get(&code_hash) { - return cached_module.instantiate_unchecked(); + return cached_module.instantiate(); } } #[cfg(feature = "moka")] if let Some(cached_module) = self.modules_cache.get(&code_hash) { - return cached_module.as_ref().instantiate_unchecked(); + return cached_module.as_ref().instantiate(); } } - let module = WasmiModule::new(instrumented_code).expect("Failed to compile module"); - let instance = module.instantiate_unchecked(); + let module = WasmiModule::new(instrumented_code).expect("Failed to instantiate module"); + let instance = module.instantiate(); #[cfg(not(feature = "fuzzing"))] { @@ -2143,14 +2165,17 @@ mod tests { ) } pub fn run_module_with_mutable_global( - module: &Module, + engine: &Engine, mut store: StoreContextMut, + code: &[u8], func_name: &str, global_name: &str, global_value: &Global, step: i32, ) { - let mut linker = >::new(module.engine()); + let module = Module::new(&engine, code).unwrap(); + + let mut linker = >::new(); linker_define!(linker, global_name, *global_value); let instance = linker @@ -2164,8 +2189,8 @@ mod tests { .and_then(Extern::into_func) .unwrap(); - let input = [Val::I32(step)]; - let mut ret = [Val::I32(0)]; + let input = [Value::I32(step)]; + let mut ret = [Value::I32(0)]; let _ = func.call(store.as_context_mut(), &input, &mut ret); } @@ -2175,17 +2200,16 @@ mod tests { // wat2wasm has "mutable-globals" enabled by default let code = wat2wasm(MODULE_MUTABLE_GLOBALS).unwrap(); - let wasmi_module = WasmiModule::new(&code).unwrap(); - let module = wasmi_module.module; - - let mut store = Store::new(&module.engine(), WasmiInstanceEnv::new()); + let engine = Engine::default(); + let mut store = Store::new(&engine, WasmiInstanceEnv::new()); // Value of this Global shall be updated by the below WASM module calls - let global_value = Global::new(store.as_context_mut(), Val::I32(100), Mutability::Var); + let global_value = Global::new(store.as_context_mut(), Value::I32(100), Mutability::Var); run_module_with_mutable_global( - &module, + &engine, store.as_context_mut(), + &code, "increase_global_value", "global_mutable_value", &global_value, @@ -2193,24 +2217,22 @@ mod tests { ); let updated_value = global_value.get(store.as_context()); let val = match updated_value { - Val::I32(val) => val, + Value::I32(val) => val, _ => panic!("Unexpected return value type"), }; assert_eq!(val, 1100); run_module_with_mutable_global( - &module, + &engine, store.as_context_mut(), + &code, "increase_global_value", "global_mutable_value", &global_value, 10000, ); let updated_value = global_value.get(store.as_context()); - let val = match updated_value { - Val::I32(val) => val, - _ => panic!("Unexpected return value type"), - }; + let val = i32::try_from(updated_value).unwrap(); assert_eq!(val, 11100); } }