Skip to content

Uninitialised opline read in zend_generator_dtor_storage() when an internal function drives a generator that throws while holding a suspended generator with finally #25929

Backlog

Description

@developer-SJLee

Description

The following code:

<?php
function inner() { try { yield 1; } finally { echo "fin "; } }
function outer() { $s = inner(); $s->current(); throw new RuntimeException("x"); yield 1; }
try { iterator_to_array(outer()); } catch (RuntimeException $e) { echo "caught\n"; }

Resulted in this output:

Segmentation fault (exit code 139)

But I expected this output instead:

fin caught

It crashes reliably (3/3) with the PHP CLI of the dunglas/frankenphp:1.13.5-php8.5.12-bookworm Docker image (ZTS, Debian bookworm, x86_64) and our ini. A plain build of the 8.5.12 source with --disable-all --enable-zts shows the same uninitialised read under valgrind (below), so it does not depend on extensions. With php -n it happens to pass, but valgrind reports the same uninitialised read, so passing looks like luck of memory layout.

Conditions (all three needed, as far as I could narrow it down):

  1. An outer generator throws.
  2. At that moment one of its CVs holds a suspended inner generator that still has a pending finally (an empty finally {} is enough).
  3. The outer generator is driven by an internal function (iterator_to_array, iterator_count, IteratorIterator). Driving it with a userland foreach is clean under valgrind.

valgrind on 8.5.12 built with -g -O2 --disable-all --enable-zts:

Conditional jump or move depends on uninitialised value(s)
  at zend_generator_dtor_storage (zend_generators.c:317)
  by zend_objects_store_del (zend_objects_API.c:181)
  by zend_free_compiled_variables (zend_execute.c:4285)
  by zend_generator_close (zend_generators.c:146)
  by zend_dispatch_try_catch_finally_helper_SPEC (zend_vm_execute.h:3379)
  by execute_ex
  by zend_generator_resume (zend_generators.c:849)
  by zend_generator_iterator_rewind (zend_generators.c:1197)
  by spl_iterator_apply (spl_iterators.c:2961)
  by zif_iterator_to_array (spl_iterators.c:3054)

Line 317 is:

if (EG(current_execute_data)
 && EG(current_execute_data)->opline
 && EG(current_execute_data)->opline->opcode == ZEND_HANDLE_EXCEPTION) {

Here EG(current_execute_data) is the frame of the internal function iterator_to_array, whose opline is not initialised, so the check dereferences garbage. The opline->opcode check came with fb1ec9a (GH-20714), whose message notes EG(current_execute_data) may already be the parent frame; the parent being an internal frame does not seem to be covered. Guarding with ZEND_USER_CODE(EG(current_execute_data)->func->type) made the valgrind error go away in my local build, but I have not run the php-src test suite with it.

The same lines are unchanged on PHP-8.4, PHP-8.5, master and php-8.5.13 as of today.

PHP Version

PHP 8.5.12 (cli) (ZTS)

Operating System

Debian GNU/Linux 12 (bookworm), x86_64

Activity

  1. lazerg commented on Nov 30, 2026

    @lazerg
    Contributor

    Looks like #25278 already covers this with a ZEND_USER_CODE guard in zend_generator_dtor_storage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions