Repository navigation
fix: a preload script that reads a module, calls a checked method or emits leaves the request working - #146
Merged
Merged
Conversation
…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
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
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.
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.
\Sharp\Internal\readModule, as the library's autoloader does, aborts the preload request at shutdown withgc_possible_root: Assertion GC_TYPE(ref) == IS_ARRAY || GC_TYPE(ref) == IS_OBJECT failed.Sharp\Container::getdoes when it builds a service, aborts withsharp_type_list_of_frame: Assertion ZSTR_IS_INTERNED(Z_STR_P(text)) failed.Sharp\Internal\emitaborts the preload request at shutdown with the samegc_possible_rootassertion.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_readcompiles a module file withzend_compile_file, runs it, and keeps the rows it returns incontainer_bindings.sharp_read_listener_tablekeeps the array its file returns as a constant inlistener_tables. Both take a reference to the file's literal arrays. When opcache persists the preloaded scripts,zend_persist_zvalcopies each literal array to shared memory withzend_shared_memdup_put_free, whichefrees the original whatever its refcount. The preload request's shutdown then destroyscontainer_bindingsandlistener_tablesand reads the freed arrays.efreeinaccel_preload's persist step,ZendAccelerator.cL4829. It runs afterzend_shutdown_executor_valuesand before the request's module shutdown. The read waszm_deactivate_sharpdestroyingcontainer_bindings.zend_hash_destroy(&SHARP_G(listener_tables))inzm_deactivate_sharp.Checked call: the type text is not interned yet. The compiler interns literals through
zend_new_interned_string. With opcache on, that isaccel_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_frameasserted 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?ofApp\Shop\Checkout::__construct, with refcount 1 and no interned flag.Solution
sharp_copy_literalcopies 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_readandsharp_read_listener_tablekeep that copy in place of the literal.sharp_type_list_of_framekeeps the request's spelled lists under the type text's own string, in place of a key holding its address.zend_hash_findmatches 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_acceptsandsharp_function_carriedno longer mark the text they pass as interned.Sharp_Internal_acceptsno longer interns a text to give it a stable address, which under opcache returned the string unchanged.type_arguments_cache.inctells 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
Zend/tests/sharp/container_preload.phptpreloads 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_rootassertion, then, with both fixed, the listener table's.USE_ZEND_ALLOC=0 MallocScribble=1, and all 100 printed the expected output.sharp/bin/test --opcache --repeat Zend/tests/sharppasses 294 of 294 in each of its 4 modes on macOS.sharp/bin/test --passespassescontainer_preload.phpt,class_header_preload.phptandtype_arguments_requests.phptin each of its 19 modes.USE_ZEND_ALLOC=0over all ofZend/tests/sharp,type_arguments_cache.phptandtype_arguments_out_of_memory.phptfail the same way on master d31140d, because they measure the engine's own allocator.type_arguments_requests.phptfails there now and then too, because it measures the system allocator's peak. Run alone, its fournewrequests 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\acceptscall 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-alland no--enable-debug, withopcache.enable_cli=1. The writes setChecks\Tally::$amounts, aList<float>, from plain PHP. The calls checkList<float>.acceptscalls take 140.9 to 144.7 ms and this branch's 142.7 to 145.5 ms./usr/bin/time -l, which a busy machine does not move:acceptscalls: master 2670.9M and 2680.8M, this branch 2683.2M and 2685.7M, inside master's own spread.do_alloca, still copied and hashed the text: 1189.1M instructions for the writes and 2817.2M for theacceptscalls, about 150 more than master per lookup. So the lists are now found under the text's string, whose hash it keeps.