Skip to content

Commit ee34f49

Browse files
committed
Split the live-ranges of loop variables again
b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION. This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling. At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions. This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically. Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now. Closes #20766. Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
1 parent 0c6e1b5 commit ee34f49

File tree

10 files changed

+86
-135
lines changed

10 files changed

+86
-135
lines changed

Zend/Optimizer/zend_dump.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ static void zend_dump_unused_op(const zend_op *opline, znode_op op, uint32_t fla
122122
if (op.num != (uint32_t)-1) {
123123
fprintf(stderr, " try-catch(%u)", op.num);
124124
}
125+
} else if (ZEND_VM_OP_LOOP_END == (flags & ZEND_VM_OP_MASK)) {
126+
if (opline->extended_value & ZEND_FREE_ON_RETURN) {
127+
fprintf(stderr, " loop-end(%u)", op.num);
128+
}
125129
} else if (ZEND_VM_OP_THIS == (flags & ZEND_VM_OP_MASK)) {
126130
fprintf(stderr, " THIS");
127131
} else if (ZEND_VM_OP_NEXT == (flags & ZEND_VM_OP_MASK)) {

Zend/tests/gc_050.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
GC 050: Try/finally in foreach should create separate live ranges
3+
--FILE--
4+
<?php
5+
6+
function f(int $n): object {
7+
try {
8+
foreach ((array) $n as $v) {
9+
if ($n === 1) {
10+
try {
11+
$a = new stdClass;
12+
return $a;
13+
} finally {
14+
return $ret = $a;
15+
}
16+
}
17+
if ($n === 2) {
18+
$b = new stdClass;
19+
return $ret = $b;
20+
}
21+
}
22+
} finally {
23+
$ret->v = 1;
24+
}
25+
return new stdClass;
26+
}
27+
28+
for ($i = 0; $i < 100000; $i++) {
29+
// Create cyclic garbage to trigger GC
30+
$a = new stdClass;
31+
$b = new stdClass;
32+
$a->r = $b;
33+
$b->r = $a;
34+
35+
$r = f($i % 2 + 1);
36+
}
37+
echo "OK\n";
38+
?>
39+
--EXPECT--
40+
OK

Zend/zend_execute.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4472,20 +4472,6 @@ static void cleanup_unfinished_calls(zend_execute_data *execute_data, uint32_t o
44724472
}
44734473
/* }}} */
44744474

4475-
static const zend_live_range *find_live_range(const zend_op_array *op_array, uint32_t op_num, uint32_t var_num) /* {{{ */
4476-
{
4477-
int i;
4478-
for (i = 0; i < op_array->last_live_range; i++) {
4479-
const zend_live_range *range = &op_array->live_range[i];
4480-
if (op_num >= range->start && op_num < range->end
4481-
&& var_num == (range->var & ~ZEND_LIVE_MASK)) {
4482-
return range;
4483-
}
4484-
}
4485-
return NULL;
4486-
}
4487-
/* }}} */
4488-
44894475
static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num) /* {{{ */
44904476
{
44914477
int i;

Zend/zend_opcode.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -963,38 +963,35 @@ static void zend_calc_live_ranges(
963963
/* OP_DATA is really part of the previous opcode. */
964964
last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA);
965965
}
966-
} else if (opline->opcode == ZEND_FE_FREE
967-
&& opline->extended_value & ZEND_FREE_ON_RETURN
968-
&& opnum + 1 < op_array->last
969-
&& ((opline + 1)->opcode == ZEND_RETURN
970-
|| (opline + 1)->opcode == ZEND_RETURN_BY_REF
971-
|| (opline + 1)->opcode == ZEND_GENERATOR_RETURN)) {
972-
/* FE_FREE with ZEND_FREE_ON_RETURN immediately followed by RETURN frees
966+
} else if ((opline->opcode == ZEND_FREE || opline->opcode == ZEND_FE_FREE) && opline->extended_value & ZEND_FREE_ON_RETURN) {
967+
int jump_offset = 1;
968+
while (((opline + jump_offset)->opcode == ZEND_FREE || (opline + jump_offset)->opcode == ZEND_FE_FREE)
969+
&& (opline + jump_offset)->extended_value & ZEND_FREE_ON_RETURN) {
970+
++jump_offset;
971+
}
972+
// loop var frees directly precede the jump (or return) operand, except that ZEND_VERIFY_RETURN_TYPE may happen first.
973+
if ((opline + jump_offset)->opcode == ZEND_VERIFY_RETURN_TYPE) {
974+
++jump_offset;
975+
}
976+
/* FREE with ZEND_FREE_ON_RETURN immediately followed by RETURN frees
973977
* the loop variable on early return. We need to split the live range
974-
* so GC doesn't access the freed variable after this FE_FREE.
975-
*
976-
* FE_FREE is included in the range only if it pertains to an early
977-
* return. */
978-
uint32_t opnum_last_use = last_use[var_num]; // likely a FE_FREE
979-
__auto_type opline_last_use = &op_array->opcodes[opnum_last_use];
980-
if (opline_last_use->opcode == ZEND_FE_FREE &&
981-
opline_last_use->extended_value & ZEND_FREE_ON_RETURN) {
982-
/* another early return; we include the FE_FREE */
983-
emit_live_range_raw(op_array, var_num, ZEND_LIVE_LOOP,
984-
opnum + 2, opnum_last_use + 1);
985-
} else if (opline_last_use->opcode == ZEND_FE_FREE &&
986-
!(opline_last_use->extended_value & ZEND_FREE_ON_RETURN)) {
987-
/* the normal return; don't include the FE_FREE */
988-
emit_live_range_raw(op_array, var_num, ZEND_LIVE_LOOP,
989-
opnum + 2, opnum_last_use);
990-
} else {
991-
/* if the last use is not FE_FREE, include it */
978+
* so GC doesn't access the freed variable after this FREE. */
979+
uint32_t opnum_last_use = last_use[var_num];
980+
zend_op *opline_last_use = op_array->opcodes + opnum_last_use;
981+
ZEND_ASSERT(opline_last_use->opcode == opline->opcode); // any ZEND_FREE_ON_RETURN must be followed by a FREE without
982+
if (opnum + jump_offset + 1 != opnum_last_use) {
992983
emit_live_range_raw(op_array, var_num, ZEND_LIVE_LOOP,
993-
opnum + 2, opnum_last_use + 1);
984+
opnum + jump_offset + 1, opnum_last_use);
994985
}
995986

996-
/* Update last_use so next range includes this FE_FREE */
997-
last_use[var_num] = opnum + 1;
987+
/* Update last_use so next range includes this FREE */
988+
last_use[var_num] = opnum;
989+
990+
/* Store opline offset to loop end */
991+
opline->op2.opline_num = opnum_last_use - opnum;
992+
if (opline_last_use->extended_value & ZEND_FREE_ON_RETURN) {
993+
opline->op2.opline_num += opline_last_use->op2.opline_num;
994+
}
998995
}
999996
}
1000997
if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {

Zend/zend_vm_def.h

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3134,7 +3134,7 @@ ZEND_VM_COLD_CONST_HANDLER(47, ZEND_JMPNZ_EX, CONST|TMPVAR|CV, JMP_ADDR)
31343134
ZEND_VM_JMP(opline);
31353135
}
31363136

