Skip to content

hardening tests and fixes - #998

Open
citizen-stig wants to merge 11 commits into
thrumdev:masterfrom
citizen-stig:nikolai/hardening-port
Open

citizen-stig wants to merge 11 commits into
thrumdev:masterfrom
citizen-stig:nikolai/hardening-port

Conversation

@citizen-stig

@citizen-stig citizen-stig commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Six commits from 2026-10-13 that never reached master, ported onto master at 0224973, plus regression tests for three of them. Five are clean cherry-picks; one needed a manual resolution. One of them fixes a Critical finding of the 2026-11-23 audit: a commit could report success after its hash-table writes had failed.

Independent of nikolai/audit-high-fixes (#997): the two branches merge without conflicts and pass cargo test --workspace together.

Commits

01c08c6 Keep branch and leaf nodes alive in their write commands (from 1512c2b)

What. IoKind::WriteRaw, which carried a raw page pointer, becomes IoKind::WriteShared, which holds an Arc to the node until the write completes. The deferred-drop bookkeeping and FatPage::page() are removed.

Why. New nodes were kept alive only until all I/O was drained. If an I/O error made the update return early, they could be freed and their pages reused while their writes were still in flight.

Tested. No dedicated test; covered by the existing suite.

cd7c538 Free overflow pages of a replaced cell with nothing kept before it (from bc936da)

What. LeafUpdater::keep_up_to reclaims the overflow pages of the replaced cell even when there are no cells to keep before it.

Why. It returned early in that case, which happens for the first key of a leaf and for consecutive updated keys, and leaked the pages.

Tested. New nomt/tests/overflow_leak.rs: one 256 KiB value rewritten 300 times must stay within the leaf file's first 32 MiB step. On master the file grows to 96 MiB.

acbb907 Keep the WAL when hash-table writes fail, and recover from a torn WAL (from 4eb47b5)

What. write_ht drains every submitted page write and returns the first error. WAL recovery reads only the header first (WalBlobReader::read_sync_seqn) and discards a WAL that belongs to another sync without parsing its body.

Why. The write results were ignored, so the WAL was truncated even if pages never reached the hash table: the commit returned Ok and the page store was silently corrupt. Separately, a WAL left incomplete by a sync that never concluded made opening fail, although such a WAL is safe to discard.

Tested. New nomt/tests/ht_write_error.rs: with RLIMIT_FSIZE every hash-table page write past 40 MiB fails while all other writes succeed. On master the commit returns Ok and the reopened database has a different root; here the commit reports the error. The torn-WAL half has no dedicated test.

25cd9a3 Harden rollback log recovery (from e52fe48)

What. Recovery stops at the manifest's last live record, checks segment-ID gaps only up to the end of the live range, and checks that live records are contiguous across segments.

Why. A crash during an append can leave an incomplete record after the last live one, which recovery rejected. An interrupted recovery can leave gaps among the segments after the live range.

Tested. The commit adds recovery_start_live_in_pruned_segment to the seglog unit tests.

5ce119d Fix rollback log edge cases (from e947bdb)

What. After a truncation the new end of the live range is the last retained delta, or nil when none is left. max_rollback_log_len(0) is rejected in the setter, like io_workers(0).

Why. The end used to be the record before the earliest removed one. When every retained delta was removed, that record had already expired: it came back after reopening, or pruning failed because its segment was gone. A length of zero made the rollback sync thread panic on the first commit.

Tested. No dedicated test; covered by the existing rollback tests.

c6101d7 Drain in-flight work before propagating commit errors (from 7be9540, manual port)

What. Commit paths wait for sibling tasks and submitted writes before returning the first error: leaf and branch workers, the I/O completions in the update, the fsyncs, the WAL writeout and the post-meta cleanup. The store is poisoned when a commit unwinds, not only when it returns an error.

Why. Several paths returned on the first error while other work was still running. A panic inside a commit left the store usable with in-memory state ahead of the disk, and a later commit could then destroy what the panicked one had made durable.

Port note. Sync::sync changed on master since the original commit: it already waits for both preparations before the meta write, so that part was dropped. After the meta write, the rollback cleanup is now awaited before the beatree task is joined, so that neither a hash-table writeout error nor a beatree panic skips it.

Tested. New nomt/tests/commit_panic.rs: a commit that panics after the meta write is durable, and a later commit attempt on the same handle must not destroy it. Fails on master, passes here.

3418450 tests: cover three of the ported hardening fixes

Adds the three test files named above. Each fails on master and passes on this branch.

3dd6615 tests: keep the database on one thread in ht_write_error

The test opened the database on the test thread and moved it, with its open session, into a helper thread. nikolai/audit-high-fixes makes Session non-Send, and the test would no longer compile there. All database work now happens on the helper thread. The test still fails against master's code.

citizen-stig and others added 8 commits November 23, 2026 16:37
New branch and leaf nodes were written with a raw page pointer and kept
alive through deferred_drop_pages until all I/O was drained. If an I/O
error made update return early, the nodes could be freed while their
writes were still in flight.

Let the write command hold an Arc to the node instead, and remove the
deferred drop bookkeeping.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
LeafUpdater::keep_up_to returned early when there were no cells to keep
before the updated key, which skipped reclaiming the overflow pages of
the cell being replaced. This happens for the first key of a leaf and
for consecutive updated keys, and leaked those overflow pages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
write_ht ignored the results of the page writes, so post-meta truncated
the WAL even if some pages never reached the hash table. Drain all
writes and return the first error, which keeps the WAL for recovery.

Recovery parsed the whole WAL before checking its sequence number, so a
WAL left incomplete by a sync that never concluded made opening fail.
Read only the header first and discard a WAL from another sync without
parsing it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Stop at the manifest's last live record. A crash during an append can
  leave an incomplete record after it, which recovery used to reject.
- Check for gaps in segment IDs only up to the end of the live range,
  because an interrupted recovery may leave gaps among the segments
  after it.
- Check that live records are contiguous across segment boundaries.
  The first live record may come after the manifest's start, because
  that start can be an expired record whose segment was already pruned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- After truncating, the new end of the live range was the record before
  the earliest removed delta. When every retained delta was removed,
  that record had already expired: it either came back after reopening,
  or pruning failed if its segment was already gone. Use the last
  retained delta, or nil.
- max_rollback_log_len(0) made the rollback sync thread panic on the
  first commit. Reject it in the setter, like io_workers(0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Several commit paths returned on the first error while sibling tasks or
submitted writes were still running: leaf and branch workers, the I/O
completions in update, the bbn/ln fsyncs, the bitbox WAL writeout, and
the post-meta cleanup in Sync. Wait for all of them and then return the
first error.

Also poison the store when a commit unwinds, not only when it returns
an error.

Ported from 7be9540. Sync::sync changed on master since: it already
waits for both preparations before the meta write, so that part is
dropped. After the meta write the rollback cleanup is now awaited before
the beatree task is joined, so that neither an error from the hash-table
writeout nor a panic in the beatree task skips it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Mythos 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_017wb3qEshHAdfkLFsmMCXfc
Each of these fails on master and passes on this branch.

- ht_write_error: every hash-table page write past 40 MiB fails
  (RLIMIT_FSIZE). The commit must report it; before 4eb47b5 it returned
  Ok and the reopened database had a different root.
- overflow_leak: one 256 KiB value rewritten 300 times must not grow the
  leaf file beyond its first 32 MiB step (bc936da).
- commit_panic: a commit that panics after the meta write is durable and
  a later commit attempt on the same handle must not destroy it
  (7be9540, poison on unwind).

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_017wb3qEshHAdfkLFsmMCXfc
The test opened the database on the test thread and moved it, with its
open session, into a helper thread for the commit that is expected to
fail, so that a hang could be reported. A session is about to be tied to
the thread that created it, and the test would no longer compile.

Do all the database work on the helper thread.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_017wb3qEshHAdfkLFsmMCXfc
Comment thread nomt/src/beatree/branch/node.rs Resolved
Comment thread nomt/src/beatree/leaf/node.rs Resolved
citizen-stig and others added 2 commits November 24, 2026 17:43
Replaces the hand-rolled read loop. The caller only recovers from a
non-empty WAL, which is now asserted in debug builds.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
@citizen-stig
citizen-stig marked this pull request as ready for review November 24, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant