makepad/libs/relax/compiler/tests/modules/host_hook_panic.rs
Admin b22a714840 makepad run: the Rust JIT (libs/relax) and makepad-remote
The JIT that runs makepad apps from source: lexer, front end, type checker,
lazy borrow checker (Gandalf; `--check-first` opts in), codegen, session and
warm/cold caches keyed by compiler stamp, provided crates bound late,
startup images, live patching. User-facing name is `makepad run` (binary
`makepad`, MAKEPAD_RUN_* env, ~/.makepad-run); libs/relax stays the internal
library name. `makepad check` reports rustc-style diagnostics with rustc
error codes and checks a whole workspace (`--workspace`). Runs on macOS,
Linux, Windows and Android. makepad-remote: one tool schema for apps and the
WM (app/1): grabs, captures, traces, frame profiles and tweaks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-13 12:26:21 +02:00

31 lines
1.5 KiB
Rust

// Run in a fresh process: populate the host hook before the first sandbox placement.
#[test]
fn panic_after_host_catch_and_hook() {
std::panic::catch_unwind(|| ()).unwrap();
// A populated, heap-owning std hook must not enter the sandbox image either.
let label = String::from("host hook");
let calls = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
let hook_calls = calls.clone();
std::panic::set_hook(Box::new(move |_| {
assert_eq!(label.len(), 9);
hook_calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}));
let module = std::relax::Module::load_source("panic_after_catch", "panic_after_catch.rs", r#"
static INITIAL: u64 = 37;
static TEXT: &str = "module-local";
pub fn value() -> u64 { INITIAL + TEXT.len() as u64 }
pub fn fail() { panic!("contained module panic"); }
"#, &[]).unwrap();
let value: fn() -> u64 = module.get("value").unwrap();
let fail: fn() = module.get("fail").unwrap();
assert_eq!(std::relax::guard(1000, || value()).unwrap(), 49);
assert!(std::relax::guard(1000, || fail()).is_err());
assert_eq!(std::relax::guard(1000, || value()).unwrap(), 49);
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);
// The host retains its independent hook/TLS and can still catch its own panic.
assert!(std::panic::catch_unwind(|| panic!("host still running")).is_err());
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
let _ = std::panic::take_hook();
module.unload();
}