[php-src] PR #15275: Fix crash during GC of suspended generator delegate

From: Date: Wed, 07 Aug 2024 14:04:07 +0000
Subject: [php-src] PR #15275: Fix crash during GC of suspended generator delegate
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-27723@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/15275 Author: arnaud-lb We assume that non-running generators have their ->execute_data->opline pointing to the opline next to the yield or yield from that stopped it (e.g. [here](https://github.com/php/php-src/blob/2f27e0b2ab09624bab26f443d848aafd85325dc5/Zend/zend_execute.c#L4773), [here](https://github.com/php/php-src/blob/2f27e0b2ab09624bab26f443d848aafd85325dc5/Zend/zend_generators.c#L515), and [here](https://github.com/php/php-src/blob/2f27e0b2ab09624bab26f443d848aafd85325dc5/Zend/zend_generators.c#L874)). However, when fetching the next delegated value for a yield from $nonGenerator, the opline points to the yield from op due to temporary adjustments in [zend_generator_get_next_delegated_value()](https://github.com/php/php-src/blob/2832b4254c2592e6feeb087fb7b06f01edbef0c5/Zend/zend_generators.c#L663). This causes [crashes](https://3v4l.org/Yjhbg) when cleaning up unfinished calls or [assertion failures](https://github.com/php/php-src/blob/2f27e0b2ab09624bab26f443d848aafd85325dc5/Zend/zend_execute.c#L4773) during GC. Here I change zend_generator_get_next_delegated_value() to not adjust the opline, so that the expectation is always true. zend_generator_throw_exception() also adjusts the opline, which causes similar issues if zval_ptr_dtor(&generator->values) triggers the GC. I update it to cancel the adjustment before destroying generator->values. Alternatively, I think it may be simpler to stop incrementing opline in the yield and yield from op handlers (and to leave this task to zend_generator_resume()), so that we don't have to patch opline in other places. However this targets 8.2, so I didn't explore this idea. While testing I've also found that calling ->throw() on a running generator, or a child of a running generator, may crash. In [[1]](https://3v4l.org/5pi53), the getIterator() generator is getting closed during ->throw(), but its execute_data is accessed after that. In [[2]](https://3v4l.org/mlt4G) we break zend_generator_throw_exception()'s assumption of being called on a non-running generator. Should we forbid calling ->throw() when zend_generator_get_current() is running?

« previous php.git-pulls (#27723) next »