Skip to content

Commit ac8a53c

Browse files
committed
JIT: Fix register allocator
Fixes oss-fuzz #44916
1 parent 0d266a2 commit ac8a53c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

ext/opcache/jit/zend_jit_trace.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6389,6 +6389,22 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
63896389
if (p->stop == ZEND_JIT_TRACE_STOP_LOOP
63906390
|| p->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_CALL
63916391
|| p->stop == ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
6392+
if (ra) {
6393+
zend_ssa_phi *phi = ssa->blocks[1].phis;
6394+
6395+
while (phi) {
6396+
if (ra[phi->ssa_var]
6397+
&& ra[phi->sources[1]]
6398+
&& STACK_MEM_TYPE(stack, phi->var) != STACK_TYPE(stack, phi->var)
6399+
&& (ra[phi->ssa_var]->flags & (ZREG_LOAD|ZREG_STORE)) == 0
6400+
&& (ra[phi->sources[1]]->flags & (ZREG_LOAD|ZREG_STORE)) == 0) {
6401+
/* Store actual type to memory to avoid deoptimization mistakes */
6402+
/* TODO: Alternatively, we may try to update alredy generated deoptimization info */
6403+
zend_jit_store_var_type(&dasm_state, phi->var, STACK_TYPE(stack, phi->var));
6404+
}
6405+
phi = phi->next;
6406+
}
6407+
}
63926408
if (p->stop != ZEND_JIT_TRACE_STOP_RECURSIVE_RET) {
63936409
if ((t->flags & ZEND_JIT_TRACE_USES_INITIAL_IP)
63946410
&& !zend_jit_set_ip(&dasm_state, p->opline)) {

ext/opcache/jit/zend_jit_x86.dasc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3867,6 +3867,14 @@ static int zend_jit_store_var(dasm_State **Dst, uint32_t info, int var, zend_reg
38673867
return zend_jit_spill_store(Dst, src, dst, info, set_type);
38683868
}
38693869

3870+
static int zend_jit_store_var_type(dasm_State **Dst, int var, uint8_t type)
3871+
{
3872+
zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
3873+
3874+
| SET_ZVAL_TYPE_INFO dst, type
3875+
return 1;
3876+
}
3877+
38703878
static int zend_jit_store_var_if_necessary(dasm_State **Dst, int var, zend_jit_addr src, uint32_t info)
38713879
{
38723880
if (Z_MODE(src) == IS_REG && Z_STORE(src)) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Register Alloction 011: Missed type store
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit_buffer_size=1M
8+
--FILE--
9+
<?php
10+
function foo($y) {
11+
for ($cnt=0;$cnt<6;$cnt++) {
12+
$i = $y;
13+
for ($i=0;$i<1;)
14+
for(;$i<1;)
15+
for(;$i<1;$i++)
16+
for(;$y;);
17+
for($i=0;$i< 1;$i++)
18+
for(;$y;);
19+
}
20+
}
21+
foo(null);
22+
?>
23+
DONE
24+
--EXPECTF--
25+
DONE

0 commit comments

Comments
 (0)