array_multisort() segfaults and usort() calls another sort's comparator when Fibers suspend inside comparisons and resume out of order
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
Start in ext/standard/array.c at array_multisort() and PHP_ARRAY_CMP_FUNC_BACKUP()/PHP_ARRAY_CMP_FUNC_RESTORE(), where the issue identifies thread global sort state being saved and restored. Reproduce with the Fiber examples in the report, then inspect how other engine callbacks use zend_fiber_switch_block() and zend_fiber_switch_unblock(). Done means interleaved sorts no longer corrupt state: either suspension is rejected safely or each fiber retains its own sort state.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Description
array_multisort() and the usort() family keep the state of the sort in progress in thread globals: ARRAYG(multisort_func) and BG(user_compare_fci) / BG(user_compare_fci_cache). Each call saves the previous value on entry and restores it on return, which assumes calls nest. A Fiber suspended inside a comparison (in __toString(), a user comparator, or an error handler) breaks that assumption. If another fiber sorts in the meantime, and the two sorts finish in a different order than they started, the sort that is still running uses the other call's state.
array_multisort() crashes. The first sort returns and puts NULL back into ARRAYG(multisort_func). The second sort's next comparison then calls through it:
<?php
class S {
public function __construct(public string $v, public bool $park = false) {}
public function __toString(): string {
if ($this->park && Fiber::getCurrent()) { $this->park = false; Fiber::suspend(); }
return $this->v;
}
}
$a = [new S('c', true), new S('b'), new S('a'), new S('d')];
$b = [new S('z', true), new S('y'), new S('x'), new S('w')];
$fa = new Fiber(function () use (&$a) { array_multisort($a, SORT_STRING); echo "A done\n"; });
$fb = new Fiber(function () use (&$b) { array_multisort($b, SORT_STRING); echo "B done\n"; });
$fa->start(); echo "A parked\n";
$fb->start(); echo "B parked\n";
$fa->resume();
$fb->resume();
echo "survived\n";
Resulted in this output:
A parked
B parked
A done
Segmentation fault
(exit status 139)
But I expected this output instead:
A parked
B parked
A done
B done
survived
With three interleaved calls, a sort can also be left calling through another call's comparison table after that table has been freed.
usort() calls the other sort's comparator, then fails.
<?php
$mk = function (string $who) {
$first = true;
return function ($x, $y) use ($who, &$first) {
echo "cmp($who)\n";
if ($first && Fiber::getCurrent()) { $first = false; Fiber::suspend(); }
return $x <=> $y;
};
};
$a = [3, 1, 2]; $b = [30, 10, 20];
$fa = new Fiber(function () use (&$a, $mk) { usort($a, $mk('A')); echo "A done\n"; });
$fb = new Fiber(function () use (&$b, $mk) { usort($b, $mk('B')); echo "B done\n"; });
$fa->start(); $fb->start();
$fa->resume();
try { $fb->resume(); } catch (\Throwable $e) { echo get_class($e), ': ', $e->getMessage(), "\n"; }
echo json_encode([$a, $b]), "\n";
Resulted in this output (A's sort runs B's comparator, then B's sort finds no callback):
cmp(A)
cmp(B)
cmp(B)
cmp(B)
A done
Error: Invalid callback , no array or string given
[[1,2,3],[10,30,20]]
But I expected this output instead:
cmp(A)
cmp(B)
cmp(A)
...
A done
B done
[[1,2,3],[10,20,30]]
The save and restore are in ext/standard/array.c: array_multisort() (old_multisort_func / ARRAYG(multisort_func)) and PHP_ARRAY_CMP_FUNC_BACKUP() / PHP_ARRAY_CMP_FUNC_RESTORE(). The code is the same on current master.
Possible fixes:
- block fiber switching for the duration of the sort with
zend_fiber_switch_block()/zend_fiber_switch_unblock(), as some other engine callbacks already do. AFiber::suspend()inside a comparison would then throwFiberErrorinstead of corrupting the state; - or keep this state per fiber instead of per thread.
Reproduced on both NTS and ZTS builds (official Docker images php:8.5-cli-alpine, php:8.4-zts-alpine3.24, php:8.5-zts-alpine3.24).
PHP Version
PHP 8.5.12 (cli) (built: Nov 5 2026 18:54:25) (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 (cli) (built: Nov 5 2026 18:56:54) (ZTS)
Operating System
Alpine Linux 3.24 (Docker)
- Lingua principale
- C
- Stelle
- 41.8k
- Fork
- 8.6k
- Merge medio
- 2g 15h
- PR unite (30g)
- 125
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di php/php-src
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)ApertaExtension: openssl Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
php/php-src#25402 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Bug Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
php/php-src#25397 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
SNMP accepts malformed remote portsForse già presa @somethingwithproof l’ha presa 21 giorni fa. Aperta
Difficoltà 2/5 Mezza giornata Idoneità per principianti 62/100
php/php-src#25137 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Bug Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
I maintainer di solito rispondono entro 1 giorno
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
ZIP/ZIPS decode dereferences NULL when the cached libdeflate decompressor can't be allocated (3.5 regression)Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
AcademySoftwareFoundation/openexr#2884 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
python-pillow/Pillow#10590 ·
I maintainer di solito rispondono entro 1 giorno
-
status: needs-triage type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
openwrt/packages#31407 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno