From 709ba7aa244d6a49e833486eb803c101d9631feb Mon Sep 17 00:00:00 2001 From: Ryan Macnak Date: Fri, 28 Apr 2023 18:29:58 +0000 Subject: [PATCH] [vm] Use PR_SET_VMA_ANON_NAME on Linux too. This was upstreamed to Linux in 5.17. TEST=ci Change-Id: Idcc0c6a80a8f8e148bf13ed4a1d9d54122379ff9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279206 Commit-Queue: Ryan Macnak Reviewed-by: Siva Annamalai --- runtime/bin/virtual_memory_posix.cc | 17 ++++++++++++++ runtime/vm/virtual_memory_posix.cc | 36 ++++++++--------------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/runtime/bin/virtual_memory_posix.cc b/runtime/bin/virtual_memory_posix.cc index 36185648ccd..06e0c1480bb 100644 --- a/runtime/bin/virtual_memory_posix.cc +++ b/runtime/bin/virtual_memory_posix.cc @@ -12,6 +12,10 @@ #include #include +#if defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_LINUX) +#include +#endif + #include "platform/assert.h" #include "platform/utils.h" @@ -56,6 +60,19 @@ VirtualMemory* VirtualMemory::Allocate(intptr_t size, if (address == MAP_FAILED) { return nullptr; } + +#if defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_LINUX) + // PR_SET_VMA was only added to mainline Linux in 5.17, and some versions of + // the Android NDK have incorrect headers, so we manually define it if absent. +#if !defined(PR_SET_VMA) +#define PR_SET_VMA 0x53564d41 +#endif +#if !defined(PR_SET_VMA_ANON_NAME) +#define PR_SET_VMA_ANON_NAME 0 +#endif + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, address, size, name); +#endif + return new VirtualMemory(address, size); } diff --git a/runtime/vm/virtual_memory_posix.cc b/runtime/vm/virtual_memory_posix.cc index d66e87090c4..1382af75c5c 100644 --- a/runtime/vm/virtual_memory_posix.cc +++ b/runtime/vm/virtual_memory_posix.cc @@ -15,7 +15,7 @@ #include #include -#if defined(DART_HOST_OS_ANDROID) +#if defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_LINUX) #include #endif @@ -402,30 +402,6 @@ VirtualMemory* VirtualMemory::AllocateAligned(intptr_t size, PROT_READ | PROT_WRITE | ((is_executable && !FLAG_write_protect_code) ? PROT_EXEC : 0); -#if defined(DUAL_MAPPING_SUPPORTED) - // Try to use memfd for single-mapped regions too, so they will have an - // associated name for memory attribution. Skip if FLAG_dual_map_code is - // false, which happens if we detected memfd wasn't working in Init above. - if (FLAG_dual_map_code) { - int fd = memfd_create(name, MFD_CLOEXEC); - if (fd == -1) { - return nullptr; - } - if (ftruncate(fd, size) == -1) { - close(fd); - return nullptr; - } - void* region_ptr = - MapAligned(nullptr, fd, prot, size, alignment, allocated_size); - close(fd); - if (region_ptr == nullptr) { - return nullptr; - } - MemoryRegion region(region_ptr, size); - return new VirtualMemory(region, region); - } -#endif - int map_flags = MAP_PRIVATE | MAP_ANONYMOUS; #if (defined(DART_HOST_OS_MACOS) && !defined(DART_HOST_OS_IOS)) if (is_executable && IsAtLeastOS10_14()) { @@ -448,7 +424,15 @@ VirtualMemory* VirtualMemory::AllocateAligned(intptr_t size, return nullptr; } -#if defined(DART_HOST_OS_ANDROID) +#if defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_LINUX) + // PR_SET_VMA was only added to mainline Linux in 5.17, and some versions of + // the Android NDK have incorrect headers, so we manually define it if absent. +#if !defined(PR_SET_VMA) +#define PR_SET_VMA 0x53564d41 +#endif +#if !defined(PR_SET_VMA_ANON_NAME) +#define PR_SET_VMA_ANON_NAME 0 +#endif prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, address, size, name); #endif