From e22d93f750b1f8f0c10d0b21dbe1e2f6c26e69e7 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 9 Jan 2020 14:21:56 -0800 Subject: [PATCH] Tidy up the custom signal handler test No need to check raw `*.wasm` files into the repo, and `*.wat` files can typically be written inline in the test to avoid tabbing back and forth. --- tests/custom_signal_handler.rs | 83 ++++++++++++++++++----------- tests/custom_signal_handler.wasm | Bin 108 -> 0 bytes tests/custom_signal_handler.wat | 20 ------- tests/custom_signal_handler_2.wasm | Bin 59 -> 0 bytes tests/custom_signal_handler_2.wat | 5 -- 5 files changed, 51 insertions(+), 57 deletions(-) delete mode 100644 tests/custom_signal_handler.wasm delete mode 100644 tests/custom_signal_handler.wat delete mode 100644 tests/custom_signal_handler_2.wasm delete mode 100644 tests/custom_signal_handler_2.wat diff --git a/tests/custom_signal_handler.rs b/tests/custom_signal_handler.rs index 0b45f69f0a..613bb2db04 100644 --- a/tests/custom_signal_handler.rs +++ b/tests/custom_signal_handler.rs @@ -6,14 +6,43 @@ mod tests { use wasmtime::*; use wasmtime_interface_types::{ModuleData, Value}; + const WAT1: &str = r#" +(module + (func $read (export "read") (result i32) + (i32.load (i32.const 0)) + ) + (func $read_out_of_bounds (export "read_out_of_bounds") (result i32) + (i32.load + (i32.mul + ;; memory size in Wasm pages + (memory.size) + ;; Wasm page size + (i32.const 65536) + ) + ) + ) + (func $start + (i32.store (i32.const 0) (i32.const 123)) + ) + (start $start) + (memory (export "memory") 1 4) +) +"#; + + const WAT2: &str = r#" +(module + (import "other_module" "read" (func $other_module.read (result i32))) + (func $run (export "run") (result i32) + call $other_module.read) +) +"#; + fn invoke_export( instance: &HostRef, data: &[u8], func_name: &str, ) -> Result, anyhow::Error> { - ModuleData::new(&data) - .expect("module data") - .invoke_export(instance, func_name, &[]) + ModuleData::new(&data)?.invoke_export(instance, func_name, &[]) } // Locate "memory" export, get base address and size and set memory protection to PROT_NONE @@ -73,15 +102,12 @@ mod tests { } #[test] - fn test_custom_signal_handler_single_instance() { + fn test_custom_signal_handler_single_instance() -> anyhow::Result<()> { let engine = Engine::new(&Config::default()); let store = Store::new(&engine); - let data = - std::fs::read("tests/custom_signal_handler.wasm").expect("failed to read wasm file"); - let module = Module::new(&store, &data).expect("failed to create module"); - let instance = HostRef::new( - Instance::new(&store, &module, &[]).expect("failed to instantiate module"), - ); + let data = wat::parse_str(WAT1)?; + let module = Module::new(&store, &data)?; + let instance = HostRef::new(Instance::new(&store, &module, &[])?); let (base, length) = set_up_memory(&instance); instance @@ -131,21 +157,19 @@ mod tests { .message() .starts_with("call error: wasm trap: out of bounds memory access")); } + Ok(()) } #[test] - fn test_custom_signal_handler_multiple_instances() { + fn test_custom_signal_handler_multiple_instances() -> anyhow::Result<()> { let engine = Engine::new(&Config::default()); let store = Store::new(&engine); - let data = - std::fs::read("tests/custom_signal_handler.wasm").expect("failed to read wasm file"); - let module = Module::new(&store, &data).expect("failed to create module"); + let data = wat::parse_str(WAT1)?; + let module = Module::new(&store, &data)?; // Set up multiple instances - let instance1 = HostRef::new( - Instance::new(&store, &module, &[]).expect("failed to instantiate module"), - ); + let instance1 = HostRef::new(Instance::new(&store, &module, &[])?); let instance1_handler_triggered = Rc::new(AtomicBool::new(false)); { @@ -232,20 +256,18 @@ mod tests { "instance1 signal handler has been triggered" ); } + Ok(()) } #[test] - fn test_custom_signal_handler_instance_calling_another_instance() { + fn test_custom_signal_handler_instance_calling_another_instance() -> anyhow::Result<()> { let engine = Engine::new(&Config::default()); let store = Store::new(&engine); // instance1 which defines 'read' - let data1 = - std::fs::read("tests/custom_signal_handler.wasm").expect("failed to read wasm file"); - let module1 = Module::new(&store, &data1).expect("failed to create module"); - let instance1: HostRef = HostRef::new( - Instance::new(&store, &module1, &[]).expect("failed to instantiate module"), - ); + let data1 = wat::parse_str(WAT1)?; + let module1 = Module::new(&store, &data1)?; + let instance1: HostRef = HostRef::new(Instance::new(&store, &module1, &[])?); let (base1, length1) = set_up_memory(&instance1); instance1 .borrow_mut() @@ -259,13 +281,9 @@ mod tests { let instance1_read = instance1_exports[0].clone(); // instance2 wich calls 'instance1.read' - let data2 = - std::fs::read("tests/custom_signal_handler_2.wasm").expect("failed to read wasm file"); - let module2 = Module::new(&store, &data2).expect("failed to create module"); - let instance2 = HostRef::new( - Instance::new(&store, &module2, &[instance1_read]) - .expect("failed to instantiate module"), - ); + let data2 = wat::parse_str(WAT2)?; + let module2 = Module::new(&store, &data2)?; + let instance2 = HostRef::new(Instance::new(&store, &module2, &[instance1_read])?); // since 'instance2.run' calls 'instance1.read' we need to set up the signal handler to handle // SIGSEGV originating from within the memory of instance1 instance2 @@ -275,7 +293,8 @@ mod tests { }); println!("calling instance2.run"); - let result = invoke_export(&instance2, &data2, "run").expect("instance2.run succeeded"); + let result = invoke_export(&instance2, &data2, "run")?; assert_eq!("123", result[0].clone().to_string()); + Ok(()) } } diff --git a/tests/custom_signal_handler.wasm b/tests/custom_signal_handler.wasm deleted file mode 100644 index 9348e1a61e406405d22c64d8f06f42eb77f0d49f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 108 zcmWlNJr05}7(n0GehNV+Z{T7)fh*}r3Iv#_O@Izga&H#O^nPfUAb^ynLH-R`wK*>; zB3itx&O_V7OrFOy7P%u3-pq{uxR!)zcO@-iF(q8nR;`(>e7%HW-d NQNHyXO``%U^ABNU3X=c; diff --git a/tests/custom_signal_handler_2.wat b/tests/custom_signal_handler_2.wat deleted file mode 100644 index a93c5db8d0..0000000000 --- a/tests/custom_signal_handler_2.wat +++ /dev/null @@ -1,5 +0,0 @@ -(module - (import "other_module" "read" (func $other_module.read (result i32))) - (func $run (export "run") (result i32) - call $other_module.read) -)