forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

fault.rs61 lines, 1.9 KiBHistoryRawDownload
1//! Test-only fault injection for the threaded decode (lane-av1panichang).
2//!
3//! A worker calls [`point`] at a named site; once [`arm`]ed, the `nth` time
4//! that site is reached it panics, and the plan disarms itself. Disarmed (the
5//! default) a point is one relaxed atomic load. Hidden from the docs: it exists
6//! so `tests/worker_panic.rs` can prove a panicking worker never hangs the
7//! decode and that the next decode in the same process still works.
8
9use std::sync::Mutex;
10use std::sync::atomic::{AtomicBool, Ordering};
11
12static ARMED: AtomicBool = AtomicBool::new(false);
13static PLAN: Mutex<Option<(&'static str, usize)>> = Mutex::new(None);
14
15fn plan() -> std::sync::MutexGuard<'static, Option<(&'static str, usize)>> {
16 PLAN.lock()
17 .unwrap_or_else(std::sync::PoisonError::into_inner)
18}
19
20/// Panics the `nth` (1-based) time a worker reaches `site`, once.
21#[doc(hidden)]
22pub fn arm(site: &'static str, nth: usize) {
23 assert!(nth >= 1, "fault::arm: nth is 1-based");
24 *plan() = Some((site, nth));
25 ARMED.store(true, Ordering::SeqCst);
26}
27
28/// Clears any armed plan.
29#[doc(hidden)]
30pub fn disarm() {
31 *plan() = None;
32 ARMED.store(false, Ordering::SeqCst);
33}
34
35/// The sites a worker reports: `frame` (inside a frame job's own catch),
36/// `frame-after` (past it, unwinding through the job), `recon` (a wavefront
37/// job).
38pub(crate) fn point(site: &'static str) {
39 if !ARMED.load(Ordering::Relaxed) {
40 return;
41 }
42 let fire = {
43 let mut p = plan();
44 match p.as_mut() {
45 Some((s, n)) if *s == site => {
46 *n -= 1;
47 if *n == 0 {
48 *p = None;
49 ARMED.store(false, Ordering::SeqCst);
50 true
51 } else {
52 false
53 }
54 }
55 _ => false,
56 }
57 };
58 if fire {
59 panic!("injected worker panic at fault site `{site}`");
60 }
61}