Skip to content

Return array<T, N> by value instead of pointing into the dead frame (#573) - #576

Merged
godofecht merged 1 commit into
mainfrom
fix-array-return
Aug 19, 2026
Merged

godofecht merged 1 commit into
mainfrom
fix-array-return

Conversation

@godofecht

@godofecht godofecht commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #573.

The bug

function triad(root: i32) -> array<i32, 3> {
    return [root, root + 4, root + 7]
}

returned garbage. -> array<T, N> lowered to T* over automatic storage:

int32_t* triad_i32(int32_t root) {
    return (int32_t[]){ root, (root + 4), (root + 7) };
}
memcpy(chord, triad_i32(60), sizeof(chord));   // reads a dead frame
before:  literal: -16 -22344160 -19542520
after:   literal: 60 64 67

No crash and no diagnostic from Flow, though clang printed -Wreturn-stack-address on every build. All 25 functions in lib/stdlib/ returning a fixed-size array were affected. lib/stdlib/audio/scales.flow was entirely non-functional: scale_major(48) returned garbage rather than 48 50 52 53 55 57 59.

The fix

The value travels home inside a generated wrapper:

typedef struct { int32_t v[3]; } _flow_arr_int32_t_3;
_flow_arr_int32_t_3 triad_i32(int32_t root) { ... }

Consumers read .v, which decays to the same int32_t* the old signature produced. That is what makes this small: the memcpy at each declaration and each assignment is untouched, and so are indexing a call result and passing one to another function. The rewrite lives in the single FunctionCall branch of _gen_expr rather than at every consumer.

C11 6.2.4p8 gives the returned struct a lifetime through the enclosing full expression, which is what memcpy(dest, f().v, sizeof(dest)) and f(x).v[0] need.

What is deliberately not fixed

Nested array<array<T, N>, M> is excluded. It is represented as an array of pointers everywhere in this generator, so wrapping only its return would give back struct { int32_t* v[4]; }: still four pointers into the dead frame. _sized_array_type skips it, so behaviour there is unchanged rather than half-changed. Filed as #575 with what the representation needs first. Three functions are affected, all progression_* in scales.flow.

Why the suite was green

tests/arrays/test_array_return.flow is the only test for this and does not exercise the broken path:

function test_array() -> array<f32> {
    let arr: array<f32> = array<f32>(10)
    return arr
}

array<f32> is unsized and heap allocated, so returning it is safe. It also never reads a value back, only print("Test complete").

The new tests in tests/integration/test_array_return.py read the values, cover returning a literal, returning a local, indexing a call result and passing one along, check a real stdlib scale and triad, and assert clang emits no return-stack-address warning. That last one uses the compiler's own diagnostic as ground truth rather than depending on what the garbage happens to be.

Verification

Check Result
pytest tests/ 1655 passed, 6 skipped
./flow test --tier2 --strict 687/687
ruff check src clean

Found by compiling the examples in lib/stdlib/audio/README.md.

…573)

    function triad(root: i32) -> array<i32, 3> {
        return [root, root + 4, root + 7]
    }

returned garbage. `-> array<T, N>` lowered to `T*` over automatic storage:

    int32_t* triad_i32(int32_t root) {
        return (int32_t[]){ root, (root + 4), (root + 7) };
    }
    memcpy(chord, triad_i32(60), sizeof(chord));   // reads a dead frame

No crash and no diagnostic from Flow, though clang printed
-Wreturn-stack-address on every build. All 25 functions in lib/stdlib/
returning a fixed-size array were affected; lib/stdlib/audio/scales.flow was
entirely non-functional.

The value now travels home inside a generated wrapper:

    typedef struct { int32_t v[3]; } _flow_arr_int32_t_3;

Consumers read `.v`, which decays to the same `int32_t*` the old signature
produced, so the memcpy at each declaration and assignment is unchanged, and
so are indexing a call result and passing one along. The rewrite sits in the
single FunctionCall branch of _gen_expr rather than at each consumer.

