diff --git a/Cargo.lock b/Cargo.lock index 2e6d1f73872..f721de284e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1396,7 +1396,7 @@ dependencies = [ "async-lock 2.8.0", "async-task", "concurrent-queue", - "fastrand 2.0.1", + "fastrand 2.1.1", "futures-lite 1.13.0", "slab", ] @@ -1445,7 +1445,7 @@ dependencies = [ "futures-lite 2.3.0", "parking", "polling 3.5.0", - "rustix 0.38.31", + "rustix 0.38.37", "slab", "tracing", "windows-sys 0.52.0", @@ -1497,7 +1497,7 @@ dependencies = [ "cfg-if", "event-listener 5.2.0", "futures-lite 2.3.0", - "rustix 0.38.31", + "rustix 0.38.37", "tracing", "windows-sys 0.52.0", ] @@ -1525,7 +1525,7 @@ dependencies = [ "cfg-if", "futures-core", "futures-io", - "rustix 0.38.31", + "rustix 0.38.37", "signal-hook-registry", "slab", "windows-sys 0.52.0", @@ -2002,7 +2002,7 @@ dependencies = [ "async-channel 1.9.0", "async-lock 2.8.0", "async-task", - "fastrand 2.0.1", + "fastrand 2.1.1", "futures-io", "futures-lite 1.13.0", "piper", @@ -2408,7 +2408,7 @@ dependencies = [ "num-traits", "serde", "wasm-bindgen", - "windows-targets 0.52.0", + "windows-targets 0.52.6", ] [[package]] @@ -2882,43 +2882,13 @@ dependencies = [ "libc", ] -[[package]] -name = "cranelift-bforest" -version = "0.91.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a2ab4512dfd3a6f4be184403a195f76e81a8a9f9e6c898e19d2dc3ce20e0115" -dependencies = [ - "cranelift-entity 0.91.1", -] - [[package]] name = "cranelift-bforest" version = "0.95.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1277fbfa94bc82c8ec4af2ded3e639d49ca5f7f3c7eeab2c66accd135ece4e70" dependencies = [ - "cranelift-entity 0.95.1", -] - -[[package]] -name = "cranelift-codegen" -version = "0.91.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98b022ed2a5913a38839dfbafe6cf135342661293b08049843362df4301261dc" -dependencies = [ - "arrayvec 0.7.4", - "bumpalo", - "cranelift-bforest 0.91.1", - "cranelift-codegen-meta 0.91.1", - "cranelift-codegen-shared 0.91.1", - "cranelift-egraph", - "cranelift-entity 0.91.1", - "cranelift-isle 0.91.1", - "gimli 0.26.2", - "log", - "regalloc2 0.5.1", - "smallvec", - "target-lexicon", + "cranelift-entity", ] [[package]] @@ -2928,69 +2898,34 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c6e8c31ad3b2270e9aeec38723888fe1b0ace3bea2b06b3f749ccf46661d3220" dependencies = [ "bumpalo", - "cranelift-bforest 0.95.1", - "cranelift-codegen-meta 0.95.1", - "cranelift-codegen-shared 0.95.1", - "cranelift-entity 0.95.1", - "cranelift-isle 0.95.1", + "cranelift-bforest", + "cranelift-codegen-meta", + "cranelift-codegen-shared", + "cranelift-entity", + "cranelift-isle", "gimli 0.27.3", "hashbrown 0.13.2", "log", - "regalloc2 0.6.1", + "regalloc2", "smallvec", "target-lexicon", ] -[[package]] -name = "cranelift-codegen-meta" -version = "0.91.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "639307b45434ad112a98f8300c0f0ab085cbefcd767efcdef9ef19d4c0756e74" -dependencies = [ - "cranelift-codegen-shared 0.91.1", -] - [[package]] name = "cranelift-codegen-meta" version = "0.95.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c8ac5ac30d62b2d66f12651f6b606dbdfd9c2cfd0908de6b387560a277c5c9da" dependencies = [ - "cranelift-codegen-shared 0.95.1", + "cranelift-codegen-shared", ] -[[package]] -name = "cranelift-codegen-shared" -version = "0.91.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "278e52e29c53fcf32431ef08406c295699a70306d05a0715c5b1bf50e33a9ab7" - [[package]] name = "cranelift-codegen-shared" version = "0.95.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd82b8b376247834b59ed9bdc0ddeb50f517452827d4a11bccf5937b213748b8" -[[package]] -name = "cranelift-egraph" -version = "0.91.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624b54323b06e675293939311943ba82d323bb340468ce1889be5da7932c8d73" -dependencies = [ - "cranelift-entity 0.91.1", - "fxhash", - "hashbrown 0.12.3", - "indexmap 1.9.3", - "log", - "smallvec", -] - -[[package]] -name = "cranelift-entity" -version = "0.91.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a59bcbca89c3f1b70b93ab3cbba5e5e0cbf3e63dadb23c7525cb142e21a9d4c" - [[package]] name = "cranelift-entity" version = "0.95.1" @@ -3000,36 +2935,18 @@ dependencies = [ "serde", ] -[[package]] -name = "cranelift-frontend" -version = "0.91.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d70abacb8cfef3dc8ff7e8836e9c1d70f7967dfdac824a4cd5e30223415aca6" -dependencies = [ - "cranelift-codegen 0.91.1", - "log", - "smallvec", - "target-lexicon", -] - [[package]] name = "cranelift-frontend" version = "0.95.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64a25d9d0a0ae3079c463c34115ec59507b4707175454f0eee0891e83e30e82d" dependencies = [ - "cranelift-codegen 0.95.1", + "cranelift-codegen", "log", "smallvec", "target-lexicon", ] -[[package]] -name = "cranelift-isle" -version = "0.91.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "393bc73c451830ff8dbb3a07f61843d6cb41a084f9996319917c0b291ed785bb" - [[package]] name = "cranelift-isle" version = "0.95.1" @@ -3042,7 +2959,7 @@ version = "0.95.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb6b03e0e03801c4b3fd8ce0758a94750c07a44e7944cc0ffbf0d3f2e7c79b00" dependencies = [ - "cranelift-codegen 0.95.1", + "cranelift-codegen", "libc", "target-lexicon", ] @@ -3053,9 +2970,9 @@ version = "0.95.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff3220489a3d928ad91e59dd7aeaa8b3de18afb554a6211213673a71c90737ac" dependencies = [ - "cranelift-codegen 0.95.1", - "cranelift-entity 0.95.1", - "cranelift-frontend 0.95.1", + "cranelift-codegen", + "cranelift-entity", + "cranelift-frontend", "itertools 0.10.5", "log", "smallvec", @@ -5504,9 +5421,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.1" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" [[package]] name = "fastrlp" @@ -6151,7 +6068,7 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5" dependencies = [ - "fastrand 2.0.1", + "fastrand 2.1.1", "futures-core", "futures-io", "parking", @@ -6662,6 +6579,7 @@ dependencies = [ "proptest", "region", "sp-wasm-interface-common", + "wasmer-vm", "winapi", ] @@ -6878,12 +6796,16 @@ dependencies = [ "assert_matches", "gear-sandbox-env", "gear-sandbox-interface", + "gear-wasmer-cache", "log", "parity-scale-codec", "sp-core", "sp-std 8.0.0", "sp-wasm-interface-common", - "wasmi 0.30.0", + "wasmer", + "wasmer-compiler", + "wasmer-types", + "wasmer-vm", "wat", ] @@ -6904,15 +6826,14 @@ dependencies = [ "defer", "environmental", "gear-sandbox-env", + "gear-wasmer-cache", "log", "parity-scale-codec", "sp-allocator", "sp-wasm-interface-common", "tempfile", "thiserror", - "uluru", "wasmer", - "wasmer-cache", "wasmer-types", "wasmi 0.13.2", ] @@ -7068,6 +6989,7 @@ version = "1.6.2" dependencies = [ "anyhow", "cargo_metadata 0.18.1", + "cargo_toml", "chrono", "gear-core", "gear-pwasm-utils", @@ -7141,6 +7063,16 @@ dependencies = [ "which", ] +[[package]] +name = "gear-wasmer-cache" +version = "1.6.2" +dependencies = [ + "log", + "uluru", + "wasmer", + "wasmer-cache", +] + [[package]] name = "gear-weight-diff" version = "1.0.0" @@ -8344,12 +8276,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "intx" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f38a50a899dc47a6d0ed5508e7f601a2e34c3a85303514b5d137f3c10a0c75" - [[package]] name = "io-lifetimes" version = "1.0.11" @@ -8392,7 +8318,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ "hermit-abi 0.3.2", - "rustix 0.38.31", + "rustix 0.38.37", "windows-sys 0.48.0", ] @@ -10103,9 +10029,9 @@ checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" [[package]] name = "linux-raw-sys" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "lioness" @@ -12782,7 +12708,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" dependencies = [ "atomic-waker", - "fastrand 2.0.1", + "fastrand 2.1.1", "futures-io", ] @@ -12864,7 +12790,7 @@ dependencies = [ "cfg-if", "concurrent-queue", "pin-project-lite 0.2.13", - "rustix 0.38.31", + "rustix 0.38.37", "tracing", "windows-sys 0.52.0", ] @@ -13636,18 +13562,6 @@ dependencies = [ "syn 2.0.71", ] -[[package]] -name = "regalloc2" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "300d4fbfb40c1c66a78ba3ddd41c1110247cf52f97b87d0f2fc9209bd49b030c" -dependencies = [ - "fxhash", - "log", - "slice-group-by", - "smallvec", -] - [[package]] name = "regalloc2" version = "0.6.1" @@ -14167,14 +14081,14 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.31" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags 2.5.0", "errno", "libc", - "linux-raw-sys 0.4.13", + "linux-raw-sys 0.4.14", "windows-sys 0.52.0", ] @@ -17927,14 +17841,15 @@ checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" [[package]] name = "tempfile" -version = "3.10.1" +version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" dependencies = [ "cfg-if", - "fastrand 2.0.1", - "rustix 0.38.31", - "windows-sys 0.52.0", + "fastrand 2.1.1", + "once_cell", + "rustix 0.38.37", + "windows-sys 0.59.0", ] [[package]] @@ -17963,7 +17878,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" dependencies = [ - "rustix 0.38.31", + "rustix 0.38.37", "windows-sys 0.48.0", ] @@ -19363,12 +19278,10 @@ dependencies = [ "tracing", "wasm-bindgen", "wasmer-compiler", - "wasmer-compiler-cranelift", "wasmer-compiler-singlepass", "wasmer-derive", "wasmer-types", "wasmer-vm", - "wat", "winapi", ] @@ -19413,25 +19326,6 @@ dependencies = [ "xxhash-rust", ] -[[package]] -name = "wasmer-compiler-cranelift" -version = "4.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f3352014573750327646a690d32774312b0e8b7920e7e8ba00c0449eac18390" -dependencies = [ - "cranelift-codegen 0.91.1", - "cranelift-entity 0.91.1", - "cranelift-frontend 0.91.1", - "gimli 0.26.2", - "more-asserts", - "rayon", - "smallvec", - "target-lexicon", - "tracing", - "wasmer-compiler", - "wasmer-types", -] - [[package]] name = "wasmer-compiler-singlepass" version = "4.3.5" @@ -19544,19 +19438,6 @@ dependencies = [ "wasmi_core 0.2.1", ] -[[package]] -name = "wasmi" -version = "0.30.0" -source = "git+https://github.com/gear-tech/wasmi?branch=gear-v0.30.0#c8b0be9c2012e0478959a59074fd953a942782bc" -dependencies = [ - "intx", - "smallvec", - "spin 0.9.8", - "wasmi_arena 0.4.0", - "wasmi_core 0.12.0", - "wasmparser-nostd", -] - [[package]] name = "wasmi" version = "0.31.2" @@ -19565,7 +19446,7 @@ checksum = "77a8281d1d660cdf54c76a3efa9ddd0c270cada1383a995db3ccb43d166456c7" dependencies = [ "smallvec", "spin 0.9.8", - "wasmi_arena 0.4.1", + "wasmi_arena", "wasmi_core 0.13.0", "wasmparser-nostd", ] @@ -19578,11 +19459,6 @@ dependencies = [ "parity-wasm", ] -[[package]] -name = "wasmi_arena" -version = "0.4.0" -source = "git+https://github.com/gear-tech/wasmi?branch=gear-v0.30.0#c8b0be9c2012e0478959a59074fd953a942782bc" - [[package]] name = "wasmi_arena" version = "0.4.1" @@ -19602,18 +19478,6 @@ dependencies = [ "region", ] -[[package]] -name = "wasmi_core" -version = "0.12.0" -source = "git+https://github.com/gear-tech/wasmi?branch=gear-v0.30.0#c8b0be9c2012e0478959a59074fd953a942782bc" -dependencies = [ - "downcast-rs", - "libm", - "num-traits", - "paste", - "region", -] - [[package]] name = "wasmi_core" version = "0.13.0" @@ -19774,9 +19638,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1cefde0cce8cb700b1b21b6298a3837dba46521affd7b8c38a9ee2c869eee04" dependencies = [ "anyhow", - "cranelift-codegen 0.95.1", - "cranelift-entity 0.95.1", - "cranelift-frontend 0.95.1", + "cranelift-codegen", + "cranelift-entity", + "cranelift-frontend", "cranelift-native", "cranelift-wasm", "gimli 0.27.3", @@ -19796,7 +19660,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd041e382ef5aea1b9fc78442394f1a4f6d676ce457e7076ca4cb3f397882f8b" dependencies = [ "anyhow", - "cranelift-codegen 0.95.1", + "cranelift-codegen", "cranelift-native", "gimli 0.27.3", "object 0.30.4", @@ -19811,7 +19675,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a990198cee4197423045235bf89d3359e69bd2ea031005f4c2d901125955c949" dependencies = [ "anyhow", - "cranelift-entity 0.95.1", + "cranelift-entity", "gimli 0.27.3", "indexmap 1.9.3", "log", @@ -19914,7 +19778,7 @@ version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4f6fffd2a1011887d57f07654dd112791e872e3ff4a2e626aee8059ee17f06f" dependencies = [ - "cranelift-entity 0.95.1", + "cranelift-entity", "serde", "thiserror", "wasmparser 0.102.0", @@ -20053,7 +19917,7 @@ dependencies = [ "either", "home", "once_cell", - "rustix 0.38.31", + "rustix 0.38.37", ] [[package]] @@ -20179,7 +20043,16 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", ] [[package]] @@ -20214,17 +20087,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.0", - "windows_aarch64_msvc 0.52.0", - "windows_i686_gnu 0.52.0", - "windows_i686_msvc 0.52.0", - "windows_x86_64_gnu 0.52.0", - "windows_x86_64_gnullvm 0.52.0", - "windows_x86_64_msvc 0.52.0", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -20241,9 +20115,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -20265,9 +20139,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -20289,9 +20163,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.0" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -20313,9 +20193,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -20337,9 +20217,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -20355,9 +20235,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -20379,9 +20259,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" @@ -20529,8 +20409,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8da84f1a25939b27f6820d92aed108f83ff920fdf11a7b19366c27c4cda81d4f" dependencies = [ "libc", - "linux-raw-sys 0.4.13", - "rustix 0.38.31", + "linux-raw-sys 0.4.14", + "rustix 0.38.37", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 1fa1b5efec3..da0cb93b1f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -178,9 +178,12 @@ url = "2.5.2" # wasmer 4.3.4 for some reason have wat's version "=1.0.71" nailed down, so we have to do the same wat = "1.0.71" wabt = "0.10.0" -wasmer = "4.3.4" +wasmer = { version = "4.3.4", default-features = false, features = ["singlepass"] } wasmer-cache = "4.3.4" wasmer-types = "4.3.4" +wasmer-vm = "4.3.4" +wasmer-compiler = "4.3.4" +gear-wasmer-cache = { path = "utils/gear-wasmer-cache" } wasmtime = "8.0.1" wasmparser = { package = "wasmparser-nostd", version = "0.100.1", default-features = false } which = "4.4.2" diff --git a/core-backend/src/env.rs b/core-backend/src/env.rs index b9520b8c5c6..72cc7342fe7 100644 --- a/core-backend/src/env.rs +++ b/core-backend/src/env.rs @@ -29,7 +29,7 @@ use crate::{ BackendExternalities, }; use alloc::{collections::BTreeSet, format, string::String}; -use core::{any::Any, fmt::Debug}; +use core::{any::Any, fmt::Debug, marker::Send}; use gear_core::{ env::Externalities, gas::GasAmount, @@ -60,7 +60,7 @@ macro_rules! wrap_syscall { }; } -fn store_host_state_mut( +fn store_host_state_mut( store: &mut Store>>, ) -> &mut State> { store.data_mut().as_mut().unwrap_or_else(|| { @@ -123,7 +123,7 @@ struct EnvBuilder { impl EnvBuilder where - Ext: BackendExternalities + 'static, + Ext: BackendExternalities + Send + 'static, Ext::UnrecoverableError: BackendSyscallError, RunFallibleError: From, Ext::AllocError: BackendAllocSyscallError, @@ -159,7 +159,7 @@ impl From> impl Environment where - Ext: BackendExternalities + 'static, + Ext: BackendExternalities + Send + 'static, Ext::UnrecoverableError: BackendSyscallError, RunFallibleError: From, Ext::AllocError: BackendAllocSyscallError, @@ -232,9 +232,9 @@ struct GlobalsAccessProvider { store: Option>>>, } -impl GlobalsAccessor for GlobalsAccessProvider { - fn get_i64(&self, name: &LimitedStr) -> Result { - let store = self.store.as_ref().ok_or(GlobalsAccessError)?; +impl GlobalsAccessor for GlobalsAccessProvider { + fn get_i64(&mut self, name: &LimitedStr) -> Result { + let store = self.store.as_mut().ok_or(GlobalsAccessError)?; self.instance .get_global_val(store, name.as_str()) .and_then(i64::try_from_value) @@ -255,7 +255,7 @@ impl GlobalsAccessor for GlobalsAccessProvider Environment where - EnvExt: BackendExternalities + 'static, + EnvExt: BackendExternalities + Send + 'static, EnvExt::UnrecoverableError: BackendSyscallError, RunFallibleError: From, EnvExt::AllocError: BackendAllocSyscallError, @@ -440,7 +440,7 @@ where // Fetching global value. let gas = instance - .get_global_val(&store, GLOBAL_NAME_GAS) + .get_global_val(&mut store, GLOBAL_NAME_GAS) .and_then(i64::try_from_value) .ok_or(System(WrongInjectedGas))? as u64; diff --git a/core-backend/src/mock.rs b/core-backend/src/mock.rs index c443dd1b19e..efd6b401e7c 100644 --- a/core-backend/src/mock.rs +++ b/core-backend/src/mock.rs @@ -22,18 +22,18 @@ use crate::{ }, BackendExternalities, }; -use alloc::{collections::BTreeSet, rc::Rc, vec, vec::Vec}; +use alloc::{collections::BTreeSet, vec::Vec}; use codec::{Decode, Encode}; -use core::{cell::RefCell, fmt, fmt::Debug, mem}; +use core::{fmt, fmt::Debug, mem}; use gear_core::{ costs::CostToken, env::{Externalities, PayloadSliceLock, UnlockPayloadBound}, env_vars::{EnvVars, EnvVarsV1}, gas::{ChargeError, CounterType, CountersOwner, GasAmount, GasCounter, GasLeft}, ids::{MessageId, ProgramId, ReservationId}, - memory::{HostPointer, Memory, MemoryError, MemoryInterval}, + memory::{Memory, MemoryInterval}, message::{HandlePacket, InitPacket, ReplyPacket}, - pages::{WasmPage, WasmPagesAmount}, + pages::WasmPage, }; use gear_core_errors::{ReplyCode, SignalCode}; use gear_lazy_pages_common::ProcessAccessError; @@ -310,97 +310,113 @@ impl BackendExternalities for MockExt { } } -#[derive(Debug)] -struct InnerMockMemory { - pages: Vec, - read_attempt_count: u32, - write_attempt_count: u32, -} +#[cfg(feature = "std")] +pub use with_std_feature::*; -impl InnerMockMemory { - fn grow(&mut self, pages: WasmPagesAmount) -> u32 { - let size = self.pages.len() as u32; - let new_size = size + pages.offset() as u32; - self.pages.resize(new_size as usize, 0); +#[cfg(feature = "std")] +mod with_std_feature { + use gear_core::{ + memory::{HostPointer, Memory, MemoryError}, + pages::{WasmPage, WasmPagesAmount}, + }; + use std::sync::{Arc, Mutex, MutexGuard}; - size / WasmPage::SIZE + #[derive(Debug)] + struct InnerMockMemory { + pages: Vec, + read_attempt_count: u32, + write_attempt_count: u32, } - fn write(&mut self, offset: u32, buffer: &[u8]) -> Result<(), MemoryError> { - self.write_attempt_count += 1; + impl InnerMockMemory { + fn grow(&mut self, pages: WasmPagesAmount) -> u32 { + let size = self.pages.len() as u32; + let new_size = size + pages.offset() as u32; + self.pages.resize(new_size as usize, 0); - let offset = offset as usize; - if offset + buffer.len() > self.pages.len() { - return Err(MemoryError::AccessOutOfBounds); + size / WasmPage::SIZE } - self.pages[offset..offset + buffer.len()].copy_from_slice(buffer); + fn write(&mut self, offset: u32, buffer: &[u8]) -> Result<(), MemoryError> { + self.write_attempt_count += 1; - Ok(()) - } + let offset = offset as usize; + if offset + buffer.len() > self.pages.len() { + return Err(MemoryError::AccessOutOfBounds); + } - fn read(&mut self, offset: u32, buffer: &mut [u8]) -> Result<(), MemoryError> { - self.read_attempt_count += 1; + self.pages[offset..offset + buffer.len()].copy_from_slice(buffer); - let offset = offset as usize; - if offset + buffer.len() > self.pages.len() { - return Err(MemoryError::AccessOutOfBounds); + Ok(()) } - buffer.copy_from_slice(&self.pages[offset..(offset + buffer.len())]); + fn read(&mut self, offset: u32, buffer: &mut [u8]) -> Result<(), MemoryError> { + self.read_attempt_count += 1; - Ok(()) - } + let offset = offset as usize; + if offset + buffer.len() > self.pages.len() { + return Err(MemoryError::AccessOutOfBounds); + } + + buffer.copy_from_slice(&self.pages[offset..(offset + buffer.len())]); + + Ok(()) + } - fn size(&self) -> WasmPagesAmount { - WasmPage::from_offset(self.pages.len() as u32).into() + fn size(&self) -> WasmPagesAmount { + WasmPage::from_offset(self.pages.len() as u32).into() + } } -} -#[derive(Debug, Clone)] -pub struct MockMemory(Rc>); + #[derive(Debug, Clone)] + pub struct MockMemory(Arc>); -impl MockMemory { - pub fn new(initial_pages: u32) -> Self { - let pages = vec![0; initial_pages as usize * WasmPage::SIZE as usize]; + impl MockMemory { + pub fn new(initial_pages: u32) -> Self { + let pages = vec![0; initial_pages as usize * WasmPage::SIZE as usize]; - Self(Rc::new(RefCell::new(InnerMockMemory { - pages, - read_attempt_count: 0, - write_attempt_count: 0, - }))) - } + Self(Arc::new(Mutex::new(InnerMockMemory { + pages, + read_attempt_count: 0, + write_attempt_count: 0, + }))) + } - pub fn read_attempt_count(&self) -> u32 { - self.0.borrow().read_attempt_count - } + fn lock(&self) -> MutexGuard<'_, InnerMockMemory> { + self.0.lock().unwrap() + } - pub fn write_attempt_count(&self) -> u32 { - self.0.borrow().write_attempt_count + pub fn read_attempt_count(&self) -> u32 { + self.lock().read_attempt_count + } + + pub fn write_attempt_count(&self) -> u32 { + self.lock().write_attempt_count + } } -} -impl Memory for MockMemory { - type GrowError = &'static str; + impl Memory for MockMemory { + type GrowError = &'static str; - fn grow(&self, _ctx: &mut Context, pages: WasmPagesAmount) -> Result<(), Self::GrowError> { - let _ = self.0.borrow_mut().grow(pages); - Ok(()) - } + fn grow(&self, _ctx: &mut Context, pages: WasmPagesAmount) -> Result<(), Self::GrowError> { + let _ = self.lock().grow(pages); + Ok(()) + } - fn size(&self, _ctx: &Context) -> WasmPagesAmount { - self.0.borrow_mut().size() - } + fn size(&self, _ctx: &Context) -> WasmPagesAmount { + self.lock().size() + } - fn write(&self, _ctx: &mut Context, offset: u32, buffer: &[u8]) -> Result<(), MemoryError> { - self.0.borrow_mut().write(offset, buffer) - } + fn write(&self, _ctx: &mut Context, offset: u32, buffer: &[u8]) -> Result<(), MemoryError> { + self.lock().write(offset, buffer) + } - fn read(&self, _ctx: &Context, offset: u32, buffer: &mut [u8]) -> Result<(), MemoryError> { - self.0.borrow_mut().read(offset, buffer) - } + fn read(&self, _ctx: &Context, offset: u32, buffer: &mut [u8]) -> Result<(), MemoryError> { + self.lock().read(offset, buffer) + } - unsafe fn get_buffer_host_addr_unsafe(&self, _ctx: &Context) -> HostPointer { - unimplemented!() + unsafe fn get_buffer_host_addr_unsafe(&self, _ctx: &Context) -> HostPointer { + unimplemented!() + } } } diff --git a/core-processor/src/executor.rs b/core-processor/src/executor.rs index 3d548321087..4b0e6e0ba72 100644 --- a/core-processor/src/executor.rs +++ b/core-processor/src/executor.rs @@ -57,7 +57,7 @@ pub(crate) fn execute_wasm( msg_ctx_settings: ContextSettings, ) -> Result where - Ext: ProcessorExternalities + BackendExternalities + 'static, + Ext: ProcessorExternalities + BackendExternalities + Send + 'static, ::AllocError: BackendAllocSyscallError, RunFallibleError: From, @@ -266,7 +266,7 @@ pub fn execute_for_reply( block_info: BlockInfo, ) -> Result, String> where - Ext: ProcessorExternalities + BackendExternalities + 'static, + Ext: ProcessorExternalities + BackendExternalities + Send + 'static, ::AllocError: BackendAllocSyscallError, RunFallibleError: From, diff --git a/core-processor/src/processing.rs b/core-processor/src/processing.rs index 9e91990bbdd..7011082f2d6 100644 --- a/core-processor/src/processing.rs +++ b/core-processor/src/processing.rs @@ -51,7 +51,7 @@ pub fn process( random_data: (Vec, u32), ) -> Result, SystemExecutionError> where - Ext: ProcessorExternalities + BackendExternalities + 'static, + Ext: ProcessorExternalities + BackendExternalities + Send + 'static, ::AllocError: BackendAllocSyscallError, RunFallibleError: From, diff --git a/core/src/memory.rs b/core/src/memory.rs index c057bbf7b58..bfa191a9c80 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -91,10 +91,10 @@ impl From for (u32, u32) { impl Debug for MemoryInterval { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&format!( - "[offset: {:#x}, size: {:#x}]", - self.offset, self.size - )) + f.debug_struct("MemoryInterval") + .field("offset", &format_args!("{:#x}", self.offset)) + .field("size", &format_args!("{:#x}", self.size)) + .finish() } } diff --git a/ethexe/network/src/custom_connection_limits.rs b/ethexe/network/src/custom_connection_limits.rs index 60d45545247..041b31308f7 100644 --- a/ethexe/network/src/custom_connection_limits.rs +++ b/ethexe/network/src/custom_connection_limits.rs @@ -323,7 +323,7 @@ mod tests { assert_eq!( map.inner.into_keys().collect::>(), - Default::default() + HashSet::default() ); } diff --git a/ethexe/processor/src/host/mod.rs b/ethexe/processor/src/host/mod.rs index d28d28820c6..822b7b9498e 100644 --- a/ethexe/processor/src/host/mod.rs +++ b/ethexe/processor/src/host/mod.rs @@ -54,8 +54,6 @@ pub(crate) struct InstanceCreator { impl InstanceCreator { pub fn new(runtime: Vec) -> Result { - gear_runtime_interface::sandbox_init(); - let engine = wasmtime::Engine::default(); let module = wasmtime::Module::new(&engine, runtime)?; diff --git a/ethexe/runtime/common/src/lib.rs b/ethexe/runtime/common/src/lib.rs index 933eb4f6b7a..4583577fbd7 100644 --- a/ethexe/runtime/common/src/lib.rs +++ b/ethexe/runtime/common/src/lib.rs @@ -105,13 +105,18 @@ pub(crate) fn update_state_with_storage( new_state_hash } -pub fn process_next_message>( +pub fn process_next_message( program_id: ProgramId, program_state: ProgramState, instrumented_code: Option, code_id: CodeId, ri: &RI, -) -> Vec { +) -> Vec +where + S: Storage, + RI: RuntimeInterface, + >::LazyPages: Send, +{ let block_info = ri.block_info(); log::trace!("Processing next message for program {program_id}"); diff --git a/gcli/Cargo.toml b/gcli/Cargo.toml index 5c37586644f..fed73c7699e 100644 --- a/gcli/Cargo.toml +++ b/gcli/Cargo.toml @@ -40,7 +40,7 @@ etc.workspace = true runtime-primitives.workspace = true url = { workspace = true, features = ["serde"] } toml.workspace = true -wasmer = { workspace = true, features = ["std"] } +wasmer.workspace = true wasmer-types.workspace = true [dev-dependencies] diff --git a/lazy-pages/Cargo.toml b/lazy-pages/Cargo.toml index d0bb3f5f295..24e04286ff5 100644 --- a/lazy-pages/Cargo.toml +++ b/lazy-pages/Cargo.toml @@ -17,6 +17,7 @@ cfg-if.workspace = true region.workspace = true derive_more.workspace = true numerated.workspace = true +wasmer-vm.workspace = true gear-sandbox-host.workspace = true gear-core.workspace = true diff --git a/lazy-pages/common/src/lib.rs b/lazy-pages/common/src/lib.rs index 7ac1eecdfd6..da57f7d1783 100644 --- a/lazy-pages/common/src/lib.rs +++ b/lazy-pages/common/src/lib.rs @@ -74,7 +74,7 @@ pub struct GlobalsAccessError; /// Globals access trait. pub trait GlobalsAccessor { /// Returns global `name` value, if `name` is I64 global export. - fn get_i64(&self, name: &LimitedStr) -> Result; + fn get_i64(&mut self, name: &LimitedStr) -> Result; /// Set global `name` == `value`, if `name` is I64 global export. fn set_i64(&mut self, name: &LimitedStr, value: i64) -> Result<(), GlobalsAccessError>; diff --git a/lazy-pages/src/globals.rs b/lazy-pages/src/globals.rs index 8e56342bd22..89dd33b617d 100644 --- a/lazy-pages/src/globals.rs +++ b/lazy-pages/src/globals.rs @@ -45,7 +45,7 @@ struct GlobalsAccessWasmRuntime<'a> { } impl<'a> GlobalsAccessor for GlobalsAccessWasmRuntime<'a> { - fn get_i64(&self, name: &LimitedStr) -> Result { + fn get_i64(&mut self, name: &LimitedStr) -> Result { // SAFETY: this is safe because this method is called only from signal handler context unsafe { self.instance @@ -81,7 +81,7 @@ struct GlobalsAccessNativeRuntime<'a, 'b> { } impl<'a, 'b> GlobalsAccessor for GlobalsAccessNativeRuntime<'a, 'b> { - fn get_i64(&self, name: &LimitedStr) -> Result { + fn get_i64(&mut self, name: &LimitedStr) -> Result { self.inner_access_provider.get_i64(name) } diff --git a/lazy-pages/src/lib.rs b/lazy-pages/src/lib.rs index 1bef69b7db9..6a724c6cc6a 100644 --- a/lazy-pages/src/lib.rs +++ b/lazy-pages/src/lib.rs @@ -43,7 +43,10 @@ mod sys; #[cfg(test)] mod tests; -pub use crate::common::LazyPagesStorage; +pub use common::{Error as LazyPagesError, LazyPagesStorage, LazyPagesVersion}; +pub use host_func::pre_process_memory_accesses; +pub use signal::{ExceptionInfo, UserSignalHandler}; + use crate::{ common::{ContextError, CostNo, Costs, LazyPagesContext, PagePrefix, PageSizes}, globals::{GlobalNo, GlobalsContext}, @@ -52,16 +55,13 @@ use crate::{ GearPagesAmount, GearSizeNo, PagesAmountTrait, SizeNumber, WasmPage, WasmPagesAmount, WasmSizeNo, SIZES_AMOUNT, }, + signal::DefaultUserSignalHandler, }; -pub use common::{Error as LazyPagesError, LazyPagesVersion}; use common::{LazyPagesExecutionContext, LazyPagesRuntimeContext}; use gear_lazy_pages_common::{GlobalsAccessConfig, LazyPagesInitContext, Status}; -pub use host_func::pre_process_memory_accesses; use mprotect::MprotectError; use numerated::iterators::IntervalIterator; use pages::GearPage; -use signal::DefaultUserSignalHandler; -pub use signal::{ExceptionInfo, UserSignalHandler}; use std::{cell::RefCell, convert::TryInto, num::NonZero}; /// Initialize lazy-pages once for process. @@ -442,6 +442,11 @@ pub fn init_with_handler( }) }); + // TODO: remove after usage of `wasmer::Store::set_trap_handler` for lazy-pages + // we capture executor signal handler first to call it later + // if our handler is not effective + wasmer_vm::init_traps(); + unsafe { init_for_process::()? } unsafe { sys::init_for_thread().map_err(InitForThread)? } diff --git a/node/authorship/src/tests.rs b/node/authorship/src/tests.rs index dd1b69705bb..6f785110f61 100644 --- a/node/authorship/src/tests.rs +++ b/node/authorship/src/tests.rs @@ -329,7 +329,6 @@ type TestCase = Box; #[test] fn run_all_tests() { init_logger(); - gear_runtime_interface::sandbox_init(); use basic_tests::*; diff --git a/node/cli/src/main.rs b/node/cli/src/main.rs index 7830fb5cfbb..93e1c2326e3 100644 --- a/node/cli/src/main.rs +++ b/node/cli/src/main.rs @@ -17,6 +17,5 @@ // along with this program. If not, see . fn main() -> gear_cli::Result<()> { - gear_runtime_interface::sandbox_init(); gear_cli::run() } diff --git a/runtime-interface/sandbox/src/detail.rs b/runtime-interface/sandbox/src/detail.rs index c4f401d7f6b..68ca0fd5792 100644 --- a/runtime-interface/sandbox/src/detail.rs +++ b/runtime-interface/sandbox/src/detail.rs @@ -65,12 +65,6 @@ thread_local! { static SANDBOXES: RefCell = RefCell::new(Sandboxes::new()); } -pub fn init() { - SANDBOXES.with(|sandboxes| { - let _store = sandboxes.borrow_mut().get(0); - }) -} - struct SupervisorContext<'a, 'b> { caller: &'a mut Caller<'b, StoreData>, dispatch_thunk: Func, diff --git a/runtime-interface/sandbox/src/lib.rs b/runtime-interface/sandbox/src/lib.rs index 7d15ef14fcd..cac05370ae2 100644 --- a/runtime-interface/sandbox/src/lib.rs +++ b/runtime-interface/sandbox/src/lib.rs @@ -28,9 +28,6 @@ use sp_wasm_interface::HostPointer; #[cfg(feature = "std")] pub mod detail; -#[cfg(feature = "std")] -pub use detail::init; - /// Wasm-only interface that provides functions for interacting with the sandbox. #[runtime_interface(wasm_only)] pub trait Sandbox { diff --git a/runtime-interface/src/lib.rs b/runtime-interface/src/lib.rs index 4c099d6f685..5ce2809ca84 100644 --- a/runtime-interface/src/lib.rs +++ b/runtime-interface/src/lib.rs @@ -50,7 +50,7 @@ use { pub use gear_sandbox_interface::sandbox; #[cfg(feature = "std")] -pub use gear_sandbox_interface::{detail as sandbox_detail, init as sandbox_init, Instantiate}; +pub use gear_sandbox_interface::{detail as sandbox_detail, Instantiate}; const _: () = assert!(size_of::() >= size_of::()); diff --git a/sandbox/host/Cargo.toml b/sandbox/host/Cargo.toml index 842303c3704..df634883545 100644 --- a/sandbox/host/Cargo.toml +++ b/sandbox/host/Cargo.toml @@ -20,15 +20,15 @@ defer.workspace = true environmental.workspace = true thiserror.workspace = true log = { workspace = true, features = ["std"] } -wasmer = { workspace = true, features = ["singlepass"] } +wasmer.workspace = true wasmer-types.workspace = true sandbox-wasmi.workspace = true sp-allocator = { workspace = true, features = ["std"] } sp-wasm-interface-common = { workspace = true, features = ["std"] } gear-sandbox-env = { workspace = true, features = ["std"] } -wasmer-cache = { workspace = true, optional = true } -tempfile.workspace = true -uluru = { workspace = true, optional = true } +gear-wasmer-cache = { workspace = true, optional = true } +tempfile = { workspace = true, optional = true } [features] -default = ["wasmer-cache", "uluru"] +default = ["wasmer-cache"] +wasmer-cache = ["gear-wasmer-cache", "tempfile"] diff --git a/sandbox/host/src/sandbox/wasmer_backend.rs b/sandbox/host/src/sandbox/wasmer_backend.rs index a4431947832..d94a0c6f0ed 100644 --- a/sandbox/host/src/sandbox/wasmer_backend.rs +++ b/sandbox/host/src/sandbox/wasmer_backend.rs @@ -18,12 +18,11 @@ //! Wasmer specific impls for sandbox -use std::{cell::RefCell, rc::Rc}; - use codec::{Decode, Encode}; use gear_sandbox_env::{HostError, Instantiate, WasmReturnValue, GLOBAL_NAME_GAS}; use sp_wasm_interface_common::{util, Pointer, ReturnValue, Value, WordSize}; -use wasmer::{AsStoreMut, Module, RuntimeError, Store}; +use std::{cell::RefCell, path::PathBuf, rc::Rc}; +use wasmer::{AsStoreMut, RuntimeError, Store}; use wasmer_types::TrapCode; use crate::{ @@ -38,10 +37,8 @@ use crate::{ pub use store_refcell::StoreRefCell; mod store_refcell; -#[cfg(feature = "wasmer-cache")] -mod cache; -#[cfg(feature = "wasmer-cache")] -use cache::*; +#[cfg(feature = "gear-wasmer-cache")] +use gear_wasmer_cache::*; environmental::environmental!(SupervisorContextStore: trait SupervisorContext); @@ -158,6 +155,20 @@ pub fn invoke( } } +#[cfg(feature = "gear-wasmer-cache")] +fn fs_cache() -> PathBuf { + use std::sync::OnceLock; + use tempfile::TempDir; + + static CACHE_DIR: OnceLock = OnceLock::new(); + CACHE_DIR + .get_or_init(|| { + tempfile::tempdir().expect("Cannot create temporary directory for wasmer caches") + }) + .path() + .into() +} + /// Instantiate a module within a sandbox context pub fn instantiate( version: Instantiate, @@ -166,27 +177,11 @@ pub fn instantiate( guest_env: GuestEnvironment, supervisor_context: &mut dyn SupervisorContext, ) -> std::result::Result { - #[cfg(feature = "wasmer-cache")] - let module = match get_cached_module(wasm, &context.store().borrow()) { - Ok(module) => { - log::trace!("Found cached module for current program"); - module - } - Err(CacheMissErr { - fs_cache, - code_hash, - }) => { - log::trace!("Cache for program has not been found, so compile it now"); - let module = Module::new(&context.store().borrow(), wasm) - .map_err(|_| InstantiationError::ModuleDecoding)?; - - try_to_store_module_in_cache(fs_cache, code_hash, wasm, &module); - - module - } - }; + #[cfg(feature = "gear-wasmer-cache")] + let module = get_or_compile_with_cache(wasm, context.store().borrow().engine(), fs_cache) + .map_err(|_| InstantiationError::ModuleDecoding)?; - #[cfg(not(feature = "wasmer-cache"))] + #[cfg(not(feature = "gear-wasmer-cache"))] let module = Module::new(&context.store().borrow(), wasm) .map_err(|_| InstantiationError::ModuleDecoding)?; diff --git a/sandbox/sandbox/Cargo.toml b/sandbox/sandbox/Cargo.toml index 90b345d6fa2..57f5fa61b36 100644 --- a/sandbox/sandbox/Cargo.toml +++ b/sandbox/sandbox/Cargo.toml @@ -17,7 +17,11 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec.workspace = true log.workspace = true -wasmi = { git = "https://github.com/gear-tech/wasmi", branch = "gear-v0.30.0", default-features = false } +wasmer = { workspace = true, optional = true } +wasmer-types = { workspace = true, optional = true } +wasmer-vm = { workspace = true, optional = true } +wasmer-compiler = { workspace = true, optional = true } +gear-wasmer-cache = { workspace = true, optional = true } sp-core.workspace = true sp-std.workspace = true sp-wasm-interface-common.workspace = true @@ -38,7 +42,10 @@ std = [ "sp-wasm-interface-common/std", "gear-sandbox-interface/std", "gear-sandbox-env/std", - "wasmi/std", - "wasmi/virtual_memory", + "wasmer", + "wasmer-types", + "wasmer-vm", + "wasmer-compiler", + "gear-wasmer-cache", ] strict = [] diff --git a/sandbox/sandbox/build.rs b/sandbox/sandbox/build.rs new file mode 100644 index 00000000000..2ebc39a2c86 --- /dev/null +++ b/sandbox/sandbox/build.rs @@ -0,0 +1,27 @@ +// This file is part of Gear. +// +// Copyright (C) 2024 Gear Technologies Inc. +// 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 std::{env, fs, path::PathBuf}; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let out_dir = PathBuf::from(out_dir); + // create placeholder in `OUT_DIR` + // so `env!("OUT_DIR")` can be used in embedded executor module caching + fs::write(out_dir.join("placeholder"), "placeholder file").unwrap(); +} diff --git a/sandbox/sandbox/src/embedded_executor.rs b/sandbox/sandbox/src/embedded_executor.rs index 3ec28a62066..6a3b08f387a 100644 --- a/sandbox/sandbox/src/embedded_executor.rs +++ b/sandbox/sandbox/src/embedded_executor.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! An embedded WASM executor utilizing `wasmi`. +//! An embedded WASM executor utilizing `wasmer`. use crate::{ AsContextExt, Error, GlobalsSetError, HostError, HostFuncType, ReturnValue, SandboxStore, @@ -24,98 +24,247 @@ use crate::{ }; use alloc::string::String; use gear_sandbox_env::GLOBAL_NAME_GAS; -use sp_core::RuntimeDebug; -use sp_std::{collections::btree_map::BTreeMap, marker::PhantomData, prelude::*}; +use gear_wasmer_cache::get_or_compile_with_cache; use sp_wasm_interface_common::HostPointer; -use wasmi::{ - core::{Pages, Trap, UntypedValue}, - Config, Engine, ExternType, Linker, MemoryType, Module, StackLimits, StoreContext, - StoreContextMut, Value as RuntimeValue, +use std::{collections::btree_map::BTreeMap, fs, marker::PhantomData, path::PathBuf, ptr::NonNull}; +use wasmer::{ + sys::{BaseTunables, VMConfig}, + vm::{ + LinearMemory, MemoryStyle, TableStyle, VMGlobal, VMMemory, VMMemoryDefinition, VMTable, + VMTableDefinition, + }, + Engine, FunctionEnv, Global, GlobalType, Imports, MemoryError, MemoryType, NativeEngineExt, + RuntimeError, StoreMut, StoreObjects, StoreRef, TableType, Tunables, Value as RuntimeValue, }; +use wasmer_types::{ExternType, Target}; + +fn fs_cache() -> PathBuf { + let out_dir = PathBuf::from(env!("OUT_DIR")); + let cache = out_dir.join("wasmer-cache"); + if !cache.exists() { + fs::create_dir(&cache).unwrap(); + } + cache +} + +struct CustomTunables { + inner: BaseTunables, + vmconfig: VMConfig, +} + +impl CustomTunables { + fn for_target(target: &Target) -> Self { + Self { + inner: BaseTunables::for_target(target), + vmconfig: VMConfig { + wasm_stack_size: None, + }, + } + } + + fn with_wasm_stack_size(mut self, wasm_stack_size: impl Into>) -> Self { + self.vmconfig.wasm_stack_size = wasm_stack_size.into(); + self + } +} + +impl Tunables for CustomTunables { + fn memory_style(&self, memory: &MemoryType) -> MemoryStyle { + self.inner.memory_style(memory) + } + + fn table_style(&self, table: &TableType) -> TableStyle { + self.inner.table_style(table) + } + + fn create_host_memory( + &self, + ty: &MemoryType, + style: &MemoryStyle, + ) -> Result { + self.inner.create_host_memory(ty, style) + } + + unsafe fn create_vm_memory( + &self, + ty: &MemoryType, + style: &MemoryStyle, + vm_definition_location: NonNull, + ) -> Result { + self.inner + .create_vm_memory(ty, style, vm_definition_location) + } + + fn create_host_table(&self, ty: &TableType, style: &TableStyle) -> Result { + self.inner.create_host_table(ty, style) + } + + unsafe fn create_vm_table( + &self, + ty: &TableType, + style: &TableStyle, + vm_definition_location: NonNull, + ) -> Result { + self.inner + .create_vm_table(ty, style, vm_definition_location) + } + + fn create_global(&self, ty: GlobalType) -> Result { + self.inner.create_global(ty) + } + + unsafe fn create_memories( + &self, + context: &mut StoreObjects, + module: &wasmer_types::ModuleInfo, + memory_styles: &wasmer_types::entity::PrimaryMap, + memory_definition_locations: &[NonNull], + ) -> Result< + wasmer_types::entity::PrimaryMap< + wasmer_types::LocalMemoryIndex, + wasmer_vm::InternalStoreHandle, + >, + wasmer_compiler::LinkError, + > { + self.inner + .create_memories(context, module, memory_styles, memory_definition_locations) + } + + unsafe fn create_tables( + &self, + context: &mut StoreObjects, + module: &wasmer_types::ModuleInfo, + table_styles: &wasmer_types::entity::PrimaryMap, + table_definition_locations: &[NonNull], + ) -> Result< + wasmer_types::entity::PrimaryMap< + wasmer_types::LocalTableIndex, + wasmer_vm::InternalStoreHandle, + >, + wasmer_compiler::LinkError, + > { + self.inner + .create_tables(context, module, table_styles, table_definition_locations) + } + + fn create_globals( + &self, + context: &mut StoreObjects, + module: &wasmer_types::ModuleInfo, + ) -> Result< + wasmer_types::entity::PrimaryMap< + wasmer_types::LocalGlobalIndex, + wasmer_vm::InternalStoreHandle, + >, + wasmer_compiler::LinkError, + > { + self.inner.create_globals(context, module) + } + + fn vmconfig(&self) -> &VMConfig { + &self.vmconfig + } +} /// [`AsContextExt`] extension. -pub trait AsContext: wasmi::AsContext + wasmi::AsContextMut {} +pub trait AsContext: wasmer::AsStoreRef + wasmer::AsStoreMut {} -/// wasmi store wrapper. -#[derive(RuntimeDebug)] -pub struct Store(wasmi::Store); +#[derive(Debug)] +struct InnerState { + inner: T, + gas_global: Option, +} + +impl InnerState { + fn new(inner: T) -> Self { + Self { + inner, + gas_global: None, + } + } +} + +/// wasmer store wrapper. +#[derive(Debug)] +pub struct Store { + inner: wasmer::Store, + state: FunctionEnv>, +} impl Store { fn engine(&self) -> &Engine { - self.0.engine() + self.inner.engine() } } -impl SandboxStore for Store { +impl SandboxStore for Store { fn new(state: T) -> Self { - let register_len = size_of::(); - - const DEFAULT_MIN_VALUE_STACK_HEIGHT: usize = 1024; - const DEFAULT_MAX_VALUE_STACK_HEIGHT: usize = 1024 * DEFAULT_MIN_VALUE_STACK_HEIGHT; - const DEFAULT_MAX_RECURSION_DEPTH: usize = 16384; - - let mut config = Config::default(); - config.set_stack_limits( - StackLimits::new( - DEFAULT_MIN_VALUE_STACK_HEIGHT / register_len, - DEFAULT_MAX_VALUE_STACK_HEIGHT / register_len, - DEFAULT_MAX_RECURSION_DEPTH, - ) - .expect("infallible"), - ); + let mut engine = Engine::from(wasmer::Singlepass::new()); + let tunables = CustomTunables::for_target(engine.target()) + // make stack size bigger for fuzzer + .with_wasm_stack_size(16 * 1024 * 1024); + engine.set_tunables(tunables); + let mut store = wasmer::Store::new(engine); - let engine = Engine::new(&config); - let store = wasmi::Store::new(&engine, state); - Self(store) + let state = FunctionEnv::new(&mut store, InnerState::new(state)); + + Self { + inner: store, + state, + } } } -impl wasmi::AsContext for Store { - type UserState = T; - - fn as_context(&self) -> StoreContext { - self.0.as_context() +impl wasmer::AsStoreRef for Store { + fn as_store_ref(&self) -> StoreRef<'_> { + self.inner.as_store_ref() } } -impl wasmi::AsContextMut for Store { - fn as_context_mut(&mut self) -> StoreContextMut { - self.0.as_context_mut() +impl wasmer::AsStoreMut for Store { + fn as_store_mut(&mut self) -> StoreMut<'_> { + self.inner.as_store_mut() + } + + fn objects_mut(&mut self) -> &mut StoreObjects { + self.inner.objects_mut() } } -impl AsContextExt for Store { +impl AsContextExt for Store { type State = T; fn data_mut(&mut self) -> &mut Self::State { - self.0.data_mut() + &mut self.state.as_mut(&mut self.inner).inner } } impl AsContext for Store {} -/// wasmi caller wrapper. -pub struct Caller<'a, T>(wasmi::Caller<'a, T>); +/// wasmer function env wrapper. +pub struct Caller<'a, T>(wasmer::FunctionEnvMut<'a, InnerState>); -impl wasmi::AsContext for Caller<'_, T> { - type UserState = T; - - fn as_context(&self) -> StoreContext { - self.0.as_context() +impl wasmer::AsStoreRef for Caller<'_, T> { + fn as_store_ref(&self) -> StoreRef<'_> { + self.0.as_store_ref() } } -impl wasmi::AsContextMut for Caller<'_, T> { - fn as_context_mut(&mut self) -> StoreContextMut { - self.0.as_context_mut() +impl wasmer::AsStoreMut for Caller<'_, T> { + fn as_store_mut(&mut self) -> StoreMut<'_> { + self.0.as_store_mut() + } + + fn objects_mut(&mut self) -> &mut StoreObjects { + self.0.objects_mut() } } -impl AsContextExt for Caller<'_, T> { +impl AsContextExt for Caller<'_, T> { type State = T; fn data_mut(&mut self) -> &mut Self::State { - self.0.data_mut() + &mut self.0.data_mut().inner } } @@ -124,26 +273,33 @@ impl AsContext for Caller<'_, T> {} /// The linear memory used by the sandbox. #[derive(Clone)] pub struct Memory { - memref: wasmi::Memory, + memref: wasmer::Memory, + base: usize, } impl super::SandboxMemory for Memory { fn new(store: &mut Store, initial: u32, maximum: Option) -> Result { - let ty = MemoryType::new(initial, maximum).map_err(|_| Error::Module)?; - let memref = wasmi::Memory::new(store, ty).map_err(|_| Error::Module)?; - Ok(Memory { memref }) + let ty = MemoryType::new(initial, maximum, false); + let memory_style = store.engine().tunables().memory_style(&ty); + let memref = VMMemory::new(&ty, &memory_style).map_err(|e| { + log::trace!("Failed to create memory: {e}"); + Error::Module + })?; + // SAFETY: `vmmemory()` returns `NonNull` so pointer is valid + let memory_definition = unsafe { memref.vmmemory().as_ref() }; + let base = memory_definition.base as usize; + let memref = wasmer::Memory::new_from_existing(store, memref); + Ok(Memory { memref, base }) } fn read(&self, ctx: &Context, ptr: u32, buf: &mut [u8]) -> Result<(), Error> where Context: AsContextExt, { - let data = self - .memref - .data(ctx) - .get((ptr as usize)..(ptr as usize + buf.len())) - .ok_or(Error::OutOfBounds)?; - buf[..].copy_from_slice(data); + self.memref + .view(ctx) + .read(ptr as u64, buf) + .map_err(|_| Error::OutOfBounds)?; Ok(()) } @@ -151,12 +307,10 @@ impl super::SandboxMemory for Memory { where Context: AsContextExt, { - let data = self - .memref - .data_mut(ctx) - .get_mut((ptr as usize)..(ptr as usize + value.len())) - .ok_or(Error::OutOfBounds)?; - data[..].copy_from_slice(value); + self.memref + .view(ctx) + .write(ptr as u64, value) + .map_err(|_| Error::OutOfBounds)?; Ok(()) } @@ -164,10 +318,9 @@ impl super::SandboxMemory for Memory { where Context: AsContextExt, { - let pages = Pages::new(pages).ok_or(Error::MemoryGrow)?; self.memref .grow(ctx, pages) - .map(Into::into) + .map(|pages| pages.0) .map_err(|_| Error::MemoryGrow) } @@ -175,14 +328,14 @@ impl super::SandboxMemory for Memory { where Context: AsContextExt, { - self.memref.current_pages(ctx).into() + self.memref.view(ctx).size().0 } - unsafe fn get_buff(&self, ctx: &Context) -> u64 + unsafe fn get_buff(&self, _ctx: &Context) -> u64 where Context: AsContextExt, { - self.memref.data(ctx).as_ptr() as usize as u64 + self.base as u64 } } @@ -249,33 +402,33 @@ impl super::SandboxEnvironmentBuilder for EnvironmentDefinitionBui /// Sandboxed instance of a WASM module. pub struct Instance { - instance: wasmi::Instance, + instance: wasmer::Instance, _marker: PhantomData, } impl Clone for Instance { fn clone(&self) -> Self { Self { - instance: self.instance, + instance: self.instance.clone(), _marker: PhantomData, } } } -impl super::SandboxInstance for Instance { +impl super::SandboxInstance for Instance { type Memory = Memory; type EnvironmentBuilder = EnvironmentDefinitionBuilder; fn new( - mut store: &mut Store, + store: &mut Store, code: &[u8], env_def_builder: &Self::EnvironmentBuilder, ) -> Result, Error> { - let module = Module::new(store.engine(), code).map_err(|e| { + let module = get_or_compile_with_cache(code, store.engine(), fs_cache).map_err(|e| { log::trace!(target: TARGET, "Failed to create module: {e}"); Error::Module })?; - let mut linker = Linker::new(store.engine()); + let mut imports = Imports::new(); for import in module.imports() { let module = import.module().to_string(); @@ -290,61 +443,69 @@ impl super::SandboxInstance for Instance { .get(&key) .cloned() .and_then(|val| val.memory()) - .ok_or(Error::Module)? + .ok_or_else(|| { + log::trace!("Memory import for `{module}::{name}` not found"); + Error::Module + })? .memref; - - let mem = wasmi::Extern::Memory(mem); - linker - .define(&module, &name, mem) - .map_err(|_| Error::Module)?; + imports.define(&module, &name, mem); } - ExternType::Func(func_ty) => { + ExternType::Function(func_ty) => { let func_ptr = env_def_builder .map .get(&key) .cloned() .and_then(|val| val.host_func()) - .ok_or(Error::Module)?; + .ok_or_else(|| { + log::trace!("Function import for `{module}::{name}` not found"); + Error::Module + })?; - let func = wasmi::Func::new( - &mut store, + let func_ty = func_ty.clone(); + + let func = wasmer::Function::new_with_env( + &mut store.inner, + &store.state, func_ty.clone(), - move |caller, params, results| { - let gas = caller - .get_export(GLOBAL_NAME_GAS) - .ok_or_else(|| { - Trap::new(format!("failed to find `{GLOBAL_NAME_GAS}` export")) - })? - .into_global() - .ok_or_else(|| { - Trap::new(format!("{GLOBAL_NAME_GAS} is not global")) - })?; + move |mut env, params| { + let (inner_state, mut store) = env.data_and_store_mut(); + let gas = inner_state + .gas_global + .as_ref() + .unwrap_or_else(|| { + unreachable!( + "`{GLOBAL_NAME_GAS}` global should be set to `Some(...)`" + ) + }) + .clone(); - let params: Vec<_> = Some(gas.get(&caller)) + let params: Vec<_> = Some(gas.get(&mut store)) .into_iter() .chain(params.iter().cloned()) .map(to_interface) .map(|val| { - val.ok_or(Trap::new( - "`externref` or `funcref` are not supported", - )) + val.ok_or_else(|| { + RuntimeError::new( + "`externref` or `funcref` are not supported", + ) + }) }) .collect::>()?; - let mut caller = Caller(caller); + let mut caller = Caller(env); let val = (func_ptr)(&mut caller, ¶ms) - .map_err(|HostError| Trap::new("function error"))?; + .map_err(|HostError| RuntimeError::new("function error"))?; - match (val.inner, results) { - (ReturnValue::Unit, []) => {} + let return_val = match (val.inner, func_ty.results()) { + (ReturnValue::Unit, []) => None, (ReturnValue::Value(val), [ret]) => { - let val = to_wasmi(val); + let val = to_wasmer(val); - if val.ty() != ret.ty() { - return Err(Trap::new("mismatching return types")); + if val.ty() != *ret { + return Err(RuntimeError::new("mismatching return types")); } - *ret = val; + Some(val) } _results => { let err_msg = format!( @@ -355,34 +516,34 @@ impl super::SandboxInstance for Instance { log::error!("{err_msg}"); unreachable!("{err_msg}") } - } + }; gas.set(&mut caller.0, RuntimeValue::I64(val.gas)) .map_err(|e| { - Trap::new(format!( + RuntimeError::new(format!( "failed to set `{GLOBAL_NAME_GAS}` global: {e}" )) })?; - Ok(()) + Ok(Vec::from_iter(return_val)) }, ); - let func = wasmi::Extern::Func(func); - linker - .define(&module, &name, func) - .map_err(|_| Error::Module)?; + imports.define(&module, &name, func); } } } - let instance_pre = linker.instantiate(&mut store, &module).map_err(|e| { + let instance = wasmer::Instance::new(store, &module, &imports).map_err(|e| { log::trace!(target: TARGET, "Error instantiating module: {:?}", e); Error::Module })?; - let instance = instance_pre.start(&mut store).map_err(|e| { - log::trace!(target: TARGET, "Error starting module: {:?}", e); - Error::Module - })?; + + store.state.as_mut(&mut store.inner).gas_global = instance + .exports + .get_global(GLOBAL_NAME_GAS) + // gas global is optional during some benchmarks + .ok() + .cloned(); Ok(Instance { instance, @@ -396,23 +557,20 @@ impl super::SandboxInstance for Instance { name: &str, args: &[Value], ) -> Result { - let args = args.iter().cloned().map(to_wasmi).collect::>(); + let args = args.iter().cloned().map(to_wasmer).collect::>(); let func = self .instance - .get_func(&store, name) - .ok_or(Error::Execution)?; - - let func_ty = func.ty(&store); - let mut results = - vec![RuntimeValue::ExternRef(wasmi::ExternRef::null()); func_ty.results().len()]; + .exports + .get_function(name) + .map_err(|_| Error::Execution)?; - func.call(&mut store, &args, &mut results).map_err(|e| { + let results = func.call(&mut store, &args).map_err(|e| { log::trace!(target: TARGET, "invocation error: {e}"); Error::Execution })?; - match results.as_slice() { + match results.as_ref() { [] => Ok(ReturnValue::Unit), [val] => { let val = to_interface(val.clone()).ok_or(Error::Execution)?; @@ -430,8 +588,8 @@ impl super::SandboxInstance for Instance { } } - fn get_global_val(&self, store: &Store, name: &str) -> Option { - let global = self.instance.get_global(store, name)?; + fn get_global_val(&self, store: &mut Store, name: &str) -> Option { + let global = self.instance.exports.get_global(name).ok()?; let global = global.get(store); to_interface(global) } @@ -444,10 +602,11 @@ impl super::SandboxInstance for Instance { ) -> Result<(), GlobalsSetError> { let global = self .instance - .get_global(&store, name) - .ok_or(GlobalsSetError::NotFound)?; + .exports + .get_global(name) + .map_err(|_| GlobalsSetError::NotFound)?; global - .set(&mut store, to_wasmi(value)) + .set(&mut store, to_wasmer(value)) .map_err(|_| GlobalsSetError::Other)?; Ok(()) } @@ -460,24 +619,24 @@ impl super::SandboxInstance for Instance { } } -/// Convert the substrate value type to the wasmi value type. -fn to_wasmi(value: Value) -> RuntimeValue { +/// Convert the substrate value type to the wasmer value type. +fn to_wasmer(value: Value) -> RuntimeValue { match value { Value::I32(val) => RuntimeValue::I32(val), Value::I64(val) => RuntimeValue::I64(val), - Value::F32(val) => RuntimeValue::F32(val.into()), - Value::F64(val) => RuntimeValue::F64(val.into()), + Value::F32(val) => RuntimeValue::F32(f32::from_bits(val)), + Value::F64(val) => RuntimeValue::F64(f64::from_bits(val)), } } -/// Convert the wasmi value type to the substrate value type. +/// Convert the wasmer value type to the substrate value type. fn to_interface(value: RuntimeValue) -> Option { match value { RuntimeValue::I32(val) => Some(Value::I32(val)), RuntimeValue::I64(val) => Some(Value::I64(val)), - RuntimeValue::F32(val) => Some(Value::F32(val.into())), - RuntimeValue::F64(val) => Some(Value::F64(val.into())), - RuntimeValue::FuncRef(_) | RuntimeValue::ExternRef(_) => None, + RuntimeValue::F32(val) => Some(Value::F32(val.to_bits())), + RuntimeValue::F64(val) => Some(Value::F64(val.to_bits())), + RuntimeValue::V128(_) | RuntimeValue::FuncRef(_) | RuntimeValue::ExternRef(_) => None, } } diff --git a/sandbox/sandbox/src/host_executor.rs b/sandbox/sandbox/src/host_executor.rs index 2e8ef4e8826..a05be46ca7a 100644 --- a/sandbox/sandbox/src/host_executor.rs +++ b/sandbox/sandbox/src/host_executor.rs @@ -380,7 +380,7 @@ impl super::SandboxInstance for Instance { } } - fn get_global_val(&self, _store: &Store, name: &str) -> Option { + fn get_global_val(&self, _store: &mut Store, name: &str) -> Option { sandbox::get_global_val(*self.instance_idx, name) } diff --git a/sandbox/sandbox/src/lib.rs b/sandbox/sandbox/src/lib.rs index 5ae1106b580..886b039c445 100644 --- a/sandbox/sandbox/src/lib.rs +++ b/sandbox/sandbox/src/lib.rs @@ -44,23 +44,26 @@ extern crate alloc; #[cfg(feature = "std")] pub mod embedded_executor; -pub use gear_sandbox_env as env; +#[cfg(feature = "std")] +pub use self::embedded_executor as default_executor; + #[cfg(not(feature = "std"))] +#[cfg(target_arch = "wasm32")] pub mod host_executor; +#[cfg(not(feature = "std"))] +#[cfg(target_arch = "wasm32")] +pub use self::host_executor as default_executor; + +pub use gear_sandbox_env as env; +pub use gear_sandbox_env::HostError; + +pub use sp_wasm_interface_common::{IntoValue, ReturnValue, TryFromValue, Value}; use alloc::string::String; use sp_core::RuntimeDebug; use sp_std::prelude::*; use sp_wasm_interface_common::HostPointer; -pub use sp_wasm_interface_common::{IntoValue, ReturnValue, TryFromValue, Value}; - -#[cfg(feature = "std")] -pub use self::embedded_executor as default_executor; -pub use self::env::HostError; -#[cfg(not(feature = "std"))] -pub use self::host_executor as default_executor; - /// The target used for logging. const TARGET: &str = "runtime::sandbox"; @@ -249,7 +252,11 @@ pub trait SandboxInstance: Sized { /// Get the value from a global with the given `name`. /// /// Returns `Some(_)` if the global could be found. - fn get_global_val(&self, store: &default_executor::Store, name: &str) -> Option; + fn get_global_val( + &self, + store: &mut default_executor::Store, + name: &str, + ) -> Option; /// Set the value of a global with the given `name`. fn set_global_val( diff --git a/scripts/cargo-xwin.sh b/scripts/cargo-xwin.sh index 5e7fe7dbf23..416ef52347c 100755 --- a/scripts/cargo-xwin.sh +++ b/scripts/cargo-xwin.sh @@ -1,3 +1,19 @@ #!/usr/bin/env bash -XWIN_ARCH="x86_64" CARGO_BUILD_TARGET=x86_64-pc-windows-msvc CARGO_TARGET_DIR="target-xwin" cargo xwin $@ +export CARGO_BUILD_TARGET="x86_64-pc-windows-msvc" +export XWIN_ARCH="x86_64" +export CARGO_TARGET_DIR="target-xwin" + +if [ "$1" = "--gdb" ]; then + export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER="winedbg --gdb --no-start" + shift +fi + +if [ "$1" = "--lldb" ]; then + export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER="/opt/homebrew/opt/llvm/bin/lldb-server g :1234 -- wine" + shift +fi + +cargo xwin $@ + +wineserver -k diff --git a/utils/calc-stack-height/Cargo.toml b/utils/calc-stack-height/Cargo.toml index 31faf729ae3..15a97459277 100644 --- a/utils/calc-stack-height/Cargo.toml +++ b/utils/calc-stack-height/Cargo.toml @@ -11,7 +11,7 @@ repository.workspace = true gear-core.workspace = true gear-wasm-instrument.workspace = true vara-runtime = { workspace = true, features = ["std", "dev"] } -wasmer = { workspace = true, features = ["singlepass"] } +wasmer.workspace = true wasmer-types.workspace = true log.workspace = true env_logger.workspace = true diff --git a/utils/crates-io/src/lib.rs b/utils/crates-io/src/lib.rs index c7fb991429d..e0b4f4fb71e 100644 --- a/utils/crates-io/src/lib.rs +++ b/utils/crates-io/src/lib.rs @@ -64,6 +64,7 @@ pub const STACKED_DEPENDENCIES: &[&str] = &[ "gear-core", "gear-utils", "gear-common", + "gear-wasmer-cache", "gear-sandbox-host", "gear-lazy-pages-common", "gear-lazy-pages", diff --git a/utils/gear-replay-cli/src/cmd/mod.rs b/utils/gear-replay-cli/src/cmd/mod.rs index ffc93f217f0..0557362be4c 100644 --- a/utils/gear-replay-cli/src/cmd/mod.rs +++ b/utils/gear-replay-cli/src/cmd/mod.rs @@ -63,8 +63,6 @@ pub enum Command { impl Command { pub async fn run(&self, shared: &SharedParams) -> sc_cli::Result<()> { - gear_runtime_interface::sandbox_init(); - match &self { Command::ReplayBlock(cmd) => { replay_block::run::(shared.clone(), cmd.clone()).await diff --git a/utils/gear-wasmer-cache/Cargo.toml b/utils/gear-wasmer-cache/Cargo.toml new file mode 100644 index 00000000000..772f2b8f842 --- /dev/null +++ b/utils/gear-wasmer-cache/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "gear-wasmer-cache" +version.workspace = true +authors.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +rust-version.workspace = true + +[dependencies] +wasmer.workspace = true +wasmer-cache.workspace = true +log.workspace = true +uluru.workspace = true diff --git a/sandbox/host/src/sandbox/wasmer_backend/cache.rs b/utils/gear-wasmer-cache/src/lib.rs similarity index 81% rename from sandbox/host/src/sandbox/wasmer_backend/cache.rs rename to utils/gear-wasmer-cache/src/lib.rs index e7089cfb151..ff48e592f84 100644 --- a/sandbox/host/src/sandbox/wasmer_backend/cache.rs +++ b/utils/gear-wasmer-cache/src/lib.rs @@ -25,9 +25,8 @@ use std::{ sync::{Mutex, OnceLock}, }; -use tempfile::TempDir; use uluru::LRUCache; -use wasmer::Module; +use wasmer::{CompileError, Engine, Module}; use wasmer_cache::Hash; pub struct CacheMissErr { @@ -51,21 +50,11 @@ fn lru_cache() -> &'static CachedModules { CACHED_MODULES.get_or_init(|| Mutex::new(LRUCache::default())) } -fn fs_cache() -> FileSystemCache { - static CACHE_DIR: OnceLock = OnceLock::new(); - - // Try to load from tempfile cache - let cache_path = CACHE_DIR - .get_or_init(|| { - tempfile::tempdir().expect("Cannot create temporary directory for wasmer caches") - }) - .path(); - log::trace!("Wasmer sandbox cache dir is: {cache_path:?}"); - - FileSystemCache::new(cache_path) -} - -pub fn get_cached_module(wasm: &[u8], store: &wasmer::Store) -> Result { +pub fn get_cached_module( + wasm: &[u8], + engine: &Engine, + fs_cache: impl FnOnce() -> PathBuf, +) -> Result { let mut lru_lock = lru_cache().lock().expect("CACHED_MODULES lock fail"); let maybe_module = lru_lock.find(|x| x.wasm == wasm); @@ -77,14 +66,14 @@ pub fn get_cached_module(wasm: &[u8], store: &wasmer::Store) -> Result Result PathBuf, +) -> Result { + match get_cached_module(wasm, engine, fs_cache) { + Ok(module) => { + log::trace!("Found cached module for current program"); + Ok(module) + } + Err(CacheMissErr { + fs_cache, + code_hash, + }) => { + log::trace!("Cache for program has not been found, so compile it now"); + let module = Module::new(engine, wasm)?; + + try_to_store_module_in_cache(fs_cache, code_hash, wasm, &module); + + Ok(module) + } + } +} + /// Altered copy of the `FileSystemCache` struct from `wasmer_cache` crate. #[derive(Debug, Clone)] pub struct FileSystemCache { diff --git a/utils/lazy-pages-fuzzer/Cargo.toml b/utils/lazy-pages-fuzzer/Cargo.toml index ced04890460..ae84c472580 100644 --- a/utils/lazy-pages-fuzzer/Cargo.toml +++ b/utils/lazy-pages-fuzzer/Cargo.toml @@ -14,7 +14,7 @@ gear-lazy-pages.workspace = true gear-lazy-pages-common.workspace = true log.workspace = true region.workspace = true -wasmer = { workspace = true, features = ["singlepass"] } +wasmer.workspace = true sandbox-wasmi.workspace = true wasmprinter.workspace = true wat.workspace = true diff --git a/utils/wasm-builder/Cargo.toml b/utils/wasm-builder/Cargo.toml index d11bb245d3d..0a342ce01c5 100644 --- a/utils/wasm-builder/Cargo.toml +++ b/utils/wasm-builder/Cargo.toml @@ -13,6 +13,7 @@ readme = "README.md" [dependencies] anyhow.workspace = true cargo_metadata.workspace = true +cargo_toml.workspace = true chrono = "0.4" thiserror.workspace = true regex.workspace = true diff --git a/utils/wasm-builder/src/crate_info.rs b/utils/wasm-builder/src/crate_info.rs index 87f854ffc74..b1eb4e08827 100644 --- a/utils/wasm-builder/src/crate_info.rs +++ b/utils/wasm-builder/src/crate_info.rs @@ -33,6 +33,8 @@ pub struct CrateInfo { pub version: String, /// Crate features. pub features: BTreeMap>, + /// Crate custom profiles + pub profiles: BTreeMap, } impl CrateInfo { @@ -53,6 +55,16 @@ impl CrateInfo { .ok_or_else(|| BuilderError::RootPackageNotFound.into()) .and_then(Self::check)?; + let manifest = cargo_toml::Manifest::from_path(metadata.workspace_root.join("Cargo.toml")) + .context("manifest parsing failed")?; + let profiles = manifest + .profile + .custom + .into_iter() + .map(|(k, v)| Ok((k, toml::Value::try_from(v)?))) + .collect::>() + .context("failed to convert profile to `toml::Value`")?; + multiple_crate_versions::check(&metadata, &root_package.id)?; let name = root_package.name.clone(); @@ -65,6 +77,7 @@ impl CrateInfo { snake_case_name, version, features, + profiles, }) } diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index aecd3c7e0b4..ec9b8b1ce50 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -200,7 +200,7 @@ impl WasmProject { let mut production_profile = Table::new(); production_profile.insert("inherits".into(), "release".into()); - let mut profile = Table::new(); + let mut profile = crate_info.profiles; profile.insert("dev".into(), dev_profile.clone().into()); profile.insert("release".into(), release_profile.into()); profile.insert("production".into(), production_profile.into()); diff --git a/utils/wasm-optimizer/Cargo.toml b/utils/wasm-optimizer/Cargo.toml index 0501a8c94dd..9d85e01fcf5 100644 --- a/utils/wasm-optimizer/Cargo.toml +++ b/utils/wasm-optimizer/Cargo.toml @@ -23,5 +23,5 @@ which.workspace = true colored.workspace = true [dev-dependencies] -wasmer = { workspace = true, features = ["std"] } +wasmer.workspace = true wabt.workspace = true