Commit graph

744 commits

Author SHA1 Message Date
Arnaud Le Blanc
8538e2fe6b
Merge branch 'PHP-8.4'
* PHP-8.4:
  Prevent operands from being released during comparison
2025-07-30 18:14:45 +02:00
Arnaud Le Blanc
28ed4e6ec0
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Prevent operands from being released during comparison
2025-07-30 18:11:32 +02:00
Arnaud Le Blanc
bc4b6ce7a8
Prevent operands from being released during comparison
Fixes GH-19305
Closes GH-19309
2025-07-30 18:09:24 +02:00
Niels Dossche
d706dc1f80
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix OSS-Fuzz #427814456
2025-07-01 18:52:44 +02:00
Niels Dossche
5d590a1e87
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix OSS-Fuzz #427814456
2025-07-01 18:52:39 +02:00
Niels Dossche
91749844e6
Fix OSS-Fuzz #427814456
The first warning may trigger an error handler, destroying the operand
and its string. So we need to protect the string in that case.
Care was taken to avoid unnecessary refcounts and to avoid touching the
hot code path.

Closes GH-18951.
2025-07-01 18:50:41 +02:00
Niels Dossche
28a083bddc
Use zend_string_release_ex() in concat_function() (#18827)
The strings we encounter are either interned in which case the
persistent bool doesn't matter; or they're temporary as the code already
assumes that anyway.
This patch shrinks the function from 3332 bytes to 3173 bytes on x86-64
with GCC 15.1.1.
2025-06-12 17:49:38 +02:00
Niels Dossche
8f3e5553f3
Use zval_try_get_string_func() in concat_function() (#18815)
This allows a cheaper exception check and also does not need a release
call.
This shrinks concat_function() on x86-64 with GCC 15.1.1 from 3443 bytes
to 3332 bytes.
2025-06-10 17:33:42 +02:00
Niels Dossche
be17e9ed54
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix handling of references in zval_try_get_long()
2025-06-04 21:00:22 +02:00
Niels Dossche
2b383848a7
Fix handling of references in zval_try_get_long()
This API can't handle references, yet everyone keeps forgetting that it
can't and that you should DEREF upfront. Fix every type of this issue
once and for all by moving the reference handling to this Zend API.

Closes GH-18761.
2025-06-04 21:00:05 +02:00
Ilija Tovilo
5a482a139c
Fix enum to bool comparison
The compiler compiles $value == true to ZEND_BOOL, which always returns true for
objects (with the default cast_object handler). However, when compared to a
statically unknown rhs $value == $true, the resulting opcode ZEND_IS_EQUAL would
call the objects compare handler.

The zend_objects_not_comparable() handler, which is installed for enums and
other internal classes, blanketly returns false. This does not match the
ZEND_BOOL semantics.

Object to boolean comparison is now handled directly in zend_compare(),
analogous to object to null comparison. It continuous to call the cast_object
handler, but guarantees consistent behavior across ZEND_BOOL and ZEND_IS_EQUAL.

Fixes GH-16954
Closes GH-17031
2024-12-09 17:49:01 +01:00
Gina Peter Banyard
d95e222402
Zend: Use a dedicated enum for the status of the result of a division (#16020) 2024-09-24 20:43:06 +01:00
Arnaud Le Blanc
58aa6fc830
Lazy objects
RFC: https://wiki.php.net/rfc/lazy-objects

Closes GH-15019
2024-08-30 17:30:03 +02:00
Peter Kokot
5d3fab9334
Sync #if/ifdef/defined (#14520)
These are either undefined or defined (to value 1):
- __DragonFly__
- __FreeBSD__
- HAS_MCAST_EXT
- HAVE_GETCWD
- HAVE_GETWD
- HAVE_GLIBC_ICONV
- HAVE_JIT
- HAVE_LCHOWN
- HAVE_NL_LANGINFO
- HAVE_RL_CALLBACK_READ_CHAR
- HAVE_RL_ON_NEW_LINE
- HAVE_SQL_EXTENDED_FETCH
- HAVE_UTIME

Follow up of GH-5526 (-Wundef)
2024-06-11 22:47:05 +02:00
Levi Morrison
c461b60060
refactor: change zend_is_true to return bool (#14301)
Previously this returned `int`. Many functions actually take advantage
of the fact this returns exactly 0 or 1. For instance,
`main/streams/xp_socket.c` does:

    sockopts |= STREAM_SOCKOP_IPV6_V6ONLY_ENABLED * zend_is_true(tmpzval);

And `Zend/zend_compile.c` does:

    child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];

I changed a few places trivially from `int` to `bool`, but there are
still many places such as the object handlers which return `int` that
should eventually be `bool`.
2024-05-24 15:16:36 -06:00
Jorg Sowa
23afe57f01
Added deprecation Division by zero when using power with zero as base and negative exponent
RFC: https://wiki.php.net/rfc/raising_zero_to_power_of_negative_number

Closes GH-13128
2024-05-22 15:05:47 +02:00
SakiTakamachi
85371ec49c
Cast the parameter type of arm BLOCKCONV_LOAD (#14159)
When building with gcc in the Arm environment, a runtime error due to an
overflow occurred, so we fixed it.

closes #14159
2024-05-07 19:44:01 +09:00
George Peter Banyard
0b614a6c2b
Fixed oss-fuzz #62294: Unsetting variable after ++/-- on string variable warning
Closes GH-12202
2023-09-17 15:49:46 +01:00
George Peter Banyard
6ae9cf40d1 Fix OSS-fuzz #60709 unseting op via globals
It turns out not just NULL is affected nor -- but also on booleans and this also affects properties
2023-08-01 16:40:25 +01:00
George Peter Banyard
d8696f9216
[RFC] Path to Saner Increment/Decrement operators (#10358)
* Add behavioural tests for incdec operators

* Add support to ++/-- for objects castable to _IS_NUMBER

* Add str_increment() function

* Add str_decrement() function

RFC: https://wiki.php.net/rfc/saner-inc-dec-operators

Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>
Co-authored-by: Arnaud Le Blanc <arnaud.lb@gmail.com>
2023-07-17 15:51:24 +01:00
nielsdos
3c872661c1 Fix GH-11507: String concatenation performance regression in 8.3
When the code was moved to solve the uaf for memory overflow, this
caused the refcount to be higher than one in some self-concatenation
scenarios. This in turn causes quadratic time performance problems when
these concatenations happen in a loop.

Closes GH-11508.
2023-06-22 23:42:25 +02:00
George Peter Banyard
ea8f934fe5
Zend: Expose zendi_try_get_long() function via a public API (#10175) 2023-06-19 14:07:46 +01:00
Ilija Tovilo
7790ee8736
Fix concat_function use-after-free on out-of-memory error (#11297)
Introduced by GH-10049
2023-05-22 22:02:27 +02:00
Ilija Tovilo
5c741644d1
Fix string coercion for $a .= $a (#11296)
free_op2_string may be set to false when the operands are not strings, and
`result == op1 == op2`, by re-using the same string for both operands. In that
case, the string should still be copied to result because result is not actually
a string. Also change the op1 branch to stay consistent.

Introduced by GH-10049
2023-05-22 19:48:07 +02:00
Niels Dossche
727e26f9f2 Fix #97836 and #81705: Segfault / type confusion in concat_function
The following sequence of actions was happening which caused a null
pointer dereference:
1. debug_backtrace() returns an array
2. The concatenation to $c will transform the array to a string via
   `zval_get_string_func` for op2 and output a warning.
   Note that zval op1 is of type string due to the first do-while
   sequence.
3. The warning of an implicit "array to string conversion" triggers
   the ob_start callback to run. This code transform $c (==op1) to a long.
4. The code below the 2 do-while sequences assume that both op1 and op2
   are strings, but this is no longer the case. A dereference of the
   string will therefore result in a null pointer dereference.

The solution used here is to work with the zend_string directly instead
of with the ops.

For the tests:
Co-authored-by: changochen1@gmail.com
Co-authored-by: cmbecker69@gmx.de
Co-authored-by: yukik@risec.co.jp

Closes GH-10049.
2023-05-16 20:27:00 +02:00
Niels Dossche
a65cdd97a1
Implement NEON-accelerated version of BLOCKCONV for lowercasing and uppercasing strings (#11161)
Since lowercasing and uppercasing is a common operation for both
internal purposes and userland purposes, it makes sense to implement a
NEON accelerated version for this.
2023-05-04 19:51:02 +02:00
Ilija Tovilo
e2bd92f32c
Merge branch 'PHP-8.2'
* PHP-8.2:
  Endless recursion when using + on array in foreach
2023-05-01 13:21:54 +02:00
Ilija Tovilo
50127cef92
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Endless recursion when using + on array in foreach
2023-05-01 13:21:32 +02:00
Ilija Tovilo
dc20cd9c3a
Endless recursion when using + on array in foreach
This reverts commit 84b4020eb4.

Fixes GH-11171
2023-05-01 13:20:58 +02:00
Niels Dossche
a0476fd32f
Micro-optimize double comparison (#11061)
When using ZEND_NORMALIZE_BOOL(a - b) where a and b are doubles, this
generates the following instruction sequence on x64:
subsd   xmm0, xmm1
pxor    xmm1, xmm1
comisd  xmm0, xmm1
...

whereas if we use ZEND_THREEWAY_COMPARE we get two instructions less:
ucomisd xmm0, xmm1

The only difference is that the threeway compare uses *u*comisd instead
of comisd. The difference is that it will cause a FP signal if a
signaling NAN is used, but as far as I'm aware this doesn't matter for
our use case.

Similarly, the amount of instructions on AArch64 is also quite a bit
lower for this code compared to the old code.

** Results **

Using the benchmark https://gist.github.com/nielsdos/b36517d81a1af74d96baa3576c2b70df
I used hyperfine: hyperfine --runs 25 --warmup 3 './sapi/cli/php sort_double.php'
No extensions such as opcache used during benchmarking.

BEFORE THIS PATCH
-----------------
  Time (mean ± σ):     255.5 ms ±   2.2 ms    [User: 251.0 ms, System: 2.5 ms]
  Range (min … max):   251.5 ms … 260.7 ms    25 runs

AFTER THIS PATCH
----------------
  Time (mean ± σ):     236.2 ms ±   2.8 ms    [User: 228.9 ms, System: 5.0 ms]
  Range (min … max):   231.5 ms … 242.7 ms    25 runs
2023-04-14 18:22:42 +02:00
Ilija Tovilo
8360efde8d
Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix add_function_array() assertion when op2 contains op1
2023-04-03 12:49:43 +02:00
Ilija Tovilo
c4f56c5099
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix add_function_array() assertion when op2 contains op1
2023-04-03 12:49:33 +02:00
Ilija Tovilo
84b4020eb4
Fix add_function_array() assertion when op2 contains op1
Fixes GH-10085
Closes GH-10975
Co-authored-by: Dmitry Stogov <dmitry@zend.com>
2023-04-03 12:48:46 +02:00
Niels Dossche
2b9d2bcee7 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix undefined behaviour in string uppercasing and lowercasing
2023-03-25 21:28:09 +01:00
Niels Dossche
bf487bde13 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix undefined behaviour in string uppercasing and lowercasing
2023-03-25 21:22:35 +01:00
Niels Dossche
93e0f6b424 Fix undefined behaviour in string uppercasing and lowercasing
At least on 32-bit, the address computations overflow in running the
test on CI with UBSAN enabled. Fix it by reordering the arithmetic.
Since a part of the expression is already used in the code above the
computation, this should not negatively affect performance.

Closes GH-10936.
2023-03-25 21:17:15 +01:00
Max Kellermann
d5c649b36b
zend_compiler, ...: use uint8_t instead of zend_uchar (#10621)
`zend_uchar` suggests that the value is an ASCII character, but here,
it's about very small integers.  This is misleading, so let's use a
C99 integer instead.

On all architectures currently supported by PHP, `zend_uchar` and
`uint8_t` are identical.  This change is only about code readability.
2023-02-23 14:56:54 +00:00
Max Kellermann
49c1e6eb33
Make various pointers const in Zend/ (#10608)
* Zend/zend_operators: pass const pointers to zend_is_identical()

* Zend/zend_operators: pass const pointers to zend_get_{long,double}()

* Zend/Optimizer/sccp: make pointers const

* Zend/Optimizer/scdf: make pointers const

* Zend/Optimizer/zend_worklist: make pointers const

* Zend/Optimizer/zend_optimizer: make pointers const

* Zend/zend_compile: make pointers const
2023-02-20 14:00:59 +00:00
Niels Dossche
99b86141ae Introduce convenience macros for copying flags that hold when concatenating two strings
This abstracts away, and cleans up, the flag handling for properties of
strings that hold when concatenating two strings if they both hold that
property. (These macros also work with simply copies of strings because
a copy of a string can be considered a concatenation with the empty
string.) This gets rid of some branches and some repetitive code, and
leaves room for adding more flags like these in the future.
2023-02-05 14:32:50 +00:00
Alex Dowad
c02af98ae5 Use AVX2 to accelerate strto{upper,lower} (only on 'AVX2-native' builds for now)
On short strings, there is no difference in performance. However, for
strings around 10,000 bytes long, the AVX2-accelerated function is
about 55% faster than the SSE2-accelerated one.
2023-02-03 16:29:27 +02:00
George Peter Banyard
64127b66c6 Concatenating two valid UTF-8 strings produces a valid UTF-8 string
The UTF-8 valid flag needs to be copied upon interning,
otherwise strings that are concatenated at compile time lose this information.

However, if previously this string was interned without the flag it is not added
E.g. in the case the string is an existing class name.

Co-authored-by: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
2023-02-02 12:02:36 +00:00
George Peter Banyard
78720e39a6 Mark numeric strings as valid UTF-8 2023-02-02 12:02:36 +00:00
Máté Kocsis
7936c8085e
Fix GH-8329 Print true/false instead of bool in error and debug messages (#8385) 2023-01-23 10:52:14 +01:00
Alex Dowad
0b7986f976 Tweak SSE2-accelerated strtoupper() and strtolower() for speed
I learned this trick for doing a faster bounds check with both upper
and lower bounds by reading a disassembler listing of optimized code
produced by GCC; instead of doing 2 compares to check the upper and the
lower bound, add an immediate value to shift the range you are testing
for to the far low or high end of the range of possible values for the
type in question, and then a single compare will do. Intstead of
compare + compare + AND, you just do ADD + compare.

From microbenchmarking on my development PC, this makes strtoupper()
about 10% faster on long strings (~10,000 bytes).
2023-01-20 08:21:45 +02:00
Christoph M. Becker
c8955c078a
Revert GH-10220
Cf. <https://github.com/php/php-src/pull/10220#issuecomment-1383739816>.

This reverts commit ecc880f491.
This reverts commit 588a07f737.
This reverts commit f377e15751.
This reverts commit b4ba16fe18.
This reverts commit 694ec1deea.
This reverts commit 6b34de8eba.
This reverts commit aa1cd02a43.
This reverts commit 308fd311ea.
This reverts commit 16203b53e1.
This reverts commit 738fb5ca54.
This reverts commit 9fdbefacd3.
This reverts commit cd4a7c1d90.
This reverts commit 928685eba2.
This reverts commit 01e5ffc85c.
2023-01-16 12:27:33 +01:00
Max Kellermann
694ec1deea Zend/zend_{operators,variables}: include cleanup 2023-01-10 14:19:03 +00:00
Max Kellermann
a8eb399ca3 Zend/zend_operators: make several pointers const 2023-01-04 12:59:16 +00:00
zeriyoshi
30ed8fb32d Merge remote-tracking branch 'upstream/PHP-8.1' 2022-08-05 00:08:36 +09:00
zeriyoshi
2d777466c0 Merge remote-tracking branch 'upstream/PHP-8.0' into PHP-8.1 2022-08-05 00:06:04 +09:00
Go Kudo
3725717de1
Remove ZEND_DVAL_TO_LVAL_CAST_OK (#9215)
* Remove ZEND_DVAL_TO_LVAL_CAST_OK
As far as I can see, this operation should always use the _slow method, and the results seem to be wrong when ZEND_DVAL_TO_LVAL_CAST_OK is enabled.

* update NEWS
2022-08-04 23:56:19 +09:00