Catch panics on the FFI boundary between the runtime and the host for wasmtime (#11189)

* Catch panics on the FFI boundary between the runtime and the host for `wasmtime`

* Use an already existing test runtime function

* Merge the tests together
This commit is contained in:
Koute
2022-04-08 20:26:47 +09:00
committed by GitHub
parent 2a5c11e2cc
commit 1411e028aa
3 changed files with 39 additions and 10 deletions
@@ -113,7 +113,9 @@ sp_core::wasm_export_functions! {
}
}
fn test_exhaust_heap() -> Vec<u8> { Vec::with_capacity(16777216) }
fn test_allocate_vec(size: u32) -> Vec<u8> {
Vec::with_capacity(size as usize)
}
fn test_fp_f32add(a: [u8; 4], b: [u8; 4]) -> [u8; 4] {
let a = f32::from_le_bytes(a);
@@ -466,13 +466,24 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
&mut ext.ext(),
true,
"test_exhaust_heap",
&[0],
"test_allocate_vec",
&16777216_u32.encode(),
)
.map_err(|e| e.to_string())
.unwrap_err();
assert!(err.contains("Allocator ran out of space"));
match err {
#[cfg(feature = "wasmtime")]
Error::AbortedDueToTrap(error) if wasm_method == WasmExecutionMethod::Compiled => {
assert_eq!(
error.message,
r#"host code panicked while being called by the runtime: Failed to allocate memory: "Allocator ran out of space""#
);
},
Error::RuntimePanicked(error) if wasm_method == WasmExecutionMethod::Interpreted => {
assert_eq!(error, r#"Failed to allocate memory: "Allocator ran out of space""#);
},
error => panic!("unexpected error: {:?}", error),
}
}
fn mk_test_runtime(wasm_method: WasmExecutionMethod, pages: u64) -> Arc<dyn WasmModule> {