Skip to content

fix(cgen): evaluate a call on the right of a store before reading the target - #1077

Merged
godofecht merged 3 commits into
mainfrom
fix/cgen-store-order
Sep 30, 2026
Merged

godofecht merged 3 commits into
mainfrom
fix/cgen-store-order

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Found while merging #1071. For a[i] = grow(), cgen emitted a[i] = grow();. C leaves the order of the two operands of = unspecified. gcc reads a before the call, so when grow() reallocs the array, the store lands in the freed block. clang happened to evaluate the call first. This crashed flowc on Linux while it built tools/lsp. #1071 worked around it by hand at 11 sites.

Fix

When the target is reached through a pointer and the right side may call a function, cgen evaluates the right side into a typed temporary first:

{ __typeof__(arr[0]) __flowc_st164 = grow(); arr[0] = __flowc_st164; }

A target counts as reached through a pointer when it is an index into anything but a sized array, or a field read through ptr<...>. The chained-assignment expression form (#1036) does the same inside a statement expression. A store whose right side has no call keeps the plain form, and so do stores to plain names and to value-struct fields.

mlirgen needs no change: mlg_emit_assign already emits the right side before it loads the target address.

The 11 workarounds from #1071 in sem_check.flow and sem_model.flow are plain stores again. The regenerated bootstrap C gives each of them the temporary.

Reproduction

tests/lang/test_store_after_call.flow covers arr[0] = grow(), t.data[0] = push(t, i) and h.p.x = move_point(h), where each call reallocs the storage.

Before, with the flowc from main:

  • gcc 13 + ASan on Linux: heap-use-after-free ... WRITE of size 4 at arr[0] = grow();, where the block was freed by realloc in grow
  • gcc-15 on macOS at -O0 and -O2: exit 138 (SIGBUS)
  • clang + ASan: clean, which is why the bug went unseen

After: exit 0 under gcc-15 at -O0 and -O2, and clean under gcc 13 + ASan.

Goldens

No existing tests/cgen expectation changed. There is one new case, tests/cgen/store_after_call, and it fails against the old compiler.

Gates

All pass:

  • bootstrap_from_c.sh --verify
  • self_host_full.sh
  • roundtrip.sh
  • parse_coverage.sh --check
  • fmt_check.sh --check
  • corpus_parity.sh --check
  • parity_typecheck.sh --check
  • typecheck_rules.sh
  • every parity_*.sh
  • tests/cgen/run.sh (100/0)
  • ./flow test-lang (220 passed)

The one exception is the Metal step of parity_shader_dsl.sh, which cannot run here because the Metal toolchain is not installed. Its fixtures and host stub pass.

--regen reached a fixed point at gen3, as expected for a cgen change.

On Linux, flowc built with gcc 13 + ASan from the new bootstrap C passes its self-test with no reports. It self-emits byte-identical C and builds tools/lsp with no reports. That LSP, also built with gcc + ASan, answers initialize.

A separate gap showed up while writing the test: a field read through a module-level ptr<T> (let mut tab: ptr<Table> then tab.len) emits (tab).len, which does not compile. The test uses locals and parameters instead.

`a[i] = grow()` was emitted as `a[i] = grow();`. C leaves the order of the
operands of `=` unspecified, and gcc reads `a` before the call, so when
grow() reallocs the array the store goes to the freed block. This crashed
flowc on Linux building tools/lsp.

When the target is reached through a pointer (an index into anything but a
sized array, or a field read through ptr<...>) and the right side may call
a function, cgen now emits

    { __typeof__(a[i]) __flowc_stN = grow(); a[i] = __flowc_stN; }

and the chained-assignment expression form does the same inside a
statement expression. Stores whose right side has no call keep the plain
form. The MLIR emitter already evaluates the right side before it loads
the target address.

tests/lang/test_store_after_call.flow and tests/cgen/store_after_call
cover `arr[0] = grow()`, `t.data[0] = push(t, i)` and `h.p.x = move(h)`.
The 11 stores from #1071 that went through a local to dodge gcc reading
the array before the call are plain assignments again. cgen now evaluates
a call on the right of a store through a pointer before it reads the
target.
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