Skip to content

Commit aad6cc5

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 9d89a89 commit aad6cc5

10 files changed

Lines changed: 97 additions & 136 deletions

File tree

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: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4673,20 +4673,6 @@ static void cleanup_unfinished_calls(zend_execute_data *execute_data, uint32_t o
46734673
}
46744674
/* }}} */
46754675

4676-
static const zend_live_range *find_live_range(const zend_op_array *op_array, uint32_t op_num, uint32_t var_num) /* {{{ */
4677-
{
4678-
int i;
4679-
for (i = 0; i < op_array->last_live_range; i++) {
4680-
const zend_live_range *range = &op_array->live_range[i];
4681-
if (op_num >= range->start && op_num < range->end
4682-
&& var_num == (range->var & ~ZEND_LIVE_MASK)) {
4683-
return range;
4684-
}
4685-
}
4686-
return NULL;
4687-
}
4688-
/* }}} */
4689-
46904676
static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num) /* {{{ */
46914677
{
46924678
int i;
@@ -4702,6 +4688,16 @@ static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num,
47024688
uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
47034689
zval *var = EX_VAR(var_num);
47044690

4691+
/* Handle the split range for loop vars */
4692+
if (catch_op_num) {
4693+
zend_op *final_op = EX(func)->op_array.opcodes + range->end;
4694+
if (final_op->extended_value & ZEND_FREE_ON_RETURN && (final_op->opcode == ZEND_FE_FREE || final_op->opcode == ZEND_FREE)) {
4695+
if (catch_op_num < range->end + final_op->op2.num) {
4696+
continue;
4697+
}
4698+
}
4699+
}
4700+
47054701
if (kind == ZEND_LIVE_TMPVAR) {
47064702
zval_ptr_dtor_nogc(var);
47074703
} else if (kind == ZEND_LIVE_NEW) {

Zend/zend_opcode.c

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -981,38 +981,35 @@ static void zend_calc_live_ranges(
981981
/* OP_DATA is really part of the previous opcode. */
982982
last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA);
983983
}
984-
} else if (opline->opcode == ZEND_FE_FREE
985-
&& opline->extended_value & ZEND_FREE_ON_RETURN
986-
&& opnum + 1 < op_array->last
987-
&& ((opline + 1)->opcode == ZEND_RETURN
988-
|| (opline + 1)->opcode == ZEND_RETURN_BY_REF
989-
|| (opline + 1)->opcode == ZEND_GENERATOR_RETURN)) {
990-
/* FE_FREE with ZEND_FREE_ON_RETURN immediately followed by RETURN frees
984+
} else if ((opline->opcode == ZEND_FREE || opline->opcode == ZEND_FE_FREE) && opline->extended_value & ZEND_FREE_ON_RETURN) {
985+
int jump_offset = 1;
986+
while (((opline + jump_offset)->opcode == ZEND_FREE || (opline + jump_offset)->opcode == ZEND_FE_FREE)
987+
&& (opline + jump_offset)->extended_value & ZEND_FREE_ON_RETURN) {
988+
++jump_offset;
989+
}
990+
// loop var frees directly precede the jump (or return) operand, except that ZEND_VERIFY_RETURN_TYPE may happen first.
991+
if ((opline + jump_offset)->opcode == ZEND_VERIFY_RETURN_TYPE) {
992+
++jump_offset;
993+
}
994+
/* FREE with ZEND_FREE_ON_RETURN immediately followed by RETURN frees
991995
* the loop variable on early return. We need to split the live range
992-
* so GC doesn't access the freed variable after this FE_FREE.
993-
*
994-
* FE_FREE is included in the range only if it pertains to an early
995-
* return. */
996-
uint32_t opnum_last_use = last_use[var_num]; // likely a FE_FREE
997-
__auto_type opline_last_use = &op_array->opcodes[opnum_last_use];
998-
if (opline_last_use->opcode == ZEND_FE_FREE &&
999-
opline_last_use->extended_value & ZEND_FREE_ON_RETURN) {
1000-
/* another early return; we include the FE_FREE */
1001-
emit_live_range_raw(op_array, var_num, ZEND_LIVE_LOOP,
1002-
opnum + 2, opnum_last_use + 1);
1003-
} else if (opline_last_use->opcode == ZEND_FE_FREE &&
1004-
!(opline_last_use->extended_value & ZEND_FREE_ON_RETURN)) {
1005-
/* the normal return; don't include the FE_FREE */
1006-
emit_live_range_raw(op_array, var_num, ZEND_LIVE_LOOP,
1007-
opnum + 2, opnum_last_use);
1008-
} else {
1009-
/* if the last use is not FE_FREE, include it */
1010-
emit_live_range_raw(op_array, var_num, ZEND_LIVE_LOOP,
1011-
opnum + 2, opnum_last_use + 1);
996+
* so GC doesn't access the freed variable after this FREE. */
997+
uint32_t opnum_last_use = last_use[var_num];
998+
zend_op *opline_last_use = op_array->opcodes + opnum_last_use;
999+
ZEND_ASSERT(opline_last_use->opcode == opline->opcode); // any ZEND_FREE_ON_RETURN must be followed by a FREE without
1000+
if (opnum + jump_offset + 1 != opnum_last_use) {
1001+
emit_live_range_raw(op_array, var_num, opline->opcode == ZEND_FE_FREE ? ZEND_LIVE_LOOP : ZEND_LIVE_TMPVAR,
1002+
opnum + jump_offset + 1, opnum_last_use);
10121003
}
10131004

1014-
/* Update last_use so next range includes this FE_FREE */
1015-
last_use[var_num] = opnum + 1;
1005+
/* Update last_use so next range includes this FREE */
1006+
last_use[var_num] = opnum;
1007+
1008+
/* Store opline offset to loop end */
1009+
opline->op2.opline_num = opnum_last_use - opnum;
1010+
if (opline_last_use->extended_value & ZEND_FREE_ON_RETURN) {
1011+
opline->op2.opline_num += opline_last_use->op2.opline_num;
1012+
}
10161013
}
10171014
}
10181015
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
@@ -3193,7 +3193,7 @@ ZEND_VM_COLD_CONST_HANDLER(47, ZEND_JMPNZ_EX, CONST|TMPVAR|CV, JMP_ADDR)
31933193
ZEND_VM_JMP(opline);
31943194
}
31953195

