From 3b1ef4b02808f7c18cbeb538d998f2ef17ef41ce Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Thu, 3 Dec 2026 18:34:19 +0300 Subject: [PATCH] Sqlite: per-connection phase-targetable fault guard (VL-SQLITE-03) --- include/Void/Sqlite/Types.hpp | 21 +++++ src/Sqlite/Sqlite.cpp | 89 ++++++++++++++++++--- tests/standalone/Sqlite/SqliteHardening.cpp | 71 ++++++++++++++++ 3 files changed, 170 insertions(+), 11 deletions(-) diff --git a/include/Void/Sqlite/Types.hpp b/include/Void/Sqlite/Types.hpp index 7c9aed3..0891737 100644 --- a/include/Void/Sqlite/Types.hpp +++ b/include/Void/Sqlite/Types.hpp @@ -15,6 +15,8 @@ #include #include +struct sqlite3; + namespace Void::Sqlite { using Void::Result::Error; @@ -458,7 +460,26 @@ namespace Void::Sqlite Ignore }; + /// Operation phase a fault can target. + enum class FaultPhase : std::uint8_t { + any, + prepare, + step, + exec, + commit, + checkpoint, + close + }; + struct FaultConfig { + /// Restrict the injected fault to this connection. Null targets every + /// connection, preserving the original process-wide behavior. + sqlite3* connection{nullptr}; + /// Restrict the generic phase fault below to this operation phase. + FaultPhase phase{FaultPhase::any}; + /// If non-zero, the next matching phase operation fails with this + /// SQLite result code before running the native operation. + int fail_next_rc{0}; /// If non-zero, next COMMIT returns this SQLite error code without /// calling SQLite COMMIT (leaving transaction active so autocommit==0). int fail_next_commit_rc{0}; diff --git a/src/Sqlite/Sqlite.cpp b/src/Sqlite/Sqlite.cpp index 81e4b1d..26813df 100644 --- a/src/Sqlite/Sqlite.cpp +++ b/src/Sqlite/Sqlite.cpp @@ -25,24 +25,63 @@ namespace Void::Sqlite std::mutex g_fault_mutex; FaultConfig g_fault_config{}; - [[nodiscard]] FaultConfig consume_fault_commit() noexcept + [[nodiscard]] bool fault_targets(sqlite3* db) noexcept + { + return g_fault_config.connection == nullptr || + g_fault_config.connection == db; + } + + [[nodiscard]] FaultConfig consume_fault_commit(sqlite3* db) noexcept { std::lock_guard lk{g_fault_mutex}; FaultConfig out{}; - if (g_fault_config.fail_next_commit_rc != 0) { - out.fail_next_commit_rc = std::exchange(g_fault_config.fail_next_commit_rc, 0); - out.auto_rollback_on_faulted_commit = - std::exchange(g_fault_config.auto_rollback_on_faulted_commit, false); - out.commit_applied_before_error = - std::exchange(g_fault_config.commit_applied_before_error, false); + if (fault_targets(db)) { + if (g_fault_config.fail_next_commit_rc != 0) { + out.fail_next_commit_rc = + std::exchange(g_fault_config.fail_next_commit_rc, 0); + out.auto_rollback_on_faulted_commit = + std::exchange(g_fault_config.auto_rollback_on_faulted_commit, false); + out.commit_applied_before_error = + std::exchange(g_fault_config.commit_applied_before_error, false); + } else if (g_fault_config.fail_next_rc != 0 && + (g_fault_config.phase == FaultPhase::any || + g_fault_config.phase == FaultPhase::commit)) { + out.fail_next_commit_rc = + std::exchange(g_fault_config.fail_next_rc, 0); + } } return out; } - [[nodiscard]] int consume_fault_close() noexcept + [[nodiscard]] int consume_fault_close(sqlite3* db) noexcept { std::lock_guard lk{g_fault_mutex}; - return std::exchange(g_fault_config.fail_next_close_rc, 0); + if (!fault_targets(db)) { + return 0; + } + if (g_fault_config.fail_next_close_rc != 0) { + return std::exchange(g_fault_config.fail_next_close_rc, 0); + } + if (g_fault_config.fail_next_rc != 0 && + (g_fault_config.phase == FaultPhase::any || + g_fault_config.phase == FaultPhase::close)) { + return std::exchange(g_fault_config.fail_next_rc, 0); + } + return 0; + } + + /// Consume a generic phase-targeted fault for `db`. + [[nodiscard]] int consume_fault_phase(sqlite3* db, FaultPhase phase) noexcept + { + std::lock_guard lk{g_fault_mutex}; + if (g_fault_config.fail_next_rc == 0 || !fault_targets(db)) { + return 0; + } + if (g_fault_config.phase != FaultPhase::any && + g_fault_config.phase != phase) { + return 0; + } + return std::exchange(g_fault_config.fail_next_rc, 0); } [[nodiscard]] SqliteErrorCode map_sqlite_rc(int rc) noexcept @@ -969,6 +1008,13 @@ namespace Void::Sqlite return Void::Result::fail(make_sqlite_error( SqliteErrorCode::misuse, "Sqlite: step on finalized statement")); } + if (const int injected = + consume_fault_phase(m_conn->db, FaultPhase::step); + injected != 0) { + return Void::Result::fail(make_sqlite_error( + map_sqlite_rc(injected), "Sqlite: injected step failure", injected, + injected)); + } ++m_row_generation; m_has_row = false; @@ -1426,7 +1472,7 @@ namespace Void::Sqlite "Sqlite: transaction lost its underlying connection before commit")); } - const auto fault = consume_fault_commit(); + const auto fault = consume_fault_commit(m_conn->db); if (fault.fail_next_commit_rc != 0) { bool applied = false; if (fault.commit_applied_before_error) { @@ -1829,6 +1875,13 @@ namespace Void::Sqlite return Void::Result::fail(make_sqlite_error( SqliteErrorCode::closed, "Sqlite: exec rejected while closing")); } + if (const int injected = + consume_fault_phase(m_conn->db, FaultPhase::exec); + injected != 0) { + return Void::Result::fail(make_sqlite_error( + map_sqlite_rc(injected), "Sqlite: injected exec failure", injected, + injected)); + } if (sql.size() > m_conn->options.limits.max_sql_bytes) { return Void::Result::fail(make_sqlite_error( SqliteErrorCode::size_limit, @@ -1855,6 +1908,13 @@ namespace Void::Sqlite return Void::Result::fail(make_sqlite_error( SqliteErrorCode::closed, "Sqlite: prepare rejected while closing")); } + if (const int injected = + consume_fault_phase(m_conn->db, FaultPhase::prepare); + injected != 0) { + return Void::Result::fail(make_sqlite_error( + map_sqlite_rc(injected), "Sqlite: injected prepare failure", + injected, injected)); + } if (sql.size() > m_conn->options.limits.max_sql_bytes) { return Void::Result::fail(make_sqlite_error( SqliteErrorCode::size_limit, @@ -2115,7 +2175,7 @@ namespace Void::Sqlite SQLITE_BUSY)); } - if (const int injected = consume_fault_close(); injected != 0) { + if (const int injected = consume_fault_close(m_conn->db); injected != 0) { return Void::Result::fail(make_sqlite_error( map_sqlite_rc(injected), "Sqlite: injected close failure", @@ -2453,6 +2513,13 @@ namespace Void::Sqlite return Void::Result::fail(make_sqlite_error( SqliteErrorCode::closed, "Sqlite: checkpoint on closed database")); } + if (const int injected = + consume_fault_phase(m_conn->db, FaultPhase::checkpoint); + injected != 0) { + return Void::Result::fail(make_sqlite_error( + map_sqlite_rc(injected), "Sqlite: injected checkpoint failure", + injected, injected)); + } int native_mode = SQLITE_CHECKPOINT_PASSIVE; switch (mode) { diff --git a/tests/standalone/Sqlite/SqliteHardening.cpp b/tests/standalone/Sqlite/SqliteHardening.cpp index ab7ed22..432cea3 100644 --- a/tests/standalone/Sqlite/SqliteHardening.cpp +++ b/tests/standalone/Sqlite/SqliteHardening.cpp @@ -806,3 +806,74 @@ Void::Testing::Test sqlite_failed_commit_outcome_is_unknown_not_rolled_back() Void::Testing::expect(tx->state() == TransactionState::RolledBack); } } + +Void::Testing::Test sqlite_fault_guard_targets_connection_and_phase() +{ + TempSqliteDir dir; + auto db1_r = open_database(dir.path / "one.db"); + auto db2_r = open_database(dir.path / "two.db"); + Void::Testing::require(db1_r.has_value()); + Void::Testing::require(db2_r.has_value()); + auto& db1 = *db1_r; + auto& db2 = *db2_r; + Void::Testing::require(db1.exec("CREATE TABLE t(id INTEGER PRIMARY KEY)").has_value()); + Void::Testing::require(db2.exec("CREATE TABLE t(id INTEGER PRIMARY KEY)").has_value()); + + // A connection-targeted exec fault does not leak to the other connection. + { + ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(), + .phase = FaultPhase::exec, + .fail_next_rc = SQLITE_IOERR}}; + auto failed = db1.exec("INSERT INTO t VALUES(1)"); + Void::Testing::require(!failed.has_value()); + Void::Testing::expect_eq(failed.error().code().value, + static_cast(SqliteErrorCode::io)); + Void::Testing::require(db2.exec("INSERT INTO t VALUES(2)").has_value()); + Void::Testing::require(db1.exec("INSERT INTO t VALUES(1)").has_value()); + } + + // A phase mismatch neither fires nor consumes the pending fault. + { + ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(), + .phase = FaultPhase::commit, + .fail_next_rc = SQLITE_IOERR}}; + Void::Testing::require(db1.exec("INSERT INTO t VALUES(3)").has_value()); + auto tx = db1.begin(BeginMode::Immediate); + Void::Testing::require(tx.has_value()); + auto failed = tx->commit(); + Void::Testing::require(!failed.has_value()); + Void::Testing::expect(tx->state() == TransactionState::Active); + Void::Testing::require(tx->rollback_checked().has_value()); + } + + // prepare, step and checkpoint phases are individually targetable. + { + ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(), + .phase = FaultPhase::prepare, + .fail_next_rc = SQLITE_IOERR}}; + auto stmt = db1.prepare("SELECT 1"); + Void::Testing::require(!stmt.has_value()); + Void::Testing::expect_eq(stmt.error().code().value, + static_cast(SqliteErrorCode::io)); + } + { + ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(), + .phase = FaultPhase::step, + .fail_next_rc = SQLITE_IOERR}}; + auto stmt = db1.prepare("SELECT 1"); + Void::Testing::require(stmt.has_value()); + auto stepped = stmt->step(); + Void::Testing::require(!stepped.has_value()); + Void::Testing::expect_eq(stepped.error().code().value, + static_cast(SqliteErrorCode::io)); + } + { + ScopedFaultGuard guard{FaultConfig{.connection = db1.handle(), + .phase = FaultPhase::checkpoint, + .fail_next_rc = SQLITE_IOERR}}; + auto checkpoint = db1.checkpoint(); + Void::Testing::require(!checkpoint.has_value()); + Void::Testing::expect_eq(checkpoint.error().code().value, + static_cast(SqliteErrorCode::io)); + } +}