Skip to content

Commit ec30f4c

Browse files
committed
JIT: Fix OP_DATA leak when ASSIGN_DIM fails
Free OP_DATA when appending to a full array, and when converting a float or resource key throws, matching the VM. zend_jit_fetch_dim_w_helper() no longer frees OP_DATA, which zend_jit_assign_dim_helper() frees again after converting false to an array. Generated ASSIGN_DIM code frees it through a dedicated fetch helper and inline on append failure. Closes GH-24581
1 parent 7c0b900 commit ec30f4c

13 files changed

Lines changed: 369 additions & 12 deletions

‎NEWS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ PHP NEWS
1515
is exhausted while compiling side traces). (Ilia Alshanetsky)
1616
. Fixed bug GH-25164 (Function JIT loop calling a user function runs once
1717
or never terminates). (Ilia Alshanetsky)
18+
. Fixed memory leak in JIT ASSIGN_DIM when the assignment fails. (Jorg Sowa)
1819

1920
19 Nov 2026, PHP 8.4.28
2021

‎ext/opcache/jit/zend_jit_helpers.c‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -922,10 +922,6 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_w_helper(zend_array *ht, zval *dim
922922
ZVAL_NULL(EX_VAR(opline->result.var));
923923
}
924924
}
925-
if (opline->opcode == ZEND_ASSIGN_DIM
926-
&& ((opline+1)->op1_type & (IS_VAR | IS_TMP_VAR))) {
927-
zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var));
928-
}
929925
return NULL;
930926
}
931927
ZEND_FALLTHROUGH;
@@ -1003,13 +999,6 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_w_helper(zend_array *ht, zval *dim
1003999
default:
10041000
zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), dim, BP_VAR_R);
10051001
undef_result_after_exception();
1006-
if (EG(opline_before_exception)
1007-
&& (EG(opline_before_exception)+1)->opcode == ZEND_OP_DATA
1008-
&& ((EG(opline_before_exception)+1)->op1_type & (IS_VAR|IS_TMP_VAR))) {
1009-
zend_execute_data *execute_data = EG(current_execute_data);
1010-
1011-
zval_ptr_dtor_nogc(EX_VAR((EG(opline_before_exception)+1)->op1.var));
1012-
}
10131002
return NULL;
10141003
}
10151004

