• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
37 using namespace v8::internal;
38 
39 static v8::Persistent<v8::Context> env;
40 
InitializeVM()41 static void InitializeVM() {
42   if (env.IsEmpty()) env = v8::Context::New();
43   v8::HandleScope scope;
44   env->Enter();
45 }
46 
47 
TEST(MarkingStack)48 TEST(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 
TEST(Promotion)73 TEST(Promotion) {
74   // Test the situation that some objects in new space are promoted to the
75   // old space
76   if (Snapshot::IsEnabled()) return;
77 
78   // 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;
82   Heap::ConfigureHeap(2*256*KB, 4*MB);
83 
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);
92   Object* obj = Heap::AllocateFixedArray(array_size);
93   CHECK(!obj->IsFailure());
94 
95   Handle<FixedArray> array(FixedArray::cast(obj));
96 
97   // Array should be in the new space.
98   CHECK(Heap::InSpace(*array, NEW_SPACE));
99 
100   // Call the m-c collector, so array becomes an old object.
101   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
102 
103   // Array now sits in the old space
104   CHECK(Heap::InSpace(*array, OLD_POINTER_SPACE));
105 }
106 
107 
TEST(NoPromotion)108 TEST(NoPromotion) {
109   if (Snapshot::IsEnabled()) return;
110   Heap::ConfigureHeap(2*256*KB, 4*MB);
111 
112   // Test the situation that some objects in new space are promoted to
113   // the old space
114   InitializeVM();
115 
116   v8::HandleScope sc;
117 
118   // Do a mark compact GC to shrink the heap.
119   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
120 
121   // Allocate a big Fixed array in the new space.
122   int size = (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) /
123       kPointerSize;
124   Object* obj = Heap::AllocateFixedArray(size);
125 
126   Handle<FixedArray> array(FixedArray::cast(obj));
127 
128   // Array still stays in the new space.
129   CHECK(Heap::InSpace(*array, NEW_SPACE));
130 
131   // Allocate objects in the old space until out of memory.
132   FixedArray* host = *array;
133   while (true) {
134     Object* obj = Heap::AllocateFixedArray(100, TENURED);
135     if (obj->IsFailure()) break;
136 
137     host->set(0, obj);
138     host = FixedArray::cast(obj);
139   }
140 
141   // Call mark compact GC, and it should pass.
142   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
143 
144   // array should not be promoted because the old space is full.
145   CHECK(Heap::InSpace(*array, NEW_SPACE));
146 }
147 
148 
TEST(MarkCompactCollector)149 TEST(MarkCompactCollector) {
150   InitializeVM();
151 
152   v8::HandleScope sc;
153   // call mark-compact when heap is empty
154   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
155 
156   // keep allocating garbage in new space until it fails
157   const int ARRAY_SIZE = 100;
158   Object* array;
159   do {
160     array = Heap::AllocateFixedArray(ARRAY_SIZE);
161   } while (!array->IsFailure());
162   CHECK(Heap::CollectGarbage(0, NEW_SPACE));
163 
164   array = Heap::AllocateFixedArray(ARRAY_SIZE);
165   CHECK(!array->IsFailure());
166 
167   // keep allocating maps until it fails
168   Object* mapp;
169   do {
170     mapp = Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
171   } while (!mapp->IsFailure());
172   CHECK(Heap::CollectGarbage(0, MAP_SPACE));
173   mapp = Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
174   CHECK(!mapp->IsFailure());
175 
176   // allocate a garbage
177   String* func_name = String::cast(Heap::LookupAsciiSymbol("theFunction"));
178   SharedFunctionInfo* function_share =
179     SharedFunctionInfo::cast(Heap::AllocateSharedFunctionInfo(func_name));
180   JSFunction* function =
181     JSFunction::cast(Heap::AllocateFunction(*Top::function_map(),
182                                             function_share,
183                                             Heap::undefined_value()));
184   Map* initial_map =
185       Map::cast(Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize));
186   function->set_initial_map(initial_map);
187   Top::context()->global()->SetProperty(func_name, function, NONE);
188 
189   JSObject* obj = JSObject::cast(Heap::AllocateJSObject(function));
190   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
191 
192   func_name = String::cast(Heap::LookupAsciiSymbol("theFunction"));
193   CHECK(Top::context()->global()->HasLocalProperty(func_name));
194   Object* func_value = Top::context()->global()->GetProperty(func_name);
195   CHECK(func_value->IsJSFunction());
196   function = JSFunction::cast(func_value);
197 
198   obj = JSObject::cast(Heap::AllocateJSObject(function));
199   String* obj_name = String::cast(Heap::LookupAsciiSymbol("theObject"));
200   Top::context()->global()->SetProperty(obj_name, obj, NONE);
201   String* prop_name = String::cast(Heap::LookupAsciiSymbol("theSlot"));
202   obj->SetProperty(prop_name, Smi::FromInt(23), NONE);
203 
204   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
205 
206   obj_name = String::cast(Heap::LookupAsciiSymbol("theObject"));
207   CHECK(Top::context()->global()->HasLocalProperty(obj_name));
208   CHECK(Top::context()->global()->GetProperty(obj_name)->IsJSObject());
209   obj = JSObject::cast(Top::context()->global()->GetProperty(obj_name));
210   prop_name = String::cast(Heap::LookupAsciiSymbol("theSlot"));
211   CHECK(obj->GetProperty(prop_name) == Smi::FromInt(23));
212 }
213 
214 
215 static int gc_starts = 0;
216 static int gc_ends = 0;
217 
GCPrologueCallbackFunc()218 static void GCPrologueCallbackFunc() {
219   CHECK(gc_starts == gc_ends);
220   gc_starts++;
221 }
222 
223 
GCEpilogueCallbackFunc()224 static void GCEpilogueCallbackFunc() {
225   CHECK(gc_starts == gc_ends + 1);
226   gc_ends++;
227 }
228 
229 
TEST(GCCallback)230 TEST(GCCallback) {
231   InitializeVM();
232 
233   Heap::SetGlobalGCPrologueCallback(&GCPrologueCallbackFunc);
234   Heap::SetGlobalGCEpilogueCallback(&GCEpilogueCallbackFunc);
235 
236   // Scavenge does not call GC callback functions.
237   Heap::PerformScavenge();
238 
239   CHECK_EQ(0, gc_starts);
240   CHECK_EQ(gc_ends, gc_starts);
241 
242   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
243   CHECK_EQ(1, gc_starts);
244   CHECK_EQ(gc_ends, gc_starts);
245 }
246 
247 
248 static int NumberOfWeakCalls = 0;
WeakPointerCallback(v8::Persistent<v8::Value> handle,void * id)249 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) {
250   NumberOfWeakCalls++;
251 }
252 
TEST(ObjectGroups)253 TEST(ObjectGroups) {
254   InitializeVM();
255 
256   NumberOfWeakCalls = 0;
257   v8::HandleScope handle_scope;
258 
259   Handle<Object> g1s1 =
260     GlobalHandles::Create(Heap::AllocateFixedArray(1));
261   Handle<Object> g1s2 =
262     GlobalHandles::Create(Heap::AllocateFixedArray(1));
263   GlobalHandles::MakeWeak(g1s1.location(),
264                           reinterpret_cast<void*>(1234),
265                           &WeakPointerCallback);
266   GlobalHandles::MakeWeak(g1s2.location(),
267                           reinterpret_cast<void*>(1234),
268                           &WeakPointerCallback);
269 
270   Handle<Object> g2s1 =
271     GlobalHandles::Create(Heap::AllocateFixedArray(1));
272   Handle<Object> g2s2 =
273     GlobalHandles::Create(Heap::AllocateFixedArray(1));
274   GlobalHandles::MakeWeak(g2s1.location(),
275                           reinterpret_cast<void*>(1234),
276                           &WeakPointerCallback);
277   GlobalHandles::MakeWeak(g2s2.location(),
278                           reinterpret_cast<void*>(1234),
279                           &WeakPointerCallback);
280 
281   Handle<Object> root = GlobalHandles::Create(*g1s1);  // make a root.
282 
283   // Connect group 1 and 2, make a cycle.
284   Handle<FixedArray>::cast(g1s2)->set(0, *g2s2);
285   Handle<FixedArray>::cast(g2s1)->set(0, *g1s1);
286 
287   {
288     Object** g1_objects[] = { g1s1.location(), g1s2.location() };
289     Object** g2_objects[] = { g2s1.location(), g2s2.location() };
290     GlobalHandles::AddGroup(g1_objects, 2);
291     GlobalHandles::AddGroup(g2_objects, 2);
292   }
293   // Do a full GC
294   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
295 
296   // All object should be alive.
297   CHECK_EQ(0, NumberOfWeakCalls);
298 
299   // Weaken the root.
300   GlobalHandles::MakeWeak(root.location(),
301                           reinterpret_cast<void*>(1234),
302                           &WeakPointerCallback);
303 
304   // Groups are deleted, rebuild groups.
305   {
306     Object** g1_objects[] = { g1s1.location(), g1s2.location() };
307     Object** g2_objects[] = { g2s1.location(), g2s2.location() };
308     GlobalHandles::AddGroup(g1_objects, 2);
309     GlobalHandles::AddGroup(g2_objects, 2);
310   }
311 
312   CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));
313 
314   // All objects should be gone. 5 global handles in total.
315   CHECK_EQ(5, NumberOfWeakCalls);
316 }
317