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:
| Fault | Control |
|---|---|
| Blocks that always refuse a program, or an erase | faults.unwritable_blocks, faults.unerasable_blocks |
| Single pages that refuse a program | faults.unwritable_pages |
| Cells that do not program, while the program reports success | faults.has_stuck_cells |
A dead block: every byte reads 00h, and every program and erase is refused | nandlog_sim_kill_block() |
| A power cut at the n-th program or erase, after which nothing runs | nandlog_sim_power_cut() |
| Cut model: the operation never starts | NANDLOG_SIM_CUT_BOUNDARY |
| Cut model: the first half of the page or block takes effect | NANDLOG_SIM_CUT_PREFIX |
| Cut model: each bit the operation would change does, with probability q | NANDLOG_SIM_CUT_BITWISE |
| Cut model: as above, and the undecided bits read randomly until the block is erased | NANDLOG_SIM_CUT_UNSTABLE |
| An ECC verdict on a page (corrected, uncorrectable, or at the limit) until its block is erased | nandlog_sim_set_ecc_status() |
| Worn cells: pages that read at the ECC limit whatever they hold, through every erase | faults.worn_pages |
| A page a cut tore reads uncorrectable with ECC on | faults.torn_reads_uncorrectable |
| A barely programmed page reads as erased with ECC on | faults.ecc_hides_bits |
| Bit rot in place | nandlog_sim_corrupt() |
| Time passing: weak pages drift (below) | nandlog_sim_drift() |
| A part that never clears BUSY | faults.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:
- The write head (the next position the log will program) never moves for a fault anywhere in the epoch, and no torn, drifted, or blank page below it pulls it back. The head search's first probes move from boot to boot.
- Erases happen once per block, never in the call that commits a page, and never reach the current epoch's own blocks. A block whose erase a power cut interrupted is erased again before the head programs it.
- Relocation never trusts a target that refused a copy or an erase, and moves an unreadable page as a tombstone. A page that failed a program is never programmed again, even after a reboot.
- The bad-block table never rolls back after a cut, a torn version, a torn mark, or a stray
bit in a mark. The recovery utility (
nandlog_reset_bad_block_table()) keeps every retired block. - With a dead reserve, the table moves below it once, and later boots find it cheaply. With nowhere for the table, a block's own mark retires it. With no free pair at all, the log stops and says so.
- Format 1's pages, left by 1.0 or earlier, are never adopted or counted as the new epoch's, and a boot over them latches no more pages than one over erased blocks. After the whole ring is lost, the next epoch is numbered above the pages before it, and an epoch begun on a part with a ring reads no more than a handful of pages.
- The metadata ring survives torn, unreadable, and half-written slots, and a lost slot is recovered from the epoch's pages. A sealed epoch stays sealed, and a new epoch begun after it erases none of its pages. A warm boot reads a handful of ring pages.
- A metadata copy at the ECC limit is written again elsewhere, once, and a page stuck there costs one write, not one per boot.
- Reads declare exact byte totals, never write past a payload's worth of the caller's buffer, and never serve other records under a sequence number already served.
- The API says why it refuses each record, never reports a dropped record stored, catches a payload that did not program, and takes the library's lock in every accessor.
- The CRC-32 is zlib's, continues exactly from any split, and matches the payload on every path that commits or serves a page.
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:
| Trace | What it exercises |
|---|---|
basic | A first boot, two epochs, partial flushes, a factory-bad block |
reloc | A block failing under the head, and a window block refusing its erase |
first | The epoch's first block failing three pages in |
ring | Seventy epochs, crossing a metadata ring block |
table | Four retirements across the bad-block table's block boundary |
full | The region filling, a warm boot of it full, and a new epoch on it |
wrapfail | The last block before the wrap failing |
wrapbad | An epoch wrapping onto blocks that refuse an erase or fail |
slottorn | A boot settling a metadata slot torn under the unstable model |
tabletorn | A boot settling a table version torn the same way |
blocktorn | A boot erasing again a block whose only page of the epoch is torn |
unwritable | A commit no block will take, relocated after a reboot |
tornloop | Torn commits listed by later boots, with cuts in those boots' slot writes |
refused | A 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 removed | Regression tests that fail | Exhaustive: combinations / metadata trials that fail | Campaign subset: trials that break the contract | Fuzz: seeds that report a violation |
|---|---|---|---|---|
| None: the library as shipped | 0 | 0 / 0 | 0 | 0 of 12 |
The passed-over list, in the head search (IGNORE_PASSED_OVER) | 3 | 2,483 / 1,816 | 8 | 12 of 12 |
The erase guard (NO_ERASE_GUARD) | 2 | 10,368 / 9,791 | 0 | 0 of 12 |
One read per slot copy: judged from a second read, used from the first (READ_SLOT_TWICE) | 1 | 0 / 16, then the run aborts | 0 | 0 of 12 |
The block-entry rule (NO_BLOCK_ENTRY_RULE) | 4 | 0 / 0 | 0 | 0 of 12 |
Numbered positions: the head is the first header that reads erased (NAIVE_HEAD_SEARCH) | 11 | 5,131 / 2,395 | 5,130 | 12 of 12 |
Tombstones: a position that cannot keep its page is left blank (NO_TOMBSTONES) | 1 | 3,011 / 0 | 0 | 0 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:
nandlog_benchmeasured the costs on the Performance page.nandlog_interruptstopped programs and erases with the part's RESET command (FFh) in 672 program trials and 42 erase trials. A RESET at 50 µs or later into a BLOCK ERASE never shortened it: the block always finished erasing.- The unattended soak (
nandlog_selfcutandnandlog_selfcut_inject) stopped its own programs and erases at random with RESET and then reset the MCU, andtests/powercut_check.pychecked the contract at every boot. It passed 2,059 interrupted operations (1,579 programs and 480 erases) over 1,443 verified boots with no violation. These included 8 injected program and erase failures, which retired blocks and relocated the write head.
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:
- Tests 1 and 2 passed, the second run of test 2 after a power cycle. Test 3 does not apply, because the part retires blocks through its own remap table rather than a mark.
nandlog_benchran clean twice. A header read costs 177 µs and a whole-page read 512 µs, a commit 1.4–1.6 ms, and a full block's erase 0.7 ms.nandlog_interruptran 672 program trials and 48 erase trials. A program stopped within its first 20 µs left the page reading erased in every bit, 127 times in 127, and a second program over such a page read back clean every time. An erase stopped at 50 µs left 2 or 3 of a block's 64 pages unerased, a torn erase; stopped at 75 µs or later, it always finished.- The soak stopped 2,017 operations over 1,432 verified boots, with 2 injected program failures
that retired blocks and relocated the write head. It found one violation: a
program stopped 23 µs in left its page reading erased, the next boot took that page for the write
head and programmed it again, the page read back whole, and a boot later it was unreadable, losing an
acknowledged record. That is a residual second program, the limit
Durability describes, and
NANDLOG_PASS_OVER_HEAD_AT_BOOTrules it out.
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 target | What it answers | Writes to the part |
|---|---|---|
test_nandlog_partial_read.cnandlog_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.cnandlog_copy | Does the internal page copy reproduce the source and leave its spare area behind? | Three scratch blocks |
test_nandlog_ecc_marker.cnandlog_marker | Can a bad-block mark be programmed into the spare area and read back? | Four scratch blocks |
test_nandlog_bench.cnandlog_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.cnandlog_interrupt | What a program or erase stopped by RESET leaves on the part. | Six blocks mid-region, left erased |
test_nandlog_powercut.cnandlog_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:
- Timing, ECC, and the board. The simulator says nothing about them. The benchmark measures timing on the Alliance part.
- The fault models. They are adversarial by design, not characterized against silicon. They take a page that reads whole as committed: nothing in them makes a fully programmed cell drift back, or makes a page that read blank in every bit hold charge that a second program would disturb.
- Power cuts. On hardware, RESET has so far stood in for a power cut. RESET stops the part on a healthy supply, while a failing supply takes the part down with it. The campaign that removes power from the board is still to come.
- Torn erases. The Alliance part always finished an erase that RESET interrupted 50 µs or more after it began, so its soak could not tear one. The Winbond part tears an erase stopped about 50 µs in, and 20 of its soak's 230 erase cuts landed between 25 and 75 µs.
- The Winbond part's remap table. The soak wrote entries to it, but no cut landed in one, so that an entry is written whole or not at all across a cut is still assumed.
- The long run. No test here covers retention over months, temperature, or wear over a part's life. Only a deployment shows them.