Commit graph

373 commits

Author SHA1 Message Date
Arnaud Le Blanc
6fa8a25a40
Prevent throwing in running generator
Generator::throw() on a running generator is not allowed. It throws "Cannot
resume an already running generator" when trying to resume the generator to
handle the provided exception.

However, when calling Generator::throw() on a generator with a non-Generator
delegate, we release the delegate regardless. If a Fiber was suspended in
the delegate, this causes use after frees when the Fiber is resumed.

Fix this by throwing "Cannot resume an already running generator" earlier.

Fixes GH-19326
Closes GH-19327
2025-07-31 14:26:41 +02:00
Arnaud Le Blanc
0406a55c92
Prevent resumption of generator suspended in yield from
Normally we prevent generators from being resumed while they are already
running, but we failed to do so for generators delegating to non-Generators. As
a result such generator can be resumed, terminated, which causes unexpected
results (crashes) later.

In gh19306.phpt in particular, the generator delegate It::getIterator() suspends
while being called by generator g(). We then resume g(), which throws while
trying to resume It::getIterator(). This causes g() and It::getIterator()
to be released. We then UAF when resuming the Fiber in It::getIterator().

Fix this by ensuring that generators are marked as running while they fetch
the next value from the delegate.

Fixes GH-19306
Closes GH-19315
2025-07-31 08:45:19 +02:00
Arnaud Le Blanc
7e6616cafb
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  [ci skip] NEWS for GH-16025
  Fix assertion failure in generator dtor (#16025)
2024-10-02 12:31:21 +02:00
Arnaud Le Blanc
6e55f4df23
Fix assertion failure in generator dtor (#16025) 2024-10-02 12:29:19 +02:00
Arnaud Le Blanc
f551a719dc
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  [ci skip] NEWS for GH-15330
  Do not scan generator frames more than once (#15330)
2024-08-28 17:44:41 +02:00
Arnaud Le Blanc
cd25500766
Do not scan generator frames more than once (#15330) 2024-08-28 17:43:17 +02:00
Arnaud Le Blanc
ba9f65bacd
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  [ci skip] NEWS for GH-15275
  Fix crash during GC of suspended generator delegate (#15275)
2024-08-10 15:48:43 +02:00
Arnaud Le Blanc
c767fec2d0
Fix crash during GC of suspended generator delegate (#15275) 2024-08-10 15:46:31 +02:00
Arnaud Le Blanc
e24101acb4
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  [ci skip] NEWS
  Fix destruction of generator running in fibers during shutdown (#15158)
2024-07-30 14:56:28 +02:00
Arnaud Le Blanc
99e0d3fe09
Fix destruction of generator running in fibers during shutdown (#15158)
The destructor of generators is a no-op when the generator is running in a fiber,
because the fiber may resume the generator. Normally the destructor
is not called in this case, but this can happen during shutdown.

We detect that a generator is running in a fiber with the
ZEND_GENERATOR_IN_FIBER flag.

This change fixes two cases not handled by this mechanism:

- The ZEND_GENERATOR_IN_FIBER flag was not added when resuming a "yield from $nonGenerator"

- When a generator that is running in a fiber has multiple children (aka multiple generators yielding from it), all of them could be considered to also run in a fiber (only one actually is), and could leak if not destroyed before shutdown.
2024-07-30 14:53:19 +02:00
Bob Weinand
d98586b6ed Merge branch 'PHP-8.2' into PHP-8.3 2024-06-01 02:41:39 +02:00
Bob Weinand
be7f3aa474 Fix GH-14387: Crash when stack walking in destructor of yielded from values during Generator->throw() 2024-06-01 02:38:55 +02:00
Niels Dossche
a44acde736 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-11028: Heap Buffer Overflow in zval_undefined_cv.
2023-04-15 18:34:13 +02:00
Niels Dossche
1799327356 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix GH-11028: Heap Buffer Overflow in zval_undefined_cv.
2023-04-15 18:30:47 +02:00
Niels Dossche
fc32d39b7f Fix GH-11028: Heap Buffer Overflow in zval_undefined_cv.
For analysis see https://github.com/php/php-src/issues/11028#issuecomment-1508460440

Closes GH-11083.
2023-04-15 18:22:55 +02:00
Bob Weinand
e8d16fda13 Merge branch 'PHP-8.2' 2023-02-13 13:10:44 +01:00
Bob Weinand
0e31e03bc5 Merge branch 'PHP-8.1' into PHP-8.2 2023-02-13 13:10:08 +01:00
Arnaud Le Blanc
d721dcc2ef Fix colletion of unfinished function call in fibers
Fixes GH-10496.

Co-authored-by: Bob Weinand <bobwei9@hotmail.com>
2023-02-13 13:09:04 +01:00
Bob Weinand
8076ca2bcf Merge branch 'PHP-8.2' 2023-01-30 12:33:23 +01:00
Bob Weinand
f732486c08 Merge branch 'PHP-8.1' into PHP-8.2 2023-01-30 12:33:06 +01:00
Bob Weinand
00be6e1aed Look at executing generator for fiber destructor behaviour 2023-01-30 12:32:53 +01:00
Bob Weinand
1679235f0f Merge branch 'PHP-8.2' 2023-01-30 12:13:52 +01:00
Bob Weinand
e746b89bc9 Merge branch 'PHP-8.1' into PHP-8.2 2023-01-30 12:13:41 +01:00
Bob Weinand
b9bca2dadb Fix resetting ZEND_GENERATOR_IN_FIBER flag
Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
2023-01-30 12:13:07 +01:00
Arnaud Le Blanc
479b47c6f0 Merge branch 'PHP-8.2'
* PHP-8.2:
  [ci skip] NEWS
  [ci skip] NEWS
  Fix overflow check in OnUpdateMemoryConsumption (#10456)
  Prevent dtor of generator in suspended fiber (#10462)
2023-01-27 19:40:16 +01:00
Arnaud Le Blanc
6c22dea3c1 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  [ci skip] NEWS
  Fix overflow check in OnUpdateMemoryConsumption (#10456)
  Prevent dtor of generator in suspended fiber (#10462)
2023-01-27 19:37:01 +01:00
Arnaud Le Blanc
1173c2e64a
Prevent dtor of generator in suspended fiber (#10462)
Generators that suspended a fiber should not be dtor because they will be
executed during the fiber dtor.

Fiber dtor throws an exception in the fiber's context in order to unwind and
execute finally blocks, which will also properly dtor the generator.

Fixes GH-9916
2023-01-27 19:32:25 +01:00
Arnaud Le Blanc
ba091ab3c7 Merge branch 'PHP-8.2'
* PHP-8.2:
  [ci skip] NEWS
  Reduce HT_MAX_SIZE to account for the max load factor of 0.5 (#10242)
  GC fiber unfinished executions (#9810)
2023-01-13 12:31:12 +01:00
Arnaud Le Blanc
d89408075f Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Reduce HT_MAX_SIZE to account for the max load factor of 0.5 (#10242)
  GC fiber unfinished executions (#9810)
2023-01-13 12:28:21 +01:00
Arnaud Le Blanc
4fb149390a
GC fiber unfinished executions (#9810) 2023-01-13 12:04:28 +01:00
Arnaud Le Blanc
02ed12240e Merge branch 'PHP-8.2'
* PHP-8.2:
  [ci skip] NEWS
  [ci skip] NEWS
  [ci skip] NEWS
  Fix generator memory leaks when interrupted during argument evaluation (#9756)
2022-11-04 16:06:42 +01:00
Arnaud Le Blanc
34a85fbea0 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  [ci skip] NEWS
  [ci skip] NEWS
  Fix generator memory leaks when interrupted during argument evaluation (#9756)
2022-11-04 16:02:04 +01:00
Arnaud Le Blanc
4011657719 Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  [ci skip] NEWS
  Fix generator memory leaks when interrupted during argument evaluation (#9756)
2022-11-04 15:59:14 +01:00
Arnaud Le Blanc
5d1f3e047c
Fix generator memory leaks when interrupted during argument evaluation (#9756) 2022-11-04 15:55:55 +01:00
Arnaud Le Blanc
1c1481bbb5 Merge branch 'PHP-8.2'
* PHP-8.2:
  [ci skip] NEWS
  [ci skip] NEWS
  [ci skip] NEWS
  Restore extra_named_params when restoring frozen call stack
2022-10-16 12:48:26 +02:00
Arnaud Le Blanc
a1295cbfce Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  [ci skip] NEWS
  [ci skip] NEWS
  Restore extra_named_params when restoring frozen call stack
2022-10-16 12:45:41 +02:00
Arnaud Le Blanc
2831e0c624 Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  [ci skip] NEWS
  Restore extra_named_params when restoring frozen call stack
2022-10-16 12:43:34 +02:00
Arnaud Le Blanc
86e1fea39a Restore extra_named_params when restoring frozen call stack 2022-10-16 12:40:09 +02:00
Bob Weinand
5a0b68bed7 Revert "Store default object handlers alongside the class entry"
This reverts commit 9e6eab3c13.

Reverted along a01dd9feda.
2022-09-14 11:18:14 +02:00
Bob Weinand
9e6eab3c13 Store default object handlers alongside the class entry
Object handlers being separate from class entries is a legacy inherited from PHP 5. Today it has little benefit to keep them separate: in fact, accessing object handlers usually requires not-so-safe hacks.
While it is possible to swap handlers in a custom installed create_object handler, this mostly is tedious, as well as it requires allocating the object handlers struct at runtime, possibly caching it etc..

This allows extensions, which intend to observe other classes to install their own class handlers.
The life cycle of internal classes may now be simply observed by swapping the class handlers in post_startup stage.
The life cycle of userland classes may be observed by iterating over the new classes in zend_compile_file and zend_compile_string and then swapping their handlers.

In general, this would also be a first step in directly tying the object handlers to classes. Especially given that I am not aware of any case where the object handlers would be different between various instances of a given class.

Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
2022-08-31 16:45:27 +02:00
Bob Weinand
ceda8a8c41 Merge branch 'PHP-8.1' 2022-04-04 14:25:42 +02:00
Bob Weinand
e0025562d0 Merge branch 'PHP-8.0' into PHP-8.1 2022-04-04 14:25:23 +02:00
Bob Weinand
9cb512ecc1 Ensure correct target opline for exceptions thrown during yield from
Also appends the exception during a yield from values dtor instead of prepending it

Fixing regression introduced in 13649451c2.
2022-04-04 14:24:39 +02:00
Bob Weinand
914c1ba1e7 Merge branch 'PHP-8.1' 2022-04-01 17:38:56 +02:00
Bob Weinand
16dcededa8 Merge branch 'PHP-8.0' into PHP-8.1 2022-04-01 17:38:10 +02:00
Bob Weinand
13649451c2 Fix GH-8289: Exceptions thrown within a yielded from iterator are not rethrown into the generator
This also fixes the fact that exception traces were not including the generator frame when thrown in a yielded from iterator.
2022-04-01 17:32:48 +02:00
Dmitry Stogov
83d96d6779 Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix use after free
2022-03-01 01:34:27 +03:00
Dmitry Stogov
ba6bb8579a Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Fix use after free
2022-03-01 01:34:17 +03:00
Dmitry Stogov
01702a851b Fix use after free
Fixes oss-fuzz #44885
2022-03-01 01:33:22 +03:00
Dmitry Stogov
90e4d5456b Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix memory leak
2022-02-18 12:21:48 +03:00