View Issue Details

IDProjectCategoryView StatusLast Update
0002445XdebugCode Coveragepublic2026-11-24 12:29
ReporterW0rma Assigned To 
PrioritynormalSeveritycrashReproducibilityalways
Status newResolutionopen 
Summary0002445: 3.6: Segfault in xdebug_branch_info_mark_reached when branch coverage is restarted inside a function (regression since 3.5.3)
Description

When code coverage with XDEBUG_CC_BRANCH_CHECK is stopped and started again, and the function that calls xdebug_start_code_coverage() reaches a branch right afterwards, Xdebug segfaults in xdebug_branch_info_mark_reached().

  • Expected: the script below prints "cycle 1 done" … "cycle 3 done" and "no crash".
  • Actual: it prints "cycle 1 done" and then segfaults (exit code 139).

This is exactly what phpunit/php-code-coverage's XdebugDriver::start() does (an if around the flags, then xdebug_start_code_coverage()), so any test suite with path/branch coverage and at least two tests can run into this. Whether it crashes depends on the stale value (see analysis below); otherwise it reads out of bounds silently. We found it in Codeception's test suite, where the second test always crashes.

Results with the script below:

Xdebug 3.5.3          PHP 8.5: works
Xdebug 3.6.0alpha1    PHP 8.6: segfault (via Codeception)
Xdebug master         PHP 8.5: segfault   PHP 8.6: segfault (via Codeception)
(master = 3.6.0alpha2-dev, as of commit 5e19866, 2026-11-18)
Steps To Reproduce

Run with XDEBUG_MODE=coverage php xdebug-repro.php:

<?php
function start(bool $branch): void
{
    xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE | XDEBUG_CC_BRANCH_CHECK);
    if ($branch) {
        $branch = false;
    }
}

$file = sys_get_temp_dir() . '/xdebug-repro-big.php';
file_put_contents($file, '<?php function big($x) { ' . str_repeat('$x++; ', 2000) . 'if ($x) { $x = 0; } return $x; }');

for ($i = 1; $i <= 3; $i++) {
    start(true);         // entered while coverage is off, hits a branch right after turning it on
    require_once $file;  // compiled while coverage is on -> Xdebug analyses all functions, incl. start()
    big(1);              // same stack depth as start(), leaves a high last_branch_nr behind
    xdebug_get_code_coverage();
    xdebug_stop_code_coverage();
    echo "cycle $i done\n";
}
echo "no crash\n";

The str_repeat() only makes the stale index large enough to segfault without tools. With a small big() (<?php function big($x) { $x++; if ($x) { $x = 0; } return $x; }) the invalid read still happens, but only shows up under valgrind/gdb with USE_ZEND_ALLOC=0.

Additional Information

Analysis (from reading src/coverage/branch_info.c and code_coverage.c on master using AI tools; please correct me if I'm wrong):

XG_COV(branches).last_branch_nr[level] is only reset to -1 in xdebug_code_coverage_start_of_function(), i.e. when a function is entered while coverage is active. xdebug_stop_code_coverage() does not reset it.

In the second cycle, start() was entered while coverage was off, so last_branch_nr[level] still holds the value that big() left behind at the same stack depth in the first cycle (an opcode number around 2000). Because start() was analysed in the first cycle (when the require_once'd file was compiled with coverage active, xdebug_prefill_code_coverage() analysed all functions), xdebug_branch_info_mark_reached() handles the branch in start() and uses the stale value as an index:

branch_info->branches[XG_COV(branches).last_branch_nr[level]].outs_count

start()'s branch_info only has a few entries, so this reads far out of bounds.

Why 3.5.3 is not affected: there, xdebug_stop_code_coverage() destroys XG_COV(code_coverage_info) completely, so in the next cycle start()'s file is unknown and xdebug_branch_info_mark_reached() returns early, before last_branch_nr is used. In 3.6 the coverage data is split into "analysis" and "runtime" parts, and xdebug_stop_code_coverage() only clears the runtime part (scrub_runtime). The analysis of start() survives the stop, so the stale last_branch_nr is now used.

Possible fixes: reset all last_branch_nr entries to -1 in xdebug_start_code_coverage() (or in the cleanup part of xdebug_stop_code_coverage()), and/or check last_branch_nr against the size of branch_info->branches before using it.

gdb bt full and valgrind output (USE_ZEND_ALLOC=0 ZEND_DONT_UNLOAD_MODULES=1) for this script are attached. Valgrind first reports an "Invalid read of size 4" in xdebug_branch_info_mark_reached at an address about 1 MB past a valid allocation (fits the outs_count read with the stale index), followed by an "Invalid read of size 8" at address 0x10, which causes the SIGSEGV.

Output of php -v:

PHP 8.5.13 (cli) (built: Nov 19 2026 14:29:14) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.13, Copyright (c) Zend Technologies
    with Xdebug v3.6.0alpha2-dev, Copyright (c) 2002-2026, by Derick Rethans
    with Zend OPcache v8.5.13, Copyright (c), by Zend Technologies
TagsNo tags attached.
Operating SystemDebian 13 (trixie), Docker image php:8.5-cli
PHP Version8.5.13

Activities

There are no notes attached to this issue.