1d4b0d8e7d7a9188d47bf4feb250a41ae11a0d55
[platform/framework/web/crosswalk.git] / src / v8 / test / cctest / test-mark-compact.cc
1 // Copyright 2012 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 #ifdef __linux__
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #endif
37
38 #include <utility>
39
40 #include "src/v8.h"
41
42 #include "src/full-codegen.h"
43 #include "src/global-handles.h"
44 #include "src/snapshot.h"
45 #include "test/cctest/cctest.h"
46
47 using namespace v8::internal;
48
49
50 TEST(MarkingDeque) {
51   CcTest::InitializeVM();
52   int mem_size = 20 * kPointerSize;
53   byte* mem = NewArray<byte>(20*kPointerSize);
54   Address low = reinterpret_cast<Address>(mem);
55   Address high = low + mem_size;
56   MarkingDeque s;
57   s.Initialize(low, high);
58
59   Address original_address = reinterpret_cast<Address>(&s);
60   Address current_address = original_address;
61   while (!s.IsFull()) {
62     s.PushBlack(HeapObject::FromAddress(current_address));
63     current_address += kPointerSize;
64   }
65
66   while (!s.IsEmpty()) {
67     Address value = s.Pop()->address();
68     current_address -= kPointerSize;
69     CHECK_EQ(current_address, value);
70   }
71
72   CHECK_EQ(original_address, current_address);
73   DeleteArray(mem);
74 }
75
76
77 TEST(Promotion) {
78   CcTest::InitializeVM();
79   TestHeap* heap = CcTest::test_heap();
80   heap->ConfigureHeap(1, 1, 1, 0);
81
82   v8::HandleScope sc(CcTest::isolate());
83
84   // Allocate a fixed array in the new space.
85   int array_length =
86       (Page::kMaxRegularHeapObjectSize - FixedArray::kHeaderSize) /
87       (4 * kPointerSize);
88   Object* obj = heap->AllocateFixedArray(array_length).ToObjectChecked();
89   Handle<FixedArray> array(FixedArray::cast(obj));
90
91   // Array should be in the new space.
92   CHECK(heap->InSpace(*array, NEW_SPACE));
93
94   // Call mark compact GC, so array becomes an old object.
95   heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
96   heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
97
98   // Array now sits in the old space
99   CHECK(heap->InSpace(*array, OLD_POINTER_SPACE));
100 }
101
102
103 TEST(NoPromotion) {
104   CcTest::InitializeVM();
105   TestHeap* heap = CcTest::test_heap();
106   heap->ConfigureHeap(1, 1, 1, 0);
107
108   v8::HandleScope sc(CcTest::isolate());
109
110   // Allocate a big fixed array in the new space.
111   int array_length =
112       (Page::kMaxRegularHeapObjectSize - FixedArray::kHeaderSize) /
113       (2 * kPointerSize);
114   Object* obj = heap->AllocateFixedArray(array_length).ToObjectChecked();
115   Handle<FixedArray> array(FixedArray::cast(obj));
116
117   // Array should be in the new space.
118   CHECK(heap->InSpace(*array, NEW_SPACE));
119
120   // Simulate a full old space to make promotion fail.
121   SimulateFullSpace(heap->old_pointer_space());
122
123   // Call mark compact GC, and it should pass.
124   heap->CollectGarbage(OLD_POINTER_SPACE);
125 }
126
127
128 TEST(MarkCompactCollector) {
129   FLAG_incremental_marking = false;
130   CcTest::InitializeVM();
131   Isolate* isolate = CcTest::i_isolate();
132   TestHeap* heap = CcTest::test_heap();
133   Factory* factory = isolate->factory();
134
135   v8::HandleScope sc(CcTest::isolate());
136   Handle<GlobalObject> global(isolate->context()->global_object());
137
138   // call mark-compact when heap is empty
139   heap->CollectGarbage(OLD_POINTER_SPACE, "trigger 1");
140
141   // keep allocating garbage in new space until it fails
142   const int ARRAY_SIZE = 100;
143   AllocationResult allocation;
144   do {
145     allocation = heap->AllocateFixedArray(ARRAY_SIZE);
146   } while (!allocation.IsRetry());
147   heap->CollectGarbage(NEW_SPACE, "trigger 2");
148   heap->AllocateFixedArray(ARRAY_SIZE).ToObjectChecked();
149
150   // keep allocating maps until it fails
151   do {
152     allocation = heap->AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
153   } while (!allocation.IsRetry());
154   heap->CollectGarbage(MAP_SPACE, "trigger 3");
155   heap->AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize).ToObjectChecked();
156
157   { HandleScope scope(isolate);
158     // allocate a garbage
159     Handle<String> func_name = factory->InternalizeUtf8String("theFunction");
160     Handle<JSFunction> function = factory->NewFunction(func_name);
161     JSReceiver::SetProperty(global, func_name, function, SLOPPY).Check();
162
163     factory->NewJSObject(function);
164   }
165
166   heap->CollectGarbage(OLD_POINTER_SPACE, "trigger 4");
167
168   { HandleScope scope(isolate);
169     Handle<String> func_name = factory->InternalizeUtf8String("theFunction");
170     v8::Maybe<bool> maybe = JSReceiver::HasOwnProperty(global, func_name);
171     CHECK(maybe.has_value);
172     CHECK(maybe.value);
173     Handle<Object> func_value =
174         Object::GetProperty(global, func_name).ToHandleChecked();
175     CHECK(func_value->IsJSFunction());
176     Handle<JSFunction> function = Handle<JSFunction>::cast(func_value);
177     Handle<JSObject> obj = factory->NewJSObject(function);
178
179     Handle<String> obj_name = factory->InternalizeUtf8String("theObject");
180     JSReceiver::SetProperty(global, obj_name, obj, SLOPPY).Check();
181     Handle<String> prop_name = factory->InternalizeUtf8String("theSlot");
182     Handle<Smi> twenty_three(Smi::FromInt(23), isolate);
183     JSReceiver::SetProperty(obj, prop_name, twenty_three, SLOPPY).Check();
184   }
185
186   heap->CollectGarbage(OLD_POINTER_SPACE, "trigger 5");
187
188   { HandleScope scope(isolate);
189     Handle<String> obj_name = factory->InternalizeUtf8String("theObject");
190     v8::Maybe<bool> maybe = JSReceiver::HasOwnProperty(global, obj_name);
191     CHECK(maybe.has_value);
192     CHECK(maybe.value);
193     Handle<Object> object =
194         Object::GetProperty(global, obj_name).ToHandleChecked();
195     CHECK(object->IsJSObject());
196     Handle<String> prop_name = factory->InternalizeUtf8String("theSlot");
197     CHECK_EQ(*Object::GetProperty(object, prop_name).ToHandleChecked(),
198              Smi::FromInt(23));
199   }
200 }
201
202
203 // TODO(1600): compaction of map space is temporary removed from GC.
204 #if 0
205 static Handle<Map> CreateMap(Isolate* isolate) {
206   return isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
207 }
208
209
210 TEST(MapCompact) {
211   FLAG_max_map_space_pages = 16;
212   CcTest::InitializeVM();
213   Isolate* isolate = CcTest::i_isolate();
214   Factory* factory = isolate->factory();
215
216   {
217     v8::HandleScope sc;
218     // keep allocating maps while pointers are still encodable and thus
219     // mark compact is permitted.
220     Handle<JSObject> root = factory->NewJSObjectFromMap(CreateMap());
221     do {
222       Handle<Map> map = CreateMap();
223       map->set_prototype(*root);
224       root = factory->NewJSObjectFromMap(map);
225     } while (CcTest::heap()->map_space()->MapPointersEncodable());
226   }
227   // Now, as we don't have any handles to just allocated maps, we should
228   // be able to trigger map compaction.
229   // To give an additional chance to fail, try to force compaction which
230   // should be impossible right now.
231   CcTest::heap()->CollectAllGarbage(Heap::kForceCompactionMask);
232   // And now map pointers should be encodable again.
233   CHECK(CcTest::heap()->map_space()->MapPointersEncodable());
234 }
235 #endif
236
237
238 static int NumberOfWeakCalls = 0;
239 static void WeakPointerCallback(
240     const v8::WeakCallbackData<v8::Value, void>& data) {
241   std::pair<v8::Persistent<v8::Value>*, int>* p =
242       reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
243           data.GetParameter());
244   DCHECK_EQ(1234, p->second);
245   NumberOfWeakCalls++;
246   p->first->Reset();
247 }
248
249
250 TEST(ObjectGroups) {
251   FLAG_incremental_marking = false;
252   CcTest::InitializeVM();
253   GlobalHandles* global_handles = CcTest::i_isolate()->global_handles();
254   TestHeap* heap = CcTest::test_heap();
255   NumberOfWeakCalls = 0;
256   v8::HandleScope handle_scope(CcTest::isolate());
257
258   Handle<Object> g1s1 =
259       global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
260   Handle<Object> g1s2 =
261       global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
262   Handle<Object> g1c1 =
263       global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
264   std::pair<Handle<Object>*, int> g1s1_and_id(&g1s1, 1234);
265   GlobalHandles::MakeWeak(g1s1.location(),
266                           reinterpret_cast<void*>(&g1s1_and_id),
267                           &WeakPointerCallback);
268   std::pair<Handle<Object>*, int> g1s2_and_id(&g1s2, 1234);
269   GlobalHandles::MakeWeak(g1s2.location(),
270                           reinterpret_cast<void*>(&g1s2_and_id),
271                           &WeakPointerCallback);
272   std::pair<Handle<Object>*, int> g1c1_and_id(&g1c1, 1234);
273   GlobalHandles::MakeWeak(g1c1.location(),
274                           reinterpret_cast<void*>(&g1c1_and_id),
275                           &WeakPointerCallback);
276
277   Handle<Object> g2s1 =
278       global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
279   Handle<Object> g2s2 =
280     global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
281   Handle<Object> g2c1 =
282     global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
283   std::pair<Handle<Object>*, int> g2s1_and_id(&g2s1, 1234);
284   GlobalHandles::MakeWeak(g2s1.location(),
285                           reinterpret_cast<void*>(&g2s1_and_id),
286                           &WeakPointerCallback);
287   std::pair<Handle<Object>*, int> g2s2_and_id(&g2s2, 1234);
288   GlobalHandles::MakeWeak(g2s2.location(),
289                           reinterpret_cast<void*>(&g2s2_and_id),
290                           &WeakPointerCallback);
291   std::pair<Handle<Object>*, int> g2c1_and_id(&g2c1, 1234);
292   GlobalHandles::MakeWeak(g2c1.location(),
293                           reinterpret_cast<void*>(&g2c1_and_id),
294                           &WeakPointerCallback);
295
296   Handle<Object> root = global_handles->Create(*g1s1);  // make a root.
297
298   // Connect group 1 and 2, make a cycle.
299   Handle<FixedArray>::cast(g1s2)->set(0, *g2s2);
300   Handle<FixedArray>::cast(g2s1)->set(0, *g1s1);
301
302   {
303     Object** g1_objects[] = { g1s1.location(), g1s2.location() };
304     Object** g1_children[] = { g1c1.location() };
305     Object** g2_objects[] = { g2s1.location(), g2s2.location() };
306     Object** g2_children[] = { g2c1.location() };
307     global_handles->AddObjectGroup(g1_objects, 2, NULL);
308     global_handles->AddImplicitReferences(
309         Handle<HeapObject>::cast(g1s1).location(), g1_children, 1);
310     global_handles->AddObjectGroup(g2_objects, 2, NULL);
311     global_handles->AddImplicitReferences(
312         Handle<HeapObject>::cast(g2s1).location(), g2_children, 1);
313   }
314   // Do a full GC
315   heap->CollectGarbage(OLD_POINTER_SPACE);
316
317   // All object should be alive.
318   CHECK_EQ(0, NumberOfWeakCalls);
319
320   // Weaken the root.
321   std::pair<Handle<Object>*, int> root_and_id(&root, 1234);
322   GlobalHandles::MakeWeak(root.location(),
323                           reinterpret_cast<void*>(&root_and_id),
324                           &WeakPointerCallback);
325   // But make children strong roots---all the objects (except for children)
326   // should be collectable now.
327   global_handles->ClearWeakness(g1c1.location());
328   global_handles->ClearWeakness(g2c1.location());
329
330   // Groups are deleted, rebuild groups.
331   {
332     Object** g1_objects[] = { g1s1.location(), g1s2.location() };
333     Object** g1_children[] = { g1c1.location() };
334     Object** g2_objects[] = { g2s1.location(), g2s2.location() };
335     Object** g2_children[] = { g2c1.location() };
336     global_handles->AddObjectGroup(g1_objects, 2, NULL);
337     global_handles->AddImplicitReferences(
338         Handle<HeapObject>::cast(g1s1).location(), g1_children, 1);
339     global_handles->AddObjectGroup(g2_objects, 2, NULL);
340     global_handles->AddImplicitReferences(
341         Handle<HeapObject>::cast(g2s1).location(), g2_children, 1);
342   }
343
344   heap->CollectGarbage(OLD_POINTER_SPACE);
345
346   // All objects should be gone. 5 global handles in total.
347   CHECK_EQ(5, NumberOfWeakCalls);
348
349   // And now make children weak again and collect them.
350   GlobalHandles::MakeWeak(g1c1.location(),
351                           reinterpret_cast<void*>(&g1c1_and_id),
352                           &WeakPointerCallback);
353   GlobalHandles::MakeWeak(g2c1.location(),
354                           reinterpret_cast<void*>(&g2c1_and_id),
355                           &WeakPointerCallback);
356
357   heap->CollectGarbage(OLD_POINTER_SPACE);
358   CHECK_EQ(7, NumberOfWeakCalls);
359 }
360
361
362 class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
363  public:
364   TestRetainedObjectInfo() : has_been_disposed_(false) {}
365
366   bool has_been_disposed() { return has_been_disposed_; }
367
368   virtual void Dispose() {
369     DCHECK(!has_been_disposed_);
370     has_been_disposed_ = true;
371   }
372
373   virtual bool IsEquivalent(v8::RetainedObjectInfo* other) {
374     return other == this;
375   }
376
377   virtual intptr_t GetHash() { return 0; }
378
379   virtual const char* GetLabel() { return "whatever"; }
380
381  private:
382   bool has_been_disposed_;
383 };
384
385
386 TEST(EmptyObjectGroups) {
387   CcTest::InitializeVM();
388   GlobalHandles* global_handles = CcTest::i_isolate()->global_handles();
389
390   v8::HandleScope handle_scope(CcTest::isolate());
391
392   Handle<Object> object = global_handles->Create(
393       CcTest::test_heap()->AllocateFixedArray(1).ToObjectChecked());
394
395   TestRetainedObjectInfo info;
396   global_handles->AddObjectGroup(NULL, 0, &info);
397   DCHECK(info.has_been_disposed());
398
399   global_handles->AddImplicitReferences(
400         Handle<HeapObject>::cast(object).location(), NULL, 0);
401 }
402
403
404 #if defined(__has_feature)
405 #if __has_feature(address_sanitizer)
406 #define V8_WITH_ASAN 1
407 #endif
408 #endif
409
410
411 // Here is a memory use test that uses /proc, and is therefore Linux-only.  We
412 // do not care how much memory the simulator uses, since it is only there for
413 // debugging purposes. Testing with ASAN doesn't make sense, either.
414 #if defined(__linux__) && !defined(USE_SIMULATOR) && !defined(V8_WITH_ASAN)
415
416
417 static uintptr_t ReadLong(char* buffer, intptr_t* position, int base) {
418   char* end_address = buffer + *position;
419   uintptr_t result = strtoul(buffer + *position, &end_address, base);
420   CHECK(result != ULONG_MAX || errno != ERANGE);
421   CHECK(end_address > buffer + *position);
422   *position = end_address - buffer;
423   return result;
424 }
425
426
427 // The memory use computed this way is not entirely accurate and depends on
428 // the way malloc allocates memory.  That's why the memory use may seem to
429 // increase even though the sum of the allocated object sizes decreases.  It
430 // also means that the memory use depends on the kernel and stdlib.
431 static intptr_t MemoryInUse() {
432   intptr_t memory_use = 0;
433
434   int fd = open("/proc/self/maps", O_RDONLY);
435   if (fd < 0) return -1;
436
437   const int kBufSize = 10000;
438   char buffer[kBufSize];
439   int length = read(fd, buffer, kBufSize);
440   intptr_t line_start = 0;
441   CHECK_LT(length, kBufSize);  // Make the buffer bigger.
442   CHECK_GT(length, 0);  // We have to find some data in the file.
443   while (line_start < length) {
444     if (buffer[line_start] == '\n') {
445       line_start++;
446       continue;
447     }
448     intptr_t position = line_start;
449     uintptr_t start = ReadLong(buffer, &position, 16);
450     CHECK_EQ(buffer[position++], '-');
451     uintptr_t end = ReadLong(buffer, &position, 16);
452     CHECK_EQ(buffer[position++], ' ');
453     CHECK(buffer[position] == '-' || buffer[position] == 'r');
454     bool read_permission = (buffer[position++] == 'r');
455     CHECK(buffer[position] == '-' || buffer[position] == 'w');
456     bool write_permission = (buffer[position++] == 'w');
457     CHECK(buffer[position] == '-' || buffer[position] == 'x');
458     bool execute_permission = (buffer[position++] == 'x');
459     CHECK(buffer[position] == '-' || buffer[position] == 'p');
460     bool private_mapping = (buffer[position++] == 'p');
461     CHECK_EQ(buffer[position++], ' ');
462     uintptr_t offset = ReadLong(buffer, &position, 16);
463     USE(offset);
464     CHECK_EQ(buffer[position++], ' ');
465     uintptr_t major = ReadLong(buffer, &position, 16);
466     USE(major);
467     CHECK_EQ(buffer[position++], ':');
468     uintptr_t minor = ReadLong(buffer, &position, 16);
469     USE(minor);
470     CHECK_EQ(buffer[position++], ' ');
471     uintptr_t inode = ReadLong(buffer, &position, 10);
472     while (position < length && buffer[position] != '\n') position++;
473     if ((read_permission || write_permission || execute_permission) &&
474         private_mapping && inode == 0) {
475       memory_use += (end - start);
476     }
477
478     line_start = position;
479   }
480   close(fd);
481   return memory_use;
482 }
483
484
485 intptr_t ShortLivingIsolate() {
486   v8::Isolate* isolate = v8::Isolate::New();
487   { v8::Isolate::Scope isolate_scope(isolate);
488     v8::Locker lock(isolate);
489     v8::HandleScope handle_scope(isolate);
490     v8::Local<v8::Context> context = v8::Context::New(isolate);
491     CHECK(!context.IsEmpty());
492   }
493   isolate->Dispose();
494   return MemoryInUse();
495 }
496
497
498 TEST(RegressJoinThreadsOnIsolateDeinit) {
499   intptr_t size_limit = ShortLivingIsolate() * 2;
500   for (int i = 0; i < 10; i++) {
501     CHECK_GT(size_limit, ShortLivingIsolate());
502   }
503 }
504
505 #endif  // __linux__ and !USE_SIMULATOR