1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "src/base/platform/platform.h"
31 #include "src/snapshot/snapshot.h"
33 #include "test/cctest/cctest.h"
36 using namespace v8::internal;
39 static void VerifyRegionMarking(Address page_start) {
40 #ifdef ENABLE_CARDMARKING_WRITE_BARRIER
41 Page* p = Page::FromAddress(page_start);
43 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
45 for (Address addr = p->ObjectAreaStart();
46 addr < p->ObjectAreaEnd();
47 addr += kPointerSize) {
48 CHECK(!Page::FromAddress(addr)->IsRegionDirty(addr));
51 for (Address addr = p->ObjectAreaStart();
52 addr < p->ObjectAreaEnd();
53 addr += kPointerSize) {
54 Page::FromAddress(addr)->MarkRegionDirty(addr);
57 for (Address addr = p->ObjectAreaStart();
58 addr < p->ObjectAreaEnd();
59 addr += kPointerSize) {
60 CHECK(Page::FromAddress(addr)->IsRegionDirty(addr));
67 // TODO(gc) you can no longer allocate pages like this. Details are hidden.
70 byte* mem = NewArray<byte>(2*Page::kPageSize);
73 Address start = reinterpret_cast<Address>(mem);
74 Address page_start = RoundUp(start, Page::kPageSize);
76 Page* p = Page::FromAddress(page_start);
77 // Initialized Page has heap pointer, normally set by memory_allocator.
78 p->heap_ = CcTest::heap();
79 CHECK(p->address() == page_start);
83 p->SetIsLargeObjectPage(false);
84 CHECK(!p->next_page()->is_valid());
86 CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset);
87 CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize);
89 CHECK(p->Offset(page_start + Page::kObjectStartOffset) ==
90 Page::kObjectStartOffset);
91 CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize);
93 CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart());
94 CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd());
96 // test region marking
97 VerifyRegionMarking(page_start);
107 // Temporarily sets a given allocator in an isolate.
108 class TestMemoryAllocatorScope {
110 TestMemoryAllocatorScope(Isolate* isolate, MemoryAllocator* allocator)
112 old_allocator_(isolate->memory_allocator_) {
113 isolate->memory_allocator_ = allocator;
116 ~TestMemoryAllocatorScope() {
117 isolate_->memory_allocator_ = old_allocator_;
122 MemoryAllocator* old_allocator_;
124 DISALLOW_COPY_AND_ASSIGN(TestMemoryAllocatorScope);
128 // Temporarily sets a given code range in an isolate.
129 class TestCodeRangeScope {
131 TestCodeRangeScope(Isolate* isolate, CodeRange* code_range)
133 old_code_range_(isolate->code_range_) {
134 isolate->code_range_ = code_range;
137 ~TestCodeRangeScope() {
138 isolate_->code_range_ = old_code_range_;
143 CodeRange* old_code_range_;
145 DISALLOW_COPY_AND_ASSIGN(TestCodeRangeScope);
148 } // namespace internal
152 static void VerifyMemoryChunk(Isolate* isolate,
154 CodeRange* code_range,
155 size_t reserve_area_size,
156 size_t commit_area_size,
157 size_t second_commit_area_size,
158 Executability executable) {
159 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
160 CHECK(memory_allocator->SetUp(heap->MaxReserved(),
161 heap->MaxExecutableSize()));
162 TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator);
163 TestCodeRangeScope test_code_range_scope(isolate, code_range);
165 size_t header_size = (executable == EXECUTABLE)
166 ? MemoryAllocator::CodePageGuardStartOffset()
167 : MemoryChunk::kObjectStartOffset;
168 size_t guard_size = (executable == EXECUTABLE)
169 ? MemoryAllocator::CodePageGuardSize()
172 MemoryChunk* memory_chunk = memory_allocator->AllocateChunk(reserve_area_size,
176 size_t alignment = code_range != NULL && code_range->valid() ?
177 MemoryChunk::kAlignment : v8::base::OS::CommitPageSize();
178 size_t reserved_size =
179 ((executable == EXECUTABLE))
180 ? RoundUp(header_size + guard_size + reserve_area_size + guard_size,
182 : RoundUp(header_size + reserve_area_size,
183 v8::base::OS::CommitPageSize());
184 CHECK(memory_chunk->size() == reserved_size);
185 CHECK(memory_chunk->area_start() < memory_chunk->address() +
186 memory_chunk->size());
187 CHECK(memory_chunk->area_end() <= memory_chunk->address() +
188 memory_chunk->size());
189 CHECK(static_cast<size_t>(memory_chunk->area_size()) == commit_area_size);
191 Address area_start = memory_chunk->area_start();
193 memory_chunk->CommitArea(second_commit_area_size);
194 CHECK(area_start == memory_chunk->area_start());
195 CHECK(memory_chunk->area_start() < memory_chunk->address() +
196 memory_chunk->size());
197 CHECK(memory_chunk->area_end() <= memory_chunk->address() +
198 memory_chunk->size());
199 CHECK(static_cast<size_t>(memory_chunk->area_size()) ==
200 second_commit_area_size);
202 memory_allocator->Free(memory_chunk);
203 memory_allocator->TearDown();
204 delete memory_allocator;
209 Isolate* isolate = CcTest::i_isolate();
210 Heap* heap = isolate->heap();
211 const int pageSize = Page::kPageSize;
212 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
214 memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize()));
215 TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator);
216 CodeRange* code_range = new CodeRange(isolate);
217 const size_t code_range_size = 4 * pageSize;
218 if (!code_range->SetUp(
220 RoundUp(v8::base::OS::CommitPageSize() * kReservedCodeRangePages,
221 MemoryChunk::kAlignment) +
222 v8::internal::MemoryAllocator::CodePageAreaSize())) {
227 address = code_range->AllocateRawMemory(
228 code_range_size - 2 * pageSize, code_range_size - 2 * pageSize, &size);
229 CHECK(address != NULL);
230 Address null_address;
232 null_address = code_range->AllocateRawMemory(
233 code_range_size - pageSize, code_range_size - pageSize, &null_size);
234 CHECK(null_address == NULL);
235 code_range->FreeRawMemory(address, size);
237 memory_allocator->TearDown();
238 delete memory_allocator;
242 static unsigned int Pseudorandom() {
243 static uint32_t lo = 2345;
244 lo = 18273 * (lo & 0xFFFFF) + (lo >> 16);
250 Isolate* isolate = CcTest::i_isolate();
251 Heap* heap = isolate->heap();
253 size_t reserve_area_size = 1 * MB;
254 size_t initial_commit_area_size, second_commit_area_size;
256 for (int i = 0; i < 100; i++) {
257 initial_commit_area_size = Pseudorandom();
258 second_commit_area_size = Pseudorandom();
261 CodeRange* code_range = new CodeRange(isolate);
262 const size_t code_range_size = 32 * MB;
263 if (!code_range->SetUp(code_range_size)) return;
265 VerifyMemoryChunk(isolate,
269 initial_commit_area_size,
270 second_commit_area_size,
273 VerifyMemoryChunk(isolate,
277 initial_commit_area_size,
278 second_commit_area_size,
282 // Without CodeRange.
284 VerifyMemoryChunk(isolate,
288 initial_commit_area_size,
289 second_commit_area_size,
292 VerifyMemoryChunk(isolate,
296 initial_commit_area_size,
297 second_commit_area_size,
303 TEST(MemoryAllocator) {
304 Isolate* isolate = CcTest::i_isolate();
305 Heap* heap = isolate->heap();
307 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
308 CHECK(memory_allocator->SetUp(heap->MaxReserved(),
309 heap->MaxExecutableSize()));
312 OldSpace faked_space(heap, heap->MaxReserved(), OLD_SPACE, NOT_EXECUTABLE);
313 Page* first_page = memory_allocator->AllocatePage(
314 faked_space.AreaSize(), &faked_space, NOT_EXECUTABLE);
316 first_page->InsertAfter(faked_space.anchor()->prev_page());
317 CHECK(Page::IsValid(first_page));
318 CHECK(first_page->next_page() == faked_space.anchor());
321 for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) {
322 CHECK(p->owner() == &faked_space);
325 // Again, we should get n or n - 1 pages.
326 Page* other = memory_allocator->AllocatePage(
327 faked_space.AreaSize(), &faked_space, NOT_EXECUTABLE);
328 CHECK(Page::IsValid(other));
330 other->InsertAfter(first_page);
332 for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) {
333 CHECK(p->owner() == &faked_space);
336 CHECK(total_pages == page_count);
338 Page* second_page = first_page->next_page();
339 CHECK(Page::IsValid(second_page));
340 memory_allocator->Free(first_page);
341 memory_allocator->Free(second_page);
342 memory_allocator->TearDown();
343 delete memory_allocator;
348 Isolate* isolate = CcTest::i_isolate();
349 Heap* heap = isolate->heap();
350 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
351 CHECK(memory_allocator->SetUp(heap->MaxReserved(),
352 heap->MaxExecutableSize()));
353 TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
355 NewSpace new_space(heap);
357 CHECK(new_space.SetUp(CcTest::heap()->ReservedSemiSpaceSize(),
358 CcTest::heap()->ReservedSemiSpaceSize()));
359 CHECK(new_space.HasBeenSetUp());
361 while (new_space.Available() >= Page::kMaxRegularHeapObjectSize) {
363 new_space.AllocateRawUnaligned(Page::kMaxRegularHeapObjectSize)
365 CHECK(new_space.Contains(HeapObject::cast(obj)));
368 new_space.TearDown();
369 memory_allocator->TearDown();
370 delete memory_allocator;
375 Isolate* isolate = CcTest::i_isolate();
376 Heap* heap = isolate->heap();
377 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
378 CHECK(memory_allocator->SetUp(heap->MaxReserved(),
379 heap->MaxExecutableSize()));
380 TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
382 OldSpace* s = new OldSpace(heap, heap->MaxOldGenerationSize(), OLD_SPACE,
388 while (s->Available() > 0) {
389 s->AllocateRawUnaligned(Page::kMaxRegularHeapObjectSize).ToObjectChecked();
394 memory_allocator->TearDown();
395 delete memory_allocator;
399 TEST(LargeObjectSpace) {
400 v8::V8::Initialize();
402 LargeObjectSpace* lo = CcTest::heap()->lo_space();
405 int lo_size = Page::kPageSize;
407 Object* obj = lo->AllocateRaw(lo_size, NOT_EXECUTABLE).ToObjectChecked();
408 CHECK(obj->IsHeapObject());
410 HeapObject* ho = HeapObject::cast(obj);
412 CHECK(lo->Contains(HeapObject::cast(obj)));
414 CHECK(lo->FindObject(ho->address()) == obj);
416 CHECK(lo->Contains(ho));
419 intptr_t available = lo->Available();
420 { AllocationResult allocation = lo->AllocateRaw(lo_size, NOT_EXECUTABLE);
421 if (allocation.IsRetry()) break;
423 // The available value is conservative such that it may report
424 // zero prior to heap exhaustion.
425 CHECK(lo->Available() < available || available == 0);
428 CHECK(!lo->IsEmpty());
430 CHECK(lo->AllocateRaw(lo_size, NOT_EXECUTABLE).IsRetry());
434 TEST(SizeOfFirstPageIsLargeEnough) {
435 if (i::FLAG_always_opt) return;
436 // Bootstrapping without a snapshot causes more allocations.
437 CcTest::InitializeVM();
438 Isolate* isolate = CcTest::i_isolate();
439 if (!isolate->snapshot_available()) return;
440 if (Snapshot::EmbedsScript(isolate)) return;
442 // If this test fails due to enabling experimental natives that are not part
443 // of the snapshot, we may need to adjust CalculateFirstPageSizes.
445 // Freshly initialized VM gets by with one page per space.
446 for (int i = FIRST_PAGED_SPACE; i <= LAST_PAGED_SPACE; i++) {
447 // Debug code can be very large, so skip CODE_SPACE if we are generating it.
448 if (i == CODE_SPACE && i::FLAG_debug_code) continue;
449 CHECK_EQ(1, isolate->heap()->paged_space(i)->CountTotalPages());
452 // Executing the empty script gets by with one page per space.
453 HandleScope scope(isolate);
454 CompileRun("/*empty*/");
455 for (int i = FIRST_PAGED_SPACE; i <= LAST_PAGED_SPACE; i++) {
456 // Debug code can be very large, so skip CODE_SPACE if we are generating it.
457 if (i == CODE_SPACE && i::FLAG_debug_code) continue;
458 CHECK_EQ(1, isolate->heap()->paged_space(i)->CountTotalPages());
461 // No large objects required to perform the above steps.
462 CHECK(isolate->heap()->lo_space()->IsEmpty());
466 UNINITIALIZED_TEST(NewSpaceGrowsToTargetCapacity) {
467 FLAG_target_semi_space_size = 2 * (Page::kPageSize / MB);
468 if (FLAG_optimize_for_size) return;
470 v8::Isolate::CreateParams create_params;
471 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
472 v8::Isolate* isolate = v8::Isolate::New(create_params);
474 v8::Isolate::Scope isolate_scope(isolate);
475 v8::HandleScope handle_scope(isolate);
476 v8::Context::New(isolate)->Enter();
478 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
480 NewSpace* new_space = i_isolate->heap()->new_space();
482 // This test doesn't work if we start with a non-default new space
484 if (new_space->InitialTotalCapacity() == Page::kPageSize) {
485 CHECK(new_space->CommittedMemory() == new_space->InitialTotalCapacity());
487 // Fill up the first (and only) page of the semi space.
488 FillCurrentPage(new_space);
490 // Try to allocate out of the new space. A new page should be added and
492 // allocation should succeed.
493 v8::internal::AllocationResult allocation =
494 new_space->AllocateRawUnaligned(80);
495 CHECK(!allocation.IsRetry());
496 CHECK(new_space->CommittedMemory() == 2 * Page::kPageSize);
498 // Turn the allocation into a proper object so isolate teardown won't
500 HeapObject* free_space = NULL;
501 CHECK(allocation.To(&free_space));
502 new_space->heap()->CreateFillerObjectAt(free_space->address(), 80);