Skip to content

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

Closed

Description

@godofecht

Repro

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

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

function main() -> i32 {
    let a: array<i32, 3> = from_literal(60)
    printf("literal: %d %d %d\n", a[0], a[1], a[2])
    let b: array<i32, 3> = from_local(60)
    printf("local:   %d %d %d\n", b[0], b[1], b[2])
    return 0
}

Expected 60 64 67 twice. Actual:

literal: -16 -22344160 -19542520
local:   -16 -22344160 -19542520

No crash, no diagnostic from Flow. Silent garbage.

Cause

-> array<T, N> lowers to T*, and the returned storage is automatic:

int32_t* triad_i32(int32_t root) {
    return (int32_t[]){ root, (root + 4), (root + 7) };
}
...
memcpy(chord, triad_i32(60), sizeof(chord));

The compound literal has automatic storage duration, so the caller's memcpy reads a frame that is already gone. clang says so plainly, and the warning is in every build today:

warning: address of stack memory associated with compound literal
         {root, (root + 4), (root + 7)} returned [-Wreturn-stack-address]
warning: address of stack memory associated with local variable 'built' returned

Returning a local array variable has the same defect, so this is the return convention rather than one expression form.

Impact

25 functions in lib/stdlib/ return array<T, N>. Every one of them returns garbage. lib/stdlib/audio/scales.flow is entirely non-functional: scale_major, scale_minor, scale_pentatonic_major, scale_blues, scale_chromatic, chord_major, chord_minor and the rest all return fixed-size arrays.

Why the suite is green

tests/arrays/test_array_return.flow is the only test for this, and it 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. The test also never reads a value back, only print("Test complete").

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

Fix options

T* cannot be made safe here without changing the convention, since the storage has to outlive the callee's frame.

  1. Struct wrapper. Return struct { T v[N]; } by value. C returns structs by value safely, and the change is contained to the return type, the return statement, and call sites that consume an array return.
  2. Out-parameter. The caller passes the destination; the callee fills it. Matches what the caller already does with memcpy, but changes the signature of every array-returning function, including exported ones.

Option 1 looks smaller and keeps the exported ABI expressible in C.

A regression test should read the values back. Returning without checking the contents is what let this survive.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions