Skip to content

feat(opcache): decline inheritance-cache publication for classes that receive handlers during linking #241

Closed

Description

@lisachenko

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

  1. Add zend_inheritance_cache_add (and any companion symbols needed for a clean decline) to tools/generator/symbols.php; regenerate include/ for all platform targets — Linux via composer gen-headers (docker), macOS/Windows via the generate-darwin-headers.yml / generate-windows-headers.yml dispatch workflows.
  2. 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.
  3. Un-throw the interim guard: with the interception active, handler installation from interface_gets_implemented becomes supported; the guard remains only as a fallback when the interception is unavailable (e.g. platform target without regenerated headers).
  4. 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) asserting write_property fires with opcache enabled; a matrix leg asserting an un-hooked sibling class still lands in the inheritance cache.
  5. 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_string read inside an FFI callback, and does not remove the multi-worker hazard.

Target branch 8.4 first, cascade to master.

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

    enhancementNew feature or requestopcacheIssue 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