Skip to content

await in fn-param defaults checks green but is invisible to the suspend artifact #986

Closed

Description

@eas4ai

Classification

Suspend-boundary bypass, candidate-low (audit finding ASYNC-001). Verified: the language accepts it, the analysis misses it.

Source chain

await in a default-param expression passes with check (probe .audit/probes/readonly_pass_20260903/default_await_probe.w: sync fn f(x: i32 = fetch().await)), while AsyncLower.lower_body walks only the fn-body node (src/AsyncLower.w:90-92) — the suspend is NEVER recorded. Consequences: a sync fn can harbor a suspend; the artifact gating runtime linkage (src/compiler/Compilation.w:1146) may skip linking the fiber runtime for a program whose sole suspend hides in a default (loud link failure expected — reasoned, not executed); with analyze suspend lists are incomplete. (MirSuspendCheck works on MIR bodies so it sees whatever MirLower emits; the hole is AST-walk attribution + flavor gating.)

Repair boundary

Walk default-param expressions in AsyncLower (attributing to caller or callee per the lowering rule) and include them in flavor computation; or reject suspending defaults loudly.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced, and the consequence is worse than "invisible": the awaited value is discarded

    Verdict. The report's claims hold on current main: .await inside a default-parameter expression passes with check, and AsyncLower.lower_body walks only the function body node, so the suspend is never attributed anywhere. Running it shows a silent miscompile on top: the default is lowered at the call site (per spec), but the call passes a Unit-typed temporary to the i32 parameter instead of the awaited value, so f() returns whatever happened to be in the argument register (7, 9, 1 in my runs, never 5). --validate-all, --validate-ownership, and with analyze audit:all all report ok on that MIR.

    Tested revision and compiler

    item value
    source main @ 5809ac1 (2026-09-14); src/AsyncLower.w unchanged since the reported 450733e except the least-ceremony sweep
    compiler with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1
    platform macOS Darwin 25.6.0, arm64

    Probes

    async fn fetch() -> i32: 5
    fn f(x: i32 = fetch().await) -> i32: x
    fn main:
        print(f"{f()} {f(7)}")
    
    program with check with run expected
    above ok 7 7 5 7
    let a = f(); ... let b = f(9); let c = f() ok a=9, b=9 c=9 5, 9, 5
    async fn run() -> i32: f() + f(1) awaited from main ok 2 6
    control: sync-call default fn h(x: i32 = five()) and literal default ok h()=5 h(7)=7 k()=5 k(7)=7 correct
    control: .await in a sync fn body (fn g() -> i32: fetch().await) ok 5 correct

    So the value loss is specific to .await in the default expression, not to defaults or to awaiting from sync code in general.

    Root cause (observed in MIR)

    --dump-mir of the first program, function main:

    bb13: call const ()(move _11, const 1ty3) -> [return: _12, next: bb14];   // await scaffold
    ...
    bb19: call const fn sym300(copy _12) -> [return: _19, next: bb24];       // f(<default>)
    bb28: call const fn sym300(const 7ty3) -> [return: _23, ...];            // f(7), correct
    

    --dump-place-map: main:_12 is ty14, the same type id as main's own Unit return local; f:_1 (the parameter) is ty3, the type of fetch's i32 result. So the caller passes a Unit temp to an i32 parameter. In f the MIR is _0 = copy _1, which reads the garbage.

    Why it is Unit: in src/MirLower.w:13809-13813 the NK_AWAIT handler takes result_ty = self.expr_type(node) and hands it to lower_single_await. When the await node is reached through lower_default_call_arg from the caller's call lowering (:8939-8960), the recorded expression type for that node is the Unit type rather than i32. I did not trace inside Sema to see whether the default expression's await is type-checked in a mode that does not record the node type or whether the lookup is keyed to the callee's context; that part is a hypothesis. The attribution gap the report names is real and adjacent: src/AsyncLower.w:90-92 walks only fn_body_node, so the suspend inside a default is recorded for neither the declaring function nor the caller.

    Tools that stayed silent

    validate-all: ok, validate-ownership: ok, and compiler-analysis-audit: ... violations=0 ok on a call whose argument type does not match the callee parameter type. A typed-MIR check of call-argument types against the resolved signature would have caught this in one line; per AGENTS.md that gap should be filed with the fix.

    Spec

    • Spec line 3480: "Default expressions are evaluated at the call site on every call where the argument is omitted." The implementation does evaluate at the call site; it loses the value.
    • §14 may_suspend (line 6891): a function that "directly performs a primitive current-fiber suspension" is may_suspend, and .await is in the closed primitive set. Whether the suspension in a default belongs to the caller (where it executes) or the callee (where it is written) is exactly the attribution rule the report asks for; the spec does not say, so a ruling is needed. Rejecting suspending defaults outright is the other spec-consistent option.

    Related PRs

    None since 450733e touch AsyncLower.lower_body, lower_default_call_arg, or the NK_AWAIT lowering. No open PR does.

    Would I champion a fix? Yes, and the value bug first.

    Acceptance criteria:

    1. The first program prints 5 7; the three-call program prints 5, 9, 5; the async-context program prints 6.
    2. A typed-MIR validator (in validate_mir_body or the analyzer's caller-marshalling audit) rejects a call whose argument operand type differs from the resolved parameter type, and --validate-all reports it on today's MIR.
    3. Ruling recorded in docs/decisions.md: either suspending defaults are attributed to the caller and AsyncLower walks default expressions at each omitted-argument call site, or .await/yield in a default expression is a compile error with a named diagnostic.
    4. Behaviour tests for both spellings (accepted-and-correct, or rejected), plus one for a default in an async fn callee.

    Untested

    The report's linkage hypothesis (a program whose only suspend hides in a default might skip the fiber runtime link) was not exercised; every probe here also had an async fn, so the runtime was linked regardless. Linux and Windows not executed.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce. An await in a default argument checks, links and runs; defaults are evaluated in the caller (#2024), so the suspend is in the caller's body.

    $ cat main.w
    async fn fetch() -> i32: 7
    fn f(x: i32 = fetch().await) -> i32: x + 1
    fn main:
        print(f())
    $ with check main.w
    ok
    $ with run main.w
    8
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions