Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ PHP 8.7 INTERNALS UPGRADE NOTES
from zend_is_callable(const zval *callable, uint32_t check_flags, zend_string **callable_name).
This new signature covers the most common parameters used with
zend_is_callable_ex().
. zend_array_destroy() and zend_objects_store_del() no longer assume that
the stack has room for one more nesting level. When the native stack is
within ZEND_GARBAGE_STACK_MARGIN (128 KiB) of the limit set by
zend.max_allowed_stack_size, the array or object is queued instead of being
destroyed, and that caller destroys the queue in a loop. The
array or object may therefore still be alive when these functions return,
and a queued object holds one reference until its destructor has run. This
avoids a stack overflow when releasing deeply nested arrays or objects
(GH-15869). It does not apply where the stack limit is not supported or is
disabled with zend.max_allowed_stack_size=-1. The executor globals gained
the deferred_garbage* fields for this.

- Added:
. The zend_object_copy() function was added to increases the refcount of a
Expand Down
44 changes: 44 additions & 0 deletions Zend/tests/stack_limit/gh15869_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
GH-15869 001: Releasing a deeply nested array does not overflow the stack
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
?>
--INI--
zend.max_allowed_stack_size=1M
memory_limit=256M
--FILE--
<?php

$before = memory_get_usage();

$a = [];
for ($i = 0; $i < 100000; $i++) {
$a = [$a];
}
echo "built\n";

$a = null;
echo "released\n";

// Nothing was left behind by the deferred release
var_dump(memory_get_usage() - $before < 1024);

// The same with a reference, a nested array and a string key in each level
$a = [];
for ($i = 0; $i < 100000; $i++) {
$a = ['x' => [&$a, 1], 'y' => 'z'];
}
echo "built\n";
unset($a);
echo "released\n";

?>
--EXPECT--
built
released
bool(true)
built
released
40 changes: 40 additions & 0 deletions Zend/tests/stack_limit/gh15869_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
GH-15869 002: Releasing a deep linked list of objects does not overflow the stack
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
?>
--INI--
zend.max_allowed_stack_size=1M
memory_limit=256M
--FILE--
<?php

class Node {
public $next = null;
}

$before = memory_get_usage();

$head = null;
for ($i = 0; $i < 100000; $i++) {
$node = new Node();
$node->next = $head;
$head = $node;
}
unset($node);
echo "built\n";

$head = null;
echo "released\n";

// Nothing was left behind by the deferred release, except the object store buckets
var_dump(memory_get_usage() - $before < 2 * 1024 * 1024);

?>
--EXPECT--
built
released
bool(true)
48 changes: 48 additions & 0 deletions Zend/tests/stack_limit/gh15869_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
GH-15869 003: Destructors of a deep linked list run from head to tail
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
?>
--INI--
zend.max_allowed_stack_size=1M
memory_limit=256M
--FILE--
<?php

class Node {
public static int $expected = 0;
public static int $outOfOrder = 0;
public static int $destructed = 0;
public $next = null;

public function __construct(public int $id) {}

public function __destruct() {
if ($this->id !== self::$expected++) {
self::$outOfOrder++;
}
self::$destructed++;
}
}

$count = 100000;
$head = null;
for ($i = $count - 1; $i >= 0; $i--) {
$node = new Node($i);
$node->next = $head;
$head = $node;
}
unset($node);

$head = null;

var_dump(Node::$destructed === $count);
var_dump(Node::$outOfOrder);

?>
--EXPECT--
bool(true)
int(0)
56 changes: 56 additions & 0 deletions Zend/tests/stack_limit/gh15869_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
GH-15869 004: Exceptions thrown by destructors of a deep list propagate and the rest is released
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
?>
--INI--
zend.max_allowed_stack_size=1M
memory_limit=256M
--FILE--
<?php

class Node {
public static int $destructed = 0;
public $next = null;

public function __construct(public int $id) {}

public function __destruct() {
self::$destructed++;
if ($this->id === 50000 || $this->id === 99999) {
throw new Exception("Node $this->id");
}
}
}

$count = 100000;
$head = null;
for ($i = $count - 1; $i >= 0; $i--) {
$node = new Node($i);
$node->next = $head;
$head = $node;
}
unset($node);

try {
$head = null;
echo "not reached\n";
} catch (Exception $e) {
// The second exception is chained to the first
echo get_class($e), ': ', $e->getMessage(), "\n";
echo get_class($e->getPrevious()), ': ', $e->getPrevious()->getMessage(), "\n";
var_dump($e->getPrevious()->getPrevious());
}

// Every destructor ran, despite the exceptions
var_dump(Node::$destructed === $count);

?>
--EXPECT--
Exception: Node 99999
Exception: Node 50000
NULL
bool(true)
78 changes: 78 additions & 0 deletions Zend/tests/stack_limit/gh15869_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--TEST--
GH-15869 005: An object resurrected by its destructor while its release is deferred
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
?>
--INI--
zend.max_allowed_stack_size=1M
memory_limit=256M
--FILE--
<?php

class Node {
public static int $destructed = 0;
public static int $resurrect = -1;
public static $saved = null;
public $next = null;

public function __construct(public int $id) {}

public function __destruct() {
self::$destructed++;
if ($this->id === self::$resurrect) {
self::$saved = $this;
}
}
}

function build(int $count) {
$head = null;
for ($i = $count - 1; $i >= 0; $i--) {
$node = new Node($i);
$node->next = $head;
$head = $node;
}
return $head;
}

$count = 100000;

echo "Resurrect the tail\n";
Node::$resurrect = $count - 1;
$head = build($count);
$head = null;
var_dump(Node::$destructed);
var_dump(Node::$saved->id, Node::$saved->next);
Node::$saved = null;
// The destructor is not called a second time
var_dump(Node::$destructed);

echo "Resurrect a node in the middle, together with the rest of the list\n";
Node::$destructed = 0;
Node::$resurrect = 60000;
$head = build($count);
$head = null;
var_dump(Node::$destructed);
$length = 0;
for ($n = Node::$saved; $n !== null; $n = $n->next) {
$length++;
}
unset($n);
var_dump($length);
Node::$saved = null;
var_dump(Node::$destructed);

?>
--EXPECT--
Resurrect the tail
int(100000)
int(99999)
NULL
int(100000)
Resurrect a node in the middle, together with the rest of the list
int(60001)
int(40000)
int(100000)
58 changes: 58 additions & 0 deletions Zend/tests/stack_limit/gh15869_006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
GH-15869 006: Releasing deeply nested arrays and objects mixed together
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
?>
--INI--
zend.max_allowed_stack_size=1M
memory_limit=256M
--FILE--
<?php

class Node {
public static int $destructed = 0;
public $next = null;
public $items = [];

public function __destruct() {
self::$destructed++;
}
}

$count = 100000;
$objects = 0;
$x = null;
for ($i = 0; $i < $count; $i++) {
switch ($i % 4) {
case 0:
$x = [$x];
break;
case 1:
$node = new Node();
$node->next = $x;
$x = $node;
$objects++;
break;
case 2:
$node = new Node();
$node->items = ['a' => $x, 'b' => 1];
$x = $node;
$objects++;
break;
case 3:
$x = ['k' => [$x, 'v']];
break;
}
}
unset($node);

$x = null;
var_dump(Node::$destructed === $objects, $objects);

?>
--EXPECT--
bool(true)
int(50000)
Loading
Loading