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
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.
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.
31 #include <sys/types.h>
42 #include "src/full-codegen.h"
43 #include "src/global-handles.h"
44 #include "src/snapshot.h"
45 #include "test/cctest/cctest.h"
47 using namespace v8::internal;
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;
57 s.Initialize(low, high);
59 Address original_address = reinterpret_cast<Address>(&s);
60 Address current_address = original_address;
62 s.PushBlack(HeapObject::FromAddress(current_address));
63 current_address += kPointerSize;
66 while (!s.IsEmpty()) {
67 Address value = s.Pop()->address();
68 current_address -= kPointerSize;
69 CHECK_EQ(current_address, value);
72 CHECK_EQ(original_address, current_address);
78 CcTest::InitializeVM();
79 TestHeap* heap = CcTest::test_heap();
80 heap->ConfigureHeap(1, 1, 1, 0);
82 v8::HandleScope sc(CcTest::isolate());
84 // Allocate a fixed array in the new space.
86 (Page::kMaxRegularHeapObjectSize - FixedArray::kHeaderSize) /
88 Object* obj = heap->AllocateFixedArray(array_length).ToObjectChecked();
89 Handle<FixedArray> array(FixedArray::cast(obj));
91 // Array should be in the new space.
92 CHECK(heap->InSpace(*array, NEW_SPACE));
94 // Call mark compact GC, so array becomes an old object.
95 heap->CollectGarbage(OLD_POINTER_SPACE);
97 // Array now sits in the old space
98 CHECK(heap->InSpace(*array, OLD_POINTER_SPACE));
103 CcTest::InitializeVM();
104 TestHeap* heap = CcTest::test_heap();
105 heap->ConfigureHeap(1, 1, 1, 0);
107 v8::HandleScope sc(CcTest::isolate());
109 // Allocate a big fixed array in the new space.
111 (Page::kMaxRegularHeapObjectSize - FixedArray::kHeaderSize) /
113 Object* obj = heap->AllocateFixedArray(array_length).ToObjectChecked();
114 Handle<FixedArray> array(FixedArray::cast(obj));
116 // Array should be in the new space.
117 CHECK(heap->InSpace(*array, NEW_SPACE));
119 // Simulate a full old space to make promotion fail.
120 SimulateFullSpace(heap->old_pointer_space());
122 // Call mark compact GC, and it should pass.
123 heap->CollectGarbage(OLD_POINTER_SPACE);
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();
134 v8::HandleScope sc(CcTest::isolate());
135 Handle<GlobalObject> global(isolate->context()->global_object());
137 // call mark-compact when heap is empty
138 heap->CollectGarbage(OLD_POINTER_SPACE, "trigger 1");
140 // keep allocating garbage in new space until it fails
141 const int ARRAY_SIZE = 100;
142 AllocationResult allocation;
144 allocation = heap->AllocateFixedArray(ARRAY_SIZE);
145 } while (!allocation.IsRetry());
146 heap->CollectGarbage(NEW_SPACE, "trigger 2");
147 heap->AllocateFixedArray(ARRAY_SIZE).ToObjectChecked();
149 // keep allocating maps until it fails
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();
156 { HandleScope scope(isolate);
157 // allocate a garbage
158 Handle<String> func_name = factory->InternalizeUtf8String("theFunction");
159 Handle<JSFunction> function = factory->NewFunction(func_name);
160 JSReceiver::SetProperty(global, func_name, function, NONE, SLOPPY).Check();
162 factory->NewJSObject(function);
165 heap->CollectGarbage(OLD_POINTER_SPACE, "trigger 4");
167 { HandleScope scope(isolate);
168 Handle<String> func_name = factory->InternalizeUtf8String("theFunction");
169 CHECK(JSReceiver::HasOwnProperty(global, func_name));
170 Handle<Object> func_value =
171 Object::GetProperty(global, func_name).ToHandleChecked();
172 CHECK(func_value->IsJSFunction());
173 Handle<JSFunction> function = Handle<JSFunction>::cast(func_value);
174 Handle<JSObject> obj = factory->NewJSObject(function);
176 Handle<String> obj_name = factory->InternalizeUtf8String("theObject");
177 JSReceiver::SetProperty(global, obj_name, obj, NONE, SLOPPY).Check();
178 Handle<String> prop_name = factory->InternalizeUtf8String("theSlot");
179 Handle<Smi> twenty_three(Smi::FromInt(23), isolate);
180 JSReceiver::SetProperty(obj, prop_name, twenty_three, NONE, SLOPPY).Check();
183 heap->CollectGarbage(OLD_POINTER_SPACE, "trigger 5");
185 { HandleScope scope(isolate);
186 Handle<String> obj_name = factory->InternalizeUtf8String("theObject");
187 CHECK(JSReceiver::HasOwnProperty(global, obj_name));
188 Handle<Object> object =
189 Object::GetProperty(global, obj_name).ToHandleChecked();
190 CHECK(object->IsJSObject());
191 Handle<String> prop_name = factory->InternalizeUtf8String("theSlot");
192 CHECK_EQ(*Object::GetProperty(object, prop_name).ToHandleChecked(),
198 // TODO(1600): compaction of map space is temporary removed from GC.
200 static Handle<Map> CreateMap(Isolate* isolate) {
201 return isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
206 FLAG_max_map_space_pages = 16;
207 CcTest::InitializeVM();
208 Isolate* isolate = CcTest::i_isolate();
209 Factory* factory = isolate->factory();
213 // keep allocating maps while pointers are still encodable and thus
214 // mark compact is permitted.
215 Handle<JSObject> root = factory->NewJSObjectFromMap(CreateMap());
217 Handle<Map> map = CreateMap();
218 map->set_prototype(*root);
219 root = factory->NewJSObjectFromMap(map);
220 } while (CcTest::heap()->map_space()->MapPointersEncodable());
222 // Now, as we don't have any handles to just allocated maps, we should
223 // be able to trigger map compaction.
224 // To give an additional chance to fail, try to force compaction which
225 // should be impossible right now.
226 CcTest::heap()->CollectAllGarbage(Heap::kForceCompactionMask);
227 // And now map pointers should be encodable again.
228 CHECK(CcTest::heap()->map_space()->MapPointersEncodable());
233 static int NumberOfWeakCalls = 0;
234 static void WeakPointerCallback(
235 const v8::WeakCallbackData<v8::Value, void>& data) {
236 std::pair<v8::Persistent<v8::Value>*, int>* p =
237 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
238 data.GetParameter());
239 ASSERT_EQ(1234, p->second);
246 FLAG_incremental_marking = false;
247 CcTest::InitializeVM();
248 GlobalHandles* global_handles = CcTest::i_isolate()->global_handles();
249 TestHeap* heap = CcTest::test_heap();
250 NumberOfWeakCalls = 0;
251 v8::HandleScope handle_scope(CcTest::isolate());
253 Handle<Object> g1s1 =
254 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
255 Handle<Object> g1s2 =
256 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
257 Handle<Object> g1c1 =
258 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
259 std::pair<Handle<Object>*, int> g1s1_and_id(&g1s1, 1234);
260 GlobalHandles::MakeWeak(g1s1.location(),
261 reinterpret_cast<void*>(&g1s1_and_id),
262 &WeakPointerCallback);
263 std::pair<Handle<Object>*, int> g1s2_and_id(&g1s2, 1234);
264 GlobalHandles::MakeWeak(g1s2.location(),
265 reinterpret_cast<void*>(&g1s2_and_id),
266 &WeakPointerCallback);
267 std::pair<Handle<Object>*, int> g1c1_and_id(&g1c1, 1234);
268 GlobalHandles::MakeWeak(g1c1.location(),
269 reinterpret_cast<void*>(&g1c1_and_id),
270 &WeakPointerCallback);
272 Handle<Object> g2s1 =
273 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
274 Handle<Object> g2s2 =
275 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
276 Handle<Object> g2c1 =
277 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
278 std::pair<Handle<Object>*, int> g2s1_and_id(&g2s1, 1234);
279 GlobalHandles::MakeWeak(g2s1.location(),
280 reinterpret_cast<void*>(&g2s1_and_id),
281 &WeakPointerCallback);
282 std::pair<Handle<Object>*, int> g2s2_and_id(&g2s2, 1234);
283 GlobalHandles::MakeWeak(g2s2.location(),
284 reinterpret_cast<void*>(&g2s2_and_id),
285 &WeakPointerCallback);
286 std::pair<Handle<Object>*, int> g2c1_and_id(&g2c1, 1234);
287 GlobalHandles::MakeWeak(g2c1.location(),
288 reinterpret_cast<void*>(&g2c1_and_id),
289 &WeakPointerCallback);
291 Handle<Object> root = global_handles->Create(*g1s1); // make a root.
293 // Connect group 1 and 2, make a cycle.
294 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2);
295 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1);
298 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
299 Object** g1_children[] = { g1c1.location() };
300 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
301 Object** g2_children[] = { g2c1.location() };
302 global_handles->AddObjectGroup(g1_objects, 2, NULL);
303 global_handles->AddImplicitReferences(
304 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1);
305 global_handles->AddObjectGroup(g2_objects, 2, NULL);
306 global_handles->AddImplicitReferences(
307 Handle<HeapObject>::cast(g2s1).location(), g2_children, 1);
310 heap->CollectGarbage(OLD_POINTER_SPACE);
312 // All object should be alive.
313 CHECK_EQ(0, NumberOfWeakCalls);
316 std::pair<Handle<Object>*, int> root_and_id(&root, 1234);
317 GlobalHandles::MakeWeak(root.location(),
318 reinterpret_cast<void*>(&root_and_id),
319 &WeakPointerCallback);
320 // But make children strong roots---all the objects (except for children)
321 // should be collectable now.
322 global_handles->ClearWeakness(g1c1.location());
323 global_handles->ClearWeakness(g2c1.location());
325 // Groups are deleted, rebuild groups.
327 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
328 Object** g1_children[] = { g1c1.location() };
329 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
330 Object** g2_children[] = { g2c1.location() };
331 global_handles->AddObjectGroup(g1_objects, 2, NULL);
332 global_handles->AddImplicitReferences(
333 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1);
334 global_handles->AddObjectGroup(g2_objects, 2, NULL);
335 global_handles->AddImplicitReferences(
336 Handle<HeapObject>::cast(g2s1).location(), g2_children, 1);
339 heap->CollectGarbage(OLD_POINTER_SPACE);
341 // All objects should be gone. 5 global handles in total.
342 CHECK_EQ(5, NumberOfWeakCalls);
344 // And now make children weak again and collect them.
345 GlobalHandles::MakeWeak(g1c1.location(),
346 reinterpret_cast<void*>(&g1c1_and_id),
347 &WeakPointerCallback);
348 GlobalHandles::MakeWeak(g2c1.location(),
349 reinterpret_cast<void*>(&g2c1_and_id),
350 &WeakPointerCallback);
352 heap->CollectGarbage(OLD_POINTER_SPACE);
353 CHECK_EQ(7, NumberOfWeakCalls);
357 class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
359 TestRetainedObjectInfo() : has_been_disposed_(false) {}
361 bool has_been_disposed() { return has_been_disposed_; }
363 virtual void Dispose() {
364 ASSERT(!has_been_disposed_);
365 has_been_disposed_ = true;
368 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) {
369 return other == this;
372 virtual intptr_t GetHash() { return 0; }
374 virtual const char* GetLabel() { return "whatever"; }
377 bool has_been_disposed_;
381 TEST(EmptyObjectGroups) {
382 CcTest::InitializeVM();
383 GlobalHandles* global_handles = CcTest::i_isolate()->global_handles();
385 v8::HandleScope handle_scope(CcTest::isolate());
387 Handle<Object> object = global_handles->Create(
388 CcTest::test_heap()->AllocateFixedArray(1).ToObjectChecked());
390 TestRetainedObjectInfo info;
391 global_handles->AddObjectGroup(NULL, 0, &info);
392 ASSERT(info.has_been_disposed());
394 global_handles->AddImplicitReferences(
395 Handle<HeapObject>::cast(object).location(), NULL, 0);
399 #if defined(__has_feature)
400 #if __has_feature(address_sanitizer)
401 #define V8_WITH_ASAN 1
406 // Here is a memory use test that uses /proc, and is therefore Linux-only. We
407 // do not care how much memory the simulator uses, since it is only there for
408 // debugging purposes. Testing with ASAN doesn't make sense, either.
409 #if defined(__linux__) && !defined(USE_SIMULATOR) && !defined(V8_WITH_ASAN)
412 static uintptr_t ReadLong(char* buffer, intptr_t* position, int base) {
413 char* end_address = buffer + *position;
414 uintptr_t result = strtoul(buffer + *position, &end_address, base);
415 CHECK(result != ULONG_MAX || errno != ERANGE);
416 CHECK(end_address > buffer + *position);
417 *position = end_address - buffer;
422 // The memory use computed this way is not entirely accurate and depends on
423 // the way malloc allocates memory. That's why the memory use may seem to
424 // increase even though the sum of the allocated object sizes decreases. It
425 // also means that the memory use depends on the kernel and stdlib.
426 static intptr_t MemoryInUse() {
427 intptr_t memory_use = 0;
429 int fd = open("/proc/self/maps", O_RDONLY);
430 if (fd < 0) return -1;
432 const int kBufSize = 10000;
433 char buffer[kBufSize];
434 int length = read(fd, buffer, kBufSize);
435 intptr_t line_start = 0;
436 CHECK_LT(length, kBufSize); // Make the buffer bigger.
437 CHECK_GT(length, 0); // We have to find some data in the file.
438 while (line_start < length) {
439 if (buffer[line_start] == '\n') {
443 intptr_t position = line_start;
444 uintptr_t start = ReadLong(buffer, &position, 16);
445 CHECK_EQ(buffer[position++], '-');
446 uintptr_t end = ReadLong(buffer, &position, 16);
447 CHECK_EQ(buffer[position++], ' ');
448 CHECK(buffer[position] == '-' || buffer[position] == 'r');
449 bool read_permission = (buffer[position++] == 'r');
450 CHECK(buffer[position] == '-' || buffer[position] == 'w');
451 bool write_permission = (buffer[position++] == 'w');
452 CHECK(buffer[position] == '-' || buffer[position] == 'x');
453 bool execute_permission = (buffer[position++] == 'x');
454 CHECK(buffer[position] == '-' || buffer[position] == 'p');
455 bool private_mapping = (buffer[position++] == 'p');
456 CHECK_EQ(buffer[position++], ' ');
457 uintptr_t offset = ReadLong(buffer, &position, 16);
459 CHECK_EQ(buffer[position++], ' ');
460 uintptr_t major = ReadLong(buffer, &position, 16);
462 CHECK_EQ(buffer[position++], ':');
463 uintptr_t minor = ReadLong(buffer, &position, 16);
465 CHECK_EQ(buffer[position++], ' ');
466 uintptr_t inode = ReadLong(buffer, &position, 10);
467 while (position < length && buffer[position] != '\n') position++;
468 if ((read_permission || write_permission || execute_permission) &&
469 private_mapping && inode == 0) {
470 memory_use += (end - start);
473 line_start = position;
480 intptr_t ShortLivingIsolate() {
481 v8::Isolate* isolate = v8::Isolate::New();
482 { v8::Isolate::Scope isolate_scope(isolate);
483 v8::Locker lock(isolate);
484 v8::HandleScope handle_scope(isolate);
485 v8::Local<v8::Context> context = v8::Context::New(isolate);
486 CHECK(!context.IsEmpty());
489 return MemoryInUse();
493 TEST(RegressJoinThreadsOnIsolateDeinit) {
494 intptr_t size_limit = ShortLivingIsolate() * 2;
495 for (int i = 0; i < 10; i++) {
496 CHECK_GT(size_limit, ShortLivingIsolate());
500 #endif // __linux__ and !USE_SIMULATOR