1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

2475 Commits

Author SHA1 Message Date
Ilija Tovilo
984f95ffc1 Merge branch 'PHP-8.5'
* PHP-8.5:
  Fix assign-op/inc/dec on untyped hooked property backing value
2026-02-05 14:48:21 +01:00
Ilija Tovilo
f9df448bab Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
  Fix assign-op/inc/dec on untyped hooked property backing value
2026-02-05 14:48:15 +01:00
Ilija Tovilo
3cb85cc681 Fix assign-op/inc/dec on untyped hooked property backing value
Fixes OSS-Fuzz #478009707
Closes GH-21124
2026-02-05 14:46:06 +01:00
Ilija Tovilo
4188c3ee2c Fix missing deref in zend_fe_fetch_object_helper (GH-21116)
Fixes OSS-Fuzz #481017027
Introduced in GH-20628
2026-02-03 13:55:49 +01:00
Khaled Alam
32bd33983d Remove unreachable code after zend_error_noreturn calls (GH-20983) 2026-02-02 14:14:15 +01:00
Ilija Tovilo
6173a9a109 VAR|TMP overhaul (GH-20628)
The aim of this PR is twofold:

- Reduce the number of highly similar TMP|VAR handlers
- Avoid ZVAL_DEREF in most of these cases

This is achieved by guaranteeing that all zend_compile_expr() calls, as well as
all other compile calls with BP_VAR_{R,IS}, will result in a TMP variable. This
implies that the result will not contain an IS_INDIRECT or IS_REFERENCE value,
which was mostly already the case, with two exceptions:

- Calls to return-by-reference functions. Because return-by-reference functions
  are quite rare, this is solved by delegating the DEREF to the RETURN_BY_REF
  handler, which will examine the stack to check whether the caller expects a
  VAR or TMP to understand whether the DEREF is needed. Internal functions will
  also need to adjust by calling the zend_return_unwrap_ref() function.

- By-reference assignments, including both $a = &$b, as well as $a = [&$b]. When
  the result of these expressions is used in a BP_VAR_R context, the reference
  is unwrapped via a ZEND_QM_ASSIGN opcode beforehand. This is exceptionally
  rare.

