7447fd53b5
This reverts commitf2cd10db14
. Reason for revert: Blocks the roll https://chromium-review.googlesource.com/c/chromium/src/+/674623 Original change's description: > Reland "[Memory] Move VirtualMemory out of base:: platform." > > This is a reland of4dd293d922
> Original change's description: > > [Memory] Move VirtualMemory out of base:: platform. > > > > - Moves base::VirtualMemory to v8::internal::VirtualMemory. > > - Makes VirtualMemory platform-independent by moving internals to new > > OS:: static methods, for each platform. > > > > This will make it easier to delegate memory management in VirtualMemory > > to V8::Platform, so that embedders like Blink can override it. We can't > > depend on V8::Platform in base/platform. > > > > Bug: chromium:756050 > > Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng > > Change-Id: Iadfe230b6850bd917727a373f277afded9883adf > > Reviewed-on: https://chromium-review.googlesource.com/653214 > > Commit-Queue: Bill Budge <bbudge@chromium.org> > > Reviewed-by: Ulan Degenbaev <ulan@chromium.org> > > Cr-Commit-Position: refs/heads/master@{#48048} > > Bug: chromium:756050 > Change-Id: Ib492c7c69f1833be127a571808301e96b84b8aa2 > Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng > Reviewed-on: https://chromium-review.googlesource.com/671125 > Commit-Queue: Bill Budge <bbudge@chromium.org> > Reviewed-by: Michael Lippautz <mlippautz@chromium.org> > Cr-Commit-Position: refs/heads/master@{#48084} TBR=bbudge@chromium.org,ulan@chromium.org,hpayer@chromium.org,mlippautz@chromium.org,scottmg@chromium.org Change-Id: I04176d77ca9ad8315b0e1bb2b21f40f2c8ab9536 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: chromium:756050 Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng Reviewed-on: https://chromium-review.googlesource.com/674843 Reviewed-by: Michael Hablich <hablich@chromium.org> Commit-Queue: Michael Hablich <hablich@chromium.org> Cr-Commit-Position: refs/heads/master@{#48088}
155 lines
5.5 KiB
C++
155 lines
5.5 KiB
C++
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "src/v8.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
using v8::internal::AccountingAllocator;
|
|
|
|
using v8::IdleTask;
|
|
using v8::Isolate;
|
|
using v8::Task;
|
|
|
|
#include "src/allocation.h"
|
|
#include "src/zone/accounting-allocator.h"
|
|
|
|
// ASAN isn't configured to return NULL, so skip all of these tests.
|
|
#if !defined(V8_USE_ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \
|
|
!defined(THREAD_SANITIZER)
|
|
|
|
namespace {
|
|
|
|
// Implementation of v8::Platform that can register OOM callbacks.
|
|
class AllocationPlatform : public TestPlatform {
|
|
public:
|
|
AllocationPlatform() {
|
|
current_platform = this;
|
|
// Now that it's completely constructed, make this the current platform.
|
|
i::V8::SetPlatformForTesting(this);
|
|
}
|
|
virtual ~AllocationPlatform() = default;
|
|
|
|
void OnCriticalMemoryPressure() override { oom_callback_called = true; }
|
|
|
|
static AllocationPlatform* current_platform;
|
|
bool oom_callback_called = false;
|
|
};
|
|
|
|
AllocationPlatform* AllocationPlatform::current_platform = nullptr;
|
|
|
|
bool DidCallOnCriticalMemoryPressure() {
|
|
return AllocationPlatform::current_platform &&
|
|
AllocationPlatform::current_platform->oom_callback_called;
|
|
}
|
|
|
|
// No OS should be able to malloc/new this number of bytes. Generate enough
|
|
// random values in the address space to get a very large fraction of it. Using
|
|
// even larger values is that overflow from rounding or padding can cause the
|
|
// allocations to succeed somehow.
|
|
size_t GetHugeMemoryAmount() {
|
|
static size_t huge_memory = 0;
|
|
if (!huge_memory) {
|
|
for (int i = 0; i < 100; i++) {
|
|
huge_memory |= bit_cast<size_t>(v8::base::OS::GetRandomMmapAddr());
|
|
}
|
|
// Make it larger than the available address space.
|
|
huge_memory *= 2;
|
|
CHECK_NE(0, huge_memory);
|
|
}
|
|
return huge_memory;
|
|
}
|
|
|
|
void OnMallocedOperatorNewOOM(const char* location, const char* message) {
|
|
// exit(0) if the OOM callback was called and location matches expectation.
|
|
if (DidCallOnCriticalMemoryPressure())
|
|
exit(strcmp(location, "Malloced operator new"));
|
|
exit(1);
|
|
}
|
|
|
|
void OnNewArrayOOM(const char* location, const char* message) {
|
|
// exit(0) if the OOM callback was called and location matches expectation.
|
|
if (DidCallOnCriticalMemoryPressure()) exit(strcmp(location, "NewArray"));
|
|
exit(1);
|
|
}
|
|
|
|
void OnAlignedAllocOOM(const char* location, const char* message) {
|
|
// exit(0) if the OOM callback was called and location matches expectation.
|
|
if (DidCallOnCriticalMemoryPressure()) exit(strcmp(location, "AlignedAlloc"));
|
|
exit(1);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(AccountingAllocatorOOM) {
|
|
AllocationPlatform platform;
|
|
v8::internal::AccountingAllocator allocator;
|
|
CHECK(!platform.oom_callback_called);
|
|
v8::internal::Segment* result = allocator.GetSegment(GetHugeMemoryAmount());
|
|
// On a few systems, allocation somehow succeeds.
|
|
CHECK_EQ(result == nullptr, platform.oom_callback_called);
|
|
}
|
|
|
|
TEST(MallocedOperatorNewOOM) {
|
|
AllocationPlatform platform;
|
|
CHECK(!platform.oom_callback_called);
|
|
CcTest::isolate()->SetFatalErrorHandler(OnMallocedOperatorNewOOM);
|
|
// On failure, this won't return, since a Malloced::New failure is fatal.
|
|
// In that case, behavior is checked in OnMallocedOperatorNewOOM before exit.
|
|
void* result = v8::internal::Malloced::New(GetHugeMemoryAmount());
|
|
// On a few systems, allocation somehow succeeds.
|
|
CHECK_EQ(result == nullptr, platform.oom_callback_called);
|
|
}
|
|
|
|
TEST(NewArrayOOM) {
|
|
AllocationPlatform platform;
|
|
CHECK(!platform.oom_callback_called);
|
|
CcTest::isolate()->SetFatalErrorHandler(OnNewArrayOOM);
|
|
// On failure, this won't return, since a NewArray failure is fatal.
|
|
// In that case, behavior is checked in OnNewArrayOOM before exit.
|
|
int8_t* result = v8::internal::NewArray<int8_t>(GetHugeMemoryAmount());
|
|
// On a few systems, allocation somehow succeeds.
|
|
CHECK_EQ(result == nullptr, platform.oom_callback_called);
|
|
}
|
|
|
|
TEST(AlignedAllocOOM) {
|
|
AllocationPlatform platform;
|
|
CHECK(!platform.oom_callback_called);
|
|
CcTest::isolate()->SetFatalErrorHandler(OnAlignedAllocOOM);
|
|
// On failure, this won't return, since an AlignedAlloc failure is fatal.
|
|
// In that case, behavior is checked in OnAlignedAllocOOM before exit.
|
|
void* result = v8::internal::AlignedAlloc(GetHugeMemoryAmount(),
|
|
v8::base::OS::AllocateAlignment());
|
|
// On a few systems, allocation somehow succeeds.
|
|
CHECK_EQ(result == nullptr, platform.oom_callback_called);
|
|
}
|
|
|
|
TEST(AllocVirtualMemoryOOM) {
|
|
AllocationPlatform platform;
|
|
CHECK(!platform.oom_callback_called);
|
|
v8::base::VirtualMemory result;
|
|
bool success =
|
|
v8::internal::AllocVirtualMemory(GetHugeMemoryAmount(), nullptr, &result);
|
|
// On a few systems, allocation somehow succeeds.
|
|
CHECK_IMPLIES(success, result.IsReserved());
|
|
CHECK_IMPLIES(!success, !result.IsReserved() && platform.oom_callback_called);
|
|
}
|
|
|
|
TEST(AlignedAllocVirtualMemoryOOM) {
|
|
AllocationPlatform platform;
|
|
CHECK(!platform.oom_callback_called);
|
|
v8::base::VirtualMemory result;
|
|
bool success = v8::internal::AlignedAllocVirtualMemory(
|
|
GetHugeMemoryAmount(), v8::base::OS::AllocateAlignment(), nullptr,
|
|
&result);
|
|
// On a few systems, allocation somehow succeeds.
|
|
CHECK_IMPLIES(success, result.IsReserved());
|
|
CHECK_IMPLIES(!success, !result.IsReserved() && platform.oom_callback_called);
|
|
}
|
|
|
|
#endif // !defined(V8_USE_ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) &&
|
|
// !defined(THREAD_SANITIZER)
|