[php-src] PR #17303: Fix GH-12837: Combination of phpdbg and Generator method causes memory leak
| From: | nielsdos | Date: | Mon, 30 Dec 2024 15:26:08 +0000 |
| Subject: | [php-src] PR #17303: Fix GH-12837: Combination of phpdbg and Generator method causes memory leak | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-30167@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/17303
Author: nielsdos
The fix for [bug #72523](https://bugs.php.net/bug.php?id=72523) (d1dd474) added a check on
zend_execute_ex to add an extra refcount to This. This is necessary
because the VM does a re-entry here:
https://github.com/php/php-src/blob/ecb90c1db7efe9d87c07e31336185fc393281035/Zend/zend_vm_def.h#L4294-L4299
and then cleans up This here:
https://github.com/php/php-src/blob/ecb90c1db7efe9d87c07e31336185fc393281035/Zend/zend_vm_def.h#L4358-L4360
So if we don't add the refcount, we destroy This both in the VM and in
zend_generator_close.
The reason this causes a leak in phpdbg is because phpdbg temporarily changes
zend_execute_ex to the default here for ZEND_DO_FCALL: https://github.com/php/php-src/blob/ecb90c1db7efe9d87c07e31336185fc393281035/sapi/phpdbg/phpdbg_prompt.c#L1820-L1827
which means that we only execute OBJ_RELEASE on the This object once in
zend_generator_close instead of also doing it in the ZEND_DO_FCALL
handler.
To solve this, we make sure the check for the re-entrancy is consistently done by checking the
ZEND_CALL_TOP flag instead of relying solely on execute_ex pointer,
because this flag is set when zend_execute_ex was changed:
https://github.com/php/php-src/blob/ecb90c1db7efe9d87c07e31336185fc393281035/Zend/zend_vm_def.h#L4298