Skip to content

Modeled C stage 13: runtime audit — domain rows for the libc seams and the audit lane (ruling §52) - #1615

Merged
ehartford merged 7 commits into
modeled-c-stage10from
modeled-c-stage13
Sep 24, 2026
Merged

ehartford merged 7 commits into
modeled-c-stage10from
modeled-c-stage13

Conversation

@ehartford

@ehartford ehartford commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on stage 10 (#1614): this branch is rebased onto modeled-c-stage10 (git rebase --onto origin/modeled-c-stage10 origin/main, no conflicts) and linked with gh stack link --base main 1614 1615; :dev is green on the combined tree and runtime-domain-audit, libc-surface-check and contract-view-tests all pass on it.

Stage 13 of docs/modeled-c-implementation-plan.md: the runtime audit of ruling §52 (spec §16.2b.14) — "Runtime foreign calls must themselves be described by an internal facade or equivalent audited contract data … There is no hidden 'runtime calls don't count' exception."

What lands

Rows (rt/*.w, 385 rows in 12 files). Each runtime file that reaches a foreign symbol ends with an internal c facade block — the declaration form the facade parser already accepts (§16.2b.7), verified by Sema when the file compiles — declaring the three libc domains and one fn row per foreign extern:

file facade rows foreign symbols
rt/darwin_aarch64.w libsystem 72 libSystem via @[link_name]
rt/linux_x86_64.w libc 66 glibc via @[link_name]
rt/linux_aarch64.w libc 66 glibc via @[link_name]
rt/rt_core.w libc 3 malloc, free, gethostname
rt/fiber_core_darwin.w libc 14 pthread/mmap seams
rt/fiber_runtime.w, rt/fiber_stubs.w, rt/panic_runtime.w libc 2, 1, 1 _exit and friends
rt/fiber_core_windows.w ucrt 1 abort
rt/windows_x86_64.w win32 70 Win32 + Winsock (bare) + UCRT _errno/_fileno/_fseeki64/_ftelli64/_isatty/__acrt_iob_func
rt/windows_aarch64.w win32 64 same, minus the x64 unwind/DbgHelp calls
rt/wasm.w wasi 25 WASI preview1 imports (bare); llvm.wasm.memory.* are intrinsics, not rows

Every block declares domain errno thread, domain environ process, domain locale process (ruling §33–§36 defaults, as the plan directs). A row that says nothing invalidates all three (§38: unknown effect means invalidate). preserves domain D appears only where the C standard says so, cited in each block's header comment:

  • errno — C11 7.5p3: any library function may set errno, so no call preserves it. The one exception is the errno accessor itself (__error, __errno_location, _errno): it is the macro's lvalue (C11 7.5p2), and evaluating an lvalue is not a library call; it preserves all three.
  • environ — C11 7.22.4.6 leaves altering the environment to the implementation, which for the runtime's targets is POSIX setenv/unsetenv/putenv; POSIX getenv may overwrite the text it returned. Those rows (rt_libc_setenv, rt_libc_unsetenv, rt_libc_getenv) do not preserve environ; every other libc row does.
  • locale — C11 7.11.1.1: setlocale is the function that changes the locale; the runtime never calls it, so every libc row preserves locale.
  • Win32, Winsock and WASI rows are bare: the C standard does not describe them, and a row it cannot justify is never preserves.

Evidence the rows change nothing but the facts: each file still checks under --no-prelude with stage1, and rt/darwin_aarch64.w's emitted object has a byte-identical __TEXT (25978 bytes) with and without the block — the facade renders nothing (no resources, no methods). Rows were generated from the extern lists by a With script, not typed.

Lane (runtime-domain-audit, build/compiler.w + build.w). In the shape of libc-surface-check, its sibling: for every rt/*.w, each foreign extern — @[link_name]/@[import_module], or a name outside rt_*/with_* — must have an fn row in the file's one c facade block, and the block must declare the three domains. The verdict names the extern, its C symbol, and the facade the row belongs in:

error: rt/rt_core.w:4439: foreign extern 'rt_libc_setlocale' (C symbol 'setlocale') has no domain row; add `fn rt_libc_setlocale` to facade 'libc' — silence invalidates errno, environ and locale (§38), `preserves domain D` only where the C standard says so (ruling §52)

Wired as a :test dependency beside libc-surface-check. audit:all is with analyze and libc-surface-check is not part of it, so neither is this lane. The compiler verifies what a row says (a row names a declaration in scope, preserves domain D a declared domain); the lane verifies nothing is left unsaid, which the compiler cannot know — a user's facade may describe a subset of its imports, the runtime's may not.

Fixtures (test/runtime_domain_audit/). The lane runs itself over four checked-in fixtures before it reports green, so a silent lane is itself red: undeclared_extern.w (a seam with no row), no_facade.w (foreign symbols, no facade), missing_domain.w (one libc domain undeclared) each carry //! expect-violation: <text>; clean.w (//! expect-clean) covers runtime-internal externs, the LLVM intrinsic and comments inside the block. Test targets glob per directory (test/behavior/*.w), so the fixtures never run as programs.

Derived decisions (the ruling is silent; the option that at worst removes capability)

  1. Which externs are "runtime foreign calls". Every extern fn in rt/ that carries @[link_name] or @[import_module], plus every one whose name is not an rt_*/with_* runtime symbol (abort, GetLastError, socket, fd_write, __acrt_iob_func). Nothing is inferred from a name toward safety: the classification only decides who must have a row, and a row's default is invalidate. §52 names no libc-only carve-out, so Win32, Winsock and WASI calls are rows too.

  2. Rows for the runtime's stdio-stream variables (extern var __stdinp/stdin/…) — none: §52 speaks of foreign calls, and a facade fn item names a function. Listed in Modeled C: runtime process-global state outside the errno/environ/locale domains (signals, cwd, fd table, rlimits, process group, stdio streams) #1608.

  3. The implicit lend of an fn row is inert in rt/. SemaFacade records a described fn as lending its parameters (§16.2b.5) and lifts raw classification for covered parameters; runtime code is already exempt from the raw-call rule by path (SemaCheck.w:139), and no resource is declared, so nothing renders and nothing changes in the object (byte-identical text). The rows assert view effects, not ownership.

  4. getenv invalidates environ — C11 7.22.4.6p4: the returned string "may be overwritten by a subsequent call to the getenv function". The toolchain libc facade (src/compiler/LibcFacade.w) stated preserves domain environ on getenv, granting a capability the standard does not back; ruled (coordinator, on the ruling's test: never state what can create unsafety) and fixed in the last commit — that one clause deleted from fn getenv, every other row untouched. err_c_facade_getenv_view_invalidated pins the refusal (a second getenv while the first view is live), behav_c_facade_getenv_twice is the accepted spelling (copy the first out with to_owned() before the second call). rg getenv test/ examples/ build/release_uat_fixtures/ finds two behavior tests, neither holding a view across a second call; both still pass, and no example or UAT fixture names it.

  5. Same-named domains of distinct facades are one domain (ruling §35). With the rows in, the dark rt-in-unit lane (WITH_RT_IN_UNIT=1, D30 R2b — off by default, green on test/hello.w at the base) parsed every runtime file into one Sema and refused domain errno thread as "declared twice" (rt_core.w's libc block against darwin_aarch64.w's libsystem block, and either against a program's libc facade). facade_domains was keyed by name across all facades, which made two facades naming the same state a contradiction. §35 says the opposite — a facade "may also merge domains from distinct imports when they refer to the same actual state" — so SemaFacade.collect_facade_domain now merges a second declaration with the same name and scope into the first (every declaring facade's functions touch the one domain), still refuses one facade declaring it twice, and refuses a different scope ("one state has one scope"). behav_c_facade_domain_merged and err_c_facade_domain_merged_invalidated prove the merge is real (a bare fn in facade B invalidates the text facade A borrowed from errno); err_c_facade_domain_scope_conflict and err_c_facade_domain_twice pin the two refusals. The same lane then refused fn rt_libc_exit as "described twice" — four runtime files declare and describe that extern — so a second block's word-for-word restatement of an fn item is accepted as the same facts (compared as words, facade_item_words), and different clauses are refused: one function has one contract (behav_c_facade_fn_restated, err_c_facade_fn_restated_differently). Identity for both rules is the block node, not the facade name: each runtime file's block is called libc. WITH_RT_IN_UNIT=1 with-stage1 check test/hello.w is green again. This is the one Sema change in the PR; it touches facade collection only (no ownership, drop or ABI), so the PR still batches freely per the plan.

    Two things the dark lane still shows, neither introduced here and both for the D30 flip owner: (a) WITH_RT_IN_UNIT=1 … run test/hello.w exits 139 — it does so on main's own stage1 at ae609cd too (R2b is "off by default until R2c retargets codegen"); (b) behav_c_facade_domain_errno.w spells domain errno process, so under rt-in-unit it now meets the runtime's domain errno thread and is refused by the scope rule. Ruling §37's spelling is thread; when the lane is flipped that fixture conforms to the ruling, not the other way round — I did not edit it, since no running lane is red.

Outside the three domains

Process-global C state the runtime touches that the ruling does not name — signal disposition/mask (sigaction, sigaltstack, sigprocmask, raise, kill), cwd (chdir, getcwd), the fd table (dup2, fcntl), rlimits, process group/children (setpgid, wait4), and the stdio stream objects — got rows under the three named domains (invalidate by default) and no domain of their own. Filed as #1608 with the full list; a later domain cwd process is a refinement of existing rows, not a new obligation on the lane.

Verification (iterate tier; :fixpoint/:test/audits deliberately not run)

  • ./out/bootstrap/bin/with-stage1 check src/main.w — ok
  • with build :dev — green (before and after the rows; stage1 rebuilt with the rows embedded)
  • with build :runtime-domain-audit — 16 runtime files, 385 domain rows, every foreign extern described; 4 fixtures agree
  • the lane red end-to-end on an undeclared rt_libc_setlocale seam (verdict above), then restored
  • with build :libc-surface-check — still green
  • with the rows embedded in stage1: test/hello.w links and runs; behav_c_facade_domain_errno, behav_c_facade_facts_verify, behav_c_facade_parses, err_c_facade_view_invalidated_by_domain, err_c_facade_bad_domain_kind pass
  • with the §35 merge in stage1: the six new facade tests pass, err_c_facade_fn_twice still passes; WITH_RT_IN_UNIT=1 with-stage1 check test/hello.w — ok (red with the rows before the merge, green at the base)
  • three behav_c_facade_* tests fail in this tree — libc_system_headers (a memchr arity error inside lib/std/re/pcre2_match.w when <string.h> is imported), pending_producer_warns and presentation_ambiguous_warns (their pre_d_build_runner gets rc=127) — and fail identically on a separate worktree of the base commit d783366 with its own :dev stage1, so they are the tree's, not this change's (the worktree was removed afterwards)

@ehartford
ehartford changed the base branch from main to modeled-c-stage10 September 24, 2026 05:25
@ehartford
ehartford added this pull request to stack #1616 September 24, 2026 05:25
Eric Hartford added 5 commits September 24, 2026 06:14
…§52, spec §16.2b.14)

Each rt/*.w file that reaches a foreign symbol ends with an internal
`c facade` block declaring the three libc domains — `domain errno thread`,
`domain environ process`, `domain locale process` (ruling §33-§36) — and
one `fn` row per foreign extern: every `extern fn` under `@[link_name]` or
`@[import_module]`, and every one whose name is not an rt_*/with_* runtime
symbol. A row's silence invalidates (§38: unknown effect means invalidate);
`preserves domain D` appears only where the C standard says so, cited in
each block's header: C11 7.5p3 (errno — any library function may set it;
the accessor is the macro's lvalue, 7.5p2), C11 7.22.4.6 with POSIX
setenv/unsetenv/putenv/getenv (environ), C11 7.11.1.1 (locale: setlocale,
never called by the runtime). Win32, Winsock and WASI rows say nothing: the
C standard does not describe them. The LLVM memory.size/grow intrinsics are
not foreign calls.

385 rows over 12 files; each file still checks under --no-prelude, and the
emitted object's text is byte-identical to the row-less one (the facade
renders nothing: no resources, no methods).
… it must accept

The lane proves it sees before it is trusted: `undeclared_extern.w` (a
libc seam with no row), `no_facade.w` (foreign symbols, no facade),
`missing_domain.w` (a facade that leaves one libc domain undeclared) each
start with `//! expect-violation: <text>` the lane must report;
`clean.w` (`//! expect-clean`) covers the runtime-internal externs the
lane must ignore, the LLVM intrinsic, and comments inside the block.
… domain row is a red build (ruling §52)

In the shape of libc-surface-check (build/compiler.w): for every rt/*.w,
each foreign extern (@[link_name]/@[import_module], or a name outside
rt_*/with_*) must have an `fn` row in the file's one `c facade` block, and
the block must declare `domain errno thread`, `domain environ process`,
`domain locale process`. The verdict names the extern, its C symbol and
the facade the row belongs in. The compiler verifies what a row says when
the file compiles; the lane verifies nothing is left unsaid. It then runs
itself over test/runtime_domain_audit: each negative fixture must trip it
and the clean one must pass, so a silent lane is itself red. A `:test`
dependency beside libc-surface-check.
…word-for-word restated fn item is one contract (ruling §35)

