Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / test / cctest / test-spaces.cc
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
4 // met:
5 //
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.
15 //
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.
27
28 #include <stdlib.h>
29
30 #include "src/base/platform/platform.h"
31 #include "src/snapshot.h"
32 #include "src/v8.h"
33 #include "test/cctest/cctest.h"
34
35
36 using namespace v8::internal;
37
38 #if 0
39 static void VerifyRegionMarking(Address page_start) {
40 #ifdef ENABLE_CARDMARKING_WRITE_BARRIER
41   Page* p = Page::FromAddress(page_start);
42
43   p->SetRegionMarks(Page::kAllRegionsCleanMarks);
44
45   for (Address addr = p->ObjectAreaStart();
46        addr < p->ObjectAreaEnd();
47        addr += kPointerSize) {
48     CHECK(!Page::FromAddress(addr)->IsRegionDirty(addr));
49   }
50
51   for (Address addr = p->ObjectAreaStart();
52        addr < p->ObjectAreaEnd();
53        addr += kPointerSize) {
54     Page::FromAddress(addr)->MarkRegionDirty(addr);
55   }
56
57   for (Address addr = p->ObjectAreaStart();
58        addr < p->ObjectAreaEnd();
59        addr += kPointerSize) {
60     CHECK(Page::FromAddress(addr)->IsRegionDirty(addr));
61   }
62 #endif
63 }
64 #endif
65
66
67 // TODO(gc) you can no longer allocate pages like this. Details are hidden.
68 #if 0
69 TEST(Page) {
70   byte* mem = NewArray<byte>(2*Page::kPageSize);
71   CHECK(mem != NULL);
72
73   Address start = reinterpret_cast<Address>(mem);
74   Address page_start = RoundUp(start, Page::kPageSize);
75
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);
80   CHECK(p->is_valid());
81
82   p->opaque_header = 0;
83   p->SetIsLargeObjectPage(false);
84   CHECK(!p->next_page()->is_valid());
85
86   CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset);
87   CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize);
88
89   CHECK(p->Offset(page_start + Page::kObjectStartOffset) ==
90         Page::kObjectStartOffset);
91   CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize);
92
93   CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart());
94   CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd());
95
96   // test region marking
97   VerifyRegionMarking(page_start);
98
99   DeleteArray(mem);
100 }
101 #endif
102
103
104 namespace v8 {
105 namespace internal {
106
107 // Temporarily sets a given allocator in an isolate.
108 class TestMemoryAllocatorScope {
109  public:
110   TestMemoryAllocatorScope(Isolate* isolate, MemoryAllocator* allocator)
111       : isolate_(isolate),
112         old_allocator_(isolate->memory_allocator_) {
113     isolate->memory_allocator_ = allocator;
114   }
115
116   ~TestMemoryAllocatorScope() {
117     isolate_->memory_allocator_ = old_allocator_;
118   }
119
120  private:
121   Isolate* isolate_;
122   MemoryAllocator* old_allocator_;
123
124   DISALLOW_COPY_AND_ASSIGN(TestMemoryAllocatorScope);
125 };
126
127
128 // Temporarily sets a given code range in an isolate.
129 class TestCodeRangeScope {
130  public:
131   TestCodeRangeScope(Isolate* isolate, CodeRange* code_range)
132       : isolate_(isolate),
133         old_code_range_(isolate->code_range_) {
134     isolate->code_range_ = code_range;
135   }
136
137   ~TestCodeRangeScope() {
138     isolate_->code_range_ = old_code_range_;
139   }
140
141  private:
142   Isolate* isolate_;
143   CodeRange* old_code_range_;
144
145   DISALLOW_COPY_AND_ASSIGN(TestCodeRangeScope);
146 };
147
148 } }  // namespace v8::internal
149
150
151 static void VerifyMemoryChunk(Isolate* isolate,
152                               Heap* heap,
153                               CodeRange* code_range,
154                               size_t reserve_area_size,
155                               size_t commit_area_size,
156                               size_t second_commit_area_size,
157                               Executability executable) {
158   MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
159   CHECK(memory_allocator->SetUp(heap->MaxReserved(),
160                                 heap->MaxExecutableSize()));
161   TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator);
162   TestCodeRangeScope test_code_range_scope(isolate, code_range);
163
164   size_t header_size = (executable == EXECUTABLE)
165                        ? MemoryAllocator::CodePageGuardStartOffset()
166                        : MemoryChunk::kObjectStartOffset;
167   size_t guard_size = (executable == EXECUTABLE)
168                        ? MemoryAllocator::CodePageGuardSize()
169                        : 0;
170
171   MemoryChunk* memory_chunk = memory_allocator->AllocateChunk(reserve_area_size,
172                                                               commit_area_size,
173                                                               executable,
174                                                               NULL);
175   size_t alignment = code_range != NULL && code_range->valid() ?
176                      MemoryChunk::kAlignment : v8::base::OS::CommitPageSize();
177   size_t reserved_size =
178       ((executable == EXECUTABLE))
179           ? RoundUp(header_size + guard_size + reserve_area_size + guard_size,
180                     alignment)
181           : RoundUp(header_size + reserve_area_size,
182                     v8::base::OS::CommitPageSize());
183   CHECK(memory_chunk->size() == reserved_size);
184   CHECK(memory_chunk->area_start() < memory_chunk->address() +
185                                      memory_chunk->size());
186   CHECK(memory_chunk->area_end() <= memory_chunk->address() +
187                                     memory_chunk->size());
188   CHECK(static_cast<size_t>(memory_chunk->area_size()) == commit_area_size);
189
190   Address area_start = memory_chunk->area_start();
191
192   memory_chunk->CommitArea(second_commit_area_size);
193   CHECK(area_start == memory_chunk->area_start());
194   CHECK(memory_chunk->area_start() < memory_chunk->address() +
195                                      memory_chunk->size());
196   CHECK(memory_chunk->area_end() <= memory_chunk->address() +
197                                     memory_chunk->size());
198   CHECK(static_cast<size_t>(memory_chunk->area_size()) ==
199       second_commit_area_size);
200
201   memory_allocator->Free(memory_chunk);
202   memory_allocator->TearDown();
203   delete memory_allocator;
204 }
205
206
207 TEST(Regress3540) {
208   Isolate* isolate = CcTest::i_isolate();
209   Heap* heap = isolate->heap();
210   MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
211   CHECK(
212       memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize()));
213   TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator);
214   CodeRange* code_range = new CodeRange(isolate);
215   const size_t code_range_size = 4 * MB;
216   if (!code_range->SetUp(
217           code_range_size +
218           RoundUp(v8::base::OS::CommitPageSize() * kReservedCodeRangePages,
219                   MemoryChunk::kAlignment) +
220           v8::internal::MemoryAllocator::CodePageAreaSize())) {
221     return;
222   }
223   Address address;
224   size_t size;
225   address = code_range->AllocateRawMemory(code_range_size - 2 * MB,
226                                           code_range_size - 2 * MB, &size);
227   CHECK(address != NULL);
228   Address null_address;
229   size_t null_size;
230   null_address = code_range->AllocateRawMemory(
231       code_range_size - MB, code_range_size - MB, &null_size);
232   CHECK(null_address == NULL);
233   code_range->FreeRawMemory(address, size);
234   delete code_range;
235   memory_allocator->TearDown();
236   delete memory_allocator;
237 }
238
239
240 static unsigned int Pseudorandom() {
241   static uint32_t lo = 2345;
242   lo = 18273 * (lo & 0xFFFFF) + (lo >> 16);
243   return lo & 0xFFFFF;
244 }
245
246
247 TEST(MemoryChunk) {
248   Isolate* isolate = CcTest::i_isolate();
249   Heap* heap = isolate->heap();
250
251   size_t reserve_area_size = 1 * MB;
252   size_t initial_commit_area_size, second_commit_area_size;
253
254   for (int i = 0; i < 100; i++) {
255     initial_commit_area_size = Pseudorandom();
256     second_commit_area_size = Pseudorandom();
257
258     // With CodeRange.
259     CodeRange* code_range = new CodeRange(isolate);
260     const size_t code_range_size = 32 * MB;
261     if (!code_range->SetUp(code_range_size)) return;
262
263     VerifyMemoryChunk(isolate,
264                       heap,
265                       code_range,
266                       reserve_area_size,
267                       initial_commit_area_size,
268                       second_commit_area_size,
269                       EXECUTABLE);
270
271     VerifyMemoryChunk(isolate,
272                       heap,
273                       code_range,
274                       reserve_area_size,
275                       initial_commit_area_size,
276                       second_commit_area_size,
277                       NOT_EXECUTABLE);
278     delete code_range;
279
280     // Without CodeRange.
281     code_range = NULL;
282     VerifyMemoryChunk(isolate,
283                       heap,
284                       code_range,
285                       reserve_area_size,
286                       initial_commit_area_size,
287                       second_commit_area_size,
288                       EXECUTABLE);
289
290     VerifyMemoryChunk(isolate,
291                       heap,
292                       code_range,
293                       reserve_area_size,
294                       initial_commit_area_size,
295                       second_commit_area_size,
296                       NOT_EXECUTABLE);
297   }
298 }
299
300
301 TEST(MemoryAllocator) {
302   Isolate* isolate = CcTest::i_isolate();
303   Heap* heap = isolate->heap();
304
305   MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
306   CHECK(memory_allocator->SetUp(heap->MaxReserved(),
307                                 heap->MaxExecutableSize()));
308
309   int total_pages = 0;
310   OldSpace faked_space(heap,
311                        heap->MaxReserved(),
312                        OLD_POINTER_SPACE,
313                        NOT_EXECUTABLE);
314   Page* first_page = memory_allocator->AllocatePage(
315       faked_space.AreaSize(), &faked_space, NOT_EXECUTABLE);
316
317   first_page->InsertAfter(faked_space.anchor()->prev_page());
318   CHECK(first_page->is_valid());
319   CHECK(first_page->next_page() == faked_space.anchor());
320   total_pages++;
321
322   for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) {
323     CHECK(p->owner() == &faked_space);
324   }
325
326   // Again, we should get n or n - 1 pages.
327   Page* other = memory_allocator->AllocatePage(
328       faked_space.AreaSize(), &faked_space, NOT_EXECUTABLE);
329   CHECK(other->is_valid());
330   total_pages++;
331   other->InsertAfter(first_page);
332   int page_count = 0;
333   for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) {
334     CHECK(p->owner() == &faked_space);
335     page_count++;
336   }
337   CHECK(total_pages == page_count);
338
339   Page* second_page = first_page->next_page();
340   CHECK(second_page->is_valid());
341   memory_allocator->Free(first_page);
342   memory_allocator->Free(second_page);
343   memory_allocator->TearDown();
344   delete memory_allocator;
345 }
346
347
348 TEST(NewSpace) {
349   Isolate* isolate = CcTest::i_isolate();
350   Heap* heap = isolate->heap();
351   MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
352   CHECK(memory_allocator->SetUp(heap->MaxReserved(),
353                                 heap->MaxExecutableSize()));
354   TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
355
356   NewSpace new_space(heap);
357
358   CHECK(new_space.SetUp(CcTest::heap()->ReservedSemiSpaceSize(),
359                         CcTest::heap()->ReservedSemiSpaceSize()));
360   CHECK(new_space.HasBeenSetUp());
361
362   while (new_space.Available() >= Page::kMaxRegularHeapObjectSize) {
363     Object* obj = new_space.AllocateRaw(
364         Page::kMaxRegularHeapObjectSize).ToObjectChecked();
365     CHECK(new_space.Contains(HeapObject::cast(obj)));
366   }
367
368   new_space.TearDown();
369   memory_allocator->TearDown();
370   delete memory_allocator;
371 }
372
373
374 TEST(OldSpace) {
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);
381
382   OldSpace* s = new OldSpace(heap,
383                              heap->MaxOldGenerationSize(),
384                              OLD_POINTER_SPACE,
385                              NOT_EXECUTABLE);
386   CHECK(s != NULL);
387
388   CHECK(s->SetUp());
389
390   while (s->Available() > 0) {
391     s->AllocateRaw(Page::kMaxRegularHeapObjectSize).ToObjectChecked();
392   }
393
394   s->TearDown();
395   delete s;
396   memory_allocator->TearDown();
397   delete memory_allocator;
398 }
399
400
401 TEST(LargeObjectSpace) {
402   v8::V8::Initialize();
403
404   LargeObjectSpace* lo = CcTest::heap()->lo_space();
405   CHECK(lo != NULL);
406
407   int lo_size = Page::kPageSize;
408
409   Object* obj = lo->AllocateRaw(lo_size, NOT_EXECUTABLE).ToObjectChecked();
410   CHECK(obj->IsHeapObject());
411
412   HeapObject* ho = HeapObject::cast(obj);
413
414   CHECK(lo->Contains(HeapObject::cast(obj)));
415
416   CHECK(lo->FindObject(ho->address()) == obj);
417
418   CHECK(lo->Contains(ho));
419
420   while (true) {
421     intptr_t available = lo->Available();
422     { AllocationResult allocation = lo->AllocateRaw(lo_size, NOT_EXECUTABLE);
423       if (allocation.IsRetry()) break;
424     }
425     CHECK(lo->Available() < available);
426   }
427
428   CHECK(!lo->IsEmpty());
429
430   CHECK(lo->AllocateRaw(lo_size, NOT_EXECUTABLE).IsRetry());
431 }
432
433
434 TEST(SizeOfFirstPageIsLargeEnough) {
435   if (i::FLAG_always_opt) return;
436   // Bootstrapping without a snapshot causes more allocations.
437   if (!i::Snapshot::HaveASnapshotToStartFrom()) return;
438   CcTest::InitializeVM();
439   Isolate* isolate = CcTest::i_isolate();
440
441   // Freshly initialized VM gets by with one page per space.
442   for (int i = FIRST_PAGED_SPACE; i <= LAST_PAGED_SPACE; i++) {
443     // Debug code can be very large, so skip CODE_SPACE if we are generating it.
444     if (i == CODE_SPACE && i::FLAG_debug_code) continue;
445     CHECK_EQ(1, isolate->heap()->paged_space(i)->CountTotalPages());
446   }
447
448   // Executing the empty script gets by with one page per space.
449   HandleScope scope(isolate);
450   CompileRun("/*empty*/");
451   for (int i = FIRST_PAGED_SPACE; i <= LAST_PAGED_SPACE; i++) {
452     // Debug code can be very large, so skip CODE_SPACE if we are generating it.
453     if (i == CODE_SPACE && i::FLAG_debug_code) continue;
454     CHECK_EQ(1, isolate->heap()->paged_space(i)->CountTotalPages());
455   }
456
457   // No large objects required to perform the above steps.
458   CHECK(isolate->heap()->lo_space()->IsEmpty());
459 }
460
461
462 static inline void FillCurrentPage(v8::internal::NewSpace* space) {
463   int new_linear_size = static_cast<int>(*space->allocation_limit_address() -
464                                          *space->allocation_top_address());
465   if (new_linear_size == 0) return;
466   v8::internal::AllocationResult allocation =
467       space->AllocateRaw(new_linear_size);
468   v8::internal::FreeListNode* node =
469       v8::internal::FreeListNode::cast(allocation.ToObjectChecked());
470   node->set_size(space->heap(), new_linear_size);
471 }
472
473
474 UNINITIALIZED_TEST(NewSpaceGrowsToTargetCapacity) {
475   FLAG_target_semi_space_size = 2;
476   if (FLAG_optimize_for_size) return;
477
478   v8::Isolate* isolate = v8::Isolate::New();
479   {
480     v8::Isolate::Scope isolate_scope(isolate);
481     v8::HandleScope handle_scope(isolate);
482     v8::Context::New(isolate)->Enter();
483
484     Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
485
486     NewSpace* new_space = i_isolate->heap()->new_space();
487
488     // This test doesn't work if we start with a non-default new space
489     // configuration.
490     if (new_space->InitialTotalCapacity() == Page::kPageSize) {
491       CHECK(new_space->CommittedMemory() == new_space->InitialTotalCapacity());
492
493       // Fill up the first (and only) page of the semi space.
494       FillCurrentPage(new_space);
495
496       // Try to allocate out of the new space. A new page should be added and
497       // the
498       // allocation should succeed.
499       v8::internal::AllocationResult allocation = new_space->AllocateRaw(80);
500       CHECK(!allocation.IsRetry());
501       CHECK(new_space->CommittedMemory() == 2 * Page::kPageSize);
502
503       // Turn the allocation into a proper object so isolate teardown won't
504       // crash.
505       v8::internal::FreeListNode* node =
506           v8::internal::FreeListNode::cast(allocation.ToObjectChecked());
507       node->set_size(new_space->heap(), 80);
508     }
509   }
510   isolate->Dispose();
511 }