Repository navigation
Make the freestanding runtime reachable, placeable, and able to call its host - #36
Merged
Merged
Conversation
A vessel that concatenates a string needs memory, and the freestanding runtime deliberately had none. The alternatives were imported alloc hooks the host implements, which would have taken the boundary a consumer must satisfy from five symbols to seven, or a static arena here, which keeps the surface but makes the size of a vessel's memory our decision and charges every consumer for it in BSS. Instead the host owns the memory and the runtime owns the mechanism: nyrt_heap_init installs a region, exported rather than imported, so a host calls it without implementing anything, and nyrt_alloc bumps a 16-byte-aligned, zeroed pointer through it. There is no free -- a bump region is released as a whole, and nyrt_context_end returns it to empty, which is sound because everything allocated inside came from generated code whose lifetime ended with the context. Under --freestanding the string helper now calls nyrt_str_length, nyrt_mem_copy and nyrt_alloc instead of strlen, memcpy and calloc, so no libc symbol survives in the object under any configuration. Its shape is otherwise unchanged, including the null check: an allocator with no region returns null exactly as a hosted one does under pressure, and the existing failure path reports it through nyrt_fail. The hosted runtime answers to the two new names too, so which runtime to link stays the host's choice. The added checks run rather than inspect. The link test installs an array in its driver and afterwards finds the concatenated bytes inside it, which proves they landed in memory the host owns; the starved case links the same program with no region and requires it to stop through nyrt_fail with a message. Each is the other's control. docs/advanced/freestanding-targets.md writes the contract down so Anomalous can read it instead of inferring it from our tests, and names the tests as the authority if the two disagree. It also records what this does not provide: no release qualification, no evidence any kernel boots a vessel, no system-call bindings, no threads, TLS, unwinding or hosted I/O. Also rewords this notebook entry's citations of Anomalous's decision records, which used the retired ADR spelling the repository policy forbids and had been failing ny.repository-policy since 00eced9. Linux: 574 registered, 0 failed, 3 environment skips. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A consumer pins an SDK generation; it does not reach into our build tree. The freestanding archive had no install rule, so the generation a kernel consumes could not contain the one archive its vessels link, and the capability shipped yesterday was unreachable in practice. It now stages beside the hosted runtime and lands at nillib/native/libnilruntime_freestanding.a, recorded in the manifest by size and hash so a consumer can pin the archive by content. Two things were wrong for the target this archive exists for, and both were found by trying to link a vessel rather than by reading the code. It was position-independent, so the weak host failure hook was reached through a GOT -- and a fixed-address consumer with no dynamic linker rejects a GOT outright. POSITION_INDEPENDENT_CODE OFF is not enough on its own, because it only stops CMake adding -fPIC while the host compiler still defaults to PIE, so -fno-pic is explicit. The link test now links -no-pie and reads the section headers to pin that no GOT or PLT exists at all. It also used SSE. The runtime has no floating point of its own, but at -O2 the compiler vectorized its zero and copy loops into pxor/movups/movdqu, and a kernel profile that denies inherited x87 and SIMD state -- by leaving CR0.TS set, the ordinary way -- faults on the first one. A consumer cannot opt out of what is already in the archive, so the archive gives up the registers it does not need: -mgeneral-regs-only, checked for support rather than assumed, with a disassembly tripwire that reports rather than silently passes where no disassembler exists. Text fell from 1260 to 852 bytes as a side effect. Also records what ending a context does to the region, because a host depends on the answer: the allocation pointer resets and the bytes are deliberately left. For a freestanding host that is often the only channel out of a Ny object, which exports nothing but its entry, so the bytes must still be there when __ny_entry returns. A host that needs them gone clears the region, since only it knows whether they were sensitive. Linux: 574 registered, 0 failed, 3 environment skips. The freestanding link contract is 10 checks. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Found by mapping the vessel: a kernel whose own page-table branch occupies the first PML4 entry places user code above 512 GiB, and the object would not link there. Every address it materializes was a 32-bit absolute, so the link failed on the string constants of a program that never asked to live low. `nilassembler --code-model <small|large>` selects the model. The default keeps the target's own, so nothing changes for a hosted consumer; `large` removes the 32-bit absolutes and the object links at any address, with code and read-only data arriving in .ltext and .lrodata for a linker script to name. It is independent of --freestanding and --no-red-zone, because where an object is placed is a separate question from how it is entered or whether it keeps a red zone. The runtime archive needed no option -- it already reaches its own statics relative to the instruction pointer -- but it did need one fix for the same reason. `__ny_host_fail` was a weak *declaration*, so the only call to it was a 32-bit PC-relative relocation to an address the linker resolves to zero, which cannot be represented from above 4 GiB: the link failed over a symbol nobody intended to use. It is now a weak *definition* that does nothing, which a host's strong definition replaces as before. The null check is gone with it, and the archive now imports nothing at all. The new pin asserts both directions: the default model does use 32-bit absolutes, so the large-model assertion cannot pass by the program having had no addresses to materialize. Relocation types are read from the section headers directly, so it needs no binutils. Linux: 574 registered, 0 failed, 3 environment skips. The symbol surface is 14 checks, the freestanding link contract 10. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The capability a freestanding consumer needed and the one thing it could not
express. A kernel's own call boundary -- its system calls, its gate, its rights
model -- belongs to that kernel and is usually unstable while it is being
designed, so it must not enter the standard library. What belongs here is the
ability to reach it.
`@interop("host")` on a body-less declaration means the Ny function *is* an
external symbol of exactly that name, called with the platform's ordinary C
convention, and whoever links the object defines it. `host` is the one library
name that names no library. No new annotation and no frontend change was
needed: the Ny function's own name is the symbol, which keeps it visible in the
source and in the import surface where a reader and the symbol-surface test can
both see it. Names beginning nyrt_ or __ny_ are refused, because the native
runtime ABI is ours.
Nothing can check the host's signature, so the profile is narrow on purpose --
i64, f64 and text parameters, one of those or no value returned -- and anything
outside it is refused with a diagnostic naming the profile rather than guessed
at. text passes as a pointer to NUL-terminated bytes, which is the
representation a Ny string already has natively. Two spellings of the same
signature for one symbol are rejected rather than silently bitcast.
The interpreter now refuses a host call by name. It previously asked the
dynamic loader for a library called `host`: it failed here, but a machine that
happened to have one would have bound an unrelated symbol under a Ny
declaration, which is the kind of thing that works until it does not.
Pinned both ways. A declaration nobody calls imports nothing, because every
consumer would otherwise owe a definition for a capability the program did not
use; a called one imports exactly the declared name beside the runtime ABI and
no libc. The link test defines the symbol in its driver, checks the exact
NUL-terminated bytes it was handed, and returns a length Ny returns in turn, so
the round trip is exercised rather than the link alone.
Linux: 574 registered, 0 failed, 3 environment skips. The symbol surface is 18
checks, the freestanding link contract 12.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Four commits that take the freestanding capability from "emitted" to "usable by a real consumer". Every one of them was prompted by trying to build and boot an actual vessel rather than by reading the code, and each carries its own tripwire so it cannot regress quietly.
1.
a079345— allocation: the host owns the memoryA vessel that concatenates a string needs memory, and the freestanding runtime deliberately had none.
nyrt_heap_init(base, size)is exported, not imported, so a consumer installs a region without implementing anything, andnyrt_allocbumps a 16-byte-aligned, zeroed pointer through it. No free —nyrt_context_endreturns the region to empty, which is sound because everything in it came from generated code whose lifetime ended with the context.Chosen over imported
alloc/freehooks, which would have grown the boundary a consumer must implement from five symbols to seven — the opposite of the point — and over a static arena here, which would make the size of a vessel's memory our decision and charge every consumer for it in BSS.Under
--freestandingthe string helper now callsnyrt_str_length/nyrt_mem_copy/nyrt_alloc, so no libc symbol survives in the object under any configuration.2.
4c07a16— ship it, and fit it to a kernel targetThe archive had no install rule, so a pinned SDK generation could not contain the one archive its vessels link. It now lands at
nillib/native/libnilruntime_freestanding.a, recorded in the manifest by size and hash.Two things were wrong for the target it exists for:
POSITION_INDEPENDENT_CODE OFFis not enough on its own, because it only stops CMake adding-fPICwhile the host compiler still defaults to PIE, so-fno-picis explicit.-O2the compiler vectorized its zero and copy loops intopxor/movups/movdqu, and a kernel profile that denies inherited x87/SIMD state by leaving CR0.TS set faults on the first one.-mgeneral-regs-only, checked for support rather than assumed. Text fell 1260 → 852 bytes.3.
0f9e0ac— let it be linked where the consumer puts itA kernel whose own page-table branch occupies the first PML4 entry places user code above 512 GiB, and the object would not link there: every address it materialized was a 32-bit absolute, so the link failed on the string constants of a program that never asked to live low.
nilassembler --code-model <small|large>selects the model; the default keeps the target's own, so nothing changes for a hosted consumer. Independent of--freestandingand--no-red-zone, because where an object is placed is a separate question from how it is entered.The archive needed no option, but
__ny_host_faildid: as a weak declaration it left an undefined symbol reached through a 32-bit PC-relative relocation to an address the linker resolves to zero, which cannot be represented from above 4 GiB. It is a weak definition now, the null check is gone, and the archive imports nothing at all.4.
241eb76— let Ny reach a symbol its consumer suppliesThe capability a freestanding consumer needed and the one thing it could not express. A kernel's own call boundary belongs to that kernel and is usually unstable, so it must not enter the standard library; what belongs here is the ability to reach it.
@interop("host")on a body-less declaration means the Ny function is an external symbol of exactly that name, called with the ordinary C convention. No new annotation and no frontend change: the function's own name is the symbol, which keeps it visible in the source and in the import surface. Names beginningnyrt_or__ny_are refused.Nothing can check the host's signature, so the profile is narrow on purpose —
i64,f64andtextparameters, one of those or no value returned — and anything outside it is refused with a diagnostic naming the profile.textpasses as a pointer to NUL-terminated bytes, the representation a Ny string already has natively.The interpreter now refuses a
hostcall by name. It previously asked the dynamic loader for a library calledhost: it failed here, but a machine that happened to have one would have bound an unrelated symbol under a Ny declaration.Testing
Linux: 574 registered, 0 failed, 3 environment skips at the branch tip. The symbol surface contract is 18 checks, the freestanding link contract 12. The added pins include both directions where that matters — a declaration nobody calls must import nothing, and the default code model must use 32-bit absolutes, so the large-model assertion cannot pass by the program having had no addresses to materialize.
docs/advanced/freestanding-targets.mdis updated throughout and names the tests as the authority where the page and the toolchain disagree.What this is for
The consuming side is the Anomalous kernel, and it now boots a Ny vessel that issues its own rites: a
console_writecarrying a message Ny composed in the region the kernel installed, then a second one Ny sends only because the first returned the count it had passed, then exit 42. That work lives inexec-anomalousand is not part of this pull request.🤖 Generated with Claude Code