@@ -1472,6 +1461,20 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_obj_rw_helper(zval *object_ptr, zva
14721461
// zend_jit_fetch_dim_obj_helper(object_ptr, dim, result, BP_VAR_UNSET);
14731462
//}
14741463

1464+
static zval* ZEND_FASTCALL zend_jit_assign_dim_fetch_w_helper(zend_array *ht, zval *dim)
1465+
{
1466+
zval *retval = zend_jit_fetch_dim_w_helper(ht, dim);
1467+
1468+
if (UNEXPECTED(!retval)) {
1469+
zend_execute_data *execute_data = EG(current_execute_data);
1470+
/* Throwing replaces EX(opline) with HANDLE_EXCEPTION */
1471+
const zend_op *opline = EG(exception) ? EG(opline_before_exception) : EX(opline);
1472+
1473+
zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var));
1474+
}
1475+
return retval;
1476+
}
1477+
14751478
static void ZEND_FASTCALL zend_jit_assign_dim_helper(zval *object_ptr, zval *dim, zval *value, zval *result)
14761479
{
14771480
if (EXPECTED(Z_TYPE_P(object_ptr) == IS_OBJECT)) {

‎ext/opcache/jit/zend_jit_ir.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3139,6 +3139,7 @@ static void zend_jit_setup_disasm(void)
31393139
REGISTER_HELPER(zend_jit_fetch_dim_isset_helper);
31403140
REGISTER_HELPER(zend_jit_fetch_dim_rw_helper);
31413141
REGISTER_HELPER(zend_jit_fetch_dim_w_helper);
3142+
REGISTER_HELPER(zend_jit_assign_dim_fetch_w_helper);
31423143
REGISTER_HELPER(zend_jit_fetch_dim_str_offset_r_helper);
31433144
REGISTER_HELPER(zend_jit_fetch_dim_str_r_helper);
31443145
REGISTER_HELPER(zend_jit_fetch_dim_str_is_helper);
@@ -12437,7 +12438,11 @@ static int zend_jit_fetch_dimension_address_inner(zend_jit_ctx *jit,
1243712438
ir_END_list(*end_inputs);
1243812439
break;
1243912440
case BP_VAR_W:
12440-
ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_w_helper), ht_ref, ref);
12441+
if (opline->opcode == ZEND_ASSIGN_DIM && ((opline+1)->op1_type & (IS_VAR|IS_TMP_VAR))) {
12442+
ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_assign_dim_fetch_w_helper), ht_ref, ref);
12443+
} else {
12444+
ref = ir_CALL_2(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_fetch_dim_w_helper), ht_ref, ref);
12445+
}
1244112446
if_found = ir_IF(ref);
1244212447
ir_IF_TRUE(if_found);
1244312448
ir_refs_add(found_inputs, ir_END());
@@ -13373,6 +13378,7 @@ static int zend_jit_assign_dim(zend_jit_ctx *jit,
1337313378
// JIT: zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
1337413379
jit_SET_EX_OPLINE(jit, opline);
1337513380
ir_CALL(IR_VOID, jit_STUB_FUNC_ADDR(jit, jit_stub_cannot_add_element, IR_FASTCALL_FUNC));
13381+
jit_FREE_OP(jit, (opline+1)->op1_type, (opline+1)->op1, val_info, NULL);
1337613382

1337713383
ir_END_list(end_inputs);
1337813384

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: OP_DATA is freed when appending to a full array fails
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit=function
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
class D {
13+
function __destruct() {
14+
echo "dtor\n";
15+
}
16+
}
17+
18+
function test() {
19+
$a = [PHP_INT_MAX => 1];
20+
$a[] = new D;
21+
}
22+
23+
try {
24+
test();
25+
} catch (Throwable $e) {
26+
echo $e::class, ": ", $e->getMessage(), "\n";
27+
}
28+
?>
29+
--EXPECT--
30+
dtor
31+
Error: Cannot add element to the array as the next element is already occupied
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: OP_DATA is freed when a float key conversion throws
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit=function
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
class D {
13+
function __destruct() {
14+
echo "dtor\n";
15+
}
16+
}
17+
18+
set_error_handler(function ($errno, $errstr) {
19+
throw new Exception($errstr);
20+
});
21+
22+
function test() {
23+
$a[1.5] = new D;
24+
}
25+
26+
try {
27+
test();
28+
} catch (Throwable $e) {
29+
echo $e::class, ": ", $e->getMessage(), "\n";
30+
}
31+
?>
32+
--EXPECT--
33+
dtor
34+
Exception: Implicit conversion from float 1.5 to int loses precision
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: OP_DATA is freed when a resource key conversion throws
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit=function
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
class D {
13+
function __destruct() {
14+
echo "dtor\n";
15+
}
16+
}
17+
18+
set_error_handler(function ($errno, $errstr) {
19+
throw new Exception($errstr);
20+
});
21+
22+
function test() {
23+
$a[STDIN] = new D;
24+
}
25+
26+
try {
27+
test();
28+
} catch (Throwable $e) {
29+
echo $e::class, ": ", $e->getMessage(), "\n";
30+
}
31+
?>
32+
--EXPECTF--
33+
dtor
34+
Exception: Resource ID#%d used as offset, casting to integer (%d)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: OP_DATA is freed once when a float key conversion throws on a false container
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit=function
8+
zend.exception_ignore_args=1
9+
--EXTENSIONS--
10+
opcache
11+
--FILE--
12+
<?php
13+
class D {
14+
function __destruct() {
15+
echo "dtor\n";
16+
}
17+
}
18+
19+
set_error_handler(function ($errno, $errstr) {
20+
if (!str_starts_with($errstr, "Automatic conversion of false")) {
21+
throw new Exception($errstr);
22+
}
23+
});
24+
25+
function id($x) {
26+
return $x;
27+
}
28+
29+
function test($o) {
30+
$a = false;
31+
$a[1.5] = id($o);
32+
}
33+
34+
try {
35+
test(new D);
36+
} catch (Exception $e) {
37+
echo $e::class, ": ", $e->getMessage(), "\n";
38+
}
39+
?>
40+
--EXPECT--
41+
dtor
42+
Exception: Implicit conversion from float 1.5 to int loses precision
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: OP_DATA is freed once when a resource key conversion throws on a false container
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit=function
8+
zend.exception_ignore_args=1
9+
--EXTENSIONS--
10+
opcache
11+
--FILE--
12+
<?php
13+
class D {
14+
function __destruct() {
15+
echo "dtor\n";
16+
}
17+
}
18+
19+
set_error_handler(function ($errno, $errstr) {
20+
if (!str_starts_with($errstr, "Automatic conversion of false")) {
21+
throw new Exception($errstr);
22+
}
23+
});
24+
25+
function id($x) {
26+
return $x;
27+
}
28+
29+
function test($o) {
30+
$a = false;
31+
$a[STDIN] = id($o);
32+
}
33+
34+
try {
35+
test(new D);
36+
} catch (Exception $e) {
37+
echo $e::class, ": ", $e->getMessage(), "\n";
38+
}
39+
?>
40+
--EXPECTF--
41+
dtor
42+
Exception: Resource ID#%d used as offset, casting to integer (%d)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: OP_DATA is freed when the key is an illegal offset
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit=function
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
class D {
13+
function __destruct() {
14+
echo "dtor\n";
15+
}
16+
}
17+
18+
function test() {
19+
$a[[]] = new D;
20+
}
21+
22+
try {
23+
test();
24+
} catch (Throwable $e) {
25+
echo $e::class, ": ", $e->getMessage(), "\n";
26+
}
27+
?>
28+
--EXPECT--
29+
dtor
30+
TypeError: Cannot access offset of type array on array
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: OP_DATA is freed when an undefined key warning throws
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit=function
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
class D {
13+
function __destruct() {
14+
echo "dtor\n";
15+
}
16+
}
17+
18+
set_error_handler(function ($errno, $errstr) {
19+
throw new Exception($errstr);
20+
});
21+
22+
function test() {
23+
$a[$k] = new D;
24+
}
25+
26+
try {
27+
test();
28+
} catch (Throwable $e) {
29+
echo $e::class, ": ", $e->getMessage(), "\n";
30+
}
31+
?>
32+
--EXPECT--
33+
dtor
34+
Exception: Undefined variable $k

0 commit comments

Comments
 (0)