Commit graph

538 commits

Author SHA1 Message Date
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
Max Kellermann
e67565f54c
Zend, ext/opcache: use PR_SET_VMA_ANON_NAME (Linux 5.17) (#8234)
The new Linux 5.17 feature PR_SET_VMA_ANON_NAME can give names to
anonymous private memory, see:

 https://lwn.net/Articles/867818/

It can be useful while debugging, to identify which portion of the
process's memory belongs to which subsystem.

This is how /proc/PID/maps can look like:

 555ccd400000-555ccdc00000 r-xp 00000000 00:00 0                          [anon:huge_code_pages]
 7f6ec6600000-7f6ec6800000 rw-p 00000000 00:00 0                          [anon:zend_alloc]

The first mapping is the PHP executable copied to anonymous memory by
option "opcache.huge_code_pages".  The second one is a memory area for
the "zend_alloc.h" memory allocator library.

Unfortunately, it is not possible to give names to shared memory
(MAP_SHARED),  because Linux MAP_SHARED really maps /dev/zero (see
shmem_zero_setup()), which makes madvise_vma_anon_name() believe this
is a file mapping, failing the prctl() with EBADF.
2022-06-20 12:27:01 +01:00
Arnaud Le Blanc
96838129ea
Panic with abort() (#8590) 2022-05-21 15:07:00 +02:00
David CARLIER
31692a16af
Support zend alloc USE_ZEND_ALLOC_HUGE_PAGES option on MacOS
ZEND_MM_CHUNK_SIZE fits the VM_FLAGS_SUPERPAGE_SIZE_2MB special
file descriptor for mmap call.
2022-04-29 22:12:25 +01:00
Ilija Tovilo
ca134f7a3e
Remove unused include of stdbool.h 2022-03-20 20:39:39 +01:00
Max Kellermann
a83cc9d397
Zend/zend_alloc: use bool and make internal variable static (#8230)
* Zend/zend_alloc: make zend_mm_use_huge_pages static

This is an internal variable and it should not be exported.

* Zend/zend_alloc: convert zend_mm_use_huge_pages to bool

* Zend/zend_alloc: convert has_free_pages to bool

* Zend/zend_alloc: convert empty to bool
2022-03-20 20:30:03 +01:00
David CARLIER
c9385ee1ad
zend_mm_map_fixed using MAP_TRYFIXED on NetBSD.DragonFlyBSD
attempts to map on addr but does not replace it if already present.
Note on OpenBSD it has no effect, addr is used just as a hint.

Closes GH-7923.
2022-03-19 12:12:00 +01:00
Patrick Allaert
67440096c5
Added: [zend_]memory_reset_peak_usage() (#8151) 2022-03-04 13:24:08 +01:00
Patrick Allaert
8c60e21515
Avoid possible [-Wstrict-prototypes] build warnings 2022-02-24 16:14:47 +01:00
Dmitry Stogov
68a51e3350 Merge branch 'PHP-8.1'
* PHP-8.1:
  Free cached chunks when the requested memory limit is above real usage
2022-02-08 15:48:40 +03:00
Dmitry Stogov
c7558e2fe1 Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Free cached chunks when the requested memory limit is above real usage
2022-02-08 15:48:31 +03:00
Dmitry Stogov
c035298eb2 Free cached chunks when the requested memory limit is above real usage 2022-02-08 15:45:40 +03:00
David CARLIER
1a4d2dd962
zend alloc USE_ZEND_ALLOC_HUGE_PAGES option support on solaris based systems (#7789) 2021-12-21 07:18:11 +01:00
Christoph M. Becker
4013ebe431
Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix #81585: cached_chunks are not counted to real_size on shutdown
2021-12-10 12:26:17 +01:00
Christoph M. Becker
2362722dcf
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Fix #81585: cached_chunks are not counted to real_size on shutdown
2021-12-10 12:25:44 +01:00
Christoph M. Becker
5675ebe649
Fix #81585: cached_chunks are not counted to real_size on shutdown
The amount of allocated system memory is kept in `real_size`, including
the allocated `cached_chunks`.  Thus, we need to keep the proper count
at the end of the shutdown.

Closes GH-7745.
2021-12-10 12:24:06 +01:00
Nikita Popov
1553dfaf42 Avoid __zend_malloc() wrapper in tracked_malloc()
oss-fuzz currently coalesces all leaks into one issue, presumably
because the five lowest stack frames always look the same. Let's
see whether dropping the __zend_malloc() frame helps.
2021-09-27 16:50:59 +02:00
Nikita Popov
cce31657d6 Fix typo
Accidentially dropped the "!" here.
2021-07-13 14:00:18 +02:00