blob: 1c353604fa53ba648cff4a4ed229effc54fa0577 [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
33// Returns true if the given class loader is either a PathClassLoader or a DexClassLoader.
David Brazdil05909d82018-12-06 16:25:16 +000034// (they both have the same behaviour with respect to class lookup order)
Andreas Gampeb8e7c372018-02-20 18:24:55 -080035inline bool IsPathOrDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
Calin Juravle57d0acc2017-07-11 17:41:30 -070036 Handle<mirror::ClassLoader> class_loader)
37 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markodfc0de72019-04-01 10:57:55 +010038 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
Calin Juravle57d0acc2017-07-11 17:41:30 -070039 return
40 (class_loader_class ==
41 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)) ||
42 (class_loader_class ==
43 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DexClassLoader));
44}
45
David Brazdil05909d82018-12-06 16:25:16 +000046// Returns true if the given class loader is an InMemoryDexClassLoader.
47inline bool IsInMemoryDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
48 Handle<mirror::ClassLoader> class_loader)
49 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markodfc0de72019-04-01 10:57:55 +010050 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
David Brazdil05909d82018-12-06 16:25:16 +000051 return (class_loader_class ==
52 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_InMemoryDexClassLoader));
53}
54
Andreas Gampeb8e7c372018-02-20 18:24:55 -080055inline bool IsDelegateLastClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
Calin Juravle57d0acc2017-07-11 17:41:30 -070056 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();
Calin Juravle57d0acc2017-07-11 17:41:30 -070059 return class_loader_class ==
60 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader);
61}
62
Andreas Gampeb8e7c372018-02-20 18:24:55 -080063// Visit the DexPathList$Element instances in the given classloader with the given visitor.
64// Constraints on the visitor:
65// * The visitor should return true to continue visiting more Elements.
66// * The last argument of the visitor is an out argument of RetType. It will be returned
67// when the visitor ends the visit (by returning false).
68// This function assumes that the given classloader is a subclass of BaseDexClassLoader!
69template <typename Visitor, typename RetType>
70inline RetType VisitClassLoaderDexElements(ScopedObjectAccessAlreadyRunnable& soa,
71 Handle<mirror::ClassLoader> class_loader,
72 Visitor fn,
73 RetType defaultReturn)
74 REQUIRES_SHARED(Locks::mutator_lock_) {
75 Thread* self = soa.Self();
76 ObjPtr<mirror::Object> dex_path_list =
77 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)->
78 GetObject(class_loader.Get());
79 if (dex_path_list != nullptr) {
80 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
81 ObjPtr<mirror::Object> dex_elements_obj =
82 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
83 GetObject(dex_path_list);
84 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
85 // at the mCookie which is a DexFile vector.
86 if (dex_elements_obj != nullptr) {
87 StackHandleScope<1> hs(self);
88 Handle<mirror::ObjectArray<mirror::Object>> dex_elements =
89 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>());
Alex Lighta9bbc082019-11-14 14:51:41 -080090 for (auto element : dex_elements.Iterate<mirror::Object>()) {
Andreas Gampeb8e7c372018-02-20 18:24:55 -080091 if (element == nullptr) {
92 // Should never happen, fail.
93 break;
94 }
95 RetType ret_value;
96 if (!fn(element, &ret_value)) {
97 return ret_value;
98 }
99 }
100 }
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800101 }
102 return defaultReturn;
103}
104
105// Visit the DexFiles in the given classloader with the given visitor.
106// Constraints on the visitor:
107// * The visitor should return true to continue visiting more DexFiles.
108// * The last argument of the visitor is an out argument of RetType. It will be returned
109// when the visitor ends the visit (by returning false).
110// This function assumes that the given classloader is a subclass of BaseDexClassLoader!
111template <typename Visitor, typename RetType>
112inline RetType VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa,
113 Handle<mirror::ClassLoader> class_loader,
114 Visitor fn,
115 RetType defaultReturn)
116 REQUIRES_SHARED(Locks::mutator_lock_) {
117 ArtField* const cookie_field =
118 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
119 ArtField* const dex_file_field =
120 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
121 if (dex_file_field == nullptr || cookie_field == nullptr) {
122 return defaultReturn;
123 }
124 auto visit_dex_files = [&](ObjPtr<mirror::Object> element, RetType* ret)
125 REQUIRES_SHARED(Locks::mutator_lock_) {
126 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
127 if (dex_file != nullptr) {
128 ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray();
129 if (long_array == nullptr) {
130 // This should never happen so log a warning.
131 LOG(WARNING) << "Null DexFile::mCookie";
132 *ret = defaultReturn;
133 return true;
134 }
135 int32_t long_array_size = long_array->GetLength();
136 // First element is the oat file.
137 for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) {
138 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
139 long_array->GetWithoutChecks(j)));
140 RetType ret_value;
141 if (!fn(cp_dex_file, /* out */ &ret_value)) {
142 *ret = ret_value;
143 return false;
144 }
145 }
146 }
147 return true;
148 };
149
150 return VisitClassLoaderDexElements(soa, class_loader, visit_dex_files, defaultReturn);
151}
152
153// Simplified version of the above, w/o out argument.
154template <typename Visitor>
155inline void VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa,
156 Handle<mirror::ClassLoader> class_loader,
157 Visitor fn)
158 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampee383d232018-06-19 12:29:51 -0700159 auto helper = [&fn](const art::DexFile* dex_file, void** ret)
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800160 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampee383d232018-06-19 12:29:51 -0700161#ifdef __clang_analyzer__
162 *ret = nullptr;
163#else
164 UNUSED(ret);
165#endif
166
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800167 return fn(dex_file);
168 };
169 VisitClassLoaderDexFiles<decltype(helper), void*>(soa,
170 class_loader,
171 helper,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700172 /* default= */ nullptr);
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800173}
174
Calin Juravle57d0acc2017-07-11 17:41:30 -0700175} // namespace art
176
177#endif // ART_RUNTIME_CLASS_LOADER_UTILS_H_