Question
Sketch a crash-consistency §8.4 argument for separately written candidate data and a selector.
Short answer
A crash-consistency argument fixes the order of the writes and the recovery rule, then checks every point at which power can fail. The candidate is written, checked and made durable first; only then is a new selector record, the small record naming the current version, written to a second location. The proof assumes recovery can recognize a complete record, reject torn records, and order the two generations correctly. It then uses the newest complete record. A crash before that record is complete finds the old record and the untouched old version; a crash after it finds a complete candidate. Either way, one whole version starts.
Terms in the question
Crash: an unplanned stop, such as a power cut or a reset, at any instant. RAM loses its content. Persistent storage, usually flash memory §6.6 on a small device, keeps every write that finished. A write that was in progress may be left incomplete, which is called a torn write §8.19.
Crash consistency: the property that, whatever the instant of a crash, recovery reaches one of a stated set of allowed states. Here the allowed states are two: the old version in force, or the new version in force.
Candidate data: the bytes of the new version, for example a firmware image (the program stored in a device’s flash memory). They go into a storage area of their own, a slot, while the old version stays in a second slot; this layout is an A/B update §8.10.
Selector: a small record in persistent storage that names the slot holding the current version. Writing a new selector record is the commit: the single write that makes the new version current.
Crash-consistency argument: a proof that lists every point at which a crash can cut the sequence of writes, here called a crash cut, and shows that recovery from each cut reaches an allowed state.
Full answer
Step 1: fix the sequence of writes. At the start, slot A holds the old version, and selector location 0 holds the record = (slot A, sequence number , check value). Selector location 1 holds no valid record. The check value is computed from the record’s other bytes, for example as a 32-bit cyclic redundancy check §4.32 (CRC-32), to help detect incomplete or damaged records. The proof below states explicitly which interrupted writes the format must detect. The update then performs these steps in this order.
Erase slot B.
Write into slot B, page by page, the candidate bytes and the signed description of the update, which contains the digest of the candidate, its fixed-length fingerprint computed with a cryptographic hash function §5.1.
Read slot B back, compute the digest of the candidate bytes, and compare it with the digest in the signed description; stop if the two differ.
Wait until every write of steps 1 and 2 is durable, meaning it has reached the storage medium and will survive a power cut.
Erase selector location 1, then write the record = (slot B, sequence number , check value) into it.
Step 2: fix the recovery rule. At every start, the bootloader §6.30, the small program that runs first after a reset, reads both selector locations. It ignores any record whose check value does not match, takes the valid record with the larger sequence number, and checks the content of the slot that this record names against the digest in that slot’s signed description. If the digests match, it starts that slot; if not, it uses the other valid record.
Step 3: state the invariant. An invariant is a statement that must be true at every instant. Here it has three parts.
- (I1)
-
At least one selector location holds a valid record.
- (I2)
-
The newest valid record, the one with the larger sequence number, names a slot whose content is complete and matches the digest in its signed description.
- (I3)
-
That newest valid record names slot A before the commit and slot B after it.
If (I1) to (I3) hold when a crash happens, the recovery rule starts slot A or slot B, which is an allowed state. The argument therefore reduces to two claims: the invariant holds at the start, and no step, interrupted at any point, breaks it.
Step 4: examine every crash cut.
A cut during steps 1 to 4. These steps write only slot B. The newest valid record is , which names slot A, and neither nor slot A is touched, so (I2) still holds. Recovery starts slot A. The partial candidate is harmless and is overwritten by the next attempt.
A cut during step 5. Location 1 holds an erased page, a partial record or the complete record. An erased page counts as empty and a partial record fails its check under the stated fault model §1.30; either is ignored, and recovery uses and starts slot A. A complete record has the larger sequence number and names slot B, which steps 1 to 4 had already made complete and durable, so recovery starts slot B. In both cases was never touched, so (I1) holds.
A cut after step 5. Both records are valid. Because , recovery starts slot B.
The commit therefore happens at one instant: when the last byte of becomes durable. Before that instant every recovery starts slot A; after it, every recovery starts slot B.
Step 5: name the assumptions. The argument holds only if all five of the following are true.
Write ordering §8.20. No part of step 5 reaches the storage medium before steps 1 and 2 are complete. On the internal flash memory of a microcontroller §6.1 (a small computer on one chip), the driver usually waits until each write has finished before it starts the next. A storage device with a write cache needs an explicit flush command.
A torn record is detected. The fault model must ensure recovery ignores any interrupted write or erase that leaves a different, spurious record. A 32-bit CRC detects many error patterns, but distinct byte strings can have the same CRC. A CRC alone cannot establish this assumption for arbitrary corruption. Some flash memory leaves bits whose programming was interrupted in a weak state that can read differently on later reads, so this assumption must be checked against the chip’s documentation.
Separate erase units. Flash memory erases whole pages at a time. Slot A, slot B and each selector location occupy pages of their own, so erasing one never erases another.
No overwrite in place. The new record goes to the location that does not hold the newest valid record. Overwriting in place would need an erase first, and a crash between that erase and the write would leave no valid record at all.
Generation ordering. This example compares 32-bit unsigned sequence numbers as ordinary integers and stops updating when the newest number is 4,294,967,295. Otherwise the next number would wrap to 0, and recovery would incorrectly prefer 4,294,967,295. A design that continues across wraparound needs a different ordering rule with its own assumptions.
Putting all the steps inside one function of the source code does not make them one indivisible step. The guarantee comes only from the order of the writes, the behavior of the storage and the recovery rule.
Worked example
A device has flash memory with pages of 4,096 bytes (4 KiB). Slot A occupies pages 0 to 63 and slot B pages 64 to 127, so each slot holds KiB. Selector location 0 is page 128, and location 1 is page 129. A selector record is 12 bytes: the slot number (4 bytes, 0 for A and 1 for B), the sequence number (4 bytes) and the CRC-32 of those first 8 bytes (4 bytes), in the variant of CRC-32 used by Ethernet and by the zlib compression library. Erased flash reads as bytes of value 0xFF, and the recovery rule treats a location in that state as empty. At the start, location 0 holds = (A, 41) with a correct check value, and location 1 is erased. The candidate with its signed description is 200,000 bytes long. Since , it fills 49 pages, pages 64 to 112.
Power fails while page 94, the 31st page of the candidate, is being written. Location 0 holds the only valid record, = (A, 41). Recovery starts slot A, and the update starts again later.
Power fails after all 49 pages are written and the digest has matched, but before location 1 is written. The outcome is the same: slot A starts.
Power fails while = (B, 42) is being written. The slot and sequence fields were programmed, but the check-value field still reads
0xFFFFFFFF. The 8 programmed bytes are01 00 00 00 2A 00 00 00: the numbers 1 and 42, each stored lowest byte first, where0x2Ais 42 in hexadecimal. Their CRC-32 is0x660790AD, which differs from0xFFFFFFFF, so recovery ignores the record and starts slot A.Power fails after is complete. Both records are valid, , and recovery starts slot B.
The next update writes a candidate into slot A, then erases location 0 in order to write = (A, 43) there. Power fails during that erase. Location 0 may still hold the old record (A, 41), may be empty, or may be rejected as damaged. In all three cases = (B, 42) is still valid, and recovery starts slot B. Because the two locations alternate, the record being written is never the only valid one.
Two broken variants show what the assumptions protect.
One selector location, rewritten in place. The update erases page 128 and then writes (B, 42) there. Power fails between the erase and the write. No valid record exists, so (I1) is broken and the recovery rule has nothing to choose from. Whatever default the bootloader falls back on lies outside the argument and needs a proof of its own.
Clean-up too early. To free space sooner, the updater erases slot A before is durable. A crash in that window leaves as the newest valid record, naming an erased slot, which breaks (I2). No valid version can start.
Follow-up questions and limits
Does this argument cover the whole update? No, only the two slots and the selector. A real update often changes more persistent state §8.1: a trial flag and a start counter for a confirmation window §8.24, an anti-rollback §8.26 floor (the lowest version the device will accept), keys and replay counters. Each extra record adds crash cuts that must be listed. For example, if the updater raises the floor from version 7 to version 8 before writing , a crash between the two writes leaves the selector naming version 7 in slot A while the floor is 8, and a bootloader that enforces the floor refuses to start anything. Writing first, and raising the floor only after version 8 is confirmed, removes that cut.
Why not make the whole update one atomic write? The example flash device supplies no atomic write for a 200,000-byte image. The selector reduces the large update to one small write. Even that write can be interrupted if the record format and storage guarantees together make every harmful partial result detectable under the stated fault model.
How is the argument tested? By power-loss injection §8.38: cutting the power, or forcing a reset, at many points of the write sequence and checking the recovered state each time. Testing samples crash cuts and checks the assumptions on real hardware; the argument covers every cut, provided the assumptions hold.
This answer does not cover whether the new version works once started, which needs a success test and a fallback, nor the wear on the two selector pages, one of which is erased in every update.
Fullest treatment: Crash consistency (§8.4).
Related entries: Commit record and selector (§8.21), Torn write (§8.19), Write ordering (§8.20), Failure atomicity (§8.5), A/B update (§8.10), Inductive invariant and its three obligations (§19.2.4), Power-loss injection (§8.38), Checksum versus CRC (§4.32). Further reading: [154].