1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std::{
    ffi::{c_char, c_int, c_void, CStr},
    marker::PhantomData,
};

type Wal = crate::ffi::libsql_wal;
use crate::ffi::{
    libsql_wal_methods, libsql_wal_methods_find, sqlite3, sqlite3_file, sqlite3_vfs, PgHdr,
};
use crate::types::*;

fn get_orig_wal_methods() -> crate::Result<*mut libsql_wal_methods> {
    let orig: *mut libsql_wal_methods = unsafe { libsql_wal_methods_find(std::ptr::null()) };
    if orig.is_null() {
        return Err(crate::Error::Bug("no underlying methods"));
    }

    Ok(orig)
}

/// This macro handles the registering of a WalHook within libSQL. It first instantiates `WalMethodsHook`
/// to a stable location in memory, and then calls `libsql_wal_methods_register` with the WAL methods.
///
/// The methods are never unregistered, since they're expected to live for the entirety of the program.
#[macro_export]
macro_rules! init_static_wal_method {
    ($name:ident, $ty:path) => {
        #[allow(dead_code)]
        pub static $name: once_cell::sync::Lazy<&'static $crate::WalMethodsHook<$ty>> =
            once_cell::sync::Lazy::new(|| {
                // we need a 'static address before we can register the methods.
                static METHODS: once_cell::sync::Lazy<$crate::WalMethodsHook<$ty>> =
                    once_cell::sync::Lazy::new(|| $crate::WalMethodsHook::<$ty>::new());

                let ret = unsafe {
                    $crate::ffi::libsql_wal_methods_register(METHODS.as_wal_methods_ptr() as *mut _)
                };

                assert!(
                    ret == 0,
                    "failed to register wal methods for {}",
                    stringify!($ty)
                );

                &METHODS
            });
    };
}

/// The `WalHook` trait allows to intercept WAL method call.
///
/// All the methods in this trait have the following format: - arguments to the WAL method -
/// function pointer to the wrapped WAL method
///
/// The default implementations for this trait methods is to transparently call the wrapped methods
/// with the passed arguments
///
/// # Safety
/// The implementer is responsible for calling the orig method with valid arguments.
pub unsafe trait WalHook {
    type Context;

    fn name() -> &'static CStr;
    /// Intercept `xFrame` call. `orig` is the function pointer to the underlying wal method.
    /// The default implementation of this trait simply calls orig with the other passed arguments.
    #[allow(clippy::too_many_arguments)]
    fn on_frames(
        wal: &mut Wal,
        page_size: c_int,
        page_headers: *mut PgHdr,
        size_after: u32,
        is_commit: c_int,
        sync_flags: c_int,
        orig: XWalFrameFn,
    ) -> c_int {
        unsafe {
            (orig)(
                wal,
                page_size,
                page_headers,
                size_after,
                is_commit,
                sync_flags,
            )
        }
    }

    /// Intercept `xUndo` call. `orig` is the function pointer to the underlying wal method.
    /// The default implementation of this trait simply calls orig with the other passed arguments.
    fn on_undo(
        wal: &mut Wal,
        func: Option<unsafe extern "C" fn(*mut c_void, u32) -> i32>,
        undo_ctx: *mut c_void,
        orig: XWalUndoFn,
    ) -> i32 {
        unsafe { orig(wal, func, undo_ctx) }
    }

    fn wal_extract_ctx(wal: &mut Wal) -> &mut Self::Context {
        let ctx_ptr = wal.pMethodsData as *mut Self::Context;
        assert!(!ctx_ptr.is_null(), "missing wal context");
        unsafe { &mut *ctx_ptr }
    }

    fn on_savepoint_undo(wal: &mut Wal, wal_data: *mut u32, orig: XWalSavePointUndoFn) -> i32 {
        unsafe { orig(wal, wal_data) }
    }

