blob: aabc4ca91d26f6d90416f26f3086afa2a92b1866 [file] [log] [blame]
Calin Juravle57d0acc2017-07-11 17:41:30 -07001/*
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
Andreas Gampeb8e7c372018-02-20 18:24:55 -080020#include "art_field-inl.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080021#include "base/locks.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070022#include "handle_scope.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010023#include "jni/jni_internal.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070024#include "mirror/class_loader.h"
Vladimir Markodfc0de72019-04-01 10:57:55 +010025#include "mirror/object-inl.h"
Alex Lighta9bbc082019-11-14 14:51:41 -080026#include "mirror/object.h"
Andreas Gampeb8e7c372018-02-20 18:24:55 -080027#include "native/dalvik_system_DexFile.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070028#include "scoped_thread_state_change-inl.h"
29#include "well_known_classes.h"
30
31namespace art {
32
Dan Zimmermanb682ea42019-12-23 06:59:06 -080033// Returns true if the given class loader derives from BaseDexClassLoader.
34inline bool IsInstanceOfBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
35 Handle<mirror::ClassLoader> class_loader)
36 REQUIRES_SHARED(Locks::mutator_lock_) {
37 return class_loader->InstanceOf(
38 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_BaseDexClassLoader));
39}
40
Calin Juravle57d0acc2017-07-11 17:41:30 -070041// Returns true if the given class loader is either a PathClassLoader or a DexClassLoader.
David Brazdil05909d82018-12-06 16:25:16 +000042// (they both have the same behaviour with respect to class lookup order)
Andreas Gampeb8e7c372018-02-20 18:24:55 -080043inline bool IsPathOrDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
Calin Juravle57d0acc2017-07-11 17:41:30 -070044 Handle<mirror::ClassLoader> class_loader)
45 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markodfc0de72019-04-01 10:57:55 +010046 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
Calin Juravle57d0acc2017-07-11 17:41:30 -070047 return
48 (class_loader_class ==
49 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)) ||
50 (class_loader_class ==
51 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DexClassLoader));
52}
53
David Brazdil05909d82018-12-06 16:25:16 +000054// Returns true if the given class loader is an InMemoryDexClassLoader.
55inline bool IsInMemoryDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
56 Handle<mirror::ClassLoader> class_loader)
57 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markodfc0de72019-04-01 10:57:55 +010058 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
David Brazdil05909d82018-12-06 16:25:16 +000059 return (class_loader_class ==
60 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_InMemoryDexClassLoader));
61}
62
Andreas Gampeb8e7c372018-02-20 18:24:55 -080063inline bool IsDelegateLastClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
Calin Juravle57d0acc2017-07-11 17:41:30 -070064 Handle<mirror::ClassLoader> class_loader)
65 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markodfc0de72019-04-01 10:57:55 +010066 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
Calin Juravle57d0acc2017-07-11 17:41:30 -070067 return class_loader_class ==
68 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader);
69}
70
Andreas Gampeb8e7c372018-02-20 18:24:55 -080071// Visit the DexPathList$Element instances in the given classloader with the given visitor.
72// Constraints on the visitor:
73// * The visitor should return true to continue visiting more Elements.
74// * The last argument of the visitor is an out argument of RetType. It will be returned
75// when the visitor ends the visit (by returning false).
76// This function assumes that the given classloader is a subclass of BaseDexClassLoader!
77template <typename Visitor, typename RetType>
78inline RetType VisitClassLoaderDexElements(ScopedObjectAccessAlreadyRunnable& soa,
79 Handle<mirror::ClassLoader> class_loader,
80 Visitor fn,
81 RetType defaultReturn)
82 REQUIRES_SHARED(Locks::mutator_lock_) {
83 Thread* self = soa.Self();
84 ObjPtr<mirror::Object> dex_path_list =
Vladimír Marko5be5ce72022-10-18 09:49:00 +000085 WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList->GetObject(class_loader.Get());
Andreas Gampeb8e7c372018-02-20 18:24:55 -080086 if (dex_path_list != nullptr) {
87 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
88 ObjPtr<mirror::Object> dex_elements_obj =
Vladimír Marko5be5ce72022-10-18 09:49:00 +000089 WellKnownClasses::dalvik_system_DexPathList_dexElements->GetObject(dex_path_list);
Andreas Gampeb8e7c372018-02-20 18:24:55 -080090 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
91 // at the mCookie which is a DexFile vector.
92 if (dex_elements_obj != nullptr) {
93 StackHandleScope<1> hs(self);
94 Handle<mirror::ObjectArray<mirror::Object>> dex_elements =
95 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>());
Alex Lighta9bbc082019-11-14 14:51:41 -080096 for (auto element : dex_elements.Iterate<mirror::Object>()) {
Andreas Gampeb8e7c372018-02-20 18:24:55 -080097 if (element == nullptr) {
98 // Should never happen, fail.
99 break;
100 }
101 RetType ret_value;
102 if (!fn(element, &ret_value)) {
103 return ret_value;
104 }
105 }
106 }
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800107 }
108 return defaultReturn;
109}
110
111// Visit the DexFiles in the given classloader with the given visitor.
112// Constraints on the visitor:
113// * The visitor should return true to continue visiting more DexFiles.
114// * The last argument of the visitor is an out argument of RetType. It will be returned
115// when the visitor ends the visit (by returning false).
116// This function assumes that the given classloader is a subclass of BaseDexClassLoader!
117template <typename Visitor, typename RetType>
118inline RetType VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa,
119 Handle<mirror::ClassLoader> class_loader,
120 Visitor fn,
121 RetType defaultReturn)
122 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimír Marko5be5ce72022-10-18 09:49:00 +0000123 ArtField* const cookie_field = WellKnownClasses::dalvik_system_DexFile_cookie;
124 ArtField* const dex_file_field = WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800125 if (dex_file_field == nullptr || cookie_field == nullptr) {
126 return defaultReturn;
127 }
128 auto visit_dex_files = [&](ObjPtr<mirror::Object> element, RetType* ret)
129 REQUIRES_SHARED(Locks::mutator_lock_) {
130 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
131 if (dex_file != nullptr) {
132 ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray();
133 if (long_array == nullptr) {
134 // This should never happen so log a warning.
135 LOG(WARNING) << "Null DexFile::mCookie";
136 *ret = defaultReturn;
137 return true;
138 }
139 int32_t long_array_size = long_array->GetLength();
140 // First element is the oat file.
141 for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) {
142 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
143 long_array->GetWithoutChecks(j)));
144 RetType ret_value;
145 if (!fn(cp_dex_file, /* out */ &ret_value)) {
146 *ret = ret_value;
147 return false;
148 }
149 }
150 }
151 return true;
152 };
153
154 return VisitClassLoaderDexElements(soa, class_loader, visit_dex_files, defaultReturn);
155}
156
157// Simplified version of the above, w/o out argument.
158template <typename Visitor>
159inline void VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa,
160 Handle<mirror::ClassLoader> class_loader,
161 Visitor fn)
162 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampee383d232018-06-19 12:29:51 -0700163 auto helper = [&fn](const art::DexFile* dex_file, void** ret)
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800164 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampee383d232018-06-19 12:29:51 -0700165#ifdef __clang_analyzer__
166 *ret = nullptr;
167#else
168 UNUSED(ret);
169#endif
170
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800171 return fn(dex_file);
172 };
173 VisitClassLoaderDexFiles<decltype(helper), void*>(soa,
174 class_loader,
175 helper,
Stefano Cianciulli9b73d5d2022-06-20 13:49:35 +0000176 /* defaultReturn= */ nullptr);
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800177}
178
Calin Juravle57d0acc2017-07-11 17:41:30 -0700179} // namespace art
180
181#endif // ART_RUNTIME_CLASS_LOADER_UTILS_H_