Skip to content

Commit

Permalink
Do not create the opcache shm mapping too close to the end of the heap
Browse files Browse the repository at this point in the history
Under some conditions we may create the opcache shm segment just after the
heap, which prevents it from expanding. Memory allocators may fallback to
mmap(), but this may confuse some (unidentified) code.
  • Loading branch information
arnaud-lb committed Jul 5, 2024
1 parent 5a726fd commit c6dc654
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions ext/opcache/shared_alloc_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include "zend_shared_alloc.h"
#include "jit/zend_jit.h"

#ifdef USE_MMAP

Expand All @@ -45,7 +46,7 @@
# define MAP_HUGETLB MAP_ALIGNED_SUPER
#endif

#if (defined(__linux__) || defined(__FreeBSD__)) && (defined(__x86_64__) || defined (__aarch64__)) && !defined(__SANITIZE_ADDRESS__)
#if defined(HAVE_JIT) && (defined(__linux__) || defined(__FreeBSD__)) && (defined(__x86_64__) || defined (__aarch64__)) && !defined(__SANITIZE_ADDRESS__)
static void *find_prefered_mmap_base(size_t requested_size)
{
size_t huge_page_size = 2 * 1024 * 1024;
Expand Down Expand Up @@ -188,8 +189,17 @@ static int create_segments(size_t requested_size, zend_shared_segment ***shared_
#ifdef PROT_MAX
flags |= PROT_MAX(PROT_READ | PROT_WRITE | PROT_EXEC);
#endif
#if (defined(__linux__) || defined(__FreeBSD__)) && (defined(__x86_64__) || defined (__aarch64__)) && !defined(__SANITIZE_ADDRESS__)
void *hint = find_prefered_mmap_base(requested_size);
#if defined(HAVE_JIT) && (defined(__linux__) || defined(__FreeBSD__)) && (defined(__x86_64__) || defined (__aarch64__)) && !defined(__SANITIZE_ADDRESS__)
void *hint;
if (JIT_G(enabled) && JIT_G(buffer_size)
&& zend_jit_check_support() == SUCCESS) {
hint = find_prefered_mmap_base(requested_size);
} else {
/* Do not use a hint if JIT is not enabled, as this profits only JIT and
* this may be unsafe when the hole after the heap is the only candidate
* (e.g. in non-PIE builds) (GH-13775). */
hint = MAP_FAILED;
}
if (hint != MAP_FAILED) {
# ifdef MAP_HUGETLB
size_t huge_page_size = 2 * 1024 * 1024;
Expand Down

0 comments on commit c6dc654

Please sign in to comment.