Commit graph

246 commits

Author SHA1 Message Date
Niels Dossche
3dcf36c701
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fixed bug GH-13193 again
2025-03-21 11:48:22 +01:00
Niels Dossche
e98e4e39a3
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fixed bug GH-13193 again
2025-03-21 11:48:16 +01:00
Niels Dossche
447d143b9d
Fixed bug GH-13193 again
Closes GH-13193.
2025-03-21 11:47:52 +01:00
Ilija Tovilo
e69317b501
Reduce gc stack usage for strings (and resources)
Adding strings to the worklist is useless, because they never contribute to
cycles. The assembly size on x86_64 does not change. This significantly improves
performance in this synthetic benchmark by 33%.

    function test($a) {}

    $a = new stdClass();
    $a->self = $a;
    $a->prop1 = str_repeat('a', 10);
    $a->prop2 = str_repeat('a', 10);
    $a->prop3 = str_repeat('a', 10);
    $a->prop4 = str_repeat('a', 10);
    $a->prop5 = str_repeat('a', 10);
    $a->prop6 = str_repeat('a', 10);
    $a->prop7 = str_repeat('a', 10);
    $a->prop8 = str_repeat('a', 10);
    $a->prop9 = str_repeat('a', 10);
    $a->prop10 = str_repeat('a', 10);

    for ($i = 0; $i < 10_000_000; $i++) {
        test($a);
        gc_collect_cycles();
    }

This requires adding IS_TYPE_COLLECTABLE to IS_REFERENCE_EX to ensure these
values continue to be pushed onto the stack. Luckily, IS_TYPE_COLLECTABLE is
currently only used in gc_check_possible_root(), where the checked value cannot
be a reference.

Note that this changes the output of gc_collect_cycles(). Non-cyclic, refcounted
values no longer count towards the total reported values collected.

Also, there is some obvious overlap with GH-17130. This change should be good
nonetheless, especially if we can remove the GC_COLLECTABLE(Z_COUNTED_P(zv))
condition in PHP 9 and rely on Z_COLLECTABLE_P() exclusively, given we can
assume an object doesn't become cyclic at runtime anymore.

