xref: /aosp_15_r20/art/runtime/imtable_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2011 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 "imtable-inl.h"
18 
19 #include <memory>
20 #include <string>
21 
22 #include "jni.h"
23 
24 #include "base/mutex.h"
25 #include "class_linker.h"
26 #include "common_runtime_test.h"
27 #include "handle_scope-inl.h"
28 #include "mirror/accessible_object.h"
29 #include "mirror/class.h"
30 #include "mirror/class_loader.h"
31 #include "scoped_thread_state_change-inl.h"
32 #include "thread-current-inl.h"
33 
34 namespace art HIDDEN {
35 
36 class ImTableTest : public CommonRuntimeTest {
37  public:
LoadClasses(const std::string & class_name)38   std::pair<mirror::Class*, mirror::Class*> LoadClasses(const std::string& class_name)
39       REQUIRES_SHARED(Locks::mutator_lock_) {
40     jobject jclass_loader_a = LoadDex("IMTA");
41     CHECK(jclass_loader_a != nullptr);
42     jobject jclass_loader_b = LoadDex("IMTB");
43     CHECK(jclass_loader_b != nullptr);
44 
45     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
46     Thread* self = Thread::Current();
47 
48     StackHandleScope<3> hs(self);
49     MutableHandle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>(nullptr);
50 
51     // A.
52     h_class_loader.Assign(
53         ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(jclass_loader_a)));
54     Handle<mirror::Class> h_class_a = hs.NewHandle(FindClass(class_name.c_str(), h_class_loader));
55     if (h_class_a == nullptr) {
56       LOG(ERROR) << self->GetException()->Dump();
57       CHECK(false) << "h_class_a == nullptr";
58     }
59 
60     // B.
61     h_class_loader.Assign(
62         ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(jclass_loader_b)));
63     Handle<mirror::Class> h_class_b = hs.NewHandle(FindClass(class_name.c_str(), h_class_loader));
64     if (h_class_b == nullptr) {
65       LOG(ERROR) << self->GetException()->Dump();
66       CHECK(false) << "h_class_b == nullptr";
67     }
68 
69     return std::make_pair(h_class_a.Get(), h_class_b.Get());
70   }
71 
LoadMethods(const std::string & class_name,const std::string & method_name)72   std::pair<ArtMethod*, ArtMethod*> LoadMethods(const std::string& class_name,
73                                                 const std::string& method_name)
74       REQUIRES_SHARED(Locks::mutator_lock_) {
75     std::pair<mirror::Class*, mirror::Class*> classes = LoadClasses(class_name);
76 
77     const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
78 
79     ArtMethod* method_a =
80         classes.first->FindDeclaredVirtualMethodByName(method_name, pointer_size);
81     ArtMethod* method_b =
82         classes.second->FindDeclaredVirtualMethodByName(method_name, pointer_size);
83 
84     return std::make_pair(method_a, method_b);
85   }
86 };
87 
TEST_F(ImTableTest,NewMethodBefore)88 TEST_F(ImTableTest, NewMethodBefore) {
89   ScopedObjectAccess soa(Thread::Current());
90 
91   std::pair<ArtMethod*, ArtMethod*> methods = LoadMethods("LInterfaces$A;", "foo");
92   CHECK_EQ(ImTable::GetImtIndex(methods.first), ImTable::GetImtIndex(methods.second));
93 }
94 
TEST_F(ImTableTest,NewClassBefore)95 TEST_F(ImTableTest, NewClassBefore) {
96   ScopedObjectAccess soa(Thread::Current());
97 
98   std::pair<ArtMethod*, ArtMethod*> methods = LoadMethods("LInterfaces$Z;", "foo");
99   CHECK_EQ(ImTable::GetImtIndex(methods.first), ImTable::GetImtIndex(methods.second));
100 }
101 
102 }  // namespace art
103