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 "native/dalvik_system_DexFile.h"
27 #include "scoped_thread_state_change-inl.h"
28 #include "well_known_classes.h"
29 
30 namespace art {
31 
32 // Returns true if the given class loader is either a PathClassLoader or a DexClassLoader.
33 // (they both have the same behaviour with respect to class lookup order)
IsPathOrDexClassLoader(ScopedObjectAccessAlreadyRunnable & soa,Handle<mirror::ClassLoader> class_loader)34 inline bool IsPathOrDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
35                                    Handle<mirror::ClassLoader> class_loader)
36     REQUIRES_SHARED(Locks::mutator_lock_) {
37   ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
38   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 
45 // Returns true if the given class loader is an InMemoryDexClassLoader.
IsInMemoryDexClassLoader(ScopedObjectAccessAlreadyRunnable & soa,Handle<mirror::ClassLoader> class_loader)46 inline bool IsInMemoryDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
47                                      Handle<mirror::ClassLoader> class_loader)
48     REQUIRES_SHARED(Locks::mutator_lock_) {
49   ObjPtr<mirror::Class> class_loader_class = class_loader->GetClass();
50   return (class_loader_class ==
51       soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_InMemoryDexClassLoader));
52 }
53 
IsDelegateLastClassLoader(ScopedObjectAccessAlreadyRunnable & soa,Handle<mirror::ClassLoader> class_loader)54 inline bool IsDelegateLastClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
55                                       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 ==
59       soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader);
60 }
61 
62 // 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!
68 template <typename Visitor, typename RetType>
VisitClassLoaderDexElements(ScopedObjectAccessAlreadyRunnable & soa,Handle<mirror::ClassLoader> class_loader,Visitor fn,RetType defaultReturn)69 inline 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   }
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!
111 template <typename Visitor, typename RetType>
VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable & soa,Handle<mirror::ClassLoader> class_loader,Visitor fn,RetType defaultReturn)112 inline 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.
154 template <typename Visitor>
VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable & soa,Handle<mirror::ClassLoader> class_loader,Visitor fn)155 inline void VisitClassLoaderDexFiles(ScopedObjectAccessAlreadyRunnable& soa,
156                                      Handle<mirror::ClassLoader> class_loader,
157                                      Visitor fn)
158     REQUIRES_SHARED(Locks::mutator_lock_) {
159   auto helper = [&fn](const art::DexFile* dex_file, void** ret)
160       REQUIRES_SHARED(Locks::mutator_lock_) {
161 #ifdef __clang_analyzer__
162     *ret = nullptr;
163 #else
164     UNUSED(ret);
165 #endif
166 
167     return fn(dex_file);
168   };
169   VisitClassLoaderDexFiles<decltype(helper), void*>(soa,
170                                                     class_loader,
171                                                     helper,
172                                                     /* default= */ nullptr);
173 }
174 
175 }  // namespace art
176 
177 #endif  // ART_RUNTIME_CLASS_LOADER_UTILS_H_
178