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
+78
-11
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user