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