24
#include "precompiled.hpp"
25
#include "gc/z/zAddress.inline.hpp"
26
#include "gc/z/zGlobals.hpp"
27
#include "unittest.hpp"
29
class ZAddressTest : public ::testing::Test {
31
static zpointer color(uintptr_t value, uintptr_t color) {
32
return ZAddress::color(zaddress(value | ZAddressHeapBase), color);
35
static const uintptr_t valid_value = (1 << 3 );
36
static const uintptr_t null_value = 0;
55
static uintptr_t make_color(ZColor remembered, ZColor remapped_young, ZColor remapped_old, ZColor marked_young, ZColor marked_old) {
57
switch (remapped_young) {
58
case RemappedYoung0: {
59
switch (remapped_old) {
61
color |= ZPointer::remap_bits(ZPointerRemapped00);
64
color |= ZPointer::remap_bits(ZPointerRemapped10);
71
case RemappedYoung1: {
72
switch (remapped_old) {
74
color |= ZPointer::remap_bits(ZPointerRemapped01);
77
color |= ZPointer::remap_bits(ZPointerRemapped11);
88
switch (marked_young) {
90
color |= ZPointerMarkedYoung0;
93
color |= ZPointerMarkedYoung1;
101
color |= ZPointerMarkedOld0;
104
color |= ZPointerMarkedOld1;
107
color |= ZPointerFinalizable0;
110
color |= ZPointerFinalizable1;
116
switch (remembered) {
118
color |= ZPointerRemembered0;
121
color |= ZPointerRemembered1;
124
color |= ZPointerRemembered0 | ZPointerRemembered1;
133
static zpointer color(uintptr_t addr,
135
ZColor remapped_young,
139
if (remembered == Uncolored &&
140
remapped_young == Uncolored &&
141
remapped_old == Uncolored &&
142
marked_young == Uncolored &&
143
marked_old == Uncolored) {
144
return zpointer(addr);
146
return color(addr, make_color(remembered, remapped_young, remapped_old, marked_young, marked_old));
150
static bool is_remapped_young_odd(uintptr_t bits) {
151
return ZPointer::remap_bits(bits) & (ZPointerRemapped01 | ZPointerRemapped11);
154
static bool is_remapped_old_odd(uintptr_t bits) {
155
return ZPointer::remap_bits(bits) & (ZPointerRemapped10 | ZPointerRemapped11);
158
static bool is_marked_young_odd(uintptr_t bits) {
159
return bits & ZPointerMarkedYoung1;
162
static bool is_marked_old_odd(uintptr_t bits) {
163
return bits & (ZPointerMarkedOld1 | ZPointerFinalizable1);
166
static bool is_remembered(uintptr_t bits) {
167
return bits & (ZPointerRemembered0 | ZPointerRemembered1);
170
static bool is_remembered_odd(uintptr_t bits) {
171
return bits & (ZPointerRemembered1);
174
static bool is_remembered_even(uintptr_t bits) {
175
return bits & (ZPointerRemembered0);
178
static void test_is_checks_on(uintptr_t value,
180
ZColor remapped_young,
184
const zpointer ptr = color(value, remembered, remapped_young, remapped_old, marked_young, marked_old);
185
uintptr_t ptr_raw = untype(ptr);
187
EXPECT_TRUE(ZPointerLoadGoodMask != 0);
188
EXPECT_TRUE(ZPointerStoreGoodMask != 0);
190
bool ptr_raw_null = ptr_raw == 0;
191
bool global_remapped_old_odd = is_remapped_old_odd(ZPointerLoadGoodMask);
192
bool global_remapped_young_odd = is_remapped_young_odd(ZPointerLoadGoodMask);
193
bool global_marked_old_odd = is_marked_old_odd(ZPointerStoreGoodMask);
194
bool global_marked_young_odd = is_marked_young_odd(ZPointerStoreGoodMask);
195
bool global_remembered_odd = is_remembered_odd(ZPointerStoreGoodMask);
196
bool global_remembered_even = is_remembered_even(ZPointerStoreGoodMask);
199
EXPECT_FALSE(ZPointer::is_marked_any_old(ptr));
200
EXPECT_FALSE(ZPointer::is_load_good(ptr));
201
EXPECT_TRUE(ZPointer::is_load_good_or_null(ptr));
202
EXPECT_FALSE(ZPointer::is_load_bad(ptr));
203
EXPECT_FALSE(ZPointer::is_mark_good(ptr));
204
EXPECT_TRUE(ZPointer::is_mark_good_or_null(ptr));
205
EXPECT_FALSE(ZPointer::is_mark_bad(ptr));
206
EXPECT_FALSE(ZPointer::is_store_good(ptr));
207
EXPECT_TRUE(ZPointer::is_store_good_or_null(ptr));
208
EXPECT_FALSE(ZPointer::is_store_bad(ptr));
210
bool ptr_remapped_old_odd = is_remapped_old_odd(ptr_raw);
211
bool ptr_remapped_young_odd = is_remapped_young_odd(ptr_raw);
212
bool ptr_marked_old_odd = is_marked_old_odd(ptr_raw);
213
bool ptr_marked_young_odd = is_marked_young_odd(ptr_raw);
214
bool ptr_final = ptr_raw & (ZPointerFinalizable0 | ZPointerFinalizable1);
215
bool ptr_remembered = is_power_of_2(ptr_raw & (ZPointerRemembered0 | ZPointerRemembered1));
216
bool ptr_remembered_odd = is_remembered_odd(ptr_raw);
217
bool ptr_remembered_even = is_remembered_even(ptr_raw);
218
bool ptr_colored_null = !ptr_raw_null && (ptr_raw & ~ZPointerAllMetadataMask) == 0;
220
bool same_old_marking = global_marked_old_odd == ptr_marked_old_odd;
221
bool same_young_marking = global_marked_young_odd == ptr_marked_young_odd;
222
bool same_old_remapping = global_remapped_old_odd == ptr_remapped_old_odd;
223
bool same_young_remapping = global_remapped_young_odd == ptr_remapped_young_odd;
224
bool same_remembered = ptr_remembered_even == global_remembered_even && ptr_remembered_odd == global_remembered_odd;
226
EXPECT_EQ(ZPointer::is_marked_finalizable(ptr), same_old_marking && ptr_final);
227
EXPECT_EQ(ZPointer::is_marked_any_old(ptr), same_old_marking);
228
EXPECT_EQ(ZPointer::is_remapped(ptr), same_old_remapping && same_young_remapping);
229
EXPECT_EQ(ZPointer::is_load_good(ptr), same_old_remapping && same_young_remapping);
230
EXPECT_EQ(ZPointer::is_load_good_or_null(ptr), same_old_remapping && same_young_remapping);
231
EXPECT_EQ(ZPointer::is_load_bad(ptr), !same_old_remapping || !same_young_remapping);
232
EXPECT_EQ(ZPointer::is_mark_good(ptr), same_young_remapping && same_old_remapping && same_young_marking && same_old_marking);
233
EXPECT_EQ(ZPointer::is_mark_good_or_null(ptr), same_young_remapping && same_old_remapping && same_young_marking && same_old_marking);
234
EXPECT_EQ(ZPointer::is_mark_bad(ptr), !same_young_remapping || !same_old_remapping || !same_young_marking || !same_old_marking);
235
EXPECT_EQ(ZPointer::is_store_good(ptr), same_young_remapping && same_old_remapping && same_young_marking && same_old_marking && ptr_remembered && same_remembered);
236
EXPECT_EQ(ZPointer::is_store_good_or_null(ptr), same_young_remapping && same_old_remapping && same_young_marking && same_old_marking && ptr_remembered && same_remembered);
237
EXPECT_EQ(ZPointer::is_store_bad(ptr), !same_young_remapping || !same_old_remapping || !same_young_marking || !same_old_marking || !ptr_remembered || !same_remembered);
241
static void test_is_checks_on_all() {
242
test_is_checks_on(valid_value, Remembered0, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld0);
243
test_is_checks_on(null_value, Remembered0, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld0);
244
test_is_checks_on(valid_value, Remembered0, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld1);
245
test_is_checks_on(null_value, Remembered0, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld1);
246
test_is_checks_on(valid_value, Remembered0, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld0);
247
test_is_checks_on(null_value, Remembered0, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld0);
248
test_is_checks_on(valid_value, Remembered0, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld1);
249
test_is_checks_on(null_value, Remembered0, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld1);
251
test_is_checks_on(valid_value, Remembered0, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld0);
252
test_is_checks_on(null_value, Remembered0, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld0);
253
test_is_checks_on(valid_value, Remembered0, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld1);
254
test_is_checks_on(null_value, Remembered0, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld1);
255
test_is_checks_on(valid_value, Remembered0, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld0);
256
test_is_checks_on(null_value, Remembered0, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld0);
257
test_is_checks_on(valid_value, Remembered0, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld1);
258
test_is_checks_on(null_value, Remembered0, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld1);
260
test_is_checks_on(valid_value, Remembered0, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld0);
261
test_is_checks_on(null_value, Remembered0, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld0);
262
test_is_checks_on(valid_value, Remembered0, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld1);
263
test_is_checks_on(null_value, Remembered0, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld1);
264
test_is_checks_on(valid_value, Remembered0, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld0);
265
test_is_checks_on(null_value, Remembered0, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld0);
266
test_is_checks_on(valid_value, Remembered0, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld1);
267
test_is_checks_on(null_value, Remembered0, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld1);
269
test_is_checks_on(valid_value, Remembered0, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld0);
270
test_is_checks_on(null_value, Remembered0, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld0);
271
test_is_checks_on(valid_value, Remembered0, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld1);
272
test_is_checks_on(null_value, Remembered0, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld1);
273
test_is_checks_on(valid_value, Remembered0, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld0);
274
test_is_checks_on(null_value, Remembered0, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld0);
275
test_is_checks_on(valid_value, Remembered0, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld1);
276
test_is_checks_on(null_value, Remembered0, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld1);
278
test_is_checks_on(valid_value, Remembered1, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld0);
279
test_is_checks_on(null_value, Remembered1, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld0);
280
test_is_checks_on(valid_value, Remembered1, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld1);
281
test_is_checks_on(null_value, Remembered1, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld1);
282
test_is_checks_on(valid_value, Remembered1, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld0);
283
test_is_checks_on(null_value, Remembered1, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld0);
284
test_is_checks_on(valid_value, Remembered1, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld1);
285
test_is_checks_on(null_value, Remembered1, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld1);
287
test_is_checks_on(valid_value, Remembered1, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld0);
288
test_is_checks_on(null_value, Remembered1, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld0);
289
test_is_checks_on(valid_value, Remembered1, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld1);
290
test_is_checks_on(null_value, Remembered1, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld1);
291
test_is_checks_on(valid_value, Remembered1, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld0);
292
test_is_checks_on(null_value, Remembered1, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld0);
293
test_is_checks_on(valid_value, Remembered1, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld1);
294
test_is_checks_on(null_value, Remembered1, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld1);
296
test_is_checks_on(valid_value, Remembered1, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld0);
297
test_is_checks_on(null_value, Remembered1, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld0);
298
test_is_checks_on(valid_value, Remembered1, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld1);
299
test_is_checks_on(null_value, Remembered1, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld1);
300
test_is_checks_on(valid_value, Remembered1, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld0);
301
test_is_checks_on(null_value, Remembered1, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld0);
302
test_is_checks_on(valid_value, Remembered1, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld1);
303
test_is_checks_on(null_value, Remembered1, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld1);
305
test_is_checks_on(valid_value, Remembered1, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld0);
306
test_is_checks_on(null_value, Remembered1, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld0);
307
test_is_checks_on(valid_value, Remembered1, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld1);
308
test_is_checks_on(null_value, Remembered1, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld1);
309
test_is_checks_on(valid_value, Remembered1, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld0);
310
test_is_checks_on(null_value, Remembered1, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld0);
311
test_is_checks_on(valid_value, Remembered1, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld1);
312
test_is_checks_on(null_value, Remembered1, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld1);
314
test_is_checks_on(valid_value, Remembered11, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld0);
315
test_is_checks_on(null_value, Remembered11, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld0);
316
test_is_checks_on(valid_value, Remembered11, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld1);
317
test_is_checks_on(null_value, Remembered11, RemappedYoung0, RemappedOld0, MarkedYoung0, MarkedOld1);
318
test_is_checks_on(valid_value, Remembered11, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld0);
319
test_is_checks_on(null_value, Remembered11, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld0);
320
test_is_checks_on(valid_value, Remembered11, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld1);
321
test_is_checks_on(null_value, Remembered11, RemappedYoung0, RemappedOld0, MarkedYoung1, MarkedOld1);
323
test_is_checks_on(valid_value, Remembered11, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld0);
324
test_is_checks_on(null_value, Remembered11, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld0);
325
test_is_checks_on(valid_value, Remembered11, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld1);
326
test_is_checks_on(null_value, Remembered11, RemappedYoung0, RemappedOld1, MarkedYoung0, MarkedOld1);
327
test_is_checks_on(valid_value, Remembered11, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld0);
328
test_is_checks_on(null_value, Remembered11, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld0);
329
test_is_checks_on(valid_value, Remembered11, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld1);
330
test_is_checks_on(null_value, Remembered11, RemappedYoung0, RemappedOld1, MarkedYoung1, MarkedOld1);
332
test_is_checks_on(valid_value, Remembered11, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld0);
333
test_is_checks_on(null_value, Remembered11, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld0);
334
test_is_checks_on(valid_value, Remembered11, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld1);
335
test_is_checks_on(null_value, Remembered11, RemappedYoung1, RemappedOld0, MarkedYoung0, MarkedOld1);
336
test_is_checks_on(valid_value, Remembered11, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld0);
337
test_is_checks_on(null_value, Remembered11, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld0);
338
test_is_checks_on(valid_value, Remembered11, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld1);
339
test_is_checks_on(null_value, Remembered11, RemappedYoung1, RemappedOld0, MarkedYoung1, MarkedOld1);
341
test_is_checks_on(valid_value, Remembered11, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld0);
342
test_is_checks_on(null_value, Remembered11, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld0);
343
test_is_checks_on(valid_value, Remembered11, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld1);
344
test_is_checks_on(null_value, Remembered11, RemappedYoung1, RemappedOld1, MarkedYoung0, MarkedOld1);
345
test_is_checks_on(valid_value, Remembered11, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld0);
346
test_is_checks_on(null_value, Remembered11, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld0);
347
test_is_checks_on(valid_value, Remembered11, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld1);
348
test_is_checks_on(null_value, Remembered11, RemappedYoung1, RemappedOld1, MarkedYoung1, MarkedOld1);
350
test_is_checks_on(null_value, Uncolored, Uncolored, Uncolored, Uncolored, Uncolored);
353
static void advance_and_test_young_phase(int& phase, int amount) {
354
for (int i = 0; i < amount; ++i) {
356
ZGlobalsPointers::flip_young_mark_start();
358
ZGlobalsPointers::flip_young_relocate_start();
360
test_is_checks_on_all();
364
static void advance_and_test_old_phase(int& phase, int amount) {
365
for (int i = 0; i < amount; ++i) {
367
ZGlobalsPointers::flip_old_mark_start();
369
ZGlobalsPointers::flip_old_relocate_start();
371
test_is_checks_on_all();
375
static void is_checks() {
379
ZGlobalsPointers::initialize();
380
test_is_checks_on_all();
382
advance_and_test_old_phase(old_phase, 4);
383
advance_and_test_young_phase(young_phase, 4);
385
advance_and_test_old_phase(old_phase, 1);
386
advance_and_test_young_phase(young_phase, 4);
388
advance_and_test_old_phase(old_phase, 1);
389
advance_and_test_young_phase(young_phase, 4);
391
advance_and_test_old_phase(old_phase, 1);
392
advance_and_test_young_phase(young_phase, 4);
394
advance_and_test_old_phase(old_phase, 1);
395
advance_and_test_young_phase(young_phase, 4);
397
advance_and_test_old_phase(old_phase, 1);
398
advance_and_test_young_phase(young_phase, 3);
400
advance_and_test_old_phase(old_phase, 1);
401
advance_and_test_young_phase(young_phase, 3);
403
advance_and_test_old_phase(old_phase, 1);
404
advance_and_test_young_phase(young_phase, 3);
406
advance_and_test_old_phase(old_phase, 1);
407
advance_and_test_young_phase(young_phase, 3);
409
advance_and_test_old_phase(old_phase, 1);
410
advance_and_test_young_phase(young_phase, 2);
412
advance_and_test_old_phase(old_phase, 1);
413
advance_and_test_young_phase(young_phase, 2);
415
advance_and_test_old_phase(old_phase, 1);
416
advance_and_test_young_phase(young_phase, 2);
418
advance_and_test_old_phase(old_phase, 1);
419
advance_and_test_young_phase(young_phase, 2);
421
advance_and_test_old_phase(old_phase, 1);
422
advance_and_test_young_phase(young_phase, 1);
424
advance_and_test_old_phase(old_phase, 1);
425
advance_and_test_young_phase(young_phase, 1);
427
advance_and_test_old_phase(old_phase, 1);
428
advance_and_test_young_phase(young_phase, 1);
430
advance_and_test_old_phase(old_phase, 1);
431
advance_and_test_young_phase(young_phase, 1);
435
TEST_F(ZAddressTest, is_checks) {