Commit graph

555 commits

Author SHA1 Message Date
Arnaud Le Blanc
c561f7da85
Refresh zend_mm shadow key on fork
The shadow key is refreshed when resetting the memory manager between two
requests. But in forking SAPIs the first request of a child process inherits the
shadow key of the parent. As a result, a leak of the shadow key during the first
request of one process gives away the shadow key used during the first request
of other processes. This makes the key refresh mechanism less useful.

Here I ensure that we refresh the shadow key after a fork. We can not reset the
manager as there may be active allocations. Instead, we have to recompute shadow
pointers with the new key.

Closes GH-16765
2025-07-29 14:00:37 +02:00
Ilija Tovilo
feb1d63771
Merge branch 'PHP-8.4'
* PHP-8.4:
  Track heap->real_size for USE_TRACKED_ALLOC
2025-06-20 14:51:29 +02:00
Ilija Tovilo
0ee73ccbe5
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Track heap->real_size for USE_TRACKED_ALLOC
2025-06-20 14:51:20 +02:00
Ilija Tovilo
dfc4caa1e4
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Track heap->real_size for USE_TRACKED_ALLOC
2025-06-20 14:51:00 +02:00
Ilija Tovilo
7841c8a3df
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Track heap->real_size for USE_TRACKED_ALLOC
2025-06-20 14:49:47 +02:00
Ilija Tovilo
9cacc57350
Track heap->real_size for USE_TRACKED_ALLOC
real_size is returned by memory_get_usage(true), which previously returned 0.
Discovered in Symfony ConsumeMessagesCommandTest::testRunWithMemoryLimit()
through nightly.

Closes GH-18880
2025-06-20 14:48:47 +02:00
Niels Dossche
8a3201d91e
Merge branch 'PHP-8.4'
* PHP-8.4:
  zend_alloc: Fix compile with ZEND_MM_STAT=0
