1/*
2 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24#include "precompiled.hpp"
25#include "gc/z/zAddress.inline.hpp"
26#include "gc/z/zGlobals.hpp"
27#include "unittest.hpp"
28
29class ZAddressTest : public ::testing::Test {
30protected:
31 static void is_good_bit(uintptr_t bit_mask) {
32 // Setup
33 ZAddress::initialize();
34 ZAddress::set_good_mask(bit_mask);
35
36 // Test that a pointer with only the given bit is considered good.
37 EXPECT_EQ(ZAddress::is_good(ZAddressMetadataMarked0), (bit_mask == ZAddressMetadataMarked0));
38 EXPECT_EQ(ZAddress::is_good(ZAddressMetadataMarked1), (bit_mask == ZAddressMetadataMarked1));
39 EXPECT_EQ(ZAddress::is_good(ZAddressMetadataRemapped), (bit_mask == ZAddressMetadataRemapped));
40
41 // Test that a pointer with the given bit and some extra bits is considered good.
42 EXPECT_EQ(ZAddress::is_good(ZAddressMetadataMarked0 | 0x8),(bit_mask == ZAddressMetadataMarked0));
43 EXPECT_EQ(ZAddress::is_good(ZAddressMetadataMarked1 | 0x8), (bit_mask == ZAddressMetadataMarked1));
44 EXPECT_EQ(ZAddress::is_good(ZAddressMetadataRemapped | 0x8), (bit_mask == ZAddressMetadataRemapped));
45
46 // Test that null is not considered good.
47 EXPECT_FALSE(ZAddress::is_good(0));
48 }
49
50 static void is_good_or_null_bit(uintptr_t bit_mask) {
51 // Setup
52 ZAddress::initialize();
53 ZAddress::set_good_mask(bit_mask);
54
55 // Test that a pointer with only the given bit is considered good.
56 EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataMarked0), (bit_mask == ZAddressMetadataMarked0));
57 EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataMarked1), (bit_mask == ZAddressMetadataMarked1));
58 EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataRemapped), (bit_mask == ZAddressMetadataRemapped));
59
60 // Test that a pointer with the given bit and some extra bits is considered good.
61 EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataMarked0 | 0x8), (bit_mask == ZAddressMetadataMarked0));
62 EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataMarked1 | 0x8), (bit_mask == ZAddressMetadataMarked1));
63 EXPECT_EQ(ZAddress::is_good_or_null(ZAddressMetadataRemapped | 0x8), (bit_mask == ZAddressMetadataRemapped));
64
65 // Test that null is considered good_or_null.
66 EXPECT_TRUE(ZAddress::is_good_or_null(0));
67 }
68
69 static void finalizable() {
70 // Setup
71 ZAddress::initialize();
72 ZAddress::flip_to_marked();
73
74 // Test that a normal good pointer is good and weak good, but not finalizable
75 const uintptr_t addr1 = ZAddress::good(1);
76 EXPECT_FALSE(ZAddress::is_finalizable(addr1));
77 EXPECT_TRUE(ZAddress::is_marked(addr1));
78 EXPECT_FALSE(ZAddress::is_remapped(addr1));
79 EXPECT_TRUE(ZAddress::is_weak_good(addr1));
80 EXPECT_TRUE(ZAddress::is_weak_good_or_null(addr1));
81 EXPECT_TRUE(ZAddress::is_good(addr1));
82 EXPECT_TRUE(ZAddress::is_good_or_null(addr1));
83
84 // Test that a finalizable good pointer is finalizable and weak good, but not good
85 const uintptr_t addr2 = ZAddress::finalizable_good(1);
86 EXPECT_TRUE(ZAddress::is_finalizable(addr2));
87 EXPECT_TRUE(ZAddress::is_marked(addr2));
88 EXPECT_FALSE(ZAddress::is_remapped(addr2));
89 EXPECT_TRUE(ZAddress::is_weak_good(addr2));
90 EXPECT_TRUE(ZAddress::is_weak_good_or_null(addr2));
91 EXPECT_FALSE(ZAddress::is_good(addr2));
92 EXPECT_FALSE(ZAddress::is_good_or_null(addr2));
93
94 // Flip to remapped and test that it's no longer weak good
95 ZAddress::flip_to_remapped();
96 EXPECT_TRUE(ZAddress::is_finalizable(addr2));
97 EXPECT_TRUE(ZAddress::is_marked(addr2));
98 EXPECT_FALSE(ZAddress::is_remapped(addr2));
99 EXPECT_FALSE(ZAddress::is_weak_good(addr2));
100 EXPECT_FALSE(ZAddress::is_weak_good_or_null(addr2));
101 EXPECT_FALSE(ZAddress::is_good(addr2));
102 EXPECT_FALSE(ZAddress::is_good_or_null(addr2));
103 }
104};
105
106TEST_F(ZAddressTest, is_good) {
107 is_good_bit(ZAddressMetadataMarked0);
108 is_good_bit(ZAddressMetadataMarked1);
109 is_good_bit(ZAddressMetadataRemapped);
110}
111
112TEST_F(ZAddressTest, is_good_or_null) {
113 is_good_or_null_bit(ZAddressMetadataMarked0);
114 is_good_or_null_bit(ZAddressMetadataMarked1);
115 is_good_or_null_bit(ZAddressMetadataRemapped);
116}
117
118TEST_F(ZAddressTest, is_weak_good_or_null) {
119#define check_is_weak_good_or_null(value) \
120 EXPECT_EQ(ZAddress::is_weak_good_or_null(value), \
121 (ZAddress::is_good_or_null(value) || ZAddress::is_remapped(value))) \
122 << "is_good_or_null: " << ZAddress::is_good_or_null(value) \
123 << " is_remaped: " << ZAddress::is_remapped(value) \
124 << " is_good_or_null_or_remapped: " << ZAddress::is_weak_good_or_null(value)
125
126 check_is_weak_good_or_null((uintptr_t)NULL);
127 check_is_weak_good_or_null(ZAddressMetadataMarked0);
128 check_is_weak_good_or_null(ZAddressMetadataMarked1);
129 check_is_weak_good_or_null(ZAddressMetadataRemapped);
130 check_is_weak_good_or_null((uintptr_t)0x123);
131}
132
133TEST_F(ZAddressTest, finalizable) {
134 finalizable();
135}
136