Skip to content

mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded #25779

Done

Description

@thunderer

Description

The following code, run with -d opcache.enable_cli=1 -d opcache.jit=function:

<?php

class Row
{
    public static array $rows = [];

    public function __construct()
    {
        self::$rows[] = $this;
    }
}

function load(mysqli_result $result): void
{
    mysqli_fetch_object($result, Row::class);
}

$connection = mysqli_connect("127.0.0.1", "root", "root", "test");
load(mysqli_query($connection, "SELECT 1 AS id"));
var_dump(Row::$rows);

Resulted in this output:

array(1) {
  [0]=>
  Segmentation fault (core dumped)

But I expected this output instead (PHP without JIT prints it):

array(1) {
  [0]=>
  object(Row)#3 (1) {
    ["id"]=>
    string(1) "1"
  }
}

With opcache.jit=tracing, the JIT frees the objects once load() is hot. When load() is called 1,000 times in a loop, count(array_unique(array_map('spl_object_id', Row::$rows))) gives int(62) instead of int(1000), because new objects reuse the slots of objects that Row::$rows still holds.

mysqli_fetch_object() calls the constructor on the object it returns, and the constructor can keep a reference to $this. The stub still declares @refcount 1, so the JIT destroys the object when the result is discarded, while Row::$rows still holds it. A debug build with ZEND_VERIFY_FUNC_INFO reports mysqli_fetch_object() missing rcn. The method mysqli_result::fetch_object() is not affected.

I have a patch with a test and will open a PR against PHP-8.4.

PHP Version

PHP 8.5.12 (cli) (built: Oct 28 2026 01:20:43) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies

Also reproduced with PHP 8.4.27 and the current PHP-8.4, PHP-8.5, PHP-8.6 and master branches.

Operating System

Debian 13 (php:8.5-cli Docker image with the mysqli extension, MySQL 8.4.13)

Activity

  1. added a commit that references this issue on Nov 26, 2026
    24bb1fa
  2. added a commit that references this issue on Nov 26, 2026
    8084ab3
  3. added 2 commits that reference this issue on Nov 27, 2026
    81c0240
    bd2f53f
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