Skip to content

bug(opcache): getStaticVariables() reads the defaults table instead of the live static_variables_ptr table for opcache-shared functions #239

Closed

Description

@lisachenko

Summary

For a function whose op_array lives in opcache shared memory (ZEND_ACC_IMMUTABLE), getStaticVariables() (src/Reflection/FunctionLikeTrait.php) returns the declaration defaults instead of the live, materialized static-variables table. Static-variable state changes made by calling the function are invisible to the reader.

Found by running the full suite with opcache.enable_cli=1 in the runner process: tests/Reflection/FunctionLikeInfoTest::testStaticVariablesOfNamedFunction fails with Failed asserting that 0 is identical to 2 — the test calls the function twice and expects the live invocations counter, but gets the default 0. Reproduced identically on the 8.4 line (PHP 8.4.19) and master (PHP 8.5.9); deterministic.

Mechanism

getStaticVariables() reads zend_op_array.static_variables_ptr and, when the value has the low bit set, falls back to the defaults table (the docblock even documents this as intended). But for immutable (shared-memory) op_arrays zend_persist.c stores the offset form of ZEND_MAP_PTR there: an odd byte offset into the per-process map-ptr area. The live table (materialized by the first ZEND_BIND_STATIC) sits in the slot at CG(map_ptr_base) + offset and is never dereferenced.

The codebase already resolves exactly this encoding elsewhere: StringEntry::setCachedClassEntry() computes the slot as base + offset (base is pre-biased, no -1) and bound-checks with (offset - 1) / sizeof(void*) < CG(map_ptr_last), mirroring ZEND_MAP_PTR_OFFSET2PTR / ZSTR_VALID_CE_CACHE.

Fix shape

In getStaticVariables(): when the low bit is set, dereference the map-ptr slot (Compiler::getMapPointerBaseAddress() + offset, with the bound check); return the materialized table when the slot is non-NULL, and fall back to the defaults only while the slot is still NULL (before the first call — semantically correct, since the defaults are what the first bind installs). Rewrite the misleading docblock.

getStaticVariables() is the only static-variables reader in the codebase, so no other call site is affected.

Regression coverage

  • FunctionLikeInfoTest::testStaticVariablesOfNamedFunction as-is, under an opcache-active runner.
  • A deterministic leg in the OpcacheSupportMatrixTest child script asserting the raw field is in offset form and that the reader sees the live count, so the branch is exercised in the default (opcache-off) CI too.

Applies to branch 8.4 first, cascades to master (encoding identical on both lines).

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

    bugSomething isn't workingopcacheIssue is related to opcache integration. Either doesn't work well or behaves incorrect.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions