1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 16:22:37 +01:00
Commit Graph

5949 Commits

Author SHA1 Message Date
Niels Dossche
2ad0b5cf05 Fix GH-19792: SCCP causes UAF for return value if both warning and exception are triggered
If an exception _and_ a warning (or deprecation) is emitted, then the
result is destroyed twice. Use an `else if` to prevent this.
This is tested via zend_test because the deprecation that triggered the
original reproducer may disappear in the future.

Closes GH-19793.
2025-09-11 19:35:53 +02:00
Niels Dossche
be9f1d3d56 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Reset global pointers to prevent use-after-free
2025-07-30 09:23:12 +02:00
Niels Dossche
7016ad558b Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Reset global pointers to prevent use-after-free
2025-07-30 09:22:50 +02:00
Florian Engelhardt
3aaa8d3526 Reset global pointers to prevent use-after-free
Closes GH-19212.
2025-07-30 09:22:15 +02:00
Ilija Tovilo
b6660634b4 Disable JIT on Apple Silicon + ZTS
Apple Silicon has stricter rules about rwx mmap regions. They need to be created
using the MAP_JIT flag. However, the MAP_JIT seems to be incompatible with
MAP_SHARED. ZTS requires MAP_SHARED so that some threads may execute code from a
page while another writes/appends to it. We did not find another solution, other
than completely disabling JIT for Apple Silicon + ZTS.

See discussion in https://github.com/php/php-src/pull/13351.

Co-authored-by: Peter Kokot <peterkokot@gmail.com>
Fixes GH-13400
Closes GH-13396
2025-07-03 10:34:04 -05:00
Niels Dossche
1e3d92f8a9 Fix GH-14082: Segmentation fault on unknown address 0x600000000018 in ext/opcache/jit/zend_jit.c
During persisting, the JIT may trigger and fill in the call graph.
The call graph info is allocated on the arena which will be gone after preloading.
To prevent invalid accesses during normal requests, the arena data should be cleared.
This has to be done after all scripts have been persisted because shared op arrays between
scripts can change the call graph.

Closes GH-18916.
2025-06-23 22:27:36 +02:00
Niels Dossche
8e731ca622 Fix GH-18639: Internal class aliases can break preloading + JIT
ZEND_FUNC_INFO() can not be used on internal CE's. If preloading makes a
CE that's an alias of an internal class, the invalid access happens when
setting the FUNC_INFO.

While we could check the class type to be of user code, we can just skip
aliases altogether anyway which may be faster.

Closes GH-18915.
2025-06-23 20:01:15 +02:00
Niels Dossche
b3c8afe272 Fix GH-18743: Incompatibility in Inline TLS Assembly on Alpine 3.22
GAS started checking the relocation for tlsgd: it must use the %rdi
register. However, the inline assembly now uses %rax instead.
Fix it by changing the "=a" output register to "=D".
Source: ec181e1710/gas/config/tc-i386.c (L6793)

gottpoff is unaffected.

Closes GH-18779.
2025-06-09 11:39:34 +02:00
Niels Dossche
b2d78ae00c Backport accel_globals->key leak fix (8.3)
Closes GH-18602.
2025-05-19 22:27:54 +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
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
071f707a6d [ci skip] Make sure opcache can output in these tests 2025-03-21 16:35:47 +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
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
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
Ilija Tovilo
1009ff65cc Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Upgrade security branches to Ubuntu 22.04
2025-03-06 15:24:35 +01:00
Ilija Tovilo
5fcc8d4cd1 Upgrade security branches to Ubuntu 22.04
20.04 goes out of security support in 2 months. Backporting various
commits.

See d98963a071
See af721c9c36
See 378b79b90c

Closes GH-17963
2025-03-06 15:24:15 +01: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
Niels Dossche
3b4a58da44 Backport GH-17869 to PHP 8.3 JIT
Closes GH-17918.
2025-02-24 21:45:03 +01: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
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
Niels Dossche
f88445bdf8 Fix GH-17654: Multiple classes using same trait causes function JIT crash
This test has two classes that use the same trait. In function JIT mode
the same cache slot will be used. This causes problems because it is
primed for the first class and then reused for the second class,
resulting in an incorrect type check failure.

