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
28 changes: 23 additions & 5 deletions ext/opcache/jit/zend_jit_ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ static int zend_jit_assign_to_variable(zend_jit_ctx *jit,
zend_jit_addr ref_addr,
bool check_exception);

static void zend_jit_preserve_parent_regs(zend_jit_ctx *jit,
zend_ssa *ssa,
zend_jit_trace_info *parent,
uint32_t exit_num);

typedef struct _zend_jit_stub {
const char *name;
int (*stub)(zend_jit_ctx *jit);
Expand Down Expand Up @@ -16990,6 +16995,7 @@ static int zend_jit_trace_handler(zend_jit_ctx *jit, const zend_op_array *op_arr
static int zend_jit_deoptimizer_start(zend_jit_ctx *jit,
zend_string *name,
uint32_t trace_num,
zend_jit_trace_info *parent,
uint32_t exit_num)
{
zend_jit_init_ctx(jit, (zend_jit_vm_kind == ZEND_VM_KIND_CALL) ? 0 : IR_START_BR_TARGET);
Expand All @@ -17002,6 +17008,8 @@ static int zend_jit_deoptimizer_start(zend_jit_ctx *jit,

jit->ctx.flags |= IR_SKIP_PROLOGUE;

zend_jit_preserve_parent_regs(jit, NULL, parent, exit_num);

return 1;
}

Expand Down Expand Up @@ -17034,14 +17042,28 @@ static int zend_jit_trace_start(zend_jit_ctx *jit,
jit->ctx.flags |= IR_SKIP_PROLOGUE;
}

zend_jit_preserve_parent_regs(jit, ssa, parent, exit_num);

ir_STORE(jit_EG(jit_trace_num), ir_CONST_U32(trace_num));

return 1;
}

static void zend_jit_preserve_parent_regs(zend_jit_ctx *jit,
zend_ssa *ssa,
zend_jit_trace_info *parent,
uint32_t exit_num)
{
/* Emit early RLOADs of registers used for deoptimization to prevent
* clobbering. zend_jit_deopt_rload() will reference these. */

if (parent) {
int i;
int parent_vars_count = parent->exit_info[exit_num].stack_size;
zend_jit_trace_stack *parent_stack = parent_vars_count == 0 ? NULL :
parent->stack_map +
parent->exit_info[exit_num].stack_offset;

/* prevent clobbering of registers used for deoptimization */
for (i = 0; i < parent_vars_count; i++) {
if (STACK_FLAGS(parent_stack, i) != ZREG_CONST
&& STACK_REG(parent_stack, i) != ZREG_NONE) {
Expand Down Expand Up @@ -17085,10 +17107,6 @@ static int zend_jit_trace_start(zend_jit_ctx *jit,
ir_RLOAD_A(parent->exit_info[exit_num].poly_this.reg);
}
}

ir_STORE(jit_EG(jit_trace_num), ir_CONST_U32(trace_num));

return 1;
}

static int zend_jit_trace_begin_loop(zend_jit_ctx *jit)
Expand Down
2 changes: 1 addition & 1 deletion ext/opcache/jit/zend_jit_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -7364,7 +7364,7 @@ static const void *zend_jit_trace_exit_to_vm(uint32_t trace_num, uint32_t exit_n

name = zend_jit_trace_escape_name(trace_num, exit_num);

if (!zend_jit_deoptimizer_start(&ctx, name, trace_num, exit_num)) {
if (!zend_jit_deoptimizer_start(&ctx, name, trace_num, &zend_jit_traces[trace_num], exit_num)) {
zend_string_release(name);
return NULL;
}
Expand Down
78 changes: 78 additions & 0 deletions ext/opcache/tests/jit/gh22915.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--TEST--
GH-22915: compiled exit clobbers registers before saving
--ENV--
F=iter
--FILE--
<?php

final class It implements Iterator {
public readonly array $values;
public int $position = 0;
public function __construct(array $values) {
$this->values = $values;
}

public function rewind(): void {}

public function valid(): bool {
return $this->position === 0;
}

public function current(): mixed {
if (!isset($this->values[$this->position])) {
throw new Exception();
}

return $this->values[$this->position];
}

public function key(): mixed {
return $this->position;
}

public function next(): void {
$this->position++;
}
}

function iter(It $it) {
foreach ($it as $value) {
var_dump($value);
if (!$value instanceof stdClass) {
continue;
}
}
}

echo "# First run\n";
for ($i = 0; $i < 5; $i++) {
getenv('F')(new It([getenv('F')])); // non-immutable, packed array
}

// Next side-exit should deoptimize
if (ini_set('opcache.jit_max_side_traces', '0') === false) {
die("Failed setting opcache.jit_max_side_traces");
}
if (ini_set('opcache.jit_blacklist_side_trace', '0') === false) {
die("Failed setting opcache.jit_blacklist_side_trace");
}

echo "# Second run\n";
for ($i = 0; $i < 5; $i++) {
getenv('F')(new It([getenv('F'), 'map' => true])); // non-immutable, map, triggers exit
}

?>
--EXPECT--
# First run
string(4) "iter"
string(4) "iter"
string(4) "iter"
string(4) "iter"
string(4) "iter"
# Second run
string(4) "iter"
string(4) "iter"
string(4) "iter"
string(4) "iter"
string(4) "iter"
2 changes: 1 addition & 1 deletion ext/opcache/zend_accelerator_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ ZEND_INI_BEGIN()
STD_PHP_INI_ENTRY("opcache.jit_bisect_limit" , "0", PHP_INI_ALL, OnUpdateLong, bisect_limit, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_prof_threshold" , "0.005", PHP_INI_ALL, OnUpdateReal, prof_threshold, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_root_traces" , "1024", PHP_INI_SYSTEM, OnUpdateLong, max_root_traces, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_side_traces" , "128", PHP_INI_SYSTEM, OnUpdateLong, max_side_traces, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_side_traces" , "128", PHP_INI_ALL, OnUpdateLong, max_side_traces, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_exit_counters" , "8192", PHP_INI_SYSTEM, OnUpdateLong, max_exit_counters, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_hot_loop" , "64", PHP_INI_SYSTEM, OnUpdateCounter, hot_loop, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_hot_func" , "127", PHP_INI_SYSTEM, OnUpdateCounter, hot_func, zend_jit_globals, jit_globals)
Expand Down
Loading