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
TEST(MarkingDeque)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
TEST(Promotion)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
TEST(NoPromotion)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
TEST(MarkCompactCollector)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 arraysize = 100;
143 AllocationResult allocation;
144 do {
145 allocation = heap->AllocateFixedArray(arraysize);
146 } while (!allocation.IsRetry());
147 heap->CollectGarbage(NEW_SPACE, "trigger 2");
148 heap->AllocateFixedArray(arraysize).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;
WeakPointerCallback(const v8::WeakCallbackData<v8::Value,void> & data)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
TEST(ObjectGroups)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** g2_objects[] = { g2s1.location(), g2s2.location() };
305 global_handles->AddObjectGroup(g1_objects, 2, NULL);
306 global_handles->SetReference(Handle<HeapObject>::cast(g1s1).location(),
307 g1c1.location());
308 global_handles->AddObjectGroup(g2_objects, 2, NULL);
309 global_handles->SetReference(Handle<HeapObject>::cast(g2s1).location(),
310 g2c1.location());
311 }
312 // Do a full GC
313 heap->CollectGarbage(OLD_POINTER_SPACE);
314
315 // All object should be alive.
316 CHECK_EQ(0, NumberOfWeakCalls);
317
318 // Weaken the root.
319 std::pair<Handle<Object>*, int> root_and_id(&root, 1234);
320 GlobalHandles::MakeWeak(root.location(),
321 reinterpret_cast<void*>(&root_and_id),
322 &WeakPointerCallback);
323 // But make children strong roots---all the objects (except for children)
324 // should be collectable now.
325 global_handles->ClearWeakness(g1c1.location());
326 global_handles->ClearWeakness(g2c1.location());
327
328 // Groups are deleted, rebuild groups.
329 {
330 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
331 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
332 global_handles->AddObjectGroup(g1_objects, 2, NULL);
333 global_handles->SetReference(Handle<HeapObject>::cast(g1s1).location(),
334 g1c1.location());
335 global_handles->AddObjectGroup(g2_objects, 2, NULL);
336 global_handles->SetReference(Handle<HeapObject>::cast(g2s1).location(),
337 g2c1.location());
338 }
339
340 heap->CollectGarbage(OLD_POINTER_SPACE);
341
342 // All objects should be gone. 5 global handles in total.
343 CHECK_EQ(5, NumberOfWeakCalls);
344
345 // And now make children weak again and collect them.
346 GlobalHandles::MakeWeak(g1c1.location(),
347 reinterpret_cast<void*>(&g1c1_and_id),
348 &WeakPointerCallback);
349 GlobalHandles::MakeWeak(g2c1.location(),
350 reinterpret_cast<void*>(&g2c1_and_id),
351 &WeakPointerCallback);
352
353 heap->CollectGarbage(OLD_POINTER_SPACE);
354 CHECK_EQ(7, NumberOfWeakCalls);
355 }
356
357
358 class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
359 public:
TestRetainedObjectInfo()360 TestRetainedObjectInfo() : has_been_disposed_(false) {}
361
has_been_disposed()362 bool has_been_disposed() { return has_been_disposed_; }
363
Dispose()364 virtual void Dispose() {
365 DCHECK(!has_been_disposed_);
366 has_been_disposed_ = true;
367 }
368
IsEquivalent(v8::RetainedObjectInfo * other)369 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) {
370 return other == this;
371 }
372
GetHash()373 virtual intptr_t GetHash() { return 0; }
374
GetLabel()375 virtual const char* GetLabel() { return "whatever"; }
376
377 private:
378 bool has_been_disposed_;
379 };
380
381
TEST(EmptyObjectGroups)382 TEST(EmptyObjectGroups) {
383 CcTest::InitializeVM();
384 GlobalHandles* global_handles = CcTest::i_isolate()->global_handles();
385
386 v8::HandleScope handle_scope(CcTest::isolate());
387
388 TestRetainedObjectInfo info;
389 global_handles->AddObjectGroup(NULL, 0, &info);
390 DCHECK(info.has_been_disposed());
391 }
392
393
394 #if defined(__has_feature)
395 #if __has_feature(address_sanitizer)
396 #define V8_WITH_ASAN 1
397 #endif
398 #endif
399
400
401 // Here is a memory use test that uses /proc, and is therefore Linux-only. We
402 // do not care how much memory the simulator uses, since it is only there for
403 // debugging purposes. Testing with ASAN doesn't make sense, either.
404 #if defined(__linux__) && !defined(USE_SIMULATOR) && !defined(V8_WITH_ASAN)
405
406
ReadLong(char * buffer,intptr_t * position,int base)407 static uintptr_t ReadLong(char* buffer, intptr_t* position, int base) {
408 char* end_address = buffer + *position;
409 uintptr_t result = strtoul(buffer + *position, &end_address, base);
410 CHECK(result != ULONG_MAX || errno != ERANGE);
411 CHECK(end_address > buffer + *position);
412 *position = end_address - buffer;
413 return result;
414 }
415
416
417 // The memory use computed this way is not entirely accurate and depends on
418 // the way malloc allocates memory. That's why the memory use may seem to
419 // increase even though the sum of the allocated object sizes decreases. It
420 // also means that the memory use depends on the kernel and stdlib.
MemoryInUse()421 static intptr_t MemoryInUse() {
422 intptr_t memory_use = 0;
423
424 int fd = open("/proc/self/maps", O_RDONLY);
425 if (fd < 0) return -1;
426
427 const int kBufSize = 10000;
428 char buffer[kBufSize];
429 int length = read(fd, buffer, kBufSize);
430 intptr_t line_start = 0;
431 CHECK_LT(length, kBufSize); // Make the buffer bigger.
432 CHECK_GT(length, 0); // We have to find some data in the file.
433 while (line_start < length) {
434 if (buffer[line_start] == '\n') {
435 line_start++;
436 continue;
437 }
438 intptr_t position = line_start;
439 uintptr_t start = ReadLong(buffer, &position, 16);
440 CHECK_EQ(buffer[position++], '-');
441 uintptr_t end = ReadLong(buffer, &position, 16);
442 CHECK_EQ(buffer[position++], ' ');
443 CHECK(buffer[position] == '-' || buffer[position] == 'r');
444 bool read_permission = (buffer[position++] == 'r');
445 CHECK(buffer[position] == '-' || buffer[position] == 'w');
446 bool write_permission = (buffer[position++] == 'w');
447 CHECK(buffer[position] == '-' || buffer[position] == 'x');
448 bool execute_permission = (buffer[position++] == 'x');
449 CHECK(buffer[position] == '-' || buffer[position] == 'p');
450 bool private_mapping = (buffer[position++] == 'p');
451 CHECK_EQ(buffer[position++], ' ');
452 uintptr_t offset = ReadLong(buffer, &position, 16);
453 USE(offset);
454 CHECK_EQ(buffer[position++], ' ');
455 uintptr_t major = ReadLong(buffer, &position, 16);
456 USE(major);
457 CHECK_EQ(buffer[position++], ':');
458 uintptr_t minor = ReadLong(buffer, &position, 16);
459 USE(minor);
460 CHECK_EQ(buffer[position++], ' ');
461 uintptr_t inode = ReadLong(buffer, &position, 10);
462 while (position < length && buffer[position] != '\n') position++;
463 if ((read_permission || write_permission || execute_permission) &&
464 private_mapping && inode == 0) {
465 memory_use += (end - start);
466 }
467
468 line_start = position;
469 }
470 close(fd);
471 return memory_use;
472 }
473
474
ShortLivingIsolate()475 intptr_t ShortLivingIsolate() {
476 v8::Isolate* isolate = v8::Isolate::New();
477 { v8::Isolate::Scope isolate_scope(isolate);
478 v8::Locker lock(isolate);
479 v8::HandleScope handle_scope(isolate);
480 v8::Local<v8::Context> context = v8::Context::New(isolate);
481 CHECK(!context.IsEmpty());
482 }
483 isolate->Dispose();
484 return MemoryInUse();
485 }
486
487
TEST(RegressJoinThreadsOnIsolateDeinit)488 TEST(RegressJoinThreadsOnIsolateDeinit) {
489 intptr_t size_limit = ShortLivingIsolate() * 2;
490 for (int i = 0; i < 10; i++) {
491 CHECK_GT(size_limit, ShortLivingIsolate());
492 }
493 }
494
495 #endif // __linux__ and !USE_SIMULATOR
496