2017-08-01 01:41:13 +00:00
|
|
|
// 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>
|
|
|
|
|
2017-12-15 17:59:57 +00:00
|
|
|
#if V8_OS_POSIX
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
2021-04-29 16:54:13 +00:00
|
|
|
#include <unistd.h>
|
2017-12-15 17:59:57 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2017-08-01 01:41:13 +00:00
|
|
|
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
|
|
|
|
using v8::internal::AccountingAllocator;
|
|
|
|
|
|
|
|
using v8::IdleTask;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Task;
|
|
|
|
|
2019-05-23 13:27:57 +00:00
|
|
|
#include "src/utils/allocation.h"
|
2017-08-01 01:41:13 +00:00
|
|
|
#include "src/zone/accounting-allocator.h"
|
|
|
|
|
2017-10-13 16:33:03 +00:00
|
|
|
// ASAN isn't configured to return nullptr, so skip all of these tests.
|
2017-08-14 09:20:27 +00:00
|
|
|
#if !defined(V8_USE_ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \
|
|
|
|
!defined(THREAD_SANITIZER)
|
|
|
|
|
2017-08-01 01:41:13 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-08-01 14:16:30 +00:00
|
|
|
// Implementation of v8::Platform that can register OOM callbacks.
|
|
|
|
class AllocationPlatform : public TestPlatform {
|
2017-08-01 01:41:13 +00:00
|
|
|
public:
|
2017-08-01 14:16:30 +00:00
|
|
|
AllocationPlatform() {
|
|
|
|
current_platform = this;
|
|
|
|
// Now that it's completely constructed, make this the current platform.
|
|
|
|
i::V8::SetPlatformForTesting(this);
|
2017-08-01 01:41:13 +00:00
|
|
|
}
|
2018-09-14 15:34:02 +00:00
|
|
|
~AllocationPlatform() override = default;
|
2017-08-01 01:41:13 +00:00
|
|
|
|
2017-08-01 14:16:30 +00:00
|
|
|
void OnCriticalMemoryPressure() override { oom_callback_called = true; }
|
2017-08-01 01:41:13 +00:00
|
|
|
|
2017-12-25 14:43:43 +00:00
|
|
|
bool OnCriticalMemoryPressure(size_t length) override {
|
|
|
|
oom_callback_called = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-01 14:16:30 +00:00
|
|
|
static AllocationPlatform* current_platform;
|
2017-08-01 01:41:13 +00:00
|
|
|
bool oom_callback_called = false;
|
|
|
|
};
|
|
|
|
|
2017-08-01 14:16:30 +00:00
|
|
|
AllocationPlatform* AllocationPlatform::current_platform = nullptr;
|
2017-08-01 01:41:13 +00:00
|
|
|
|
|
|
|
bool DidCallOnCriticalMemoryPressure() {
|
2017-08-01 14:16:30 +00:00
|
|
|
return AllocationPlatform::current_platform &&
|
|
|
|
AllocationPlatform::current_platform->oom_callback_called;
|
2017-08-01 01:41:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 09:20:27 +00:00
|
|
|
// 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++) {
|
2017-12-15 17:59:57 +00:00
|
|
|
huge_memory |= bit_cast<size_t>(v8::internal::GetRandomMmapAddr());
|
2017-08-14 09:20:27 +00:00
|
|
|
}
|
|
|
|
// Make it larger than the available address space.
|
|
|
|
huge_memory *= 2;
|
|
|
|
CHECK_NE(0, huge_memory);
|
|
|
|
}
|
|
|
|
return huge_memory;
|
|
|
|
}
|
|
|
|
|
2017-08-01 01:41:13 +00:00
|
|
|
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) {
|
2017-08-01 14:16:30 +00:00
|
|
|
AllocationPlatform platform;
|
2017-08-01 01:41:13 +00:00
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
CHECK(!platform.oom_callback_called);
|
2020-07-23 09:29:59 +00:00
|
|
|
const bool support_compression = false;
|
2019-03-01 08:26:26 +00:00
|
|
|
v8::internal::Segment* result =
|
2020-07-23 09:29:59 +00:00
|
|
|
allocator.AllocateSegment(GetHugeMemoryAmount(), support_compression);
|
2017-08-14 09:20:27 +00:00
|
|
|
// On a few systems, allocation somehow succeeds.
|
|
|
|
CHECK_EQ(result == nullptr, platform.oom_callback_called);
|
2017-08-01 01:41:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 13:05:06 +00:00
|
|
|
TEST(AccountingAllocatorCurrentAndMax) {
|
|
|
|
AllocationPlatform platform;
|
|
|
|
v8::internal::AccountingAllocator allocator;
|
|
|
|
static constexpr size_t kAllocationSizes[] = {51, 231, 27};
|
|
|
|
std::vector<v8::internal::Segment*> segments;
|
2020-07-23 09:29:59 +00:00
|
|
|
const bool support_compression = false;
|
2019-03-28 13:05:06 +00:00
|
|
|
CHECK_EQ(0, allocator.GetCurrentMemoryUsage());
|
|
|
|
CHECK_EQ(0, allocator.GetMaxMemoryUsage());
|
|
|
|
size_t expected_current = 0;
|
|
|
|
size_t expected_max = 0;
|
|
|
|
for (size_t size : kAllocationSizes) {
|
2020-07-23 09:29:59 +00:00
|
|
|
segments.push_back(allocator.AllocateSegment(size, support_compression));
|
2019-03-28 13:05:06 +00:00
|
|
|
CHECK_NOT_NULL(segments.back());
|
|
|
|
CHECK_EQ(size, segments.back()->total_size());
|
|
|
|
expected_current += size;
|
|
|
|
if (expected_current > expected_max) expected_max = expected_current;
|
|
|
|
CHECK_EQ(expected_current, allocator.GetCurrentMemoryUsage());
|
|
|
|
CHECK_EQ(expected_max, allocator.GetMaxMemoryUsage());
|
|
|
|
}
|
|
|
|
for (auto* segment : segments) {
|
|
|
|
expected_current -= segment->total_size();
|
2020-07-23 09:29:59 +00:00
|
|
|
allocator.ReturnSegment(segment, support_compression);
|
2019-03-28 13:05:06 +00:00
|
|
|
CHECK_EQ(expected_current, allocator.GetCurrentMemoryUsage());
|
|
|
|
}
|
|
|
|
CHECK_EQ(expected_max, allocator.GetMaxMemoryUsage());
|
|
|
|
CHECK_EQ(0, allocator.GetCurrentMemoryUsage());
|
|
|
|
CHECK(!platform.oom_callback_called);
|
|
|
|
}
|
|
|
|
|
2017-08-01 01:41:13 +00:00
|
|
|
TEST(MallocedOperatorNewOOM) {
|
2017-08-01 14:16:30 +00:00
|
|
|
AllocationPlatform platform;
|
2017-08-01 01:41:13 +00:00
|
|
|
CHECK(!platform.oom_callback_called);
|
|
|
|
CcTest::isolate()->SetFatalErrorHandler(OnMallocedOperatorNewOOM);
|
2017-08-14 09:20:27 +00:00
|
|
|
// On failure, this won't return, since a Malloced::New failure is fatal.
|
|
|
|
// In that case, behavior is checked in OnMallocedOperatorNewOOM before exit.
|
2019-07-15 08:58:31 +00:00
|
|
|
void* result = v8::internal::Malloced::operator new(GetHugeMemoryAmount());
|
2017-08-14 09:20:27 +00:00
|
|
|
// On a few systems, allocation somehow succeeds.
|
|
|
|
CHECK_EQ(result == nullptr, platform.oom_callback_called);
|
2017-08-01 01:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NewArrayOOM) {
|
2017-08-01 14:16:30 +00:00
|
|
|
AllocationPlatform platform;
|
2017-08-01 01:41:13 +00:00
|
|
|
CHECK(!platform.oom_callback_called);
|
|
|
|
CcTest::isolate()->SetFatalErrorHandler(OnNewArrayOOM);
|
|
|
|
// On failure, this won't return, since a NewArray failure is fatal.
|
2017-08-14 09:20:27 +00:00
|
|
|
// 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);
|
2017-08-01 01:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AlignedAllocOOM) {
|
2017-08-01 14:16:30 +00:00
|
|
|
AllocationPlatform platform;
|
2017-08-01 01:41:13 +00:00
|
|
|
CHECK(!platform.oom_callback_called);
|
|
|
|
CcTest::isolate()->SetFatalErrorHandler(OnAlignedAllocOOM);
|
|
|
|
// On failure, this won't return, since an AlignedAlloc failure is fatal.
|
2017-08-14 09:20:27 +00:00
|
|
|
// In that case, behavior is checked in OnAlignedAllocOOM before exit.
|
|
|
|
void* result = v8::internal::AlignedAlloc(GetHugeMemoryAmount(),
|
2017-12-15 17:59:57 +00:00
|
|
|
v8::internal::AllocatePageSize());
|
2017-08-14 09:20:27 +00:00
|
|
|
// On a few systems, allocation somehow succeeds.
|
|
|
|
CHECK_EQ(result == nullptr, platform.oom_callback_called);
|
2017-08-01 01:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AllocVirtualMemoryOOM) {
|
2017-08-01 14:16:30 +00:00
|
|
|
AllocationPlatform platform;
|
2017-08-01 01:41:13 +00:00
|
|
|
CHECK(!platform.oom_callback_called);
|
2018-09-17 08:49:40 +00:00
|
|
|
v8::internal::VirtualMemory result(v8::internal::GetPlatformPageAllocator(),
|
|
|
|
GetHugeMemoryAmount(), nullptr);
|
2017-08-14 09:20:27 +00:00
|
|
|
// On a few systems, allocation somehow succeeds.
|
2018-09-17 08:49:40 +00:00
|
|
|
CHECK_IMPLIES(!result.IsReserved(), platform.oom_callback_called);
|
2017-08-01 01:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AlignedAllocVirtualMemoryOOM) {
|
2017-08-01 14:16:30 +00:00
|
|
|
AllocationPlatform platform;
|
2017-08-01 01:41:13 +00:00
|
|
|
CHECK(!platform.oom_callback_called);
|
2018-09-17 08:49:40 +00:00
|
|
|
v8::internal::VirtualMemory result(v8::internal::GetPlatformPageAllocator(),
|
|
|
|
GetHugeMemoryAmount(), nullptr,
|
|
|
|
v8::internal::AllocatePageSize());
|
2017-08-14 09:20:27 +00:00
|
|
|
// On a few systems, allocation somehow succeeds.
|
2018-09-17 08:49:40 +00:00
|
|
|
CHECK_IMPLIES(!result.IsReserved(), platform.oom_callback_called);
|
2017-08-01 01:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !defined(V8_USE_ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) &&
|
|
|
|
// !defined(THREAD_SANITIZER)
|