Closes GH-20628
2026-01-31 19:44:56 +01:00
Tim Düsterhus
a3576bddc5 zend_compile: Optimize array_map() with callable convert callback into foreach (#20934)
* zend_compile: Optimize `array_map()` with callable convert callback into foreach

For:

    <?php

    function plus1($x) {
    	return $x + 1;
    }

    $array = array_fill(0, 100, 1);

    $count = 0;
    for ($i = 0; $i < 100_000; $i++) {
    	$count += count(array_map(plus1(...), $array));
    }

    var_dump($count);

This is ~1.1× faster:

    Benchmark 1: /tmp/test/before -d opcache.enable_cli=1 /tmp/test/test6.php
      Time (mean ± σ):     172.2 ms ±   0.5 ms    [User: 167.8 ms, System: 4.2 ms]
      Range (min … max):   171.6 ms … 173.1 ms    17 runs

    Benchmark 2: /tmp/test/after -d opcache.enable_cli=1 /tmp/test/test6.php
      Time (mean ± σ):     155.1 ms ±   1.3 ms    [User: 150.6 ms, System: 4.2 ms]
      Range (min … max):   154.2 ms … 159.3 ms    18 runs

    Summary
      /tmp/test/after -d opcache.enable_cli=1 /tmp/test/test6.php ran
        1.11 ± 0.01 times faster than /tmp/test/before -d opcache.enable_cli=1 /tmp/test/test6.php

With JIT it becomes ~1.7× faster:

    Benchmark 1: /tmp/test/before -d opcache.enable_cli=1 -d opcache.jit=tracing /tmp/test/test6.php
      Time (mean ± σ):     166.9 ms ±   0.6 ms    [User: 162.7 ms, System: 4.1 ms]
      Range (min … max):   166.1 ms … 167.9 ms    17 runs

    Benchmark 2: /tmp/test/after -d opcache.enable_cli=1 -d opcache.jit=tracing /tmp/test/test6.php
      Time (mean ± σ):      94.5 ms ±   2.7 ms    [User: 90.4 ms, System: 3.9 ms]
      Range (min … max):    92.5 ms … 103.1 ms    31 runs

    Summary
      /tmp/test/after -d opcache.enable_cli=1 -d opcache.jit=tracing /tmp/test/test6.php ran
        1.77 ± 0.05 times faster than /tmp/test/before -d opcache.enable_cli=1 -d opcache.jit=tracing /tmp/test/test6.php

* zend_compile: Skip `assert(...)` callbacks for array_map() optimization

* zend_compile: Remove `zend_eval_const_expr()` in array_map optimization

* zend_vm_def: Check simple types without loading the arginfo in ZEND_TYPE_ASSERT

* zend_vm_def: Handle references for ZEND_TYPE_ASSERT

* zend_compile: Fix handling of constant arrays for `array_map()`

* zend_compile: Fix leak of unused result in array_map() optimization

* zend_compile: Support static methods for `array_map()` optimization

* UPGRADING
2026-01-19 10:18:24 +01:00
Ilija Tovilo
084e409694 Remove zend_exception_save() and zend_exception_restore()
These are leftovers from the pre-PHP-7.0 era. This also implicitly solves
GH-20564 by not clearing exceptions before entering the autoloader.

Closes GH-20256
Fixes GH-20564
2026-01-16 20:18:51 +01:00
Ilija Tovilo
8b4ef3a09f Fix FETCH_OBJ_UNSET IS_UNDEF result
UNSET_OBJ et al. do not expect to find IS_UNDEF results for IS_INDIRECT vars. To
solve this, return IS_NULL from FETCH_OBJ_UNSET when properties are
uninitialized. Do the same for FETCH_STATIC_PROP_IS, as we're otherwise copying
IS_UNDEF into the VAR result, which is not a valid value for VAR.

Fixes OSS-Fuzz #429429090
Closes GH-19160
2026-01-16 19:27:21 +01:00
Ilija Tovilo
eca7494122 Merge branch 'PHP-8.5'
* PHP-8.5:
  Fix uaf for nested finally with repeated return type check
2026-01-16 18:39:09 +01:00
Ilija Tovilo
ac0dc9859a Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
  Fix uaf for nested finally with repeated return type check
2026-01-16 18:38:53 +01:00
Ilija Tovilo
19b30032c9 Fix uaf for nested finally with repeated return type check
Fixes OSS-Fuzz #438780145
Closes GH-19488
2026-01-16 18:38:24 +01:00
Bob Weinand
82e2055300 Regenerate VM after merge
Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
2026-01-15 17:45:26 +01:00
Bob Weinand
b95d2ee70a Merge branch 'PHP-8.5'
* PHP-8.5:
  Split the live-ranges of loop variables again (#20865)
2026-01-15 16:17:34 +01:00
Bob Weinand
c878380065 Merge branch 'PHP-8.4' of github.com:php/php-src into PHP-8.5
* 'PHP-8.4' of github.com:php/php-src:
  Split the live-ranges of loop variables again (#20865)
2026-01-15 16:15:29 +01:00
Bob Weinand
27ed48c0be Split the live-ranges of loop variables again (#20865)
* Fix use-after-free in FE_FREE with GC interaction

When FE_FREE with ZEND_FREE_ON_RETURN frees the loop variable during
an early return from a foreach loop, the live range for the loop
variable was incorrectly extending past the FE_FREE to the normal
loop end. This caused GC to access the already-freed loop variable
when it ran after the RETURN opcode, resulting in use-after-free.

Fix by splitting the ZEND_LIVE_LOOP range when an FE_FREE with
ZEND_FREE_ON_RETURN is encountered:
- One range covers the early return path up to the FE_FREE
- A separate range covers the normal loop end FE_FREE
- Multiple early returns create multiple separate ranges

* Split the live-ranges of loop variables again

b0af9ac733 removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION.
This was sort-of elegant, until it was realized in 8258b7731b that it would leak the return variable, requiring some more special handling.
At some point we added live tmpvar rooting in 52cf7ab8a2, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions.

This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically.
Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now.

Closes #20766.

Signed-off-by: Bob Weinand <bobwei9@hotmail.com>

---------

Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
Co-authored-by: Gustavo Lopes <mail@geleia.net>
2026-01-15 16:13:43 +01:00
Niels Dossche
3abdef26fe VM: Reuse result variable in ICALL_0 implementation (#20561)
This reduces the assembly size from 52 to 46 bytes on x86-64 with GCC
15.2.1, strangely.

Before:
```
<+0>:	sub    $0x8,%rsp
<+4>:	movslq 0x10(%r15),%rax
<+8>:	movslq 0x10(%r15),%rdi
<+12>:	mov    %r15,(%r14)
<+15>:	mov    0x14(%r15),%edx
<+19>:	movl   $0x1,0x8(%r14,%rax,1)
<+28>:	mov    0x140695d(%rip),%rax        # 0x1addfe0 <zend_flf_handlers>
<+35>:	add    %r14,%rdi
<+38>:	call   *(%rax,%rdx,8)
<+41>:	mov    (%r14),%r15
<+44>:	add    $0x8,%rsp
<+48>:	add    $0x20,%r15
<+52>:	ret
```

After:
```
<+0>:	sub    $0x8,%rsp
<+4>:	movslq 0x10(%r15),%rdi
<+8>:	mov    0x14(%r15),%edx
<+12>:	mov    %r15,(%r14)
<+15>:	mov    0xace58a(%rip),%rax        # 0x10d9840 <zend_flf_handlers>
<+22>:	add    %r14,%rdi
<+25>:	movl   $0x1,0x8(%rdi)
<+32>:	call   *(%rax,%rdx,8)
<+35>:	mov    (%r14),%r15
<+38>:	add    $0x8,%rsp
<+42>:	add    $0x20,%r15
<+46>:	ret
```
2025-11-22 17:58:41 +01:00
Gina Peter Banyard
0df31e5a5b Merge branch 'PHP-8.5'
* PHP-8.5:
  Update NEWS for null deprecation bug fix
  Fix GH-20194: null offset deprecation not emitted for writes (#20238)
2025-10-29 18:37:29 +00:00
Gina Peter Banyard
9a1b8a785d Fix GH-20194: null offset deprecation not emitted for writes (#20238)
Based on a patch from @ndossche
2025-10-29 18:36:10 +00:00
Gina Peter Banyard
f5e782ec2d Zend: use type uint32_t for ticks_count global 2025-10-21 22:42:00 +01:00
Niels Dossche
bbe2191002 ROR the callable_convert_cache key for better hash distribution (#20052) 2025-10-15 21:58:23 +02:00
Niels Dossche
f5240b6189 Merge branch 'PHP-8.5'
* PHP-8.5:
  Regenerate zend_vm_execute.h
2025-10-08 18:56:43 +02:00
Niels Dossche
a07d257790 Regenerate zend_vm_execute.h 2025-10-08 18:56:19 +02:00
Niels Dossche
ca78da4888 Merge branch 'PHP-8.5'
* PHP-8.5:
  Fix GH-20085: Assertion failure when combining lazy object get_properties exception with foreach loop
2025-10-08 17:35:00 +02:00
Niels Dossche
d64e0ff5f3 Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
  Fix GH-20085: Assertion failure when combining lazy object get_properties exception with foreach loop
2025-10-08 17:34:53 +02:00
Niels Dossche
27035eb01e Fix GH-20085: Assertion failure when combining lazy object get_properties exception with foreach loop
In this test, we will loop once, and then replace the object with an
instance that'll throw on property construction in Z_OBJPROP_P() in
the ZEND_FE_FETCH_RW VM handler.
Since at that point `pos >= fe_ht->nNumUsed`, we exit via
`fe_fetch_w_exit` without checking for an exception, causing incorrect
continuation of the code and an eventual assertion failure.

To solve this, we perform an exception check at the end of the
iteration. This should be sufficient to guarantee the exception is
checked in time as failure of get_properties() via Z_OBJPROP_P() will
always result in an empty hash table.
This should also be more efficient than the alternative fix that checks
for an exception right after Z_OBJPROP_P() as that would be executed at
each iteration.

Closes GH-20098.
2025-10-08 17:34:38 +02:00
Ilija Tovilo
28fd7597ba Add first-class callable cache
This cache is implemented in two levels: A EG(callable_convert_cache) global
that maps zend_function pointers to a shared callable instance, and a
CALLABLE_CONVERT cache slot to remember the result of the hash table lookup.

Fixes GH-19754
Closes GH-19863
2025-10-03 01:04:56 +02:00
Gina Peter Banyard
0a47dd9bb4 Zend: Convert _zend_op_array.last_try_catch field to uint32_t 2025-09-29 15:53:58 +01:00
Gina Peter Banyard
b4ed215299 core: Warn when non-representable floats are coerced to int (#19760)
RFC: https://wiki.php.net/rfc/warnings-php-8-5#casting_out_of_range_floats_to_int
2025-09-21 23:53:16 +01:00
Bob Weinand
0bb146fae0 Make the call VM read the opline back after interrupts (#19890)
This happened implicitly in the past due to EX(opline) being used - or in the hybrid VM case, the implicit LOAD_OPLINE() happening as part of ZEND_VM_ENTER().

With 76d7c616bb opline is used standalone and the return value of zend_interrupt_helper_SPEC ignored. Make use of it...

Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
2025-09-19 19:44:56 +02:00
Tim Düsterhus
51033c2e8d zend_vm_gen: Fix line numbers for --with-lines (#19789)
The removal of empty lines and lines containing only semicolons moved the
offsets around. Remove just the standalone semicolons without removing entire
lines to keep both files in sync. This is also useful for IDE split view mode
even when not enabling `--with-lines`.
2025-09-11 12:29:24 +02:00
Arnaud Le Blanc
73b98a3858 TAILCALL VM
Introduce the TAILCALL VM, a more efficient variant of the CALL VM:

 * Each opcode handler tailcalls the next opcode handler directly instead of
   returning to the interpreter loop. This eliminates call and interpreter loop
   overhead.
 * Opcode handlers use the preserve_none calling convention to eliminate
   register saving overhead.
 * preserve_none uses non-volatile registers for its first arguments, so
   execute_data and opline are usually kept in these registers and no code is
   required to forward them to the next handlers.

Generated machine code is similar to a direct-threaded VM with register pinning,
like the HYBRID VM.

JIT+TAILCALL VM also benefits from this compared to JIT+CALL VM:

 * JIT uses the registers of the execute_data and opline args as fixed regs,
   eliminating the need to move them in prologue.
 * Traces exit by tailcalling the next handler. No code is needed to forward
   execute_data and opline.
 * No register saving/restoring in epilogue/prologue.

The TAILCALL VM is used when the HYBRID VM is not supported, and the compiler
supports the musttail and preserve_none attributes: The HYBRID VM is used when
compiling with GCC, the TAILCALL VM when compiling with Clang>=19 on x86_64 or
aarch64, and the CALL VM otherwise.

This makes binaries built with Clang>=19 as fast as binaries built with GCC.
Before, these were considerably slower (by 2.8% to 44% depending on benchmark,
and by 5% to 77% before 76d7c616bb).

Closes GH-17849
Closes GH-18720
2025-08-22 18:05:52 +02:00
Niels Dossche
a5219c1ecc Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix GH-19303: Unpacking empty packed array into uninitialized array causes assertion failure
2025-07-30 22:49:08 +02:00
Niels Dossche
a08df32f18 Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-19303: Unpacking empty packed array into uninitialized array causes assertion failure
2025-07-30 22:48:59 +02:00
Niels Dossche
5bd5f352e5 Fix GH-19303: Unpacking empty packed array into uninitialized array causes assertion failure
Having an empty result array is not a problem, because zend_hash_extend()
will initialize it. Except it does not when the number of elements to add
equals 0, which leaves the array uninitialized and therefore does not
set the packed flag, causing the assertion failure.

Technically, removing the assert would also work and save a check.
On the other hand, this check could also prevent some real work to be
done and should be relatively cheap as we already have to compute the
sum anyway.

Closes GH-19318.
2025-07-30 22:47:11 +02:00
Arnaud Le Blanc
7b3e68ff69 Fix error handling inconsistency with opcache
When opcache is enabled, error handling is altered in the following ways:

 * Errors emitted during compilation bypass the user-defined error handler
 * Exceptions emitted during class linking are turned into fatal errors

Changes here make the behavior consistent regardless of opcache being enabled or
not:

 * Errors emitted during compilation and class linking are always delayed and
   handled after compilation or class linking. During handling, user-defined
   error handlers are not bypassed. Fatal errors emitted during compilation or
   class linking cause any delayed errors to be handled immediately (without
   calling user-defined error handlers, as it would be unsafe).
 * Exceptions thrown by user-defined error handlers when handling class linking
   error are not promoted to fatal errors anymore and do not prevent linking.

Fixes GH-17422.
Closes GH-18541.
Closes GH-17627.

Co-authored-by: Tim Düsterhus <tim@bastelstu.be>
2025-07-27 11:01:49 +02:00
Arnaud Le Blanc
7f7b3cdb90 Introduce zend_vm_opcode_handler_t / zend_vm_opcode_handler_func_t
This reduces the chances of confusion between opcode handlers used by the
VM, and opcode handler functions used for tracing or debugging. Depending
on the VM, zend_vm_opcode_handler_t may not be a function. For instance in
the HYBRID VM this is a label pointer.

Closes GH-19006
2025-07-26 13:20:59 +02:00
Ilija Tovilo
aba6b89399 Merge branch 'PHP-8.4'
* PHP-8.4:
  Coerce numeric string keys from iterators when argument unpacking
2025-07-22 17:48:06 +02:00
Ilija Tovilo
4bc5aa3531 Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Coerce numeric string keys from iterators when argument unpacking
2025-07-22 17:47:56 +02:00
Ilija Tovilo
23ec35bf4a Coerce numeric string keys from iterators when argument unpacking
Fixes GH-18581
Closes GH-19151
2025-07-22 17:46:34 +02:00
Tim Düsterhus
7f4076bae0 RFC: Clone with v2 (#18747)
RFC: https://wiki.php.net/rfc/clone_with_v2

Co-authored-by: Volker Dusch <volker@tideways-gmbh.com>
2025-07-17 21:13:42 +02:00
Niels Dossche
224f95f442 Merge branch 'PHP-8.4'
* PHP-8.4:
  Update NEWS for GH-19068
  ext/gd: Drop useless and doubtful MSVC specific code (libgd/libgd@f1480ab)
  Zend: fix undefined symbol 'execute_ex' on Windows ARM64 #19064; ext/gd: fix emmintrin.h not found on Windows ARM64
2025-07-10 22:15:43 +02:00
Demon
2be3aa86f0 Zend: fix undefined symbol 'execute_ex' on Windows ARM64 #19064; ext/gd: fix emmintrin.h not found on Windows ARM64 2025-07-10 22:13:29 +02:00
Tim Düsterhus
45d948f2da Zend: Add zend_check_method_accessible() to DRY method visibility checks (#18995)
* Zend: Add `zend_check_method_accessible()` to DRY method visibility checks

* Zend: Add assertions verifying flags didn't change before `zend_check_method_accessible()`

* Try `zend_always_inline` for `zend_check_method_accessible`
2025-07-07 21:30:13 +02:00
Tim Düsterhus
59dd0f8a48 Zend: Use zend_bad_method_call() when cloning from the wrong scope (#18999) 2025-07-01 20:24:11 +02:00
Niels Dossche
4a18c895ca Fix OSS-Fuzz #428053935 (#18969)
Registering the constant may happen under another name due to
lowercasing. This will cause the lookup to the constant to fail.
Instead of looking it up, just change the Zend API to return a pointer
instead.
2025-06-30 09:09:55 +02:00
Tim Düsterhus
ca49a7bec2 RFC: Turn clone() into a function (#18919)
RFC: https://wiki.php.net/rfc/clone_with_v2

Co-authored-by: Volker Dusch <volker@tideways-gmbh.com>
2025-06-24 20:14:40 +02:00
Niels Dossche
36891a6775 Move VM exception checks (#18730)
Checking the non-exception path without arguments first, this avoids a redundant check in the case without arguments. The exception path may become more expensive, but we don't optimize for exception flow, rather we optimize for the happy flow. The other paths are unaffected.
2025-06-02 23:35:42 +02:00
Daniel Scherzer
04522cd1c4 Merge branch 'PHP-8.4'
* PHP-8.4:
  Reapply GH-17712 with a fix for internal class constants (#18464)
2025-05-25 16:51:18 -07:00
DanielEScherzer
cd751f98cb Reapply GH-17712 with a fix for internal class constants (#18464)
Add recursion protection when emitting deprecation warnings for class
constants, since the deprecation message can come from an attribute that is
using the same constant for the message, or otherwise result in recursion.

But, internal constants are persisted, and thus cannot have recursion
protection. Otherwise, if a user error handler triggers bailout before the
recursion flag is removed then a subsequent request (e.g. with `--repeat 2`)
would start with that flag already applied. Internal constants can presumably
be trusted not to use deprecation messages that come from recursive attributes.

Fixes GH-18463
Fixes GH-17711
2025-05-25 16:43:36 -07:00