Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 20 | #include "art_field-inl.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 21 | #include "base/locks.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 22 | #include "handle_scope.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 23 | #include "jni/jni_internal.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 24 | #include "mirror/class_loader.h" |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 25 | #include "mirror/object-inl.h" |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 26 | #include "mirror/object.h" |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 27 | #include "native/dalvik_system_DexFile.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change-inl.h" |
| 29 | #include "well_known_classes.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 33 | // Returns true if the given class loader derives from BaseDexClassLoader. |
| 34 | inline 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 Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 41 | // Returns true if the given class loader is either a PathClassLoader or a DexClassLoader. |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 42 | // (they both have the same behaviour with respect to class lookup order) |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 43 | inline bool IsPathOrDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 44 | Handle<mirror::ClassLoader> class_loader) |
| 45 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 46 | ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass(); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 47 | 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 Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 54 | // Returns true if the given class loader is an InMemoryDexClassLoader. |
| 55 | inline bool IsInMemoryDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 56 | Handle<mirror::ClassLoader> class_loader) |
| 57 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 58 | ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass(); |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 59 | return (class_loader_class == |
| 60 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_InMemoryDexClassLoader)); |
| 61 | } |
| 62 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 63 | inline bool IsDelegateLastClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 64 | Handle<mirror::ClassLoader> class_loader) |
| 65 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 66 | ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass(); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 67 | return class_loader_class == |
| 68 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader); |
| 69 | } |
| 70 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 71 | // 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! |
| 77 | template <typename Visitor, typename RetType> |
| 78 | inline 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 = |
| 85 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)-> |
| 86 | GetObject(class_loader.Get()); |
| 87 | if (dex_path_list != nullptr) { |
| 88 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 89 | ObjPtr<mirror::Object> dex_elements_obj = |
| 90 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 91 | GetObject(dex_path_list); |
| 92 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 93 | // at the mCookie which is a DexFile vector. |
| 94 | if (dex_elements_obj != nullptr) { |
| 95 | StackHandleScope<1> hs(self); |
| 96 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 97 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 98 | for (auto element : dex_elements.Iterate<mirror::Object>()) { |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 99 | if (element == nullptr) { |
| 100 | // Should never happen, fail. |
| 101 | break; |
| 102 | } |
| 103 | RetType ret_value; |
| 104 | if (!fn(element, &ret_value)) { |
| 105 | return ret_value; |
| 106 | } |
| 107 | } |
| 108 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 109 | } |
| 110 | return defaultReturn; |
| 111 | } |
| 112 | |
| 113 | // Visit the DexFiles in the given classloader with the given visitor. |
| 114 | // Constraints on the visitor: |
| 115 | // * The visitor should return true to continue visiting more DexFiles. |
| 116 | // * The last argument of the visitor is an out argument of RetType. It will be returned |
| 117 | // when the visitor ends the visit (by returning false). |
| 118 | // This function assumes that the given classloader is a subclass of BaseDexClassLoader! |
| 119 | template <typename Visitor, typename RetType> |
| 120 | inline RetType VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa, |
| 121 | Handle<mirror::ClassLoader> class_loader, |
| 122 | Visitor fn, |
| 123 | RetType defaultReturn) |
| 124 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 125 | ArtField* const cookie_field = |
| 126 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 127 | ArtField* const dex_file_field = |
| 128 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 129 | if (dex_file_field == nullptr || cookie_field == nullptr) { |
| 130 | return defaultReturn; |
| 131 | } |
| 132 | auto visit_dex_files = [&](ObjPtr<mirror::Object> element, RetType* ret) |
| 133 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 134 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
| 135 | if (dex_file != nullptr) { |
| 136 | ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
| 137 | if (long_array == nullptr) { |
| 138 | // This should never happen so log a warning. |
| 139 | LOG(WARNING) << "Null DexFile::mCookie"; |
| 140 | *ret = defaultReturn; |
| 141 | return true; |
| 142 | } |
| 143 | int32_t long_array_size = long_array->GetLength(); |
| 144 | // First element is the oat file. |
| 145 | for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) { |
| 146 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 147 | long_array->GetWithoutChecks(j))); |
| 148 | RetType ret_value; |
| 149 | if (!fn(cp_dex_file, /* out */ &ret_value)) { |
| 150 | *ret = ret_value; |
| 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | return true; |
| 156 | }; |
| 157 | |
| 158 | return VisitClassLoaderDexElements(soa, class_loader, visit_dex_files, defaultReturn); |
| 159 | } |
| 160 | |
| 161 | // Simplified version of the above, w/o out argument. |
| 162 | template <typename Visitor> |
| 163 | inline void VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa, |
| 164 | Handle<mirror::ClassLoader> class_loader, |
| 165 | Visitor fn) |
| 166 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | e383d23 | 2018-06-19 12:29:51 -0700 | [diff] [blame] | 167 | auto helper = [&fn](const art::DexFile* dex_file, void** ret) |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 168 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | e383d23 | 2018-06-19 12:29:51 -0700 | [diff] [blame] | 169 | #ifdef __clang_analyzer__ |
| 170 | *ret = nullptr; |
| 171 | #else |
| 172 | UNUSED(ret); |
| 173 | #endif |
| 174 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 175 | return fn(dex_file); |
| 176 | }; |
| 177 | VisitClassLoaderDexFiles<decltype(helper), void*>(soa, |
| 178 | class_loader, |
| 179 | helper, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 180 | /* default= */ nullptr); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 183 | } // namespace art |
| 184 | |
| 185 | #endif // ART_RUNTIME_CLASS_LOADER_UTILS_H_ |