C11 6.2.4p8 gives the returned struct a lifetime through the enclosing full
expression, which covers `memcpy(dest, f().v, sizeof(dest))` and `f(x).v[0]`.

Nested `array<array<T, N>, M>` is excluded. It is an array of pointers
everywhere in this generator, so wrapping only its return would still hand
back pointers into the dead frame. Behaviour there is unchanged rather than
half-changed; filed as #575 with what the representation needs first.

The one existing test, tests/arrays/test_array_return.flow, returns an
unsized array<f32>, which is heap allocated and therefore safe, and never
reads a value back. The new tests read the values, and assert clang emits no
return-stack-address warning.

Closes #573

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
godofecht added a commit that referenced this pull request Aug 19, 2026
The xfail asserted that -> array<T, N> stops returning stack memory. #576
makes that true, so keeping it here would XPASS and fail the build the moment
that lands. The real regression test lives with the fix, in
tests/integration/test_array_return.py, and reads the values back.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@godofecht
godofecht merged commit 83ba359 into main Aug 19, 2026
19 checks passed
@godofecht
godofecht deleted the fix-array-return branch August 19, 2026 10:31
godofecht added a commit that referenced this pull request Aug 19, 2026
The xfail asserted that -> array<T, N> stops returning stack memory. #576
makes that true, so keeping it here would XPASS and fail the build the moment
that lands. The real regression test lives with the fix, in
tests/integration/test_array_return.py, and reads the values back.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
godofecht added a commit that referenced this pull request Aug 19, 2026
* Make lib/stdlib/audio/README.md describe the API that exists

Every code block in this README failed to compile. Three sections named
functions that were never written:

  gpu_process_buffer(input, effect_kernel)   -> audio_gain_block(backend, buf, gain)
  simd_gain_f32x8(samples, gain)             -> audio_gain_interleaved_f32(...)
  graph_new(); graph_add_oscillator(...)     -> effect_chain_new(rate); effect_chain_*

The graph section described a node-and-edge API. What exists is a value:
each stage returns a new EffectChain, so the section is rewritten around
that rather than renamed onto it.

pattern_4 and euclidean_pattern were called with a trailing voice argument.
They take no such argument; pattern_4 always plays through bass and
pattern_8 through lead, and the comment in livecode.flow ("caller can modify
this pattern if needed") shows the parameter was intended and never added.
The README now says which voice each one uses.

Five blocks used `clock` without declaring it, and two used both `clock` and
`pos` at file scope with no enclosing function. A reader copying any of them
got the same errors CI now catches.

The GPU block is tagged ignore= rather than fixed: it links
runtime/audio_gpu_metal.m, which the doc example build does not compile. The
prose now gives the clang line, which it did not before.

unverified: 235 -> 224.

This turned up issue #573. lib/stdlib/audio/scales.flow returns fixed-size
arrays from every function, and `-> array<T, N>` lowers to a pointer into
the callee's dead frame, so all 25 such functions in the stdlib return
garbage. Added as a strict xfail in test_coverage_known_bugs.py so it
reports the moment it is fixed. The blocks here compile, which is all the
doc checker asserts; the one that also runs a scale function still prints
garbage until #573 is fixed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* Drop the #573 xfail: PR #576 fixes the bug rather than recording it

The xfail asserted that -> array<T, N> stops returning stack memory. #576
makes that true, so keeping it here would XPASS and fail the build the moment
that lands. The real regression test lives with the fix, in
tests/integration/test_array_return.py, and reads the values back.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* Regenerate the example ledger after rebasing onto the occurrence keys

#572 changed the key format and #574 changed which blocks pass, so the
checked-in ledger conflicted. Regenerated rather than hand-merged.

unverified: 235 -> 221 with both changes applied.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

Any function returning array<T, N> returns garbage: the C backend returns a pointer into the dead frame

1 participant