blob: c7d65310a45c04a1198f91b00f26c333dc0abeef [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Ben Murdoch3ef787d2012-04-12 10:51:47 +010030#ifdef __linux__
Ben Murdoch3ef787d2012-04-12 10:51:47 +010031#include <errno.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#include <fcntl.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <unistd.h>
Ben Murdoch3ef787d2012-04-12 10:51:47 +010036#endif
37
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038#include <utility>
Steve Blocka7e24c12009-10-30 11:49:00 +000039
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040#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"
Steve Blocka7e24c12009-10-30 11:49:00 +000046
47using namespace v8::internal;
48
Steve Blocka7e24c12009-10-30 11:49:00 +000049
Ben Murdoch3ef787d2012-04-12 10:51:47 +010050TEST(MarkingDeque) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 CcTest::InitializeVM();
Steve Blocka7e24c12009-10-30 11:49:00 +000052 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;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010056 MarkingDeque s;
Steve Blocka7e24c12009-10-30 11:49:00 +000057 s.Initialize(low, high);
58
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 Address original_address = reinterpret_cast<Address>(&s);
60 Address current_address = original_address;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010061 while (!s.IsFull()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 s.PushBlack(HeapObject::FromAddress(current_address));
63 current_address += kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000064 }
65
Ben Murdoch3ef787d2012-04-12 10:51:47 +010066 while (!s.IsEmpty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000067 Address value = s.Pop()->address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 current_address -= kPointerSize;
69 CHECK_EQ(current_address, value);
Steve Blocka7e24c12009-10-30 11:49:00 +000070 }
71
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 CHECK_EQ(original_address, current_address);
Steve Blocka7e24c12009-10-30 11:49:00 +000073 DeleteArray(mem);
74}
75
76
77TEST(Promotion) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 CcTest::InitializeVM();
79 TestHeap* heap = CcTest::test_heap();
80 heap->ConfigureHeap(1, 1, 1, 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +010081
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 v8::HandleScope sc(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +000083
84 // Allocate a fixed array in the new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 int array_length =
86 (Page::kMaxRegularHeapObjectSize - FixedArray::kHeaderSize) /
87 (4 * kPointerSize);
88 Object* obj = heap->AllocateFixedArray(array_length).ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +000089 Handle<FixedArray> array(FixedArray::cast(obj));
90
91 // Array should be in the new space.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 CHECK(heap->InSpace(*array, NEW_SPACE));
Steve Blocka7e24c12009-10-30 11:49:00 +000093
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 // Call mark compact GC, so array becomes an old object.
95 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
96 heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
Steve Blocka7e24c12009-10-30 11:49:00 +000097
98 // Array now sits in the old space
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 CHECK(heap->InSpace(*array, OLD_POINTER_SPACE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000100}
101
102
103TEST(NoPromotion) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 CcTest::InitializeVM();
105 TestHeap* heap = CcTest::test_heap();
106 heap->ConfigureHeap(1, 1, 1, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000107
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 v8::HandleScope sc(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 // 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();
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 Handle<FixedArray> array(FixedArray::cast(obj));
116
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 // Array should be in the new space.
118 CHECK(heap->InSpace(*array, NEW_SPACE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000119
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 // Simulate a full old space to make promotion fail.
121 SimulateFullSpace(heap->old_pointer_space());
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
123 // Call mark compact GC, and it should pass.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 heap->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000125}
126
127
128TEST(MarkCompactCollector) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 FLAG_incremental_marking = false;
130 CcTest::InitializeVM();
131 Isolate* isolate = CcTest::i_isolate();
132 TestHeap* heap = CcTest::test_heap();
133 Factory* factory = isolate->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000134
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 v8::HandleScope sc(CcTest::isolate());
136 Handle<GlobalObject> global(isolate->context()->global_object());
137
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 // call mark-compact when heap is empty
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 heap->CollectGarbage(OLD_POINTER_SPACE, "trigger 1");
Steve Blocka7e24c12009-10-30 11:49:00 +0000140
141 // keep allocating garbage in new space until it fails
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 const int arraysize = 100;
143 AllocationResult allocation;
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 do {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 allocation = heap->AllocateFixedArray(arraysize);
146 } while (!allocation.IsRetry());
147 heap->CollectGarbage(NEW_SPACE, "trigger 2");
148 heap->AllocateFixedArray(arraysize).ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000149
150 // keep allocating maps until it fails
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 do {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 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();
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 { 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();
Steve Blocka7e24c12009-10-30 11:49:00 +0000162
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 factory->NewJSObject(function);
164 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 heap->CollectGarbage(OLD_POINTER_SPACE, "trigger 4");
Steve Blocka7e24c12009-10-30 11:49:00 +0000167
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 { 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 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 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000185
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 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 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000200}
201
202
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100203// TODO(1600): compaction of map space is temporary removed from GC.
204#if 0
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205static Handle<Map> CreateMap(Isolate* isolate) {
206 return isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
Leon Clarked91b9f72010-01-27 17:25:45 +0000207}
208
209
210TEST(MapCompact) {
211 FLAG_max_map_space_pages = 16;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 CcTest::InitializeVM();
213 Isolate* isolate = CcTest::i_isolate();
214 Factory* factory = isolate->factory();
Leon Clarked91b9f72010-01-27 17:25:45 +0000215
216 {
217 v8::HandleScope sc;
218 // keep allocating maps while pointers are still encodable and thus
219 // mark compact is permitted.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000220 Handle<JSObject> root = factory->NewJSObjectFromMap(CreateMap());
Leon Clarked91b9f72010-01-27 17:25:45 +0000221 do {
222 Handle<Map> map = CreateMap();
223 map->set_prototype(*root);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224 root = factory->NewJSObjectFromMap(map);
225 } while (CcTest::heap()->map_space()->MapPointersEncodable());
Leon Clarked91b9f72010-01-27 17:25:45 +0000226 }
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.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000231 CcTest::heap()->CollectAllGarbage(Heap::kForceCompactionMask);
Leon Clarked91b9f72010-01-27 17:25:45 +0000232 // And now map pointers should be encodable again.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 CHECK(CcTest::heap()->map_space()->MapPointersEncodable());
Leon Clarked91b9f72010-01-27 17:25:45 +0000234}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100235#endif
Leon Clarked91b9f72010-01-27 17:25:45 +0000236
Steve Blocka7e24c12009-10-30 11:49:00 +0000237
238static int NumberOfWeakCalls = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239static 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 NumberOfWeakCalls++;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 p->first->Reset();
Steve Blocka7e24c12009-10-30 11:49:00 +0000247}
248
Steve Blocka7e24c12009-10-30 11:49:00 +0000249
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250TEST(ObjectGroups) {
251 FLAG_incremental_marking = false;
252 CcTest::InitializeVM();
253 GlobalHandles* global_handles = CcTest::i_isolate()->global_handles();
254 TestHeap* heap = CcTest::test_heap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 NumberOfWeakCalls = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000256 v8::HandleScope handle_scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000257
258 Handle<Object> g1s1 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 Handle<Object> g1s2 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
Steve Block44f0eee2011-05-26 01:26:41 +0100262 Handle<Object> g1c1 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000263 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000276
277 Handle<Object> g2s1 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000278 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 Handle<Object> g2s2 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 global_handles->Create(heap->AllocateFixedArray(1).ToObjectChecked());
Steve Block44f0eee2011-05-26 01:26:41 +0100281 Handle<Object> g2c1 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000295
Steve Block44f0eee2011-05-26 01:26:41 +0100296 Handle<Object> root = global_handles->Create(*g1s1); // make a root.
Steve Blocka7e24c12009-10-30 11:49:00 +0000297
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() };
Steve Block44f0eee2011-05-26 01:26:41 +0100305 global_handles->AddObjectGroup(g1_objects, 2, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 global_handles->SetReference(Handle<HeapObject>::cast(g1s1).location(),
307 g1c1.location());
Steve Block44f0eee2011-05-26 01:26:41 +0100308 global_handles->AddObjectGroup(g2_objects, 2, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 global_handles->SetReference(Handle<HeapObject>::cast(g2s1).location(),
310 g2c1.location());
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 }
312 // Do a full GC
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313 heap->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000314
315 // All object should be alive.
316 CHECK_EQ(0, NumberOfWeakCalls);
317
318 // Weaken the root.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 std::pair<Handle<Object>*, int> root_and_id(&root, 1234);
320 GlobalHandles::MakeWeak(root.location(),
321 reinterpret_cast<void*>(&root_and_id),
322 &WeakPointerCallback);
Steve Block44f0eee2011-05-26 01:26:41 +0100323 // 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());
Steve Blocka7e24c12009-10-30 11:49:00 +0000327
328 // Groups are deleted, rebuild groups.
329 {
330 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
331 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
Steve Block44f0eee2011-05-26 01:26:41 +0100332 global_handles->AddObjectGroup(g1_objects, 2, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333 global_handles->SetReference(Handle<HeapObject>::cast(g1s1).location(),
334 g1c1.location());
Steve Block44f0eee2011-05-26 01:26:41 +0100335 global_handles->AddObjectGroup(g2_objects, 2, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336 global_handles->SetReference(Handle<HeapObject>::cast(g2s1).location(),
337 g2c1.location());
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 }
339
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340 heap->CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000341
342 // All objects should be gone. 5 global handles in total.
343 CHECK_EQ(5, NumberOfWeakCalls);
Steve Block44f0eee2011-05-26 01:26:41 +0100344
345 // And now make children weak again and collect them.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 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);
Steve Block44f0eee2011-05-26 01:26:41 +0100352
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 heap->CollectGarbage(OLD_POINTER_SPACE);
Steve Block44f0eee2011-05-26 01:26:41 +0100354 CHECK_EQ(7, NumberOfWeakCalls);
Steve Blocka7e24c12009-10-30 11:49:00 +0000355}
Ben Murdoch8b112d22011-06-08 16:22:53 +0100356
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() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365 DCHECK(!has_been_disposed_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100366 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) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 CcTest::InitializeVM();
384 GlobalHandles* global_handles = CcTest::i_isolate()->global_handles();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100385
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 v8::HandleScope handle_scope(CcTest::isolate());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100387
388 TestRetainedObjectInfo info;
389 global_handles->AddObjectGroup(NULL, 0, &info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390 DCHECK(info.has_been_disposed());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100391}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100392
393
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394#if defined(__has_feature)
395#if __has_feature(address_sanitizer)
396#define V8_WITH_ASAN 1
397#endif
398#endif
399
400
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100401// 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000403// debugging purposes. Testing with ASAN doesn't make sense, either.
404#if defined(__linux__) && !defined(USE_SIMULATOR) && !defined(V8_WITH_ASAN)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100405
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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417// 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.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100421static 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475intptr_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
488TEST(RegressJoinThreadsOnIsolateDeinit) {
489 intptr_t size_limit = ShortLivingIsolate() * 2;
490 for (int i = 0; i < 10; i++) {
491 CHECK_GT(size_limit, ShortLivingIsolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100492 }
493}
494
495#endif // __linux__ and !USE_SIMULATOR