Commit graph

513 commits

Author SHA1 Message Date
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
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
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
Nikita Popov
989205e95c Remove incorrect uses of zend_atoi()
zend_atoi() parses integers with size suffixes (like "128M").
These just want to use a plain number, so use ZEND_ATOL instead.
2021-07-12 16:56:00 +02:00
Patrick Allaert
aff365871a Fixed some spaces used instead of tabs 2021-06-29 11:30:26 +02:00
Nikita Popov
e9b005158f Fix output buffer discard on memory limit
Move this code directly into the error handler, and check the
heap->overflow flag. Discarding output here allows us to print
the normal memory limit message to standard output. Otherwise
nothing would be printed unless a different log medium was used,
which makes for a suboptimal debugging experience.
2021-05-31 16:30:36 +02:00
Peter van Dommelen
3a4ea6cb91 Don't automatically adjust memory_limit to 2M
As PHP has a minimum memory usage of 2M (size of allocator chunk),
setting a limit below that value is not meaningful and will be
automatically rounded up to the chunk size. Rather than doing this
silently, show the newly introduced error message.

The memory limit had to be increased to 2M for a number of tests.

tests/lang/bug45392 has been marked as XFAIL. This old bugfix is
not working as intended. The memory limit in main's `PG(memory_limit)`
differs from the one in zend_alloc. In zend_alloc the `AG(mm_heap)->limit`
is defined as `max(passed_value, ZEND_MM_CHUNK_SIZE)`. The check made in
an unclean shutdown will never be true unless the memory limit is lower
than ZEND_MM_CHUNK_SIZE, which happened to be the case in the test.
https://bugs.php.net/bug.php?id=45392
fcc0fdd125
2021-05-31 15:42:10 +02:00
Nikita Popov
1aafed5e98 Remove zend_set_memory_limit_ex() API
This was added temporarily for the PHP-8.0 branch to avoid an
ABI break.
2021-05-31 15:27:46 +02:00
Nikita Popov
b4559e7ae4 Merge branch 'PHP-8.0'
* PHP-8.0:
  Fixed bug #81070
2021-05-31 15:24:29 +02:00
Nikita Popov
324ad2f42c Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4:
  Fixed bug #81070

This is a non-trivial merge. To avoid an ABI break, a new
zend_set_memory_limit_ex() function is added.
2021-05-31 15:24:00 +02:00
Peter van Dommelen
1b3b5c94e5 Fixed bug #81070
When the memory limit is reduced using an `ini_set("memory_limit", ..)`
below the currently allocated memory, the out-of-memory check overflowed.
Instead of implementing additional checks during allocation,
`zend_set_memory_limit()` now validates the new memory limit. When
below the current memory usage the ini_set call will fail and throw
a warning.

This is part of GH-7040.
2021-05-31 15:18:58 +02:00
George Peter Banyard
c40231afbf
Mark various functions with void arguments.
This fixes a bunch of [-Wstrict-prototypes] warning,
because in C func() and func(void) have different semantics.
2021-05-12 14:55:53 +01:00
Josh Soref
462da6e09c Fix spelling and grammar mistakes
This PR corrects misspellings identified by the check-spelling action.

The misspellings have been reported at jsoref@b6ba3e2#commitcomment-48946465

The action reports that the changes in this PR would make it happy: jsoref@602417c

Closes GH-6822.
2021-04-13 12:09:37 +02:00
Dmitry Stogov
70bb12feec Merge branch 'PHP-8.0'
* PHP-8.0:
  Update zend_alloc.c
2021-04-12 09:28:57 +03:00
Dmitry Stogov
9c6b926dae Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4:
  Update zend_alloc.c
2021-04-12 09:28:41 +03:00
fishmore
600402d986 Update zend_alloc.c
Don't free main thunk.  It will crash.
2021-04-12 09:28:24 +03:00
David CARLIER
2b93ae601a Use VM_MAKE_TAG for macos memory tag
In case Apple changes the meaning of the macro in the future.

Closes GH-6687.
2021-02-15 09:48:04 +01:00
Nikita Popov
3e01f5afb1 Replace zend_bool uses with bool
We're starting to see a mix between uses of zend_bool and bool.
Replace all usages with the standard bool type everywhere.

Of course, zend_bool is retained as an alias.
2021-01-15 12:33:06 +01:00
Levi Morrison
95a4e1ea3b Fix ubsan error on Mac
Fixes this error:
> Zend/zend_alloc.c:473:73: runtime error: left shift of 250 by 24 places cannot be represented in type 'int'
2020-12-16 21:24:31 -07:00
George Peter Banyard
fa8d9b1183 Improve type declarations for Zend APIs
Voidification of Zend API which always succeeded
Use bool argument types instead of int for boolean arguments
Use bool return type for functions which return true/false (1/0)
Use zend_result return type for functions which return SUCCESS/FAILURE as they don't follow normal boolean semantics

Closes GH-6002
2020-08-28 15:41:27 +02:00
Nikita Popov
5db44e39b1 Fix tracked_realloc
We should only drop the information about the old allocation after
checking the memory limit. This makes the code a bit more awkward...
2020-08-28 11:37:42 +02:00
Nikita Popov
bd6feb7b12 Enforce memory limit in tracked allocation mode
A very basic limit (for single allocations) was already enforced.
This extends it to count the total memory allocations.

This is useful to avoid out of memory conditions while fuzzing.
2020-08-27 10:18:18 +02:00
tangl163
7d4ae7fa23 Drop the unneeded pointer casting
The standard says that "A pointer to void may be converted to or from a
pointer to any object type". So the casting is unneeded.

REF:
    * c11: http://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p1
    * c99: http://port70.net/~nsz/c/c99/n1256.html

Closes GH-5916
2020-08-03 02:05:14 +01:00
Nikita Popov
370c00e9cf Add crude memory limit to tracked alloc
Check whether the requested allocation size exceeds limit (rather
than the cumulative size).

This is useful to prevent allocations triggering OOM during fuzzing.
2020-04-21 10:44:15 +02:00
Dmitry Stogov
4786207ecc Merge branch 'PHP-7.4'
* PHP-7.4:
  Fixed incorrect behavior of internal memory debugger
2020-02-27 12:28:09 +03:00
Dmitry Stogov
d9f56903ad Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3:
  Fixed incorrect behavior of internal memory debugger
2020-02-27 12:27:58 +03:00
Dmitry Stogov
45b4368d5c Fixed incorrect behavior of internal memory debugger 2020-02-27 12:27:22 +03:00
Tyson Andre
9d48bf5152 Fix miscellaneous typos in docs and error messages
Closes GH-4863.
2019-10-28 11:44:08 +01:00