Skip to content

fix(storage): fail fast on out-of-bounds PIP/header page reads - #946

Merged
adsharma merged 1 commit into
fix/840-null-scan-memmovefrom
fix/843-pip-bounds-validation
Sep 8, 2026
Merged

adsharma merged 1 commit into
fix/840-null-scan-memmovefrom
fix/843-pip-bounds-validation

Conversation

@adsharma

@adsharma adsharma commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Related to #843.

A corrupted page-index-pointer (PIP) chain turned into a wild pread far beyond EOF (position: 4727899947008 on a 2884-page file), surfacing as a cryptic IO error on every open, including read_only.

  • FileHandle::readPageFromDisk: runtime bounds check throwing RuntimeException naming the offending page and file size (DASSERT alone is stripped in release).
  • PIPWrapper ctor and DiskArrayCollection header-chain walk: validate PIP/header indices before reading.

This converts an unopenable database with a misleading offset into an actionable corruption error, following the existing validateCheckpointPageRange pattern in checkpointer.cpp. It does not fix the original misdirected write — that writer is still unknown — but it bounds the blast radius and makes the next occurrence diagnosable.

@adsharma
adsharma force-pushed the fix/843-pip-bounds-validation branch from 8907442 to f7484ab Compare September 8, 2026 17:51
@adsharma
adsharma marked this pull request as ready for review September 8, 2026 17:51
Corrupted page-index-pointer (PIP) chains previously issued wild preads
far beyond EOF (issue #843: position 4727899947008 on a 2884-page file),
surfacing as a cryptic IO error on every open, including read-only.

Validate page indices against the file size before reading:
- FileHandle::readPageFromDisk now throws a RuntimeException naming the
  offending page and file size (DASSERT alone is stripped in release).
- PIPWrapper and DiskArrayCollection header-chain walk check PIP/header
  indices up front.

This converts an unopenable database with a misleading offset into an
actionable corruption error, and follows the existing
validateCheckpointPageRange pattern in checkpointer.cpp.
@adsharma
adsharma force-pushed the fix/843-pip-bounds-validation branch from f7484ab to 962603d Compare September 8, 2026 19:00
@adsharma
adsharma merged commit 0cb5839 into main Sep 8, 2026
4 checks passed
@adsharma
adsharma deleted the fix/843-pip-bounds-validation branch September 8, 2026 19:04
fabzter pushed a commit to fabzter/ladybug that referenced this pull request Sep 9, 2026
…write path

A page index becomes a file offset on the write path exactly as it does on the
read path, but only reads validated it (LadybugDB#946). Nothing checked the ranges the
allocator handed out, so a corrupted free page list silently extended the data
file instead of failing: on the database from LadybugDB#843, PageManager returned page
212,650,604 of a 2884-page file and a checkpoint column flush wrote there,
leaving an 11.8 MB database reporting 892.2 GiB of apparent size (LadybugDB#948).
Allocated blocks barely move, so nothing looks wrong until a copy or backup
materialises the sparse hole, which filled a 926 GiB volume here.

Validation at three levels, from the source outwards:

- PageManager::allocatePageRange rejects a range that is not inside the data
  file, naming the range and the file's page count. The free list is
  deserialized from the database file, so a corrupted entry surfaces here
  first, and nothing shrinks the data file, so a legitimate free entry is
  always inside it. The existing DASSERT on the extension path is kept and
  backed by a real check, since DASSERT is stripped in release builds.
- FileHandle::validatePageIdxForWrite guards flushPageIfDirtyWithoutLock and
  writePagesToFile, which cover the checkpoint column flushes and the spiller,
  so any other source of a bad index also fails at the write instead of
  extending the file.
- ShadowFile::applyShadowPages and replayShadowPageRecordsCore validate
  record.originalPageIdx before writing into the data file. Those indices come
  from deserialized shadow records, and the recovery path runs before any
  FileHandle exists, so it is bounded by the old header's page count plus the
  number of shadow pages: a checkpoint can only grow the file by pages it
  shadowed.

Zero-page allocations and zero-length writes are exempt: they write nothing,
and their start index legitimately sits at the current end of the file.

Verified against the LadybugDB#948 reproducer (a content-scrubbed copy of the LadybugDB#843
database, https://github.com/fabzter/ladybug-948-repro):

  Page allocation from the free page list returned pages
  [212650604, 212650605), which are out of bounds for a data file with 2884
  pages. The free page list may be corrupted.

The file stays 11.8 MB instead of reaching 892 GiB, and the error identifies
the corrupted free list rather than surfacing as a SIGSEGV. Checks for false
positives: 2000-cycle insert+CHECKPOINT soaks on a clean store carrying FTS and
HNSW indexes pass on two seeds; a smoke covering DDL, 3000 inserts with strings
up to 3 KB, deletes that free pages, reuse of those freed pages through the
free list, and EXPORT/IMPORT all pass. LadybugDB#940 still reproduces, so the guards
neither fire spuriously nor mask it.
fabzter pushed a commit to fabzter/ladybug that referenced this pull request Sep 9, 2026
A page index becomes a file offset on the write path exactly as it does on the
read path, but only reads validated it (LadybugDB#946). Nothing checked the ranges the
allocator returned, so a corrupted free page list silently extended the data
file instead of failing: on the database from LadybugDB#843, PageManager returned page
212,650,604 of a 2884-page file and a checkpoint column flush wrote there,
leaving an 11.8 MB database reporting 892.2 GiB of apparent size (LadybugDB#948).
Allocated blocks barely move, so nothing looks wrong until a copy or backup
materialises the sparse hole, which filled a 926 GiB volume here.

PageManager::allocatePageRange now rejects a range that is not inside the data
file, naming the range and the file's page count. The free list is deserialized
from the database file, so a corrupted entry surfaces here first, and nothing
shrinks the data file, so a legitimate free entry is always inside it. The
existing DASSERT on the extension path is kept and backed by a real check,
since DASSERT is stripped in release builds. Zero-page allocations are exempt:
they write nothing, and their start index legitimately sits at the current end
of the file.

Verified against the LadybugDB#948 reproducer (a content-scrubbed copy of the LadybugDB#843
database, https://github.com/fabzter/ladybug-948-repro):

  Page allocation from the free page list returned pages
  [212650604, 212650605), which are out of bounds for a data file with 2884
  pages. The free page list may be corrupted.

The file stays 11.8 MB instead of reaching 892 GiB, and the error identifies
the corrupted free list rather than surfacing as a SIGSEGV. Checks for false
positives: 2000-cycle insert+CHECKPOINT soaks on a clean store carrying FTS and
HNSW indexes pass on two seeds, and a smoke covering DDL, 3000 inserts with
strings up to 3 KB, deletes that free pages, reuse of those freed pages through
the free list, and EXPORT/IMPORT all pass.
fabzter pushed a commit to fabzter/ladybug that referenced this pull request Sep 9, 2026
A page index becomes a file offset on the write path exactly as it does on the
read path, but only reads validated it (LadybugDB#946). Nothing checked the ranges the
allocator returned, so a corrupted free page list silently extended the data
file instead of failing: on the database from LadybugDB#843, PageManager returned page
212,650,604 of a 2884-page file and a checkpoint column flush wrote there,
leaving an 11.8 MB database reporting 892.2 GiB of apparent size (LadybugDB#948).
Allocated blocks barely move, so nothing looks wrong until a copy or backup
materialises the sparse hole, which filled a 926 GiB volume here.

PageManager::allocatePageRange now rejects a range that is not inside the data
file, naming the range and the file's page count. The free list is deserialized
from the database file, so a corrupted entry surfaces here first, and nothing
shrinks the data file, so a legitimate free entry is always inside it. The
existing DASSERT on the extension path is kept and backed by a real check,
since DASSERT is stripped in release builds. Zero-page allocations are exempt:
they write nothing, and their start index legitimately sits at the current end
of the file.

Verified against the LadybugDB#948 reproducer (a content-scrubbed copy of the LadybugDB#843
database, https://github.com/fabzter/ladybug-948-repro):

  Page allocation from the free page list returned pages
  [212650604, 212650605), which are out of bounds for a data file with 2884
  pages. The free page list may be corrupted.

The file stays 11.8 MB instead of reaching 892 GiB, and the error identifies
the corrupted free list rather than surfacing as a SIGSEGV. Checks for false
positives: 2000-cycle insert+CHECKPOINT soaks on a clean store carrying FTS and
HNSW indexes pass on two seeds, and a smoke covering DDL, 3000 inserts with
strings up to 3 KB, deletes that free pages, reuse of those freed pages through
the free list, and EXPORT/IMPORT all pass.
adsharma pushed a commit that referenced this pull request Sep 9, 2026
A page index becomes a file offset on the write path exactly as it does on the
read path, but only reads validated it (#946). Nothing checked the ranges the
allocator returned, so a corrupted free page list silently extended the data
file instead of failing: on the database from #843, PageManager returned page
212,650,604 of a 2884-page file and a checkpoint column flush wrote there,
leaving an 11.8 MB database reporting 892.2 GiB of apparent size (#948).
Allocated blocks barely move, so nothing looks wrong until a copy or backup
materialises the sparse hole, which filled a 926 GiB volume here.

PageManager::allocatePageRange now rejects a range that is not inside the data
file, naming the range and the file's page count. The free list is deserialized
from the database file, so a corrupted entry surfaces here first, and nothing
shrinks the data file, so a legitimate free entry is always inside it. The
existing DASSERT on the extension path is kept and backed by a real check,
since DASSERT is stripped in release builds. Zero-page allocations are exempt:
they write nothing, and their start index legitimately sits at the current end
of the file.

Verified against the #948 reproducer (a content-scrubbed copy of the #843
database, https://github.com/fabzter/ladybug-948-repro):

  Page allocation from the free page list returned pages
  [212650604, 212650605), which are out of bounds for a data file with 2884
  pages. The free page list may be corrupted.

The file stays 11.8 MB instead of reaching 892 GiB, and the error identifies
the corrupted free list rather than surfacing as a SIGSEGV. Checks for false
positives: 2000-cycle insert+CHECKPOINT soaks on a clean store carrying FTS and
HNSW indexes pass on two seeds, and a smoke covering DDL, 3000 inserts with
strings up to 3 KB, deletes that free pages, reuse of those freed pages through
the free list, and EXPORT/IMPORT all pass.
adsharma pushed a commit that referenced this pull request Sep 10, 2026
A page index becomes a file offset on the write path exactly as it does on the
read path, but only reads validated it (#946). Nothing checked the ranges the
allocator returned, so a corrupted free page list silently extended the data
file instead of failing: on the database from #843, PageManager returned page
212,650,604 of a 2884-page file and a checkpoint column flush wrote there,
leaving an 11.8 MB database reporting 892.2 GiB of apparent size (#948).
Allocated blocks barely move, so nothing looks wrong until a copy or backup
materialises the sparse hole, which filled a 926 GiB volume here.

PageManager::allocatePageRange now rejects a range that is not inside the data
file, naming the range and the file's page count. The free list is deserialized
from the database file, so a corrupted entry surfaces here first, and nothing
shrinks the data file, so a legitimate free entry is always inside it. The
existing DASSERT on the extension path is kept and backed by a real check,
since DASSERT is stripped in release builds. Zero-page allocations are exempt:
they write nothing, and their start index legitimately sits at the current end
of the file.

Verified against the #948 reproducer (a content-scrubbed copy of the #843
database, https://github.com/fabzter/ladybug-948-repro):

  Page allocation from the free page list returned pages
  [212650604, 212650605), which are out of bounds for a data file with 2884
  pages. The free page list may be corrupted.

The file stays 11.8 MB instead of reaching 892 GiB, and the error identifies
the corrupted free list rather than surfacing as a SIGSEGV. Checks for false
positives: 2000-cycle insert+CHECKPOINT soaks on a clean store carrying FTS and
HNSW indexes pass on two seeds, and a smoke covering DDL, 3000 inserts with
strings up to 3 KB, deletes that free pages, reuse of those freed pages through
the free list, and EXPORT/IMPORT all pass.
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