blob: b809bafc7fd93f891dfc74c95d8afefcdfeb29e7 [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"
Andreas Gampeb8e7c372018-02-20 18:24:55 -080026#include "native/dalvik_system_DexFile.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070027#include "scoped_thread_state_change-inl.h"
28#include "well_known_classes.h"
29
30namespace art {
31
32// Returns true if the given class loader is either a PathClassLoader or a DexClassLoader.
David Brazdil05909d82018-12-06 16:25:16 +000033// (they both have the same behaviour with respect to class lookup order)
Andreas Gampeb8e7c372018-02-20 18:24:55 -080034inline bool IsPathOrDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
Calin Juravle57d0acc2017-07-11 17:41:30 -070035 Handle<mirror::ClassLoader> class_loader)
36 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markodfc0de72019-04-01 10:57:55 +010037 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
Calin Juravle57d0acc2017-07-11 17:41:30 -070038 return
39 (class_loader_class ==
40 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)) ||
41 (class_loader_class ==
42 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DexClassLoader));
43}
44
David Brazdil05909d82018-12-06 16:25:16 +000045// Returns true if the given class loader is an InMemoryDexClassLoader.
46inline bool IsInMemoryDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
47 Handle<mirror::ClassLoader> class_loader)
48 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markodfc0de72019-04-01 10:57:55 +010049 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
David Brazdil05909d82018-12-06 16:25:16 +000050 return (class_loader_class ==
51 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_InMemoryDexClassLoader));
52}
53
Andreas Gampeb8e7c372018-02-20 18:24:55 -080054inline bool IsDelegateLastClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
Calin Juravle57d0acc2017-07-11 17:41:30 -070055 Handle<mirror::ClassLoader> class_loader)
56 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markodfc0de72019-04-01 10:57:55 +010057 ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
Calin Juravle57d0acc2017-07-11 17:41:30 -070058 return class_loader_class ==
59 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader);
60}
61
Andreas Gampeb8e7c372018-02-20 18:24:55 -080062// Visit the DexPathList$Element instances in the given classloader with the given visitor.
63// Constraints on the visitor:
64// * The visitor should return true to continue visiting more Elements.
65// * The last argument of the visitor is an out argument of RetType. It will be returned
66// when the visitor ends the visit (by returning false).
67// This function assumes that the given classloader is a subclass of BaseDexClassLoader!
68template <typename Visitor, typename RetType>
69inline RetType VisitClassLoaderDexElements(ScopedObjectAccessAlreadyRunnable& soa,
70 Handle<mirror::ClassLoader> class_loader,
71 Visitor fn,
72 RetType defaultReturn)
73 REQUIRES_SHARED(Locks::mutator_lock_) {
74 Thread* self = soa.Self();
75 ObjPtr<mirror::Object> dex_path_list =
76 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)->
77 GetObject(class_loader.Get());
78 if (dex_path_list != nullptr) {
79 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
80 ObjPtr<mirror::Object> dex_elements_obj =
81 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
82 GetObject(dex_path_list);
83 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
84 // at the mCookie which is a DexFile vector.
85 if (dex_elements_obj != nullptr) {
86 StackHandleScope<1> hs(self);
87 Handle<mirror::ObjectArray<mirror::Object>> dex_elements =
88 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>());
89 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
90 ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i);
91 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 }
101 self->AssertNoPendingException();
102 }
103 return defaultReturn;
104}
105
106// Visit the DexFiles in the given classloader with the given visitor.
107// Constraints on the visitor:
108// * The visitor should return true to continue visiting more DexFiles.
109// * The last argument of the visitor is an out argument of RetType. It will be returned
110// when the visitor ends the visit (by returning false).
111// This function assumes that the given classloader is a subclass of BaseDexClassLoader!
112template <typename Visitor, typename RetType>
113inline RetType VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa,
114 Handle<mirror::ClassLoader> class_loader,
115 Visitor fn,
116 RetType defaultReturn)
117 REQUIRES_SHARED(Locks::mutator_lock_) {
118 ArtField* const cookie_field =
119 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
120 ArtField* const dex_file_field =
121 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
122 if (dex_file_field == nullptr || cookie_field == nullptr) {
123 return defaultReturn;
124 }
125 auto visit_dex_files = [&](ObjPtr<mirror::Object> element, RetType* ret)
126 REQUIRES_SHARED(Locks::mutator_lock_) {
127 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
128 if (dex_file != nullptr) {
129 ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray();
130 if (long_array == nullptr) {
131 // This should never happen so log a warning.
132 LOG(WARNING) << "Null DexFile::mCookie";
133 *ret = defaultReturn;
134 return true;
135 }
136 int32_t long_array_size = long_array->GetLength();
137 // First element is the oat file.
138 for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) {
139 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
140 long_array->GetWithoutChecks(j)));
141 RetType ret_value;
142 if (!fn(cp_dex_file, /* out */ &ret_value)) {
143 *ret = ret_value;
144 return false;
145 }
146 }
147 }
148 return true;
149 };
150
151 return VisitClassLoaderDexElements(soa, class_loader, visit_dex_files, defaultReturn);
152}
153
154// Simplified version of the above, w/o out argument.
155template <typename Visitor>
156inline void VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa,
157 Handle<mirror::ClassLoader> class_loader,
158 Visitor fn)
159 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampee383d232018-06-19 12:29:51 -0700160 auto helper = [&fn](const art::DexFile* dex_file, void** ret)
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800161 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampee383d232018-06-19 12:29:51 -0700162#ifdef __clang_analyzer__
163 *ret = nullptr;
164#else
165 UNUSED(ret);
166#endif
167
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800168 return fn(dex_file);
169 };
170 VisitClassLoaderDexFiles<decltype(helper), void*>(soa,
171 class_loader,
172 helper,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700173 /* default= */ nullptr);
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800174}
175
Calin Juravle57d0acc2017-07-11 17:41:30 -0700176} // namespace art
177
178#endif // ART_RUNTIME_CLASS_LOADER_UTILS_H_