Commit graph

6372 commits

Author SHA1 Message Date
Niels Dossche
db3bf715e0
Fix leak of accel_globals->key
I don't know why this was guarded with ZTS, but it leaks on this test
(and a few more):
`./sapi/cli/php ./run-tests.php -c . --show-diff sapi/phpdbg/tests/stdin_001.phpt`

Closes GH-18593.
2025-05-19 19:34:16 +02:00
Niels Dossche
6b795f64a5
Fix GH-18534: FPM exit code 70 with enabled opcache and hooked properties in traits
The trait handling for property hooks in preloading did not exist, we
add a check to skip trait clones and we add the necessary code to update
the op arrays.

Closes GH-18586.
2025-05-19 19:21:53 +02:00
Niels Dossche
43915b302c
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-18567: Preloading with internal class alias triggers assertion failure
2025-05-19 19:20:11 +02:00
Niels Dossche
41e11a627d
Fix GH-18567: Preloading with internal class alias triggers assertion failure
The assertion is imprecise now, and the code assumed that from the
moment an internal class was encountered that there were only internal
classes remaining. This is wrong now, and we still have to continue if
we encounter an internal class. We can only skip the remaining iterations
if the entry in the hash table is not an alias.

Closes GH-18575.
2025-05-19 19:19:26 +02:00
Arnaud Le Blanc
18276a8b42
Snapshotted poly_func / poly_this may be spilled
Polymorphic calls pass this and the function to side traces via snapshotting.
However, we assume that this/func are in registers, when in fact they may be
spilled.

Here I update snapshotting of poly_func/poly_this to support spilling:

 - In zend_jit_snapshot_handler, keep track of the C stack offset
   of the spilled register, in a way similar to how stack variables.
 - In zend_jit_start, do not pre-load the registers if they were spilled.
 - In zend_jit_trace_exit / zend_jit_trace_deoptimization, load from the
   stack if the register was spilled.
 - Store a reference to poly_func/poly_this in zend_jit_ctx so we can use that
   directly in the side trace.

Closes GH-18408
2025-05-14 12:27:57 +02:00
Ilija Tovilo
386ab1dad2
Revert "Fix infinite recursion on deprecated attribute evaluation"
This reverts commit 272f7f75e2.

Reverts GH-17712 for the PHP-8.4 branch. This will be reapplied later
with a fix for GH-18463 (GH-18464).
2025-04-30 20:52:56 +02:00
Arnaud Le Blanc
978c01ce15
JIT: Check exception on exit
Add a new exit flag (ZEND_JIT_EXIT_CHECK_EXCEPTION) that enables exception
checking during exit/deoptimization.

We already checked for exceptions during exit/deoptimization, but only when
ZEND_JIT_EXIT_FREE_OP1 or ZEND_JIT_EXIT_FREE_OP2 were set (presumably to
handle exceptions thrown during dtor). The new flag makes it possible to request
it explicitly.

This also fixes two issues in zend_jit_trace_exit():

- By returning 1, we were telling the caller (zend_jit_trace_exit_stub()) to
  execute the original op handler of EG(current_execute_data)->opline, but in
  reality we want to execute EX(opline), which should be EG(exception_op).

- EX(opline) is set to the value of %r15 in zend_jit_trace_exit_stub() before
  calling zend_jit_trace_exit(), but this may be the address of a
  zend_execute_data when the register is being reused to cache EX(call).

Fixes GH-18262
Closes GH-18297
2025-04-29 10:55:31 +02:00
Niels Dossche
c620fee404
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-18417: Windows SHM reattachment fails when increasing memory_consumption or jit_buffer_size
2025-04-28 19:52:22 +02:00
Niels Dossche
7869af6fa8
Fix GH-18417: Windows SHM reattachment fails when increasing memory_consumption or jit_buffer_size
When a first PHP process launches, Opcache creates a shared file mapping
to use as a shm region. The size of this mapping is set by
opcache.memory_consumption.
When a new PHP process launches while the old one is still running,
Opcache tries to reattach to the shm.
When reattaching it tries to map the requested size (i.e. set by
opcache.memory_consumption). However, if the new requested size is
larger than the size used in the original file mapping, then the call
to VirtualProtect() will fail and the new PHP process will fail to
launch.
It's not possible to resize the virtual region on Windows, unless
relying on undocumented APIs like `NtExtendSection` but then we would
sitll need to communicate that to the first process.

