Commit Graph

48 Commits

Author SHA1 Message Date
Arnaud Le Blanc
4fb149390a
GC fiber unfinished executions (#9810) 2023-01-13 12:04:28 +01:00
Niels Dossche
833b45ac44 Fix GH-10249: Assertion `size >= page_size + 1 * page_size' failed.
Co-authored-by: Changochen <changochen1@gmail.com>

Closes GH-10284
2023-01-12 22:00:00 +00:00
Chen
040a37d3f1
Add IBT support for fiber
Indirect Branch Tracking (IBT) is part of Intel's Control-Flow
Enforcement Technology (CET). IBT is hardware based, forward edge
Control-Flow-Integrity mechanism where any indirect CALL/JMP must target
an ENDBR instruction or suffer #CP.

This commit adds IBT support for fiber:
1. Add endbr32/64 in assembly
2. Inform compiler jump_fcontext may return via indirect branch

Furthermore:
gcc support CET since v8.1 and set it to default since gcc 11. That is,
the ELF header of sapi/cli/php has a property named IBT. However, such
property is lost since PHP8.1 because the assembly introduced by Fiber.
This commit also fixes this.

Closes GH-8339

Signed-off-by: Chen, Hu <hu1.chen@intel.com>
Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
2022-05-25 13:42:31 -05:00
Petr Sumbera
069bbf3e80
Fix zend_fibers.c build with ZEND_FIBER_UCONTEXT
Avoids (Solaris SPARC) issue:
  zend_fibers.c:77:9: error: unknown type name 'ucontext_t'

Closes GH-7773.
2021-12-20 14:21:50 +01:00
Nikita Popov
578b785ecf Don't return reference from Fiber::getReturn()
Even if the fiber function returns by reference, we must return
a value from Fiber::getReturn() to satisfy the function signature.

Fixes oss-fuzz #36417.
2021-07-22 09:44:19 +02:00
Joe Watkins
848b5458d1
more fiber notifications (#7293) 2021-07-21 17:18:15 +02:00
Joe Watkins
576655e23f
drop unused header in fibers 2021-07-20 22:22:53 +02:00
Nikita Popov
814a932734 Add ZEND_ACC_NOT_SERIALIZABLE flag
This prevents serialization and unserialization of a class and its
children in a way that does not depend on the zend_class_serialize_deny
and zend_class_unserialize_deny handlers that will be going away
in PHP 9 together with the Serializable interface.

In stubs, `@not-serializable` can be used to set this flag.

This patch only uses the new flag for a handful of Zend classes,
converting the remainder is left for later.

Closes GH-7249.
Fixes bug #81111.
2021-07-19 15:59:11 +02:00
Aaron Piotrowski
8fd747a2a0
Fiber ucontext support (#7226)
Co-authored-by: Martin Schröder <m.schroeder2007@gmail.com>
2021-07-11 15:40:11 -05:00
Patrick Allaert
aff365871a Fixed some spaces used instead of tabs 2021-06-29 11:30:26 +02:00
Aaron Piotrowski
66442a51d1
Allow creating Graceful/UnwindExit and use when destroying a fiber (#7174)
Direct creation of GracefulExit allows the the special exception object to be transfered and thrown into a destroyed fiber using the same path as any other exception thrown into a fiber instead of needing to check for a flag.
2021-06-28 15:23:34 -05:00
Martin Schröder
7713302fbd Implemented Fiber GC handler 2021-06-28 13:08:48 +02:00
Aaron Piotrowski
15dcdd7672
Remove copying of fiber result to transfer value
Removes unnecessary copying of fiber return value to transfer value. This zval was not used as the return of start/resume/throw, instead being destroyed when the fiber was dead. Now the zval initialized to NULL when starting the fiber is maintained as the transfer value and is subsequently returned from start/resume/throw.
2021-06-18 23:38:02 -05:00
Aaron Piotrowski
f71d62e3d2
Fiber cleanup
Add zend_fiber prefix to delegate_transfer_result().

Ensure status is set to INIT when initializing the fiber context in case memory is not zeroed.

Assert destination fiber context is not dead.

Update stack alloc failure messages.

getThis() -> ZEND_THIS
2021-06-18 23:37:35 -05:00
Aaron Piotrowski
865fb353f5
Rename Fiber::this() to Fiber::getCurrent() (#7155) 2021-06-18 11:02:02 -05:00
Martin Schröder
c5f9cdedd6
Flexible fiber bailout handling (#7163) 2021-06-18 11:01:10 -05:00
Martin Schröder
d4ead60672
Do not expose fiber VM state management (#7170) 2021-06-18 08:04:31 +02:00
Aaron Piotrowski
5d4f05527b
Drop fiber block hooks
Removes the ability for an extension to allow Fiber switching in blocked contexts.

See discussion on fdc22744a8.
2021-06-15 08:58:07 -05:00
Joe Watkins
ba3c8027bc
Adds zend_fiber_startup at the right stage, moves setting of switch hooks
to correct stage

In a threaded environment, these ought to be set before any threads are
created, and they ought to be set once.
2021-06-15 09:56:15 +02:00
Joe Watkins
f46063495d
reset blocking state on init 2021-06-15 09:41:36 +02:00
Aaron Piotrowski
859524c246
Remove fiber context embedding 2021-06-14 14:34:36 -05: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
Martin Schröder
039f0fec58
Destroy previous fiber context in trampoline as needed (#7143) 2021-06-11 23:40:19 -05:00
Aaron Piotrowski
7df3fd9d55
Change stack field to a pointer in fiber context
The variable size of zend_fiber_stack results in the offset of other fields to be variable, which causes compatiblity issues with extensions when php-src is compiled with ASan enabled. This solution was prefered over moving the stack field to be the last member, as inclusion of ZEND_FIBER_CONTEXT_FIELDS into other structs may still result in field offset errors.

The definition of zend_fiber_stack was removed from the header to hide it from the ABI.

Renamed prior_pointer and prior_size to asan_pointer and asan_size to reflect their current use.

Changed context flags type to uint8_t.

Renamed valgrind stack id field to valgrind_stack_id and fixed the type to unsigned int.
2021-06-11 16:05:42 -05:00
Aaron Piotrowski
e6e6b3e68b
Improve fiber interoperability (#7128) 2021-06-10 23:00:07 -05:00
Joe Watkins
ef5de9fb3f
ditch function from zend_fiber 2021-06-10 15:40:43 +02:00
Martin Schröder
fa3d19830c
Unify control & data transfer between fibers (#7120)
Co-authored-by: Aaron Piotrowski <aaron@trowski.com>
2021-06-09 11:20:13 -05:00
Aaron Piotrowski
2184422adc
Merge fiber switching functions (#7106)
Co-authored-by: Martin Schröder <m.schroeder2007@gmail.com>
2021-06-08 08:23:56 -05:00
Martin Schröder
b76a9dbcf9
Add fiber type to better support custom fiber APIs (#7105) 2021-06-07 14:21:05 -05:00
twosee
e2e770c442
Fix undefined return value of fiber after bailout (#7103) 2021-06-05 07:20:47 -05:00
Aaron Piotrowski
42dbd1f09e
Improve Fiber::getReturn()
Allows flags besides ZEND_FIBER_FLAG_THREW to be set without causing this method to throw.

The method will not be called after a bailout, so the ZEND_FIBER_FLAG_BAILOUT case was removed.
2021-06-04 23:55:08 -05:00
Martin Schröder
a65989b127
Alternative Fiber Internals Refactoring (#7101) 2021-06-04 23:25:35 -05:00
Aaron Piotrowski
d2e5203865
Split fiber status and flags (#7094)
Co-Authored-By: twosee <twose@qq.com>
2021-06-03 15:34:48 -05:00
twosee
7e11b4d325
Improve memory allocation related code in zend_fibers (#7058)
- make_fcontext() never returns NULL.
- Show syscall error info in exception just like zend_alloc does (we also used php_win32_error_*() in Zend).
- MAP_ANON was marked as deprecated or compatibility macro on Linux, It would be better to use MAP_ANONYMOUS first.
- Makes the code consistent with the code of other modules in the kernel.
- adds some comments.
2021-05-28 12:04:44 +08:00
twosee
795efd7613
Add zend_getpagesize() and reuse it in accelerator and fiber (#7057) 2021-05-27 18:27:18 +08:00
Aaron Piotrowski
3939c9b07e
Add internal Fiber API (#7045)
This additional internal fiber API creates and manipulates a Fiber object, allowing any internal function to start, resume, or suspend a fiber. The existing zend_fiber_context API allows custom C-based fiber creation using the bundled switching context, but does not interact with the PHP VM. This API behaves the same as calling Fiber object methods from user code, switching EGs, and triggering the fiber switch observer. In general, the Fiber object methods call these new API methods.
2021-05-27 08:33:40 +02:00
twosee
0e2ac303af
Use ZEND_WIN32 instead of PHP_WIN32 (#7055) 2021-05-27 08:32:40 +02:00
Aaron Piotrowski
3c3ec441e4
Move fiber stack allocation within try
If VM stack allocation fails, the bailout will be caught within the fiber.
2021-05-08 23:30:48 -05:00
Aaron Piotrowski
5fb03758e5
Rename sanitizer members for clarity
These members hold the prior stack pointer and size, so bottom and capacity were poor name choices, prior_pointer and prior_size are more clear.
2021-05-08 13:12:06 -05:00
Aaron Piotrowski
af2905968c
Add sanitizer fiber switching support 2021-05-07 15:52:12 -05:00
Aaron Piotrowski
ccc069d0bb
Catch and repeat zend_bailout in fibers
This removes switching to main for fatal errors in fibers in favor of catching any zend_bailout in a fiber and calling zend_bailout again after switching to the previous fiber or {main}.
2021-05-07 15:52:11 -05:00
Aaron Piotrowski
779fe8e43a
Check current_execute_data instead of flags in fiber destructor
Checking EG(current_exectue_data) throws into the previous fiber instead of triggering a fatal error during shutdown. A fatal error is triggered only if the throwing destroyed fiber was resumed from {main}.
2021-05-05 10:48:30 -05:00
Nikita Popov
fb374f56a7 Rethrow exceptions in fiber destructor
We need to make sure that HANDLE_EXCEPTION is set when the fiber
throws during destruction.

Fixes oss-fuzz #33875.
2021-05-05 12:49:47 +02:00
Aaron Piotrowski
810fb59f66
Improve fiber backtraces
The start/resume/throw execute_data is now attached as the prev_execute_data to the bottom frame of the fiber stack when the fiber is running.
2021-04-30 12:17:50 -05:00
Aaron Piotrowski
f3465e6740
Minor fiber fixes
Assert error is fatal when calling zend_error_suspend_fiber.

Added missing static.

Removed .S extension from output of configure.
2021-04-30 10:08:01 -05:00
Aaron Piotrowski
4166653298
Fix exception thrown during fiber destruction
Previously an exception thrown during fiber destruction resulted in a fatal error, but that exception should be able to be caught (unless we’ve entered shutdown, then still use a fatal error so the error is not hidden).
2021-04-30 00:26:47 -05:00
Nikita Popov
e8e7c04a3a Use common struct to store error information
This is needed by both fibers and opcache (and GH-6903 also uses it),
so make it a common structure that can be used by any functionality
storing warnings/errors.
2021-04-29 11:50:54 +02:00
Aaron Piotrowski
c276c16b66
Implement Fibers
RFC: https://wiki.php.net/rfc/fibers

Closes GH-6875.
2021-04-26 11:07:06 -05:00