oxidecomputer / hubris

A lightweight, memory-protected, message-passing kernel for deeply embedded systems.
Mozilla Public License 2.0
2.96k stars 169 forks source link

hf-api: knock 2 kiB off stack usage #1874

Closed cbiffle closed 1 week ago

cbiffle commented 1 week ago

hf-api includes a function that computes a CRC32 of a chunk of data. Unfortunately, it made the (easy, common) mistake of allocating a crc::Crc on the stack.

The upstream crc crate, despite our protests, optimizes for time over space. A crc::Crc includes a lookup table to accelerate the calculation of the CRC. Creating one at runtime on the stack means

  1. The lookup table is on your stack (all 2048 bytes of it, in this case), and

  2. Your function now contains code to generate the lookup table.

This is a footgun in the crc API design, IMO. It's far too easy to make this mistake.

Anyway, made it static for a 2 kiB improvement in stack margin.