This issue is the root cause of Psalm end-to-end tests failing in
GH-18417: Psalm estimates the required memory sizes and relaunches itself
with more memory requested, if its estimate is below the currently allocated
shared memory. This causes a crash on startup and the tests fail.

To solve this, we need to make the mappings unique per requested size.
There are two ideas:
1. Include in zend_system_id. However, this also affects other things
   and may be too overkill.
2. Include it in the filename, this is an easy local change.
   I went with this option.

Closes GH-18443.
2025-04-28 19:51:31 +02:00
Niels Dossche
1a1a83f1fc
Fix GH-18136: tracing JIT floating point register clobbering on Windows and ARM64
On win64, xmm6-xmm15 are preserved registers, but the prologues and
epilogues of JITted code don't handle these. The issue occurs when
calling into the JIT code again via an internal handler
(like call_user_func). Therefore, we want to save/restore xmm registers
upon entering/leaving execute_ex. Since MSVC x64 does not support inline
assembly, we create an assembly wrapper around the real execute_ex
function.
The alternative is to always save/restore these xmm registers into the
fixed call frame, but this causes unnecessary overhead.
The same issue occurs for ARM64 platforms for floating point register
8 to 15. However, there we can use inline asm to fix this.

Closes GH-18352.
2025-04-21 13:15:43 +02:00
Florian Engelhardt
061b46e09d
Save opline in zend_jit_hot_func()
Closes GH-18289
2025-04-15 14:11:32 +02:00
Niels Dossche
14853ea2f2
Fix reproducibility of test GH-17190
The test failure did not trigger for me when playing with the JIT code.
From the original issue report some INI settings were not set properly.
2025-04-11 17:54:28 +02:00
Niels Dossche
4a12a9f3e9
Fix GH-18294: assertion failure zend_jit_ir.c
The JIT helper `zend_jit_assign_op_to_typed_ref` expects a `zval*` as an
argument, so we have to store to the stack if OP1_DATA(=op3) is in a
register.

Closes GH-18299.
2025-04-11 17:54:19 +02:00
Dmitry Stogov
79dc7a2d26
Update IR
IR commit: 8d17022fb61ebfed9f6be81a8182ea31202697ed
2025-04-02 16:20:09 +03:00
Dmitry Stogov
2b9840894d
Update IR
IR commit: dd228777b67334d8ed51de44f427d66d4ac99c08
2025-03-27 22:24:46 +03:00
Ilija Tovilo
272f7f75e2
Fix infinite recursion on deprecated attribute evaluation
Fixes GH-17711
Fixes GH-18022
Closes GH-17712
2025-03-26 23:39:38 +01:00
Ilija Tovilo
d5bdf8f508
Fix segfault when evaluating const expr default value of child prop with added hooks
Introduced by GH-17870. Not adding a NEWS entry since this is fixed in
the same version.

Fixes oss-fuzz #403816122
Closes GH-18098
2025-03-23 16:35:04 +01:00
Niels Dossche
7d1a2d03e4
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  [ci skip] Make sure opcache can output in these tests
2025-03-21 16:35:57 +01:00
Niels Dossche
071f707a6d
[ci skip] Make sure opcache can output in these tests 2025-03-21 16:35:47 +01:00
Niels Dossche
d765b60778
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-18107: Opcache CFG jmp optimization with try-finally breaks the exception table
2025-03-21 13:57:16 +01:00
Niels Dossche
2ec8d37eb4
Fix GH-18107: Opcache CFG jmp optimization with try-finally breaks the exception table
If there's a try-finally where the try_op starts on a basic block with a
single JMP, and the JMP optimization causes that basic block to become
unreachable, then we update try_op.
In this case, there is no catch_op, so try_op is erroneously set to 0,
we should instead set it to `b->start`.

