[php-src] master: Merge branch 'PHP-8.3'
| From: | Arnaud Le Blanc | Date: | Mon, 25 Mar 2024 15:26:05 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.3' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-127849@lists.php.net to get a copy of this message | ||
Author: Arnaud Le Blanc (arnaud-lb)
Date: 2024-03-25T16:24:48+01:00
Commit: https://github.com/php/php-src/commit/c7ca3e5c2761174877845293bd760200132b882e
Raw diff: https://github.com/php/php-src/commit/c7ca3e5c2761174877845293bd760200132b882e.diff
Merge branch 'PHP-8.3'
* PHP-8.3:
[ci skip]
[ci skip]
Adjust GC threshold if num_roots is higher than gc_threshold after collection (#13758)
Changed paths:
A Zend/tests/gh13670_001.phpt
A Zend/tests/gh13670_002.phpt
A Zend/tests/gh13670_003.phpt
M Zend/zend_gc.c
Diff:
diff --git a/Zend/tests/gh13670_001.phpt b/Zend/tests/gh13670_001.phpt
new file mode 100644
index 000000000000..def8fa3f5f25
--- /dev/null
+++ b/Zend/tests/gh13670_001.phpt
@@ -0,0 +1,43 @@
+--TEST--
+GH-13670 001
+--FILE--
+<?php
+
+register_shutdown_function(function () {
+ global $shutdown;
+ $shutdown = true;
+});
+
+class Cycle {
+ public $self;
+ public function __construct() {
+ $this->self = $this;
+ }
+ public function __destruct() {
+ global $shutdown;
+ if (!$shutdown) {
+ new Cycle();
+ }
+ }
+}
+
+$defaultThreshold = gc_status()['threshold'];
+for ($i = 0; $i < $defaultThreshold+1; $i++) {
+ new Cycle();
+}
+
+$objs = [];
+for ($i = 0; $i < 100; $i++) {
+ $obj = new stdClass;
+ $objs[] = $obj;
+}
+
+$st = gc_status();
+
+if ($st['runs'] > 10) {
+ var_dump($st);
+}
+?>
+==DONE==
+--EXPECT--
+==DONE==
diff --git a/Zend/tests/gh13670_002.phpt b/Zend/tests/gh13670_002.phpt
new file mode 100644
index 000000000000..c822585e3632
--- /dev/null
+++ b/Zend/tests/gh13670_002.phpt
@@ -0,0 +1,66 @@
+--TEST--
+GH-13670 002
+--FILE--
+<?php
+
+register_shutdown_function(function () {
+ global $shutdown;
+ $shutdown = true;
+});
+
+class Cycle {
+ public $self;
+ public function __construct() {
+ $this->self = $this;
+ }
+}
+
+class Canary {
+ public $self;
+ public function __construct() {
+ $this->self = $this;
+ }
+ public function __destruct() {
+ global $shutdown;
+ if (!$shutdown) {
+ work();
+ }
+ }
+}
+
+function work() {
+ global $objs, $defaultThreshold;
+ new Canary();
+ // Create some collectable garbage so the next run will not adjust
+ // threshold
+ for ($i = 0; $i < 100; $i++) {
+ new Cycle();
+ }
+ // Add potential garbage to buffer
+ foreach (array_slice($objs, 0, $defaultThreshold) as $obj) {
+ $o = $obj;
+ }
+}
+
+$defaultThreshold = gc_status()['threshold'];
+$objs = [];
+for ($i = 0; $i < $defaultThreshold*2; $i++) {
+ $obj = new stdClass;
+ $objs[] = $obj;
+}
+
+work();
+
+foreach ($objs as $obj) {
+ $o = $obj;
+}
+
+$st = gc_status();
+
+if ($st['runs'] > 10) {
+ var_dump($st);
+}
+?>
+==DONE==
+--EXPECT--
+==DONE==
diff --git a/Zend/tests/gh13670_003.phpt b/Zend/tests/gh13670_003.phpt
new file mode 100644
index 000000000000..defd3da43867
--- /dev/null
+++ b/Zend/tests/gh13670_003.phpt
@@ -0,0 +1,68 @@
+--TEST--
+GH-13670 003
+--FILE--
+<?php
+
+register_shutdown_function(function () {
+ global $shutdown;
+ $shutdown = true;
+});
+
+class Cycle {
+ public $self;
+ public function __construct() {
+ $this->self = $this;
+ }
+}
+
+class Canary {
+ public $self;
+ public function __construct() {
+ $this->self = $this;
+ }
+ public function __destruct() {
+ global $shutdown;
+ if (!$shutdown) {
+ work();
+ }
+ }
+}
+
+function work() {
+ global $objs, $defaultThreshold;
+ new Canary();
+ // Create some collectable garbage so the next run will not adjust
+ // threshold
+ for ($i = 0; $i < 100; $i++) {
+ new Cycle();
+ }
+ // Add potential garbage to buffer
+ foreach (array_slice($objs, 0, $defaultThreshold) as $obj) {
+ $o = $obj;
+ }
+}
+
+$defaultThreshold = gc_status()['threshold'];
+$objs = [];
+for ($i = 0; $i < $defaultThreshold*2; $i++) {
+ $obj = new stdClass;
+ $objs[] = $obj;
+}
+
+work();
+
+// Result of array_slice() is a tmpvar that will be checked by
+// zend_gc_check_root_tmpvars()
+foreach (array_slice($objs, -10) as $obj) {
+ $o = $obj;
+}
+
+$st = gc_status();
+
+if ($st['runs'] > 10) {
+ var_dump($st);
+}
+?>
+==DONE==
+--EXPECT--
+==DONE==
diff --git a/Zend/zend_gc.c b/Zend/zend_gc.c
index 572e3b66fd43..3016ff8a1af0 100644
--- a/Zend/zend_gc.c
+++ b/Zend/zend_gc.c
@@ -608,7 +608,7 @@ static void gc_adjust_threshold(int count)
/* TODO Very simple heuristic for dynamic GC buffer resizing:
* If there are "too few" collections, increase the collection threshold
* by a fixed step */
- if (count < GC_THRESHOLD_TRIGGER) {
+ if (count < GC_THRESHOLD_TRIGGER || GC_G(num_roots) >= GC_G(gc_threshold)) {
/* increase */
if (GC_G(gc_threshold) < GC_THRESHOLD_MAX) {
new_threshold = GC_G(gc_threshold) + GC_THRESHOLD_STEP;
@@ -1990,7 +1990,13 @@ ZEND_API int zend_gc_collect_cycles(void)
finish:
zend_get_gc_buffer_release();
+
+ /* Prevent GC from running during zend_gc_check_root_tmpvars, before
+ * gc_threshold is adjusted, as this may result in unbounded recursion */
+ GC_G(gc_active) = 1;
zend_gc_check_root_tmpvars();
+ GC_G(gc_active) = 0;
+
GC_G(collector_time) += zend_hrtime() - start_time;
return total_count;
}