Skip to content

require of a unit that returns a value: the array is released once per call but retained once, and realpath's buffer leaks #559

Closed

Description

@blacktrs

require / include of a compile unit that ends in return [...] has two memory bugs. Both are fixed in #555; this issue records them so the gap is visible on main.

1. Crash: the returned value is released once per require, but the unit slot holds a single count

// a.php
<?php return ['name' => 'conf', 'n' => 3];
// main.php
<?php
$x1 = require __DIR__ . '/a.php';
$x2 = require __DIR__ . '/a.php';   // second require of the same unit
var_dump($x1['n'], $x2['n']);

Compile the directory. With one require the program is fine. With two or more, the same array is released twice at exit: __mir_array_release_cell reads freed memory. macOS libgmalloc shows it (DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib ./bin, exit 139); on Linux the default allocator crashes (CI: require_const_fold, rc=139). Cause: __mir_incl_resolve returns the slot's word without a retain, while every consumer treats a call result as an owned +1.

2. Leak: every require leaks a realpath buffer

The include resolver calls realpath(path, NULL), which mallocs the answer, and never frees it. About 1 KB per call in a loop; 300,000 calls of function f() { $x = require __DIR__ . '/a.php'; ... } grew memory by 308 MB. Matters for a long-running server that requires data files per request.

Fix

In #555: __mir_cell_retain on the slot-hit arm and free(realpath) after the resolve chain; case require_value_ownership (double require, require as argument / statement / element, flat memory over 100k calls).

Activity

  1. added
    bug
    crash
    leak
    on Nov 2, 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

    bugcrashleak

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions