Clear WASM linear memory on other OSes besides Linux too (#10291)

This commit is contained in:
Koute
2021-11-18 20:16:38 +09:00
committed by GitHub
parent 6e424d74ff
commit 15a0bfb0f6
3 changed files with 91 additions and 11 deletions
@@ -699,3 +699,81 @@ fn panic_in_spawned_instance_panics_on_joining_its_result(wasm_method: WasmExecu
assert!(format!("{}", error_result).contains("Spawned task"));
}
test_wasm_execution!(memory_is_cleared_between_invocations);
fn memory_is_cleared_between_invocations(wasm_method: WasmExecutionMethod) {
// This is based on the code generated by compiling a runtime *without*
// the `-C link-arg=--import-memory` using the following code and then
// disassembling the resulting blob with `wasm-dis`:
//
// ```
// #[no_mangle]
// #[cfg(not(feature = "std"))]
// pub fn returns_no_bss_mutable_static(_: *mut u8, _: usize) -> u64 {
// static mut COUNTER: usize = 0;
// let output = unsafe {
// COUNTER += 1;
// COUNTER as u64
// };
// sp_core::to_substrate_wasm_fn_return_value(&output)
// }
// ```
//
// This results in the BSS section to *not* be emitted, hence the executor has no way
// of knowing about the `static` variable's existence, so this test will fail if the linear
// memory is not properly cleared between invocations.
let binary = wat::parse_str(r#"
(module
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_i32_=>_i64 (func (param i32 i32) (result i64)))
(import "env" "ext_allocator_malloc_version_1" (func $ext_allocator_malloc_version_1 (param i32) (result i32)))
(global $__stack_pointer (mut i32) (i32.const 1048576))
(global $global$1 i32 (i32.const 1048580))
(global $global$2 i32 (i32.const 1048592))
(memory $0 17)
(export "memory" (memory $0))
(export "returns_no_bss_mutable_static" (func $returns_no_bss_mutable_static))
(export "__data_end" (global $global$1))
(export "__heap_base" (global $global$2))
(func $returns_no_bss_mutable_static (param $0 i32) (param $1 i32) (result i64)
(local $2 i32)
(local $3 i32)
(i32.store offset=1048576
(i32.const 0)
(local.tee $2
(i32.add
(i32.load offset=1048576 (i32.const 0))
(i32.const 1)
)
)
)
(i64.store
(local.tee $3
(call $ext_allocator_malloc_version_1 (i32.const 8))
)
(i64.extend_i32_u (local.get $2))
)
(i64.or
(i64.extend_i32_u (local.get $3))
(i64.const 34359738368)
)
)
)"#).unwrap();
let runtime = crate::wasm_runtime::create_wasm_runtime_with_code(
wasm_method,
1024,
RuntimeBlob::uncompress_if_needed(&binary[..]).unwrap(),
HostFunctions::host_functions(),
true,
None,
)
.unwrap();
let mut instance = runtime.new_instance().unwrap();
let res = instance.call_export("returns_no_bss_mutable_static", &[0]).unwrap();
assert_eq!(1, u64::decode(&mut &res[..]).unwrap());
let res = instance.call_export("returns_no_bss_mutable_static", &[0]).unwrap();
assert_eq!(1, u64::decode(&mut &res[..]).unwrap());
}