Closes GH-18110.
2025-03-21 13:56:31 +01:00
Niels Dossche
66498152f1
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-18112: NULL access with preloading and INI option
2025-03-20 19:12:47 +01:00
Niels Dossche
e9c0296240
Fix GH-18112: NULL access with preloading and INI option
Preloading shutdown calls request shutdown which will deactivate the
virtual cwd state. However, further startup code still assumes the state
that was set by virtual_cwd_startup(). So we need to reactivate it
manually.

Creating a test was a bit difficult because the INI setting I wanted to
test this with is overridden by the test runner apparently.
To reproduce the issue, create an empty file test.php and execute this
in a ZTS build:
`php -d opcache.preload=./ext/opcache/tests/preload_class_alias_2.inc -d "error_log=" -d "allow_url_include=1" test.php`

Closes GH-18117.
2025-03-20 19:12:06 +01:00
Niels Dossche
9488684703
Add test for GH-18113
Fixed in https://github.com/dstogov/ir/pull/110 and merged via b932c267.

Closes GH-18113.
2025-03-19 23:49:43 +01:00
Dmitry Stogov
b932c267f8
Update IR
IR commit: 3d0124a06ee4321e1305f893b74840033d939e88
2025-03-20 01:10:56 +03:00
Ilija Tovilo
56841998de
Fix IN_ARRAY optimization
in_array() calls are compiled to frameless calls. Adjust the
optimization appropriately. Luckily, frameless opcodes simplify the
optimization quite a bit.

Fixes GH-18050
Closes GH-18066
2025-03-18 13:42:53 +01:00
Niels Dossche
f4ba3564f5
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Correct check for maximum string length in JIT helpers
2025-03-13 23:49:42 +01:00
Niels Dossche
413938143b
Fix GH-18037: SEGV Zend/zend_execute.c
A frameless icall with 3 arguments is a special case because it uses
OP_DATA, but this was not added to the list, so the opline pointed to
the wrong address resulting in UBSAN report or crash.

Closes GH-18048.
2025-03-13 23:48:24 +01:00
Niels Dossche
a7d2703246
Correct check for maximum string length in JIT helpers
This is a bit of a theoretical issue, but the maximum string length is
actually ZSTR_MAX_LEN instead of SIZE_MAX. The resulting check is a bit
slower but should still be relatively cheap.

