Path: blob/main/tests/disable_host_trap_handlers.rs
1685 views
//! A standalone test to assert that Wasmtime can operate in "no signal handlers1//! mode"2//!3//! This is a test for `Config::signals_based_traps(false)` which resides in its4//! own binary to assert properties about signal handlers that Wasmtime uses.5//! Due to the global nature of signals no other tests can be in this binary.6//! This will ensure that various trapping scenarios all work and additionally7//! signal handlers are not registered.89use anyhow::Result;10use wasmtime::{Config, Engine, Instance, Module, Store, Trap};1112#[test]13#[cfg_attr(miri, ignore)]14fn no_host_trap_handlers() -> Result<()> {15let mut config = Config::new();16config.signals_based_traps(false);17let engine = Engine::new(&config)?;18let module = Module::new(19&engine,20r#"21(module22(memory 1)2324(func (export "load") (param i32) (result i32)25(i32.load (local.get 0)))2627(func (export "div") (param i32 i32) (result i32)28(i32.div_s (local.get 0) (local.get 1)))2930(func (export "unreachable") unreachable)31(func $oflow (export "overflow") call $oflow)32)33"#,34)?;3536let mut store = Store::new(&engine, ());37let instance = Instance::new(&mut store, &module, &[])?;38let load = instance.get_typed_func::<i32, i32>(&mut store, "load")?;39let div = instance.get_typed_func::<(i32, i32), i32>(&mut store, "div")?;40let unreachable = instance.get_typed_func::<(), ()>(&mut store, "unreachable")?;41let overflow = instance.get_typed_func::<(), ()>(&mut store, "overflow")?;4243let trap = load44.call(&mut store, 1 << 20)45.unwrap_err()46.downcast::<Trap>()?;47assert_eq!(trap, Trap::MemoryOutOfBounds);4849let trap = div50.call(&mut store, (1, 0))51.unwrap_err()52.downcast::<Trap>()?;53assert_eq!(trap, Trap::IntegerDivisionByZero);5455let trap = unreachable56.call(&mut store, ())57.unwrap_err()58.downcast::<Trap>()?;59assert_eq!(trap, Trap::UnreachableCodeReached);6061let trap = overflow62.call(&mut store, ())63.unwrap_err()64.downcast::<Trap>()?;65assert_eq!(trap, Trap::StackOverflow);6667assert_host_signal_handlers_are_unset();6869Ok(())70}7172fn assert_host_signal_handlers_are_unset() {73#[cfg(unix)]74unsafe {75let mut prev = std::mem::zeroed::<libc::sigaction>();76let rc = libc::sigaction(libc::SIGILL, std::ptr::null(), &mut prev);77assert_eq!(rc, 0);78assert_eq!(79prev.sa_sigaction,80libc::SIG_DFL,81"fault handler was installed when it shouldn't have been"82);83}84#[cfg(windows)]85{86// Note that this can't be checked on Windows because vectored exception87// handlers work a bit differently and aren't as "global" as a signal88// handler. For now rely on the check above on unix to also guarantee89// that on Windows we don't register any vectored exception handlers.90}91}929394