Repository navigation
fix(core): repoint the class-name cache at the live class after a hot swap - #302
Conversation
… 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
|
CI: the header-drift checks are failing, and this PR didn't cause it Failing checks: This PR doesn't touch anything under
No existing offset moves, so the code that reads these structs stays correct. Only the generated files are stale. The last CI run on No fix exists yet. The repo's documented path is a separate refresh of the 8.4 definitions:
I've kept that out of this PR so the fix stays separate from a generated-file refresh. Once Generated by Claude Code |
What this changes
Fixes a use-after-free in
HotSwapthat 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. Afterapply()ordiscard()destroys the donor, any lookup by an interned name (new ReflectionClass('Foo'),new Foo, ...) returns the freed donor.ClassDeltaTest::testRemovedOverrideRestoresInheritedMethodwithzend_hash.c: ht=... is already destroyed, insidemethod_exists().The fix:
ReflectionClass::bindClassNameCache(), which repoints the slot through the existingStringEntry::setCachedClassEntry();HotSwap::compileDonorClass()right after the live class is republished.The new test
testNameLookupResolvesLiveClassAfterSwapOfLinkedClasscompares class-entry addresses after bothdiscard()andapply(). 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), andHotSwap.phpis identical on8.4andmaster. So this targets8.4and cascades up.Environment it was verified on
PHP 8.6.0RC2 (cli) (built: Nov 7 2026 00:04:30) (NTS DEBUG)--enable-debug)? yesBecause of the version-matching rule, I tested this patch applied onto
masterwith PHP 8.6. I did not run it on 8.4; the8.4CI legs will cover that. Results onmaster+ 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 greenphpstan(level max) andcs:check: greenChecklist
8.4)composer testpasses on the matching PHP minor (verified on 8.6 viamaster; 8.4 left to CI)composer phpstan(level max) andcomposer cs:checkare greentools/generator/symbols.phpunchangedGenerated by Claude Code