    #[allow(clippy::too_many_arguments)]
    fn on_checkpoint(
        wal: &mut Wal,
        db: *mut sqlite3,
        emode: i32,
        busy_handler: Option<unsafe extern "C" fn(*mut c_void) -> i32>,
        busy_arg: *mut c_void,
        sync_flags: i32,
        n_buf: i32,
        z_buf: *mut u8,
        frames_in_wal: *mut i32,
        backfilled_frames: *mut i32,
        orig: XWalCheckpointFn,
    ) -> i32 {
        unsafe {
            orig(
                wal,
                db,
                emode,
                busy_handler,
                busy_arg,
                sync_flags,
                n_buf,
                z_buf,
                frames_in_wal,
                backfilled_frames,
            )
        }
    }
}

init_static_wal_method!(TRANSPARENT_METHODS, TransparentMethods);

/// Wal implemementation that just proxies calls to the wrapped WAL methods implementation
pub enum TransparentMethods {}

unsafe impl WalHook for TransparentMethods {
    type Context = ();

    fn name() -> &'static CStr {
        CStr::from_bytes_with_nul(b"transparent\0").unwrap()
    }
}

impl<T: WalHook> Default for WalMethodsHook<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: WalHook> WalMethodsHook<T> {
    pub fn new() -> Self {
        let default_methods = get_orig_wal_methods().expect("failed to get original WAL methods");

        WalMethodsHook {
            methods: libsql_wal_methods {
                iVersion: 1,
                xOpen: Some(xOpen::<T>),
                xClose: Some(xClose::<T>),
                xLimit: Some(xLimit::<T>),
                xBeginReadTransaction: Some(xBeginReadTransaction::<T>),
                xEndReadTransaction: Some(xEndReadTransaction::<T>),
                xFindFrame: Some(xFindFrame::<T>),
                xReadFrame: Some(xReadFrame::<T>),
                xDbsize: Some(xDbsize::<T>),
                xBeginWriteTransaction: Some(xBeginWriteTransaction::<T>),
                xEndWriteTransaction: Some(xEndWriteTransaction::<T>),
                xUndo: Some(xUndo::<T>),
                xSavepoint: Some(xSavepoint::<T>),
                xSavepointUndo: Some(xSavepointUndo::<T>),
                xFrames: Some(xFrames::<T>),
                xCheckpoint: Some(xCheckpoint::<T>),
                xCallback: Some(xCallback::<T>),
                xExclusiveMode: Some(xExclusiveMode::<T>),
                xHeapMemory: Some(xHeapMemory::<T>),
                xSnapshotGet: None,
                xSnapshotOpen: None,
                xSnapshotRecover: None,
                xSnapshotCheck: None,
                xSnapshotUnlock: None,
                xFramesize: None,
                xFile: Some(xFile::<T>),
                xWriteLock: None,
                xDb: Some(xDb::<T>),
                xPathnameLen: Some(xPathnameLen),
                xGetWalPathname: Some(xGetPathname),
                xPreMainDbOpen: Some(xPreMainDbOpen::<T>),
                zName: T::name().as_ptr(),
                bUsesShm: 0,
                pNext: std::ptr::null_mut(),
            },
            underlying_methods: default_methods,
            _pth: PhantomData,
        }
    }

    pub fn as_wal_methods_ptr(&self) -> *const libsql_wal_methods {
        self as *const _ as *mut _
    }
}

#[allow(non_snake_case)]
pub extern "C" fn xOpen<T: WalHook>(
    vfs: *mut sqlite3_vfs,
    db_file: *mut sqlite3_file,
    wal_name: *const c_char,
    no_shm_mode: i32,
    max_size: i64,
    methods: *mut libsql_wal_methods,
    wal: *mut *mut Wal,
) -> i32 {
    tracing::debug!("Opening WAL {}", unsafe {
        std::ffi::CStr::from_ptr(wal_name).to_str().unwrap()
    });
    let ref_methods = unsafe { &*(methods as *mut WalMethodsHook<T>) };
    let origxOpen = unsafe { (*ref_methods.underlying_methods).xOpen.unwrap() };
    unsafe { (origxOpen)(vfs, db_file, wal_name, no_shm_mode, max_size, methods, wal) }
}

