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