mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 23:57:56 +00:00
Clear WASM linear memory on other OSes besides Linux too (#10291)
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user