3137-
ZEND_VM_HANDLER(70, ZEND_FREE, TMPVAR, ANY)
3137+
ZEND_VM_HANDLER(70, ZEND_FREE, TMPVAR, LOOP_END)
31383138
{
31393139
USE_OPLINE
31403140

@@ -3143,7 +3143,7 @@ ZEND_VM_HANDLER(70, ZEND_FREE, TMPVAR, ANY)
31433143
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
31443144
}
31453145

3146-
ZEND_VM_HOT_HANDLER(127, ZEND_FE_FREE, TMPVAR, ANY)
3146+
ZEND_VM_HOT_HANDLER(127, ZEND_FE_FREE, TMPVAR, LOOP_END)
31473147
{
31483148
zval *var;
31493149
USE_OPLINE
@@ -8055,50 +8055,11 @@ ZEND_VM_HANDLER(149, ZEND_HANDLE_EXCEPTION, ANY, ANY)
80558055
&& throw_op->extended_value & ZEND_FREE_ON_RETURN) {
80568056
/* exceptions thrown because of loop var destruction on return/break/...
80578057
* are logically thrown at the end of the foreach loop, so adjust the
8058-
* throw_op_num.
8058+
* throw_op_num to the final loop variable FREE.
80598059
*/
8060-
const zend_live_range *range = find_live_range(
8061-
&EX(func)->op_array, throw_op_num, throw_op->op1.var);
8062-
8063-
/* free op1 of the corresponding RETURN - must use original throw_op_num
8064-
* and first range, before any split-range skipping */
8065-
uint32_t range_end = range->end;
8066-
for (i = throw_op_num; i < range_end; i++) {
8067-
__auto_type current_opline = EX(func)->op_array.opcodes[i];
8068-
if (current_opline.opcode == ZEND_FREE
8069-
|| current_opline.opcode == ZEND_FE_FREE) {
8070-
if (current_opline.extended_value & ZEND_FREE_ON_RETURN) {
8071-
/* if this is a split end, the ZEND_RETURN is not included
8072-
* in the range, so extend the range */
8073-
range_end++;
8074-
}
8075-
/* pass */
8076-
} else {
8077-
if (current_opline.opcode == ZEND_RETURN
8078-
&& (current_opline.op1_type & (IS_VAR|IS_TMP_VAR))) {
8079-
zval_ptr_dtor(EX_VAR(current_opline.op1.var));
8080-
}
8081-
break;
8082-
}
8083-
}
8084-
8085-
/* skip any split ranges to find the final range of the loop var and
8086-
* adjust throw_op_num */
8087-
for (;;) {
8088-
if (range->end < EX(func)->op_array.last) {
8089-
__auto_type last_range_opline = EX(func)->op_array.opcodes[range->end - 1];
8090-
if (last_range_opline.opcode == ZEND_FE_FREE &&
8091-
(last_range_opline.extended_value & ZEND_FREE_ON_RETURN)) {
8092-
/* the range was split, skip to find the final range */
8093-
throw_op_num = range->end + 1;
8094-
range = find_live_range(
8095-
&EX(func)->op_array, throw_op_num, throw_op->op1.var);
8096-
continue;
8097-
}
8098-
}
8099-
break;
8100-
}
8101-
throw_op_num = range->end;
8060+
uint32_t new_throw_op_num = throw_op_num + throw_op->op2.opline_num;
8061+
cleanup_live_vars(execute_data, throw_op_num, new_throw_op_num);
8062+
throw_op_num = new_throw_op_num;
81028063
}
81038064

