Repository navigation
Fix GH-17990: Tracing JIT uses the SSA of a dead process #25399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kamil-tekiela
wants to merge
1
commit into
php:PHP-8.4
Choose a base branch
from
kamil-tekiela:fix-gh17990-trace-jit-stale-ssa
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1295,6 +1295,21 @@ typedef struct _zend_tssa { | |
|
|
||
| static const zend_op _nop_opcode = {0}; | ||
|
|
||
| /* The func_info of an op_array lives in shared memory, but its SSA is allocated | ||
| * in the arena of the process that compiles a trace, and zend_jit_trace() resets | ||
| * it when the compilation ends. A process that dies in the middle of a | ||
| * compilation leaves its SSA behind, so a non-zero ssa->cfg.blocks_count does | ||
| * not prove that the SSA was built by the current compilation. */ | ||
| static bool zend_jit_trace_has_ssa(const zend_jit_op_array_trace_extension *jit_extension, const zend_op_array **op_arrays, int num_op_arrays) | ||
| { | ||
| for (int i = 0; i < num_op_arrays; i++) { | ||
| if ((const void*)ZEND_FUNC_INFO(op_arrays[i]) == (const void*)jit_extension) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| static zend_ssa *zend_jit_trace_build_tssa(zend_jit_trace_rec *trace_buffer, uint32_t parent_trace, uint32_t exit_num, zend_script *script, const zend_op_array **op_arrays, int *num_op_arrays_ptr) | ||
| { | ||
| zend_ssa *tssa; | ||
|
|
@@ -1398,7 +1413,7 @@ static zend_ssa *zend_jit_trace_build_tssa(zend_jit_trace_rec *trace_buffer, uin | |
| jit_extension = | ||
| (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array); | ||
| ssa = &jit_extension->func_info.ssa; | ||
| if (ssa->cfg.blocks_count) { | ||
| if (zend_jit_trace_has_ssa(jit_extension, op_arrays, num_op_arrays)) { | ||
| /* pass */ | ||
| } else if (num_op_arrays == ZEND_JIT_TRACE_MAX_FUNCS) { | ||
| /* Too many functions in single trace */ | ||
|
|
@@ -1416,7 +1431,7 @@ static zend_ssa *zend_jit_trace_build_tssa(zend_jit_trace_rec *trace_buffer, uin | |
| jit_extension = | ||
| (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array); | ||
| ssa = &jit_extension->func_info.ssa; | ||
| if (ssa->cfg.blocks_count) { | ||
| if (zend_jit_trace_has_ssa(jit_extension, op_arrays, num_op_arrays)) { | ||
| /* pass */ | ||
| } else if (num_op_arrays == ZEND_JIT_TRACE_MAX_FUNCS) { | ||
| /* Too many functions in single trace */ | ||
|
|
@@ -7450,6 +7465,8 @@ static zend_jit_trace_stop zend_jit_compile_root_trace(zend_jit_trace_rec *trace | |
| zend_jit_trace_exit_info exit_info[ZEND_JIT_TRACE_MAX_EXITS]; | ||
| bool do_bailout = 0; | ||
|
|
||
| /* Don't let a signal kill the process while func_info in SHM refers to our arena */ | ||
| HANDLE_BLOCK_INTERRUPTIONS(); | ||
| zend_shared_alloc_lock(); | ||
|
|
||
| /* Checks under lock */ | ||
|
|
@@ -7590,6 +7607,7 @@ exit:; | |
| } | ||
|
|
||
| zend_shared_alloc_unlock(); | ||
| HANDLE_UNBLOCK_INTERRUPTIONS(); | ||
|
|
||
| if (do_bailout) { | ||
| zend_bailout(); | ||
|
|
@@ -8288,6 +8306,8 @@ static zend_jit_trace_stop zend_jit_compile_side_trace(zend_jit_trace_rec *trace | |
| zend_jit_trace_exit_info exit_info[ZEND_JIT_TRACE_MAX_EXITS]; | ||
| bool do_bailout = 0; | ||
|
|
||
| /* Don't let a signal kill the process while func_info in SHM refers to our arena */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
| HANDLE_BLOCK_INTERRUPTIONS(); | ||
| zend_shared_alloc_lock(); | ||
|
|
||
| /* Checks under lock */ | ||
|
|
@@ -8430,6 +8450,7 @@ exit:; | |
| } | ||
|
|
||
| zend_shared_alloc_unlock(); | ||
| HANDLE_UNBLOCK_INTERRUPTIONS(); | ||
|
|
||
| if (do_bailout) { | ||
| zend_bailout(); | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| --TEST-- | ||
| GH-17990 (Tracing JIT uses the SSA of a process that died during trace compilation) | ||
| --EXTENSIONS-- | ||
| opcache | ||
| pcntl | ||
| posix | ||
| --INI-- | ||
| opcache.enable=1 | ||
| opcache.enable_cli=1 | ||
| opcache.file_update_protection=0 | ||
| opcache.jit=tracing | ||
| opcache.jit_buffer_size=64M | ||
| opcache.jit_hot_func=1 | ||
| --FILE-- | ||
| <?php | ||
| function foo($x) { | ||
| return $x + 42; | ||
| } | ||
|
|
||
| function test() { | ||
| return foo(3); | ||
| } | ||
|
|
||
| /* Kill a child at different moments, so that one of them dies while it compiles | ||
| * the trace of test(). The shutdown function starts that trace outside of any | ||
| * other trace. */ | ||
| for ($delay = 0; $delay <= 2000; $delay += 50) { | ||
| $pid = pcntl_fork(); | ||
| if ($pid === -1) { | ||
| die("fork failed\n"); | ||
| } | ||
| if ($pid === 0) { | ||
| register_shutdown_function('test'); | ||
| exit(0); | ||
| } | ||
| usleep($delay); | ||
| posix_kill($pid, SIGKILL); | ||
| pcntl_waitpid($pid, $status); | ||
| } | ||
|
|
||
| register_shutdown_function(function () { | ||
| var_dump(test()); | ||
| }); | ||
| ?> | ||
| --EXPECT-- | ||
| int(45) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only defers the signals
zend_signalhandles (SIGTERM, SIGINT, SIGHUP...). SIGKILL, a segfault or the OOM killer still end the process here, and that is the case your test covers. Maybe/* Defer signals while func_info in SHM refers to our arena */?