Skip to content

Fix GH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded - #25784

Merged
kamil-tekiela merged 1 commit into
php:PHP-8.4from
thunderer:fix/mysqli-fetch-object-refcount
Nov 26, 2026
Merged

kamil-tekiela merged 1 commit into
php:PHP-8.4from
thunderer:fix/mysqli-fetch-object-refcount

Conversation

@thunderer

Copy link
Copy Markdown
Contributor

Fixes GH-25779.

mysqli_fetch_object() calls the constructor on the object it returns, and the constructor can keep a reference to $this. The stub still declared @refcount 1, so function JIT and tracing JIT destroyed the object when the result was discarded, while that reference still used it.

This drops the annotation and regenerates the arginfo and optimizer info, as c726574 did for pg_fetch_object(). The test fails without the change. The method mysqli_result::fetch_object() is not in the optimizer info and is not affected.

The merge into PHP-8.5 and PHP-8.6 conflicts only in the stub hash line of ext/mysqli/mysqli_arginfo.h; build/gen_stub.php regenerates it.

@kamil-tekiela

Copy link
Copy Markdown
Member

You might also want to open a PR to fix the same kind of bug in clone:

<?php
class C {
    public static array $all = [];
    public array $data = [];
    public function __clone() { self::$all[] = $this; }
    public function __destruct() { echo "destruct\n"; }
}

function viaFunc(C $c): void {
    clone($c, []);
}

viaFunc(new C);
echo "after viaFunc()\n";
for ($i = 0; $i < 3; $i++) { $o = new C; $o->data = range(1, 100); $keep[] = $o; }
var_dump(C::$all[0]->data);
echo "end\n";

It should target PHP 8.5.

@kamil-tekiela kamil-tekiela left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a destructor so the test doesn't rely on the debug+compiler combination.

Comment thread ext/mysqli/tests/gh25779.phpt Resolved
…tructor keeps a reference and the result is discarded

mysqli_fetch_object() calls the constructor on the returned object, and the
constructor can keep a reference to $this. The stub still declared
@refcount 1, which lets function JIT and tracing JIT destroy the object when
the result is discarded while that reference still uses it. Drop the
annotation.
@thunderer
thunderer force-pushed the fix/mysqli-fetch-object-refcount branch from 88110bb to 24bb1fa Compare November 26, 2026 17:56
@thunderer

Copy link
Copy Markdown
Contributor Author

@kamil-tekiela clone() is handled already in #25787.

@kamil-tekiela
kamil-tekiela merged commit 8084ab3 into php:PHP-8.4 Nov 26, 2026
18 checks passed
arnaud-lb added a commit to pavimus/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4: (91 commits)
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  ext/dom: fix Dom\Notation publicId and systemId property handlers
  Fix FFI::string() reading out of bounds for a negative size
  [ci skip] Update test title
  Fix phpGH-25558: FFI: struct with an enum field passed by value is described to libffi with a void element
  Fix OSS-Fuzz #588264417
  Fix FPM log overflow when errno text exceeds the buffer (phpGH-25648)
  [skip ci] Use USE_TRACKED_ALLOC=1 for Symfony Preloading test
  [skip ci] Fix Symfony test without USE_TRACKED_ALLOC
  [skip ci] Skip assign_dim_undef_op_data_008.phpt when JIT is unavailable
  ...
arnaud-lb added a commit to lazerg/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4: (262 commits)
  ext/pdo_sqlite: crash when a collation callback closes the statement cursor
  Admit NTS requests to OPcache SHM only in RINIT (php#25509)
  ext/pgsql: pg_meta_data() numeric column names not integer keys with opcache
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  ext/dom: fix Dom\Notation publicId and systemId property handlers
  Fix FFI::string() reading out of bounds for a negative size
  [ci skip] Update test title
  Fix phpGH-25558: FFI: struct with an enum field passed by value is described to libffi with a void element
  Fix OSS-Fuzz #588264417
  Fix FPM log overflow when errno text exceeds the buffer (phpGH-25648)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants