Commit Graph

237 Commits

Author SHA1 Message Date
Niels Dossche
64f2d11e38
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix GH-16628: FPM logs are getting corrupted with this log statement
  Fix GH-16601: Memory leak in Reflection constructors
2024-11-02 19:39:00 +01:00
Niels Dossche
bfd9e0cca3
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-16628: FPM logs are getting corrupted with this log statement
  Fix GH-16601: Memory leak in Reflection constructors
2024-11-02 19:38:54 +01:00
Niels Dossche
16cda10650
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-16628: FPM logs are getting corrupted with this log statement
  Fix GH-16601: Memory leak in Reflection constructors
2024-11-02 19:37:28 +01:00
Niels Dossche
e643129bbb
Fix GH-16628: FPM logs are getting corrupted with this log statement
zlog_buf_prefix() can return a larger length than what actually was
written due to its use of snprintf(). The code in
zlog_stream_prefix_ex() does not take this into account, other callers
do. What ends up happening then is that stream->length is set to the
length as if snprintf() was able to write all bytes, causing
stream->length to become larger than stream->buf.size, causing a
segfault.

In case the buffer was too small we try with a larger buffer up to a
limit of zlog_limit. This makes sure that the stream length will remain
bounded by the buffer size.

This also adds assertions to make the programmer intent clear and catch
this more easily in debug builds.

Closes GH-16680.
2024-11-02 19:36:20 +01:00
Christoph M. Becker
836a162089
Don't fiddle with NDEBUG in C code (GH-16511)
* Don't fiddle with NDEBUG in C code

It is way to late to do this in php.h, since assert.h has already been
included.  Even pushing that down to zend_portability.h may not have
the desired effect.  Instead we define or undefine NDEBUG as CFLAG, so
that it works in all circumstances.

As a last resort we fail at build time, if `NDEBUG` is defined when
`ZEND_DEBUG` is enabled.

We also remove the useless workaround in zend_test to include assert.h
again, since that usually won't have any effect anyway.

Co-authored-by: Arnaud Le Blanc <arnaud.lb@gmail.com>
2024-10-27 18:20:59 +01:00
Christoph M. Becker
50acf5eb15
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix GH-16266: _ZendTestClass::test() segfaults on named parameter
2024-10-21 19:46:34 +02:00
Christoph M. Becker
241e3e0e27
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-16266: _ZendTestClass::test() segfaults on named parameter
2024-10-21 19:46:02 +02:00
Christoph M. Becker
713c71adbd
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-16266: _ZendTestClass::test() segfaults on named parameter
2024-10-21 19:44:34 +02:00
Christoph M. Becker
b73bcaa47c
Fix GH-16266: _ZendTestClass::test() segfaults on named parameter
We need to assign the proper number of arguments.

