//! Checked builds: invariants of generated code that nothing else //! verifies, checked at run time (issue #217). Debug builds of generated //! code define `TL_CHECKED` and call [`tl_checked`] first; release builds //! do neither, and pay nothing. //! //! - **Externs marked as never unwinding.** The compiler's extern table //! (`louise_core/src/externs.rs`) marks most runtime externs as never //! unwinding, and generated code has no yield check after a call of //! one. A wrong mark is silent: if the extern panics or parks after //! all, the code after the call runs on. Checked code wraps each such //! call in [`tl_no_unwind_enter`] and [`tl_no_unwind_exit`] //! (`TL_NO_UNWIND` in `louise_rt.h`), and a panic or a park meanwhile //! aborts, naming the extern. //! - **`xargs`.** Arguments after the sixth go through the running //! thread's slots, and nothing may suspend between a caller's stores //! and the callee's copy (docs/runtime-abi.md). Once [`tl_checked`] has //! run, every slice poisons the slots, and a checked callee that copies //! a poisoned slot aborts (`TL_XARG`). use std::cell::Cell; use std::ffi::{CStr, c_char}; use std::sync::atomic::{AtomicBool, Ordering}; use crate::abi::Value; /// Never a value: not a small int (odd) nor an aligned pointer. pub const XARGS_POISON: Value = 2; static CHECKED: AtomicBool = AtomicBool::new(false); thread_local! { /// The outermost extern marked as never unwinding that is running on /// this thread, and how many are nested in it. static CALLING: Cell<(*const c_char, u32)> = const { Cell::new((std::ptr::null(), 0)) }; } /// The program's code is checked: poison `xargs` from now on. #[unsafe(no_mangle)] pub extern "C" fn tl_checked() { CHECKED.store(true, Ordering::Relaxed); } /// Poison the slots `xargs` points at, if the program's code is checked. /// /// # Safety /// `xargs` must point at the running thread's slots. pub(crate) unsafe fn poison_xargs(xargs: *mut Value) { if CHECKED.load(Ordering::Relaxed) { // SAFETY: caller guarantees the thread's slots. unsafe { std::slice::from_raw_parts_mut(xargs, crate::abi::XARGS).fill(XARGS_POISON) }; } } /// A checked callee copied a poisoned argument slot: something suspended /// between its caller's stores and its copy. #[unsafe(no_mangle)] pub extern "C" fn tl_xargs_poisoned(slot: i64) -> ! { abort(&format!( "louise: argument slot {slot} was read after the actor suspended: the call stored its arguments, then something suspended before the callee copied them" )) } /// A call of `symbol`, marked as never unwinding, starts. /// /// # Safety /// `symbol` must be a NUL-terminated string that outlives the call. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_no_unwind_enter(symbol: *const c_char) { CALLING.with(|c| { let (outer, depth) = c.get(); c.set((if depth == 0 { symbol } else { outer }, depth + 1)); }); } /// The call started by the matching [`tl_no_unwind_enter`] returned. #[unsafe(no_mangle)] pub extern "C" fn tl_no_unwind_exit() { CALLING.with(|c| { let (outer, depth) = c.get(); let depth = depth.saturating_sub(1); c.set((if depth == 0 { std::ptr::null() } else { outer }, depth)); }); } /// Abort if an extern marked as never unwinding is running: it is about /// to do `what`, which its mark says it never does. pub(crate) fn no_unwind(what: &str) { let (symbol, _) = CALLING.with(Cell::get); if symbol.is_null() { return; } // SAFETY: `tl_no_unwind_enter`'s caller guarantees a live C string. let name = unsafe { CStr::from_ptr(symbol) }.to_string_lossy(); abort(&format!( "louise: `{name}` is marked as never unwinding (louise_core/src/externs.rs), but it {what}" )); } fn abort(message: &str) -> ! { crate::console::flush_before_abort(); eprintln!("{message}"); std::process::abort(); }