81048065
/* Find the innermost try/catch/finally the exception was thrown in */

Zend/zend_vm_execute.h

Lines changed: 4 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Zend/zend_vm_gen.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"ZEND_VM_OP_NUM" => 0x10,
6464
"ZEND_VM_OP_JMP_ADDR" => 0x20,
6565
"ZEND_VM_OP_TRY_CATCH" => 0x30,
66-
// unused 0x40
66+
"ZEND_VM_OP_LOOP_END" => 0x40,
6767
"ZEND_VM_OP_THIS" => 0x50,
6868
"ZEND_VM_OP_NEXT" => 0x60,
6969
"ZEND_VM_OP_CLASS_FETCH" => 0x70,
@@ -111,6 +111,7 @@
111111
"NUM" => ZEND_VM_OP_NUM,
112112
"JMP_ADDR" => ZEND_VM_OP_JMP_ADDR,
113113
"TRY_CATCH" => ZEND_VM_OP_TRY_CATCH,
114+
"LOOP_END" => ZEND_VM_OP_LOOP_END,
114115
"THIS" => ZEND_VM_OP_THIS,
115116
"NEXT" => ZEND_VM_OP_NEXT,
116117
"CLASS_FETCH" => ZEND_VM_OP_CLASS_FETCH,

Zend/zend_vm_opcodes.c

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Zend/zend_vm_opcodes.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/opcache/tests/opt/gh11245_2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ $_main:
2828
0000 T1 = PRE_INC_STATIC_PROP string("prop") string("X")
2929
0001 T2 = ISSET_ISEMPTY_CV (empty) CV0($xx)
3030
0002 JMPZ T2 0005
31-
0003 FREE T1
31+
0003 FREE T1 loop-end(2)
3232
0004 RETURN null
3333
0005 FREE T1
3434
0006 RETURN int(1)
3535
LIVE RANGES:
36-
1: 0001 - 0005 (tmp/var)
36+
1: 0001 - 0003 (tmp/var)

0 commit comments

Comments
 (0)