From 9c93abf2955b045d7b5028cb03d14f1d67417a23 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 00:25:35 +0000 Subject: [PATCH] fix(core): repoint the class-name cache at the live class after a hot 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 Claude-Session: https://claude.ai/code/session_01FxhC1Ec16gRujMto2EbKdR --- src/HotSwap/HotSwap.php | 4 ++++ src/Reflection/ReflectionClass.php | 30 ++++++++++++++++++++++++++++++ src/Type/StringEntry.php | 3 ++- tests/HotSwap/ClassDeltaTest.php | 19 +++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/HotSwap/HotSwap.php b/src/HotSwap/HotSwap.php index df89b02f..e9cbf998 100644 --- a/src/HotSwap/HotSwap.php +++ b/src/HotSwap/HotSwap.php @@ -207,6 +207,10 @@ private static function compileDonorClass( self::unpublishClassEntry($lowerName); } self::publishClassEntry($lowerName, $liveClass); + // Linking the donor (a class with a parent or interfaces) memoized it in the + // engine's class-name cache. Repoint that slot at the live entry, or every later + // lookup by name resolves the donor, which is destroyed on apply or discard. + $liveClass->bindClassNameCache($donorClass); } if ($compileError !== null) { diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index 56087fdd..522690cb 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -782,6 +782,36 @@ public function copyOutOfSharedMemory(): void } } + /** + * Points the engine's fast class-name cache at this class entry + * + * A class lookup by an interned name reads a memoized per-request slot BEFORE the + * class table, and the engine fills that slot itself whenever it links a class + * (zend_do_link_class). Code that swaps the entry a name resolves to behind the + * engine's back must repoint the slot too, or lookups keep returning the old entry. + * + * @param ReflectionClass|null $namesake Another entry declared under the same name + * whose name string must stop resolving to it + * + * @internal shared with the hot-swap machinery (HotSwap) + */ + public function bindClassNameCache(?ReflectionClass $namesake = null): void + { + $nameStrings = [$this->pointer->name]; + if ($namesake !== null) { + // Usually the same interned string, but nothing guarantees it + $nameStrings[] = $namesake->pointer->name; + } + foreach ($nameStrings as $nameString) { + // Engine invariant: every class entry carries a name + assert($nameString !== null); + $nameEntry = StringEntry::fromCData($nameString); + if ($nameEntry->hasClassEntryCache()) { + $nameEntry->setCachedClassEntry($this->pointer); + } + } + } + /** * Returns the raw zend_class_entry pointer this reflection wraps * diff --git a/src/Type/StringEntry.php b/src/Type/StringEntry.php index 59dac5bf..a662ae20 100644 --- a/src/Type/StringEntry.php +++ b/src/Type/StringEntry.php @@ -16,6 +16,7 @@ use FFI\CData; use ReflectionClass; use ZEngine\Core; +use ZEngine\Generated\zend_class_entry; use ZEngine\Generated\zend_refcounted_h; use ZEngine\Generated\zend_string; use ZEngine\Memory\Allocator; @@ -335,7 +336,7 @@ public function hasClassEntryCache(): bool * sites compiled into cached scripts read the memoized entry, not the table, so without * this refresh they would keep dispatching the shared-memory class entry. * - * @param CData $classEntry zend_class_entry the name must resolve to from now on + * @param CData|zend_class_entry $classEntry zend_class_entry the name must resolve to from now on * * @see zend_types.h:ZSTR_SET_CE_CACHE */ diff --git a/tests/HotSwap/ClassDeltaTest.php b/tests/HotSwap/ClassDeltaTest.php index d28bcd41..4c39e002 100644 --- a/tests/HotSwap/ClassDeltaTest.php +++ b/tests/HotSwap/ClassDeltaTest.php @@ -20,6 +20,7 @@ use ZEngine\ClassExtension\ObjectCreateTrait; use ZEngine\OpCache\SharedMemoryException; use ZEngine\Reflection\ReflectionClass; +use ZEngine\Type\ObjectEntry; class ClassDeltaTest extends TestCase { @@ -139,6 +140,24 @@ public function testRemovedOverrideRestoresInheritedMethod(): void $this->assertSame('base', self::callMethod($restoredInstance, 'speak')); } + public function testNameLookupResolvesLiveClassAfterSwapOfLinkedClass(): void + { + $baseName = 'HotSwapBaseForNameCache'; + $className = 'HotSwapNameCacheTarget'; + eval("class {$baseName} {}"); + eval("class {$className} extends {$baseName} { public function speak(): string { return \"live\"; } }"); + $liveAddress = (new ReflectionClass($className))->getAddress(); + + // Linking a donor with a parent stores it in the engine's class-name cache. A lookup + // by name must still reach the live entry once the donor is destroyed, on discard + // as well as on apply. + HotSwap::prepare($className, "class {$className} extends {$baseName} {}")->discard(); + $this->assertSame($liveAddress, (new ObjectEntry(self::newInstance($className)))->getClass()->getAddress()); + + HotSwap::prepare($className, "class {$className} extends {$baseName} {}")->apply(); + $this->assertSame($liveAddress, (new ObjectEntry(self::newInstance($className)))->getClass()->getAddress()); + } + public function testConstantChangeAndAddition(): void { $className = $this->declareClass('Const', '{ public const VERSION = 1; }');