Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not create the opcache shm mapping too close to the heap #14793

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 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 @@ -76,8 +77,13 @@ static void *find_prefered_mmap_base(size_t requested_size)
}
if ((uintptr_t)execute_ex >= start) {
/* the current segment lays before PHP .text segment or PHP .text segment itself */
/*Search for candidates at the end of the free segment near the .text segment
to prevent candidates from being missed due to large hole*/
if (last_free_addr + requested_size <= start) {
last_candidate = last_free_addr;
last_candidate = ZEND_MM_ALIGNED_SIZE_EX(start - requested_size, huge_page_size);
if (last_candidate + requested_size > start) {
last_candidate -= huge_page_size;
}
}
if ((uintptr_t)execute_ex < end) {
/* the current segment is PHP .text segment itself */
Expand Down Expand Up @@ -128,7 +134,10 @@ static void *find_prefered_mmap_base(size_t requested_size)
if ((uintptr_t)execute_ex >= e_start) {
/* the current segment lays before PHP .text segment or PHP .text segment itself */
if (last_free_addr + requested_size <= e_start) {
last_candidate = last_free_addr;
last_candidate = ZEND_MM_ALIGNED_SIZE_EX(e_start - requested_size, huge_page_size);
if (last_candidate + requested_size > e_start) {
last_candidate -= huge_page_size;
}
}
if ((uintptr_t)execute_ex < e_end) {
/* the current segment is PHP .text segment itself */
Expand Down Expand Up @@ -180,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
Loading