Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions ext/opcache/jit/zend_jit_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */

Copy link
Copy Markdown
Member

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_signal handles (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 */?

HANDLE_BLOCK_INTERRUPTIONS();
zend_shared_alloc_lock();

/* Checks under lock */
Expand Down Expand Up @@ -7590,6 +7607,7 @@ exit:;
}

zend_shared_alloc_unlock();
HANDLE_UNBLOCK_INTERRUPTIONS();

if (do_bailout) {
zend_bailout();
Expand Down Expand Up @@ -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 */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 */
Expand Down Expand Up @@ -8430,6 +8450,7 @@ exit:;
}

zend_shared_alloc_unlock();
HANDLE_UNBLOCK_INTERRUPTIONS();

if (do_bailout) {
zend_bailout();
Expand Down
46 changes: 46 additions & 0 deletions ext/opcache/tests/jit/gh17990.phpt
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)
Loading