Closes GH-17194
2024-12-18 18:58:41 +01:00
Arnaud Le Blanc
d093c10caf
Fix reuse of dtor fiber during shutdown (#16026) 2024-10-02 12:11:10 +02:00
Arnaud Le Blanc
3c56af9902
Allow fiber switching during destructor execution
Fiber switching was disabled during destructor execution due to conflicts
with the garbage collector. This unfortunately introduces a function color
problem: destructors can not call functions that may switch Fibers.

In this change we update the GC so that Fiber switching during GC is safe. In
turn we allow Fiber switching during destrutor execution.

The GC executes destructors in a dedicated Fiber. If a destructor suspends, the
Fiber is owned by userland and a new dedicated Fiber is created to execute the
remaining destructors. Destructor suspension results in a resurection of the
object, which is handled as usual: The object is not considered garbage anymore,
but may be collected in a later run.

When the GC is executed in the main context (not in a Fiber), then destructors
are executed in the main context as well because there is no risk of conflicting
with GC in this case (main context can not suspend).

Fixes GH-11389
Closes GH-13460
2024-07-02 15:00:40 +02:00
Arnaud Le Blanc
9a51a7fb30 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  [ci skip]
  Adjust GC threshold if num_roots is higher than gc_threshold after collection (#13758)
2024-03-25 16:22:54 +01:00
Arnaud Le Blanc
c13794cdcb
Adjust GC threshold if num_roots is higher than gc_threshold after collection (#13758)
This fixes an edge case causing the GC to be triggered repeatedly.

Destructors might add potential garbage to the buffer, so it may happen that num_root it higher than gc_threshold after collection, thus triggering a GC run almost immediately. This can happen by touching enough objects in a destructor, e.g. by iterating over an array. If this happens again in the new run, and the threshold is not updated, the GC may be triggered again.

The edge case requires specific conditions to be triggered and it must happen rarely in practice:

 * At least GC_THRESHOLD_TRIGGER (100) objects must be collected during each run for the threshold to not be updated
 * At least GC_G(gc_threshold) (initially 10k) objects must be touched (decref'ed to n>0) by any destructor during each run to fill the buffer

The fix is to increase the threshold if GC_G(num_roots) >= GC_G(gc_threshold) after GC. The threshold eventually reaches a point at which the second condition is not met anymore.

The included tests trigger more than 200 GC runs before the fix, and 2 after the fix (dtors always trigger a second run).

A related issue is that zend_gc_check_root_tmpvars() may add potential garbage before the threshold is adjusted, which may trigger GC and exhaust the stack. This is fixed by setting GC_G(active)=1 around zend_gc_check_root_tmpvars().
2024-03-25 16:17:54 +01:00
Arnaud Le Blanc
6b57e2d988 Fix GH-13569: GC buffer unnecessarily grows up to GC_MAX_BUF_SIZE when scanning WeakMaps 2024-03-05 12:17:21 +01:00
Dmitry Stogov
49f85c24da Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-13193: Significant performance degradation in 'foreach' starting from PHP 8.2.13 (caused by garbage collection) (#13265)
2024-01-30 06:40:44 +03:00
Dmitry Stogov
397d4c244d
Fix GH-13193: Significant performance degradation in 'foreach' starting from PHP 8.2.13 (caused by garbage collection) (#13265)
* Fix GH-13193: Significant performance degradation in 'foreach' starting from PHP 8.2.13 (caused by garbage collection)

* Don't run zend_gc_remove_root_tmpvars() if GC is not active or GC buffer is empty
2024-01-30 06:38:30 +03:00
Dmitry Stogov
004d8951ce Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix memory leak after GC inside a foreach loop (#12572)
2023-10-30 23:36:50 +03:00
Dmitry Stogov
d906d8b5ea Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix memory leak after GC inside a foreach loop (#12572)
2023-10-30 23:36:41 +03:00
Dmitry Stogov
abe3673d1f
Fix memory leak after GC inside a foreach loop (#12572)
Fixes oss-fuzz #54515
2023-10-30 23:36:12 +03:00
Arnaud Le Blanc
cbf67e4fee
Remove WeakMap entries whose key is only reachable through the entry value (#10932) 2023-07-16 13:39:08 +02:00
Arnaud Le Blanc
d0731934b7
Expose time spent collecting cycles in gc_status() (#11523) 2023-07-16 12:34:28 +02:00
Patrick Allaert
973e9b2eec
Fixes "GC_BENCH" is not defined in extensions including zend_gc.h
Compilation warning encountered:

include/php/Zend/zend_gc.h:49:5: warning: "GC_BENCH" is not defined, evaluates to 0 [-Wundef]
   49 | #if GC_BENCH
      |     ^~~~~~~~
2023-06-22 17:40:15 +02:00
Ilija Tovilo
6f1e5ff8c3
Fix GC_BENCH flag (#10823)
zend_gc_globals is now hidden, so we can't access it from zend.c.
2023-03-10 15:02:22 +01:00
Niels Dossche
9f02a11846
Fix incorrect UNEXPECTED paren placement in zend_gc.c
Closes GH-10371.
2023-01-18 19:42:20 +01:00
Christoph M. Becker
bf1cfc0753
Revert GH-10300
Cf. <https://github.com/php/php-src/pull/10220#issuecomment-1383739816>.

This reverts commit 68ada76f9a.
his reverts commit 45384c6e20.
This reverts commit ef7fbfd710.
This reverts commit 9b9ea0d7c6.
This reverts commit f15747c26b.
This reverts commit e883ba93c4.
This reverts commit 7e87551c37.
This reverts commit 921274d2b8.
This reverts commit fc1f528e5e.
This reverts commit 0961715cda.
This reverts commit a93f264526.
This reverts commit 72dd94e1c6.
This reverts commit 29b2dc8964.
This reverts commit 05c7653bba.
This reverts commit 5190e5c260.
This reverts commit 6b55bf228c.
This reverts commit 184b4a12d3.
This reverts commit 4c31b7888a.
This reverts commit d44e9680f0.
This reverts commit 4069a5c43f.
2023-01-16 12:22:54 +01:00
Max Kellermann
e883ba93c4 Zend/zend_gc: include cleanup 2023-01-15 15:07:58 +00:00
Tim Starling
b8811d4ff1
Add four extra fields to gc_status() (#9336)
- running: true if garbage collection is currently running
- protected: true if the garbage collector is protected and root
  additions are forbidden
- full: true if the garbage collector buffer size exceeds GC_MAX_BUF_SIZE
- buffer_size: current garbage collector buffer size

Documentation for existing fields:

- runs: the number of times the garbage collector has been run
- collected: the number of objects collected
- threshold: the number of roots in the buffer which will trigger
  garbage collection
- roots: the current number of roots in the buffer

Updated manual example output:

array(8) {
  ["running"]=>
  bool(false)
  ["protected"]=>
  bool(false)
  ["full"]=>
  bool(false)
  ["runs"]=>
  int(5)
  ["collected"]=>
  int(100002)
  ["threshold"]=>
  int(50001)
  ["buffer_size"]=>
  int(131072)
  ["roots"]=>
  int(0)
}
2022-10-04 13:56:02 +01:00
Christoph M. Becker
31a99331c1
Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix GH-9266: GC root buffer keeps growing when dtors are present
2022-08-09 14:16:35 +02:00
Michael Olšavský
0709578517
Fix GH-9266: GC root buffer keeps growing when dtors are present
Do not reset cleared count on GC rerun.

Closes GH-9265.
2022-08-09 14:16:11 +02:00
Patrick Allaert
8c60e21515
Avoid possible [-Wstrict-prototypes] build warnings 2022-02-24 16:14:47 +01:00
Dmitry Stogov
4b4b9edcc0 Refactor GC data traversal loops
Previusly, to redce stack usage GC traversed object properties and array
elements in both directions. At first, it seeked backward from the last
element, to find the last refcounted zval. Then, it traversed elements
forward. As result, we had 2 non-spatial memory accesses for each
object/array and usuall 2 expensive CPU cache misses.

Now all the traversal algorithms are refactored to scan elements only in
forward direction. The number of checks and GC stack usage should be the
same. The order of element processing may be differenr, but this
should not be a problem.
2021-11-24 20:38:00 +03:00
Dmitry Stogov
90b7bde615 Use more compact representation for packed arrays.
- for packed arrays we store just an array of zvals without keys.
- the elements of packed array are accessible throuf as ht->arPacked[i]
  instead of ht->arData[i]
- in addition to general ZEND_HASH_FOREACH_* macros, we introduced similar
  familied for packed (ZEND_HASH_PACKED_FORECH_*) and real hashes
  (ZEND_HASH_MAP_FOREACH_*)
- introduced an additional family of macros to access elements of array
  (packed or real hashes) ZEND_ARRAY_ELEMET_SIZE, ZEND_ARRAY_ELEMET_EX,
  ZEND_ARRAY_ELEMET, ZEND_ARRAY_NEXT_ELEMENT, ZEND_ARRAY_PREV_ELEMENT
- zend_hash_minmax() prototype was changed to compare only values

Because of smaller data set, this patch may show performance improvement
on some apps and benchmarks that use packed arrays. (~1% on PHP-Parser)

TODO:
    - sapi/phpdbg needs special support for packed arrays (WATCH_ON_BUCKET).
    - zend_hash_sort_ex() may require converting packed arrays to hash.
2021-11-03 15:18:26 +03:00
Javier Eguiluz
ffc8717401
Fix some typos (#7320) 2021-07-31 08:34:57 +02:00
Nikita Popov
5bde82a442 Clean up gc_scan() implementation
The HT handling no longer needs to be shared, so move it into the
right branch. Also use a couple of early gotos to reduce nesting.
2021-07-12 11:49:12 +02:00
Nikita Popov
5f8ed7765a Fix GC of object properties HT
We partially fixed this in bug #78379, but still don't handle
the case where the properties array is marked as grey first,
which causes a delref to not be performed later.

Fix this by treating the object properties HT the same way as
other refcounted values, including addrefs/delrefs. The object
dtor code already handles properties HT with NULL GC type, so
out of order destruction should not be a problem.

Fixes oss-fuzz #36023.
2021-07-12 11:30:20 +02:00
Nikita Popov
52cf7ab8a2 Fix bug #80072: Root live tmpvars after GC
TMPVAR operands are destroyed using zval_ptr_dtor_nogc(), because
they usually cannot contain cycles. However, there are some rare
exceptions where this is possible, e.g. unserialize() return value.
In such cases we rely on the producing code to root the value. If
a GC run occurs between the rooting and consumption of the value,
we would end up leaking it. To avoid this, root all live TMPVAR
values after a GC run.

Closes GH-7210.
2021-07-02 15:28:36 +02:00
Patrick Allaert
ceb6fa6dc0 Convert some recently introduced zend_bool to bool
As well as `scripts/dev/check_parameters.php` utility.

Cfr. 3e01f5afb1
2021-06-18 15:21:39 +01:00
Aaron Piotrowski
fdc22744a8
Add API to prevent Fiber switch in select contexts
Co-authored-by: Martin Schröder <m.schroeder2007@gmail.com>
2021-06-14 14:19:00 -05:00
Nikita Popov
b58d74547f Rerun GC if destructors encountered
Since PHP 7.4 objects that have a destructor require two GC runs
to be collected. Currently the collection is delayed to the next
automatic GC run. However, in some cases this may result in a large
increase in memory usage, as in one of the cases of bug #79519.

See also bug #78933 and bug #81117 where the current behavior is
unexpected for users.

This patch will automatically rerun GC if destructors were encountered.
I think this should not have much cost, because it is very likely that
objects on which the destructor has been called really are garbage,
so the extra GC run should not be doing wasted work.

Closes GH-5581.
2021-06-09 14:53:14 +02:00
Nikita Popov
0643301c75 Don't perform recursive get_gc call
On further consideration, we should be making use of the fact
that zend_object_iterator is also a zend_object here, and let
GC handle the get_gc call on it. Calling get_gc recursively like
this is generally not safe, because there is only one gc_buffer.

This also happens to be much simpler...
2021-06-09 11:15:59 +02:00
Nikita Popov
15fafcd664 Expose inner dual_it iterator to GC
Moving the zend_iterator_dtor from dual_it_dtor to dual_it_free_storage
exposed this GC leak in an existing test. Forward the result of the
iterator get_gc to the dual_it get_gc.
2021-06-08 16:55:22 +02:00
George Peter Banyard
c40231afbf
Mark various functions with void arguments.
This fixes a bunch of [-Wstrict-prototypes] warning,
because in C func() and func(void) have different semantics.
2021-05-12 14:55:53 +01:00
Nikita Popov
3e01f5afb1 Replace zend_bool uses with bool
We're starting to see a mix between uses of zend_bool and bool.
Replace all usages with the standard bool type everywhere.

Of course, zend_bool is retained as an alias.
2021-01-15 12:33:06 +01:00
Dmitry Stogov
9fc11762e5 PHP array cannot refer to EG(symbol_table) any more. Replace corresponding checks by ZEND_ASSERT(). 2021-01-11 18:26:01 +03:00
Nikita Popov
0fb2374e06 Make GC default threshold handling consistent
While the initial threshold is set to 10001 roots, the threshold
adjustment logic may then set it to 10000. The exact value really
doesn't matter, but we should make it consistent.
2020-10-23 10:30:07 +02:00
Dmitry Stogov
bb3d4456ee Change GC_COLLECTABLE flag into GC_NOT_COLLECTABLE to simplify GC_MAY_LEAK() check 2020-06-15 14:26:22 +03:00
Nikita Popov
50c87e92fc Use GC stack in nested data removal
We should be doing this anyway to prevent stack overflow, but on
master this is important for an additional reason: The temporary
GC buffer provided for get_gc handlers may get reused if the scan
is performed recursively instead of indirected via the GC stack.

This fixes oss-fuzz #23350.
2020-06-12 15:02:12 +02:00
Nikita Popov
4a7ec516e0 Move label to correct position 2020-06-12 10:44:37 +02:00
Nikita Popov
0949214ab3 Fix null pointer UB in GC
This is just plain stupid: In C, it is not permitted to add zero
to a null pointer. In C++, it is permitted.
2020-06-12 10:33:39 +02:00
twosee
1b85e749c7 Fix warning of strict-prototypes
Closes GH-5673.
2020-06-07 10:36:50 +02:00
Nikita Popov
48a34bc120 Add helper APIs for get_gc implementations
get_gc() implementations that need to explore heterogeneous data
currently work by computing how many GC entries they need,
allocating a buffer for that and storing it on the object. This
is inefficient and wastes memory, because the buffer is retained
after the GC run.

This commit adds an API for a single global GC buffer, which can
be reused by get_gc implementations (as only one get_gc call is
ever active at the same time). The GC buffer will automatically
grow during the GC run and be discarded at the end.
2020-04-27 10:48:22 +02:00
Tyson Andre
500ba8b2b8 Handle reallocated root buffer during GC destroy phase (v2)
We no longer protect GC during the destroy phase, so we need to
deal with buffer reallocation.

Note that the implementation of spl_SplObjectStorage_free_storage
will call the destructor of SplObjectStorage, and free the instance properties,
which I think is what caused the root buffer to be reallocated.
(`current` is a pointer for an index within the root buffer?)

This fixes bug #78811 for me.

Closes GH-4935
2019-11-23 10:24:48 -05:00
Nikita Popov
9899fdc454 Merge branch 'PHP-7.4'
* PHP-7.4:
  Handle reallocated root buffer during GC destroy phase
  Zend Engine version is no longer in -dev
2019-11-15 15:54:46 +01:00
Nikita Popov
3f4a15113c Handle reallocated root buffer during GC destroy phase
We no longer protect GC during the destroy phase, so we need to
deal with buffer reallocation.

Possible fix for bug #78811.
2019-11-15 15:53:49 +01:00
Nikita Popov
3b52307c86 Merge branch 'PHP-7.4' 2019-09-24 12:19:14 +02:00