-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathobject_id_ring_test.cc
313 lines (280 loc) · 11.6 KB
/
object_id_ring_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/object_id_ring.h"
#include "platform/assert.h"
#include "vm/dart_api_impl.h"
#include "vm/service.h"
#include "vm/symbols.h"
#include "vm/unit_test.h"
namespace dart {
#ifndef PRODUCT
class ObjectIdRingTestHelper {
public:
static void SetMaxSerial(ObjectIdRing& ring, int32_t max_serial) {
ring.max_serial_ = max_serial;
}
static void ExpectIdIsValid(ObjectIdRing& ring, intptr_t id) {
EXPECT(ring.IsValidId(id));
}
static void ExpectIdIsInvalid(ObjectIdRing& ring, intptr_t id) {
EXPECT(!ring.IsValidId(id));
}
static void ExpectIndexId(ObjectIdRing& ring, intptr_t index, intptr_t id) {
EXPECT_EQ(id, ring.IdOfIndex(index));
}
static void ExpectInvalidIndex(ObjectIdRing& ring, intptr_t index) {
EXPECT_EQ(-1, ring.IdOfIndex(index));
}
static ObjectPtr MakeString(const char* s) {
return Symbols::New(Thread::Current(), s);
}
static void ExpectString(ObjectPtr obj, const char* s) {
String& str = String::Handle();
str ^= obj;
EXPECT(str.Equals(s));
}
};
// Test that serial number wrapping works.
ISOLATE_UNIT_TEST_CASE(ObjectIdRingSerialWrapTest) {
ObjectIdRing ring(/*capacity=*/2);
ObjectIdRingTestHelper::SetMaxSerial(ring, 4);
intptr_t id;
ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid;
id = ring.GetIdForObject(ObjectIdRingTestHelper::MakeString("0"));
EXPECT_EQ(0, id);
ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 0);
ObjectIdRingTestHelper::ExpectInvalidIndex(ring, 1);
id = ring.GetIdForObject(ObjectIdRingTestHelper::MakeString("1"));
EXPECT_EQ(1, id);
ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 0);
ObjectIdRingTestHelper::ExpectIndexId(ring, 1, 1);
// Test that id 1 gives us the "1" string.
ObjectIdRingTestHelper::ExpectString(ring.GetObjectForId(id, &kind), "1");
EXPECT_EQ(ObjectIdRing::kValid, kind);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 0);
ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 0);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 1);
ObjectIdRingTestHelper::ExpectIndexId(ring, 1, 1);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 2);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 3);
// We have wrapped, index 0 is being reused.
id = ring.GetIdForObject(ObjectIdRingTestHelper::MakeString("2"));
EXPECT_EQ(2, id);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 0);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 1);
// Index 0 has id 2.
ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 2);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 2);
// Index 1 has id 1.
ObjectIdRingTestHelper::ExpectIndexId(ring, 1, 1);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 3);
id = ring.GetIdForObject(ObjectIdRingTestHelper::MakeString("3"));
EXPECT_EQ(3, id);
// Index 0 has id 2.
ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 2);
// Index 1 has id 3.
ObjectIdRingTestHelper::ExpectIndexId(ring, 1, 3);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 0);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 1);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 2);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 3);
id = ring.GetIdForObject(ObjectIdRingTestHelper::MakeString("4"));
EXPECT_EQ(0, id);
// Index 0 has id 0.
ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 0);
// Index 1 has id 3.
ObjectIdRingTestHelper::ExpectIndexId(ring, 1, 3);
ObjectIdRingTestHelper::ExpectString(ring.GetObjectForId(id, &kind), "4");
EXPECT_EQ(ObjectIdRing::kValid, kind);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 0);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 1);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 2);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 3);
id = ring.GetIdForObject(ObjectIdRingTestHelper::MakeString("5"));
EXPECT_EQ(1, id);
// Index 0 has id 0.
ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 0);
// Index 1 has id 1.
ObjectIdRingTestHelper::ExpectIndexId(ring, 1, 1);
ObjectIdRingTestHelper::ExpectString(ring.GetObjectForId(id, &kind), "5");
EXPECT_EQ(ObjectIdRing::kValid, kind);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 0);
ObjectIdRingTestHelper::ExpectIdIsValid(ring, 1);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 2);
ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 3);
}
class ServiceIdZonePolicyOverrideScope : public ValueObject {
public:
explicit ServiceIdZonePolicyOverrideScope(ServiceIdZone& id_zone,
ObjectIdRing::IdPolicy new_policy)
: id_zone_(id_zone), original_policy_(id_zone.policy()) {
id_zone_.policy_ = new_policy;
}
~ServiceIdZonePolicyOverrideScope() { id_zone_.policy_ = original_policy_; }
private:
ServiceIdZone& id_zone_;
ObjectIdRing::IdPolicy original_policy_;
};
// Test that the ring table is updated when the scavenger moves an object.
TEST_CASE(ObjectIdRingScavengeMoveTest) {
const char* kScriptChars =
"main() {\n"
" return [1, 2, 3];\n"
"}\n";
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, nullptr);
Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, nullptr);
Dart_Handle moved_handle;
intptr_t list_length = 0;
EXPECT_VALID(result);
EXPECT(!Dart_IsNull(result));
EXPECT(Dart_IsList(result));
EXPECT_VALID(Dart_ListLength(result, &list_length));
EXPECT_EQ(3, list_length);
ServiceIdZone& default_id_zone =
thread->isolate()->EnsureDefaultServiceIdZone();
ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid;
{
TransitionNativeToVM to_vm(thread);
const ObjectPtr raw_obj = Api::UnwrapHandle(result);
// Located in new heap.
EXPECT(raw_obj->IsNewObject());
EXPECT_NE(Object::null(), raw_obj);
intptr_t raw_obj_id1 = default_id_zone.GetIdForObject(raw_obj);
EXPECT_EQ(0, raw_obj_id1);
{
ServiceIdZonePolicyOverrideScope override(
default_id_zone, ObjectIdRing::IdPolicy::kReuseId);
// Get id 0 again.
EXPECT_EQ(raw_obj_id1, default_id_zone.GetIdForObject(raw_obj));
}
// Add to ring a second time.
intptr_t raw_obj_id2 = default_id_zone.GetIdForObject(raw_obj);
EXPECT_EQ(1, raw_obj_id2);
{
ServiceIdZonePolicyOverrideScope override(
default_id_zone, ObjectIdRing::IdPolicy::kReuseId);
// Get id 0 again.
EXPECT_EQ(raw_obj_id1, default_id_zone.GetIdForObject(raw_obj));
}
const ObjectPtr raw_obj1 =
default_id_zone.GetObjectForId(raw_obj_id1, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
const ObjectPtr raw_obj2 =
default_id_zone.GetObjectForId(raw_obj_id2, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
EXPECT_NE(Object::null(), raw_obj1);
EXPECT_NE(Object::null(), raw_obj2);
EXPECT_EQ(UntaggedObject::ToAddr(raw_obj),
UntaggedObject::ToAddr(raw_obj1));
EXPECT_EQ(UntaggedObject::ToAddr(raw_obj),
UntaggedObject::ToAddr(raw_obj2));
// Force a scavenge.
GCTestHelper::CollectNewSpace();
const ObjectPtr raw_object_moved1 =
default_id_zone.GetObjectForId(raw_obj_id1, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
const ObjectPtr raw_object_moved2 =
default_id_zone.GetObjectForId(raw_obj_id2, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
EXPECT_NE(Object::null(), raw_object_moved1);
EXPECT_NE(Object::null(), raw_object_moved2);
EXPECT_EQ(UntaggedObject::ToAddr(raw_object_moved1),
UntaggedObject::ToAddr(raw_object_moved2));
// Test that objects have moved.
EXPECT_NE(UntaggedObject::ToAddr(raw_obj1),
UntaggedObject::ToAddr(raw_object_moved1));
EXPECT_NE(UntaggedObject::ToAddr(raw_obj2),
UntaggedObject::ToAddr(raw_object_moved2));
// Test that we still point at the same list.
moved_handle = Api::NewHandle(thread, raw_object_moved1);
// Test id reuse.
{
ServiceIdZonePolicyOverrideScope override(
default_id_zone, ObjectIdRing::IdPolicy::kReuseId);
EXPECT_EQ(raw_obj_id1, default_id_zone.GetIdForObject(raw_object_moved1));
}
}
EXPECT_VALID(moved_handle);
EXPECT(!Dart_IsNull(moved_handle));
EXPECT(Dart_IsList(moved_handle));
EXPECT_VALID(Dart_ListLength(moved_handle, &list_length));
EXPECT_EQ(3, list_length);
}
// Test that the ring table is updated when major GC runs.
ISOLATE_UNIT_TEST_CASE(ObjectIdRingOldGCTest) {
ServiceIdZone& default_id_zone =
thread->isolate()->EnsureDefaultServiceIdZone();
ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid;
intptr_t raw_obj_id1 = -1;
intptr_t raw_obj_id2 = -1;
{
HandleScope handle_scope(thread);
const String& str = String::Handle(String::New("old", Heap::kOld));
EXPECT(!str.IsNull());
EXPECT_EQ(3, str.Length());
const ObjectPtr raw_obj = Object::RawCast(str.ptr());
// Verify that it is located in old heap.
EXPECT(raw_obj->IsOldObject());
EXPECT_NE(Object::null(), raw_obj);
raw_obj_id1 = default_id_zone.GetIdForObject(raw_obj);
EXPECT_EQ(0, raw_obj_id1);
raw_obj_id2 = default_id_zone.GetIdForObject(raw_obj);
EXPECT_EQ(1, raw_obj_id2);
const ObjectPtr raw_obj1 =
default_id_zone.GetObjectForId(raw_obj_id1, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
const ObjectPtr raw_obj2 =
default_id_zone.GetObjectForId(raw_obj_id2, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
EXPECT_NE(Object::null(), raw_obj1);
EXPECT_NE(Object::null(), raw_obj2);
EXPECT_EQ(UntaggedObject::ToAddr(raw_obj),
UntaggedObject::ToAddr(raw_obj1));
EXPECT_EQ(UntaggedObject::ToAddr(raw_obj),
UntaggedObject::ToAddr(raw_obj2));
// Exit scope. Freeing String handle.
}
// Force a GC. No other reference to the old string exists, but the service id
// should keep it alive.
GCTestHelper::CollectOldSpace();
const ObjectPtr raw_object_moved1 =
default_id_zone.GetObjectForId(raw_obj_id1, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
EXPECT(raw_object_moved1->IsOneByteString());
const ObjectPtr raw_object_moved2 =
default_id_zone.GetObjectForId(raw_obj_id2, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
EXPECT(raw_object_moved2->IsOneByteString());
EXPECT_EQ(raw_object_moved1, raw_object_moved2);
}
// Test that the ring table correctly reports an entry as expired when it is
// overridden by new entries.
ISOLATE_UNIT_TEST_CASE(ObjectIdRingExpiredEntryTest) {
ObjectIdRing ring(RingServiceIdZone::kFallbackCapacityForCreateIdZone);
// Insert an object and check we can look it up.
String& obj = String::Handle(String::New("I will expire"));
intptr_t obj_id = ring.GetIdForObject(obj.ptr());
ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid;
ObjectPtr obj_lookup = ring.GetObjectForId(obj_id, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
EXPECT_EQ(obj.ptr(), obj_lookup);
// Insert as many new objects as the ring size to bump out our first entry.
Object& new_obj = Object::Handle();
for (intptr_t i = 0; i < RingServiceIdZone::kFallbackCapacityForCreateIdZone;
i++) {
new_obj = String::New("Bump");
intptr_t new_obj_id = ring.GetIdForObject(new_obj.ptr());
ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid;
ObjectPtr new_obj_lookup = ring.GetObjectForId(new_obj_id, &kind);
EXPECT_EQ(ObjectIdRing::kValid, kind);
EXPECT_EQ(new_obj.ptr(), new_obj_lookup);
}
// Check our first entry reports it has expired.
obj_lookup = ring.GetObjectForId(obj_id, &kind);
EXPECT_EQ(ObjectIdRing::kExpired, kind);
EXPECT_NE(obj.ptr(), obj_lookup);
EXPECT_EQ(Object::null(), obj_lookup);
}
#endif // !PRODUCT
} // namespace dart