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