| 1 | // Copyright 2026 Deno Land Inc. Apache-2.0 license. |
| 2 | |
| 3 | //! Test-only SQLite fault injection: a wrapper VFS that delegates to the |
| 4 | //! process default VFS but fails writes with `SQLITE_IOERR` while the current |
| 5 | //! thread has armed the fault. Compiled only into test builds — |
| 6 | //! production binaries contain none of this code and no seam for it. |
| 7 | use rusqlite::ffi; |
| 8 | use std::cell::Cell; |
| 9 | use std::ffi::c_char; |
| 10 | use std::ffi::c_int; |
| 11 | use std::ffi::c_void; |
| 12 | use std::mem::size_of; |
| 13 | use std::sync::Once; |
| 14 | |
| 15 | const FAULT_VFS_NAME: &std::ffi::CStr = c"cells-fault-vfs"; |
| 16 | |
| 17 | thread_local! { |
| 18 | static WRITE_FAULT: Cell<bool> = const { Cell::new(false) }; |
| 19 | } |
| 20 | |
| 21 | pub fn set_write_fault(enabled: bool) { |
| 22 | WRITE_FAULT.with(|flag| flag.set(enabled)); |
| 23 | } |
| 24 | |
| 25 | fn write_fault_armed() -> bool { |
| 26 | WRITE_FAULT.try_with(Cell::get).unwrap_or(false) |
| 27 | } |
| 28 | |
| 29 | /// The shim's per-file state: SQLite allocates `szOsFile` bytes, we use the |
| 30 | /// front for this header and hand the tail to the real VFS as its file. |
| 31 | #[repr(C)] |
| 32 | struct ShimFile { |
| 33 | base: ffi::sqlite3_file, |
| 34 | real: *mut ffi::sqlite3_file, |
| 35 | } |
| 36 | |
| 37 | unsafe fn real_file(file: *mut ffi::sqlite3_file) -> *mut ffi::sqlite3_file { |
| 38 | (*file.cast::<ShimFile>()).real |
| 39 | } |
| 40 | |
| 41 | unsafe fn real_vfs(vfs: *mut ffi::sqlite3_vfs) -> *mut ffi::sqlite3_vfs { |
| 42 | (*vfs).pAppData.cast() |
| 43 | } |
| 44 | |
| 45 | macro_rules! file_delegate { |
| 46 | ($file:ident, $method:ident $(, $arg:expr)*) => {{ |
| 47 | let real = real_file($file); |
| 48 | ((*(*real).pMethods).$method.expect("real io method"))(real $(, $arg)*) |
| 49 | }}; |
| 50 | } |
| 51 | |
| 52 | macro_rules! vfs_delegate { |
| 53 | ($vfs:ident, $method:ident $(, $arg:expr)*) => {{ |
| 54 | let real = real_vfs($vfs); |
| 55 | ((*real).$method.expect("real vfs method"))(real $(, $arg)*) |
| 56 | }}; |
| 57 | } |
| 58 | |
| 59 | unsafe extern "C" fn shim_close(file: *mut ffi::sqlite3_file) -> c_int { |
| 60 | file_delegate!(file, xClose) |
| 61 | } |
| 62 | unsafe extern "C" fn shim_read( |
| 63 | file: *mut ffi::sqlite3_file, |
| 64 | buf: *mut c_void, |
| 65 | amt: c_int, |
| 66 | offset: i64, |
| 67 | ) -> c_int { |
| 68 | file_delegate!(file, xRead, buf, amt, offset) |
| 69 | } |
| 70 | unsafe extern "C" fn shim_write( |
| 71 | file: *mut ffi::sqlite3_file, |
| 72 | buf: *const c_void, |
| 73 | amt: c_int, |
| 74 | offset: i64, |
| 75 | ) -> c_int { |
| 76 | if write_fault_armed() { |
| 77 | return ffi::SQLITE_IOERR; |
| 78 | } |
| 79 | file_delegate!(file, xWrite, buf, amt, offset) |
| 80 | } |
| 81 | unsafe extern "C" fn shim_truncate(file: *mut ffi::sqlite3_file, size: i64) -> c_int { |
| 82 | file_delegate!(file, xTruncate, size) |
| 83 | } |
| 84 | unsafe extern "C" fn shim_sync(file: *mut ffi::sqlite3_file, flags: c_int) -> c_int { |
| 85 | file_delegate!(file, xSync, flags) |
| 86 | } |
| 87 | unsafe extern "C" fn shim_file_size(file: *mut ffi::sqlite3_file, size: *mut i64) -> c_int { |
| 88 | file_delegate!(file, xFileSize, size) |
| 89 | } |
| 90 | unsafe extern "C" fn shim_lock(file: *mut ffi::sqlite3_file, level: c_int) -> c_int { |
| 91 | file_delegate!(file, xLock, level) |
| 92 | } |
| 93 | unsafe extern "C" fn shim_unlock(file: *mut ffi::sqlite3_file, level: c_int) -> c_int { |
| 94 | file_delegate!(file, xUnlock, level) |
| 95 | } |
| 96 | unsafe extern "C" fn shim_check_reserved_lock( |
| 97 | file: *mut ffi::sqlite3_file, |
| 98 | out: *mut c_int, |
| 99 | ) -> c_int { |
| 100 | file_delegate!(file, xCheckReservedLock, out) |
| 101 | } |
| 102 | unsafe extern "C" fn shim_file_control( |
| 103 | file: *mut ffi::sqlite3_file, |
| 104 | op: c_int, |
| 105 | arg: *mut c_void, |
| 106 | ) -> c_int { |
| 107 | file_delegate!(file, xFileControl, op, arg) |
| 108 | } |
| 109 | unsafe extern "C" fn shim_sector_size(file: *mut ffi::sqlite3_file) -> c_int { |
| 110 | file_delegate!(file, xSectorSize) |
| 111 | } |
| 112 | unsafe extern "C" fn shim_device_characteristics(file: *mut ffi::sqlite3_file) -> c_int { |
| 113 | file_delegate!(file, xDeviceCharacteristics) |
| 114 | } |
| 115 | unsafe extern "C" fn shim_shm_map( |
| 116 | file: *mut ffi::sqlite3_file, |
| 117 | region: c_int, |
| 118 | size: c_int, |
| 119 | extend: c_int, |
| 120 | out: *mut *mut c_void, |
| 121 | ) -> c_int { |
| 122 | file_delegate!(file, xShmMap, region, size, extend, out) |
| 123 | } |
| 124 | unsafe extern "C" fn shim_shm_lock( |
| 125 | file: *mut ffi::sqlite3_file, |
| 126 | offset: c_int, |
| 127 | count: c_int, |
| 128 | flags: c_int, |
| 129 | ) -> c_int { |
| 130 | file_delegate!(file, xShmLock, offset, count, flags) |
| 131 | } |
| 132 | unsafe extern "C" fn shim_shm_barrier(file: *mut ffi::sqlite3_file) { |
| 133 | file_delegate!(file, xShmBarrier) |
| 134 | } |
| 135 | unsafe extern "C" fn shim_shm_unmap(file: *mut ffi::sqlite3_file, delete_flag: c_int) -> c_int { |
| 136 | file_delegate!(file, xShmUnmap, delete_flag) |
| 137 | } |
| 138 | unsafe extern "C" fn shim_fetch( |
| 139 | file: *mut ffi::sqlite3_file, |
| 140 | offset: i64, |
| 141 | amt: c_int, |
| 142 | out: *mut *mut c_void, |
| 143 | ) -> c_int { |
| 144 | file_delegate!(file, xFetch, offset, amt, out) |
| 145 | } |
| 146 | unsafe extern "C" fn shim_unfetch( |
| 147 | file: *mut ffi::sqlite3_file, |
| 148 | offset: i64, |
| 149 | page: *mut c_void, |
| 150 | ) -> c_int { |
| 151 | file_delegate!(file, xUnfetch, offset, page) |
| 152 | } |
| 153 | |
| 154 | static SHIM_IO_METHODS: ffi::sqlite3_io_methods = ffi::sqlite3_io_methods { |
| 155 | iVersion: 3, |
| 156 | xClose: Some(shim_close), |
| 157 | xRead: Some(shim_read), |
| 158 | xWrite: Some(shim_write), |
| 159 | xTruncate: Some(shim_truncate), |
| 160 | xSync: Some(shim_sync), |
| 161 | xFileSize: Some(shim_file_size), |
| 162 | xLock: Some(shim_lock), |
| 163 | xUnlock: Some(shim_unlock), |
| 164 | xCheckReservedLock: Some(shim_check_reserved_lock), |
| 165 | xFileControl: Some(shim_file_control), |
| 166 | xSectorSize: Some(shim_sector_size), |
| 167 | xDeviceCharacteristics: Some(shim_device_characteristics), |
| 168 | xShmMap: Some(shim_shm_map), |
| 169 | xShmLock: Some(shim_shm_lock), |
| 170 | xShmBarrier: Some(shim_shm_barrier), |
| 171 | xShmUnmap: Some(shim_shm_unmap), |
| 172 | xFetch: Some(shim_fetch), |
| 173 | xUnfetch: Some(shim_unfetch), |
| 174 | }; |
| 175 | |
| 176 | unsafe extern "C" fn shim_open( |
| 177 | vfs: *mut ffi::sqlite3_vfs, |
| 178 | name: *const c_char, |
| 179 | file: *mut ffi::sqlite3_file, |
| 180 | flags: c_int, |
| 181 | out_flags: *mut c_int, |
| 182 | ) -> c_int { |
| 183 | let real = real_vfs(vfs); |
| 184 | let shim = file.cast::<ShimFile>(); |
| 185 | let inner = file.cast::<u8>().add(size_of::<ShimFile>()).cast(); |
| 186 | (*shim).base.pMethods = std::ptr::null(); |
| 187 | (*shim).real = inner; |
| 188 | let open = (*real).xOpen.expect("real vfs xOpen"); |
| 189 | let rc = open(real, name, inner, flags, out_flags); |
| 190 | if !(*inner).pMethods.is_null() { |
| 191 | (*shim).base.pMethods = &SHIM_IO_METHODS; |
| 192 | } |
| 193 | rc |
| 194 | } |
| 195 | unsafe extern "C" fn shim_delete( |
| 196 | vfs: *mut ffi::sqlite3_vfs, |
| 197 | name: *const c_char, |
| 198 | sync_dir: c_int, |
| 199 | ) -> c_int { |
| 200 | vfs_delegate!(vfs, xDelete, name, sync_dir) |
| 201 | } |
| 202 | unsafe extern "C" fn shim_access( |
| 203 | vfs: *mut ffi::sqlite3_vfs, |
| 204 | name: *const c_char, |
| 205 | flags: c_int, |
| 206 | out: *mut c_int, |
| 207 | ) -> c_int { |
| 208 | vfs_delegate!(vfs, xAccess, name, flags, out) |
| 209 | } |
| 210 | unsafe extern "C" fn shim_full_pathname( |
| 211 | vfs: *mut ffi::sqlite3_vfs, |
| 212 | name: *const c_char, |
| 213 | length: c_int, |
| 214 | out: *mut c_char, |
| 215 | ) -> c_int { |
| 216 | vfs_delegate!(vfs, xFullPathname, name, length, out) |
| 217 | } |
| 218 | unsafe extern "C" fn shim_randomness( |
| 219 | vfs: *mut ffi::sqlite3_vfs, |
| 220 | length: c_int, |
| 221 | out: *mut c_char, |
| 222 | ) -> c_int { |
| 223 | vfs_delegate!(vfs, xRandomness, length, out) |
| 224 | } |
| 225 | unsafe extern "C" fn shim_sleep(vfs: *mut ffi::sqlite3_vfs, microseconds: c_int) -> c_int { |
| 226 | vfs_delegate!(vfs, xSleep, microseconds) |
| 227 | } |
| 228 | unsafe extern "C" fn shim_current_time(vfs: *mut ffi::sqlite3_vfs, out: *mut f64) -> c_int { |
| 229 | vfs_delegate!(vfs, xCurrentTime, out) |
| 230 | } |
| 231 | unsafe extern "C" fn shim_get_last_error( |
| 232 | vfs: *mut ffi::sqlite3_vfs, |
| 233 | length: c_int, |
| 234 | out: *mut c_char, |
| 235 | ) -> c_int { |
| 236 | vfs_delegate!(vfs, xGetLastError, length, out) |
| 237 | } |
| 238 | unsafe extern "C" fn shim_current_time_int64(vfs: *mut ffi::sqlite3_vfs, out: *mut i64) -> c_int { |
| 239 | vfs_delegate!(vfs, xCurrentTimeInt64, out) |
| 240 | } |
| 241 | |
| 242 | fn install() { |
| 243 | static INSTALL: Once = Once::new(); |
| 244 | INSTALL.call_once(|| { |
| 245 | // SAFETY: the default VFS outlives the process; the shim is leaked so |
| 246 | // SQLite may hold it forever. Registration is not made the default, |
| 247 | // so only connections opened by name route through the shim. |
| 248 | unsafe { |
| 249 | let real = ffi::sqlite3_vfs_find(std::ptr::null()); |
| 250 | assert!(!real.is_null(), "no default SQLite VFS"); |
| 251 | let vfs = Box::leak(Box::new(ffi::sqlite3_vfs { |
| 252 | iVersion: 2, |
| 253 | szOsFile: (*real).szOsFile + size_of::<ShimFile>() as c_int, |
| 254 | mxPathname: (*real).mxPathname, |
| 255 | pNext: std::ptr::null_mut(), |
| 256 | zName: FAULT_VFS_NAME.as_ptr(), |
| 257 | pAppData: real.cast(), |
| 258 | xOpen: Some(shim_open), |
| 259 | xDelete: Some(shim_delete), |
| 260 | xAccess: Some(shim_access), |
| 261 | xFullPathname: Some(shim_full_pathname), |
| 262 | xDlOpen: None, |
| 263 | xDlError: None, |
| 264 | xDlSym: None, |
| 265 | xDlClose: None, |
| 266 | xRandomness: Some(shim_randomness), |
| 267 | xSleep: Some(shim_sleep), |
| 268 | xCurrentTime: Some(shim_current_time), |
| 269 | xGetLastError: Some(shim_get_last_error), |
| 270 | xCurrentTimeInt64: Some(shim_current_time_int64), |
| 271 | xSetSystemCall: None, |
| 272 | xGetSystemCall: None, |
| 273 | xNextSystemCall: None, |
| 274 | })); |
| 275 | assert_eq!(ffi::sqlite3_vfs_register(vfs, 0), ffi::SQLITE_OK); |
| 276 | } |
| 277 | }); |
| 278 | } |
| 279 | |
| 280 | /// Open `path` through the fault-injection VFS. |
| 281 | pub fn open_database(path: &str) -> rusqlite::Result<rusqlite::Connection> { |
| 282 | install(); |
| 283 | rusqlite::Connection::open_with_flags_and_vfs( |
| 284 | path, |
| 285 | rusqlite::OpenFlags::default(), |
| 286 | FAULT_VFS_NAME.to_str().unwrap(), |
| 287 | ) |
| 288 | } |