nandlog v1.1

Testing

How nandlog is tested, what each check can show, and how much has run on real hardware. Read it if you are deciding whether to trust the library, changing it, or bringing up a board.

Most of nandlog's testing runs on the host. The simulator in sim/ emulates a SPI NAND part at the command level, so the unmodified core and chip driver run on it, and it can cut power or inject a fault at any program or erase. Every check holds the log to its durability contract: once the call that commits a page returns, every record in that page survives any later power cut, intact and exactly once.

The checks come in layers. About a hundred regression tests each target one failure mode. A crash-point campaign cuts power at every operation of fourteen traces. A fuzzer and a bounded-exhaustive check reach states the traces do not, and an ablation shows that the checks fail when any one recovery mechanism is removed.

A simulator shows that the logic holds under every fault it models. It cannot show what a real part does: its timing, its ECC, or what an interrupted operation leaves in its cells. On an Ambiq Apollo4 Plus with the Alliance AS5F18G04SND, an unattended soak of the 1.0 library stopped 2,059 operations with the part's RESET command and found no violation over 1,443 verified boots. With the Winbond W25N01GWZEIG, a soak of the 1.1 library stopped 2,017 operations and found one violation, a residual second program (see On-device tests). Removing power from the board is still to come.

For the hardware status, read On-device tests and What host tests cannot tell you. The other sections say what each host check does and how to run it.

The host simulator

The simulator is a RAM-backed implementation of nandlog_port.h that behaves like a NAND part. Every host test runs the shipping core and the real chip driver on it, built by sim/Makefile with a host C compiler.

cd sim
make              # suite and regression tests (five builds), bad-block answers, parser
make campaign     # crash-point campaign and sweeps (four builds)
make fuzz         # random calls, faults, and cuts against an independent oracle (nine builds)
make exhaustive   # every page-state combination below the head, and every metadata write path, cut
make opcounts     # flash operations and bus bytes of each call
make windows      # regression tests at erase-ahead windows of 0 and 1 (dead-reserve tests at 3)
make headpass     # the NANDLOG_PASS_OVER_HEAD_AT_BOOT share of the exhaustive check and campaign
make soak         # the on-device soak built for the host
make ablation     # each recovery mechanism removed in turn
make baselines    # comparison with Dhara and littlefs

make runs the suite and the regression tests under AddressSanitizer and UndefinedBehaviorSanitizer in five builds: record framing off, framing on, relocation through RAM instead of the part's internal copy, the Winbond driver, and every CRC routed through the port (NANDLOG_PORT_CRC32). It then holds about 11 million of the Alliance driver's answers to nandlog_chip_is_bad_block() to a search of its table, across factory marks, retirements, a table moved below the reserve, and degraded mode. Last, the reference parser must agree with the library on every image the suite dumped.