The current check for a megamorphic trait call requires current_frame to
not be NULL, but this is only set in tracing mode and not in function
mode.

This patch corrects the check.

Closes GH-17660.
2025-02-03 19:21:15 +01:00
Ilija Tovilo
2ad778bc76 Fix missing GC_PERSISTENT_LOCAL flag on accel_globals.key 2025-01-28 12:55:09 +01:00
Ilija Tovilo
8ea9b04a23 Fix inline zend_string using struct padding
As explained by Snape3058: On 64-bit machines, we typically have 7 bytes
of padding between the zend_string.val[0] char and the following char[].
This means that zend_string.val[1-7] write to and read from the struct
padding, which is a bad idea.

Allocate the given string separately instead.

Fixes GH-17564
Closes GH-17576
2025-01-27 19:50:38 +01:00
Niels Dossche
e8fce295bc Backport fix GH-17307
This is a backport of GH-17319 to fix GH-17307 on lower branches.

Closes GH-17424.
2025-01-10 18:24:25 +01:00
Niels Dossche
df6db27580 Fix GH-17246: GC during SCCP causes segfault
This bug happens because of a nested `SHM_UNPROTECT()` sequence.
In particular:
```
unprotect memory at ext/opcache/ZendAccelerator.c:2127
protect memory at ext/opcache/ZendAccelerator.c:2160
unprotect memory at ext/opcache/ZendAccelerator.c:2164
unprotect memory at ext/opcache/jit/zend_jit_trace.c:7464
^^^ Nested
protect memory at ext/opcache/jit/zend_jit_trace.c:7591
^^^ Problem is here: it should not protect again due to the nested unprotect
protect memory at ext/opcache/ZendAccelerator.c:2191
^^^ This one should actually protect, not the previous one
```

The reason this nesting happen is because:
1. We try to include the script, this eventually calls `cache_script_in_shared_memory`
2. `zend_optimize_script` will eventually run SCCP as part of the DFA pass.
3. SCCP will try to replace constants, but can also run destructors when a partial array is destructed here:

4e9cde758e/Zend/Optimizer/sccp.c (L2387-L2389)

In this case, this destruction invokes the GC which invokes the tracing JIT,
leading to the nested unprotects.

This patch disables the GC to prevent invoking user code, as user code
is not supposed to run during the optimizer pipeline.

Closes GH-17249.

Co-authored-by: Dmitry Stogov <dmitry@zend.com>
2024-12-24 14:22:48 +01:00
Ilija Tovilo
cdfd960150 Fix ZEND_MATCH_ERROR misoptimization
op1 of ZEND_MATCH_ERROR, which refers to the match expression, is not freed by
MATCH_ERROR itself. Instead, it is freed by ZEND_HANDLE_EXCEPTION. For normal
control flow, a FREE is placed at the end of the match expression.

Since FREE may appear after MATCH_ERROR in the opcode sequence, we need to
correctly handle op1 of MATCH_ERROR as alive.