fn get_orig_methods<T: WalHook>(wal: &mut Wal) -> &libsql_wal_methods {
    let methods = get_methods::<T>(wal);
    assert!(!methods.underlying_methods.is_null());
    unsafe { &*methods.underlying_methods }
}

fn get_methods<T>(wal: &mut Wal) -> &mut WalMethodsHook<T> {
    assert!(!wal.pMethods.is_null());
    unsafe { &mut *(wal.pMethods as *mut _ as *mut WalMethodsHook<T>) }
}

#[allow(non_snake_case)]
pub extern "C" fn xClose<T: WalHook>(
    wal: *mut Wal,
    db: *mut sqlite3,
    sync_flags: i32,
    n_buf: c_int,
    z_buf: *mut u8,
) -> c_int {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xClose.unwrap())(wal, db, sync_flags, n_buf, z_buf) }
}

#[allow(non_snake_case)]
pub extern "C" fn xLimit<T: WalHook>(wal: *mut Wal, limit: i64) {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xLimit.unwrap())(wal, limit) }
}

#[allow(non_snake_case)]
pub extern "C" fn xBeginReadTransaction<T: WalHook>(wal: *mut Wal, changed: *mut i32) -> i32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xBeginReadTransaction.unwrap())(wal, changed) }
}

#[allow(non_snake_case)]
pub extern "C" fn xEndReadTransaction<T: WalHook>(wal: *mut Wal) {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xEndReadTransaction.unwrap())(wal) }
}

#[allow(non_snake_case)]
pub extern "C" fn xFindFrame<T: WalHook>(wal: *mut Wal, pgno: u32, frame: *mut u32) -> c_int {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xFindFrame.unwrap())(wal, pgno, frame) }
}

#[allow(non_snake_case)]
pub extern "C" fn xReadFrame<T: WalHook>(
    wal: *mut Wal,
    frame: u32,
    n_out: c_int,
    p_out: *mut u8,
) -> i32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xReadFrame.unwrap())(wal, frame, n_out, p_out) }
}

#[allow(non_snake_case)]
pub extern "C" fn xDbsize<T: WalHook>(wal: *mut Wal) -> u32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xDbsize.unwrap())(wal) }
}

#[allow(non_snake_case)]
pub extern "C" fn xBeginWriteTransaction<T: WalHook>(wal: *mut Wal) -> i32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xBeginWriteTransaction.unwrap())(wal) }
}

#[allow(non_snake_case)]
pub extern "C" fn xEndWriteTransaction<T: WalHook>(wal: *mut Wal) -> i32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xEndWriteTransaction.unwrap())(wal) }
}

#[allow(non_snake_case)]
pub extern "C" fn xUndo<T: WalHook>(
    wal: *mut Wal,
    func: Option<unsafe extern "C" fn(*mut c_void, u32) -> i32>,
    undo_ctx: *mut c_void,
) -> i32 {
    assert!(!wal.is_null());
    let wal = unsafe { &mut *wal };
    let orig_methods = get_orig_methods::<T>(wal);
    let orig_xundo = orig_methods.xUndo.unwrap();
    T::on_undo(wal, func, undo_ctx, orig_xundo)
}

#[allow(non_snake_case)]
pub extern "C" fn xSavepoint<T: WalHook>(wal: *mut Wal, wal_data: *mut u32) {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xSavepoint.unwrap())(wal, wal_data) }
}

#[allow(non_snake_case)]
pub extern "C" fn xSavepointUndo<T: WalHook>(wal: *mut Wal, wal_data: *mut u32) -> i32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xSavepointUndo.unwrap())(wal, wal_data) }
}

