Repository navigation
Description
Summary
The real fix for #238. Under opcache, a class linked at runtime against a cached interface is linked on a temporary mutable copy (zend_lazy_class_load()), and after linking opcache's inheritance cache persists the linked result into shared memory and discards the temporary. Handlers installed from an interface_gets_implemented hook are keyed to the discarded temporary's address and are silently lost (full diagnosis in #238's comments).
The interim measure (landing with the opcache CI/test effort) is a loud guard: handler installation on a lazy-linking temporary throws SharedMemoryException. This ticket is the follow-up that makes the feature actually work under opcache.
Design
Intercept zend_inheritance_cache_add — a ZEND_API extern function pointer, so it is hookable from FFI — and decline caching for classes that received z-engine handlers during linking:
- The linked class entry then stays process-local and mutable; the address-keyed handler table stays valid; handlers fire.
- The multi-worker hazard disappears by construction: no process-local libffi trampoline address is ever published into shared memory / the inheritance cache, so no sibling FPM worker can reach a trampoline that does not exist in its address space.
- Cost: the affected class loses inheritance-cache reuse (re-linked per process/request). Only classes actually hooked during linking pay it.
Work items
- Add
zend_inheritance_cache_add(and any companion symbols needed for a clean decline) totools/generator/symbols.php; regenerateinclude/for all platform targets — Linux viacomposer gen-headers(docker), macOS/Windows via thegenerate-darwin-headers.yml/generate-windows-headers.ymldispatch workflows. - Wrap the function pointer during
Core::init(): delegate to the original unless the class (or one of the classes linked in the same request) is registered in the handler table; in that case return the "not cached" result the engine expects. - Un-throw the interim guard: with the interception active, handler installation from
interface_gets_implementedbecomes supported; the guard remains only as a fallback when the interception is unavailable (e.g. platform target without regenerated headers). - Regression tests: the bug(opcache): a write_property handler installed from an interface_gets_implemented hook never fires when opcache is enabled #238 reproducer (cached interface + hook + implementor,
opcache.file_update_protection=0) assertingwrite_propertyfires with opcache enabled; a matrix leg asserting an un-hooked sibling class still lands in the inheritance cache. - Docs: update the hot-swap opcache support matrix row added by the interim guard.
Alternatives considered and rejected (recorded from the #238 discussion)
- Publish the handler block via
zend_class_entry.default_object_handlers: works in CLI, but deliberately writes into the SHM entry, changes the documented "create_object required first" contract, and leaves the FPM hazard — a process-local trampoline address reachable from sibling workers via the shared inheritance-cache entry. Rejected as unshippable for multi-worker SAPIs. - Re-keying the handler map by class name: fixes only the lookup miss, keeps the association in userland bookkeeping, requires a
zend_stringread inside an FFI callback, and does not remove the multi-worker hazard.
Target branch 8.4 first, cascade to master.
Activity