Commit 65ae2a0f authored by Dan Elphick's avatar Dan Elphick Committed by Commit Bot

[heap] Check BasicMemoryChunk before initializing

Add nullptr guard for the return value of AllocateBasicChunk.

Bug: chromium:1097502
Change-Id: Ia4642151a119ccabe58d7084077808aac93e5d1c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2257221Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Dan Elphick <delphick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68454}
parent 8086ca30
......@@ -501,6 +501,9 @@ MemoryChunk* MemoryAllocator::AllocateChunk(size_t reserve_area_size,
BaseSpace* owner) {
BasicMemoryChunk* basic_chunk = AllocateBasicChunk(
reserve_area_size, commit_area_size, executable, owner);
if (basic_chunk == nullptr) return nullptr;
MemoryChunk* chunk =
MemoryChunk::Initialize(basic_chunk, isolate_->heap(), executable);
......
......@@ -417,6 +417,7 @@ class MemoryAllocator {
base::Mutex executable_memory_mutex_;
friend class heap::TestCodePageAllocatorScope;
friend class heap::TestMemoryAllocatorScope;
DISALLOW_IMPLICIT_CONSTRUCTORS(MemoryAllocator);
};
......
......@@ -27,6 +27,7 @@
#include <stdlib.h>
#include "include/v8-platform.h"
#include "src/base/bounded-page-allocator.h"
#include "src/base/platform/platform.h"
#include "src/heap/factory.h"
......@@ -50,11 +51,15 @@ namespace heap {
class TestMemoryAllocatorScope {
public:
TestMemoryAllocatorScope(Isolate* isolate, size_t max_capacity,
size_t code_range_size)
size_t code_range_size,
PageAllocator* page_allocator = nullptr)
: isolate_(isolate),
old_allocator_(std::move(isolate->heap()->memory_allocator_)) {
isolate->heap()->memory_allocator_.reset(
new MemoryAllocator(isolate, max_capacity, code_range_size));
if (page_allocator != nullptr) {
isolate->heap()->memory_allocator_->data_page_allocator_ = page_allocator;
}
}
MemoryAllocator* allocator() { return isolate_->heap()->memory_allocator(); }
......@@ -745,6 +750,47 @@ TEST(ShrinkPageToHighWaterMarkTwoWordFiller) {
CHECK_EQ(0u, shrunk);
}
namespace {
// PageAllocator that always fails.
class FailingPageAllocator : public v8::PageAllocator {
public:
size_t AllocatePageSize() override { return 1024; }
size_t CommitPageSize() override { return 1024; }
void SetRandomMmapSeed(int64_t seed) override {}
void* GetRandomMmapAddr() override { return nullptr; }
void* AllocatePages(void* address, size_t length, size_t alignment,
Permission permissions) override {
return nullptr;
}
bool FreePages(void* address, size_t length) override { return false; }
bool ReleasePages(void* address, size_t length, size_t new_length) override {
return false;
}
bool SetPermissions(void* address, size_t length,
Permission permissions) override {
return false;
}
};
} // namespace
TEST(NoMemoryForNewPage) {
Isolate* isolate = CcTest::i_isolate();
Heap* heap = isolate->heap();
// Memory allocator that will fail to allocate any pages.
FailingPageAllocator failing_allocator;
TestMemoryAllocatorScope test_allocator_scope(isolate, 0, 0,
&failing_allocator);
MemoryAllocator* memory_allocator = test_allocator_scope.allocator();
OldSpace faked_space(heap);
Page* page = memory_allocator->AllocatePage(
faked_space.AreaSize(), static_cast<PagedSpace*>(&faked_space),
NOT_EXECUTABLE);
CHECK_NULL(page);
}
} // namespace heap
} // namespace internal
} // namespace v8
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