Handle empty allocation list in CodeRange properly.
authorulan@chromium.org <ulan@chromium.org>
Tue, 26 Aug 2014 13:14:46 +0000 (13:14 +0000)
committerulan@chromium.org <ulan@chromium.org>
Tue, 26 Aug 2014 13:14:46 +0000 (13:14 +0000)
BUG= 407566,v8:3540
LOG=Y
TEST=cctest/test-spaces/Regress3540
R=mstarzinger@chromium.org

Review URL: https://codereview.chromium.org/496433004

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23404 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/heap/spaces.cc
test/cctest/test-spaces.cc

index ff55c89e5d2f34036d79a8d57e704bf9c2dd9302..f91181e03661a927c53b0e7f9cf99f431fa9461b 100644 (file)
@@ -186,8 +186,10 @@ Address CodeRange::AllocateRawMemory(const size_t requested_size,
                                      const size_t commit_size,
                                      size_t* allocated) {
   DCHECK(commit_size <= requested_size);
-  DCHECK(current_allocation_block_index_ < allocation_list_.length());
-  if (requested_size > allocation_list_[current_allocation_block_index_].size) {
+  DCHECK(allocation_list_.length() == 0 ||
+         current_allocation_block_index_ < allocation_list_.length());
+  if (allocation_list_.length() == 0 ||
+      requested_size > allocation_list_[current_allocation_block_index_].size) {
     // Find an allocation block large enough.
     if (!GetNextAllocationBlock(requested_size)) return NULL;
   }
@@ -211,7 +213,7 @@ Address CodeRange::AllocateRawMemory(const size_t requested_size,
   allocation_list_[current_allocation_block_index_].size -= *allocated;
   if (*allocated == current.size) {
     // This block is used up, get the next one.
-    if (!GetNextAllocationBlock(0)) return NULL;
+    GetNextAllocationBlock(0);
   }
   return current.start;
 }
index 00620944002c0c533fca61c844ff81d66e932891..3c59610065d6b568f4284b1d28e10f47b1370393 100644 (file)
@@ -203,6 +203,28 @@ static void VerifyMemoryChunk(Isolate* isolate,
 }
 
 
+TEST(Regress3540) {
+  Isolate* isolate = CcTest::i_isolate();
+  isolate->InitializeLoggingAndCounters();
+  Heap* heap = isolate->heap();
+  CHECK(heap->ConfigureHeapDefault());
+  MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
+  CHECK(
+      memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize()));
+  TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator);
+  CodeRange* code_range = new CodeRange(isolate);
+  const size_t code_range_size = 4 * MB;
+  if (!code_range->SetUp(code_range_size)) return;
+  size_t allocated_size;
+  Address result;
+  for (int i = 0; i < 5; i++) {
+    result = code_range->AllocateRawMemory(
+        code_range_size - MB, code_range_size - MB, &allocated_size);
+    CHECK((result != NULL) == (i == 0));
+  }
+}
+
+
 static unsigned int Pseudorandom() {
   static uint32_t lo = 2345;
   lo = 18273 * (lo & 0xFFFFF) + (lo >> 16);