Closes GH-18049.
2025-03-13 23:47:45 +01:00
Dmitry Stogov
ae67eb0490
Update IR
IR commit: 8bb0acca45a7b0f12691f4258e41462599efbd74
2025-03-13 03:07:43 +03:00
Dmitry Stogov
1b9d659c3f
Fix register allocarion for result of ASSIGN_OBJ (#17994)
This fixes few failures in Symfony unit tests with function JIT
2025-03-07 19:28:51 +03:00
Dmitry Stogov
5885b9490c
Fix function JIT for Wordpress unit tests 2025-03-07 02:30:30 +03:00
Niels Dossche
bac1ed6579
Add test for GH-17966
This was fixed via https://github.com/dstogov/ir/pull/109 which was
merged in cc70838dc9.
2025-03-06 21:55:53 +01:00
Dmitry Stogov
cc70838dc9
Merge IR
IR commit: 0441281e95ce9736131eddc71ce666389dcccd4b
2025-03-06 23:00:53 +03:00
Niels Dossche
f6c2e40a11
Fix GH-15834: Segfault with hook "simple get" cache slot and minimal JIT
The FETCH_OBJ_R VM handler has an optimization that directly enters into
a hook if it is a simpler getter hook. This is not compatible with the
minimal JIT because the minimal JIT will try to continue executing the
opcodes after the FETCH_OBJ_R.
To solve this, we check whether the opcode is still the expected one
after the execution of the VM handler. If it is not, we know that we are
going to execute a simple hook. In that case, exit to the VM.

Closes GH-17909.
2025-03-06 19:37:21 +01:00
Dmitry Stogov
f016caa312
Merge IR
IR commit: 1a02c4819f210a1f4548b83850ed7cd5c76c13aa
2025-03-03 23:48:08 +03:00
Niels Dossche
422e90db3b
Fix branch target in zend_jit_push_call_frame() (#17949)
Introduced by accident in 3b4a58da44.
Will request a cherry-pick.
2025-02-28 09:20:02 +01:00
Dmitry Stogov
819b1988a0
Update IR
IR commit: ca93e781eaf6b0949690d3df272ecf44528ff4a8
2025-02-25 02:23:05 +03:00
Niels Dossche
3b4a58da44
Backport GH-17869 to PHP 8.3 JIT
Closes GH-17918.
2025-02-24 21:45:03 +01:00
Niels Dossche
5ede5415e1
Fix GH-17868: Cannot allocate memory with tracing JIT on 8.4.4
The generated code tries to initialize the run time cache for even
internal closures, but it should only initialize the run time cache for
user closures. We fix this by adding a check for the function type.
If `func` is known, then we can check the type at code generation time.

Closes GH-17869.
2025-02-24 19:36:13 +01:00
Bob Weinand
53fa98ecd3
Fix GH-17715: Handle preloaded internal function runtime cache (#17835)
This solely affects the builtin enum functions currently.

Given that these are stored in SHM, we cannot simply hardwire a pointer into the internal function runtime cache on NTS too, but have to use a MAP_PTR (like on ZTS).
Now, by design, the runtime cache of internal functions no longer is reset between requests, hence we need to store them explicitly as static runtime cache.

On NTS builds we cannot trivially move the pointers into CG(internal_run_time_cache) as they're directly stored on the individual functions (on ZTS we could simply iterate the static map_ptrs).
Hence, we have the choice between having opcache managing the internal run_time_cache for its preloaded functions itself or realloc CG(internal_run_time_cache) and iterate through all functions to assign the new address. We choose the latter for simplicity and initial speed.
2025-02-24 14:35:47 +01:00
David Carlier
e8dda54dd5
Merge branch 'PHP-8.3' into PHP-8.4 2025-02-23 10:45:46 +00:00
David Carlier
cefdf00e7e
Fix GH-17899: zend_test_compile_string crash on invalid script path.
when opcache is enabled.

close GH-17901
2025-02-23 10:45:10 +00:00
Dmitry Stogov
dd404dc419
Update IR
IR commit: 1499748be439e7f786c0ee2c4f738c126af57df8
2025-02-20 23:58:08 +03:00
Dmitry Stogov
352aca1ee1
Update IR
IR commit: 7f920cb660a1ef615a3059eab77b86ca1a43121e
2025-02-18 21:32:17 +03:00
Niels Dossche
34d8befe8d
Fix GH-17747: Exception on reading property in register-based FETCH_OBJ_R breaks JIT
When read_property fails, it may return `&EG(uninitialized_zval)`, and
the exception is handled in the VM. The VM will try to
`zval_ptr_dtor_nogc` the result, but the result was never set, resulting
in dtor'ing garbage data. To solve this, we check when a different zval*
was returned and initialize the result with UNDEF. We don't need to copy
as the slow_ex handler return values are used directly in a register.

Closes GH-17749.
2025-02-11 21:55:23 +01:00
Dmitry Stogov
9fb355b863
Update IR
IR commit: 8ec4979470fe2a123d0e56da92aa8d5d19b6e54c
2025-02-10 16:47:47 +03:00
Dmitry Stogov
c730175be8
IR Update
IR commit: 36329a0398ff57929c51c52084027730a75b9862
2025-02-06 02:04:03 +03:00
Niels Dossche
78da288222
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-17577: JIT packed type guard crash
2025-02-03 19:35:32 +01:00
Niels Dossche
0c3cf1f311
Fix GH-17577: JIT packed type guard crash
When a guard check is created for a variable to check if it's a packed array,
it is possible that there was no prior type check for that variable.
This happens in the global scope for example when the variable aliases.
In the test, this causes a dereference of address 8 because the integer
element in `$a` is interpreted as an array address.

This patch adds a check to see if the guard is handled.
If we were not able to determine or guard the type then we also cannot know the array is packed.

Closes GH-17584.
2025-02-03 19:34:39 +01:00