Fixes GH-17106
Closes GH-17108
2024-12-12 13:10:34 +01:00
Dmitry Stogov
b0b39cdc3e Backport JIT fix: set valid EX(opline) before calling gc_possible_root() (#16858)
This will finally make the COMMUNTIY build of the PHP 8.1 build green.

See https://github.com/php/php-src/pull/16858#issuecomment-2509010556
Closes GH-17091
2024-12-09 16:59:41 +01:00
Christoph M. Becker
3702f9783b opcache_get_configuration() properly reports jit_prof_threshold
The `jit_prof_threshold` is a float, supposed to be in range [0, 1],
and usually very small (the default is 0.005).  Reporting it as int
is meaningless.

Closes GH-17077.
2024-12-09 11:45:16 +01:00
Dmitry Stogov
6bac907cb1 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Backport fix for GH-9011 (#17052)
2024-12-05 18:32:18 +03:00
Dmitry Stogov
9d4f5f0762 Backport fix for GH-9011 (#17052)
* Backport fix for GH-9011

* Fix build
2024-12-05 18:32:02 +03:00
Niels Dossche
d50a56139c Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-16902: Set of opcache tests fail zts+aarch64 (8.2-8.3)
2024-11-25 19:50:05 +01:00
Niels Dossche
de96b43d2a Fix GH-16902: Set of opcache tests fail zts+aarch64 (8.2-8.3)
Closes GH-16925.
2024-11-25 19:49:33 +01:00
Christoph M. Becker
58ed759ba7 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-16851: JIT_G(enabled) not set correctly on other threads
2024-11-20 19:12:40 +01:00
Dylan K. Taylor
ff3b4eca0e Fix GH-16851: JIT_G(enabled) not set correctly on other threads
There doesn't seem to be a thread post-startup hook that runs after
zend_startup_cb() that could be used for this

this fix is similar to accel_startup_ok() as seen here: fc1db70f10/ext/opcache/ZendAccelerator.c (L2631-L2634)

Closes GH-16853.
2024-11-20 19:11:44 +01:00
Dmitry Stogov
d1a9281814 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Use the immutable twin of temporary op_array (#16861)
2024-11-19 20:55:32 +03:00
Dmitry Stogov
ef5844a1ca Use the immutable twin of temporary op_array (#16861) 2024-11-19 20:55:15 +03:00
Dmitry Stogov
c6c3d9fa5a Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Backport JIT fix: set valid EX(opline) before calling gc_possible_root() (#16858)
2024-11-19 18:04:15 +03:00
Dmitry Stogov
6167c64782 Backport JIT fix: set valid EX(opline) before calling gc_possible_root() (#16858) 2024-11-19 18:03:54 +03:00
Dmitry Stogov
5198bcc561 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fixed test
2024-11-18 15:35:09 +03:00
Dmitry Stogov
71403558d3 Fixed test 2024-11-18 15:34:55 +03:00
Dmitry Stogov
5575703fb3 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-16829: Segmentation fault with opcache.jit=tracing enabled on aarch64
2024-11-18 14:34:42 +03:00
Dmitry Stogov
79aaeeafe5 Fix GH-16829: Segmentation fault with opcache.jit=tracing enabled on aarch64 2024-11-18 14:27:08 +03:00
Niels Dossche
0bf74bf9d8 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-16770: Tracing JIT type mismatch when returning UNDEF
2024-11-14 22:34:05 +01:00
Niels Dossche
cbb3b9371d Fix GH-16770: Tracing JIT type mismatch when returning UNDEF
When returning an UNDEF value, it actually becomes NULL.
The following code took this into account:
28344e0445/ext/opcache/jit/zend_jit_trace.c (L2196-L2199)

But the stack does not update the type to NULL, causing a mismatch.

Closes GH-16784.

Co-authored-by: Dmitry Stogov <dmitry@zend.com>
2024-11-14 22:33:06 +01:00
Dmitry Stogov
b9c6f07713 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix incorrect handling of ZEND_ACC_FINAL flag in JIT (#16778)
2024-11-13 14:39:18 +03:00
Dmitry Stogov
19809a526b Fix incorrect handling of ZEND_ACC_FINAL flag in JIT (#16778) 2024-11-13 14:38:54 +03:00
Ilija Tovilo
ed041c3443 Partial backport of e7462bf
e7462bff19
2024-10-30 14:20:47 +01:00
Ilija Tovilo
381e020edb Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix lineno in function redeclaration error
2024-10-22 15:05:29 +02:00
Ilija Tovilo
de7ef3fa66 Fix lineno in function redeclaration error
We were previously using the lineno of the first instruction, rather than the
start of the function itself.

Fixes GH-16509
Closes GH-16531
2024-10-22 15:04:20 +02:00