Commit Graph

220 Commits

Author SHA1 Message Date
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
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
Nikita Popov
73115ef873 Fixed bug #78589
Don't protect GC while destroying zvals. We may need to add GC
roots during this phase.
2019-09-24 12:17:21 +02:00
Nikita Popov
38c2230d07 Merge branch 'PHP-7.4' 2019-08-26 17:54:00 +02:00
Nikita Popov
fa8565a0f1 Merge branch 'PHP-7.3' into PHP-7.4 2019-08-26 17:52:56 +02:00
Nikita Popov
461db52400 Merge branch 'PHP-7.2' into PHP-7.3 2019-08-26 17:52:37 +02:00
Nikita Popov
589542f50c Remove properties HT from nested GC data
The properties HT may be a GC root itself, so we need to remove it.
I'm not sure this issue actually applies to PHP 7.2, but committing
it there to be safe. As seen from the test case, the handling here
is rather buggy on 7.2.
2019-08-26 17:49:37 +02:00
Nikita Popov
ded28e2435 Merge branch 'PHP-7.4' 2019-08-14 17:53:38 +02:00
Nikita Popov
c238b5bbef Fix reference printing in GC tracing 2019-08-14 17:51:41 +02:00
Nikita Popov
8c927442b4 Merge branch 'PHP-7.4' 2019-08-13 14:54:57 +02:00
Nikita Popov
60a7e60b61 Fixed bug #72530
For objects with destructors, we will now only call the destructor
in the initial GC run, and remove any nested data. The object is
marked purple so it will be considered a root for the next GC run,
at which point it will be fully destroyed, if possible.

GC counts change on a number of tests, as the objects now get
destroyed later.
2019-08-13 14:53:53 +02:00
Nikita Popov
1ae0b68404 Merge branch 'PHP-7.4' 2019-08-13 12:17:47 +02:00
Nikita Popov
72b7d99d0d Remove removed nested data from GC count 2019-08-13 12:17:08 +02:00
Nikita Popov
f0f3fe0b6c Don't include non-refcounted structures in GC count 2019-08-13 12:08:59 +02:00
Nikita Popov
9af705e753 Merge branch 'PHP-7.4' 2019-08-13 11:45:16 +02:00
Nikita Popov
7bd2b9d2e6 Generalize delref assertion
The refcount should never become negative, not just during GC.
2019-08-13 11:44:54 +02:00
Dmitry Stogov
172c71980d Merge branch 'PHP-7.4'
* PHP-7.4:
  Fixed handling of references in nested data of objects with destructor
2019-08-09 17:47:50 +03:00
Dmitry Stogov
4ebf527136 Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3:
  Fixed handling of references in nested data of objects with destructor
2019-08-09 17:47:35 +03:00
Dmitry Stogov
722a44d515 Merge branch 'PHP-7.2' into PHP-7.3
* PHP-7.2:
  Fixed handling of references in nested data of objects with destructor
2019-08-09 17:47:06 +03:00
Dmitry Stogov
9b43e29d9b Fixed handling of references in nested data of objects with destructor 2019-08-09 17:43:50 +03:00
Dmitry Stogov
a037a5bd33 Merge branch 'PHP-7.4'
* PHP-7.4:
  Fixed second part of the bug #78379 (Cast to object confuses GC, causes crash)
2019-08-09 15:58:44 +03:00
Dmitry Stogov
1456467cfe Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3:
  Fixed second part of the bug #78379 (Cast to object confuses GC, causes crash)
2019-08-09 15:58:33 +03:00
Dmitry Stogov
22d23e08c9 Merge branch 'PHP-7.2' into PHP-7.3
* PHP-7.2:
  Fixed second part of the bug #78379 (Cast to object confuses GC, causes crash)
2019-08-09 15:58:16 +03:00
Dmitry Stogov
6b1cc1252e Fixed second part of the bug #78379 (Cast to object confuses GC, causes crash) 2019-08-09 15:42:39 +03:00
Dmitry Stogov
9b973550b9 Merge branch 'PHP-7.4'
* PHP-7.4:
  Added asserts to catch GC errors when refcount goes below zero.
2019-08-09 13:40:56 +03:00
Dmitry Stogov
b2044cd93e Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3:
  Added asserts to catch GC errors when refcount goes below zero.
2019-08-09 13:40:39 +03:00
Dmitry Stogov
21a0a28fd0 Merge branch 'PHP-7.2' into PHP-7.3
* PHP-7.2:
  Added asserts to catch GC errors when refcount goes below zero.
2019-08-09 13:39:59 +03:00
Dmitry Stogov
2e2cd65d73 Added asserts to catch GC errors when refcount goes below zero. 2019-08-09 13:37:23 +03:00