v8  3.11.10(node0.8.26)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-weakmaps.cc
Go to the documentation of this file.
1 // Copyright 2011 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 "v8.h"
29 
30 #include "global-handles.h"
31 #include "snapshot.h"
32 #include "cctest.h"
33 
34 using namespace v8::internal;
35 
36 
37 static Handle<JSWeakMap> AllocateJSWeakMap() {
39  Handle<JSObject> weakmap_obj = FACTORY->NewJSObjectFromMap(map);
40  Handle<JSWeakMap> weakmap(JSWeakMap::cast(*weakmap_obj));
41  // Do not use handles for the hash table, it would make entries strong.
42  Object* table_obj = ObjectHashTable::Allocate(1)->ToObjectChecked();
43  ObjectHashTable* table = ObjectHashTable::cast(table_obj);
44  weakmap->set_table(table);
45  weakmap->set_next(Smi::FromInt(0));
46  return weakmap;
47 }
48 
49 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap,
50  Handle<JSObject> key,
51  Handle<Object> value) {
55  value);
56  weakmap->set_table(*table);
57 }
58 
59 static int NumberOfWeakCalls = 0;
60 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) {
61  ASSERT(id == reinterpret_cast<void*>(1234));
62  NumberOfWeakCalls++;
63  handle.Dispose();
64 }
65 
66 
67 TEST(Weakness) {
68  FLAG_incremental_marking = false;
69  LocalContext context;
70  v8::HandleScope scope;
71  Handle<JSWeakMap> weakmap = AllocateJSWeakMap();
72  GlobalHandles* global_handles = Isolate::Current()->global_handles();
73 
74  // Keep global reference to the key.
75  Handle<Object> key;
76  {
77  v8::HandleScope scope;
79  Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map);
80  key = global_handles->Create(*object);
81  }
82  CHECK(!global_handles->IsWeak(key.location()));
83 
84  // Put entry into weak map.
85  {
86  v8::HandleScope scope;
87  PutIntoWeakMap(weakmap,
90  }
91  CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
92 
93  // Force a full GC.
94  HEAP->CollectAllGarbage(false);
95  CHECK_EQ(0, NumberOfWeakCalls);
96  CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
97  CHECK_EQ(
98  0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
99 
100  // Make the global reference to the key weak.
101  {
102  v8::HandleScope scope;
103  global_handles->MakeWeak(key.location(),
104  reinterpret_cast<void*>(1234),
105  &WeakPointerCallback);
106  }
107  CHECK(global_handles->IsWeak(key.location()));
108 
109  // Force a full GC.
110  // Perform two consecutive GCs because the first one will only clear
111  // weak references whereas the second one will also clear weak maps.
112  HEAP->CollectAllGarbage(false);
113  CHECK_EQ(1, NumberOfWeakCalls);
114  CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
115  CHECK_EQ(
116  0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
117  HEAP->CollectAllGarbage(false);
118  CHECK_EQ(1, NumberOfWeakCalls);
119  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
120  CHECK_EQ(
121  1, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
122 }
123 
124 
125 TEST(Shrinking) {
126  LocalContext context;
127  v8::HandleScope scope;
128  Handle<JSWeakMap> weakmap = AllocateJSWeakMap();
129 
130  // Check initial capacity.
131  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
132 
133  // Fill up weak map to trigger capacity change.
134  {
135  v8::HandleScope scope;
137  for (int i = 0; i < 32; i++) {
138  Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map);
139  PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(i)));
140  }
141  }
142 
143  // Check increased capacity.
144  CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
145 
146  // Force a full GC.
147  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
148  CHECK_EQ(
149  0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
150  HEAP->CollectAllGarbage(false);
151  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
152  CHECK_EQ(
153  32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
154 
155  // Check shrunk capacity.
156  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
157 }
158 
159 
160 // Test that weak map values on an evacuation candidate which are not reachable
161 // by other paths are correctly recorded in the slots buffer.
162 TEST(Regress2060a) {
163  FLAG_always_compact = true;
164  LocalContext context;
165  v8::HandleScope scope;
166  Handle<JSFunction> function =
167  FACTORY->NewFunction(FACTORY->function_symbol(), FACTORY->null_value());
168  Handle<JSObject> key = FACTORY->NewJSObject(function);
169  Handle<JSWeakMap> weakmap = AllocateJSWeakMap();
170 
171  // Start second old-space page so that values land on evacuation candidate.
172  Page* first_page = HEAP->old_pointer_space()->anchor()->next_page();
173  FACTORY->NewFixedArray(900 * KB / kPointerSize, TENURED);
174 
175  // Fill up weak map with values on an evacuation candidate.
176  {
177  v8::HandleScope scope;
178  for (int i = 0; i < 32; i++) {
179  Handle<JSObject> object = FACTORY->NewJSObject(function, TENURED);
180  CHECK(!HEAP->InNewSpace(object->address()));
181  CHECK(!first_page->Contains(object->address()));
182  PutIntoWeakMap(weakmap, key, object);
183  }
184  }
185 
186  // Force compacting garbage collection.
187  CHECK(FLAG_always_compact);
188  HEAP->CollectAllGarbage(Heap::kNoGCFlags);
189 }
190 
191 
192 // Test that weak map keys on an evacuation candidate which are reachable by
193 // other strong paths are correctly recorded in the slots buffer.
194 TEST(Regress2060b) {
195  FLAG_always_compact = true;
196 #ifdef DEBUG
197  FLAG_verify_heap = true;
198 #endif
199  LocalContext context;
200  v8::HandleScope scope;
201  Handle<JSFunction> function =
202  FACTORY->NewFunction(FACTORY->function_symbol(), FACTORY->null_value());
203 
204  // Start second old-space page so that keys land on evacuation candidate.
205  Page* first_page = HEAP->old_pointer_space()->anchor()->next_page();
206  FACTORY->NewFixedArray(900 * KB / kPointerSize, TENURED);
207 
208  // Fill up weak map with keys on an evacuation candidate.
209  Handle<JSObject> keys[32];
210  for (int i = 0; i < 32; i++) {
211  keys[i] = FACTORY->NewJSObject(function, TENURED);
212  CHECK(!HEAP->InNewSpace(keys[i]->address()));
213  CHECK(!first_page->Contains(keys[i]->address()));
214  }
215  Handle<JSWeakMap> weakmap = AllocateJSWeakMap();
216  for (int i = 0; i < 32; i++) {
217  PutIntoWeakMap(weakmap, keys[i], Handle<Smi>(Smi::FromInt(i)));
218  }
219 
220  // Force compacting garbage collection. The subsequent collections are used
221  // to verify that key references were actually updated.
222  CHECK(FLAG_always_compact);
223  HEAP->CollectAllGarbage(Heap::kNoGCFlags);
224  HEAP->CollectAllGarbage(Heap::kNoGCFlags);
225  HEAP->CollectAllGarbage(Heap::kNoGCFlags);
226 }
#define CHECK_EQ(expected, value)
Definition: checks.h:219
void Dispose()
Definition: v8.h:4065
bool Contains(Address addr)
Definition: spaces.h:365
static Smi * FromInt(int value)
Definition: objects-inl.h:973
const int KB
Definition: globals.h:221
static MUST_USE_RESULT MaybeObject * Allocate(int at_least_space_for, PretenureFlag pretenure=NOT_TENURED)
#define ASSERT(condition)
Definition: checks.h:270
#define CHECK(condition)
Definition: checks.h:56
T ** location() const
Definition: handles.h:75
Handle< Object > Create(Object *value)
static const int kNoGCFlags
Definition: heap.h:1049
const int kPointerSize
Definition: globals.h:234
Handle< ObjectHashTable > PutIntoObjectHashTable(Handle< ObjectHashTable > table, Handle< Object > key, Handle< Object > value)
Definition: handles.cc:794
static ObjectHashTable * cast(Object *obj)
Definition: objects.h:3311
static JSWeakMap * cast(Object *obj)
#define HEAP
Definition: isolate.h:1408
static bool IsWeak(Object **location)
static const int kHeaderSize
Definition: objects.h:2115
#define FACTORY
Definition: isolate.h:1409
static const int kSize
Definition: objects.h:8013
void MakeWeak(Object **location, void *parameter, WeakReferenceCallback callback)
static JSObject * cast(Object *obj)