Skip to content

How it works

main.gooseyour program, withimport mcu; Goose compilerofficial release,writes one C file MicroGoosesizes data stacks,adds the board port Board toolchainESP-IDF, STM32Cubeor the Pico SDK Firmwarefor the ESP32s, theSTM32H7 or the Pico a board, over USB QEMU or Renode Velxio, in a browser

In more detail, with the board's port:

app.goose ──goose -o──▶ app.c ──┐
lib/mcu.goose (extern fns)      ├─ fixes ───────▶ PlatformIO ─▶ firmware
goose --emit-runtime ─▶ runtime ┘                    │
                                   port: POSIX shim (mmap, clocks, stubs)
                                         HAL (GPIO, LED, ADC, time)
                                         main → Goose main()

MicroGoose never modifies the Goose compiler. For each build it (microgoose/core.py):

  1. stages the program next to lib/*.goose, so that import mcu; resolves;
  2. runs goose -o app.c and, when the compiler version changes, goose --emit-runtime;
  3. shrinks each data stack's reservation to --reserve;
  4. applies two source fixes that make the C correct on 32-bit MCUs (below);
  5. hands the C to the target's PlatformIO project. The port supplies the rest.

Data stacks

Goose has no heap. A slice that grows, a string being built, or a tree under construction all live on data stacks: contiguous regions where a push is an unchecked pointer bump. On a PC each one reserves 2 GB of address space, with an unmapped guard page behind it, and running into that page is how overflow is detected.

MCUs have kilobytes of RAM and, mostly, no MMU. So MicroGoose:

  • shrinks each reservation to --reserve (16 KB by default). This is safe because the compiler's elided checks assume at most the reservation;
  • lets a worker's stacks have their own size. A board commits every byte it reserves, so a program whose main stacks need 16 KB would pay 16 KB for each stack of each thread_spawn worker too. --worker-reserve sets what those reserve, and --worker-stack the size of a worker's native stack. On the ESP32s a worker's data stack is a block the heap can give to the next worker's data stack once it is freed;
  • guards the end of each stack with what the chip has:
First stacks The rest
ESP32 / ESP32-S3 a hardware data watchpoint per stack (2 per core), which trap on the first store past the end a 64-byte canary, checked every 100 ms and at exit
STM32H7 an MPU no-access region per stack (14 available), which fault on the first store past the end a 32-byte canary, checked every 100 ms and at exit

Either way, an overflow ends the program with the same message the PC build gives:

goose runtime error: data stack overflow (the 16384-byte data stack at 0x3ffbc840 is full; raise --reserve)

The canary is best-effort: it reports an overflow after the fact rather than stopping the store. A program that reads past the end of such a stack before the next check can crash on what it finds there. The canary's bytes are 0xA5, so on an ESP32 a LoadProhibited panic at address 0xa5a5a5a5 points to a data stack that overflowed: raise --reserve. Findings lists the compiler options that would make detection exact everywhere.

The two source fixes

64-bit layout on 32-bit chips

Goose's C backend computes struct sizes and offsets assuming 8-byte pointers. On a 32-bit MCU a struct holding a slice is 28 bytes to Goose but 24 to the C compiler, and indexing an array of them walks off the end. ptr64_layout gives every pointer field in Goose's packed types 8 bytes:

union { uint8_t *data; uint64_t data__mgpad; };

Field access is unchanged. CI checks this on every change by running the stock samples as 32-bit x86 Linux programs (tests/host32.py).

An Xtensa GCC calling-convention bug

Goose declares its scalars aligned(1), so that packed data can be read anywhere. Xtensa GCC 15.2 chooses the register pair for a 64-bit argument from that alignment, and passes the argument one register off from where the callee reads it. xtensa_abi_fix keeps aligned(1) only on pointers to 64-bit scalars, and gives the value types natural alignment. Findings has the full diagnosis.

Threads

thread_spawn and the typed queues use pthreads. ESP-IDF implements pthreads on FreeRTOS, so workers run on both ESP32 cores. On the ESP32s with PSRAM (the S3 and the WROVER-E) a finished worker's thread is joined as it ends, by a task above the program's priorities, which frees its stack then. On the WROOM-32 it is left to ESP-IDF, as a detached thread: its stack waits for an idle task, which doesn't run while workers keep both cores busy, and a spawn that finds no room waits up to five ticks for it (Optimizations, section 16). The H7 port runs the FreeRTOS kernel that STM32Cube ships, and mg_h7.c implements the pthread calls the runtime makes on its tasks:

  • Mutexes are FreeRTOS mutexes. Condition variables are lists of waiting tasks, each woken by a task notification.
  • Goose's thread-locals use GCC's emulated TLS on this target. libgcc's version keeps one copy of each, so the port has its own, with each task's copies in its FreeRTOS thread-local slot.
  • newlib-nano writes fwrite a byte at a time through the stream's one-byte buffer, which two tasks printing at once garbled. On the console, each fwrite is now one write under a lock, so lines stay whole.
  • A worker's stack and task are freed by the next spawn, or by the idle task, rather than by the idle task alone, which never runs while some task computes.

On every board, a worker's copy of the globals holds only what it uses. In Goose a worker gets its own copy of the globals its program uses, and the compiler's C allocates and zeroes a copy of every global in the program for it, then fills those in. MicroGoose allocates the bytes from the worker's first used global to its last, and nothing for a worker that uses none. It takes that set from the worker's entry in the generated C, and only where every function the entry can reach touches no other global. Otherwise the worker keeps a whole copy.