Skip to content

Commit 97ee5bd

Browse files
committed
Zend: Reset extended_value of FREE synthesized by DCE
DCE rewrites a dead instruction whose operand still needs freeing into a FREE, but kept the instruction's extended_value. After (float) and (array) casts and empty() it has bit 0 set, which ZEND_HANDLE_EXCEPTION reads as ZEND_FREE_ON_RETURN: when a destructor run by that FREE throws, the throw point moves by the FREE's op2, which is (uint32_t)-1, and cleanup_live_vars() frees the operand a second time.
1 parent f60216c commit 97ee5bd

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ PHP NEWS
163163
. Fixed function JIT ASSIGN_DIM reporting an undefined value or key out of
164164
order, or when the VM does not, on non-array containers. (Jorg Sowa)
165165
. Fix JIT register allocation for ASSIGN to traced reference. (ndossche)
166+
. Fixed a double free when a destructor throws while freeing a temporary
167+
whose result the optimizer discarded. (Ilia Alshanetsky)
166168

167169
- PDO_Firebird:
168170
. Fixed stack buffer overflow with long connection user names, passwords,

‎Zend/Optimizer/dce.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ static bool dce_instr(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
454454
opline->opcode = ZEND_FREE;
455455
opline->op1.var = EX_NUM_TO_VAR(ssa->vars[free_var].var);
456456
opline->op1_type = free_var_type;
457+
opline->extended_value = 0;
457458

458459
ssa_op->op1_use = free_var;
459460
ssa_op->op1_use_chain = ssa->vars[free_var].use_chain;

‎ext/opcache/tests/opt/dce_029.phpt‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
DCE must not keep the replaced instruction's extended_value on a synthesized FREE
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.optimization_level=-1
7+
opcache.file_update_protection=0
8+
opcache.jit=disable
9+
--EXTENSIONS--
10+
opcache
11+
--FILE--
12+
<?php
13+
class D {
14+
public function __destruct() {
15+
throw new Exception("dtor");
16+
}
17+
}
18+
19+
final class C {
20+
public array $a;
21+
22+
public function __construct() {
23+
$this->a = [new D];
24+
}
25+
26+
public function clobber(): int {
27+
$this->a = [];
28+
return 0;
29+
}
30+
31+
public function test() {
32+
empty(($this->a ?? [])[$this->clobber()]);
33+
}
34+
}
35+
36+
$c = new C;
37+
try {
38+
$c->test();
39+
} catch (Exception $e) {
40+
echo $e::class, ": ", $e->getMessage(), "\n";
41+
}
42+
43+
$a = [];
44+
for ($i = 0; $i < 10; $i++) {
45+
$a[] = [$i];
46+
}
47+
echo count($a), "\n";
48+
?>
49+
--EXPECT--
50+
Exception: dtor
51+
10

0 commit comments

Comments
 (0)