notes

Running a foreign language: the C seam

Milestone 36, DECISIONS §31. Built 2026-07-29, both ISAs, in QEMU.

The question that started it: can a userspace service be written in another language, the way a monolith would have put a C FAT32 driver in the kernel? The answer is yes, and this milestone is the smallest thing that proves it. A memory-unsafe C component, compiled by bare-metal clang, running confined, faulting on a deliberate out-of-bounds write, and restarted by its supervisor.

The component is throwaway on purpose. What is being de-risked is the seam: the toolchain, the linkage shape, the libc, and the confinement proof. That has to happen before a real foreign component depends on it (libghostty-vt, the display ladder's later rung). If the seam has a problem, you want to find it with 150 lines of disposable C rather than half way into a port. The detail this page has no room for is in c-seam/.

Why memory-unsafe C is the best demonstration, not the worst

It looks backwards at first. The project's thesis (DECISIONS §14) is a verified-Rust capability microkernel, and here we are linking C.

But read the thesis again: a verified core that confines unverified workloads. The workload was always going to be unverified. C is simply the most unverified workload available: no bounds checks, no borrow checker, nothing at all between a bad index and a store to whatever address it computed. The more unverified the component, the more the confinement has to prove. So this is the sharpest available test of the claim rather than a dilution of it.

And the contrast with a monolith stops being rhetorical and becomes arithmetic:

in-kernel C (a monolith) confined C (here)
what a bad index reaches any physical memory, any device register one page it was granted
who notices nobody, until something else breaks the kernel, at the instruction
what recovers reboot its supervisor, in userspace
what is trusted the C the page tables

The peer project Atom keeps FAT32, AHCI, and xHCI in the kernel today, which is what the left column looks like in practice.

The other half of the argument is that isolation here does not know what a language is. Page tables, unforgeable capabilities, the DMA validator, and the IOMMU are all language-agnostic mechanisms. None of them has an opinion about which compiler produced the instructions they are confining. That is exactly why a language boundary is cheap here and would be expensive in a system whose safety came from the language.

The shape: a Rust shell that holds everything

   kernel  <--- svc / ecall --->  c_shim (Rust)  <--- C ABI: (u8*, usize) --->  c_seam.c
                                  ^^^^^^^^^^^^
                                  every capability and every system call stops here

fixtures/src/c_shim.rs is the whole design, and what it does not do is the point:

That makes the confinement claim precisely as narrow as it should be: the C can corrupt memory inside a grant the shell already had, and nothing else.

It is also the same sans-IO shape RedoxFS's Disk trait already uses (DECISIONS §27): a component with the logic and none of the I/O, wired up from outside. Here the boundary is a language rather than a trait, and the shape did not have to change.

What crosses, and what cannot

Crossing:

uint32_t c_seam_transform(unsigned char *grant, size_t len);   /* honest work */
void     c_seam_overrun  (unsigned char *grant, size_t len);   /* the bug, one byte past */
void     c_seam_wild     (unsigned char *grant, size_t len);   /* the bug, a page past */

Scalars and buffers. Not crossing, and each of these is a seam decision this spike did not have to make: structs by value, callbacks from C into Rust, ownership transfer, an error type, varargs, C++ (name mangling, exceptions, static initializers), bitfields, enum widths.

The layout of the shared page is agreed by a comment in both languages (fixtures/c/c_seam.c's C_SEAM_* defines and crates/c_seam's constants) rather than by generated bindings. For one page of bytes that is the right trade; for a real API it would not be, and the honest reason to say so here is that "we will generate bindings when there is an API worth generating" is a plan and "we forgot" is not.

The libc question, answered by tier

The roadmap's milestone-36 file records three tiers of C dependency (design/roadmap/0036-foreign-component.md, "The line this does not cross"):

  1. Freestanding. No libc. littlefs allocates nothing; libghostty-vt needs a supplied allocator (the graphical terminal runs full-screen programs).
  2. A handful of symbols. Shim what the component actually references. This is what this spike proves.
  3. Full POSIX. open, fork, socket, threads. Needs a real libc port, which is DECISIONS §15's "later, if ever" road and the one Redox took with relibc. Not walked here, and saying so is what keeps this from becoming that project.

What the object demands (five), and what the linker demands (two)

Discovered by letting the build fail and reading the error, which is the only honest way to learn what a foreign object file needs. llvm-nm --undefined-only on the compiled object:

$ llvm-nm --undefined-only c_seam.o          # identical for aarch64 and riscv64
                 U free
                 U malloc
                 U memcpy
                 U memset
                 U strlen

Five, and only five. Worth stating explicitly what is absent, because each would have been a finding: no compiler-rt helper (__muldi3 and friends), no memmove conjured out of a struct copy, no __stack_chk_fail. Checked at -O0, -Os, -O2, -O3, with and without -fno-builtin, and even with -fstack-protector-strong (this component has no stack arrays, so no canary was inserted; build.rs passes -fno-stack-protector anyway, so that stays a decision rather than luck).

Then delete all five shims and let the linker speak:

rust-lld: error: undefined symbol: malloc
rust-lld: error: undefined symbol: free

Two. memcpy, memset, and strlen are already there, weakly, from Rust's own compiler_builtins for the bare-metal targets, which is what lets a no_std Rust binary link at all. The C component's needs and the Rust runtime's needs overlap almost exactly. That is a reusable finding: tier two is smaller than it looks, because the freestanding Rust runtime has already paid for most of it.

The trap: a Rust memcpy shim calls itself

Do not define the three symbols the runtime already owns. Here is why, and it cost a debugging session.

The obvious Rust implementation of memcpy is core::ptr::copy_nonoverlapping. That lowers to a call to memcpy. So the shim calls itself, forever.

The symptom does not look like recursion. It looks like this:

  user thread ... killed: Data abort from a lower EL
    pc 0x4003a8   far 0x4fcff0   user sp 0x4fcff0   esr 0x92000047

A store fault at exactly sp, sixteen bytes below the bottom of the stack, in core::ptr::const_ptr::is_aligned_to (a debug build spends a frame per core::ptr helper, so the backtrace is all allocator plumbing). That reads unmistakably like "the stack is too small". It is not: quadrupling the stack from 16 KiB to 64 KiB reproduced it exactly, sixteen bytes below the new bottom, which is the tell. compiler_builtins avoids the trap by compiling with #[no_builtins]; a program crate cannot.

The fix is also the smaller answer: shim malloc and free, and let the runtime keep the rest.

Where malloc comes from

The untyped-backed GlobalAlloc of milestone 27 (Rust std on the native ABI), memory_region_heap::MemoryRegionHeap (DECISIONS §22 (Rust std on the native ABI, the Hermit way)), wired to the untyped region the instance was built in. Three consequences, all of them the point:

One real cost of the C ABI shows up here and cannot be shimmed away: free(p) carries no size, while GlobalAlloc::dealloc requires the original Layout. So malloc stores the size in a 16-byte header in front of the pointer it returns (16 because that is also the alignment malloc must guarantee for any C type, so the payload stays aligned for free). Every C-to-Rust allocator bridge pays this; it is worth knowing it is a property of free's signature rather than a shortcut.

The toolchain: bare-metal clang, one compiler for two ISAs

fixtures/build.rs compiles fixtures/c/c_seam.c and hands the one object to the linker for the c_shim binary only, so every other program links exactly as before. clang is found from a known list: $CRICKER_CC, then Homebrew's llvm keg by path, then clang on PATH. Each candidate must have both the AArch64 and RISC-V backends (clang -print-targets), so Apple's clang is rejected on purpose: the parity gate of §19 (architectural parity), applied to the toolchain. script/bootstrap installs one, and without one fixtures/build.rs panics with installation instructions.

The load-bearing flag is -mgeneral-regs-only on aarch64. Without it clang vectorizes the byte loops, and FP/SIMD registers are unusable at EL0 here and unsaved across a switch. On riscv64 the ABI is lp64, not lp64d, to match riscv64imac. the-toolchain.md has the full flag list and the reasons.

The confinement test, which is the milestone

system_tests/src/user/c_seam_tests.rs, both ISAs. Three processes are involved:

  c_confiner  budget + initrd + report + the C component's supervision endpoint
    |        holds GRANT, WITNESS_RO, and WITNESS_FAR mapped read/write in ITS OWN space
    |
    +-- c_shim (instance N)   report endpoint, its own region's untyped, GRANT rw, WITNESS_RO ro
          |
          +-- c_seam.c        a pointer and a length

c_confiner is builder, supervisor, and checker at once. The checker role has to be a separate address space from the C component, because a checker inside the faulting address space could only report what that address space could see, which is exactly the thing under suspicion.

The address-space layout gives the checker two witnesses. WITNESS_RO is the frame the component and the confiner share (the confinement test lays it out). WITNESS_FAR is a different frame, one page past WITNESS_RO, at an address the component has not mapped. An overrun into the first proves the store did not happen; a wild write to the second proves a virtual address means nothing outside the address space that owns it.

Five assertions are made from outside, after the component is dead. It faults. The fault address equals the address the C code computed. Both witnesses are intact, byte by byte. And a restart computes the right answer, checked against an independent Rust implementation. The control makes them mean something: each misbehaving function stores inside its grant first, and that store must be visible to the confiner. Both ISAs report the exact faulting address. aarch64's ESR_EL1 also tells a permission fault from a translation fault; RISC-V's scause does not, which would matter to a userspace pager. the-confinement-test.md has the layout, the assertions in full and the transcripts.

What the supervisor had to hold, and what it would rather have held

This is the part that feeds an open design fork, so it is written down precisely.

c_confiner restarts its child, and to restart it must first reap it: the corpse is dead-until-reaped (§26.4), and its region stays pinned until somebody says otherwise. Reaping is MemoryRegion::DESTROY, which needs WRITE on the region. WRITE on a region is also exactly what builds a process from it (RETYPE, RETYPE_OBJECT, SPLIT). There is no narrower right.

Nothing was invented to work around this: no new capability, no new method, no new system call. The concrete requirement, for whoever decides the fork, is one sentence: a supervisor needs exactly DESTROY on one region it did not create. Whether that is a rights bit split out of WRITE or a distinct Untyped::REAP changes the rights model and the system call surface, which makes it a decision rather than an implementation detail. DECISIONS §26's phase-B block already records it as one.

The confiner since §32

c_confiner reaps through the supervision endpoint each death arrives on (user_mode_runtime::reap, DECISIONS §32 (a supervisor may collect a corpse without being able to build one)), and it deletes an instance's region capability as soon as the child starts. Its supervisor role holds nothing that reaches a live instance's memory.

The program still holds a full construction budget, because it is also the builder: it splits a region per instance and lays c_shim out in it, and §32 does not touch construction. The bundling §31 (the foreign-language seam) recorded was two things, and only one of them was the reap. Split builder and supervisor into two processes and the supervisor would hold nothing but endpoints, which is what the sub_server_supervisor of milestone 22 (trusted init) does. They stay fused here so the requirement is visible in one program rather than behind an IPC hop.

Honest caveats: what a spike does not prove

The whole value of doing this cheaply is that it fails early. The corollary is that it does not prove much about the expensive thing.

See also