#[allow(non_snake_case)]
pub extern "C" fn xFrames<T: WalHook>(
    wal: *mut Wal,
    page_size: c_int,
    page_headers: *mut PgHdr,
    size_after: u32,
    is_commit: c_int,
    sync_flags: c_int,
) -> c_int {
    assert!(!wal.is_null());
    let wal = unsafe { &mut *wal };
    let orig_methods = get_orig_methods::<T>(wal);
    let orig_xframe = orig_methods.xFrames.unwrap();

    T::on_frames(
        wal,
        page_size,
        page_headers,
        size_after,
        is_commit,
        sync_flags,
        orig_xframe,
    )
}

#[tracing::instrument(skip(wal, db))]
#[allow(non_snake_case)]
pub extern "C" fn xCheckpoint<T: WalHook>(
    wal: *mut Wal,
    db: *mut sqlite3,
    emode: c_int,
    busy_handler: Option<unsafe extern "C" fn(busy_param: *mut c_void) -> c_int>,
    busy_arg: *mut c_void,
    sync_flags: c_int,
    n_buf: c_int,
    z_buf: *mut u8,
    frames_in_wal: *mut c_int,
    backfilled_frames: *mut c_int,
) -> i32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe {
        (orig_methods.xCheckpoint.unwrap())(
            wal,
            db,
            emode,
            busy_handler,
            busy_arg,
            sync_flags,
            n_buf,
            z_buf,
            frames_in_wal,
            backfilled_frames,
        )
    }
}

#[allow(non_snake_case)]
pub extern "C" fn xCallback<T: WalHook>(wal: *mut Wal) -> i32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xCallback.unwrap())(wal) }
}

#[allow(non_snake_case)]
pub extern "C" fn xExclusiveMode<T: WalHook>(wal: *mut Wal, op: c_int) -> c_int {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xExclusiveMode.unwrap())(wal, op) }
}

#[allow(non_snake_case)]
pub extern "C" fn xHeapMemory<T: WalHook>(wal: *mut Wal) -> i32 {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xHeapMemory.unwrap())(wal) }
}

#[allow(non_snake_case)]
pub extern "C" fn xFile<T: WalHook>(wal: *mut Wal) -> *mut sqlite3_file {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xFile.unwrap())(wal) }
}

#[allow(non_snake_case)]
pub extern "C" fn xDb<T: WalHook>(wal: *mut Wal, db: *mut sqlite3) {
    let orig_methods = unsafe { get_orig_methods::<T>(&mut *wal) };
    unsafe { (orig_methods.xDb.unwrap())(wal, db) }
}

#[allow(non_snake_case)]
pub extern "C" fn xPathnameLen(orig_len: i32) -> i32 {
    orig_len + 4
}

#[allow(non_snake_case)]
pub extern "C" fn xGetPathname(buf: *mut c_char, orig: *const c_char, orig_len: c_int) {
    unsafe { std::ptr::copy(orig, buf, orig_len as usize) }
    unsafe {
        std::ptr::copy(
            "-wal".as_ptr(),
            (buf as *mut u8).offset(orig_len as isize),
            4,
        )
    }
}

#[allow(non_snake_case)]
pub extern "C" fn xPreMainDbOpen<T: WalHook>(
    methods: *mut libsql_wal_methods,
    path: *const c_char,
) -> i32 {
    let orig_methods = unsafe { &*(*(methods as *mut WalMethodsHook<T>)).underlying_methods };
    unsafe { (orig_methods.xPreMainDbOpen.unwrap())(methods, path) }
}

unsafe impl<T> Send for WalMethodsHook<T> {}
unsafe impl<T> Sync for WalMethodsHook<T> {}

#[repr(C)]
#[allow(non_snake_case)]
pub struct WalMethodsHook<T> {
    pub methods: libsql_wal_methods,
    // user data
    underlying_methods: *mut libsql_wal_methods,
    _pth: PhantomData<T>,
}