blob: cfcf149c615690fb9db98772d4f1cfd73d8b1605 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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/full-codegen.h"
43#include "src/global-handles.h"
44#include "test/cctest/cctest.h"
45#include "test/cctest/heap/heap-tester.h"
46#include "test/cctest/heap/utils-inl.h"
47
48
49using namespace v8::internal;
50using v8::Just;
51
52
53TEST(MarkingDeque) {
54 CcTest::InitializeVM();
55 int mem_size = 20 * kPointerSize;
56 byte* mem = NewArray<byte>(20*kPointerSize);
57 Address low = reinterpret_cast<Address>(mem);
58 Address high = low + mem_size;
59 MarkingDeque s;
60 s.Initialize(low, high);
61
62 Address original_address = reinterpret_cast<Address>(&s);
63 Address current_address = original_address;
64 while (!s.IsFull()) {
65 s.Push(HeapObject::FromAddress(current_address));
66 current_address += kPointerSize;
67 }
68
69 while (!s.IsEmpty()) {
70 Address value = s.Pop()->address();
71 current_address -= kPointerSize;
72 CHECK_EQ(current_address, value);
73 }
74
75 CHECK_EQ(original_address, current_address);
76 DeleteArray(mem);
77}
78
79
80HEAP_TEST(Promotion) {
81 CcTest::InitializeVM();
82 Heap* heap = CcTest::heap();
83 heap->ConfigureHeap(1, 1, 1, 0);
84
85 v8::HandleScope sc(CcTest::isolate());
86
87 // Allocate a fixed array in the new space.
88 int array_length =
89 (Page::kMaxRegularHeapObjectSize - FixedArray::kHeaderSize) /
90 (4 * kPointerSize);
91 Object* obj = heap->AllocateFixedArray(array_length).ToObjectChecked();
92 Handle<FixedArray> array(FixedArray::cast(obj));
93
94 // Array should be in the new space.
95 CHECK(heap->InSpace(*array, NEW_SPACE));
96
97 // Call mark compact GC, so array becomes an old object.
98 heap->CollectAllGarbage();
99 heap->CollectAllGarbage();
100
101 // Array now sits in the old space
102 CHECK(heap->InSpace(*array, OLD_SPACE));
103}
104
105
106HEAP_TEST(NoPromotion) {
107 CcTest::InitializeVM();
108 Heap* heap = CcTest::heap();
109 heap->ConfigureHeap(1, 1, 1, 0);
110
111 v8::HandleScope sc(CcTest::isolate());
112
113 // Allocate a big fixed array in the new space.
114 int array_length =
115 (Page::kMaxRegularHeapObjectSize - FixedArray::kHeaderSize) /
116 (2 * kPointerSize);
117 Object* obj = heap->AllocateFixedArray(array_length).ToObjectChecked();
118 Handle<FixedArray> array(FixedArray::cast(obj));
119
120 // Array should be in the new space.
121 CHECK(heap->InSpace(*array, NEW_SPACE));
122
123 // Simulate a full old space to make promotion fail.
124 SimulateFullSpace(heap->old_space());
125
126 // Call mark compact GC, and it should pass.
127 heap->CollectGarbage(OLD_SPACE);
128}
129
130
131HEAP_TEST(MarkCompactCollector) {
132 FLAG_incremental_marking = false;
133 FLAG_retain_maps_for_n_gc = 0;
134 CcTest::InitializeVM();
135 Isolate* isolate = CcTest::i_isolate();
136 Heap* heap = CcTest::heap();
137 Factory* factory = isolate->factory();
138
139 v8::HandleScope sc(CcTest::isolate());
140 Handle<JSGlobalObject> global(isolate->context()->global_object());
141
142 // call mark-compact when heap is empty
143 heap->CollectGarbage(OLD_SPACE, "trigger 1");
144
145 // keep allocating garbage in new space until it fails
146 const int arraysize = 100;
147 AllocationResult allocation;
148 do {
149 allocation = heap->AllocateFixedArray(arraysize);
150 } while (!allocation.IsRetry());
151 heap->CollectGarbage(NEW_SPACE, "trigger 2");
152 heap->AllocateFixedArray(arraysize).ToObjectChecked();
153
154 // keep allocating maps until it fails
155 do {
156 allocation = heap->AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
157 } while (!allocation.IsRetry());
158 heap->CollectGarbage(MAP_SPACE, "trigger 3");
159 heap->AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize).ToObjectChecked();
160
161 { HandleScope scope(isolate);
162 // allocate a garbage
163 Handle<String> func_name = factory->InternalizeUtf8String("theFunction");
164 Handle<JSFunction> function = factory->NewFunction(func_name);
165 JSReceiver::SetProperty(global, func_name, function, SLOPPY).Check();
166
167 factory->NewJSObject(function);
168 }
169
170 heap->CollectGarbage(OLD_SPACE, "trigger 4");
171
172 { HandleScope scope(isolate);
173 Handle<String> func_name = factory->InternalizeUtf8String("theFunction");
174 CHECK(Just(true) == JSReceiver::HasOwnProperty(global, func_name));
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_SPACE, "trigger 5");
189
190 { HandleScope scope(isolate);
191 Handle<String> obj_name = factory->InternalizeUtf8String("theObject");
192 CHECK(Just(true) == JSReceiver::HasOwnProperty(global, obj_name));
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
205static Handle<Map> CreateMap(Isolate* isolate) {
206 return isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
207}
208
209
210TEST(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
238static int NumberOfWeakCalls = 0;
239static 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 CHECK_EQ(1234, p->second);
245 NumberOfWeakCalls++;
246 p->first->Reset();
247}
248
249
250HEAP_TEST(ObjectGroups) {
251 FLAG_incremental_marking = false;
252 CcTest::InitializeVM();
253 GlobalHandles* global_handles = CcTest::i_isolate()->global_handles();
254 Heap* heap = CcTest::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_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_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_SPACE);
354 CHECK_EQ(7, NumberOfWeakCalls);
355}
356
357
358class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
359 public:
360 TestRetainedObjectInfo() : has_been_disposed_(false) {}
361
362 bool has_been_disposed() { return has_been_disposed_; }
363
364 virtual void Dispose() {
365 CHECK(!has_been_disposed_);
366 has_been_disposed_ = true;
367 }
368
369 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) {
370 return other == this;
371 }
372
373 virtual intptr_t GetHash() { return 0; }
374
375 virtual const char* GetLabel() { return "whatever"; }
376
377 private:
378 bool has_been_disposed_;
379};
380
381
382TEST(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 CHECK(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
407static 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.
421static 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 ssize_t 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] == 's' || 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
475intptr_t ShortLivingIsolate() {
476 v8::Isolate::CreateParams create_params;
477 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
478 v8::Isolate* isolate = v8::Isolate::New(create_params);
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
490TEST(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