zlmdb._lmdb_vendor.verify

Offline structural verifier for LMDB data files (verify-then-trust).

This is a pure-Python byte walker. It never hands the file to liblmdb, so it is safe to run on a file from an untrusted source: nothing here can be made to crash or corrupt the process by hostile input the way opening the file through the engine could. That is the whole point – it is the tool that lets a user turn “a file I do not control” into “a file I have checked and may now trust”.

It verifies every invariant the LMDB write paths assume, against a global view of the file at rest:

  • both meta pages parse and the committed one is selected as the engine would;

  • every page reachable from the committed roots (main DB, the freeDB, every named sub-DB, dup sub-trees, inline sub-pages and overflow extents) is structurally sound, stores its own page number, and is reachable exactly once;

  • keys are ordered under the comparator each DB’s flags imply, and each DB’s page/entry/depth counters match what the walk actually finds (DUPSORT page counts written before ITS#9388’s aggregation are also accepted);

  • no reachable page carries a dirty marker (0.9 P_DIRTY; 1.0 an mp_txnid newer than the committed meta);

  • freeDB records are structurally valid, and – the global check that only an offline pass can make – the free set and the live set are disjoint and together cover exactly [NUM_METAS, next_pgno).

Scope and caveats (see docs/verifier-plan.md for the full threat model):

  • LMDB data files are architecture-specific. This verifier answers “is this file safe for the LMDB engine running on this host”, assuming the host’s native byte order and a 64-bit build (8-byte pgno/txnid/size fields). A 32-bit file, or one written on the other endianness, is reported as unsupported rather than silently mis-parsed.

  • Verification is a point-in-time statement. It proves nothing unless the file cannot be touched by an untrusted writer afterwards (verify a private copy; regenerate lock.mdb; treat every later co-writer as trusted).

Attributes

Exceptions

VerifyError

Raised for conditions that stop verification before it can even begin

Classes

_Db

An MDB_db record (48 bytes on 64-bit, identical on both engines).

_Meta

An MDB_meta record, located at PAGEHDRSZ within a meta page.

_SubTree

_Verifier

Functions

_bad_db_flags(flags)

_cmp_int(a, b)

_cmp_lexical(a, b)

_cmp_reverse(a, b)

_data_cmp(flags)

_even(n)

_key_cmp(flags)

_printable(key)

resolve_data_path(path[, subdir])

Return the path to the actual data.mdb file.

verify(path[, subdir])

Fully verify an LMDB data file offline.

Module Contents

CURSOR_STACK = 32[source]
DUPSORT_ONLY_FLAGS = 112[source]
F_BIGDATA = 1[source]
F_DUPDATA = 4[source]
F_SUBDATA = 2[source]
MDB_DUPFIXED = 16[source]
MDB_DUPSORT = 4[source]
MDB_INTEGERDUP = 32[source]
MDB_INTEGERKEY = 8[source]
MDB_MAGIC = 3203383518[source]
MDB_REVERSEDUP = 64[source]
MDB_REVERSEKEY = 2[source]
MDB_VALID = 32768[source]
NUM_METAS = 2[source]
PERSISTENT_FLAGS = 32767[source]
P_BRANCH = 1[source]
P_DIRTY_09 = 16[source]
P_INVALID = 18446744073709551615[source]
P_LEAF = 2[source]
P_LEAF2 = 32[source]
P_META = 8[source]
P_OVERFLOW = 4[source]
P_SUBP = 64[source]
SIZEOF_MDB_DB = 48[source]
exception VerifyError[source]

Bases: Exception

Raised for conditions that stop verification before it can even begin (not an LMDB data file, an unsupported build, unreadable meta pages). Ordinary corruption findings are collected and returned, not raised.

class _Db(buf, off)[source]

An MDB_db record (48 bytes on 64-bit, identical on both engines).

__slots__ = ('pad', 'flags', 'depth', 'branch_pages', 'leaf_pages', 'overflow_pages', 'entries', 'root')[source]
_E = '<'[source]
_ENGINES[source]
class _Meta(buf, base, hdrsz)[source]

An MDB_meta record, located at PAGEHDRSZ within a meta page.

__slots__ = ('pgno', 'flags', 'magic', 'version', 'mapsize', 'psize', 'free', 'main', 'last_pg', 'txnid')[source]
free[source]
main[source]
psize[source]
_ORDER = 'little'[source]
class _SubTree(name, db, key_cmp, dup_cmp)[source]
__slots__ = ('name', 'db', 'key_cmp', 'dup_cmp')[source]
db[source]
dup_cmp[source]
key_cmp[source]
name[source]
_TYPE_MASK = 111[source]
_U16[source]
_U32[source]
_U64[source]
class _Verifier(f, filesize)[source]
MAX_ERRORS = 200[source]
_bigdata(ctx, pgno, i, buf, doff, dsize, want_bytes)[source]

Validate an F_BIGDATA overflow extent, mark its pages, and return the data bytes when want_bytes (freeDB IDLs need them) else None.

_check_btree_bounds(buf, base, span, where)[source]

Validate mp_lower/mp_upper for a B-tree page or sub-page whose body occupies span bytes from base. Returns (upper, numkeys) or None.

_check_counts(name, db, ctx)[source]
_check_cover()[source]
_check_not_dirty(name, pgno, buf, flags)[source]
_decode_idl(txnid, data)[source]
_flags_at_rest_ok(flags)[source]
_free_record(pgno, i, key, data)[source]
_leaf_value(ctx, pgno, i, buf, o, nflags, ksize, dsize, key)[source]
_mark_live(pgno, what)[source]

Mark pgno reachable. Returns False (recording an error) if it is out of range or was already reachable – a page reachable twice, or as a meta page, is corruption.

_node_off(buf, base, i)[source]
_numkeys(lower)[source]
_pg_flags(buf, base)[source]
_pg_lower(buf, base)[source]
_pg_pad(buf, base)[source]
_pg_pages(buf, base)[source]
_pg_pgno(buf, base)[source]
_pg_txnid(buf, base)[source]
_pg_upper(buf, base)[source]
_read_at(off, n)[source]
_read_meta(pgno, psize)[source]

Read page pgno as a meta page. Returns a _Meta, or raises VerifyError if it cannot be parsed as one.

_validate_dup_subdb(name, pgno, i, subdb)[source]
_validate_named_subdb(name, pgno, i, subdb)[source]
_verify_tree(name, db, key_cmp, dup_cmp, is_free)[source]

Validate one B-tree and cross-check its counters.

Returns (branch, leaf): the page totals including any promoted dup sub-trees, since a DUPSORT DB’s md_branch/leaf_pages are that aggregate (mdb_subdb_adjust folds each dup sub-tree’s deltas into its parent). A named sub-DB is a wholly independent tree instead: it does not fold into its container and is verified on its own afterwards.

_walk_branch(ctx, pgno, buf, depth)[source]
_walk_leaf(ctx, pgno, buf)[source]
_walk_leaf2(ctx, pgno, buf)[source]
_walk_page(ctx, pgno, depth)[source]
_walk_subpage(ctx, pgno, i, buf, doff, dsize)[source]

Validate an inline P_SUBP sub-page (a key’s duplicate set) and return the number of data items it holds.

detect_engine()[source]
eng: dict[source]
err(msg)[source]
errors = [][source]
f[source]
filesize[source]
free: bytearray[source]
free_records = [][source]
property full[source]
hdrsz: int[source]
live: bytearray[source]
load_metas()[source]

Validate both meta pages the way mdb_env_read_header does, then select the committed one as mdb_env_pick_meta would.

meta: _Meta[source]
next_pgno: int[source]
pagebase: int[source]
psize: int[source]
read_extent(pgno, npages)[source]
read_page(pgno)[source]

Return psize bytes for page pgno, or None if it lies beyond EOF (legal only for a free page, which is never read).

verify()[source]
_bad_db_flags(flags)[source]
_cmp_int(a, b)[source]
_cmp_lexical(a, b)[source]
_cmp_reverse(a, b)[source]
_data_cmp(flags)[source]
_even(n)[source]
_key_cmp(flags)[source]
_printable(key)[source]
resolve_data_path(path, subdir=None)[source]

Return the path to the actual data.mdb file.

If subdir is None it is inferred: a directory is treated as an LMDB environment directory (<path>/data.mdb); anything else is the data file itself (an environment opened with subdir=False).

verify(path, subdir=None)[source]

Fully verify an LMDB data file offline.

path is an environment (a directory, or a single data file); see resolve_data_path. Returns a list of human-readable problem strings; an empty list means the file passed every invariant checked.

Raises VerifyError only when verification cannot begin at all.