|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +sqlite3_finalize / stmt_run_to_completion — try_lock to dodge GC re-entry deadlock. |
| 4 | +
|
| 5 | +sqlite3_finalize uses a non-reentrant std::sync::Mutex on the db. PHP's |
| 6 | +cycle GC can fire a PDO statement destructor while another statement's |
| 7 | +sqlite3_step is in progress (i.e., from inside a UDF callback whose |
| 8 | +bridge re-enters PHP). The outer step holds the mutex, the inner |
| 9 | +finalize blocks on it, and we deadlock. |
| 10 | +
|
| 11 | +Fix: use try_lock; on contention, skip the stmt_list unlink and the |
| 12 | +drain. The list is only traversed by sqlite3_next_stmt (which |
| 13 | +pdo_sqlite doesn't call) and dropped on sqlite3_close, so a stale entry |
| 14 | +is harmless; the stmt's own Box is still freed below. |
| 15 | +""" |
| 16 | + |
| 17 | +import sys |
| 18 | + |
| 19 | +PATH = 'bindings/c/src/lib.rs' |
| 20 | + |
| 21 | +OLD_FINALIZE = ( |
| 22 | + ' if !stmt_ref.db.is_null() {\n' |
| 23 | + ' let db = &mut *stmt_ref.db;\n' |
| 24 | + ' let mut db_inner = db.inner.lock().unwrap();\n' |
| 25 | + '\n' |
| 26 | + ' if db_inner.stmt_list == stmt {\n' |
| 27 | + ' db_inner.stmt_list = stmt_ref.next;\n' |
| 28 | + ' } else {\n' |
| 29 | + ' let mut current = db_inner.stmt_list;\n' |
| 30 | + ' while !current.is_null() {\n' |
| 31 | + ' let current_ref = &mut *current;\n' |
| 32 | + ' if current_ref.next == stmt {\n' |
| 33 | + ' current_ref.next = stmt_ref.next;\n' |
| 34 | + ' break;\n' |
| 35 | + ' }\n' |
| 36 | + ' current = current_ref.next;\n' |
| 37 | + ' }\n' |
| 38 | + ' }\n' |
| 39 | + ' }\n' |
| 40 | +) |
| 41 | +NEW_FINALIZE = ( |
| 42 | + ' if !stmt_ref.db.is_null() {\n' |
| 43 | + ' let db = &mut *stmt_ref.db;\n' |
| 44 | + ' // try_lock to avoid deadlock when finalize is invoked\n' |
| 45 | + ' // re-entrantly (GC destructor during UDF callback).\n' |
| 46 | + ' if let Ok(mut db_inner) = db.inner.try_lock() {\n' |
| 47 | + ' if db_inner.stmt_list == stmt {\n' |
| 48 | + ' db_inner.stmt_list = stmt_ref.next;\n' |
| 49 | + ' } else {\n' |
| 50 | + ' let mut current = db_inner.stmt_list;\n' |
| 51 | + ' while !current.is_null() {\n' |
| 52 | + ' let current_ref = &mut *current;\n' |
| 53 | + ' if current_ref.next == stmt {\n' |
| 54 | + ' current_ref.next = stmt_ref.next;\n' |
| 55 | + ' break;\n' |
| 56 | + ' }\n' |
| 57 | + ' current = current_ref.next;\n' |
| 58 | + ' }\n' |
| 59 | + ' }\n' |
| 60 | + ' }\n' |
| 61 | + ' }\n' |
| 62 | +) |
| 63 | + |
| 64 | +OLD_DRAIN = ( |
| 65 | + 'unsafe fn stmt_run_to_completion(stmt: *mut sqlite3_stmt) -> ffi::c_int {\n' |
| 66 | + ' let stmt_ref = &mut *stmt;\n' |
| 67 | + ' while stmt_ref.stmt.execution_state().is_running() {\n' |
| 68 | + ' let result = sqlite3_step(stmt);\n' |
| 69 | + ' if result != SQLITE_DONE && result != SQLITE_ROW {\n' |
| 70 | + ' return result;\n' |
| 71 | + ' }\n' |
| 72 | + ' }\n' |
| 73 | + ' SQLITE_OK\n' |
| 74 | + '}\n' |
| 75 | +) |
| 76 | +NEW_DRAIN = ( |
| 77 | + 'unsafe fn stmt_run_to_completion(stmt: *mut sqlite3_stmt) -> ffi::c_int {\n' |
| 78 | + ' let stmt_ref = &mut *stmt;\n' |
| 79 | + " // Skip drain if we can't acquire the db mutex: we're\n" |
| 80 | + " // re-entering from a UDF callback's GC destructor, and\n" |
| 81 | + ' // sqlite3_step would block forever. The stmt will be\n' |
| 82 | + ' // freed anyway by the caller.\n' |
| 83 | + ' if !stmt_ref.db.is_null() {\n' |
| 84 | + ' let db = &*stmt_ref.db;\n' |
| 85 | + ' if db.inner.try_lock().is_err() {\n' |
| 86 | + ' return SQLITE_OK;\n' |
| 87 | + ' }\n' |
| 88 | + ' }\n' |
| 89 | + ' while stmt_ref.stmt.execution_state().is_running() {\n' |
| 90 | + ' let result = sqlite3_step(stmt);\n' |
| 91 | + ' if result != SQLITE_DONE && result != SQLITE_ROW {\n' |
| 92 | + ' return result;\n' |
| 93 | + ' }\n' |
| 94 | + ' }\n' |
| 95 | + ' SQLITE_OK\n' |
| 96 | + '}\n' |
| 97 | +) |
| 98 | + |
| 99 | +with open(PATH) as f: |
| 100 | + s = f.read() |
| 101 | +if OLD_FINALIZE not in s: |
| 102 | + sys.exit(f'{PATH}: sqlite3_finalize stmt_list block not found') |
| 103 | +s = s.replace(OLD_FINALIZE, NEW_FINALIZE, 1) |
| 104 | +if OLD_DRAIN not in s: |
| 105 | + sys.exit(f'{PATH}: stmt_run_to_completion block not found') |
| 106 | +s = s.replace(OLD_DRAIN, NEW_DRAIN, 1) |
| 107 | +with open(PATH, 'w') as f: |
| 108 | + f.write(s) |
| 109 | +print('patched sqlite3_finalize + stmt_run_to_completion for GC re-entry') |
0 commit comments