Skip to content

fix(core): repoint the class-name cache at the live class after a hot swap - #302

Merged
lisachenko merged 2 commits into
8.4from
feature/sleepy-brahmagupta-hygovq
Nov 7, 2026
Merged

lisachenko merged 2 commits into
8.4from
feature/sleepy-brahmagupta-hygovq

Conversation

@lisachenko

Copy link
Copy Markdown
Owner

What this changes

Fixes a use-after-free in HotSwap that showed up while rechecking z-engine against PHP 8.6.0RC2.

HotSwap::prepare() compiles the new source as a donor class with the same name. If the donor has a parent or interfaces, zend_do_link_class() stores it in the engine's per-name class-entry cache (ZSTR_SET_CE_CACHE). HotSwap republished the live entry in the class table but left that cache pointing at the donor. After apply() or discard() destroys the donor, any lookup by an interned name (new ReflectionClass('Foo'), new Foo, ...) returns the freed donor.

  • A debug build aborts in ClassDeltaTest::testRemovedOverrideRestoresInheritedMethod with zend_hash.c: ht=... is already destroyed, inside method_exists().
  • A release build reads freed memory without any warning, which is why CI never caught it (CI runs the default suite on release builds only).

The fix:

  • adds ReflectionClass::bindClassNameCache(), which repoints the slot through the existing StringEntry::setCachedClassEntry();
  • calls it from HotSwap::compileDonorClass() right after the live class is republished.

The new test testNameLookupResolvesLiveClassAfterSwapOfLinkedClass compares class-entry addresses after both discard() and apply(). It fails without the fix on any build, not only on debug builds.

The bug is not specific to 8.6: the cache write in zend_do_link_class() is the same on 8.5 (and on 8.4, as far as I can tell), and HotSwap.php is identical on 8.4 and master. So this targets 8.4 and cascades up.

Environment it was verified on

  • PHP version: PHP 8.6.0RC2 (cli) (built: Nov 7 2026 00:04:30) (NTS DEBUG)
  • Thread safety: NTS
  • OS / architecture: Linux x64
  • Debug build (--enable-debug)? yes

Because of the version-matching rule, I tested this patch applied onto master with PHP 8.6. I did not run it on 8.4; the 8.4 CI legs will cover that. Results on master + patch:

  • composer test: 606 tests OK (before the fix the run aborted at about test 109)
  • test:opcache, test:opcache-runner, test:internal (process isolation, debug build), test:performance, worker-loop soak: all green
  • phpstan (level max) and cs:check: green

Checklist

  • Targets the minimum affected version branch (8.4)
  • composer test passes on the matching PHP minor (verified on 8.6 via master; 8.4 left to CI)
  • composer phpstan (level max) and composer cs:check are green
  • Tests added or updated; no new struct fields dereferenced
  • tools/generator/symbols.php unchanged
  • Conventional Commits used for the commit messages

Generated by Claude Code

… swap

HotSwap compiles the new source as a donor class under the same name. When the
donor has a parent or interfaces, zend_do_link_class() stores it in the
engine's per-name class-entry cache. HotSwap republished the live entry in the
class table but left that cache pointing at the donor, so any later lookup by
an interned name (new ReflectionClass('Foo'), new Foo, ...) returned the donor
after it was destroyed on apply() or discard(): a use-after-free.

A PHP debug build catches it as a hash-table consistency abort in
ClassDeltaTest::testRemovedOverrideRestoresInheritedMethod; release builds
read freed memory silently.

ReflectionClass::bindClassNameCache() now repoints the slot, and HotSwap calls
it right after republishing the live class. The new regression test compares
class-entry addresses, so it fails on release builds too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FxhC1Ec16gRujMto2EbKdR

Copy link
Copy Markdown
Owner Author

CI: the header-drift checks are failing, and this PR didn't cause it

Failing checks: Generated headers up to date, plus every Generated darwin headers up to date (*) and Generated windows headers up to date (*) leg.

This PR doesn't touch anything under include/, stubs/ or tools/generator/. The diff comes from the PHP 8.4 build itself: the latest 8.4 patch release added new engine fields, so regenerating from it no longer matches the committed definitions.

  • zend_compiler_globals.interned_strings_fallible (_Bool). It sits at offset 443, in former padding, so ast stays at 448.
  • zend_executor_globals.delayed_oom (new zend_oom_info struct). It is appended at the end; EG grows from 1960 to 1984 bytes (2128 to 2152 on ZTS).

No existing offset moves, so the code that reads these structs stays correct. Only the generated files are stale. The last CI run on 8.4 (2026-10-10) was green because it ran before this patch release, so any PR against 8.4 will hit the same drift now.

No fix exists yet. The repo's documented path is a separate refresh of the 8.4 definitions:

  1. composer gen-headers on 8.4 for the linux targets, plus stubs/ and .phpstorm.meta.php.
  2. workflow_dispatch the Generate darwin headers and Generate windows headers workflows against 8.4.

I've kept that out of this PR so the fix stays separate from a generated-file refresh. Once 8.4 carries the refreshed headers, I'll merge it in here.


Generated by Claude Code

@lisachenko
lisachenko marked this pull request as ready for review November 7, 2026 07:35
@lisachenko
lisachenko merged commit c25cdb2 into 8.4 Nov 7, 2026
21 checks passed
@lisachenko
lisachenko deleted the feature/sleepy-brahmagupta-hygovq branch November 7, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants