On this page

0045. Runtime organisation and C unit tests

Context

The runtime starts in harness_rt.h; arenas and sequences should not enlarge the include cost of scalar programs. Arenas need direct C tests, and generated heap programs need system feature declarations before system headers.

Decision

Keep runtime/harness_rt.h as the core header included by every program. Place arena APIs and implementation in runtime/hrt_mem.h, and sequences, UTF-8, and formatting in runtime/hrt_seq.h. Sequence headers include memory support; M5.3a supplies string construction.

Feature-test macros belong in the generated translation unit, before its first include: heap programs define _DEFAULT_SOURCE 1 and _DARWIN_C_SOURCE 1. Headers do not define these macros.

M5.3a's one core representation change adds cap to hrt_str, plus a GCC -Wmissing-field-initializers pragma beside it so existing two-field emitted literals remain byte-identical and warning-free. Equality guards empty strings before memcmp, allowing a null empty view. Non-heap C retains its original include path; heap C includes hrt_seq.h. The driver embeds and writes all three headers. The pragma is in the core header because scalar programs also use the three-word representation; feature macros stay in generated C.

Put standalone C tests in tests/runtime/*.c; the Rust runtime test runner compiles each test with -std=c11 -Wall -Wextra -Werror -O2 -fsanitize=address,undefined, runs it, and checks ok. The verified environment is GCC on Linux, where ASan+UBSan links and runs. There is no sanitizer fallback. MAP_NORESERVE behaviour on macOS is unverified.

Run normal, poison, stats, combined poison/stats and UBSan-only poison-fill variants. Poison reset fills freed bytes with 0xDB; GCC ASan additionally poisons their shadow memory, and allocation unpoisons reused bytes. An empty test directory fails. OOM cases assert exit 101 and the exact flushed trap.

Alternatives considered

Consequences

Scalar programs retain their existing include cost. Heap programs include the needed headers and own feature-macro ordering. C unit tests exercise runtime invariants independently of compiler-generated programs.