With the runtime rows in, the dark rt-in-unit lane (WITH_RT_IN_UNIT, D30
R2b) parses every rt/*.w into one Sema and refused `domain errno thread`
as "declared twice" — rt_core.w's block against darwin_aarch64.w's, and
either against a program's libc facade — and then `fn rt_libc_exit` as
"described twice" (four runtime files declare and describe that extern).
Both refusals keyed identity by name across every facade in the unit.

Ruling §35: a facade "may also merge domains from distinct imports when
they refer to the same actual state". collect_facade_domain now merges a
second declaration with the same name and scope into the first — every
declaring block's functions touch the one domain (facade_domain_note_file
consults the merged facade set) — refuses one block declaring it twice
(identity is the block node, not the name: each runtime file's block is
`libc`), and refuses a different scope: one state has one scope (§36).
collect_facade_fn accepts a second block's word-for-word restatement of an
fn item (the same facts, compared as words) and refuses different clauses:
one function has one contract.

Tests: behav_c_facade_domain_merged / err_c_facade_domain_merged_invalidated
prove the merge is real (a bare fn in block B invalidates the text block A
borrowed from errno); err_c_facade_domain_scope_conflict,
err_c_facade_domain_twice, behav_c_facade_fn_restated,
err_c_facade_fn_restated_differently pin the rest. `WITH_RT_IN_UNIT=1
with-stage1 check test/hello.w` is green again (green at the base, red
with the rows before this).
The toolchain libc facade stated `preserves domain environ` on `getenv`,
so a program could hold one getenv view across a second getenv. C11
7.22.4.6p4: the returned string "may be overwritten by a subsequent call
to the getenv function" — the second call invalidates the first view, and
a `preserves` grants a capability the standard does not back (the
ruling's test: never state what can create unsafety). The clause is
deleted from `fn getenv` only; the runtime's own getenv rows (rt/*.w,
ruling §52) already say the same. Every other row is unchanged.

err_c_facade_getenv_view_invalidated pins the refusal;
behav_c_facade_getenv_twice is the accepted spelling — copy the first out
(`to_owned()`) before the second call. No existing fixture holds a getenv
view across a second getenv (rg getenv test/ examples/
build/release_uat_fixtures/: two behavior tests, neither does).
Eric Hartford added 2 commits September 24, 2026 06:56
…trace_v2 present as methods (callback items render on the resource); the snapshot was recorded before #1606 landed
…a failed compilation is never ok — it prints the view and exits 1; the retained-no-owner contract fixture is a stage 9 compile error, so it becomes the refused-clause fixture
@ehartford
ehartford marked this pull request as ready for review September 24, 2026 14:12
@ehartford
ehartford merged commit 065e1d4 into main Sep 24, 2026
@ehartford
ehartford deleted the modeled-c-stage13 branch September 24, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant