Skip to content

fix: a preload script that reads a module, calls a checked method or emits leaves the request working - #146

Merged
heyJordanParker merged 3 commits into
masterfrom
fix/preload-module
Oct 16, 2026
Merged

heyJordanParker merged 3 commits into
masterfrom
fix/preload-module

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Fixes: three crashes of an app whose opcache preload script reads its modules, calls a PHP# method that checks its arguments, or emits an event.

  • The container's bindings. A preload script that calls \Sharp\Internal\readModule, as the library's autoloader does, aborts the preload request at shutdown with gc_possible_root: Assertion GC_TYPE(ref) == IS_ARRAY || GC_TYPE(ref) == IS_OBJECT failed.
  • A checked call from plain PHP. A preload script that calls a PHP# method whose parameters plain PHP's calls are checked against, as Sharp\Container::get does when it builds a service, aborts with sharp_type_list_of_frame: Assertion ZSTR_IS_INTERNED(Z_STR_P(text)) failed.
  • The listener table. A preload script that calls Sharp\Internal\emit aborts the preload request at shutdown with the same gc_possible_root assertion.

Problem

Opcache runs the preload script first, and only then persists the scripts it compiled into shared memory. The three crashes come from that order.

Bindings and listener table: opcache frees the literal arrays they keep. sharp_container_read compiles a module file with zend_compile_file, runs it, and keeps the rows it returns in container_bindings. sharp_read_listener_table keeps the array its file returns as a constant in listener_tables. Both take a reference to the file's literal arrays. When opcache persists the preloaded scripts, zend_persist_zval copies each literal array to shared memory with zend_shared_memdup_put_free, which efrees the original whatever its refcount. The preload request's shutdown then destroys container_bindings and listener_tables and reads the freed arrays.

  • A write watchpoint on the row array caught the efree in accel_preload's persist step, ZendAccelerator.c L4829. It runs after zend_shutdown_executor_values and before the request's module shutdown. The read was zm_deactivate_sharp destroying container_bindings.
  • The same crash happens with a plain PHP module file that returns literal rows. A row built at run time, such as one holding a closure, is not a literal and never crashed.
  • With the bindings fixed, the backtrace of the emit crash was zend_hash_destroy(&SHARP_G(listener_tables)) in zm_deactivate_sharp.

Checked call: the type text is not interned yet. The compiler interns literals through zend_new_interned_string. With opcache on, that is accel_new_interned_string_for_php, which returns a new string unchanged until opcache persists the script. So a preload script runs its op arrays with type texts that are plain refcounted strings. sharp_type_list_of_frame asserted the text was interned, because the request's table of spelled lists keyed the text by its address. lldb showed the text at the assertion, Any?, List<App.Pricing.PriceRule>, Any?, Any?, Any? of App\Shop\Checkout::__construct, with refcount 1 and no interned flag.

Solution

  • sharp_copy_literal copies a compiled file's literal for the request to keep. It copies each mutable array, nested ones too, and shares everything else. An immutable array, which opcache serves from shared memory on every request after the first, is shared as before. A string is shared too, since opcache's persist step only releases a string. sharp_container_read and sharp_read_listener_table keep that copy in place of the literal.
  • sharp_type_list_of_frame keeps the request's spelled lists under the type text's own string, in place of a key holding its address. zend_hash_find matches the string by address first and by contents when two strings hold one text, with the hash the string keeps. So a text opcache has not interned yet spells the same list as one it has. Each text's lists are an array keyed by the rest of the key, sharp_type_spelling, as before. The table holds a reference to the text, so its address is never freed and reused while the request runs.
  • Sharp_Internal_accepts, sharp_property_accepts and sharp_function_carried no longer mark the text they pass as interned. Sharp_Internal_accepts no longer interns a text to give it a stable address, which under opcache returned the string unchanged.
  • type_arguments_cache.inc tells a spelling from a cached list by the peak above the memory before the call. Its old measure, the peak minus the memory after the call, counted the table the first spelling keeps, which now holds one more array.

Verification

  • New test: Zend/tests/sharp/container_preload.phpt preloads a script that reads a binding and an expression over its parameters, calls a checked PHP# method from plain PHP, and emits an event. A request then does the same. Each row failed before its fix, in order: the checked-call assertion first, then, with that fixed, the bindings' gc_possible_root assertion, then, with both fixed, the listener table's.
  • Scribbled memory: the test's preload and request ran 100 times with USE_ZEND_ALLOC=0 MallocScribble=1, and all 100 printed the expected output.
  • Engine tests: sharp/bin/test --opcache --repeat Zend/tests/sharp passes 294 of 294 in each of its 4 modes on macOS.
  • Optimizer passes and JITs: sharp/bin/test --passes passes container_preload.phpt, class_header_preload.phpt and type_arguments_requests.phpt in each of its 19 modes.
  • With USE_ZEND_ALLOC=0 over all of Zend/tests/sharp, type_arguments_cache.phpt and type_arguments_out_of_memory.phpt fail the same way on master d31140d, because they measure the engine's own allocator. type_arguments_requests.phpt fails there now and then too, because it measures the system allocator's peak. Run alone, its four new requests grew master's peak by 720896 bytes and this branch's by 589824, over its 512 KiB limit. With the engine's allocator, both grow by 32768.

