Repository navigation
fix: a lambda the preload script runs keeps working under the JIT - #170
Merged
Merged
Conversation
Problem: with opcache.preload and opcache.jit=function, a debug build stopped on the assertion in zend_jit() that a closure's op array has no scope. It happened whenever the preload script ran a class method that makes a closure. Every PHP# lambda lives in a class, so each one does this. A plain PHP closure inside a class method does the same. A release build skips the assertion and compiles the closure for that scope. A plain PHP closure the preload script made, rebound to another object in a request, then crashed it under the function JIT and the hot-counter JIT. The first time a closure is made, zend_create_closure_ex() stores the class scope on the declaring op array, so the closures made after it can share one runtime cache. It only does that to an op array it thinks this request owns, which it checks with ZEND_ACC_IMMUTABLE. The preload script runs on op arrays that are not immutable yet. Opcache then stores them, scope included, in shared memory for every request. None of the suite's 22 modes caught it. The --passes function JIT mode already runs every preload test, but each preload script only requires its files or calls code that makes no lambda. Solution: zend_create_closure_ex() also treats a ZEND_ACC_PRELOADED op array as shared, so the preload request gives such a closure its own runtime cache and leaves the op array's scope alone. A new preload fixture makes each lambda shape during the preload request: one that captures nothing, one that captures a local, one that uses this, one in a static method, one passed to a plain PHP function and one in a field. lambdas_preload.phpt then runs them in every mode. Its _jit_function and _jit_tracing rows run them under each JIT even without --passes. Zend/ └── zend_closures.c ZEND_ACC_PRELOADED counts as shared Zend/tests/sharp/ ├── PreloadedLambdas/Shapes.sharp new: five lambda shapes ├── PreloadedLambdas/StoredStep.sharp new: a lambda in a field ├── lambdas_preload.inc new: makes each one while preloading ├── lambdas_preload.phpt new ├── lambdas_preload_jit_function.phpt new └── lambdas_preload_jit_tracing.phpt new
Problem: #170's rows only tripped the debug build's JIT assertion. On a release build all three passed before the fix, but the scope the preload request wrote still broke code that never reached the assertion: - The optimizer read the written scope with the JIT off. Pass 1 alone baked in the first class's self:: constant. Passes 6 and 7 together ran integer addition on a float property. Passes 4 and 16 together called the first class's private method. So a preloaded closure rebound to another class, through bind, bindTo or call, gave wrong results. - The tracing JIT hung in ir_gcm_schedule_late compiling a root trace. - No suite mode runs the hot-counter JIT, opcache.jit=1235. - An interface default body with a lambda runs as a trait method, so before the fix its lambda's op array took the scope of the first class that ran it. container_preload.phpt started its child with -n and its own opcache settings, so no mode's JIT or optimizer setting reached the preload request it tests. On a build with no JIT, such as the Apple Silicon ZTS build, every *_jit_* row failed with Undefined array key "jit" instead of skipping. Solution: - closure_binding_preload.phpt: plain PHP closures the preload request made bind, bindTo, call, a static closure, an arrow function, first-class callables, fromCallable and a trait's closure that two classes use. Before the fix it fails on the release and debug builds with no JIT. - A seventh PHP# shape: Labeled, an interface whose default label() holds a lambda, implemented by Shapes and StoredStep. - lambdas_preload_jit_hot_counters.phpt runs the shapes under opcache.jit=1235. - container_preload.phpt passes TEST_PHP_EXTRA_ARGS to its child, as environment.phpt does, so its preload request runs in each mode. - Every *_jit_* row skips when ini_get('opcache.jit') is false. The SKIPIF section runs without the test's INI, so opcache is off there and opcache_get_status() returns false on every build. Zend/tests/sharp/ ├── PreloadedLambdas/Labeled.sharp new: a default body's lambda ├── PreloadedLambdas/Shapes.sharp implements Labeled ├── PreloadedLambdas/StoredStep.sharp implements Labeled ├── lambdas_preload.inc runs label() on both ├── lambdas_preload*.phpt the seventh shape ├── lambdas_preload_jit_hot_counters.phpt new ├── closure_binding_preload.inc new ├── closure_binding_preload.phpt new ├── container_preload.phpt the mode's settings └── *_jit_*.phpt skip with no JIT
heyJordanParker
added a commit
that referenced
this pull request
Oct 18, 2026
…d beside the permanent refusals Problem #159, #165, #168, #170 and #171 landed on master. #159 gives attributes to interfaces, events, constants, accessors and lambdas, #168 gives every pattern type a test, and the collections work builds tuple and `Iterable` constants. #166's refusals of those forms as "not supported yet" are now refusals of code that compiles. `==` on an `Iterable` was still "not supported yet", and a `.sharp` file the engine refused stayed in PHP's included files, so a second `require_once` skipped it and a later use said "Class not found". Solution The merge keeps master's attribute arms, its `as` arm and its attribute-argument refusal, keeps `report_accessor` with #159's observer-attribute arm and its `ACCESSORS` note, and drops the arms master made unreachable: `is_undecided`'s attribute lists, pattern types with no test and tuple constants. `==` on an `Iterable`, or on a type parameter bound by a collection, is a permanent error that names `toList()`: "`==` compares a `List`, a `Map` or a `Set`, and an `Iterable` is none of them: compare `a.toList() == b.toList()`." A search and a struct property say the same. A `.sharp` file the engine refuses leaves PHP's included files, so a second `require_once` reports the same refusal. Spec section 20's `case-set-field` states its result, `// true`, which the spec tool now runs. php-sharp/ ├── Zend/tests/sharp/refusal_required_again.phpt a refused file is required again ├── docs/spec.md case-set-field gives true ├── ext/sharp/sharp.c sharp_compile_file drops a refused file └── sharp/mago/crates/ ├── analyzer/ │ ├── src/expression/binary/comparison.rs `==` on an Iterable names toList() │ └── tests/sharp.rs its rows, and a row that compiles toList() ├── hir/src/lower/error/mod.rs both sides' error list └── semantics/ ├── src/internal/checker/sharp.rs is_undecided, report_accessor, the as arm └── tests/sharp.rs rows for what master builds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
opcache.preloadandopcache.jit=function, a PHP# lambda that the preload script runs stops a debug build onzend_jit.c's assertion!(op_array->fn_flags & ZEND_ACC_CLOSURE) || !(op_array->scope). A release build skips the assertion, and the same written scope breaks it in three more places:self::constant. Passes 6 and 7 together run integer addition on a float property. Passes 4 and 16 together call the first class's private method. A preloaded closure rebound to another class then gives wrong results.ir_gcm_schedule_latewhile it compiles a root trace.Root cause
The first time a closure is made,
zend_create_closure_ex()inZend/zend_closures.cwrites the class scope onto the closure's declaring op array, so later closures can share one runtime cache. It does this only to an op array it believes this request owns, and it checks that withZEND_ACC_IMMUTABLE. The preload script runs before opcache makes its op arrays immutable. Opcache then stores the op array, scope included, in shared memory, and the JIT compiles the closure as if that scope never changes.The bridge's lowering is not the cause. A PHP# lambda compiles like a plain PHP closure, with no scope, and a preloaded PHP# class whose lambdas the preload script never runs passes. A plain PHP closure inside a class method, run during preloading, trips the same assertion with no PHP# code at all. Every PHP# lambda is inside a class, so every one that the preload script runs trips it. A plain PHP closure outside any class keeps a null scope, so it passes.
Fix
zend_create_closure_ex()also treats aZEND_ACC_PRELOADEDop array as shared. The preload request then gives such a closure its own runtime cache and leaves the op array's scope alone. After preloading, every preloaded op array is immutable as well, so requests behave as before.Why the suite missed it
sharp/bin/test --opcache --repeat --passesalready has a function JIT mode, and that mode runs every preload test. No mode is missing. The missing case is a lambda that the preload request itself makes. Every*_preload.inconly requires files, or calls PHP# code that makes no lambda.Tests
lambdas_preload.incmakes seven lambda shapes during the preload request:thiscall_user_funcShapesandStoredStepboth implementEach row then runs every shape twice. Each was seen failing on the base build:
lambdas_preload.phptsets no JIT, so it runs in all 22 modes. Before the fix it failed with the assertion in the--passesfunction JIT mode.lambdas_preload_jit_function.phptsetsopcache.jit=function, so a plainsharp/bin/testcatches this too. Before the fix it failed with the assertion.lambdas_preload_jit_tracing.phpt: before the fix it hung until run-tests' timeout on the release and the debug build.lambdas_preload_jit_hot_counters.phptsetsopcache.jit=1235, which no suite mode runs. Before the fix it failed with the assertion.closure_binding_preload.phptbinds, rebinds and calls plain PHP closures the preload request made:bind,bindTo,call, a static closure, an arrow function, first-class callables,fromCallableand a trait's closure that two classes use. Before the fix it failed on the release and the debug build with no JIT.Suite fixes
container_preload.phptpassesTEST_PHP_EXTRA_ARGSto its child, asenvironment.phptdoes, so the preload request it tests runs under each mode's JIT and optimizer settings.*_jit_*row skips whenini_get('opcache.jit')is false, as on the Apple Silicon ZTS build, instead of failing withUndefined array key "jit". A SKIPIF section runs without the test's INI, so opcache is off there andopcache_get_status()returns false on every build.