Commit Graph

19129 Commits

Author SHA1 Message Date
Niels Dossche
c3a5b98771
Add zend_hash_get_current_pos_ex() (#14770) 2024-07-02 19:11:41 +02:00
Arnaud Le Blanc
3c56af9902
Allow fiber switching during destructor execution
Fiber switching was disabled during destructor execution due to conflicts
with the garbage collector. This unfortunately introduces a function color
problem: destructors can not call functions that may switch Fibers.

In this change we update the GC so that Fiber switching during GC is safe. In
turn we allow Fiber switching during destrutor execution.

The GC executes destructors in a dedicated Fiber. If a destructor suspends, the
Fiber is owned by userland and a new dedicated Fiber is created to execute the
remaining destructors. Destructor suspension results in a resurection of the
object, which is handled as usual: The object is not considered garbage anymore,
but may be collected in a later run.

When the GC is executed in the main context (not in a Fiber), then destructors
are executed in the main context as well because there is no risk of conflicting
with GC in this case (main context can not suspend).

Fixes GH-11389
Closes GH-13460
2024-07-02 15:00:40 +02:00
Arnaud Le Blanc
7c6ff87869
Expose fibers API 2024-07-02 15:00:40 +02:00
Benjamin Eberlei
72c874691b
RFC: Add #[\Deprecated] Attribute (#11293)
see https://wiki.php.net/rfc/deprecated_attribute

Co-authored-by: Tim Düsterhus <tim@tideways-gmbh.com>
Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>
2024-07-02 09:44:25 +02:00
Niels Dossche
c97885b3cc
Merge branch 'PHP-8.3'
* PHP-8.3:
  Factor out common check for short-circuited ast
  Fix OSS-Fuzz #69765: Yield reference to nullsafe chain
2024-06-30 13:38:58 +02:00
Niels Dossche
015b5195ae
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Factor out common check for short-circuited ast
  Fix OSS-Fuzz #69765: Yield reference to nullsafe chain
2024-06-30 13:38:44 +02:00
Niels Dossche
8fd095669a
Factor out common check for short-circuited ast 2024-06-30 13:38:14 +02:00
Niels Dossche
d568337680
Fix OSS-Fuzz #69765: Yield reference to nullsafe chain
You cannot return or yield a reference to a nullsafe chain. This was
checked already in zend_compile_return but not yet in
zend_compile_yield.

Closes GH-14716.
2024-06-30 13:38:06 +02:00
Peter Kokot
212b2834e9
Autotools: Sync indentation style in build system files (#14725) 2024-06-29 23:25:17 +02:00
Peter Kokot
a5b7850d6c
Wrap Zend max execution timers check in M4 macro (#14696)
- PHP_CHECK_FUNC -> AC_SEARCH_LIBS
- Redundant symbols HAVE_LIBRT and HAVE_TIMER_CREATE removed
- The rt library for some older systems (Solaris <= 10, older Linux)
  appended as needed
- This uses AC_ and AS_* macros and relies more on Autoconf shell
  code handling
- Help texts updated and synced
2024-06-29 19:52:56 +02:00
Peter Kokot
c44834d8ad
Trim trailing whitespace (#14721) 2024-06-29 18:41:45 +02:00
Peter Kokot
ca3b2482cc
Refactor ZEND_MM alignment check (#14720)
- Check added into a separate M4 macro
- AC_CACHE_CHECK used for running the test program with cache variable
  for optional edge-case overrides
- Help texts updated
- If check fails for some reason, the configure step emits error as
  also done in the Zend C code
- Cross-compilation values updated with type casts as done in the
  current conftest file output
2024-06-29 18:02:17 +02:00
David Carlier
58a6e5551e
Merge branch 'PHP-8.3' 2024-06-29 15:52:52 +01:00
David Carlier
1b71a9e01f
Merge branch 'PHP-8.2' into PHP-8.3 2024-06-29 15:52:28 +01:00
David Carlier
c03196a5be
Fix GH-14712: segfault on invalid object.
If the extension does not allow to get a property pointer (like PDORow
object), we fallback
to the read property cb anyway.
2024-06-29 15:51:57 +01:00
Peter Kokot
7f8e8a08d2
Use AC_CACHE_CHECK for float precision checks (#14663)
- AC_CACHE_CHECK simplifies message checkings and can speed up the
  consequtive configure -C checks
- AC_DEFINE descriptions style synced
- Used php_cv_* cache variables style
- Reduced over-quoted arguments to AC_LANG_PROGRAM
- Sync macro help text
2024-06-28 23:23:06 +02:00
Peter Kokot
bf7be07e7d
Refactor Zend signal handling check (#14678)
This wraps the Zend signal handling check in its own M4 macro for
readability and uses AS_VAR_IF, AS_VAR_APPEND for CFLAGS variable,
updates help texts and fixes CS.
2024-06-28 23:19:58 +02:00
Peter Kokot
1a5c411dc6
Wrap stack direction check in Zend M4 macro (#14694) 2024-06-28 23:07:40 +02:00
Niels Dossche
2fc27b7d2a
Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix GH-14590: Memory leak in FPM test gh13563-conf-bool-env.phpt
2024-06-28 22:10:23 +02:00
Niels Dossche
41371900a8
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-14590: Memory leak in FPM test gh13563-conf-bool-env.phpt
2024-06-28 22:08:18 +02:00
Niels Dossche
056bec72f4
Fix GH-14590: Memory leak in FPM test gh13563-conf-bool-env.phpt
Values retrieved from zend_getenv should be freed.
Note: The only possible value for `zend_getenv` is `sapi_getenv` which uses
zend alloc to duplicate the string that it reads from the SAPI module.

Closes GH-14708.
2024-06-28 22:05:15 +02:00
Peter Kokot
11f4683ee5
Wrap __cpuid_count check into ZEND_CHECK_CPUID_COUNT M4 macro (#14692)
Instead of checking this when including the Zend.m4 file in configure.ac
check is executed with the ZEND_INIT macro.
2024-06-27 20:16:40 +02:00
Peter Kokot
c7c02f2abf
Wrap global register variables check in M4 macro (#14683)
This adds ZEND_CHECK_GLOBAL_REGISTER_VARIABLES M4 macro to properly
sort the --enable-gcc-global-regs in configure --help output, fixes
over-quoted arguments, syncs CS and updates help texts.
2024-06-27 20:08:03 +02:00
Peter Kokot
604dafff3a
Rename and refactor Zend.m4 macros (#14671)
- LIBZEND_* -> ZEND_*
- A single "public" initialization M4 macro ZEND_INIT that wraps Zend
  engine related configure step checks and initialization.
2024-06-26 22:57:01 +02:00
Derick Rethans
9b5c15ba98 Remove superfluous whitespace 2024-06-26 08:06:41 -05:00
Derick Rethans
df7d304b62 Emit opcode for frameless functions at line number where the function call starts 2024-06-26 08:06:41 -05:00
David CARLIER
bc585cd87a
Fix GH-14643 ext/standard: segfault on user shutdown function release. (#14656) 2024-06-25 21:13:15 +01:00
Arnaud Le Blanc
f7df238971
Merge branch 'PHP-8.3'
* PHP-8.3:
  [ci skip] NEWS for GH-14626
  [ci skip] NEWS for GH-14626
  Fix is_zend_ptr() for huge blocks (#14626)
2024-06-25 15:18:58 +02:00
Arnaud Le Blanc
bc57c77fa2
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  [ci skip] NEWS for GH-14626
  Fix is_zend_ptr() for huge blocks (#14626)
2024-06-25 15:15:46 +02:00
Arnaud Le Blanc
1ff277dee2
Fix is_zend_ptr() for huge blocks (#14626)
is_zend_ptr() expected zend_mm_heap.huge_list to be circular, but it's in fact NULL-terminated. It could crash when at least one huge block exists and the ptr did not belong to any block.
2024-06-25 15:14:00 +02:00
Peter Kokot
f3feef8b93
Define default RE2C_FLAGS (#14615)
The --no-generation-date flag is a common re2c flag used in all re2c
invocations. This adds the 2nd optional argument to PHP_PROG_RE2C M4
macro in BC manner to set the default re2c command-line options and sets
the default RE2C_FLAGS similarly on Windows.
2024-06-24 22:09:04 +02:00
Julien Voisin
e3c9f5a585
Compute the size of pages before allocating memory (#14650)
`start_memory_manager()` calls `zend_mm_init()` via `alloc_globals_ctor()`
before setting `REAL_PAGE_SIZE` to the right value. Moving the `REAL_PAGE_SIZE`
setting block before the call to `alloc_globals_ctor()` makes the allocator
behave properly on systems with a page size different than 4k.

Suggested-by: arnaud-lb
2024-06-24 16:51:18 +02:00
Peter Kokot
df12ffcc77
Check for library functions on one place in Zend.m4 (#14635)
The m4_normalize is for Autoconf < 2.70 (on 2.70 and later versions a
blank-or-newline separated items can be expanded without using
backslash-newline).
2024-06-23 20:18:53 +02:00
Florian Engelhardt
f4557b48a6
Add gc and shutdown callbacks to ZendMM custom handlers (#13432) 2024-06-19 19:43:57 +02:00
Jakub Zelenka
387b1c62bf
PHP-8.3 is now for PHP-8.3.10-dev 2024-06-19 00:05:24 +01:00
Máté Kocsis
c588db1ee7
Use RETURN_THROWS() for WeakMap methods 2024-06-18 22:34:18 +02:00
Sergey Panteleev
49aaa7cd9f
PHP-8.2 is now for PHP 8.2.22-dev 2024-06-18 17:56:05 +03:00
Tim Düsterhus
2c5ed50d5c
zend_compile: Add support for %d to sprintf() optimization (#14561)
* zend_compile: Rename `string_placeholder_count` to `placeholder_count` in `zend_compile_func_sprintf()`

This is intended to make the diff of a follow-up commit smaller.

* zend_compile: Add support for `%d` to `sprintf()` optimization

This extends the existing `sprintf()` optimization by support for the `%d`
placeholder, which effectively equivalent to an `(int)` cast followed by a
`(string)` cast.

For a synthetic test using:

    <?php

    $a = 'foo';
    $b = 42;

    for ($i = 0; $i < 100_000_000; $i++) {
        sprintf("%s-%d", $a, $b);
    }

This optimization yields a 1.3× performance improvement:

    $ hyperfine 'sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php' \
          '/tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php'
    Benchmark 1: sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php
      Time (mean ± σ):      3.296 s ±  0.094 s    [User: 3.287 s, System: 0.005 s]
      Range (min … max):    3.213 s …  3.527 s    10 runs

    Benchmark 2: /tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php
      Time (mean ± σ):      4.300 s ±  0.025 s    [User: 4.290 s, System: 0.007 s]
      Range (min … max):    4.266 s …  4.334 s    10 runs

    Summary
      sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php ran
        1.30 ± 0.04 times faster than /tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php

* Fix sprintf_rope_optimization_003.phpt test expecation for 32-bit integers

* zend_compile: Indent switch-case labels in zend_compile_func_sprintf()

* Add GMP test to sprintf() rope optimization

* Add `%s` test case to sprintf() GMP test
2024-06-17 17:07:50 +02:00
Peter Kokot
a6b5439e2e
Fix warnings function declaration isn't a prototype (#14577)
This fixes the -Wstrict-prototypes warnings that might pop up in certain
builds.
2024-06-15 21:27:56 +02:00
Bob Weinand
6a2c5318f9
Optimize observers (#13649)
Inline the lookup whether a function is observed at all.
This strategy is also used for FRAMELESS calls. If the frameless call is observed, we instead allocate a call frame and push the arguments, to call the the function afterwards.
Doing so is still a performance benefit as opposed to executing individual INIT_FCALL+SEND_VAL ops. Thus, even if the frameless call turns out to be observed, the call overhead is slightly lower than before.
If the internal function is not observed at all, the unavoidable overhead is fetching the FLF zend_function pointer and the run-time cache needs to be inspected.

As part of this work, it turned out to be most viable to put the result operand on the ZEND_OP_DATA instead of ZEND_FRAMELESS_ICALL_3, allowing seamless interoperability with the DO_ICALL opcode.
This is a bit unusual in comparison to all other ZEND_OP_DATA usages, but seems to not pose problems overall.

There is also a small issue resolved: trampolines would always use the ZEND_CALL_TRAMPOLINE_SPEC_OBSERVER function due to zend_observer_fcall_op_array_extension being set to -1 too late.
2024-06-15 14:42:27 +02:00
David Carlier
2bb8fbd0a8
ext/pgsql: add pg_jit server info.
since PostgreSQL 11, LLVM JIT feature had been brought thus reporting
the settings to the client connection.

Close GH-14566
2024-06-14 16:27:00 +01:00
Niels Dossche
3b83d7e3b5 Zend: change uses of sprintf into snprintf 2024-06-14 08:12:03 -07:00
Tim Düsterhus
1e7aac315e
zend_compile: Optimize sprintf() into a rope (#14546)
* zend_compile: Add `zend_compile_rope_finalize()`

This just extracts the implementation as-is into a dedicated function to make
it reusable in preparation of a future commit.

* zend_compile: Use clearer parameter names for `zend_compile_rope_finalize()`

* zend_compile: Fix `zend_compile_rope_finalize()` for ropes containing a single constant string

Without this Opcache will trigger a use-after-free in
`zend_optimizer_compact_literals()`.

Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>

* zend_compile: Optimize `sprintf()` into a rope

This optimization will compile `sprintf()` using only `%s` placeholders into a
rope at compile time, effectively making those calls equivalent to the use of
string interpolation, with the added benefit of supporting arbitrary
expressions instead of just expressions starting with a `$`.

For a synthetic test using:

    <?php

    $a = 'foo';
    $b = 'bar';

    for ($i = 0; $i < 100_000_000; $i++) {
    	sprintf("%s-%s", $a, $b);
    }

This optimization yields a 2.1× performance improvement:

    $ hyperfine 'sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php' \
          '/tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php'
    Benchmark 1: sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php
      Time (mean ± σ):      1.869 s ±  0.033 s    [User: 1.865 s, System: 0.003 s]
      Range (min … max):    1.840 s …  1.945 s    10 runs

    Benchmark 2: /tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php
      Time (mean ± σ):      4.011 s ±  0.034 s    [User: 4.006 s, System: 0.005 s]
      Range (min … max):    3.964 s …  4.079 s    10 runs

    Summary
      sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php ran
        2.15 ± 0.04 times faster than /tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php

This optimization comes with a small and probably insignificant behavioral
change: If one of the values cannot be (cleanly) converted to a string, for
example when attempting to insert an object that is not `Stringable`, the
resulting Exception will naturally not show the `sprintf()` call in the
resulting stack trace, because there is no call to `sprintf()`.

Nevertheless it will correctly point out the line of the `sprintf()` call as
the source of the Exception, pointing the user towards the correct location.

* zend_compile: Eagerly handle empty format strings in `sprintf()` optimization

* zend_compile: Add additional explanatory comments to zend_compile_func_sprintf()

* Add zero-argument test to sprintf_rope_optimization_001.phpt

---------

Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>
2024-06-13 10:41:45 +02:00
Arnaud Le Blanc
25360ef249
Detect heap freelist corruption (#14054)
We keep track of free slots by organizing them in a linked list, with the
first word of every free slot being a pointer to the next one.

In order to make corruptions more difficult to exploit, we check the consistency
of these pointers before dereference by comparing them with a shadow. The shadow
is a copy of the pointer, stored at the end of the slot.

Before this change, an off-by-1 write is enough to produce a valid freelist
pointer. After this change, a bigger out of bound write is required for that.
The difficulty is increase further by mangling the shadow with a secret, and
byte-swapping it, which increases the minimal required out of bound write
length.

Closes GH-14054
2024-06-12 17:28:52 +02:00
Arnaud Le Blanc
d1048a0869
Add zend_random_bytes(), zend_random_bytes_insecure() functions (#14054)
Co-authored-by: Tim Düsterhus <tim@bastelstu.be>
2024-06-12 17:27:01 +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
Ilija Tovilo
998bce117c
Show enum cases in errors
Closes GH-14496
2024-06-10 22:58:25 +02:00
Levi Morrison
62bfb01e00
refactor: zend_call_stack_get_linux_pthread
GCC doesn't pessimize the error cases correctly:
https://godbolt.org/z/Pa6xsKMWc

This speeds up the happy case and in this case the code size is also
smaller, so it's a double-win.
2024-06-10 10:40:40 -06:00
Florian Engelhardt
159f14c45f
fix memleak due to missing pthread_attr_destroy()-call
Closes GH-14510
2024-06-10 16:09:26 +02:00
Florian Engelhardt
3c65375adb
fix memleak due to missing pthread_attr_destroy()-call
Closes GH-14510
2024-06-10 16:07:12 +02:00