1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "register_line-inl.h"
18
19 #include "common_runtime_test.h"
20 #include "method_verifier.h"
21 #include "reg_type_cache-inl.h"
22 #include "reg_type_test_utils.h"
23
24 namespace art HIDDEN {
25 namespace verifier {
26
27 class RegisterLineTest : public CommonRuntimeTest {
28 protected:
RegisterLineTest()29 RegisterLineTest() {
30 use_boot_image_ = true; // Make the Runtime creation cheaper.
31 }
32
CreateVerifier(Thread * self,RegTypeCache * reg_types,Handle<mirror::DexCache> dex_cache,ArtMethod * method)33 MethodVerifier* CreateVerifier(Thread* self,
34 RegTypeCache* reg_types,
35 Handle<mirror::DexCache> dex_cache,
36 ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
37 return MethodVerifier::CreateVerifier(
38 self,
39 reg_types,
40 /*verifier_deps=*/ nullptr,
41 dex_cache,
42 *method->GetDeclaringClass()->GetClassDef(),
43 method->GetCodeItem(),
44 method->GetDexMethodIndex(),
45 method->GetAccessFlags(),
46 /*verify_to_dump=*/ false,
47 /*api_level=*/ 0u);
48 }
49
GetArenaAllocator(MethodVerifier * verifier)50 ArenaAllocator& GetArenaAllocator(MethodVerifier* verifier) {
51 return verifier->allocator_;
52 }
53 };
54
TEST_F(RegisterLineTest,NewInstanceDexPcsMerging)55 TEST_F(RegisterLineTest, NewInstanceDexPcsMerging) {
56 ArenaPool* arena_pool = Runtime::Current()->GetArenaPool();
57 ScopedObjectAccess soa(Thread::Current());
58 StackHandleScope<2u> hs(soa.Self());
59 Handle<mirror::Class> object_class = hs.NewHandle(GetClassRoot<mirror::Object>());
60 Handle<mirror::DexCache> dex_cache = hs.NewHandle(object_class->GetDexCache());
61 const DexFile* dex_file = dex_cache->GetDexFile();
62 ScopedNullHandle<mirror::ClassLoader> loader;
63 RegTypeCache reg_types(soa.Self(), class_linker_, arena_pool, loader, dex_file);
64 ArtMethod* method = object_class->FindClassMethod("wait", "()V", kRuntimePointerSize);
65 ASSERT_TRUE(method != nullptr);
66 std::unique_ptr<MethodVerifier> verifier(
67 CreateVerifier(soa.Self(), ®_types, dex_cache, method));
68 const RegType& resolved_type1 = reg_types.FromDescriptor("Ljava/lang/Object;");
69 const RegType& resolved_type2 = reg_types.FromDescriptor("Ljava/lang/String;");
70 const RegType& unresolved_type1 = reg_types.FromDescriptor("Ljava/lang/DoesNotExist;");
71 const RegType& unresolved_type2 = reg_types.FromDescriptor("Ljava/lang/DoesNotExistEither;");
72 const RegType& uninit_resolved_type1 = reg_types.Uninitialized(resolved_type1);
73 const RegType& uninit_resolved_type2 = reg_types.Uninitialized(resolved_type2);
74 const RegType& uninit_unresolved_type1 = reg_types.Uninitialized(unresolved_type1);
75 const RegType& uninit_unresolved_type2 = reg_types.Uninitialized(unresolved_type2);
76 const RegType& conflict = reg_types.Conflict();
77
78 struct TestCase {
79 const RegType& reg_type1;
80 uint32_t dex_pc1;
81 const RegType& reg_type2;
82 uint32_t dex_pc2;
83 const RegType& expected;
84 };
85 const TestCase test_cases[] = {
86 // Merge the same uninitialized type and allocation dex pc.
87 {uninit_resolved_type1, 1u, uninit_resolved_type1, 1u, uninit_resolved_type1},
88 {uninit_resolved_type2, 1u, uninit_resolved_type2, 1u, uninit_resolved_type2},
89 {uninit_unresolved_type1, 1u, uninit_unresolved_type1, 1u, uninit_unresolved_type1},
90 {uninit_unresolved_type2, 1u, uninit_unresolved_type2, 1u, uninit_unresolved_type2},
91 // Merge the same uninitialized type and different allocation dex pcs.
92 {uninit_resolved_type1, 1u, uninit_resolved_type1, 2u, conflict},
93 {uninit_resolved_type2, 1u, uninit_resolved_type2, 2u, conflict},
94 {uninit_unresolved_type1, 1u, uninit_unresolved_type1, 2u, conflict},
95 {uninit_unresolved_type2, 1u, uninit_unresolved_type2, 2u, conflict},
96 // Merge different uninitialized types and the same allocation dex pc.
97 {uninit_resolved_type1, 1u, uninit_resolved_type2, 1u, conflict},
98 {uninit_resolved_type1, 1u, uninit_unresolved_type1, 1u, conflict},
99 {uninit_resolved_type1, 1u, uninit_unresolved_type2, 1u, conflict},
100 {uninit_resolved_type2, 1u, uninit_resolved_type1, 1u, conflict},
101 {uninit_resolved_type2, 1u, uninit_unresolved_type1, 1u, conflict},
102 {uninit_resolved_type2, 1u, uninit_unresolved_type2, 1u, conflict},
103 {uninit_unresolved_type1, 1u, uninit_resolved_type1, 1u, conflict},
104 {uninit_unresolved_type1, 1u, uninit_resolved_type2, 1u, conflict},
105 {uninit_unresolved_type1, 1u, uninit_unresolved_type2, 1u, conflict},
106 {uninit_unresolved_type2, 1u, uninit_resolved_type1, 1u, conflict},
107 {uninit_unresolved_type2, 1u, uninit_resolved_type2, 1u, conflict},
108 {uninit_unresolved_type2, 1u, uninit_unresolved_type1, 1u, conflict},
109 // Merge uninitialized types with their initialized counterparts.
110 {uninit_resolved_type1, 1u, resolved_type1, 1u, conflict},
111 {uninit_resolved_type2, 1u, resolved_type2, 1u, conflict},
112 {uninit_unresolved_type1, 1u, unresolved_type1, 1u, conflict},
113 {uninit_unresolved_type2, 1u, unresolved_type2, 1u, conflict},
114 {resolved_type1, 1u, uninit_resolved_type1, 1u, conflict},
115 {resolved_type2, 1u, uninit_resolved_type2, 1u, conflict},
116 {unresolved_type1, 1u, uninit_unresolved_type1, 1u, conflict},
117 {unresolved_type2, 1u, uninit_unresolved_type2, 1u, conflict},
118 };
119
120 constexpr size_t kNumRegs = 1u;
121 constexpr uint32_t kVReg = 0u;
122 ArenaAllocator& allocator = GetArenaAllocator(verifier.get());
123 RegisterLineArenaUniquePtr line1(RegisterLine::Create(kNumRegs, allocator, ®_types));
124 RegisterLineArenaUniquePtr line2(RegisterLine::Create(kNumRegs, allocator, ®_types));
125 for (const TestCase& test_case : test_cases) {
126 ASSERT_TRUE(test_case.reg_type1.IsUninitializedTypes() ||
127 test_case.reg_type2.IsUninitializedTypes());
128 auto set_reg_type_and_dex_pc = [&](RegisterLine* line,
129 const RegType& reg_type,
130 uint32_t dex_pc,
131 const RegType& other_reg_type)
132 REQUIRES_SHARED(Locks::mutator_lock_) {
133 if (reg_type.IsUninitializedTypes()) {
134 line->SetRegisterTypeForNewInstance(kVReg, reg_type, dex_pc);
135 } else {
136 // Initialize the allocation dex pc using the `other_reg_type`, then set the `reg_type`.
137 line->SetRegisterTypeForNewInstance(kVReg, other_reg_type, dex_pc);
138 line->SetRegisterType<LockOp::kClear>(kVReg, reg_type);
139 }
140 };
141 set_reg_type_and_dex_pc(
142 line1.get(), test_case.reg_type1, test_case.dex_pc1, test_case.reg_type2);
143 set_reg_type_and_dex_pc(
144 line2.get(), test_case.reg_type2, test_case.dex_pc2, test_case.reg_type1);
145 line1->MergeRegisters(verifier.get(), line2.get());
146 const RegType& result = line1->GetRegisterType(verifier.get(), kVReg);
147 ASSERT_TRUE(result.Equals(test_case.expected))
148 << RegTypeWrapper(test_case.reg_type1) << " @" << test_case.dex_pc1 << " merge with "
149 << RegTypeWrapper(test_case.reg_type2) << " @" << test_case.dex_pc2 << " yielded "
150 << RegTypeWrapper(result) << " but we expected " << RegTypeWrapper(test_case.expected);
151 }
152 }
153
154 } // namespace verifier
155 } // namespace art
156