1 /*
2 * Copyright (C) 2017 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 #ifndef ART_RUNTIME_CLASS_LOADER_UTILS_H_
18 #define ART_RUNTIME_CLASS_LOADER_UTILS_H_
19
20 #include "art_field-inl.h"
21 #include "base/locks.h"
22 #include "handle_scope.h"
23 #include "jni/jni_internal.h"
24 #include "mirror/class_loader.h"
25 #include "mirror/object-inl.h"
26 #include "mirror/object.h"
27 #include "native/dalvik_system_DexFile.h"
28 #include "scoped_thread_state_change-inl.h"
29 #include "well_known_classes-inl.h"
30
31 namespace art {
32
33 // Returns true if the given class loader derives from BaseDexClassLoader.
IsInstanceOfBaseDexClassLoader(Handle<mirror::ClassLoader> class_loader)34 inline bool IsInstanceOfBaseDexClassLoader(Handle<mirror::ClassLoader> class_loader)
35 REQUIRES_SHARED(Locks::mutator_lock_) {
36 return class_loader->InstanceOf(WellKnownClasses::dalvik_system_BaseDexClassLoader.Get());
37 }
38
39 // Returns true if the given class loader is either a PathClassLoader or a DexClassLoader.
40 // (they both have the same behaviour with respect to class lookup order)
IsPathOrDexClassLoader(Handle<mirror::ClassLoader> class_loader)41 inline bool IsPathOrDexClassLoader(Handle<mirror::ClassLoader> class_loader)
42 REQUIRES_SHARED(Locks::mutator_lock_) {
43 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
44 return (class_loader_class == WellKnownClasses::dalvik_system_PathClassLoader) ||
45 (class_loader_class == WellKnownClasses::dalvik_system_DexClassLoader);
46 }
47
48 // Returns true if the given class loader is an InMemoryDexClassLoader.
IsInMemoryDexClassLoader(Handle<mirror::ClassLoader> class_loader)49 inline bool IsInMemoryDexClassLoader(Handle<mirror::ClassLoader> class_loader)
50 REQUIRES_SHARED(Locks::mutator_lock_) {
51 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
52 return (class_loader_class == WellKnownClasses::dalvik_system_InMemoryDexClassLoader);
53 }
54
IsDelegateLastClassLoader(Handle<mirror::ClassLoader> class_loader)55 inline bool IsDelegateLastClassLoader(Handle<mirror::ClassLoader> class_loader)
56 REQUIRES_SHARED(Locks::mutator_lock_) {
57 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
58 return class_loader_class == WellKnownClasses::dalvik_system_DelegateLastClassLoader;
59 }
60
61 // Visit the DexPathList$Element instances in the given classloader with the given visitor.
62 // Constraints on the visitor:
63 // * The visitor should return true to continue visiting more Elements.
64 // * The last argument of the visitor is an out argument of RetType. It will be returned
65 // when the visitor ends the visit (by returning false).
66 // This function assumes that the given classloader is a subclass of BaseDexClassLoader!
67 template <typename Visitor, typename RetType>
VisitClassLoaderDexElements(Thread * self,Handle<mirror::ClassLoader> class_loader,Visitor fn,RetType defaultReturn)68 inline RetType VisitClassLoaderDexElements(Thread* self,
69 Handle<mirror::ClassLoader> class_loader,
70 Visitor fn,
71 RetType defaultReturn)
72 REQUIRES_SHARED(Locks::mutator_lock_) {
73 ObjPtr<mirror::Object> dex_path_list =
74 WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList->GetObject(class_loader.Get());
75 if (dex_path_list != nullptr) {
76 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
77 ObjPtr<mirror::Object> dex_elements_obj =
78 WellKnownClasses::dalvik_system_DexPathList_dexElements->GetObject(dex_path_list);
79 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
80 // at the mCookie which is a DexFile vector.
81 if (dex_elements_obj != nullptr) {
82 StackHandleScope<1> hs(self);
83 Handle<mirror::ObjectArray<mirror::Object>> dex_elements =
84 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>());
85 for (auto element : dex_elements.Iterate<mirror::Object>()) {
86 if (element == nullptr) {
87 // Should never happen, fail.
88 break;
89 }
90 RetType ret_value;
91 if (!fn(element, &ret_value)) {
92 return ret_value;
93 }
94 }
95 }
96 }
97 return defaultReturn;
98 }
99
100 // Visit the DexFiles in the given classloader with the given visitor.
101 // Constraints on the visitor:
102 // * The visitor should return true to continue visiting more DexFiles.
103 // * The last argument of the visitor is an out argument of RetType. It will be returned
104 // when the visitor ends the visit (by returning false).
105 // This function assumes that the given classloader is a subclass of BaseDexClassLoader!
106 template <typename Visitor, typename RetType>
VisitClassLoaderDexFiles(Thread * self,Handle<mirror::ClassLoader> class_loader,Visitor fn,RetType defaultReturn)107 inline RetType VisitClassLoaderDexFiles(Thread* self,
108 Handle<mirror::ClassLoader> class_loader,
109 Visitor fn,
110 RetType defaultReturn)
111 REQUIRES_SHARED(Locks::mutator_lock_) {
112 ArtField* const cookie_field = WellKnownClasses::dalvik_system_DexFile_cookie;
113 ArtField* const dex_file_field = WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
114 if (dex_file_field == nullptr || cookie_field == nullptr) {
115 return defaultReturn;
116 }
117 auto visit_dex_files = [&](ObjPtr<mirror::Object> element, RetType* ret)
118 REQUIRES_SHARED(Locks::mutator_lock_) {
119 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
120 if (dex_file != nullptr) {
121 StackHandleScope<1> hs(self);
122 Handle<mirror::LongArray> long_array =
123 hs.NewHandle(cookie_field->GetObject(dex_file)->AsLongArray());
124 if (long_array == nullptr) {
125 // This should never happen so log a warning.
126 LOG(WARNING) << "Null DexFile::mCookie";
127 *ret = defaultReturn;
128 return true;
129 }
130 int32_t long_array_size = long_array->GetLength();
131 // First element is the oat file.
132 for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) {
133 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
134 long_array->GetWithoutChecks(j)));
135 RetType ret_value;
136 if (!fn(cp_dex_file, /* out */ &ret_value)) {
137 *ret = ret_value;
138 return false;
139 }
140 }
141 }
142 return true;
143 };
144
145 return VisitClassLoaderDexElements(self, class_loader, visit_dex_files, defaultReturn);
146 }
147
148 // Simplified version of the above, w/o out argument.
149 template <typename Visitor>
VisitClassLoaderDexFiles(Thread * self,Handle<mirror::ClassLoader> class_loader,Visitor fn)150 inline void VisitClassLoaderDexFiles(Thread* self,
151 Handle<mirror::ClassLoader> class_loader,
152 Visitor fn)
153 REQUIRES_SHARED(Locks::mutator_lock_) {
154 auto helper = [&fn](const art::DexFile* dex_file, void** ret)
155 REQUIRES_SHARED(Locks::mutator_lock_) {
156 #ifdef __clang_analyzer__
157 *ret = nullptr;
158 #else
159 UNUSED(ret);
160 #endif
161
162 return fn(dex_file);
163 };
164 VisitClassLoaderDexFiles<decltype(helper), void*>(self,
165 class_loader,
166 helper,
167 /* defaultReturn= */ nullptr);
168 }
169
170 } // namespace art
171
172 #endif // ART_RUNTIME_CLASS_LOADER_UTILS_H_
173