Skip to content
Draft
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
68 changes: 68 additions & 0 deletions ext/opcache/tests/jit/compile_interrupted.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

function compile_interrupted_load(string $file): void
{
$code = <<<'PHP'
<?php
function helper(int $n): int {
return $n + 1;
}
function big(int $mode, int $n): int {
if ($mode === 1) {
while (true);
}
if ($mode === 2) {
for ($i = 0; ; $i++) {
if ($i === 3) {
ini_set('opcache.jit_debug', 2);
}
}
}
if ($n > 1) {
PHP;
$code .= str_repeat(" \$n = (\$n ^ 7) + 1;\n", 2000);
$code .= <<<'PHP'
}
return helper($n);
}
function caller(int $n): int {
return big(0, $n) + helper($n);
}
PHP;
file_put_contents($file, $code);
require $file;
}

/* Sends SIGTERM to a forked child while it is blocked writing the JIT SSA dump in the middle of a compile. */
function compile_interrupted(callable $child, callable $parent): void
{
if (($_SERVER['argv'][1] ?? '') === 'inner') {
$pid = pcntl_fork();
if ($pid === 0) {
$child();
exit(0);
}
echo $pid, "\n";
pcntl_waitpid($pid, $status);
var_dump(pcntl_wtermsig($status) === SIGTERM);
var_dump($parent());
return;
}

/* fclose(STDERR) would close libc's stderr too, so the script reruns with stderr on a pipe the child inherits. */
$cmd = escapeshellarg(PHP_BINARY) . ' ' . getenv('TEST_PHP_EXTRA_ARGS') . ' '
. escapeshellarg($_SERVER['SCRIPT_FILENAME']) . ' inner';
$proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
$pid = (int) fgets($pipes[1]);
var_dump(fread($pipes[2], 1) !== '');
if ($pid > 0) {
posix_kill($pid, SIGTERM);
}
/* The child may defer SIGTERM until the compile ends, so let it finish writing. */
stream_get_contents($pipes[2]);
echo stream_get_contents($pipes[1]);
$status = proc_close($proc);
if ($status !== 0) {
echo "Inner process exited with status $status\n";
}
}
38 changes: 38 additions & 0 deletions ext/opcache/tests/jit/compile_interrupted_first_exec.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
JIT: func_info left by a killed first-exec compile is not reused
--EXTENSIONS--
opcache
pcntl
posix
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.file_update_protection=0
opcache.jit=1215
opcache.jit_buffer_size=16M
--SKIPIF--
<?php
if (!(opcache_get_status()['jit']['on'] ?? false)) die('skip JIT is not available');
?>
--FILE--
<?php
require __DIR__ . '/compile_interrupted.inc';
compile_interrupted_load(__DIR__ . '/compile_interrupted_first_exec.gen.inc');
compile_interrupted(
function () {
ini_set('opcache.jit_debug', 2);
big(0, 1);
},
function () {
return caller(1);
},
);
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/compile_interrupted_first_exec.gen.inc');
?>
--EXPECT--
bool(true)
bool(true)
int(4)
39 changes: 39 additions & 0 deletions ext/opcache/tests/jit/compile_interrupted_hot_counters.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
JIT: func_info left by a killed hot counter compile is not reused
--EXTENSIONS--
opcache
pcntl
posix
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.file_update_protection=0
opcache.jit=1235
opcache.jit_buffer_size=16M
opcache.jit_hot_func=1
--SKIPIF--
<?php
if (!(opcache_get_status()['jit']['on'] ?? false)) die('skip JIT is not available');
?>
--FILE--
<?php
require __DIR__ . '/compile_interrupted.inc';
compile_interrupted_load(__DIR__ . '/compile_interrupted_hot_counters.gen.inc');
compile_interrupted(
function () {
ini_set('opcache.jit_debug', 2);
big(0, 1);
},
function () {
return caller(1);
},
);
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/compile_interrupted_hot_counters.gen.inc');
?>
--EXPECT--
bool(true)
bool(true)
int(4)
44 changes: 44 additions & 0 deletions ext/opcache/tests/jit/compile_interrupted_prof_request.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
JIT: func_info left by a killed profiling compile is not reused
--EXTENSIONS--
opcache
pcntl
posix
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.file_update_protection=0
opcache.jit=1225
opcache.jit_buffer_size=16M
--SKIPIF--
<?php
if (!(opcache_get_status()['jit']['on'] ?? false)) die('skip JIT is not available');
?>
--FILE--
<?php
require __DIR__ . '/compile_interrupted.inc';
compile_interrupted_load(__DIR__ . '/compile_interrupted_prof_request.gen.inc');
compile_interrupted(
function () {
for ($i = 0; $i < 10; $i++) {
big(0, 1);
}
ini_set('opcache.jit_debug', 2);
},
function () {
$sum = 0;
for ($i = 0; $i < 10; $i++) {
$sum += caller(1);
}
return $sum;
},
);
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/compile_interrupted_prof_request.gen.inc');
?>
--EXPECT--
bool(true)
bool(true)
int(40)
46 changes: 46 additions & 0 deletions ext/opcache/tests/jit/compile_interrupted_trace_root.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
JIT: func_info left by a killed root trace compile is not reused
--EXTENSIONS--
opcache
pcntl
posix
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.file_update_protection=0
opcache.jit=tracing
opcache.jit_buffer_size=16M
opcache.jit_hot_loop=1
opcache.jit_hot_func=1
opcache.jit_hot_return=1
opcache.jit_hot_side_exit=1
--SKIPIF--
<?php
if (!(opcache_get_status()['jit']['on'] ?? false)) die('skip JIT is not available');
?>
--FILE--
<?php
require __DIR__ . '/compile_interrupted.inc';
compile_interrupted_load(__DIR__ . '/compile_interrupted_trace_root.gen.inc');
compile_interrupted(
function () {
ini_set('opcache.jit_debug', 2);
big(1, 0);
},
function () {
$sum = 0;
for ($i = 0; $i < 3; $i++) {
$sum += big(0, 1);
}
return $sum;
},
);
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/compile_interrupted_trace_root.gen.inc');
?>
--EXPECT--
bool(true)
bool(true)
int(6)
45 changes: 45 additions & 0 deletions ext/opcache/tests/jit/compile_interrupted_trace_side.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
JIT: func_info left by a killed side trace compile is not reused
--EXTENSIONS--
opcache
pcntl
posix
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.file_update_protection=0
opcache.jit=tracing
opcache.jit_buffer_size=16M
opcache.jit_hot_loop=1
opcache.jit_hot_func=1
opcache.jit_hot_return=1
opcache.jit_hot_side_exit=1
--SKIPIF--
<?php
if (!(opcache_get_status()['jit']['on'] ?? false)) die('skip JIT is not available');
?>
--FILE--
<?php
require __DIR__ . '/compile_interrupted.inc';
compile_interrupted_load(__DIR__ . '/compile_interrupted_trace_side.gen.inc');
compile_interrupted(
function () {
big(2, 0);
},
function () {
$sum = 0;
for ($i = 0; $i < 3; $i++) {
$sum += big(0, 1);
}
return $sum;
},
);
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/compile_interrupted_trace_side.gen.inc');
?>
--EXPECT--
bool(true)
bool(true)
int(6)
Loading