You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Method call may leak if releasing EG(This) triggers GC #13687
After thinking about it, I don't believe that switching to zval_ptr_dtor() is the right solution.
The issue is more general, as it may happen for any opcode that produces a TMPVAR and may trigger GC. For example, this also leaks:
class A {
public$cycle;
publicfunction__construct() { $this->cycle = $this; }
publicfunction__toString() { return__CLASS__; }
}
class B {
publicfunctionget() {
returnnewA();
}
}
$root = newstdClass;
gc_collect_cycles();
$objs = [];
while (gc_status()['roots']+2 < gc_status()['threshold']) {
$obj = newstdClass;
$objs[] = $obj;
}
// In FETCH_DIM_R: // - Copy A to result operand// - Dtor tmp array, which dtors A and $root, which triggers GC// - GC roots are empty, A is still live// In FETCH_CLASS_NAME:// - zval_ptr_dtor_nogc(A)$a = [newA, $root][0]::class;
In the GC, zend_gc_check_root_tmpvars() is meant to handle this kind of situations, but only when there is a live range for the TMPVAR. Here there is no live range as the TMPVAR is consumed by the next opcode.
A possible fix is to treat EX(opline)->result as live in zend_gc_check_root_tmpvars(), like we do in ZEND_HANDLE_EXCEPTION:
if (throw_op->result_type& (IS_VAR | IS_TMP_VAR)) {
switch (throw_op->opcode) {
caseZEND_ADD_ARRAY_ELEMENT:
caseZEND_ADD_ARRAY_UNPACK:
caseZEND_ROPE_INIT:
caseZEND_ROPE_ADD:
break; /* exception while building structures, live range handling will free those */
caseZEND_FETCH_CLASS:
caseZEND_DECLARE_ANON_CLASS:
break; /* return value is zend_class_entry pointer */
default:
/* smart branch opcodes may not initialize result */
if (!zend_is_smart_branch(throw_op)) {
zval_ptr_dtor_nogc(EX_VAR(throw_op->result.var));
I've implemented this solution here: #19787, but I think it's unsafe as-is, since in theory the result operand may not be initialized yet when the GC is triggered. It's safe in ZEND_HANDLE_EXCEPTION because the handler of throw_op has returned at this point.
To make it safe we may need to delay GC until a safe point: gc_check_possible_root() should set vm_interrupt and a new needs_gc flag. We can then trigger GC from the interrupt handler. This would be a good thing regardless of this issue IMHO.
gc_check_possible_root() should set vm_interrupt and a new needs_gc flag. We can then trigger GC from the interrupt handler. This would be a good thing regardless of this issue IMHO.
Agreed. Doesn't this approach solve the issue all on its own? If the problem is the order of zval_ptr_dtor() -> GC -> zval_ptr_dtor_nogc() from the VM, doesn't changing the order to zval_ptr_dtor() -> zval_ptr_dtor_nogc() -> GC already prevent the value being removed from the GC root, hence being considered for the GC run again?
Not necessarily, as the interrupt may be handled just after the opcode handler that produced the TMPVAR, and just before the one that will consume it. That will be the case for DO_FCALL for instance.
Right. These two approaches combined make sense to me then! We might have to account for the GC buffer growing over the limit while we're waiting for the interrupt.
Activity
krakjoe commentedon Sep 10, 2025
Can you elaborate on what the side effects or consequences (beyond the obvious perf difference) might be which stopped you proposing the switch ?
Can we also get some more input from @iluuu1994 and perhaps @dstogov ?
(note, I didn't add the auto-closing category on purpose, I don't intend to end the conversation, this looks like it needs to be addressed)
arnaud-lb commentedon Sep 10, 2025
After thinking about it, I don't believe that switching to
zval_ptr_dtor()is the right solution.The issue is more general, as it may happen for any opcode that produces a TMPVAR and may trigger GC. For example, this also leaks:
In the GC,
zend_gc_check_root_tmpvars()is meant to handle this kind of situations, but only when there is a live range for the TMPVAR. Here there is no live range as the TMPVAR is consumed by the next opcode.A possible fix is to treat
EX(opline)->resultas live inzend_gc_check_root_tmpvars(), like we do inZEND_HANDLE_EXCEPTION:php-src/Zend/zend_vm_def.h
Lines 8089 to 8104 in 94a12d5
I've implemented this solution here: #19787, but I think it's unsafe as-is, since in theory the result operand may not be initialized yet when the GC is triggered. It's safe in
ZEND_HANDLE_EXCEPTIONbecause the handler ofthrow_ophas returned at this point.To make it safe we may need to delay GC until a safe point:
gc_check_possible_root()should setvm_interruptand a newneeds_gcflag. We can then trigger GC from the interrupt handler. This would be a good thing regardless of this issue IMHO.iluuu1994 commentedon Sep 10, 2025
Agreed. Doesn't this approach solve the issue all on its own? If the problem is the order of
zval_ptr_dtor()-> GC ->zval_ptr_dtor_nogc()from the VM, doesn't changing the order tozval_ptr_dtor()->zval_ptr_dtor_nogc()-> GC already prevent the value being removed from the GC root, hence being considered for the GC run again?arnaud-lb commentedon Sep 10, 2025
Not necessarily, as the interrupt may be handled just after the opcode handler that produced the TMPVAR, and just before the one that will consume it. That will be the case for
DO_FCALLfor instance.iluuu1994 commentedon Sep 10, 2025
Right. These two approaches combined make sense to me then! We might have to account for the GC buffer growing over the limit while we're waiting for the interrupt.