tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

103 lignes · 3945 octetsBrut · Historique
1//! Checked builds: invariants of generated code that nothing else
2//! verifies, checked at run time (issue #217). Debug builds of generated
3//! code define `TL_CHECKED` and call [`tl_checked`] first; release builds
4//! do neither, and pay nothing.
5//!
6//! - **Externs marked as never unwinding.** The compiler's extern table
7//! (`louise_core/src/externs.rs`) marks most runtime externs as never
8//! unwinding, and generated code has no yield check after a call of
9//! one. A wrong mark is silent: if the extern panics or parks after
10//! all, the code after the call runs on. Checked code wraps each such
11//! call in [`tl_no_unwind_enter`] and [`tl_no_unwind_exit`]
12//! (`TL_NO_UNWIND` in `louise_rt.h`), and a panic or a park meanwhile
13//! aborts, naming the extern.
14//! - **`xargs`.** Arguments after the sixth go through the running
15//! thread's slots, and nothing may suspend between a caller's stores
16//! and the callee's copy (docs/runtime-abi.md). Once [`tl_checked`] has
17//! run, every slice poisons the slots, and a checked callee that copies
18//! a poisoned slot aborts (`TL_XARG`).
19
20use std::cell::Cell;
21use std::ffi::{CStr, c_char};
22use std::sync::atomic::{AtomicBool, Ordering};
23
24use crate::abi::Value;
25
26/// Never a value: not a small int (odd) nor an aligned pointer.
27pub const XARGS_POISON: Value = 2;
28
29static CHECKED: AtomicBool = AtomicBool::new(false);
30
31thread_local! {
32 /// The outermost extern marked as never unwinding that is running on
33 /// this thread, and how many are nested in it.
34 static CALLING: Cell<(*const c_char, u32)> = const { Cell::new((std::ptr::null(), 0)) };
35}
36
37/// The program's code is checked: poison `xargs` from now on.
38#[unsafe(no_mangle)]
39pub extern "C" fn tl_checked() {
40 CHECKED.store(true, Ordering::Relaxed);
41}
42
43/// Poison the slots `xargs` points at, if the program's code is checked.
44///
45/// # Safety
46/// `xargs` must point at the running thread's slots.
47pub(crate) unsafe fn poison_xargs(xargs: *mut Value) {
48 if CHECKED.load(Ordering::Relaxed) {
49 // SAFETY: caller guarantees the thread's slots.
50 unsafe { std::slice::from_raw_parts_mut(xargs, crate::abi::XARGS).fill(XARGS_POISON) };
51 }
52}
53
54/// A checked callee copied a poisoned argument slot: something suspended
55/// between its caller's stores and its copy.
56#[unsafe(no_mangle)]
57pub extern "C" fn tl_xargs_poisoned(slot: i64) -> ! {
58 abort(&format!(
59 "louise: argument slot {slot} was read after the actor suspended: the call stored its arguments, then something suspended before the callee copied them"
60 ))
61}
62
63/// A call of `symbol`, marked as never unwinding, starts.
64///
65/// # Safety
66/// `symbol` must be a NUL-terminated string that outlives the call.
67#[unsafe(no_mangle)]
68pub unsafe extern "C" fn tl_no_unwind_enter(symbol: *const c_char) {
69 CALLING.with(|c| {
70 let (outer, depth) = c.get();
71 c.set((if depth == 0 { symbol } else { outer }, depth + 1));
72 });
73}
74
75/// The call started by the matching [`tl_no_unwind_enter`] returned.
76#[unsafe(no_mangle)]
77pub extern "C" fn tl_no_unwind_exit() {
78 CALLING.with(|c| {
79 let (outer, depth) = c.get();
80 let depth = depth.saturating_sub(1);
81 c.set((if depth == 0 { std::ptr::null() } else { outer }, depth));
82 });
83}
84
85/// Abort if an extern marked as never unwinding is running: it is about
86/// to do `what`, which its mark says it never does.
87pub(crate) fn no_unwind(what: &str) {
88 let (symbol, _) = CALLING.with(Cell::get);
89 if symbol.is_null() {
90 return;
91 }
92 // SAFETY: `tl_no_unwind_enter`'s caller guarantees a live C string.
93 let name = unsafe { CStr::from_ptr(symbol) }.to_string_lossy();
94 abort(&format!(
95 "louise: `{name}` is marked as never unwinding (louise_core/src/externs.rs), but it {what}"
96 ));
97}
98
99fn abort(message: &str) -> ! {
100 crate::console::flush_before_abort();
101 eprintln!("{message}");
102 std::process::abort();
103}