Sqlite: per-connection phase-targetable fault guard (VL-SQLITE-03)
This commit is contained in:
1 parent
103523f04a
commit
3b1ef4b028
3 files changed
+170
-11
No files matched your search
@@ -15,6 +15,8 @@
|
|||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
struct sqlite3;
|
||||||
|
|
||||||
namespace Void::Sqlite
|
namespace Void::Sqlite
|
||||||
{
|
{
|
||||||
using Void::Result::Error;
|
using Void::Result::Error;
|
||||||
@@ -458,7 +460,26 @@ namespace Void::Sqlite
|
|||||||
Ignore
|
Ignore
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Operation phase a fault can target.
|
||||||
|
enum class FaultPhase : std::uint8_t {
|
||||||
|
any,
|
||||||
|
prepare,
|
||||||
|
step,
|
||||||
|
exec,
|
||||||
|
commit,
|
||||||
|
checkpoint,
|
||||||
|
close
|
||||||
|
};
|
||||||
|
|
||||||
struct FaultConfig {
|
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
|
/// If non-zero, next COMMIT returns this SQLite error code without
|
||||||
/// calling SQLite COMMIT (leaving transaction active so autocommit==0).
|
/// calling SQLite COMMIT (leaving transaction active so autocommit==0).
|
||||||
int fail_next_commit_rc{0};
|
int fail_next_commit_rc{0};
|
||||||
|
|||||||
+78
-11
@@ -25,24 +25,63 @@ namespace Void::Sqlite
|
|||||||
std::mutex g_fault_mutex;
|
std::mutex g_fault_mutex;
|
||||||
FaultConfig g_fault_config{};
|
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};
|
std::lock_guard lk{g_fault_mutex};
|
||||||
FaultConfig out{};
|
FaultConfig out{};
|
||||||
if (g_fault_config.fail_next_commit_rc != 0) {
|
if (fault_targets(db)) {
|
||||||
out.fail_next_commit_rc = std::exchange(g_fault_config.fail_next_commit_rc, 0);
|
if (g_fault_config.fail_next_commit_rc != 0) {
|
||||||
out.auto_rollback_on_faulted_commit =
|
out.fail_next_commit_rc =
|
||||||
std::exchange(g_fault_config.auto_rollback_on_faulted_commit, false);
|
std::exchange(g_fault_config.fail_next_commit_rc, 0);
|
||||||
out.commit_applied_before_error =
|
out.auto_rollback_on_faulted_commit =
|
||||||
std::exchange(g_fault_config.commit_applied_before_error, false);
|
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;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] int consume_fault_close() noexcept
|
[[nodiscard]] int consume_fault_close(sqlite3* db) noexcept
|
||||||
{
|
{
|
||||||
std::lock_guard lk{g_fault_mutex};
|
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
|
[[nodiscard]] SqliteErrorCode map_sqlite_rc(int rc) noexcept
|
||||||
@@ -969,6 +1008,13 @@ namespace Void::Sqlite
|
|||||||
return Void::Result::fail(make_sqlite_error(
|
return Void::Result::fail(make_sqlite_error(
|
||||||
SqliteErrorCode::misuse, "Sqlite: step on finalized statement"));
|
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_row_generation;
|
||||||
m_has_row = false;
|
m_has_row = false;
|
||||||
|
|
||||||
@@ -1426,7 +1472,7 @@ namespace Void::Sqlite
|
|||||||
"Sqlite: transaction lost its underlying connection before commit"));
|
"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) {
|
if (fault.fail_next_commit_rc != 0) {
|
||||||
bool applied = false;
|
bool applied = false;
|
||||||
if (fault.commit_applied_before_error) {
|
if (fault.commit_applied_before_error) {
|
||||||
@@ -1829,6 +1875,13 @@ namespace Void::Sqlite
|
|||||||
return Void::Result::fail(make_sqlite_error(
|
return Void::Result::fail(make_sqlite_error(
|
||||||
SqliteErrorCode::closed, "Sqlite: exec rejected while closing"));
|
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) {
|
if (sql.size() > m_conn->options.limits.max_sql_bytes) {
|
||||||
return Void::Result::fail(make_sqlite_error(
|
return Void::Result::fail(make_sqlite_error(
|
||||||
SqliteErrorCode::size_limit,
|
SqliteErrorCode::size_limit,
|
||||||
@@ -1855,6 +1908,13 @@ namespace Void::Sqlite
|
|||||||
return Void::Result::fail(make_sqlite_error(
|
return Void::Result::fail(make_sqlite_error(
|
||||||
SqliteErrorCode::closed, "Sqlite: prepare rejected while closing"));
|
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) {
|
if (sql.size() > m_conn->options.limits.max_sql_bytes) {
|
||||||
return Void::Result::fail(make_sqlite_error(
|
return Void::Result::fail(make_sqlite_error(
|
||||||
SqliteErrorCode::size_limit,
|
SqliteErrorCode::size_limit,
|
||||||
@@ -2115,7 +2175,7 @@ namespace Void::Sqlite
|
|||||||
SQLITE_BUSY));
|
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(
|
return Void::Result::fail(make_sqlite_error(
|
||||||
map_sqlite_rc(injected),
|
map_sqlite_rc(injected),
|
||||||
"Sqlite: injected close failure",
|
"Sqlite: injected close failure",
|
||||||
@@ -2453,6 +2513,13 @@ namespace Void::Sqlite
|
|||||||
return Void::Result::fail(make_sqlite_error(
|
return Void::Result::fail(make_sqlite_error(
|
||||||
SqliteErrorCode::closed, "Sqlite: checkpoint on closed database"));
|
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;
|
int native_mode = SQLITE_CHECKPOINT_PASSIVE;
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
|
|||||||
@@ -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::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<std::int32_t>(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<std::int32_t>(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<std::int32_t>(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<std::int32_t>(SqliteErrorCode::io));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user