Refactor sp-sandbox; make sure both sandbox executors are always tested (#10173)

* sp-sandbox: convert executors into normal `mod`s instead of using `include!`

* sp-sandbox: run `cargo fmt` on `host_executor.rs`

* sp-sandbox: abstract away the executors behind traits

* sp_sandbox: always compile both executors when possible

* sc-executor: make sure all sandbox tests run on both sandbox executors

* sc-executor: fix brainfart: actually call into the sandbox through the trait

* sc-runtime-test: fix cargo fmt

* sc-runtime-test: deduplicate executor-specific sandbox test entrypoints

* sc-executor: test each sandbox executor in a separate test

* cargo fmt (Github's conflict resolving thingy broke indentation)
This commit is contained in:
Koute
2021-11-08 21:52:11 +09:00
committed by GitHub
parent fe36fe85d1
commit a7e3d819f8
12 changed files with 351 additions and 228 deletions
@@ -71,6 +71,53 @@ macro_rules! test_wasm_execution {
};
}
/// A macro to run a given test for each available WASM execution method *and* for each
/// sandbox execution method.
#[macro_export]
macro_rules! test_wasm_execution_sandbox {
($method_name:ident) => {
paste::item! {
#[test]
fn [<$method_name _interpreted_host_executor>]() {
$method_name(WasmExecutionMethod::Interpreted, "_host");
}
#[test]
fn [<$method_name _interpreted_embedded_executor>]() {
$method_name(WasmExecutionMethod::Interpreted, "_embedded");
}
#[test]
#[cfg(feature = "wasmtime")]
fn [<$method_name _compiled_host_executor>]() {
$method_name(WasmExecutionMethod::Compiled, "_host");
}
#[test]
#[cfg(feature = "wasmtime")]
fn [<$method_name _compiled_embedded_executor>]() {
$method_name(WasmExecutionMethod::Compiled, "_embedded");
}
}
};
(interpreted_only $method_name:ident) => {
paste::item! {
#[test]
fn [<$method_name _interpreted_host_executor>]() {
$method_name(WasmExecutionMethod::Interpreted, "_host");
}
}
paste::item! {
#[test]
fn [<$method_name _interpreted_embedded_executor>]() {
$method_name(WasmExecutionMethod::Interpreted, "_embedded");
}
}
};
}
fn call_in_wasm<E: Externalities>(
function: &str,
call_data: &[u8],
@@ -17,12 +17,12 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use super::{call_in_wasm, TestExternalities};
use crate::{test_wasm_execution, WasmExecutionMethod};
use crate::{test_wasm_execution_sandbox, WasmExecutionMethod};
use codec::Encode;
test_wasm_execution!(sandbox_should_work);
fn sandbox_should_work(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(sandbox_should_work);
fn sandbox_should_work(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -51,11 +51,14 @@ fn sandbox_should_work(wasm_method: WasmExecutionMethod) {
.unwrap()
.encode();
assert_eq!(call_in_wasm("test_sandbox", &code, wasm_method, &mut ext).unwrap(), true.encode());
assert_eq!(
call_in_wasm(&format!("test_sandbox{}", fn_suffix), &code, wasm_method, &mut ext).unwrap(),
true.encode()
);
}
test_wasm_execution!(sandbox_trap);
fn sandbox_trap(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(sandbox_trap);
fn sandbox_trap(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -72,11 +75,14 @@ fn sandbox_trap(wasm_method: WasmExecutionMethod) {
)
.unwrap();
assert_eq!(call_in_wasm("test_sandbox", &code, wasm_method, &mut ext).unwrap(), vec![0]);
assert_eq!(
call_in_wasm(&format!("test_sandbox{}", fn_suffix), &code, wasm_method, &mut ext).unwrap(),
vec![0]
);
}
test_wasm_execution!(start_called);
fn start_called(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(start_called);
fn start_called(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -111,11 +117,14 @@ fn start_called(wasm_method: WasmExecutionMethod) {
.unwrap()
.encode();
assert_eq!(call_in_wasm("test_sandbox", &code, wasm_method, &mut ext).unwrap(), true.encode());
assert_eq!(
call_in_wasm(&format!("test_sandbox{}", fn_suffix), &code, wasm_method, &mut ext).unwrap(),
true.encode()
);
}
test_wasm_execution!(invoke_args);
fn invoke_args(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(invoke_args);
fn invoke_args(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -147,13 +156,14 @@ fn invoke_args(wasm_method: WasmExecutionMethod) {
.encode();
assert_eq!(
call_in_wasm("test_sandbox_args", &code, wasm_method, &mut ext,).unwrap(),
call_in_wasm(&format!("test_sandbox_args{}", fn_suffix), &code, wasm_method, &mut ext,)
.unwrap(),
true.encode(),
);
}
test_wasm_execution!(return_val);
fn return_val(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(return_val);
fn return_val(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -173,13 +183,19 @@ fn return_val(wasm_method: WasmExecutionMethod) {
.encode();
assert_eq!(
call_in_wasm("test_sandbox_return_val", &code, wasm_method, &mut ext,).unwrap(),
call_in_wasm(
&format!("test_sandbox_return_val{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
true.encode(),
);
}
test_wasm_execution!(unlinkable_module);
fn unlinkable_module(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(unlinkable_module);
fn unlinkable_module(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -197,13 +213,19 @@ fn unlinkable_module(wasm_method: WasmExecutionMethod) {
.encode();
assert_eq!(
call_in_wasm("test_sandbox_instantiate", &code, wasm_method, &mut ext,).unwrap(),
call_in_wasm(
&format!("test_sandbox_instantiate{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
1u8.encode(),
);
}
test_wasm_execution!(corrupted_module);
fn corrupted_module(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(corrupted_module);
fn corrupted_module(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -211,13 +233,19 @@ fn corrupted_module(wasm_method: WasmExecutionMethod) {
let code = vec![0u8, 0, 0, 0, 1, 0, 0, 0].encode();
assert_eq!(
call_in_wasm("test_sandbox_instantiate", &code, wasm_method, &mut ext,).unwrap(),
call_in_wasm(
&format!("test_sandbox_instantiate{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
1u8.encode(),
);
}
test_wasm_execution!(start_fn_ok);
fn start_fn_ok(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(start_fn_ok);
fn start_fn_ok(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -238,13 +266,19 @@ fn start_fn_ok(wasm_method: WasmExecutionMethod) {
.encode();
assert_eq!(
call_in_wasm("test_sandbox_instantiate", &code, wasm_method, &mut ext,).unwrap(),
call_in_wasm(
&format!("test_sandbox_instantiate{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
0u8.encode(),
);
}
test_wasm_execution!(start_fn_traps);
fn start_fn_traps(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(start_fn_traps);
fn start_fn_traps(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -266,13 +300,19 @@ fn start_fn_traps(wasm_method: WasmExecutionMethod) {
.encode();
assert_eq!(
call_in_wasm("test_sandbox_instantiate", &code, wasm_method, &mut ext,).unwrap(),
call_in_wasm(
&format!("test_sandbox_instantiate{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
2u8.encode(),
);
}
test_wasm_execution!(get_global_val_works);
fn get_global_val_works(wasm_method: WasmExecutionMethod) {
test_wasm_execution_sandbox!(get_global_val_works);
fn get_global_val_works(wasm_method: WasmExecutionMethod, fn_suffix: &str) {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
@@ -287,7 +327,13 @@ fn get_global_val_works(wasm_method: WasmExecutionMethod) {
.encode();
assert_eq!(
call_in_wasm("test_sandbox_get_global_val", &code, wasm_method, &mut ext,).unwrap(),
call_in_wasm(
&format!("test_sandbox_get_global_val{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
500i64.encode(),
);
}