Update wasmtime to 0.29.0 (#9552)

* Start

* Move to ctx

* Make it compile for now

* More work

* Get rid off state-holder

* Use less Refcells

* 🤦

* Don't use RefCell

* Use names for parameters

* Fixes after merge

* Fixes after merge

* Review feedback

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