blob: 86f105f2c971234f48641e82c00b5204a5ac2021 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +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
30#include "v8.h"
31
32#include "global-handles.h"
33#include "snapshot.h"
34#include "top.h"
35#include "cctest.h"
36
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000037using namespace v8::internal;
38
39static v8::Persistent<v8::Context> env;
40
41static void InitializeVM() {
42 if (env.IsEmpty()) env = v8::Context::New();
43 v8::HandleScope scope;
44 env->Enter();
45}
46
47
48TEST(MarkingStack) {
49 int mem_size = 20 * kPointerSize;
50 byte* mem = NewArray<byte>(20*kPointerSize);
51 Address low = reinterpret_cast<Address>(mem);
52 Address high = low + mem_size;
53 MarkingStack s;
54 s.Initialize(low, high);
55
56 Address address = NULL;
57 while (!s.is_full()) {
58 s.Push(HeapObject::FromAddress(address));
59 address += kPointerSize;
60 }
61
62 while (!s.is_empty()) {
63 Address value = s.Pop()->address();
64 address -= kPointerSize;
65 CHECK_EQ(address, value);
66 }
67
68 CHECK_EQ(NULL, address);
69 DeleteArray(mem);
70}
71
72
73TEST(Promotion) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +000074 // This test requires compaction. If compaction is turned off, we
75 // skip the entire test.
76 if (FLAG_never_compact) return;
77
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000078 // Ensure that we get a compacting collection so that objects are promoted
79 // from new space.
80 FLAG_gc_global = true;
81 FLAG_always_compact = true;
ager@chromium.org01fe7df2010-11-10 11:59:11 +000082 Heap::ConfigureHeap(2*256*KB, 4*MB, 4*MB);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000083
84 InitializeVM();
85
86 v8::HandleScope sc;
87
88 // Allocate a fixed array in the new space.
kasperl@chromium.orge959c182009-07-27 08:59:04 +000089 int array_size =
90 (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) /
ager@chromium.org5aa501c2009-06-23 07:57:28 +000091 (kPointerSize * 4);
lrn@chromium.org303ada72010-10-27 09:33:13 +000092 Object* obj = Heap::AllocateFixedArray(array_size)->ToObjectChecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000093
94 Handle<FixedArray> array(FixedArray::cast(obj));
95
96 // Array should be in the new space.
97 CHECK(Heap::InSpace(*array, NEW_SPACE));
98
99 // Call the m-c collector, so array becomes an old object.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000100 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000101
102 // Array now sits in the old space
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000103 CHECK(Heap::InSpace(*array, OLD_POINTER_SPACE));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000104}
105
106
107TEST(NoPromotion) {
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000108 Heap::ConfigureHeap(2*256*KB, 4*MB, 4*MB);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000109
110 // Test the situation that some objects in new space are promoted to
111 // the old space
112 InitializeVM();
113
114 v8::HandleScope sc;
115
116 // Do a mark compact GC to shrink the heap.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000117 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000118
119 // Allocate a big Fixed array in the new space.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000120 int size = (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) /
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000121 kPointerSize;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000122 Object* obj = Heap::AllocateFixedArray(size)->ToObjectChecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000123
124 Handle<FixedArray> array(FixedArray::cast(obj));
125
126 // Array still stays in the new space.
127 CHECK(Heap::InSpace(*array, NEW_SPACE));
128
129 // Allocate objects in the old space until out of memory.
130 FixedArray* host = *array;
131 while (true) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000132 Object* obj;
133 { MaybeObject* maybe_obj = Heap::AllocateFixedArray(100, TENURED);
134 if (!maybe_obj->ToObject(&obj)) break;
135 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000136
137 host->set(0, obj);
138 host = FixedArray::cast(obj);
139 }
140
141 // Call mark compact GC, and it should pass.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000142 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000143
144 // array should not be promoted because the old space is full.
145 CHECK(Heap::InSpace(*array, NEW_SPACE));
146}
147
148
149TEST(MarkCompactCollector) {
150 InitializeVM();
151
152 v8::HandleScope sc;
153 // call mark-compact when heap is empty
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000154 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000155
156 // keep allocating garbage in new space until it fails
157 const int ARRAY_SIZE = 100;
158 Object* array;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000159 MaybeObject* maybe_array;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000160 do {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000161 maybe_array = Heap::AllocateFixedArray(ARRAY_SIZE);
162 } while (maybe_array->ToObject(&array));
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000163 Heap::CollectGarbage(NEW_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000164
lrn@chromium.org303ada72010-10-27 09:33:13 +0000165 array = Heap::AllocateFixedArray(ARRAY_SIZE)->ToObjectChecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000166
167 // keep allocating maps until it fails
168 Object* mapp;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000169 MaybeObject* maybe_mapp;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000170 do {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000171 maybe_mapp = Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
172 } while (maybe_mapp->ToObject(&mapp));
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000173 Heap::CollectGarbage(MAP_SPACE);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000174 mapp = Heap::AllocateMap(JS_OBJECT_TYPE,
175 JSObject::kHeaderSize)->ToObjectChecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000176
177 // allocate a garbage
lrn@chromium.org303ada72010-10-27 09:33:13 +0000178 String* func_name =
179 String::cast(Heap::LookupAsciiSymbol("theFunction")->ToObjectChecked());
180 SharedFunctionInfo* function_share = SharedFunctionInfo::cast(
181 Heap::AllocateSharedFunctionInfo(func_name)->ToObjectChecked());
182 JSFunction* function = JSFunction::cast(
183 Heap::AllocateFunction(*Top::function_map(),
184 function_share,
185 Heap::undefined_value())->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000186 Map* initial_map =
lrn@chromium.org303ada72010-10-27 09:33:13 +0000187 Map::cast(Heap::AllocateMap(JS_OBJECT_TYPE,
188 JSObject::kHeaderSize)->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000189 function->set_initial_map(initial_map);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000190 Top::context()->global()->SetProperty(func_name,
191 function,
192 NONE)->ToObjectChecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000193
lrn@chromium.org303ada72010-10-27 09:33:13 +0000194 JSObject* obj =
195 JSObject::cast(Heap::AllocateJSObject(function)->ToObjectChecked());
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000196 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000197
lrn@chromium.org303ada72010-10-27 09:33:13 +0000198 func_name =
199 String::cast(Heap::LookupAsciiSymbol("theFunction")->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000200 CHECK(Top::context()->global()->HasLocalProperty(func_name));
lrn@chromium.org303ada72010-10-27 09:33:13 +0000201 Object* func_value =
202 Top::context()->global()->GetProperty(func_name)->ToObjectChecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000203 CHECK(func_value->IsJSFunction());
204 function = JSFunction::cast(func_value);
205
lrn@chromium.org303ada72010-10-27 09:33:13 +0000206 obj = JSObject::cast(Heap::AllocateJSObject(function)->ToObjectChecked());
207 String* obj_name =
208 String::cast(Heap::LookupAsciiSymbol("theObject")->ToObjectChecked());
209 Top::context()->global()->SetProperty(obj_name,
210 obj,
211 NONE)->ToObjectChecked();
212 String* prop_name =
213 String::cast(Heap::LookupAsciiSymbol("theSlot")->ToObjectChecked());
214 obj->SetProperty(prop_name, Smi::FromInt(23), NONE)->ToObjectChecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000215
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000216 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000217
lrn@chromium.org303ada72010-10-27 09:33:13 +0000218 obj_name =
219 String::cast(Heap::LookupAsciiSymbol("theObject")->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000220 CHECK(Top::context()->global()->HasLocalProperty(obj_name));
lrn@chromium.org303ada72010-10-27 09:33:13 +0000221 CHECK(Top::context()->global()->
222 GetProperty(obj_name)->ToObjectChecked()->IsJSObject());
223 obj = JSObject::cast(
224 Top::context()->global()->GetProperty(obj_name)->ToObjectChecked());
225 prop_name =
226 String::cast(Heap::LookupAsciiSymbol("theSlot")->ToObjectChecked());
227 CHECK(obj->GetProperty(prop_name)->ToObjectChecked() == Smi::FromInt(23));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000228}
229
230
kasperl@chromium.orgeac059f2010-01-25 11:02:06 +0000231static Handle<Map> CreateMap() {
232 return Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
233}
234
235
236TEST(MapCompact) {
237 FLAG_max_map_space_pages = 16;
238 InitializeVM();
239
240 {
241 v8::HandleScope sc;
242 // keep allocating maps while pointers are still encodable and thus
243 // mark compact is permitted.
244 Handle<JSObject> root = Factory::NewJSObjectFromMap(CreateMap());
245 do {
246 Handle<Map> map = CreateMap();
247 map->set_prototype(*root);
248 root = Factory::NewJSObjectFromMap(map);
249 } while (Heap::map_space()->MapPointersEncodable());
250 }
251 // Now, as we don't have any handles to just allocated maps, we should
252 // be able to trigger map compaction.
253 // To give an additional chance to fail, try to force compaction which
254 // should be impossible right now.
255 Heap::CollectAllGarbage(true);
256 // And now map pointers should be encodable again.
257 CHECK(Heap::map_space()->MapPointersEncodable());
258}
259
260
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000261static int gc_starts = 0;
262static int gc_ends = 0;
263
264static void GCPrologueCallbackFunc() {
265 CHECK(gc_starts == gc_ends);
266 gc_starts++;
267}
268
269
270static void GCEpilogueCallbackFunc() {
271 CHECK(gc_starts == gc_ends + 1);
272 gc_ends++;
273}
274
275
276TEST(GCCallback) {
277 InitializeVM();
278
279 Heap::SetGlobalGCPrologueCallback(&GCPrologueCallbackFunc);
280 Heap::SetGlobalGCEpilogueCallback(&GCEpilogueCallbackFunc);
281
282 // Scavenge does not call GC callback functions.
283 Heap::PerformScavenge();
284
285 CHECK_EQ(0, gc_starts);
286 CHECK_EQ(gc_ends, gc_starts);
287
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000288 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000289 CHECK_EQ(1, gc_starts);
290 CHECK_EQ(gc_ends, gc_starts);
291}
292
293
294static int NumberOfWeakCalls = 0;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000295static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000296 NumberOfWeakCalls++;
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000297 handle.Dispose();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000298}
299
300TEST(ObjectGroups) {
301 InitializeVM();
302
303 NumberOfWeakCalls = 0;
304 v8::HandleScope handle_scope;
305
306 Handle<Object> g1s1 =
lrn@chromium.org303ada72010-10-27 09:33:13 +0000307 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000308 Handle<Object> g1s2 =
lrn@chromium.org303ada72010-10-27 09:33:13 +0000309 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000310 GlobalHandles::MakeWeak(g1s1.location(),
311 reinterpret_cast<void*>(1234),
312 &WeakPointerCallback);
313 GlobalHandles::MakeWeak(g1s2.location(),
314 reinterpret_cast<void*>(1234),
315 &WeakPointerCallback);
316
317 Handle<Object> g2s1 =
lrn@chromium.org303ada72010-10-27 09:33:13 +0000318 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000319 Handle<Object> g2s2 =
lrn@chromium.org303ada72010-10-27 09:33:13 +0000320 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000321 GlobalHandles::MakeWeak(g2s1.location(),
322 reinterpret_cast<void*>(1234),
323 &WeakPointerCallback);
324 GlobalHandles::MakeWeak(g2s2.location(),
325 reinterpret_cast<void*>(1234),
326 &WeakPointerCallback);
327
328 Handle<Object> root = GlobalHandles::Create(*g1s1); // make a root.
329
330 // Connect group 1 and 2, make a cycle.
331 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2);
332 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1);
333
ager@chromium.org8bb60582008-12-11 12:02:20 +0000334 {
335 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
336 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
337 GlobalHandles::AddGroup(g1_objects, 2);
338 GlobalHandles::AddGroup(g2_objects, 2);
339 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000340 // Do a full GC
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000341 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000342
343 // All object should be alive.
344 CHECK_EQ(0, NumberOfWeakCalls);
345
346 // Weaken the root.
347 GlobalHandles::MakeWeak(root.location(),
348 reinterpret_cast<void*>(1234),
349 &WeakPointerCallback);
350
351 // Groups are deleted, rebuild groups.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000352 {
353 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
354 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
355 GlobalHandles::AddGroup(g1_objects, 2);
356 GlobalHandles::AddGroup(g2_objects, 2);
357 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000358
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000359 Heap::CollectGarbage(OLD_POINTER_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000360
361 // All objects should be gone. 5 global handles in total.
362 CHECK_EQ(5, NumberOfWeakCalls);
363}