blob: 86f105f2c971234f48641e82c00b5204a5ac2021 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 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#include "v8.h"
31
32#include "global-handles.h"
33#include "snapshot.h"
34#include "top.h"
35#include "cctest.h"
36
37using 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) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010074 // This test requires compaction. If compaction is turned off, we
75 // skip the entire test.
76 if (FLAG_never_compact) return;
77
Steve Blocka7e24c12009-10-30 11:49:00 +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;
Russell Brenner90bac252010-11-18 13:33:46 -080082 Heap::ConfigureHeap(2*256*KB, 4*MB, 4*MB);
Steve Blocka7e24c12009-10-30 11:49:00 +000083
84 InitializeVM();
85
86 v8::HandleScope sc;
87
88 // Allocate a fixed array in the new space.
89 int array_size =
90 (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) /
91 (kPointerSize * 4);
John Reck59135872010-11-02 12:39:01 -070092 Object* obj = Heap::AllocateFixedArray(array_size)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Ben Murdochf87a2032010-10-22 12:50:53 +0100100 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000101
102 // Array now sits in the old space
103 CHECK(Heap::InSpace(*array, OLD_POINTER_SPACE));
104}
105
106
107TEST(NoPromotion) {
Russell Brenner90bac252010-11-18 13:33:46 -0800108 Heap::ConfigureHeap(2*256*KB, 4*MB, 4*MB);
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Ben Murdochf87a2032010-10-22 12:50:53 +0100117 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000118
119 // Allocate a big Fixed array in the new space.
120 int size = (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) /
121 kPointerSize;
John Reck59135872010-11-02 12:39:01 -0700122 Object* obj = Heap::AllocateFixedArray(size)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +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) {
John Reck59135872010-11-02 12:39:01 -0700132 Object* obj;
133 { MaybeObject* maybe_obj = Heap::AllocateFixedArray(100, TENURED);
134 if (!maybe_obj->ToObject(&obj)) break;
135 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
137 host->set(0, obj);
138 host = FixedArray::cast(obj);
139 }
140
141 // Call mark compact GC, and it should pass.
Ben Murdochf87a2032010-10-22 12:50:53 +0100142 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +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
Ben Murdochf87a2032010-10-22 12:50:53 +0100154 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000155
156 // keep allocating garbage in new space until it fails
157 const int ARRAY_SIZE = 100;
158 Object* array;
John Reck59135872010-11-02 12:39:01 -0700159 MaybeObject* maybe_array;
Steve Blocka7e24c12009-10-30 11:49:00 +0000160 do {
John Reck59135872010-11-02 12:39:01 -0700161 maybe_array = Heap::AllocateFixedArray(ARRAY_SIZE);
162 } while (maybe_array->ToObject(&array));
Ben Murdochf87a2032010-10-22 12:50:53 +0100163 Heap::CollectGarbage(NEW_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000164
John Reck59135872010-11-02 12:39:01 -0700165 array = Heap::AllocateFixedArray(ARRAY_SIZE)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000166
167 // keep allocating maps until it fails
168 Object* mapp;
John Reck59135872010-11-02 12:39:01 -0700169 MaybeObject* maybe_mapp;
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 do {
John Reck59135872010-11-02 12:39:01 -0700171 maybe_mapp = Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
172 } while (maybe_mapp->ToObject(&mapp));
Ben Murdochf87a2032010-10-22 12:50:53 +0100173 Heap::CollectGarbage(MAP_SPACE);
John Reck59135872010-11-02 12:39:01 -0700174 mapp = Heap::AllocateMap(JS_OBJECT_TYPE,
175 JSObject::kHeaderSize)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000176
177 // allocate a garbage
John Reck59135872010-11-02 12:39:01 -0700178 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());
Steve Blocka7e24c12009-10-30 11:49:00 +0000186 Map* initial_map =
John Reck59135872010-11-02 12:39:01 -0700187 Map::cast(Heap::AllocateMap(JS_OBJECT_TYPE,
188 JSObject::kHeaderSize)->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 function->set_initial_map(initial_map);
John Reck59135872010-11-02 12:39:01 -0700190 Top::context()->global()->SetProperty(func_name,
191 function,
192 NONE)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000193
John Reck59135872010-11-02 12:39:01 -0700194 JSObject* obj =
195 JSObject::cast(Heap::AllocateJSObject(function)->ToObjectChecked());
Ben Murdochf87a2032010-10-22 12:50:53 +0100196 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000197
John Reck59135872010-11-02 12:39:01 -0700198 func_name =
199 String::cast(Heap::LookupAsciiSymbol("theFunction")->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 CHECK(Top::context()->global()->HasLocalProperty(func_name));
John Reck59135872010-11-02 12:39:01 -0700201 Object* func_value =
202 Top::context()->global()->GetProperty(func_name)->ToObjectChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 CHECK(func_value->IsJSFunction());
204 function = JSFunction::cast(func_value);
205
John Reck59135872010-11-02 12:39:01 -0700206 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();
Steve Blocka7e24c12009-10-30 11:49:00 +0000215
Ben Murdochf87a2032010-10-22 12:50:53 +0100216 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000217
John Reck59135872010-11-02 12:39:01 -0700218 obj_name =
219 String::cast(Heap::LookupAsciiSymbol("theObject")->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 CHECK(Top::context()->global()->HasLocalProperty(obj_name));
John Reck59135872010-11-02 12:39:01 -0700221 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));
Steve Blocka7e24c12009-10-30 11:49:00 +0000228}
229
230
Leon Clarked91b9f72010-01-27 17:25:45 +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
Steve Blocka7e24c12009-10-30 11:49:00 +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
Ben Murdochf87a2032010-10-22 12:50:53 +0100288 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 CHECK_EQ(1, gc_starts);
290 CHECK_EQ(gc_ends, gc_starts);
291}
292
293
294static int NumberOfWeakCalls = 0;
295static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) {
296 NumberOfWeakCalls++;
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100297 handle.Dispose();
Steve Blocka7e24c12009-10-30 11:49:00 +0000298}
299
300TEST(ObjectGroups) {
301 InitializeVM();
302
303 NumberOfWeakCalls = 0;
304 v8::HandleScope handle_scope;
305
306 Handle<Object> g1s1 =
John Reck59135872010-11-02 12:39:01 -0700307 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 Handle<Object> g1s2 =
John Reck59135872010-11-02 12:39:01 -0700309 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +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 =
John Reck59135872010-11-02 12:39:01 -0700318 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 Handle<Object> g2s2 =
John Reck59135872010-11-02 12:39:01 -0700320 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +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
334 {
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 }
340 // Do a full GC
Ben Murdochf87a2032010-10-22 12:50:53 +0100341 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +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.
352 {
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 }
358
Ben Murdochf87a2032010-10-22 12:50:53 +0100359 Heap::CollectGarbage(OLD_POINTER_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360
361 // All objects should be gone. 5 global handles in total.
362 CHECK_EQ(5, NumberOfWeakCalls);
363}