Skip to content

Modeled-C Amendment 2 (D76): callback argv/user data without unsafe (#1779); process state is an audited effect (#1608) - #1841

Merged
ehartford merged 4 commits into
fix-sdkfrom
impl-d76
Sep 28, 2026
Merged

ehartford merged 4 commits into
fix-sdkfrom
impl-d76

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Stack created with GitHub Stacks CLI • Give Feedback 💬

@ehartford
ehartford added this pull request to stack #1845 September 28, 2026 07:42
@ehartford ehartford changed the title impl d76 Modeled-C Amendment 2 (D76): callback argv/user data without unsafe (#1779); process state is an audited effect (#1608) Sep 28, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on the stack top ecccc1e1 (#1839 → #1844, on main 355c20b; docs-only commits since), driven by the pinned seed cffa4a40: :gate rc=0 in 648 s; :battery-checks (seed-driver, fixpoint, test-with-audits incl. drop and move audits and every test lane, user-programs-safe, last-green) rc=0 in 1680 s. The previous run's one red (behav_trie, a false positive of the closure layer's store rule) is fixed in #1842 (e3ba9f5), with a full behavior/compile-error corpus comparison against main. Merge: gh stack merge 1844 --yes --squash.

@ehartford
ehartford marked this pull request as ready for review September 28, 2026 10:05
Eric Hartford added 4 commits September 28, 2026 15:15
…ew((view, 2)) put the view on the heap (§5.1)

expr_is_ephemeral_value (SemaCheck.w), the predicate the #600 heap-escape
gate asks of a Box.new / Rc.new / Arc.new argument, had cases for struct,
array and map literals (#625) but none for a tuple literal, so a view
wrapped in a tuple moved into heap storage past its origin:

    let local = 5
    let b = Box.new((Keep { r: &local }, 2))    // accepted

The same holds through a generic body (`Box.new((x, 1))` with `x: T`, T
ephemeral). A tuple literal is now judged as the other literals are: by its
type (type_is_ephemeral_value already treats a tuple with an ephemeral
element as ephemeral) and by its elements.

Found through D76's generated callback wrapper, which boxes the userdata
beside the program's callbacks as a tuple: an ephemeral userdata borrowing
a local was handed to C, and the callback read the dead local through its
`&U` (printed 4, twice, for a local of 5).

Evidence: test/compile_errors/err_ephemeral_tuple_boxed.w — base stage1
`check` ok; this change "an ephemeral value cannot be stored on the heap".
…inside a closure panicked the capture walk

A closure body is walked for captures by expr_uses_symbol (SemaCheck.w),
which panics on a node kind it has no case for (#1570, #1598: a silent 0
would miss a capture). A call's type argument — `transmute[extern "C"
fn(i32) -> i32](p)`, `transmute[*const i64](q)` — reaches it as the index
of the callee's NK_INDEX, a type node, and there was no case for any type
node:

    panic at src/SemaCheck.w:27370:9: internal error: expr_uses_symbol has no case for node kind 127

A type names no value, so it captures nothing — except through a
`typeof(expr)` inside it, which is walked. The pointer the closure
transmutes is still captured.

Evidence: test/behavior/behav_transmute_type_arg_in_closure.w — base stage1
panics as above; this change prints 42 and 7. D76's generated callback
wrapper transmutes inside the closure C calls.
…ata as &U, through a wrapper the compiler generates (§16.2b.9, D76, ruling Amendment 2)

Ruling Amendment 2 (§44, #1779): "A clause on the registering function may
present a callback's argument vector as a slice of a callback-scope handle
(`argv paired with argc as &[Value]`) and its registered user data as the
value the facade boxed (`user_data as &U`). The compiler generates the
wrapper; both are valid for the callback's invocation only."

Clauses (Parser.w, Ast.w FACADE_CLAUSE_CALLBACK_ARGV / _USER_DATA). The
spelling names what the ruling's words leave to position, as every facade
reference does:

    fn sqlite3_create_function_v2
        consumes param 4 destroyed_by param 8
        retains param 5 by param 0
        callback param xFunc argv param 2 paired with argc param 1 as &[Value]
        user_data from sqlite3_user_data as &U

`argv`/`argc` index the callback's own parameters (C names none of a
function pointer's); `from <fn>` states which C function hands the pointer
back. Both are capability-granting, so stated, never inferred from a type's
shape or a name (D51): a wrong pairing reads past the vector, a wrong
accessor reads another pointer as `&U`.

Verification (SemaFacade.w collect_argv_clause, collect_user_data_clause,
verify_facade_wrapped_callbacks): the callback is callable; argv is `T **`
with `T *` the representation of exactly one handle, argc an integer, the
presented type `&[H]` (or `[]H`); the accessor takes exactly the handle and
returns `void *`; `user_data` needs a userdata the facade boxes (consumed
with its destroy callback, or retained); argv needs `user_data` (the only
channel from C's call back to the program's callback); each served
callback receives the handle once and is not also a `callback … userdata`
pairing.

Rendering (FacadeRender.w facade_render_wrapper and the D76 section
comment): the method boxes `(U, cb…)` — the userdata with the program's
callbacks as code pointers — hands C the box and one generated
`extern "C"` wrapper per callback that receives the accessor's handle (a
null callback stays null), and frees the box through the destroy
callback. The wrapper reads the box through the accessor, makes the slice
from C's vector and count (a negative count panics, naming the
operation), and calls the program's callback with the handles, the slice
and `&U`. The presented callback is `extern "C" fn(Context, &[]Value, &U)`
— captureless, as before — and Sema types it at each call with `U` bound
from the userdata argument (FacadeCallbackMethod.wrapped_params), so a
bare fn or a closure passes with no typed local and a capturing closure
is refused as for `db.exec`.

Valid for the invocation only: the slice and its handles are ephemeral and
`&U` a borrow, so global or heap storage is refused by the ordinary
analysis, and the diagnostic names the handle through a slice too
(facade_handle_in, type_mentions_facade_rendered). The userdata itself
must own what it holds: an ephemeral one is refused as the box's content
(the tuple rule of the previous commit).

The contract view reports each served callback's `arguments` and `user
data` facts with their clauses (AnalysisContract.w).

The SQLite facade (lib/facades/sqlite3.w) declares `handle Value wraps
*mut sqlite3_value` with `sqlite3_value_int` of it and the two clauses on
sqlite3_create_function_v2, quoting the header. The end-to-end test's SQL
function now reads its arguments and application data with no `unsafe`:
`fn shout(ctx: Context, args: &[Value], app: &AppData)`, `SELECT
shout(20, 22)` is 42, and it is registered by name, with no typed local.
The three other create_function_v2 tests spell the new callback type; the
captures test now gets the capture refusal its comment anticipated.

Tests, each failing on the base (the clauses were a parse error there):
behav_sqlite_facade_end_to_end.w, behav_c_facade_callback_argv_user_data.w
(portable: every argument, an empty NULL vector, a final callback, a null
callback, data destroyed once — clean under --debug-alloc),
err_c_facade_callback_args_escape.w, err_c_facade_callback_user_data_escapes.w,
err_sqlite_facade_function_args_escape.w, err_c_facade_callback_ephemeral_user_data.w,
err_c_facade_argv_needs_user_data.w, err_c_facade_user_data_boxes_nothing.w,
err_c_facade_argv_not_handle_vector.w, err_c_facade_user_data_accessor_not_void_ptr.w,
contract/ok_callback_arguments.w (+ .expected; audit:contract and audit:all
clean); updated: err_sqlite_facade_handle_escapes_callback.w,
err_sqlite_facade_retained_callback_captures.w, da_sqlite_facade_destroy_once.w
(leak count=0).

Fixes #1779
…d effect, not a domain (§16.2b.14, D76, ruling Amendment 2)

Ruling Amendment 2 (§52, #1608): "Process-global C state over which no safe
view is presented (signal disposition and mask, the current directory, the
file-descriptor table, resource limits, process groups and children, the
stdio stream objects) is an effect the runtime audit records, not a domain.
It becomes a domain the first time a facade presents a safe view whose
validity it decides."

#1789 had declared the seven as domains in every runtime facade, with a
`preserves domain` line per row for each state the row does not alter —
rows no view's validity depended on, the ceremony the ruling removes. The
runtime facades (rt/*.w) now declare the three libc domains only; 1,764
lines go.

The record (build/compiler.w, runtime-domain-audit): the lane keys each
seam's effect on the C symbol it links, from the interfaces POSIX, Win32
and WASI name as altering each state (comp_process_state_alterers: chdir
the working directory, setrlimit the limits, fork/exec/waitpid the
children, open/close/dup2/socket/CreateFileW/path_open the descriptor
table, sigaction/sigprocmask/raise the signals, fseeko the streams; a call
that only reads — getcwd, getrlimit — records nothing), and writes the
record into its output, one line per seam: 112 seams today. It refuses:
  - a facade declaring one a domain while no facade the toolchain ships
    (rt/*.w, the libc facade) presents a view over it;
  - a facade presenting a safe view over one (`returns borrow … from domain
    S`) without declaring it a domain;
  - once one is a domain, a runtime facade not declaring it, and a row
    whose seam alters it stating `preserves domain S`.
The lane reads src/compiler/LibcFacade.w for the views it presents.

The compiler: `from domain S` / `preserves domain S` naming undeclared
process-global state keeps the unknown-domain error and says why, citing
§16.2b.14 (SemaFacade.w facade_unknown_domain_message).

Fixtures (the lane's): clean.w keeps the three domains only;
process_state_not_a_domain.w, process_state_view_without_domain.w,
process_state_domain_preserved_by_alterer.w (negative),
process_state_effect_recorded.w (a new `//! expect-record:` head: clean,
and the record holds the line); missing_process_domain.w, the inverted
rule, is removed. Compiler: err_c_facade_view_over_process_state_undeclared.w.

Evidence: runtime-domain-audit: 16 runtime files, 400 domain rows, every
foreign extern described; 112 seam(s) record a process-global effect
(D76); 8 fixtures agree.

Fixes #1608
@ehartford
ehartford merged commit 000d27e into main Sep 28, 2026
@ehartford
ehartford deleted the impl-d76 branch September 28, 2026 19:16
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