Skip to content

Reading a function-static array through a by-reference return (tab()["len"]) leaks a copy of the whole array on every call. #125

Closed

Description

@blacktrs

Summary

Reading a function-static array through a by-reference return (tab()["len"]) leaks a copy of the whole array on every call.

Repro

<?php
/** @return array<string, mixed> */
function &tab(): array
{
    static $t = ['k' => [], 'len' => 0];
    return $t;
}
function put(int $v): void { $t = &tab(); $t['k'][] = $v; $t['len']++; }
function len(): int { return tab()['len']; }
$m0 = memory_get_peak_usage();
$s = 0;
for ($i = 0; $i < 3000; $i++) { put($i); $s += len(); }
$mb = (memory_get_peak_usage() - $m0) >> 20;
fwrite(STDERR, "grew {$mb} MB\n");
echo $s, "\n";
echo $mb < 8 ? "flat\n" : "LEAK\n";

Expected (php)

4501500
flat

Actual (manticore 46bda0f8, Darwin arm64)

4501500
LEAK
(stderr: grew 73 MB)

Tracking

Known-bug repro: tests/aot/repro/leak/static_byref_array_read_leaks_copy.php — bash tests/aot/xfail.sh -k static_byref_array_read_leaks_copy reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Activity

  1. added
    bug
    leak
    on Oct 7, 2026
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

    bugleak

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions