Commit 52cc5fe0 authored by Bill Budge's avatar Bill Budge Committed by Commit Bot

Reland [Memory] Speculative fix for sanitizer flakiness.

- Uses a mutex to prevent races on getting random mmap addresses, on
  POSIX and Windows.

Original change's description:
> [Memory] Speculative fix for sanitizer flakiness.
>
> - When allocating virtual memory, make sure addresses don't interfere
>   with hard-coded sanitizer regions.
>
> Bug: v8:7146
> Change-Id: I5bcb664b32bf53c8581772fe329190da6033701f
> Reviewed-on: https://chromium-review.googlesource.com/833171
> Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
> Reviewed-by: Hannes Payer <hpayer@chromium.org>
> Commit-Queue: Bill Budge <bbudge@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#50208}

Bug: v8:7146
Change-Id: I5a82f2a1f6136498fb2aa7a37e0206c506545073
Reviewed-on: https://chromium-review.googlesource.com/834453Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Bill Budge <bbudge@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50216}
parent 42ac7fe0
...@@ -89,6 +89,7 @@ const char* g_gc_fake_mmap = nullptr; ...@@ -89,6 +89,7 @@ const char* g_gc_fake_mmap = nullptr;
static LazyInstance<RandomNumberGenerator>::type static LazyInstance<RandomNumberGenerator>::type
platform_random_number_generator = LAZY_INSTANCE_INITIALIZER; platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
static LazyMutex rng_mutex = LAZY_MUTEX_INITIALIZER;
#if !V8_OS_FUCHSIA #if !V8_OS_FUCHSIA
#if V8_OS_MACOSX #if V8_OS_MACOSX
...@@ -206,14 +207,21 @@ size_t OS::CommitPageSize() { ...@@ -206,14 +207,21 @@ size_t OS::CommitPageSize() {
// static // static
void* OS::GetRandomMmapAddr() { void* OS::GetRandomMmapAddr() {
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
defined(THREAD_SANITIZER)
// Dynamic tools do not support custom mmap addresses.
return nullptr;
#else
uintptr_t raw_addr; uintptr_t raw_addr;
{
LockGuard<Mutex> guard(rng_mutex.Pointer());
platform_random_number_generator.Pointer()->NextBytes(&raw_addr, platform_random_number_generator.Pointer()->NextBytes(&raw_addr,
sizeof(raw_addr)); sizeof(raw_addr));
}
#if defined(V8_USE_ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER)
// If random hint addresses interfere with address ranges hard coded in
// sanitizers, bad things happen. This address range is copied from TSAN
// source but works with all tools.
// See crbug.com/539863.
raw_addr &= 0x007fffff0000ULL;
raw_addr += 0x7e8000000000ULL;
#else
#if V8_TARGET_ARCH_X64 #if V8_TARGET_ARCH_X64
// Currently available CPUs have 48 bits of virtual addressing. Truncate // Currently available CPUs have 48 bits of virtual addressing. Truncate
// the hint address to 46 bits to give the kernel a fighting chance of // the hint address to 46 bits to give the kernel a fighting chance of
...@@ -267,8 +275,8 @@ void* OS::GetRandomMmapAddr() { ...@@ -267,8 +275,8 @@ void* OS::GetRandomMmapAddr() {
raw_addr += 0x20000000; raw_addr += 0x20000000;
#endif #endif
#endif #endif
return reinterpret_cast<void*>(raw_addr);
#endif #endif
return reinterpret_cast<void*>(raw_addr);
} }
// TODO(bbudge) Move Cygwin and Fuschia stuff into platform-specific files. // TODO(bbudge) Move Cygwin and Fuschia stuff into platform-specific files.
......
...@@ -699,6 +699,7 @@ size_t OS::CommitPageSize() { ...@@ -699,6 +699,7 @@ size_t OS::CommitPageSize() {
static LazyInstance<RandomNumberGenerator>::type static LazyInstance<RandomNumberGenerator>::type
platform_random_number_generator = LAZY_INSTANCE_INITIALIZER; platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
static LazyMutex rng_mutex = LAZY_MUTEX_INITIALIZER;
void OS::Initialize(int64_t random_seed, bool hard_abort, void OS::Initialize(int64_t random_seed, bool hard_abort,
const char* const gc_fake_mmap) { const char* const gc_fake_mmap) {
...@@ -722,8 +723,11 @@ void* OS::GetRandomMmapAddr() { ...@@ -722,8 +723,11 @@ void* OS::GetRandomMmapAddr() {
static const uintptr_t kAllocationRandomAddressMax = 0x3FFF0000; static const uintptr_t kAllocationRandomAddressMax = 0x3FFF0000;
#endif #endif
uintptr_t address; uintptr_t address;
{
LockGuard<Mutex> guard(rng_mutex.Pointer());
platform_random_number_generator.Pointer()->NextBytes(&address, platform_random_number_generator.Pointer()->NextBytes(&address,
sizeof(address)); sizeof(address));
}
address <<= kPageSizeBits; address <<= kPageSizeBits;
address += kAllocationRandomAddressMin; address += kAllocationRandomAddressMin;
address &= kAllocationRandomAddressMax; address &= kAllocationRandomAddressMax;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment