Repository navigation
fix(cgen): evaluate a call on the right of a store before reading the target - #1077
Merged
Merged
Conversation
`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.
godofecht
force-pushed
the
fix/cgen-store-order
branch
from
September 30, 2026 12:20
6087e80 to
014509a
Compare
This was referenced Sep 30, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found while merging #1071. For
a[i] = grow(), cgen emitteda[i] = grow();. C leaves the order of the two operands of=unspecified. gcc readsabefore the call, so whengrow()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_assignalready emits the right side before it loads the target address.The 11 workarounds from #1071 in
sem_check.flowandsem_model.floware plain stores again. The regenerated bootstrap C gives each of them the temporary.Reproduction
tests/lang/test_store_after_call.flowcoversarr[0] = grow(),t.data[0] = push(t, i)andh.p.x = move_point(h), where each call reallocs the storage.Before, with the flowc from main:
heap-use-after-free ... WRITE of size 4atarr[0] = grow();, where the block was freed byreallocingrowAfter: exit 0 under gcc-15 at -O0 and -O2, and clean under gcc 13 + ASan.
Goldens
No existing
tests/cgenexpectation 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 --verifyself_host_full.shroundtrip.shparse_coverage.sh --checkfmt_check.sh --checkcorpus_parity.sh --checkparity_typecheck.sh --checktypecheck_rules.shparity_*.shtests/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.--regenreached 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>thentab.len) emits(tab).len, which does not compile. The test uses locals and parameters instead.