Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit bcf5610

Browse files
committed
Partially backport #7838
1 parent cac5cd2 commit bcf5610

File tree

6 files changed

+79
-94
lines changed

6 files changed

+79
-94
lines changed

Cargo.lock

Lines changed: 7 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/executor/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sc-executor"
3-
version = "0.8.0"
3+
version = "0.8.1"
44
authors = ["Parity Technologies <[email protected]>"]
55
edition = "2018"
66
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
@@ -43,12 +43,12 @@ hex-literal = "0.3.1"
4343
sc-runtime-test = { version = "2.0.0", path = "runtime-test" }
4444
substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" }
4545
sp-state-machine = { version = "0.8.0", path = "../../primitives/state-machine" }
46-
test-case = "0.3.3"
4746
sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" }
4847
sp-tracing = { version = "2.0.0", path = "../../primitives/tracing" }
4948
sc-tracing = { version = "2.0.0", path = "../tracing" }
5049
tracing = "0.1.19"
5150
tracing-subscriber = "0.2.10"
51+
paste = "0.1.6"
5252

5353
[features]
5454
default = [ "std" ]

client/executor/src/integration_tests/mod.rs

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use sp_core::{
2626
};
2727
use sc_runtime_test::wasm_binary_unwrap;
2828
use sp_state_machine::TestExternalities as CoreTestExternalities;
29-
use test_case::test_case;
3029
use sp_trie::{TrieConfiguration, trie_types::Layout};
3130
use sp_wasm_interface::HostFunctions as _;
3231
use sp_runtime::traits::BlakeTwo256;
@@ -37,6 +36,34 @@ use crate::WasmExecutionMethod;
3736
pub type TestExternalities = CoreTestExternalities<BlakeTwo256, u64>;
3837
type HostFunctions = sp_io::SubstrateHostFunctions;
3938

39+
/// Simple macro that runs a given method as test with the available wasm execution methods.
40+
#[macro_export]
41+
macro_rules! test_wasm_execution {
42+
($method_name:ident) => {
43+
paste::item! {
44+
#[test]
45+
fn [<$method_name _interpreted>]() {
46+
$method_name(WasmExecutionMethod::Interpreted);
47+
}
48+
49+
#[test]
50+
#[cfg(feature = "wasmtime")]
51+
fn [<$method_name _compiled>]() {
52+
$method_name(WasmExecutionMethod::Compiled);
53+
}
54+
}
55+
};
56+
57+
(interpreted_only $method_name:ident) => {
58+
paste::item! {
59+
#[test]
60+
fn [<$method_name _interpreted>]() {
61+
$method_name(WasmExecutionMethod::Interpreted);
62+
}
63+
}
64+
};
65+
}
66+
4067
fn call_in_wasm<E: Externalities>(
4168
function: &str,
4269
call_data: &[u8],
@@ -59,8 +86,7 @@ fn call_in_wasm<E: Externalities>(
5986
)
6087
}
6188