Performance

A checked property write and a Sharp\Internal\accepts call each look up the spelled lists, because each starts with an empty site cache. One million of each ran on release builds, configured with --disable-all and no --enable-debug, with opcache.enable_cli=1. The writes set Checks\Tally::$amounts, a List<float>, from plain PHP. The calls check List<float>.

  • Wall time, 4 alternating runs each: master's writes take 46.4 to 49.3 ms and this branch's 46.9 to 48.8 ms. Master's accepts calls take 140.9 to 144.7 ms and this branch's 142.7 to 145.5 ms.
  • Instructions retired for the whole process, from /usr/bin/time -l, which a busy machine does not move:
    • Writes: master 1035.4M and 1038.2M, this branch 1057.4M and 1060.3M, about 22 instructions or 2% more per write.
    • accepts calls: master 2670.9M and 2680.8M, this branch 2683.2M and 2685.7M, inside master's own spread.
  • The first version of this branch, ac34048, allocated a key of the text's bytes on every lookup. A key in a stack buffer, built with do_alloca, still copied and hashed the text: 1189.1M instructions for the writes and 2817.2M for the accepts calls, about 150 more than master per lookup. So the lists are now found under the text's string, whose hash it keeps.

…emits leaves the request working

Opcache runs a preload script before it persists the scripts, and persisting frees
each mutable literal array whatever its refcount. It interns literal strings only
then too.

- The container and the listener table copy each mutable literal array they keep,
  through sharp_copy_literal. An immutable array opcache serves is still shared.
- sharp_type_list_of_frame keys the request's spelled lists by the type text's
  bytes, not its address, so a text opcache has not interned yet spells as one it
  has. The callers no longer mark a refcounted text as interned.

Zend/tests/sharp/container_preload.phpt preloads a binding read, a checked call
from plain PHP and an emit, then runs them in a request.
…it keeps

ac34048 built a key of the text's bytes on every lookup, so every checked
property write and every Sharp\Internal\accepts call allocated, copied and
hashed the text. A key in a stack buffer still hashed and copied it: about 150
instructions more than master per lookup.

The request's table now keeps each text's lists under the text's own string,
which zend_hash_find matches by address first and by contents when two strings
hold one text, with the hash the string keeps. Each text's lists are an array
keyed by a sharp_type_spelling, as master's table was. The table holds the
text, so its address is never reused this request.

type_arguments_cache.inc tells spelling apart by the peak above the memory
before the call, since the table the first spelling keeps no longer fits under
the 512 bytes the old measure left it.
@heyJordanParker
heyJordanParker merged commit 3bf73ce into master Oct 16, 2026
19 checks passed
heyJordanParker added a commit that referenced this pull request Oct 16, 2026
Master brings #142, #146, #147, #148 and #149. Each conflict keeps both
sides' behavior:

- A lambda's result type: master's `AnalysisArtifacts::lambda_result` is
  the one mechanism. `try` in a lambda reads it, and #143's
  `BlockContext::lambda_result_type` with its setters is gone.
- A `match`'s declared type reaching its PHP and its arms: master's code,
  with `Expression::Match` kept in `takes_declared_type`, without which
  the PHP `match` takes no declared type.
- `==` on a struct or a case: master's per-side type arguments for a
  generic struct, and a generic enum's case fields read the enum's type
  arguments from the side that gives them, through `with_type_arguments`.
- A call's declared type: master's `declared_return_type` goes through
  `as_declared_ancestor`, so `Result.Ok(…)` into a `Result<int, E>` fixes
  the case's type arguments for the arguments and for the result.
- The bridge: master's `generic_statics`, with `class_metadata` taking the
  class it describes, as a generic enum's case needs.
- Spec section 11 keeps master's inference bullets and #143's rule for a
  static call of a generic class.
heyJordanParker added a commit that referenced this pull request Oct 16, 2026
Problem

#145 was behind master, which gained #142, #143, #144, #146 to #150.
Both sides changed the default names: this branch generates them from
the standard library into `SHARP_CLASSES`, and #143 added `Result` to a
hand-written list and made `Error` a default name nothing shadows.

Solution

One mechanism: `default_classes()` stays `NAMED_EFFECTS` plus the
generated `SHARP_CLASSES`, regenerated from the merged library, which now
holds `Sharp\Error` and `Sharp\Result`. #143's `fixed_default_class`
stays, and `default_classes()` leaves out the class it fixes, so `Error`
is never shadowable while `Result` is.

sharp/mago/crates/names/src/
├── lib.rs                 default_classes and fixed_default_class
├── sharp_classes.rs       regenerated: Error and Result join
└── internal/walker.rs     both imports kept
docs/architecture/slices/23-names-imports.md   Error is the one fixed default name
@heyJordanParker
heyJordanParker deleted the fix/preload-module 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