3196-
ZEND_VM_HANDLER(70, ZEND_FREE, TMPVAR, ANY)
3196+
ZEND_VM_HANDLER(70, ZEND_FREE, TMPVAR, LOOP_END)
31973197
{
31983198
USE_OPLINE
31993199

@@ -3202,7 +3202,7 @@ ZEND_VM_HANDLER(70, ZEND_FREE, TMPVAR, ANY)
32023202
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
32033203
}
32043204

3205-
ZEND_VM_HOT_HANDLER(127, ZEND_FE_FREE, TMPVAR, ANY)
3205+
ZEND_VM_HOT_HANDLER(127, ZEND_FE_FREE, TMPVAR, LOOP_END)
32063206
{
32073207
zval *var;
32083208
USE_OPLINE
@@ -8140,50 +8140,11 @@ ZEND_VM_HANDLER(149, ZEND_HANDLE_EXCEPTION, ANY, ANY)
81408140
&& throw_op->extended_value & ZEND_FREE_ON_RETURN) {
81418141
/* exceptions thrown because of loop var destruction on return/break/...
81428142
* are logically thrown at the end of the foreach loop, so adjust the
8143-
* throw_op_num.
8143+
* throw_op_num to the final loop variable FREE.
81448144
*/
8145-
const zend_live_range *range = find_live_range(
8146-
&EX(func)->op_array, throw_op_num, throw_op->op1.var);
8147-
8148-
/* free op1 of the corresponding RETURN - must use original throw_op_num
8149-
* and first range, before any split-range skipping */
8150-
uint32_t range_end = range->end;
8151-
for (i = throw_op_num; i < range_end; i++) {
8152-
__auto_type current_opline = EX(func)->op_array.opcodes[i];
8153-
if (current_opline.opcode == ZEND_FREE
8154-
|| current_opline.opcode == ZEND_FE_FREE) {
8155-
if (current_opline.extended_value & ZEND_FREE_ON_RETURN) {
8156-
/* if this is a split end, the ZEND_RETURN is not included
8157-
* in the range, so extend the range */
8158-
range_end++;
8159-
}
8160-
/* pass */
8161-
} else {
8162-
if (current_opline.opcode == ZEND_RETURN
8163-
&& (current_opline.op1_type & (IS_VAR|IS_TMP_VAR))) {
8164-
zval_ptr_dtor(EX_VAR(current_opline.op1.var));
8165-
}
8166-
break;
8167-
}
8168-
}
8169-
8170-
/* skip any split ranges to find the final range of the loop var and
8171-
* adjust throw_op_num */
8172-
for (;;) {
8173-
if (range->end < EX(func)->op_array.last) {
8174-
__auto_type last_range_opline = EX(func)->op_array.opcodes[range->end - 1];
8175-
if (last_range_opline.opcode == ZEND_FE_FREE &&
8176-
(last_range_opline.extended_value & ZEND_FREE_ON_RETURN)) {
8177-
/* the range was split, skip to find the final range */
8178-
throw_op_num = range->end + 1;
8179-
range = find_live_range(
8180-
&EX(func)->op_array, throw_op_num, throw_op->op1.var);
8181-
continue;
8182-
}
8183-
}
8184-
break;
8185-
}
8186-
throw_op_num = range->end;
8145+
uint32_t new_throw_op_num = throw_op_num + throw_op->op2.opline_num;
8146+
cleanup_live_vars(execute_data, throw_op_num, new_throw_op_num);
8147+
throw_op_num = new_throw_op_num;
81878148
}
81888149

81898150
/* 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)