62-
#[test_case(WasmExecutionMethod::Interpreted)]
63-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
89+
test_wasm_execution!(returning_should_work);
6490
fn returning_should_work(wasm_method: WasmExecutionMethod) {
6591
let mut ext = TestExternalities::default();
6692
let mut ext = ext.ext();
@@ -74,8 +100,7 @@ fn returning_should_work(wasm_method: WasmExecutionMethod) {
74100
assert_eq!(output, vec![0u8; 0]);
75101
}
76102

77-
#[test_case(WasmExecutionMethod::Interpreted)]
78-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
103+
test_wasm_execution!(call_not_existing_function);
79104
fn call_not_existing_function(wasm_method: WasmExecutionMethod) {
80105
let mut ext = TestExternalities::default();
81106
let mut ext = ext.ext();
@@ -102,8 +127,7 @@ fn call_not_existing_function(wasm_method: WasmExecutionMethod) {
102127
}
103128
}
104129

105-
#[test_case(WasmExecutionMethod::Interpreted)]
106-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
130+
test_wasm_execution!(call_yet_another_not_existing_function);
107131
fn call_yet_another_not_existing_function(wasm_method: WasmExecutionMethod) {
108132
let mut ext = TestExternalities::default();
109133
let mut ext = ext.ext();
@@ -130,8 +154,7 @@ fn call_yet_another_not_existing_function(wasm_method: WasmExecutionMethod) {
130154
}
131155
}
132156

133-
#[test_case(WasmExecutionMethod::Interpreted)]
134-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
157+
test_wasm_execution!(panicking_should_work);
135158
fn panicking_should_work(wasm_method: WasmExecutionMethod) {
136159
let mut ext = TestExternalities::default();
137160
let mut ext = ext.ext();
@@ -161,8 +184,7 @@ fn panicking_should_work(wasm_method: WasmExecutionMethod) {
161184
assert!(output.is_err());
162185
}
163186

164-
#[test_case(WasmExecutionMethod::Interpreted)]
165-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
187+
test_wasm_execution!(storage_should_work);
166188
fn storage_should_work(wasm_method: WasmExecutionMethod) {
167189
let mut ext = TestExternalities::default();
168190

@@ -191,8 +213,7 @@ fn storage_should_work(wasm_method: WasmExecutionMethod) {
191213
assert_eq!(ext, expected);
192214
}
193215

194-
#[test_case(WasmExecutionMethod::Interpreted)]
195-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
216+
test_wasm_execution!(clear_prefix_should_work);
196217
fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
197218
let mut ext = TestExternalities::default();
198219
{
@@ -225,8 +246,7 @@ fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
225246
assert_eq!(expected, ext);
226247
}
227248

228-
#[test_case(WasmExecutionMethod::Interpreted)]
229-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
249+
test_wasm_execution!(blake2_256_should_work);
230250
fn blake2_256_should_work(wasm_method: WasmExecutionMethod) {
231251
let mut ext = TestExternalities::default();
232252
let mut ext = ext.ext();
@@ -250,8 +270,7 @@ fn blake2_256_should_work(wasm_method: WasmExecutionMethod) {
250270
);
251271
}
252272

253-
#[test_case(WasmExecutionMethod::Interpreted)]
254-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
273+
test_wasm_execution!(blake2_128_should_work);
255274
fn blake2_128_should_work(wasm_method: WasmExecutionMethod) {
256275
let mut ext = TestExternalities::default();
257276
let mut ext = ext.ext();
@@ -275,8 +294,7 @@ fn blake2_128_should_work(wasm_method: WasmExecutionMethod) {
275294
);
276295
}
277296

278-
#[test_case(WasmExecutionMethod::Interpreted)]
279-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
297+
test_wasm_execution!(sha2_256_should_work);
280298
fn sha2_256_should_work(wasm_method: WasmExecutionMethod) {
281299
let mut ext = TestExternalities::default();
282300
let mut ext = ext.ext();
@@ -306,8 +324,7 @@ fn sha2_256_should_work(wasm_method: WasmExecutionMethod) {
306324
);
307325
}
308326

309-
#[test_case(WasmExecutionMethod::Interpreted)]
310-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
327+
test_wasm_execution!(twox_256_should_work);
311328
fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
312329
let mut ext = TestExternalities::default();
313330
let mut ext = ext.ext();
@@ -335,8 +352,7 @@ fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
335352
);
336353
}
337354

338-
#[test_case(WasmExecutionMethod::Interpreted)]
339-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
355+
test_wasm_execution!(twox_128_should_work);
340356
fn twox_128_should_work(wasm_method: WasmExecutionMethod) {
341357
let mut ext = TestExternalities::default();
342358
let mut ext = ext.ext();
@@ -360,8 +376,7 @@ fn twox_128_should_work(wasm_method: WasmExecutionMethod) {
360376
);
361377
}
362378

363-
#[test_case(WasmExecutionMethod::Interpreted)]
364-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
379+
test_wasm_execution!(ed25519_verify_should_work);
365380
fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) {
366381
let mut ext = TestExternalities::default();
367382
let mut ext = ext.ext();
@@ -397,8 +412,7 @@ fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) {
397412
);
398413
}
399414

400-
#[test_case(WasmExecutionMethod::Interpreted)]
401-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
415+
test_wasm_execution!(sr25519_verify_should_work);
402416
fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) {
403417
let mut ext = TestExternalities::default();
404418
let mut ext = ext.ext();
@@ -434,8 +448,7 @@ fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) {
434448
);
435449
}
436450

437-
#[test_case(WasmExecutionMethod::Interpreted)]
438-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
451+
test_wasm_execution!(ordered_trie_root_should_work);
439452
fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) {
440453
let mut ext = TestExternalities::default();
441454
let trie_input = vec![b"zero".to_vec(), b"one".to_vec(), b"two".to_vec()];
@@ -450,8 +463,7 @@ fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) {
450463
);
451464
}
452465

