1//! A VFS over the platform's, made the process's default by `install`, that fails what a
2//! test arms: once, the WAL's sync or commit frame; the replica's next `FAILING_OPENS` opens;
3//! or every WAL write of the database `FAILING` names, for as long as it names it.
4#![allow(dead_code)]
5
6use rusqlite::ffi;
7use std::{
8 ffi::{CStr, c_char, c_int, c_void},
9 path::PathBuf,
10 sync::{
11 Mutex, Once, OnceLock,
12 atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering},
13 },
14};
15
16/// The WAL's sync at commit fails before it reaches the disk.
17pub const SYNC_BEFORE: u8 = 1;
18/// The WAL's sync at commit reaches the disk and still reports failure.
19pub const SYNC_AFTER: u8 = 2;
20/// The commit frame's header is never written.
21pub const WRITE_COMMIT: u8 = 4;
22/// After a fault, the next sync fails, as the checkpoint closing the warm connection makes,
23/// which keeps the WAL.
24pub const KEEP_WAL: u8 = 16;
25
26pub static ARMED: AtomicU8 = AtomicU8::new(0);
27/// Between a fault and the next database open, as the replica reopens the cache cold.
28static FIRED: AtomicBool = AtomicBool::new(false);
29pub static WAL_WRITES_AFTER_FAULT: AtomicUsize = AtomicUsize::new(0);
30/// How many database opens on a replica's own threads still fail, as the cold reopen after a
31/// failed commit can, of the database `FAILING` names where it names one; a test's opens never do.
32pub static FAILING_OPENS: AtomicUsize = AtomicUsize::new(0);
33static REAL: OnceLock<usize> = OnceLock::new();
34/// The platform's WAL file methods, the shim's copy of them, and the copy failing every write.
35static WAL: OnceLock<(usize, usize, usize)> = OnceLock::new();
36/// While set, every write to this database's WAL fails, for WALs opened while it was set.
37pub static FAILING: Mutex<Option<PathBuf>> = Mutex::new(None);
38/// Held by a test for as long as it arms a fault, which one test does at a time.
39pub static EXCLUSIVE: Mutex<()> = Mutex::new(());
40
41fn fire(fault: u8) -> bool {
42 let fired = ARMED.fetch_and(!fault, Ordering::SeqCst) & fault != 0;
43 if fired {
44 FIRED.store(true, Ordering::SeqCst);
45 }
46 fired
47}
48
49fn real_wal() -> &'static ffi::sqlite3_io_methods {
50 unsafe { &*(WAL.get().unwrap().0 as *const ffi::sqlite3_io_methods) }
51}
52
53unsafe extern "C" fn open(
54 _: *mut ffi::sqlite3_vfs,
55 name: *const c_char,
56 file: *mut ffi::sqlite3_file,
57 flags: c_int,
58 out: *mut c_int,
59) -> c_int {
60 if flags & ffi::SQLITE_OPEN_MAIN_DB != 0 {
61 FIRED.store(false, Ordering::SeqCst);
62 let named = unsafe { CStr::from_ptr(name) }
63 .to_string_lossy()
64 .into_owned();
65 let chosen = FAILING
66 .lock()
67 .unwrap()
68 .as_ref()
69 .is_none_or(|database| named.starts_with(&*database.to_string_lossy()));
70 if chosen
71 && std::thread::current()
72 .name()
73 .is_some_and(|name| name.starts_with("onestore-"))
74 && FAILING_OPENS
75 .try_update(Ordering::SeqCst, Ordering::SeqCst, |opens| {
76 opens.checked_sub(1)
77 })
78 .is_ok()
79 {
80 unsafe { (*file).pMethods = std::ptr::null() };
81 return ffi::SQLITE_CANTOPEN;
82 }
83 }
84 let real = *REAL.get().unwrap() as *mut ffi::sqlite3_vfs;
85 let opened = unsafe { (*real).xOpen.unwrap()(real, name, file, flags, out) };
86 if opened == ffi::SQLITE_OK && flags & ffi::SQLITE_OPEN_WAL != 0 {
87 let methods = unsafe { (*file).pMethods };
88 let &(platform, shim, failing) = WAL.get_or_init(|| {
89 let mut shim = unsafe { *methods };
90 shim.xWrite = Some(write);
91 shim.xSync = Some(sync);
92 let mut failing = shim;
93 failing.xWrite = Some(fail);
94 let leak = |methods| Box::leak(Box::new(methods)) as *const _ as usize;
95 (methods as usize, leak(shim), leak(failing))
96 });
97 assert_eq!(platform, methods as usize);
98 let name = unsafe { CStr::from_ptr(name) }.to_string_lossy();
99 let fails = FAILING
100 .lock()
101 .unwrap()
102 .as_ref()
103 .is_some_and(|database| name.starts_with(&*database.to_string_lossy()));
104 let methods = if fails { failing } else { shim };
105 unsafe { (*file).pMethods = methods as *const ffi::sqlite3_io_methods };
106 }
107 opened
108}
109
110unsafe extern "C" fn write(
111 file: *mut ffi::sqlite3_file,
112 data: *const c_void,
113 amount: c_int,
114 offset: ffi::sqlite3_int64,
115) -> c_int {
116 // A frame header names the database size only in the frame that commits.
117 if amount == 24
118 && unsafe { std::slice::from_raw_parts(data.cast::<u8>().add(4), 4) } != [0; 4]
119 && fire(WRITE_COMMIT)
120 {
121 return ffi::SQLITE_IOERR_WRITE;
122 }
123 if FIRED.load(Ordering::SeqCst) {
124 WAL_WRITES_AFTER_FAULT.fetch_add(1, Ordering::SeqCst);
125 }
126 unsafe { real_wal().xWrite.unwrap()(file, data, amount, offset) }
127}
128
129unsafe extern "C" fn fail(
130 file: *mut ffi::sqlite3_file,
131 data: *const c_void,
132 amount: c_int,
133 offset: ffi::sqlite3_int64,
134) -> c_int {
135 if FAILING.lock().unwrap().is_some() {
136 return ffi::SQLITE_IOERR_WRITE;
137 }
138 unsafe { write(file, data, amount, offset) }
139}
140
141unsafe extern "C" fn sync(file: *mut ffi::sqlite3_file, flags: c_int) -> c_int {
142 if fire(SYNC_BEFORE) || FIRED.load(Ordering::SeqCst) && fire(KEEP_WAL) {
143 return ffi::SQLITE_IOERR_FSYNC;
144 }
145 let synced = unsafe { real_wal().xSync.unwrap()(file, flags) };
146 if synced == ffi::SQLITE_OK && fire(SYNC_AFTER) {
147 return ffi::SQLITE_IOERR_FSYNC;
148 }
149 synced
150}
151
152pub fn install() {
153 static INSTALL: Once = Once::new();
154 INSTALL.call_once(|| unsafe {
155 let real = ffi::sqlite3_vfs_find(std::ptr::null());
156 REAL.set(real as usize).unwrap();
157 let mut shim = *real;
158 shim.zName = c"commit-fault".as_ptr();
159 shim.pNext = std::ptr::null_mut();
160 shim.xOpen = Some(open);
161 assert_eq!(
162 ffi::sqlite3_vfs_register(Box::leak(Box::new(shim)), 1),
163 ffi::SQLITE_OK
164 );
165 });
166}