Remove sandboxing host function interface (#12852)

* Remove sandboxing interface

* Remove unused struct
This commit is contained in:
Alexander Theißen
2022-12-07 13:48:30 +01:00
committed by GitHub
parent 198faaa6f9
commit 32578cb010
31 changed files with 34 additions and 4478 deletions
@@ -18,7 +18,6 @@
#[cfg(target_os = "linux")]
mod linux;
mod sandbox;
use codec::{Decode, Encode};
use sc_executor_common::{error::Error, runtime_blob::RuntimeBlob, wasm_runtime::WasmModule};
@@ -98,111 +97,6 @@ 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]
fn [<$method_name _compiled_pooling_cow_host_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::PoolingCopyOnWrite
}, "_host");
}
#[test]
fn [<$method_name _compiled_pooling_cow_embedded_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::PoolingCopyOnWrite
}, "_embedded");
}
#[test]
fn [<$method_name _compiled_pooling_vanilla_host_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::Pooling
}, "_host");
}
#[test]
fn [<$method_name _compiled_pooling_vanilla_embedded_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::Pooling
}, "_embedded");
}
#[test]
fn [<$method_name _compiled_recreate_instance_cow_host_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::RecreateInstanceCopyOnWrite
}, "_host");
}
#[test]
fn [<$method_name _compiled_recreate_instance_cow_embedded_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::RecreateInstanceCopyOnWrite
}, "_embedded");
}
#[test]
fn [<$method_name _compiled_recreate_instance_vanilla_host_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::RecreateInstance
}, "_host");
}
#[test]
fn [<$method_name _compiled_recreate_instance_vanilla_embedded_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::RecreateInstance
}, "_embedded");
}
#[test]
fn [<$method_name _compiled_legacy_instance_reuse_host_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::LegacyInstanceReuse
}, "_host");
}
#[test]
fn [<$method_name _compiled_legacy_instance_reuse_embedded_executor>]() {
$method_name(WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::LegacyInstanceReuse
}, "_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],
@@ -1,339 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2018-2022 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 super::{call_in_wasm, TestExternalities};
use crate::{test_wasm_execution_sandbox, WasmExecutionMethod};
use codec::Encode;
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();
let code = wat::parse_str(
r#"
(module
(import "env" "assert" (func $assert (param i32)))
(import "env" "inc_counter" (func $inc_counter (param i32) (result i32)))
(func (export "call")
(drop
(call $inc_counter (i32.const 5))
)
(call $inc_counter (i32.const 3))
;; current counter value is on the stack
;; check whether current == 8
i32.const 8
i32.eq
call $assert
)
)
"#,
)
.unwrap()
.encode();
assert_eq!(
call_in_wasm(&format!("test_sandbox{}", fn_suffix), &code, wasm_method, &mut ext).unwrap(),
true.encode()
);
}
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();
let code = wat::parse_str(
r#"
(module
(import "env" "assert" (func $assert (param i32)))
(func (export "call")
i32.const 0
call $assert
)
)
"#,
)
.unwrap();
assert_eq!(
call_in_wasm(&format!("test_sandbox{}", fn_suffix), &code, wasm_method, &mut ext).unwrap(),
vec![0]
);
}
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();
let code = wat::parse_str(
r#"
(module
(import "env" "assert" (func $assert (param i32)))
(import "env" "inc_counter" (func $inc_counter (param i32) (result i32)))
;; Start function
(start $start)
(func $start
;; Increment counter by 1
(drop
(call $inc_counter (i32.const 1))
)
)
(func (export "call")
;; Increment counter by 1. The current value is placed on the stack.
(call $inc_counter (i32.const 1))
;; Counter is incremented twice by 1, once there and once in `start` func.
;; So check the returned value is equal to 2.
i32.const 2
i32.eq
call $assert
)
)
"#,
)
.unwrap()
.encode();
assert_eq!(
call_in_wasm(&format!("test_sandbox{}", fn_suffix), &code, wasm_method, &mut ext).unwrap(),
true.encode()
);
}
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();
let code = wat::parse_str(
r#"
(module
(import "env" "assert" (func $assert (param i32)))
(func (export "call") (param $x i32) (param $y i64)
;; assert that $x = 0x12345678
(call $assert
(i32.eq
(get_local $x)
(i32.const 0x12345678)
)
)
(call $assert
(i64.eq
(get_local $y)
(i64.const 0x1234567887654321)
)
)
)
)
"#,
)
.unwrap()
.encode();
assert_eq!(
call_in_wasm(&format!("test_sandbox_args{}", fn_suffix), &code, wasm_method, &mut ext,)
.unwrap(),
true.encode(),
);
}
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();
let code = wat::parse_str(
r#"
(module
(func (export "call") (param $x i32) (result i32)
(i32.add
(get_local $x)
(i32.const 1)
)
)
)
"#,
)
.unwrap()
.encode();
assert_eq!(
call_in_wasm(
&format!("test_sandbox_return_val{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
true.encode(),
);
}
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();
let code = wat::parse_str(
r#"
(module
(import "env" "non-existent" (func))
(func (export "call")
)
)
"#,
)
.unwrap()
.encode();
assert_eq!(
call_in_wasm(
&format!("test_sandbox_instantiate{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
1u8.encode(),
);
}
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();
// Corrupted wasm file
let code = vec![0u8, 0, 0, 0, 1, 0, 0, 0].encode();
assert_eq!(
call_in_wasm(
&format!("test_sandbox_instantiate{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
1u8.encode(),
);
}
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();
let code = wat::parse_str(
r#"
(module
(func (export "call")
)
(func $start
)
(start $start)
)
"#,
)
.unwrap()
.encode();
assert_eq!(
call_in_wasm(
&format!("test_sandbox_instantiate{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
0u8.encode(),
);
}
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();
let code = wat::parse_str(
r#"
(module
(func (export "call")
)
(func $start
unreachable
)
(start $start)
)
"#,
)
.unwrap()
.encode();
assert_eq!(
call_in_wasm(
&format!("test_sandbox_instantiate{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
2u8.encode(),
);
}
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();
let code = wat::parse_str(
r#"
(module
(global (export "test_global") i64 (i64.const 500))
)
"#,
)
.unwrap()
.encode();
assert_eq!(
call_in_wasm(
&format!("test_sandbox_get_global_val{}", fn_suffix),
&code,
wasm_method,
&mut ext,
)
.unwrap(),
500i64.encode(),
);
}