Closes GH-16271.
2024-10-21 19:43:33 +02:00
Tim Düsterhus
3401d55726
zend_weakrefs: Add zend_weakrefs_hash_(clean|destroy)() (#16439)
These are equivalent to `zend_hash_clean()` and `zend_hash_destroy()`
respectively, but take care of correctly unregistering the weak references to
the keys.

This addition rounds off the weakmap functionality added in
471102edcd by taking one possible footgun away
from the user.
2024-10-14 18:57:08 +02:00
Niels Dossche
b24cc7386b
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fixed GH-16233: Observer segfault when calling user function in internal function via trampoline
2024-10-07 17:18:32 +02:00
Niels Dossche
0338008852
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fixed GH-16233: Observer segfault when calling user function in internal function via trampoline
2024-10-07 17:17:27 +02:00
Niels Dossche
e715dd0afb
Fixed GH-16233: Observer segfault when calling user function in internal function via trampoline
In the test, I have an internal `__call` function for `_ZendTestMagicCallForward` that calls the global function with name `$name` via `call_user_function`.
Note that observer writes the pointer to the previously observed frame in the last temporary of the new call frame (`*prev_observed_frame`).

The following happens:
First, we call `$test->callee`, this will be handled via a trampoline with T=2 for the two arguments. The call frame is allocated at this point. This call frame is not observed because it has `ZEND_ACC_CALL_VIA_TRAMPOLINE` set. Next we use `ZEND_CALL_TRAMPOLINE` to call the trampoline, this reuses the stack frame allocated earlier with T=2, but this time it is observed. The pointer to the previous frame is written outside of the call frame because `T` is too small (should be 3). We are now in the internal function `_ZendTestMagicCallForward::__call` where we call the global function `callee`. This will push a new call frame which will overlap `*prev_observed_frame`. This value gets overwritten by `zend_init_func_execute_data` when `EX(opline)` is set because `*prev_observed_frame` overlaps with `EX(opline)`. From now on, `*prev_observed_frame` is corrupted. When `zend_observer_fcall_end` is called this will result in reading wrong value `*prev_observed_frame` into `current_observed_frame`. This causes issues in `zend_observer_fcall_end_all` leading to the segfault we observe.

Despite function with `ZEND_ACC_CALL_VIA_TRAMPOLINE` not being observed, the reuse of call frames makes problems when `T` is not large enough.
To fix this, we make sure to add 1 to `T` if `ZEND_OBSERVER_ENABLED` is true.

Closes GH-16252.
2024-10-07 17:16:43 +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
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
Florian Engelhardt
f4557b48a6
Add gc and shutdown callbacks to ZendMM custom handlers (#13432) 2024-06-19 19:43:57 +02:00
Tim Düsterhus
a1ea464069
gen_stub: Intern the parameter name string for named arguments in internal attributes (#14595)
This is necessary because `zend_get_attribute_object()` will use the persistent
string with the parameter name as the index for a newly created non-persistent
HashTable, which is not legal.

As parameter names are expected to be short-ish, reasonably common terms and
need to sit around in memory anyways, we might as well make them an interned
string, circumstepping the issue without needing to duplicate the parameter
name into a non-persistent string.
2024-06-19 08:06:50 +02:00
Niels Dossche
bcecbb59d3
Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix GH-11078: PHP Fatal error triggers pointer being freed was not allocated and malloc: double free for ptr errors
2024-06-10 19:40:03 +02:00
Niels Dossche
ccdd1c4e67
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-11078: PHP Fatal error triggers pointer being freed was not allocated and malloc: double free for ptr errors
2024-06-10 19:39:25 +02:00
Niels Dossche
bc558bf7a3
Fix GH-11078: PHP Fatal error triggers pointer being freed was not allocated and malloc: double free for ptr errors
Although the issue was demonstrated using Curl, the issue is purely in
the streams layer of PHP.

Full analysis is written in GH-11078 [1], but here is the brief version:
Here's what actually happens:
1) We're creating a FILE handle from a stream using the casting mechanism.
   This will create a cookie-based FILE handle using funopen.
2) We're reading stream data using fread from the userspace stream. This will
   temporarily set a buffer into a field _bf.base [2]. This buffer is now equal
   to the upload buffer that Curl allocated and note that that buffer is owned
   by Curl.
3) The fatal error occurs and we bail out from the fread function, notice how
   the reset code is never executed and so the buffer will still point to
   Curl's upload buffer instead of FILE's own buffer [3].
4) The resources are destroyed, this includes our opened stream and because the
   FILE handle is cached, it gets destroyed as well.
   In fact, the stream code calls through fclose on purpose in this case.
5) The fclose code frees the _bs.base buffer [4].
   However, this is not the buffer that FILE owns but the one that Curl owns
   because it isn't reset properly due to the bailout!
6) The objects are getting destroyed, and so the curl free logic is invoked.
   When Curl tries to gracefully clean up, it tries to free the buffer.
   But that buffer is actually already freed mistakingly by the C library!

This also explains why we can't reproduce it on Linux: this bizarre buffer
swapping only happens on macOS and BSD, not on Linux.

To solve this, we switch to an unbuffered mode for cookie-based FILEs.
This avoids any stateful problems related to buffers especially when the
bailout mechanism triggers. As streams have their own buffering
mechanism, I don't expect this to impact performance.

[1] https://github.com/php/php-src/issues/11078#issuecomment-2155616843
[2] 5e566be7a7/stdio/FreeBSD/fread.c (L102-L103)
[3] 5e566be7a7/stdio/FreeBSD/fread.c (L117)
[4] 5e566be7a7/stdio/FreeBSD/fclose.c (L66-L67)

