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