2025-06-09 17:28:11 +02:00
Niels Dossche
fc89d1968c
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  zend_alloc: Fix compile with ZEND_MM_STAT=0
2025-06-09 17:28:06 +02:00
Niels Dossche
d11f9717fd
zend_alloc: Fix compile with ZEND_MM_STAT=0
Closes GH-18811.
2025-06-09 17:27:39 +02:00
Niels Dossche
7b6c0b99bb
zend_alloc: Fix compilation with ZEND_MM_CUSTOM=0 (#18808)
The poison feature relies on ZEND_MM_CUSTOM=1.
If ZEND_MM_CUSTOM=0, the build fails.
To fix this, move some `#endif`.
2025-06-09 12:44:41 +02:00
Niels Dossche
502c68264d
Merge branch 'PHP-8.4'
* PHP-8.4:
  pdo_odbc: Fix memory leak if WideCharToMultiByte() fails
  Fix memory leak on php_odbc_fetch_hash() failure
  Do not delete main chunk in zend_gc
2025-06-09 11:26:27 +02:00
Niels Dossche
5526301908
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  pdo_odbc: Fix memory leak if WideCharToMultiByte() fails
  Fix memory leak on php_odbc_fetch_hash() failure
  Do not delete main chunk in zend_gc
2025-06-09 11:25:41 +02:00
Daniil Gentili
9a9d98e02f
Do not delete main chunk in zend_gc
Closes GH-18756.

Co-authored-by: Arnaud Le Blanc <arnaud.lb@gmail.com>
2025-06-09 11:23:29 +02:00
Niels Dossche
99d56248f8
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix compile without ZEND_MM_STORAGE
2025-06-05 21:51:44 +02:00
Niels Dossche
976143e365
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix compile without ZEND_MM_STORAGE
2025-06-05 21:51:38 +02:00
Niels Dossche
4162c20787
Fix compile without ZEND_MM_STORAGE 2025-06-05 21:51:30 +02:00
Arnaud Le Blanc
cb245411b0
Add runtime-enabled heap debugging capabilities (#18172)
Debugging memory corruption issues in production can be difficult when it's not possible to use a debug build or ASAN/MSAN/Valgrind (e.g. for performance reasons).

This change makes it possible to enable some basic heap debugging helpers without rebuilding PHP. This is controlled by the environment variable ZEND_MM_DEBUG. The env var takes a comma-separated list of parameters:

- poison_free=byte: Override freed blocks with the specified byte value (represented as a number)
- poison_alloc=byte: Override newly allocated blocks with the specified byte value (represented as a number)
- padding=bytes: Pad allocated blocks with the specified amount of bytes (if non-zero, a value >= 16 is recommended to not break alignments) 
- check_freelists_on_shutdown=0|1: Enable checking freelist consistency [1] on shutdown

Example:

    ZEND_MM_DEBUG=poison_free=0xbe,poison_alloc=0xeb,padding=16,check_freelists_on_shutdown=1 php ...

This is implemented by installing custom handlers when ZEND_MM_DEBUG is set.

This has zero overhead when ZEND_MM_DEBUG is not set. When ZEND_MM_DEBUG is set, the overhead is about 8.5% on the Symfony Demo benchmark.

Goals:

 - Crash earlier after a memory corruption, to extract a useful backtrace
 - Be usable in production with reasonable overhead
 - Having zero overhead when not enabled

Non-goals:

 - Replace debug builds, valgrind, ASAN, MSAN or other sanitizers

[1] https://github.com/php/php-src/pull/14054

Co-authored-by: Tim Düsterhus <timwolla@googlemail.com>
2025-03-31 17:24:42 +02:00
Niels Dossche
8fdcd9f051
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix is_zend_ptr() huge block comparison
2024-11-26 19:24:47 +01:00
Niels Dossche
02b1056714
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix is_zend_ptr() huge block comparison
2024-11-26 19:24:31 +01:00
Niels Dossche
18674e39ad
Fix is_zend_ptr() huge block comparison
We should compare the block memory, not the block metadata (See
zend_mm_add_huge_block).
This caused random test failure for ext/ffi/tests/gh14626.phpt when the
malloc() performed by the FFI code lies close to the block metadata, and
the size of the block is large enough.

This was reported by https://github.com/php/php-src/issues/16902#issuecomment-2498310452

Closes GH-16938.
2024-11-26 19:24:05 +01:00
Saki Takamachi
8a4a30469a
Zend: Add ZEND_BYTES_SWAP32/ZEND_BYTES_SWAP64 (#14910) 2024-07-22 17:57:16 +09:00
Julien Voisin
c2cbbd9b98
zend_alloc: remove duplicate zend_mm_alloc_large definition (#14880)
The two parts of the #if condition are the same.
2024-07-09 06:46:00 +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
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
Florian Engelhardt
f4557b48a6
Add gc and shutdown callbacks to ZendMM custom handlers (#13432) 2024-06-19 19:43:57 +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
Julien Voisin
07337df1d7
Add two checks for zend_mm_heap's integrity (#13943) 2024-04-23 11:50:24 +02:00
David CARLIER
7a3516cca5
zend_alloc trailing 1 calculation helper ZEND_ATTRIBUTE_CONST addition. (#13874) 2024-04-03 13:46:07 +01: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
Ilija Tovilo
016c3861d7
Fix asan false positive for mmap
For some reason, mmap regions which are repeatedly munmapped are not correctly
unpoisoned. See https://github.com/google/sanitizers/issues/1705.

Fixes GH-12756
Closes GH-12848
2023-12-05 12:18:09 +01:00
Niels Dossche
6537811527 Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix unspecified behaviour in zend_alloc in heap->limit computation
2023-11-06 19:47:04 +01:00
Niels Dossche
85cb081661 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix unspecified behaviour in zend_alloc in heap->limit computation
2023-11-06 19:46:57 +01:00
Niels Dossche
7ac9578e41 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix unspecified behaviour in zend_alloc in heap->limit computation
2023-11-06 19:44:33 +01:00
Niels Dossche
28110f8d0a Fix unspecified behaviour in zend_alloc in heap->limit computation
Right-shifting a negative number is unspecified (i.e.
implementation-defined) behaviour [1]. If we take a look at the
generated assembly [2], we see that the wrong value is computed.
Fix it by using Z_UL instead of Z_L.

While we're at it, just change every occurrence of this pattern to use
Z_UL instead of casting.

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf §6.5.7.5
[2] https://godbolt.org/z/4Y1qKKjsh

Closes GH-12613.
2023-11-06 19:43:55 +01:00
Ilija Tovilo
2227fefa17
Fix noreturn with warning that should be an error
E_WARNING does not actually abort.
2023-09-14 11:29:53 +02:00
Ilija Tovilo
3148da8ee1
Add block size support for tracked_malloc (#11856)
This does still deviate from USE_ZEND_ALLOC=0 in that we're not rounding up the
size of the allocation to fixed sizes. Doing so would suppress some
out-of-bounds errors checked by ASAN. Rounding up the size in
_zend_mm_block_size would not be good either as it would break code like
memset(ptr, 0 _zend_mm_block_size(ptr)).
2023-08-03 10:08:41 +02:00
Dmitry Stogov
a95316fc79 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fixed incorrect tracked malloc deallocation
2023-08-01 16:02:08 +03:00
Dmitry Stogov
5abf4f232e Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fixed incorrect tracked malloc deallocation
2023-08-01 16:01:57 +03:00
Dmitry Stogov
4553258df3 Fixed incorrect tracked malloc deallocation
Fixes ext/ffi/tests/list.phpt failure
2023-08-01 16:01:11 +03:00
Ilija Tovilo
fc88f155be
Add zend_alloc XLEAK support
In the future we may want to use a different exit code to warn for tests that
didn't leak.

Closes GH-10999
2023-04-03 12:55:26 +02:00
Max Kellermann
c0d89e54c8
Zend/zend_alloc: make stderr_last_error() static (#10587)
This function is only used internally.
2023-02-18 19:39:54 +00:00
Max Kellermann
413844d626
Zend/zend_types.h: deprecate zend_bool, zend_intptr_t, zend_uintptr_t (#10597)
These types are standard C99.

For compatibility with out-of-tree extensions, keep the typedefs
in main/php.h.
2023-02-18 19:31:28 +00:00
Christoph M. Becker
2d3427c507
Revert "#include cleanup (#10216)"
Cf. <https://github.com/php/php-src/pull/10220#issuecomment-1383739816>.

This reverts commit e628c66f9d.
2023-01-16 12:29:41 +01:00
Max Kellermann
e628c66f9d
#include cleanup (#10216)
Shift header include

In the C file, include the header first so missing #includes are
detected by the compiler, and use lighter header dependencies in the
header, to speed up compile times.
2023-01-04 13:24:28 +00:00
Christoph M. Becker
c6204ac930
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix GH-9650: Can't initialize heap: [0x000001e7]
2022-11-17 14:18:51 +01:00
Michael Voříšek
8d65c2fee5
Fix GH-9650: Can't initialize heap: [0x000001e7]
Closes GH-9721.
2022-11-17 14:16:10 +01:00
Ilija Tovilo
98bdb7f99b
Make pestr[n]dup infallible (#9295)
Fixes GH-9128
Closes GH-9295
2022-08-12 12:21:14 +02:00
Ilija Tovilo
cd363a9b1b
Specify unit in out of memory error (#8820)
Closes GH-8808
2022-06-21 12:37:38 +01:00