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