Every build in sim/Makefile also defines NANDLOG_CHECK_CRC, a test-only switch in nandlog.c. With it, each CRC the log carries forward (e.g., a page's payload CRC, kept up record by record) is compared with a full recompute wherever it is used, and a mismatch aborts the program.

What is emulated. Because the porting seam is at the SPI command level, addressing, status-register handshakes, busy-waiting, and bad-block bookkeeping are all shipping driver code under test. The emulated part takes its geometry from the compiled driver, and it emulates the Winbond part's remap table too. An erase sets bits, and a program can only clear them. A program load resets the one cache register to FFh only from its starting column, as on the Alliance part, and a random-data load (84h) outside an internal data move is refused. A read with ECC off returns the cells as they are.

The simulator counts operations per page and per block, including every second program of a page since its erase, and its snapshots let a campaign restore a nearly full part cheaply for every trial. Most harnesses use fewer blocks than the real part, so that a test can fill the log region in milliseconds: 192 blocks for the regression tests, 128 for the campaign (with an 80-block reserve, so its table trace can retire sixty blocks), and a toy part of four pages a block for the exhaustive check.

What it injects

A fault model is a claim about the medium, and an optimistic one passes code that real silicon breaks. Therefore, the simulator's model is deliberately adversarial. A harness can set any of these faults:

FaultControl
Blocks that always refuse a program, or an erasefaults.unwritable_blocks, faults.unerasable_blocks
Single pages that refuse a programfaults.unwritable_pages
Cells that do not program, while the program reports successfaults.has_stuck_cells
A dead block: every byte reads 00h, and every program and erase is refusednandlog_sim_kill_block()
A power cut at the n-th program or erase, after which nothing runsnandlog_sim_power_cut()
Cut model: the operation never startsNANDLOG_SIM_CUT_BOUNDARY
Cut model: the first half of the page or block takes effectNANDLOG_SIM_CUT_PREFIX
Cut model: each bit the operation would change does, with probability qNANDLOG_SIM_CUT_BITWISE
Cut model: as above, and the undecided bits read randomly until the block is erasedNANDLOG_SIM_CUT_UNSTABLE
An ECC verdict on a page (corrected, uncorrectable, or at the limit) until its block is erasednandlog_sim_set_ecc_status()
Worn cells: pages that read at the ECC limit whatever they hold, through every erasefaults.worn_pages
A page a cut tore reads uncorrectable with ECC onfaults.torn_reads_uncorrectable
A barely programmed page reads as erased with ECC onfaults.ecc_hides_bits
Bit rot in placenandlog_sim_corrupt()
Time passing: weak pages drift (below)nandlog_sim_drift()
A part that never clears BUSYfaults.stay_busy

Weak pages. A page is weak if a cut stopped its program short of a bit it was to change, or if it was programmed into a block whose last erase a cut interrupted, since such a block can read erased and still hold charge unevenly. nandlog_sim_drift() plays a long wait: each weak page drifts back to erased in every bit, or loses each programmed bit with even odds. A page whose program and whose block's erase both completed never drifts. Programming a drifted page again counts as a violation.

The suite and the regression tests

The suite checks ordinary behavior, and the regression tests check the failure modes the design guards against, one at a time. make runs both in all five configurations.

The suite (sim/nandlog_sim_test.c) covers round trips, a record that fills a page exactly, epoch metadata, write-head recovery across a reboot, factory bad-block markers, block retirement, relocation without bus traffic, earlier epochs, torn and rotted pages, a disabled log, reads outside a session, the busy timeout, date-limited reads across a backward time step, exact byte totals, and framed records. It also dumps an image with a relocation and a tombstone in it, for the parser.

The regression tests (sim/nandlog_sim_regress.c) number 108 in the framed Alliance build. By topic:

make windows reruns the regression tests with an erase-ahead window of 0 blocks and of 1 (the default is 2), and the dead-reserve tests at 3. make headpass covers NANDLOG_PASS_OVER_HEAD_AT_BOOT, which the regression tests skip because they check exact head positions that the option moves on purpose. To run some tests alone, pass part of their function names (./nandlog_sim_regress test_k), and set NANDLOG_VERBOSE=1 for the library's diagnostics.

The crash-point campaign

The campaign cuts power at every program and erase of fourteen fixed scenarios, then reboots and audits the log. Each trace is a fixed sequence of library calls from a fixed starting state:

TraceWhat it exercises
basicA first boot, two epochs, partial flushes, a factory-bad block
relocA block failing under the head, and a window block refusing its erase
firstThe epoch's first block failing three pages in
ringSeventy epochs, crossing a metadata ring block
tableFour retirements across the bad-block table's block boundary
fullThe region filling, a warm boot of it full, and a new epoch on it
wrapfailThe last block before the wrap failing
wrapbadAn epoch wrapping onto blocks that refuse an erase or fail
slottornA boot settling a metadata slot torn under the unstable model
tabletornA boot settling a table version torn the same way
blocktornA boot erasing again a block whose only page of the epoch is torn
unwritableA commit no block will take, relocated after a reboot
tornloopTorn commits listed by later boots, with cuts in those boots' slot writes
refusedA block refusing even its mark, and on a dead reserve the table's pair failing too

Each trace runs once uncut, to count its operations. Then, for every operation and each of the four cut models, the part is restored and the trace is cut there, with q drawn from eight values between 0.0005 and 0.995. The device reboots, is audited, logs forty more pages, reboots, and is audited again. A second pass adds a second cut at every operation of the recovering boot, and a third adds it at the first operations of the logging after that boot. An unstable cut earns three more reboots and audits, so two boots cannot read a torn page differently and both pass. Finally, every weak page drifts, and the device is audited twice more.

The audit checks that every record whose commit returned before the cut is delivered intact and once, from its own epoch (unless a later epoch has swept its block), under its position's sequence number, with an exact byte total. It also checks that no page is programmed twice outside a block being retired, that no good block is retired or factory-bad block forgotten, and that a sequence number never serves other content than it served before. Separately, the campaign counts pages that read erased in every bit and were programmed again after a reboot. No read can detect such a page, so the count is reported as a residual risk.

The sweeps. Without cuts, the campaign sweeps recovery over a rotted header, an uncorrectable page, a page at the ECC limit, and a retired block, at every position of short epochs and across the wrap. It then rots, makes unreadable, or reads at the ECC limit each written page of the ring and the table on a part with forty epochs behind it. No such page may change the epoch, its head, the epochs listed, or the blocks retired.

Four builds. The campaign runs as shipped, with an erase-ahead window of 0, with the head pass-over (traces only), and on a part whose reserve's eight table blocks and the region's top two blocks are dead. campaign_default, campaign_w0, campaign_headpass, and campaign_dead run one each. Seeds are fixed, so every run is the same run.

The fuzzer

make fuzz drives the library with random calls and faults, and checks every boot against an oracle written independently of the library. A step is a record of random length and time (sometimes stepping back), a flush, a new epoch, a reboot, or a fault. Faults include blocks and pages that refuse programs or erases (in the log region, the ring, or the reserve), pages read at the ECC limit or not at all, and an unreadable copy of the newest slot or table version. About one step in 30 arms a power cut a few operations ahead, under any cut model, so cuts land in boots too. Now and then weak pages drift before a reboot.

The oracle checks much of what the campaign's audit checks, and also that every valid page sitting at its own position is delivered and that pages fetched by sequence number match the sequential read. It runs twelve fixed seeds of 12,000 steps under the sanitizers, in nine builds: the regression tests' part, 40-block log regions with and without the internal copy, the Winbond driver, a window of 0, the head pass-over, two dead-reserve parts (the table moved below the reserve, and nowhere for it), and the port CRC.

The bounded-exhaustive check

The campaign and the fuzzer reach states by running the library into power cuts. make exhaustive instead lays states down directly and enumerates every combination over the four positions just below the write head of a toy part. A position can be a committed page, a rotted one, or a torn page in any of four listed states: unreadable, with a header that checks over a bad payload, drifted blank, or tombstoned. Directly below the head, it can also be a torn page no boot has listed yet. Five scenarios place the window at each edge of the layout: inside a block, at a block the epoch has not entered, across a retired block, at the epoch's first page, and across the wrap.

For each combination, the boot must find the head, and the medium must satisfy the invariant stated in nandlog.c, read by the check's own parser. After six more pages and a reboot, every committed page and every new one must be delivered once and intact, and no position below the head may serve anything new. Of 18,585 combinations, 10,416 lie within the fault model. The rest hold a committed page that reads blank in every bit, and they must still place the head correctly.

Metadata write paths. The second part runs the real code into nine metadata write paths, among them listing a torn page, beginning an epoch, settling a torn slot, a failed program with its relocation and retirement, and the recovery utility. Each is cut at every operation under eight cut models, and each cut comes alone, with one copy of the newest slot or table version damaged, or with one copy read at the ECC limit. The device then boots and is audited, lets weak pages drift, logs three pages, drifts again, and is audited once more. A refusal to log after a cut alone is a failure. Five builds run: as shipped, with a window of 0, with the head pass-over (metadata only), and two dead-reserve parts with five extra paths that move, rotate, and replace the table.

The ablation: do the checks notice a missing mechanism?

A clean result is only worth something if the checks would catch a broken library. make ablation builds the library once for each NANDLOG_MUTATE_* switch in nandlog.c, each of which removes one recovery mechanism (they are for test builds only and default to 0). Each build runs the regression tests, the exhaustive check, the fuzzer's twelve seeds, and a campaign subset: every sweep, tornloop under all four cut models, and slottorn, tabletorn, blocktorn, first, and unwritable under one each.

Mechanism removedRegression tests that failExhaustive: combinations / metadata trials that failCampaign subset: trials that break the contractFuzz: seeds that report a violation
None: the library as shipped00 / 000 of 12
The passed-over list, in the head search (IGNORE_PASSED_OVER)32,483 / 1,816812 of 12
The erase guard (NO_ERASE_GUARD)210,368 / 9,79100 of 12
One read per slot copy: judged from a second read, used from the first (READ_SLOT_TWICE)10 / 16, then the run aborts00 of 12
The block-entry rule (NO_BLOCK_ENTRY_RULE)40 / 000 of 12
Numbered positions: the head is the first header that reads erased (NAIVE_HEAD_SEARCH)115,131 / 2,3955,13012 of 12
Tombstones: a position that cannot keep its page is left blank (NO_TOMBSTONES)13,011 / 000 of 12

The regression tests notice every removal, and the exhaustive check notices five of the six. It misses the block-entry rule, whose loss shows only when pages programmed into a block an interrupted erase left behind later drift. The campaign subset and the fuzzer notice two each. Given every campaign job instead of the subset (CAMPAIGN=full OUT=ablation_full.out LIMIT=14400 FUZZ_SEEDS= sh ablation.sh in sim/), the campaign notices five of the six, the block-entry rule among them, and misses only the loss of tombstones.

The power-cut soak, on the host

make soak builds the on-device soak, tests/test_nandlog_powercut.c, unchanged for the host, with sim/nandlog_sim_soak.c supplying the board's few functions. It runs a thousand boots over the simulated Alliance part, each with a power cut at random inside a program or erase or between operations, and tests/powercut_check.py checks the serial output. A second run of 60 boots clears one committed bit before boot 45, and the checker must report it.

The comparison with Dhara and littlefs

make baselines runs sim/baselines/reproduce.sh. It fetches Dhara and littlefs at pinned commits into sim/baselines/third_party/, which git ignores, and measures all three stacks' Cortex-M4F footprint if arm-none-eabi-gcc is installed. It then counts each stack's flash operations over the same driver, simulated part, and record stream, and models a durable flush cycle's time and energy from those counts and the benchmark's measured primitives. The top of reproduce.sh says exactly what is compared.

On-device tests

The programs in tests/ answer what a simulator cannot: what the real part does, how long it takes, and whether the contract holds when the part is really interrupted. For how to build, run, and read each one, see tests/README.md.

Status of 1.0 on hardware. The 1.0 library (source ID 98cedad0cec6, code-identical to the captured build 3cb579c85891) has run on an Ambiq Apollo4 Plus (Cortex-M4F, 48 MHz SPI, on-die ECC on) with the Alliance AS5F18G04SND:

Status of 1.1 on the Winbond part. The 1.1 library (source ID 7b01cef16c23) has run on the same MCU with the Winbond W25N01GWZEIG, on a SociTrack revision M board:

A RESET-interrupted operation is a proxy for a brown-out, not a power cut, because the part stays powered. The campaign that removes power from the board (nandlog_powercut, driven by a relay or a switched USB hub) has not yet been run on either part.

Source and build targetWhat it answersWrites to the part
test_nandlog_partial_read.c
nandlog_reads
Do column-addressed and short page reads land where they claim? Header reads and the bad-block scan rest on them.One scratch page
test_nandlog_page_copy.c
nandlog_copy
Does the internal page copy reproduce the source and leave its spare area behind?Three scratch blocks
test_nandlog_ecc_marker.c
nandlog_marker
Can a bad-block mark be programmed into the spare area and read back?Four scratch blocks
test_nandlog_bench.c
nandlog_bench
What each primitive and library call costs in time, operations, and bus bytes.Erases the ring and the whole log region
test_nandlog_interrupt.c
nandlog_interrupt
What a program or erase stopped by RESET leaves on the part.Six blocks mid-region, left erased
test_nandlog_powercut.c
nandlog_powercut, nandlog_selfcut, nandlog_selfcut_inject
Does the contract hold across power cuts, or across RESETs the firmware issues itself, with or without injected failures?The log region, continuously

The first three work only in the top eight blocks of the reserve, which the log never places a page in, and touch neither the log, the ring, nor the bad-block table. Run them in order on a development board, because tests 2 and 3 read back through the primitive test 1 checks. The other three write all over the log, so run them only on a board whose contents do not matter.

Run the read test before you trust a new chip driver. A wrong column byte order does not fail loudly. It reads 0xFF from the wrong place, the factory bad-block scan concludes that every block is good, and nothing is retired. The simulator cannot catch it, because it decodes the column address the same way the driver encodes it.

The reference parser

tools/nandlog_parse.py implements the on-flash and wire formats independently of the library. The device computes CRC-32 with the IEEE 802.3 polynomial so that the parser can verify it with zlib.crc32 and nothing bespoke.

python3 tools/nandlog_parse.py image  dump.bin --page-size 4096 --spare-size 256
python3 tools/nandlog_parse.py stream offload.bin --json
python3 tools/nandlog_parse.py image  dump.bin --expect dump.bin.view

Use it to check a device's output end to end: if the parser and the device disagree, one of them is wrong. make does exactly that with the suite's images, and the parser's verdict on every page must match the library's.

What host tests cannot tell you

Some questions only hardware, or a deployment, can answer: