Skip to content

fix: a lambda the preload script runs keeps working under the JIT - #170

Merged
heyJordanParker merged 4 commits into
masterfrom
fix/jit-preload-lambda
Oct 18, 2026
Merged

heyJordanParker merged 4 commits into
masterfrom
fix/jit-preload-lambda

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

With opcache.preload and opcache.jit=function, a PHP# lambda that the preload script runs stops a debug build on zend_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:

  • The optimizer reads it with the JIT off. Pass 1 alone bakes in the first class's 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.
  • The tracing JIT hangs in ir_gcm_schedule_late while it compiles a root trace.
  • The function JIT and the hot-counter JIT crash on a preloaded closure rebound to another object.

Root cause

The first time a closure is made, zend_create_closure_ex() in Zend/zend_closures.c writes 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 with ZEND_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 a ZEND_ACC_PRELOADED op 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.

 			if (!ptr
 			 && (func->common.fn_flags & ZEND_ACC_CLOSURE)
 			 && (func->common.scope == scope ||
-			     !(func->common.fn_flags & ZEND_ACC_IMMUTABLE))) {
+			     !(func->common.fn_flags & (ZEND_ACC_IMMUTABLE|ZEND_ACC_PRELOADED)))) {

Why the suite missed it

sharp/bin/test --opcache --repeat --passes already 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.inc only requires files, or calls PHP# code that makes no lambda.

Tests

lambdas_preload.inc makes seven lambda shapes 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 plain PHP's call_user_func
  • one in a field
  • one in an interface's default body, which Shapes and StoredStep both implement

Each row then runs every shape twice. Each was seen failing on the base build:

  • lambdas_preload.phpt sets no JIT, so it runs in all 22 modes. Before the fix it failed with the assertion in the --passes function JIT mode.
  • lambdas_preload_jit_function.phpt sets opcache.jit=function, so a plain sharp/bin/test catches 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.phpt sets opcache.jit=1235, which no suite mode runs. Before the fix it failed with the assertion.
  • closure_binding_preload.phpt binds, rebinds and calls 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 failed on the release and the debug build with no JIT.

Suite fixes

  • container_preload.phpt passes TEST_PHP_EXTRA_ARGS to its child, as environment.phpt does, so the preload request it tests runs under each mode's JIT and optimizer settings.
  • Every *_jit_* row skips when ini_get('opcache.jit') is false, as on the Apple Silicon ZTS build, instead of failing with Undefined array key "jit". A SKIPIF section runs without the test's INI, so opcache is off there and opcache_get_status() returns false on every build.

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
heyJordanParker merged commit 6e38e24 into master Oct 18, 2026
23 checks passed
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
@heyJordanParker
heyJordanParker deleted the fix/jit-preload-lambda branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant