Skip to content

Stdlib: get_defined_vars() — exclude alias pollution + unset locals (zend_get_defined_vars parity) #4517

Closed

Description

@PurHur

Category

stdlib

Status (expanded 2026-06-03 — implementation-ready)

Child of #4377. Implement this issue before closing the umbrella.

Problem

#4377 tracks general get_defined_vars() symbol-table parity. Concrete gaps in ext/standard/VmScope.php::getDefinedVars() today:

  1. global $x; — Zend's snapshot may include globals linked into the function scope; this build iterates eachNamedScopeSlot() and can diverge on which names appear.
  2. unset($local) — Zend excludes unset locals; this build skips only TYPE_NULL via callerVarIsSet() but can still expose slots that Zend omits.
  3. $GLOBALS entries — must not duplicate superglobal table keys incorrectly.

php-src reference

Repro

repro_get_defined_vars.php:

<?php
function f() {
    global $g;
    $a = 1;
    $b = 2;
    unset($b);
    $vars = get_defined_vars();
    ksort($vars);
    var_export(array_keys($vars));
}
$g = 9;
f();

Commands

php repro_get_defined_vars.php
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro_get_defined_vars.php'

Expected (Zend PHP 8.x — authoritative)

array (
  0 => 'a',
  1 => 'g',
)

Notes:

  • b must not appear after unset($b).
  • g appears because global $g imports the global into the function symbol table for this snapshot.
  • Keys are sorted in repro for stable comparison; values are not required to match.

Failure signature today

vm.php often prints extra names (e.g. includes b, omits g, or lists $GLOBALS aliases). Capture actual output in PR and fix VmScope::getDefinedVars().

Scope (this repo)

  • ext/standard/VmScope.php — getDefinedVars()
  • ext/standard/get_defined_vars_.php
  • lib/VM/Frame.php / scope slots — unset marking

Done when

Parent

Labels

stdlib, implementation-ready

Activity

  1. added
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 2, 2026
  2. PurHur commented on Jun 9, 2026

    @PurHur
    OwnerAuthor

    claim: PHP Compiler STD Lib — starting this run

  3. PurHur commented on Jun 9, 2026

    @PurHur
    OwnerAuthor

    Unclaiming: all repros (#4517 global/unset, #4377 locals/global) already green on master. VM output matches Zend PHP 8.2 byte-for-byte. GetDefinedConstantsVars compliance PHPT green. Recommend closing as duplicate unless repro differs.

  4. PurHur commented on Jul 6, 2026

    @PurHur
    OwnerAuthor

    claim: cursor-agent-lane-c — starting this run

  5. PurHur commented on Jul 6, 2026

    @PurHur
    OwnerAuthor

    Merged via #16999.

    Root cause: VmScope::getDefinedVars() only iterated compile-time eachNamedScopeSlot() locals. extract() imports unknown names into $frame->dynamicLocals (#4826), so imported keys were absent from the snapshot.

    Fix: Append initialized dynamicLocals in VM; union namedVariablesInScope() into JIT get_defined_vars() lowering.

    Verification:

    php bin/vm.php test/repro/maintainer_gap_get_defined_vars_extract.php  # ok
    php vendor/bin/phpunit --filter GetDefinedConstantsVars test/unit/GetDefinedConstantsVarsBuiltinTest.php  # OK (6 tests)
    
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

    area:vmVirtual machineimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions