Skip to content

Sema audit gap: garbage generic-inst slot reads were invisible to audit:all; add a typed-expr well-formedness audit #692

Closed

Description

@ehartford

Found during #682-inc1 bring-up. The prelude-prefix reordering exposed a latent bug: get_generic_inst_base/arg/arg_count read d-slots/type_extra from NON-inst types, returning garbage that depended on type-table layout. Under the new layout the garbage was u7 (the bitpacked sub-byte primitive), so an unannotated Vec.new() receiver typed push(100)'s literal as u7 — wrong inference, wrong codegen (get(0) read -28), and a runtime abort in the disjoint-slice spec test.

The gap: analyze audit:all reported violations=0 on the affected programs — nothing validates that typed-expr types are well-formed for their context. A program whose int literal is typed as a bitpacked u7 outside any bitpacked-struct context should be an audit violation; so should a TY_GENERIC_INST accessor consult on a non-inst type (now impossible — the accessors are kind-guarded as of the #682-inc1 commit — but the audit class generalizes: d-slot reads interpreted under the wrong kind).

Suggested audit cells (audit:storage/audit:mir family):

  • Every typed_expr_types entry's type is spellable in its context: sub-byte int types only inside bitpacked layouts; no TY_ALIAS leaks post-resolve; no error-type propagation into MIR.
  • Generic-inst arg vectors: d2 == extra count, args are valid type ids.

The kind-guards fix the concrete bug; this issue is the instrument so the class self-identifies next time a layout shift changes which garbage gets read.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): still open. The root bug is fixed (Sema.get_generic_inst_base returns 0 for a non-instance type), but the audit gap remains: analyze has no typed-expression well-formedness audit (the audit:* list in src/Analysis.w: calls, effects, storage, pool, methods, phase, mir, returns, receivers, receiver-surface, pool-views, contract, resolution, codegen, trait-tables). audit:resolution checks that MIR agrees with Sema, which a wrong Sema type passes.

    post-release: analysis tooling.

  2. added this to the post-v0.16.0 milestone on Oct 28, 2026
  3. ehartford commented on Nov 17, 2026

    @ehartford
    ContributorAuthor

    #3390 adds the range checks to audit:storage (and audit:all): the type table's columns are parallel, every generic instance's argument vector lies inside type_extra and names type ids, and every typed expression names a type id. Clean on the compiler's own source (1,560,278 facts). Test: test/internals/analysis_type_table_test.w.

    Left open here: the context rule (a sub-byte integer type only where a bit-packed layout or a bitfield setter demands one; no TY_ALIAS after resolution; no error type reaching MIR). Each needs the expression's context or a decision about what is legitimate (a bitfield setter takes u3, D147), so they are separate cells.

  4. ehartford commented on Nov 18, 2026

    @ehartford
    ContributorAuthor

    #3422 adds one more cell to audit:mir: a MIR local's type is a type of the table (the error type, or a slot that is no type id, in a lowered body is a violation). Running the audit over the compiler's own source for it turned up two violations on main, both fixed in the same PR: a drop-state false positive (a field assigned whole after a sub-field was dropped) and a move of a Copy field out of a global (D52). audit:mir on src/main.w is clean now.

    Still open here: the context rules (a sub-byte integer type only where a bit-packed layout or a bitfield setter demands one; no alias after resolution). Each needs a statement of what is legitimate.

  5. ehartford commented on Nov 18, 2026

    @ehartford
    ContributorAuthor

    Closing: the instrument exists, and the two remaining 'context rules' turn out not to be invariants. Measured with a counting build of audit:storage (not landed):

    • 'No alias after resolution' is not true of correct programs. The compiler's own source has 18,966 alias-typed expressions out of 1,446,568, with audit:all clean. An expression typed by an alias is normal; consumers resolve it.
    • 'A sub-byte integer only inside a bit-packed layout' is not true either. u1..u7 are ordinary types (§4): fn low(x: u4) -> u4: x with let x: u4 = 3 compiles and runs. A bit-packed test has 11 sub-byte-typed expressions, all legitimate (field reads and the literals compared with them); the compiler's source has none. 'Spelled somewhere in the program' cannot be a rule, since the standard library is part of every compilation.

    What guards the class that bit #682 now:

    A layout shift that makes an accessor read garbage now shows as one of those.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions