Skip to content

Compiler segfault: two &T functions using comptime for T.fields() with value.{field.name} #574

Closed

Description

@ehartford

Spec reference: §17 comptime field reflection; §3.8 (&T parameters).

with check (and test) crashes with SIGSEGV (exit 139, no diagnostic) on:

type Pair { x: i32, y: i32 }

fn describe_pair(value: &Pair) -> str:
    var out = ""
    comptime for field in Pair.fields():
        out = out ++ field.name ++ "=" ++ value.{field.name}.debug_str() ++ ";"
    out

fn sum_pair(value: &Pair) -> i32:
    var total = 0
    comptime for field in Pair.fields():
        total = total + value.{field.name}
    total

fn main:
    let value = Pair { x: 40, y: 2 }
    assert(describe_pair(value) == "x=40;y=2;")
    assert(sum_pair(value) == 42)
    print("ok")

Either function alone compiles and runs fine with value: &Pair; the crash needs both present. With by-value value: Pair both compile fine. Reproduces on both the flipped-gate staged compiler and the pre-flip installed compiler, so it is not a #564 regression — it is a pre-existing crash in comptime field projection through references.

Expected: compile successfully (or emit a diagnostic). Actual: compiler process dies with signal 11 and no output.

Workaround in tree (remove when fixed): test/behavior/behav_comptime_computed_field_access.w keeps both helpers by-value and binds a second Pair value instead of flipping the read-only params to &Pair.

Activity

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Root-caused — and it's worse than a comptime expansion bug: active use-after-free + double-free in the shipped compiler, with the &T/computed-field framing entirely incidental.

    Minimal repro: two functions each containing comptime for field in One.fields(): (neither needs params, neither needs to be called, no computed access needed). The crash needs the Sema type tables to sit at exactly len == cap when fields() evaluates; the &One in the original report merely perturbed the type count onto the 256 boundary.

    Mechanism (lldb-verified, including a vec_grow free-trace showing the same four buffers freed twice):

    1. comptime_force_eval_expr_result/comptime_try_eval_expr_result (ComptimeEval.w:671/682; also :699/:727) do var sema = *sema_ptr — a shallow by-value Sema copy sharing all heap buffers.
    2. eval_type_fields_array (:1803) calls ensure_exact_type → Sema.add_type pushes onto the type tables on the copy; at len==cap all four vecs vec_grow, which frees the old shared buffers (rt_core.w:1725). The original transform_sema now dangles. The mutation is then discarded (only diags/tracked paths merge back).
    3. The second function re-copies the stale headers, misses the cache again (the add was discarded), grows again → frees the same buffers a second time, memcpying from freed memory. The freelist now contains live blocks; later allocation pops a clobbered next pointer (0x10) and faults in rt_alloc_unlocked.

    Corollary defects from the same root: the returned [FieldInfo; N] type id dangles (added on the discarded copy), and eval_type_variants_array (:1823) has the identical hazard for enums. With a single function the first free still happens — silent UB that just doesn't get observed.

    Spec §17.2/§17.4 bless this program; no diagnostic is warranted — pure compiler memory-safety bug.

    Fix: preferred — stop copying Sema by value at the four eval entry points (operate on the original; aligns with the by-value-Sema hazard class from #564/#576). Surgical interim — merge the four type-table vec headers back alongside the existing diags merge at :675-679/:686-690 (the only evaluator mutations of those tables are the two ensure_exact_type calls). Either way, test/behavior/behav_comptime_computed_field_access.w's by-value workaround can then revert to &Pair.

  2. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    Closing as fixed at the root on HEAD (verified by probe + source): the two-&T-functions comptime T.fields() repro checks and runs (no SIGSEGV). Root cause was the shared-Sema-buffer hazard; all four ComptimeEval entry points (ComptimeEval.w:1217/1225/1239/1267) now take sema.prepare_comptime_eval_copy() deep copies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions