Support fixed address mmap without replacement

We did not use MAP_FIXED here, because it may replace an existing
mapping. This commit adds support for MAP_FIXED_NOREPLACE (available
on newer Linux kernels) and MAP_FIXED|MAP_EXCL (available on FreeBSD),
which avoid this issue.
This commit is contained in:
David Carlier 2018-08-19 20:32:28 +01:00 committed by Nikita Popov
parent 5e5311e0ba
commit 0f68ff5c92

View File

@ -423,8 +423,13 @@ static void *zend_mm_mmap_fixed(void *addr, size_t size)
#ifdef _WIN32
return VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
#else
/* MAP_FIXED leads to discarding of the old mapping, so it can't be used. */
void *ptr = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON /*| MAP_POPULATE | MAP_HUGETLB*/, -1, 0);
int flags = MAP_PRIVATE | MAP_ANON;
#ifdef MAP_FIXED_NOREPLACE
flags |= MAP_FIXED_NOREPLACE;
#elif defined MAP_EXCL
flags |= MAP_FIXED | MAP_EXCL;
#endif
void *ptr = mmap(addr, size, PROT_READ | PROT_WRITE, flags /*| MAP_POPULATE | MAP_HUGETLB*/, -1, 0);
if (ptr == MAP_FAILED) {
#if ZEND_MM_ERROR