453-
#[test_case(WasmExecutionMethod::Interpreted)]
454-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
466+
test_wasm_execution!(offchain_index);
455467
fn offchain_index(wasm_method: WasmExecutionMethod) {
456468
let mut ext = TestExternalities::default();
457469
let (offchain, _state) = testing::TestOffchainExt::new();
@@ -472,8 +484,7 @@ fn offchain_index(wasm_method: WasmExecutionMethod) {
472484
);
473485
}
474486

475-
#[test_case(WasmExecutionMethod::Interpreted)]
476-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
487+
test_wasm_execution!(offchain_local_storage_should_work);
477488
fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
478489
use sp_core::offchain::OffchainStorage;
479490

@@ -492,8 +503,7 @@ fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
492503
assert_eq!(state.read().persistent_storage.get(b"", b"test"), Some(vec![]));
493504
}
494505

495-
#[test_case(WasmExecutionMethod::Interpreted)]
496-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
506+
test_wasm_execution!(offchain_http_should_work);
497507
fn offchain_http_should_work(wasm_method: WasmExecutionMethod) {
498508
let mut ext = TestExternalities::default();
499509
let (offchain, state) = testing::TestOffchainExt::new();
@@ -521,9 +531,7 @@ fn offchain_http_should_work(wasm_method: WasmExecutionMethod) {
521531
);
522532
}
523533

524-
#[test_case(WasmExecutionMethod::Interpreted)]
525-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
526-
#[should_panic(expected = "Allocator ran out of space")]
534+
test_wasm_execution!(should_trap_when_heap_exhausted);
527535
fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
528536
let mut ext = TestExternalities::default();
529537

@@ -533,18 +541,20 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
533541
HostFunctions::host_functions(),
534542
8,
535543
);
536-
executor.call_in_wasm(
544+
545+
let err = executor.call_in_wasm(
537546
&wasm_binary_unwrap()[..],
538547
None,
539548
"test_exhaust_heap",
540549
&[0],
541550
&mut ext.ext(),
542551
sp_core::traits::MissingHostFunctions::Allow,
543-
).unwrap();
552+
).unwrap_err();
553+
554+
assert!(err.contains("Allocator ran out of space"));
544555
}
545556

546-
#[test_case(WasmExecutionMethod::Interpreted)]
547-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
557+
test_wasm_execution!(returns_mutable_static);
548558
fn returns_mutable_static(wasm_method: WasmExecutionMethod) {
549559
let runtime = crate::wasm_runtime::create_wasm_runtime_with_code(
550560
wasm_method,
@@ -569,8 +579,7 @@ fn returns_mutable_static(wasm_method: WasmExecutionMethod) {
569579
// returned to its initial value and thus the stack space is going to be leaked.
570580
//
571581
// See https://github.com/paritytech/substrate/issues/2967 for details
572-
#[test_case(WasmExecutionMethod::Interpreted)]
573-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
582+
test_wasm_execution!(restoration_of_globals);
574583
fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
575584
// Allocate 32 pages (of 65536 bytes) which gives the runtime 2048KB of heap to operate on
576585
// (plus some additional space unused from the initial pages requested by the wasm runtime
@@ -598,7 +607,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
598607
assert!(res.is_ok());
599608
}
600609

601-
#[test_case(WasmExecutionMethod::Interpreted)]
610+
test_wasm_execution!(interpreted_only heap_is_reset_between_calls);
602611
fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {
603612
let runtime = crate::wasm_runtime::create_wasm_runtime_with_code(
604613
wasm_method,
@@ -622,8 +631,7 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {
622631
instance.call("check_and_set_in_heap", &params).unwrap();
623632
}
624633

625-
#[test_case(WasmExecutionMethod::Interpreted)]
626-
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
634+
test_wasm_execution!(parallel_execution);
627635
fn parallel_execution(wasm_method: WasmExecutionMethod) {
628636
let executor = std::sync::Arc::new(crate::WasmExecutor::new(
629637
wasm_method,
@@ -658,7 +666,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) {
658666
}
659667
}
660668

661-
#[test_case(WasmExecutionMethod::Interpreted)]
669+
test_wasm_execution!(wasm_tracing_should_work);
662670
fn wasm_tracing_should_work(wasm_method: WasmExecutionMethod) {
663671

664672
use std::sync::{Arc, Mutex};

0 commit comments

Comments
 (0)