Closes GH-14524.
2024-06-10 19:38:21 +02:00
Gina Peter Banyard
51379d66ec
Zend: Add object_init_with_constructor() API (#14440)
This will instantiate the object and execute its constructor with the given parameters.
2024-06-06 21:21:16 +01:00
Gina Peter Banyard
e54f564a0a ext/zend_test: Fix [-Wsign-compare] warnings 2024-06-06 16:18:23 +01:00
Peter Kokot
e45d2d6046
Sync HAVE_BUNDLED_PCRE #if/ifdef/defined (#14354)
Follow up of GH-5526 (-Wundef)
2024-05-29 07:53:36 +02:00
Niels Dossche
889f308e01
Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix GH-14215: Cannot use FFI::load on CRLF header file with apache2handler
2024-05-14 19:56:05 +02:00
Niels Dossche
0e98a05a3d
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-14215: Cannot use FFI::load on CRLF header file with apache2handler
2024-05-14 19:52:19 +02:00
Niels Dossche
ebd1a36670
Fix GH-14215: Cannot use FFI::load on CRLF header file with apache2handler
Some modules may reset _fmode, which causes mangling of line endings.
Always be explicit like we do in other places where the native open call
is used.

Closes GH-14218.
2024-05-14 19:49:22 +02:00
David Carlier
250b160886 Merge branch 'PHP-8.3' 2024-03-14 18:59:57 +00:00
David Carlier
c24f621f57 Merge branch 'PHP-8.2' into PHP-8.3 2024-03-14 18:59:44 +00:00
David Carlier
db1f7b1286 zend_test fix copy_file_range test for linux 32 bits
close GH-13708
2024-03-14 18:59:26 +00:00
David Carlier
c2d20f48c5 Merge branch 'PHP-8.3' 2024-03-13 19:35:50 +00:00
David Carlier
bbb6ffa545 Merge branch 'PHP-8.2' into PHP-8.3 2024-03-13 19:35:36 +00:00
David Carlier
334419e157 zend test fix copy_file_range for musl.
normally should no longer need off64_t with glibc anyway.
2024-03-13 19:35:23 +00:00
Florian Engelhardt
14873dd286
Drop zend_mm_set_custom_debug_handlers() (#13457)
Simplifies zend_mm_set_custom_debug_handlers to just use zend_mm_set_custom_handlers(), saving some conditionals when the Zend allocator is not used.
2024-02-26 14:04:33 +01:00
Máté Kocsis
f2e199e878
Implement "support doc comments for internal classes and functions" (#13266)
Fixes #13130
2024-02-25 08:41:31 +01:00
Peter Kokot
9628ca7bd2
Fix zend_test extension name (#13321)
The zend_test extension was renamed from zend-test to zend_test in
dbe5725ff3. This only syncs few minor
remainings.
2024-02-04 19:27:58 +01:00
Niels Dossche
6f460fd272 Use diagnostic macros for the warning 2023-12-07 00:05:36 +01:00
Niels Dossche
f8d50647ea Merge branch 'PHP-8.3'
* PHP-8.3:
  Silence deprecations in zend-test
2023-12-06 23:49:34 +01:00
Niels Dossche
f6268b87c7 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Silence deprecations in zend-test
2023-12-06 23:49:25 +01:00
Niels Dossche
de3c5c0bc8 Silence deprecations in zend-test
Forgot it here...
2023-12-06 23:49:08 +01:00
Ilija Tovilo
30acbba2ca
Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix leak of call->extra_named_params on internal __call
2023-12-01 16:50:56 +01:00
Ilija Tovilo
b7a468cd06
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix leak of call->extra_named_params on internal __call
2023-12-01 16:50:49 +01:00
Ilija Tovilo
f203edd3c5
Fix leak of call->extra_named_params on internal __call
Fixes GH-12835
Closes GH-12836
2023-12-01 16:49:16 +01:00
Bob Weinand
78364ef97e Merge branch 'PHP-8.3' 2023-11-25 01:02:52 +01:00
Bob Weinand
88537c55b5 Merge branch 'PHP-8.2' into PHP-8.3 2023-11-25 01:01:56 +01:00
Bob Weinand
50ccea31f2 Merge branch 'PHP-8.1' into PHP-8.2 2023-11-25 00:59:26 +01:00
Florian Engelhardt
8d2df86b06
Fix invalid opline in OOM handlers within ZEND_FUNC_GET_ARGS and ZEND_BIND_STATIC (#12768)
* fix segfault in `ZEND_BIND_STATIC`

In case a `ZEND_BIND_STATIC` is being executed, while the current chunk is full,
the `zend_array_dup()` call will trigger a OOM in ZendMM which will crash, as
the opline might be a dangling pointer.

* add missing test

* `assert()`ing seems easier than trying to make the compiler to not optimize

* moved from function call to INI setting, so we can use this in other places as well

* make `assert()` work no NDEBUG builds

* document magic number

* fix segfault in `ZEND_FUNC_GET_ARGS`

In case a `ZEND_FUNC_GET_ARGS` is being executed, while the current chunk is
full, the `zend_new_array()` call will trigger a OOM in ZendMM which will crash,
as the opline might be a dangling pointer.

---------

Co-authored-by: Florian Engelhardt <florian@engelhardt.tc>
2023-11-25 00:54:02 +01:00
Niels Dossche
490b80858d Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix GH-12633: sqlite3_defensive.phpt fails with sqlite 3.44.0
  Fix GH-12628: The gh11374 test fails on Alpinelinux
2023-11-10 00:13:15 +01:00
Niels Dossche
e6fef2944b Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-12633: sqlite3_defensive.phpt fails with sqlite 3.44.0
  Fix GH-12628: The gh11374 test fails on Alpinelinux
2023-11-10 00:12:47 +01:00