← All posts
hardware

Build the black box before the crash

· 6 min read · by aft3r_d0t

Duotone ink illustration of a boxy metal equipment case seen from a low angle, dissolving into the dark.

Altar, the desk screen I’ve written about before, now has a flight recorder. It caught its first crash 45 minutes after it was switched on. That one crash told us exactly what to fix, down to the byte.

A crash you can’t see is a crash you can’t fix

Altar is an ESP32-S3-BOX-3, a small touchscreen board, driven by a hub on the Mac over a USB cable. When firmware like this panics, the board reboots. Without a record, all you see is a face that blinked. You don’t learn which task died, or why.

We had seen that blink before. Before the recorder existed, a quick look at the board’s earlier resets showed that every panic they had reported was the firmware calling abort on itself. So the cause of each one sat in a short message and in the code that called it, and the board kept neither. We knew it had crashed. We didn’t know what it was doing at the time.

So before anything else went wrong, we built the recorder.

What the recorder is

Three parts.

On the board, a 128 KB partition on flash keeps a core dump: the state of the crashed task, its stack and its registers, written at the moment of the panic. The space was carved from the end of the app’s own partition, so the faces and sprites further along the flash didn’t move. Next to the dump, a few bytes of memory that survive a reset keep the panic’s message.

If a second panic comes before anyone has read the first, the first one is kept. The first crash is usually the honest one; the second is often the board tripping over the mess the first one left. The one who checked the recorder switched that rule off on purpose, in a copy, and two tests went red. Then she put it back.

On the next boot, the board tells the hub that there’s a dump, along with what it knows about the reset.

On the hub, a decoder turns the dump back into names: function and line. It does that against the exact firmware image that crashed. It keeps several builds around and picks the one whose checksum matches. If none matches, it says “undecoded” and moves on without crashing itself. To test it, the checker built her own dump from a known image and handed the decoder three images to choose from. It picked the right one and named all three functions on the stack correctly, file and line.

What it costs

The recorder isn’t free. It costs about 4.3 KB of internal RAM. The plan had allowed 512 bytes. Our own code came to 131 bytes. The rest is the framework’s: the core-dump code needs its own stack and its own buffers, and with our settings they have to live in fast internal memory. You can’t measure your way under a floor.

So the budget was rewritten from the measurement, and whether the trade is worth it came to me as a decision. Memory on this board is tight: the lowest point of free internal RAM during boot was about 13.5 KB, and with the recorder it’s estimated at about 9.2. There are three cheaper versions written down next to it: let task stacks live in external memory and free 1.9 KB, shrink a buffer used by a startup self-test, or keep only the panic message and drop the full dump. For now the recorder ships as built.

The first entry

45 minutes after the first boot with the recorder, it caught this:

***ERROR*** A stack overflow in task climate has been detected.

The climate task reads a small temperature and humidity sensor and sends the readings to the hub. Its stack was 4,096 bytes.

Where the bytes went

With the crash named, a static tool walked the firmware’s call graph and added up the worst-case stack of every task. The climate task came out 768 bytes over its budget. The presence task was 768 over too, and the face task 128. They just hadn’t crashed yet.

The tool itself got the same treatment as everything else. Teach it to read only decimal frame sizes, and 6 tests fail; make the board report its stack in the wrong unit, and 2 fail. A tool that counts bytes has to be shown it can miscount.

The cause was the same in every task that sends a message. On the way to the USB cable, one call path stacked up:

  • a message structure, 976 bytes,
  • a line buffer, 1,072 bytes,
  • a base64 copy of the payload, 720 bytes,
  • and snprintf, which needs about 1 KB on its own.

On a 4,096-byte stack, that’s the whole stack, before the task has done any work of its own.

The fix

Not “give every task more stack”. The fix took the cause off the path:

  • numbers go into the line without snprintf;
  • base64 is streamed straight into the line, so the separate copy is gone;
  • the line buffer is one static buffer, shared under the lock that already guards sending;
  • logging a failed encode moved to the USB task, which has room for it;
  • the message structure moved out of the function that held it on the stack.

Only the climate task got more stack, from 4,096 to 4,352 bytes, because the sensor driver’s own error logging needs it. After the fix, every task has at least 1 KB of headroom by the static count, and the whole change costs 1,344 bytes of RAM against a budget of 2,048.

Changing how numbers are written risks changing what goes over the wire. So a differential test ran the new encoders against the old snprintf formats: 269,996 cases, 0 differences.

It stays fixed

A guard test now reads the stack sizes from the source and fails if a task drops below its measured need. Setting the climate stack back to 4,096 makes the guard fail; we tried. And after every heap report the board now says how much stack each task has left. The hub warns once per task per boot when one drops under 512 bytes, so the warning comes before the next overflow instead of after it.

The first real numbers from the board, right after the new firmware went on: the tightest of the seven tasks, climate, had 2,528 bytes of stack free, and the roomiest had 6,912. All of them sat above the static floor, which is what you’d expect: the static count is the worst case, and a real afternoon is rarely the worst case.

If you build small devices, put the crash recorder in before you need it. The first crash it catches is the cheapest bug report you’ll ever get.

— Max