• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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_LINKER_H_
18 #define ART_RUNTIME_CLASS_LINKER_H_
19 
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <type_traits>
25 #include <utility>
26 #include <vector>
27 
28 #include "base/enums.h"
29 #include "base/hash_map.h"
30 #include "base/mutex.h"
31 #include "base/intrusive_forward_list.h"
32 #include "base/locks.h"
33 #include "base/macros.h"
34 #include "dex/class_accessor.h"
35 #include "dex/dex_file_types.h"
36 #include "gc_root.h"
37 #include "handle.h"
38 #include "jni.h"
39 #include "mirror/class.h"
40 #include "mirror/object.h"
41 #include "verifier/verifier_enums.h"
42 
43 namespace art {
44 
45 class ArtField;
46 class ArtMethod;
47 class ClassHierarchyAnalysis;
48 enum class ClassRoot : uint32_t;
49 class ClassTable;
50 class DexFile;
51 template<class T> class Handle;
52 class ImtConflictTable;
53 template<typename T> class LengthPrefixedArray;
54 template<class T> class MutableHandle;
55 class InternTable;
56 class LinearAlloc;
57 class OatFile;
58 template<class T> class ObjectLock;
59 class Runtime;
60 class ScopedObjectAccessAlreadyRunnable;
61 class SdkChecker;
62 template<size_t kNumReferences> class PACKED(4) StackHandleScope;
63 class Thread;
64 
65 enum VisitRootFlags : uint8_t;
66 
67 namespace dex {
68 struct ClassDef;
69 struct MethodHandleItem;
70 }  // namespace dex
71 
72 namespace gc {
73 namespace space {
74 class ImageSpace;
75 }  // namespace space
76 }  // namespace gc
77 
78 namespace linker {
79 struct CompilationHelper;
80 class ImageWriter;
81 class OatWriter;
82 }  // namespace linker
83 
84 namespace mirror {
85 class ClassLoader;
86 class DexCache;
87 class DexCachePointerArray;
88 class DexCacheMethodHandlesTest_Open_Test;
89 class DexCacheTest_Open_Test;
90 class IfTable;
91 class MethodHandle;
92 class MethodHandlesLookup;
93 class MethodType;
94 template<class T> class ObjectArray;
95 class StackTraceElement;
96 template <typename T> struct NativeDexCachePair;
97 using MethodDexCachePair = NativeDexCachePair<ArtMethod>;
98 using MethodDexCacheType = std::atomic<MethodDexCachePair>;
99 }  // namespace mirror
100 
101 namespace verifier {
102 class VerifierDeps;
103 }
104 
105 class ClassVisitor {
106  public:
~ClassVisitor()107   virtual ~ClassVisitor() {}
108   // Return true to continue visiting.
109   virtual bool operator()(ObjPtr<mirror::Class> klass) = 0;
110 };
111 
112 template <typename Func>
113 class ClassFuncVisitor final : public ClassVisitor {
114  public:
ClassFuncVisitor(Func func)115   explicit ClassFuncVisitor(Func func) : func_(func) {}
operator()116   bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
117     return func_(klass);
118   }
119 
120  private:
121   Func func_;
122 };
123 
124 class ClassLoaderVisitor {
125  public:
~ClassLoaderVisitor()126   virtual ~ClassLoaderVisitor() {}
127   virtual void Visit(ObjPtr<mirror::ClassLoader> class_loader)
128       REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) = 0;
129 };
130 
131 template <typename Func>
132 class ClassLoaderFuncVisitor final : public ClassLoaderVisitor {
133  public:
ClassLoaderFuncVisitor(Func func)134   explicit ClassLoaderFuncVisitor(Func func) : func_(func) {}
Visit(ObjPtr<mirror::ClassLoader> cl)135   void Visit(ObjPtr<mirror::ClassLoader> cl) override REQUIRES_SHARED(Locks::mutator_lock_) {
136     func_(cl);
137   }
138 
139  private:
140   Func func_;
141 };
142 
143 class AllocatorVisitor {
144  public:
~AllocatorVisitor()145   virtual ~AllocatorVisitor() {}
146   // Return true to continue visiting.
147   virtual bool Visit(LinearAlloc* alloc)
148       REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) = 0;
149 };
150 
151 class ClassLinker {
152  public:
153   static constexpr bool kAppImageMayContainStrings = true;
154 
155   explicit ClassLinker(InternTable* intern_table,
156                        bool fast_class_not_found_exceptions = true);
157   virtual ~ClassLinker();
158 
159   // Initialize class linker by bootstraping from dex files.
160   bool InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,
161                         std::string* error_msg)
162       REQUIRES_SHARED(Locks::mutator_lock_)
163       REQUIRES(!Locks::dex_lock_);
164 
165   // Initialize class linker from one or more boot images.
166   bool InitFromBootImage(std::string* error_msg)
167       REQUIRES_SHARED(Locks::mutator_lock_)
168       REQUIRES(!Locks::dex_lock_);
169 
170   // Add boot class path dex files that were not included in the boot image.
171   // ClassLinker takes ownership of these dex files.
172   void AddExtraBootDexFiles(Thread* self,
173                             std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files)
174       REQUIRES_SHARED(Locks::mutator_lock_);
175 
176   // Add an image space to the class linker, may fix up classloader fields and dex cache fields.
177   // The dex files that were newly opened for the space are placed in the out argument
178   // out_dex_files. Returns true if the operation succeeded.
179   // The space must be already added to the heap before calling AddImageSpace since we need to
180   // properly handle read barriers and object marking.
181   bool AddImageSpace(gc::space::ImageSpace* space,
182                      Handle<mirror::ClassLoader> class_loader,
183                      std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
184                      std::string* error_msg)
185       REQUIRES(!Locks::dex_lock_)
186       REQUIRES_SHARED(Locks::mutator_lock_);
187 
188   bool OpenImageDexFiles(gc::space::ImageSpace* space,
189                          std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
190                          std::string* error_msg)
191       REQUIRES(!Locks::dex_lock_)
192       REQUIRES_SHARED(Locks::mutator_lock_);
193 
194   // Finds a class by its descriptor, loading it if necessary.
195   // If class_loader is null, searches boot_class_path_.
196   ObjPtr<mirror::Class> FindClass(Thread* self,
197                                   const char* descriptor,
198                                   Handle<mirror::ClassLoader> class_loader)
199       REQUIRES_SHARED(Locks::mutator_lock_)
200       REQUIRES(!Locks::dex_lock_);
201 
202   // Finds a class by its descriptor using the "system" class loader, ie by searching the
203   // boot_class_path_.
FindSystemClass(Thread * self,const char * descriptor)204   ObjPtr<mirror::Class> FindSystemClass(Thread* self, const char* descriptor)
205       REQUIRES_SHARED(Locks::mutator_lock_)
206       REQUIRES(!Locks::dex_lock_) {
207     return FindClass(self, descriptor, ScopedNullHandle<mirror::ClassLoader>());
208   }
209 
210   // Finds the array class given for the element class.
211   ObjPtr<mirror::Class> FindArrayClass(Thread* self, ObjPtr<mirror::Class> element_class)
212       REQUIRES_SHARED(Locks::mutator_lock_)
213       REQUIRES(!Locks::dex_lock_);
214 
215   // Returns true if the class linker is initialized.
IsInitialized()216   bool IsInitialized() const {
217     return init_done_;
218   }
219 
220   // Define a new a class based on a ClassDef from a DexFile
221   ObjPtr<mirror::Class> DefineClass(Thread* self,
222                                     const char* descriptor,
223                                     size_t hash,
224                                     Handle<mirror::ClassLoader> class_loader,
225                                     const DexFile& dex_file,
226                                     const dex::ClassDef& dex_class_def)
227       REQUIRES_SHARED(Locks::mutator_lock_)
228       REQUIRES(!Locks::dex_lock_);
229 
230   // Finds a class by its descriptor, returning null if it isn't wasn't loaded
231   // by the given 'class_loader'.
232   ObjPtr<mirror::Class> LookupClass(Thread* self,
233                                     const char* descriptor,
234                                     ObjPtr<mirror::ClassLoader> class_loader)
235       REQUIRES(!Locks::classlinker_classes_lock_)
236       REQUIRES_SHARED(Locks::mutator_lock_);
237 
238   // Finds all the classes with the given descriptor, regardless of ClassLoader.
239   void LookupClasses(const char* descriptor, std::vector<ObjPtr<mirror::Class>>& classes)
240       REQUIRES(!Locks::classlinker_classes_lock_)
241       REQUIRES_SHARED(Locks::mutator_lock_);
242 
243   ObjPtr<mirror::Class> LookupPrimitiveClass(char type) REQUIRES_SHARED(Locks::mutator_lock_);
244   ObjPtr<mirror::Class> FindPrimitiveClass(char type) REQUIRES_SHARED(Locks::mutator_lock_);
245 
246   void DumpForSigQuit(std::ostream& os) REQUIRES(!Locks::classlinker_classes_lock_);
247 
248   size_t NumLoadedClasses()
249       REQUIRES(!Locks::classlinker_classes_lock_)
250       REQUIRES_SHARED(Locks::mutator_lock_);
251 
252   // Resolve a String with the given index from the DexFile associated with the given `referrer`,
253   // storing the result in the DexCache. The `referrer` is used to identify the target DexCache
254   // to use for resolution.
255   ObjPtr<mirror::String> ResolveString(dex::StringIndex string_idx,
256                                        ArtField* referrer)
257       REQUIRES_SHARED(Locks::mutator_lock_);
258   ObjPtr<mirror::String> ResolveString(dex::StringIndex string_idx,
259                                        ArtMethod* referrer)
260       REQUIRES_SHARED(Locks::mutator_lock_);
261 
262   // Resolve a String with the given index from the DexFile associated with the given DexCache,
263   // storing the result in the DexCache.
264   ObjPtr<mirror::String> ResolveString(dex::StringIndex string_idx,
265                                        Handle<mirror::DexCache> dex_cache)
266       REQUIRES_SHARED(Locks::mutator_lock_);
267 
268   // Find a String with the given index from the DexFile associated with the given DexCache,
269   // storing the result in the DexCache if found. Return null if not found.
270   ObjPtr<mirror::String> LookupString(dex::StringIndex string_idx,
271                                       ObjPtr<mirror::DexCache> dex_cache)
272       REQUIRES_SHARED(Locks::mutator_lock_);
273 
274   // Resolve a Type with the given index from the DexFile associated with the given `referrer`,
275   // storing the result in the DexCache. The `referrer` is used to identify the target DexCache
276   // and ClassLoader to use for resolution.
277   ObjPtr<mirror::Class> ResolveType(dex::TypeIndex type_idx, ObjPtr<mirror::Class> referrer)
278       REQUIRES_SHARED(Locks::mutator_lock_)
279       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
280   ObjPtr<mirror::Class> ResolveType(dex::TypeIndex type_idx, ArtField* referrer)
281       REQUIRES_SHARED(Locks::mutator_lock_)
282       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
283   ObjPtr<mirror::Class> ResolveType(dex::TypeIndex type_idx, ArtMethod* referrer)
284       REQUIRES_SHARED(Locks::mutator_lock_)
285       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
286 
287   // Resolve a type with the given index from the DexFile associated with the given DexCache
288   // and ClassLoader, storing the result in DexCache. The ClassLoader is used to search for
289   // the type, since it may be referenced from but not contained within the DexFile.
290   ObjPtr<mirror::Class> ResolveType(dex::TypeIndex type_idx,
291                                     Handle<mirror::DexCache> dex_cache,
292                                     Handle<mirror::ClassLoader> class_loader)
293       REQUIRES_SHARED(Locks::mutator_lock_)
294       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
295 
296   // Look up a resolved type with the given index from the DexFile associated with the given
297   // `referrer`, storing the result in the DexCache. The `referrer` is used to identify the
298   // target DexCache and ClassLoader to use for lookup.
299   ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_idx,
300                                            ObjPtr<mirror::Class> referrer)
301       REQUIRES_SHARED(Locks::mutator_lock_);
302   ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_idx, ArtField* referrer)
303       REQUIRES_SHARED(Locks::mutator_lock_);
304   ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_idx, ArtMethod* referrer)
305       REQUIRES_SHARED(Locks::mutator_lock_);
306 
307   // Look up a resolved type with the given index from the DexFile associated with the given
308   // DexCache and ClassLoader. The ClassLoader is used to search for the type, since it may
309   // be referenced from but not contained within the DexFile.
310   ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_idx,
311                                            ObjPtr<mirror::DexCache> dex_cache,
312                                            ObjPtr<mirror::ClassLoader> class_loader)
313       REQUIRES_SHARED(Locks::mutator_lock_);
314 
315   // Look up a resolved type with the given descriptor associated with the given ClassLoader.
316   ObjPtr<mirror::Class> LookupResolvedType(const char* descriptor,
317                                            ObjPtr<mirror::ClassLoader> class_loader)
318       REQUIRES_SHARED(Locks::mutator_lock_);
319 
320   // Determine whether a dex cache result should be trusted, or an IncompatibleClassChangeError
321   // check and IllegalAccessError check should be performed even after a hit.
322   enum class ResolveMode {  // private.
323     kNoChecks,
324     kCheckICCEAndIAE
325   };
326 
327   // Look up a previously resolved method with the given index.
328   ArtMethod* LookupResolvedMethod(uint32_t method_idx,
329                                   ObjPtr<mirror::DexCache> dex_cache,
330                                   ObjPtr<mirror::ClassLoader> class_loader)
331       REQUIRES_SHARED(Locks::mutator_lock_);
332 
333   // Find a method with the given index from class `klass`, and update the dex cache.
334   ArtMethod* FindResolvedMethod(ObjPtr<mirror::Class> klass,
335                                 ObjPtr<mirror::DexCache> dex_cache,
336                                 ObjPtr<mirror::ClassLoader> class_loader,
337                                 uint32_t method_idx)
338       REQUIRES_SHARED(Locks::mutator_lock_);
339 
340   // Find a method using the wrong lookup mechanism. If `klass` is an interface,
341   // search for a class method. If it is a class, search for an interface method.
342   // This is useful when throwing IncompatibleClassChangeError.
343   ArtMethod* FindIncompatibleMethod(ObjPtr<mirror::Class> klass,
344                                     ObjPtr<mirror::DexCache> dex_cache,
345                                     ObjPtr<mirror::ClassLoader> class_loader,
346                                     uint32_t method_idx)
347       REQUIRES_SHARED(Locks::mutator_lock_);
348 
349   // Resolve a method with a given ID from the DexFile associated with the given DexCache
350   // and ClassLoader, storing the result in DexCache. The ClassLinker and ClassLoader are
351   // used as in ResolveType. What is unique is the method type argument which is used to
352   // determine if this method is a direct, static, or virtual method.
353   template <ResolveMode kResolveMode>
354   ArtMethod* ResolveMethod(uint32_t method_idx,
355                            Handle<mirror::DexCache> dex_cache,
356                            Handle<mirror::ClassLoader> class_loader,
357                            ArtMethod* referrer,
358                            InvokeType type)
359       REQUIRES_SHARED(Locks::mutator_lock_)
360       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
361 
362   template <InvokeType type, ResolveMode kResolveMode>
363   ArtMethod* GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer)
364       REQUIRES_SHARED(Locks::mutator_lock_);
365 
366   template <ResolveMode kResolveMode>
367   ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, ArtMethod* referrer, InvokeType type)
368       REQUIRES_SHARED(Locks::mutator_lock_)
369       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
370   ArtMethod* ResolveMethodWithoutInvokeType(uint32_t method_idx,
371                                             Handle<mirror::DexCache> dex_cache,
372                                             Handle<mirror::ClassLoader> class_loader)
373       REQUIRES_SHARED(Locks::mutator_lock_)
374       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
375 
376   ArtField* LookupResolvedField(uint32_t field_idx, ArtMethod* referrer, bool is_static)
377       REQUIRES_SHARED(Locks::mutator_lock_);
378   ArtField* ResolveField(uint32_t field_idx, ArtMethod* referrer, bool is_static)
379       REQUIRES_SHARED(Locks::mutator_lock_)
380       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
381 
382   // Resolve a field with a given ID from the DexFile associated with the given DexCache
383   // and ClassLoader, storing the result in DexCache. The ClassLinker and ClassLoader
384   // are used as in ResolveType. What is unique is the is_static argument which is used
385   // to determine if we are resolving a static or non-static field.
386   ArtField* ResolveField(uint32_t field_idx,
387                          Handle<mirror::DexCache> dex_cache,
388                          Handle<mirror::ClassLoader> class_loader,
389                          bool is_static)
390       REQUIRES_SHARED(Locks::mutator_lock_)
391       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
392 
393   // Resolve a field with a given ID from the DexFile associated with the given DexCache
394   // and ClassLoader, storing the result in DexCache. The ClassLinker and ClassLoader
395   // are used as in ResolveType. No is_static argument is provided so that Java
396   // field resolution semantics are followed.
397   ArtField* ResolveFieldJLS(uint32_t field_idx,
398                             Handle<mirror::DexCache> dex_cache,
399                             Handle<mirror::ClassLoader> class_loader)
400       REQUIRES_SHARED(Locks::mutator_lock_)
401       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
402 
403   // Find a field with a given ID from the DexFile associated with the given DexCache
404   // and ClassLoader, storing the result in DexCache. The declaring class is assumed
405   // to have been already resolved into `klass`. The `is_static` argument is used to
406   // determine if we are resolving a static or non-static field.
407   ArtField* FindResolvedField(ObjPtr<mirror::Class> klass,
408                               ObjPtr<mirror::DexCache> dex_cache,
409                               ObjPtr<mirror::ClassLoader> class_loader,
410                               uint32_t field_idx,
411                               bool is_static)
412       REQUIRES_SHARED(Locks::mutator_lock_);
413 
414   // Find a field with a given ID from the DexFile associated with the given DexCache
415   // and ClassLoader, storing the result in DexCache. The declaring class is assumed
416   // to have been already resolved into `klass`. No is_static argument is provided
417   // so that Java field resolution semantics are followed.
418   ArtField* FindResolvedFieldJLS(ObjPtr<mirror::Class> klass,
419                                  ObjPtr<mirror::DexCache> dex_cache,
420                                  ObjPtr<mirror::ClassLoader> class_loader,
421                                  uint32_t field_idx)
422       REQUIRES_SHARED(Locks::mutator_lock_);
423 
424   // Resolve a method type with a given ID from the DexFile associated with a given DexCache
425   // and ClassLoader, storing the result in the DexCache.
426   ObjPtr<mirror::MethodType> ResolveMethodType(Thread* self,
427                                                dex::ProtoIndex proto_idx,
428                                                Handle<mirror::DexCache> dex_cache,
429                                                Handle<mirror::ClassLoader> class_loader)
430       REQUIRES_SHARED(Locks::mutator_lock_)
431       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
432 
433   ObjPtr<mirror::MethodType> ResolveMethodType(Thread* self,
434                                                dex::ProtoIndex proto_idx,
435                                                ArtMethod* referrer)
436       REQUIRES_SHARED(Locks::mutator_lock_);
437 
438   // Resolve a method handle with a given ID from the DexFile. The
439   // result is not cached in the DexCache as the instance will only be
440   // used once in most circumstances.
441   ObjPtr<mirror::MethodHandle> ResolveMethodHandle(Thread* self,
442                                                    uint32_t method_handle_idx,
443                                                    ArtMethod* referrer)
444       REQUIRES_SHARED(Locks::mutator_lock_);
445 
446   // Returns true on success, false if there's an exception pending.
447   // can_run_clinit=false allows the compiler to attempt to init a class,
448   // given the restriction that no <clinit> execution is possible.
449   bool EnsureInitialized(Thread* self,
450                          Handle<mirror::Class> c,
451                          bool can_init_fields,
452                          bool can_init_parents)
453       REQUIRES_SHARED(Locks::mutator_lock_)
454       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
455 
456   // Initializes classes that have instances in the image but that have
457   // <clinit> methods so they could not be initialized by the compiler.
458   void RunRootClinits(Thread* self)
459       REQUIRES_SHARED(Locks::mutator_lock_)
460       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
461 
462   // Directly register an already existing dex cache. RegisterDexFile should be preferred since that
463   // reduplicates DexCaches when possible. The DexCache given to this function must already be fully
464   // initialized and not already registered.
465   void RegisterExistingDexCache(ObjPtr<mirror::DexCache> cache,
466                                 ObjPtr<mirror::ClassLoader> class_loader)
467       REQUIRES(!Locks::dex_lock_)
468       REQUIRES_SHARED(Locks::mutator_lock_);
469   ObjPtr<mirror::DexCache> RegisterDexFile(const DexFile& dex_file,
470                                            ObjPtr<mirror::ClassLoader> class_loader)
471       REQUIRES(!Locks::dex_lock_)
472       REQUIRES_SHARED(Locks::mutator_lock_);
473 
GetBootClassPath()474   const std::vector<const DexFile*>& GetBootClassPath() {
475     return boot_class_path_;
476   }
477 
478   void VisitClasses(ClassVisitor* visitor)
479       REQUIRES(!Locks::classlinker_classes_lock_)
480       REQUIRES_SHARED(Locks::mutator_lock_);
481 
482   // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage
483   // so that it can visit individual classes without holding the doesn't hold the
484   // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code
485   // can race with insertion and deletion of classes while the visitor is being called.
486   void VisitClassesWithoutClassesLock(ClassVisitor* visitor)
487       REQUIRES_SHARED(Locks::mutator_lock_)
488       REQUIRES(!Locks::dex_lock_);
489 
490   void VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags)
491       REQUIRES(!Locks::classlinker_classes_lock_, !Locks::trace_lock_)
492       REQUIRES_SHARED(Locks::mutator_lock_);
493   void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
494       REQUIRES(!Locks::dex_lock_, !Locks::classlinker_classes_lock_, !Locks::trace_lock_)
495       REQUIRES_SHARED(Locks::mutator_lock_);
496   // Visits all dex-files accessible by any class-loader or the BCP.
497   template<typename Visitor>
498   void VisitKnownDexFiles(Thread* self, Visitor visitor) REQUIRES(Locks::mutator_lock_);
499 
500   bool IsDexFileRegistered(Thread* self, const DexFile& dex_file)
501       REQUIRES(!Locks::dex_lock_)
502       REQUIRES_SHARED(Locks::mutator_lock_);
503   ObjPtr<mirror::DexCache> FindDexCache(Thread* self, const DexFile& dex_file)
504       REQUIRES(!Locks::dex_lock_)
505       REQUIRES_SHARED(Locks::mutator_lock_);
506   ClassTable* FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache)
507       REQUIRES(!Locks::dex_lock_)
508       REQUIRES_SHARED(Locks::mutator_lock_);
509 
510   LengthPrefixedArray<ArtField>* AllocArtFieldArray(Thread* self,
511                                                     LinearAlloc* allocator,
512                                                     size_t length);
513 
514   LengthPrefixedArray<ArtMethod>* AllocArtMethodArray(Thread* self,
515                                                       LinearAlloc* allocator,
516                                                       size_t length);
517 
518   // Convenience AllocClass() overload that uses mirror::Class::InitializeClassVisitor
519   // for the class initialization and uses the `java_lang_Class` from class roots
520   // instead of an explicit argument.
521   ObjPtr<mirror::Class> AllocClass(Thread* self, uint32_t class_size)
522       REQUIRES_SHARED(Locks::mutator_lock_)
523       REQUIRES(!Roles::uninterruptible_);
524 
525   // Setup the classloader, class def index, type idx so that we can insert this class in the class
526   // table.
527   void SetupClass(const DexFile& dex_file,
528                   const dex::ClassDef& dex_class_def,
529                   Handle<mirror::Class> klass,
530                   ObjPtr<mirror::ClassLoader> class_loader)
531       REQUIRES_SHARED(Locks::mutator_lock_);
532 
533   void LoadClass(Thread* self,
534                  const DexFile& dex_file,
535                  const dex::ClassDef& dex_class_def,
536                  Handle<mirror::Class> klass)
537       REQUIRES_SHARED(Locks::mutator_lock_);
538 
539   // Link the class and place it into the class-table using the given descriptor. NB if the
540   // descriptor is null the class will not be placed in any class-table. This is useful implementing
541   // obsolete classes and should not be used otherwise.
542   bool LinkClass(Thread* self,
543                  const char* descriptor,
544                  Handle<mirror::Class> klass,
545                  Handle<mirror::ObjectArray<mirror::Class>> interfaces,
546                  MutableHandle<mirror::Class>* h_new_class_out)
547       REQUIRES_SHARED(Locks::mutator_lock_)
548       REQUIRES(!Locks::classlinker_classes_lock_);
549 
550   ObjPtr<mirror::PointerArray> AllocPointerArray(Thread* self, size_t length)
551       REQUIRES_SHARED(Locks::mutator_lock_)
552       REQUIRES(!Roles::uninterruptible_);
553 
554   ObjPtr<mirror::IfTable> AllocIfTable(Thread* self, size_t ifcount)
555       REQUIRES_SHARED(Locks::mutator_lock_)
556       REQUIRES(!Roles::uninterruptible_);
557 
558   ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> AllocStackTraceElementArray(Thread* self,
559                                                                                      size_t length)
560       REQUIRES_SHARED(Locks::mutator_lock_)
561       REQUIRES(!Roles::uninterruptible_);
562 
563   verifier::FailureKind VerifyClass(
564       Thread* self,
565       verifier::VerifierDeps* verifier_deps,
566       Handle<mirror::Class> klass,
567       verifier::HardFailLogMode log_level = verifier::HardFailLogMode::kLogNone)
568       REQUIRES_SHARED(Locks::mutator_lock_)
569       REQUIRES(!Locks::dex_lock_);
570   bool VerifyClassUsingOatFile(Thread* self,
571                                const DexFile& dex_file,
572                                Handle<mirror::Class> klass,
573                                ClassStatus& oat_file_class_status)
574       REQUIRES_SHARED(Locks::mutator_lock_)
575       REQUIRES(!Locks::dex_lock_);
576   void ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass)
577       REQUIRES_SHARED(Locks::mutator_lock_)
578       REQUIRES(!Locks::dex_lock_);
579   void ResolveMethodExceptionHandlerTypes(ArtMethod* klass)
580       REQUIRES_SHARED(Locks::mutator_lock_)
581       REQUIRES(!Locks::dex_lock_);
582 
583   ObjPtr<mirror::Class> CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa,
584                                          jstring name,
585                                          jobjectArray interfaces,
586                                          jobject loader,
587                                          jobjectArray methods,
588                                          jobjectArray throws)
589       REQUIRES_SHARED(Locks::mutator_lock_);
590 
591   // Get the oat code for a method when its class isn't yet initialized.
592   const void* GetQuickOatCodeFor(ArtMethod* method)
593       REQUIRES_SHARED(Locks::mutator_lock_);
594 
595   pid_t GetClassesLockOwner();  // For SignalCatcher.
596   pid_t GetDexLockOwner();  // For SignalCatcher.
597 
598   // Is the given entry point quick code to run the resolution stub?
599   bool IsQuickResolutionStub(const void* entry_point) const;
600 
601   // Is the given entry point quick code to bridge into the interpreter?
602   bool IsQuickToInterpreterBridge(const void* entry_point) const;
603 
604   // Is the given entry point quick code to run the generic JNI stub?
605   bool IsQuickGenericJniStub(const void* entry_point) const;
606 
607   // Is the given entry point the JNI dlsym lookup stub?
608   bool IsJniDlsymLookupStub(const void* entry_point) const;
609 
610   // Is the given entry point the JNI dlsym lookup critical stub?
611   bool IsJniDlsymLookupCriticalStub(const void* entry_point) const;
612 
613   // Is the given entry point the nterp trampoline?
IsNterpTrampoline(const void * entry_point)614   bool IsNterpTrampoline(const void* entry_point) const {
615     return nterp_trampoline_ == entry_point;
616   }
617 
GetQuickToInterpreterBridgeTrampoline()618   const void* GetQuickToInterpreterBridgeTrampoline() const {
619     return quick_to_interpreter_bridge_trampoline_;
620   }
621 
GetInternTable()622   InternTable* GetInternTable() const {
623     return intern_table_;
624   }
625 
626   // Set the entrypoints up for method to the enter the interpreter.
627   void SetEntryPointsToInterpreter(ArtMethod* method) const
628       REQUIRES_SHARED(Locks::mutator_lock_);
629 
630   // Set the entrypoints up for an obsolete method.
631   void SetEntryPointsForObsoleteMethod(ArtMethod* method) const
632       REQUIRES_SHARED(Locks::mutator_lock_);
633 
634   // Attempts to insert a class into a class table.  Returns null if
635   // the class was inserted, otherwise returns an existing class with
636   // the same descriptor and ClassLoader.
637   ObjPtr<mirror::Class> InsertClass(const char* descriptor,
638                                     ObjPtr<mirror::Class> klass,
639                                     size_t hash)
640       REQUIRES(!Locks::classlinker_classes_lock_)
641       REQUIRES_SHARED(Locks::mutator_lock_);
642 
643   // Add an oat file with .bss GC roots to be visited again at the end of GC
644   // for collector types that need it.
645   void WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file)
646       REQUIRES(!Locks::classlinker_classes_lock_)
647       REQUIRES_SHARED(Locks::mutator_lock_);
648 
649   template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
650   ObjPtr<mirror::ObjectArray<mirror::Class>> GetClassRoots() REQUIRES_SHARED(Locks::mutator_lock_);
651 
652   // Move the class table to the pre-zygote table to reduce memory usage. This works by ensuring
653   // that no more classes are ever added to the pre zygote table which makes it that the pages
654   // always remain shared dirty instead of private dirty.
655   void MoveClassTableToPreZygote()
656       REQUIRES(!Locks::classlinker_classes_lock_)
657       REQUIRES_SHARED(Locks::mutator_lock_);
658 
659   // Creates a GlobalRef PathClassLoader or DelegateLastClassLoader (specified by loader_class)
660   // that can be used to load classes from the given dex files. The parent of the class loader
661   // will be set to `parent_loader`. If `parent_loader` is null the parent will be
662   // the boot class loader.
663   // If class_loader points to a different class than PathClassLoader or DelegateLastClassLoader
664   // this method will abort.
665   // Note: the objects are not completely set up. Do not use this outside of tests and the compiler.
666   jobject CreateWellKnownClassLoader(Thread* self,
667                                      const std::vector<const DexFile*>& dex_files,
668                                      jclass loader_class,
669                                      jobject parent_loader,
670                                      jobject shared_libraries = nullptr)
671       REQUIRES_SHARED(Locks::mutator_lock_)
672       REQUIRES(!Locks::dex_lock_);
673 
674   // Calls CreateWellKnownClassLoader(self,
675   //                                  dex_files,
676   //                                  WellKnownClasses::dalvik_system_PathClassLoader,
677   //                                  nullptr)
678   jobject CreatePathClassLoader(Thread* self, const std::vector<const DexFile*>& dex_files)
679       REQUIRES_SHARED(Locks::mutator_lock_)
680       REQUIRES(!Locks::dex_lock_);
681 
682   // Non-GlobalRef version of CreateWellKnownClassLoader
683   ObjPtr<mirror::ClassLoader> CreateWellKnownClassLoader(
684       Thread* self,
685       const std::vector<const DexFile*>& dex_files,
686       Handle<mirror::Class> loader_class,
687       Handle<mirror::ClassLoader> parent_loader,
688       Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries)
689           REQUIRES_SHARED(Locks::mutator_lock_)
690           REQUIRES(!Locks::dex_lock_);
691 
GetImagePointerSize()692   PointerSize GetImagePointerSize() const {
693     return image_pointer_size_;
694   }
695 
696   // Clear the ArrayClass cache. This is necessary when cleaning up for the image, as the cache
697   // entries are roots, but potentially not image classes.
698   void DropFindArrayClassCache() REQUIRES_SHARED(Locks::mutator_lock_);
699 
700   // Clean up class loaders, this needs to happen after JNI weak globals are cleared.
701   void CleanupClassLoaders()
702       REQUIRES(!Locks::classlinker_classes_lock_)
703       REQUIRES_SHARED(Locks::mutator_lock_);
704 
705   // Unlike GetOrCreateAllocatorForClassLoader, GetAllocatorForClassLoader asserts that the
706   // allocator for this class loader is already created.
707   LinearAlloc* GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)
708       REQUIRES_SHARED(Locks::mutator_lock_);
709 
710   // Return the linear alloc for a class loader if it is already allocated, otherwise allocate and
711   // set it. TODO: Consider using a lock other than classlinker_classes_lock_.
712   LinearAlloc* GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)
713       REQUIRES(!Locks::classlinker_classes_lock_)
714       REQUIRES_SHARED(Locks::mutator_lock_);
715 
716   // May be called with null class_loader due to legacy code. b/27954959
717   void InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file,
718                                     ObjPtr<mirror::ClassLoader> class_loader)
719       REQUIRES(!Locks::classlinker_classes_lock_)
720       REQUIRES_SHARED(Locks::mutator_lock_);
721 
722   static bool ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code)
723       REQUIRES_SHARED(Locks::mutator_lock_);
724 
725   static bool IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
726                                 ObjPtr<mirror::ClassLoader> class_loader)
727       REQUIRES_SHARED(Locks::mutator_lock_);
728 
729   ArtMethod* AddMethodToConflictTable(ObjPtr<mirror::Class> klass,
730                                       ArtMethod* conflict_method,
731                                       ArtMethod* interface_method,
732                                       ArtMethod* method)
733       REQUIRES_SHARED(Locks::mutator_lock_);
734 
735   // Create a conflict table with a specified capacity.
736   ImtConflictTable* CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc);
737 
738   // Static version for when the class linker is not yet created.
739   static ImtConflictTable* CreateImtConflictTable(size_t count,
740                                                   LinearAlloc* linear_alloc,
741                                                   PointerSize pointer_size);
742 
743 
744   // Create the IMT and conflict tables for a class.
745   void FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
746 
747   // Visit all of the class tables. This is used by dex2oat to allow pruning dex caches.
748   template <class Visitor>
749   void VisitClassTables(const Visitor& visitor)
750       REQUIRES(!Locks::classlinker_classes_lock_)
751       REQUIRES_SHARED(Locks::mutator_lock_);
752 
753   // Visit all of the allocators that belong to classloaders except boot classloader.
754   // This is used by 616-cha-unloading test to confirm memory reuse.
755   void VisitAllocators(AllocatorVisitor* visitor) const
756       REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
757 
758   // Throw the class initialization failure recorded when first trying to initialize the given
759   // class.
760   void ThrowEarlierClassFailure(ObjPtr<mirror::Class> c,
761                                 bool wrap_in_no_class_def = false,
762                                 bool log = false)
763       REQUIRES_SHARED(Locks::mutator_lock_)
764       REQUIRES(!Locks::dex_lock_);
765 
766   // Get the actual holding class for a copied method. Pretty slow, don't call often.
767   ObjPtr<mirror::Class> GetHoldingClassOfCopiedMethod(ArtMethod* method)
768       REQUIRES_SHARED(Locks::mutator_lock_);
769 
770   // Returns null if not found.
771   // This returns a pointer to the class-table, without requiring any locking - including the
772   // boot class-table. It is the caller's responsibility to access this under lock, if required.
773   ClassTable* ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)
774       REQUIRES_SHARED(Locks::mutator_lock_)
775       NO_THREAD_SAFETY_ANALYSIS;
776 
777   void AppendToBootClassPath(Thread* self, const DexFile* dex_file)
778       REQUIRES_SHARED(Locks::mutator_lock_)
779       REQUIRES(!Locks::dex_lock_);
780 
781   // Visit all of the class loaders in the class linker.
782   void VisitClassLoaders(ClassLoaderVisitor* visitor) const
783       REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
784 
785   // Checks that a class and its superclass from another class loader have the same virtual methods.
786   bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
787       REQUIRES_SHARED(Locks::mutator_lock_);
788 
GetClassHierarchyAnalysis()789   ClassHierarchyAnalysis* GetClassHierarchyAnalysis() {
790     return cha_.get();
791   }
792 
793   void MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait);
794 
795   // Registers the native method and returns the new entry point. NB The returned entry point
796   // might be different from the native_method argument if some MethodCallback modifies it.
797   const void* RegisterNative(Thread* self, ArtMethod* method, const void* native_method)
798       REQUIRES_SHARED(Locks::mutator_lock_) WARN_UNUSED;
799 
800   // Unregister native code for a method.
801   void UnregisterNative(Thread* self, ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
802 
803   // Get the registered native method entrypoint, if any, otherwise null.
804   const void* GetRegisteredNative(Thread* self, ArtMethod* method)
805       REQUIRES_SHARED(Locks::mutator_lock_)
806       REQUIRES(!critical_native_code_with_clinit_check_lock_);
807 
808   struct DexCacheData {
809     // Construct an invalid data object.
DexCacheDataDexCacheData810     DexCacheData()
811         : weak_root(nullptr),
812           dex_file(nullptr),
813           class_table(nullptr) { }
814 
815     // Check if the data is valid.
IsValidDexCacheData816     bool IsValid() const {
817       return dex_file != nullptr;
818     }
819 
820     // Weak root to the DexCache. Note: Do not decode this unnecessarily or else class unloading may
821     // not work properly.
822     jweak weak_root;
823     // The following field caches the DexCache's field here to avoid unnecessary jweak decode that
824     // triggers read barriers (and marks them alive unnecessarily and messes with class unloading.)
825     const DexFile* dex_file;
826     // Identify the associated class loader's class table. This is used to make sure that
827     // the Java call to native DexCache.setResolvedType() inserts the resolved type in that
828     // class table. It is also used to make sure we don't register the same dex cache with
829     // multiple class loaders.
830     ClassTable* class_table;
831   };
832 
833   // Forces a class to be marked as initialized without actually running initializers. Should only
834   // be used by plugin code when creating new classes directly.
835   void ForceClassInitialized(Thread* self, Handle<mirror::Class> klass)
836       REQUIRES_SHARED(Locks::mutator_lock_)
837       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
838 
839   // Verifies if the method is accesible according to the SdkChecker (if installed).
840   virtual bool DenyAccessBasedOnPublicSdk(ArtMethod* art_method) const
841       REQUIRES_SHARED(Locks::mutator_lock_);
842   // Verifies if the field is accesible according to the SdkChecker (if installed).
843   virtual bool DenyAccessBasedOnPublicSdk(ArtField* art_field) const
844       REQUIRES_SHARED(Locks::mutator_lock_);
845   // Verifies if the descriptor is accesible according to the SdkChecker (if installed).
846   virtual bool DenyAccessBasedOnPublicSdk(const char* type_descriptor) const;
847   // Enable or disable public sdk checks.
848   virtual void SetEnablePublicSdkChecks(bool enabled);
849 
850  protected:
851   virtual bool InitializeClass(Thread* self,
852                                Handle<mirror::Class> klass,
853                                bool can_run_clinit,
854                                bool can_init_parents)
855       REQUIRES_SHARED(Locks::mutator_lock_)
856       REQUIRES(!Locks::dex_lock_);
857 
858   virtual verifier::FailureKind PerformClassVerification(Thread* self,
859                                                          verifier::VerifierDeps* verifier_deps,
860                                                          Handle<mirror::Class> klass,
861                                                          verifier::HardFailLogMode log_level,
862                                                          std::string* error_msg)
863       REQUIRES_SHARED(Locks::mutator_lock_);
864 
CanAllocClass()865   virtual bool CanAllocClass() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::dex_lock_) {
866     return true;
867   }
868 
869   virtual bool IsUpdatableBootClassPathDescriptor(const char* descriptor);
870 
871  private:
872   class LinkFieldsHelper;
873   class LinkInterfaceMethodsHelper;
874   class MethodTranslation;
875   class VisiblyInitializedCallback;
876 
877   struct ClassLoaderData {
878     jweak weak_root;  // Weak root to enable class unloading.
879     ClassTable* class_table;
880     LinearAlloc* allocator;
881   };
882 
883   void VisiblyInitializedCallbackDone(Thread* self, VisiblyInitializedCallback* callback);
884   VisiblyInitializedCallback* MarkClassInitialized(Thread* self, Handle<mirror::Class> klass)
885       REQUIRES_SHARED(Locks::mutator_lock_);
886 
887   // Ensures that the supertype of 'klass' ('supertype') is verified. Returns false and throws
888   // appropriate exceptions if verification failed hard. Returns true for successful verification or
889   // soft-failures.
890   bool AttemptSupertypeVerification(Thread* self,
891                                     verifier::VerifierDeps* verifier_deps,
892                                     Handle<mirror::Class> klass,
893                                     Handle<mirror::Class> supertype)
894       REQUIRES(!Locks::dex_lock_)
895       REQUIRES_SHARED(Locks::mutator_lock_);
896 
897   void DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha)
898       REQUIRES_SHARED(Locks::mutator_lock_);
899 
900   void VisitClassesInternal(ClassVisitor* visitor)
901       REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
902 
903   // Returns the number of zygote and image classes.
904   size_t NumZygoteClasses() const
905       REQUIRES(Locks::classlinker_classes_lock_)
906       REQUIRES_SHARED(Locks::mutator_lock_);
907 
908   // Returns the number of non zygote nor image classes.
909   size_t NumNonZygoteClasses() const
910       REQUIRES(Locks::classlinker_classes_lock_)
911       REQUIRES_SHARED(Locks::mutator_lock_);
912 
913   void FinishInit(Thread* self)
914       REQUIRES_SHARED(Locks::mutator_lock_)
915       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
916 
917   // If we do not allow moving classes (`art::kMovingClass` is false) or if
918   // parameter `kMovable` is false (or both), the class object is allocated in
919   // the non-moving space.
920   template <bool kMovable = true, class PreFenceVisitor>
921   ObjPtr<mirror::Class> AllocClass(Thread* self,
922                                    ObjPtr<mirror::Class> java_lang_Class,
923                                    uint32_t class_size,
924                                    const PreFenceVisitor& pre_fence_visitor)
925       REQUIRES_SHARED(Locks::mutator_lock_)
926       REQUIRES(!Roles::uninterruptible_);
927 
928   // Convenience AllocClass() overload that uses mirror::Class::InitializeClassVisitor
929   // for the class initialization.
930   template <bool kMovable = true>
931   ObjPtr<mirror::Class> AllocClass(Thread* self,
932                                    ObjPtr<mirror::Class> java_lang_Class,
933                                    uint32_t class_size)
934       REQUIRES_SHARED(Locks::mutator_lock_)
935       REQUIRES(!Roles::uninterruptible_);
936 
937   // Allocate a primitive array class and store it in appropriate class root.
938   void AllocPrimitiveArrayClass(Thread* self,
939                                 ClassRoot primitive_root,
940                                 ClassRoot array_root)
941       REQUIRES_SHARED(Locks::mutator_lock_)
942       REQUIRES(!Roles::uninterruptible_);
943 
944   // Finish setup of an array class.
945   void FinishArrayClassSetup(ObjPtr<mirror::Class> array_class)
946       REQUIRES_SHARED(Locks::mutator_lock_)
947       REQUIRES(!Roles::uninterruptible_);
948 
949   // Finish setup of a core array class (Object[], Class[], String[] and
950   // primitive arrays) and insert it into the class table.
951   void FinishCoreArrayClassSetup(ClassRoot array_root)
952       REQUIRES_SHARED(Locks::mutator_lock_)
953       REQUIRES(!Roles::uninterruptible_);
954 
955   ObjPtr<mirror::DexCache> AllocDexCache(Thread* self, const DexFile& dex_file)
956       REQUIRES_SHARED(Locks::mutator_lock_)
957       REQUIRES(!Roles::uninterruptible_);
958 
959   // Used for tests and AppendToBootClassPath.
960   ObjPtr<mirror::DexCache> AllocAndInitializeDexCache(Thread* self,
961                                                       const DexFile& dex_file,
962                                                       LinearAlloc* linear_alloc)
963       REQUIRES_SHARED(Locks::mutator_lock_)
964       REQUIRES(!Locks::dex_lock_)
965       REQUIRES(!Roles::uninterruptible_);
966 
967   // Create a primitive class and store it in the appropriate class root.
968   void CreatePrimitiveClass(Thread* self, Primitive::Type type, ClassRoot primitive_root)
969       REQUIRES_SHARED(Locks::mutator_lock_)
970       REQUIRES(!Roles::uninterruptible_);
971 
972   ObjPtr<mirror::Class> CreateArrayClass(Thread* self,
973                                          const char* descriptor,
974                                          size_t hash,
975                                          Handle<mirror::ClassLoader> class_loader)
976       REQUIRES_SHARED(Locks::mutator_lock_)
977       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
978 
979   void AppendToBootClassPath(const DexFile* dex_file, ObjPtr<mirror::DexCache> dex_cache)
980       REQUIRES_SHARED(Locks::mutator_lock_)
981       REQUIRES(!Locks::dex_lock_);
982 
983   // Precomputes size needed for Class, in the case of a non-temporary class this size must be
984   // sufficient to hold all static fields.
985   uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
986                                             const dex::ClassDef& dex_class_def);
987 
988   void LoadField(const ClassAccessor::Field& field, Handle<mirror::Class> klass, ArtField* dst)
989       REQUIRES_SHARED(Locks::mutator_lock_);
990 
991   void LoadMethod(const DexFile& dex_file,
992                   const ClassAccessor::Method& method,
993                   Handle<mirror::Class> klass,
994                   ArtMethod* dst)
995       REQUIRES_SHARED(Locks::mutator_lock_);
996 
997   void FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass)
998       REQUIRES_SHARED(Locks::mutator_lock_);
999 
1000   // Finds a class in a Path- or DexClassLoader, loading it if necessary without using JNI. Hash
1001   // function is supposed to be ComputeModifiedUtf8Hash(descriptor). Returns true if the
1002   // class-loader chain could be handled, false otherwise, i.e., a non-supported class-loader
1003   // was encountered while walking the parent chain (currently only BootClassLoader and
1004   // PathClassLoader are supported).
1005   bool FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
1006                                      Thread* self,
1007                                      const char* descriptor,
1008                                      size_t hash,
1009                                      Handle<mirror::ClassLoader> class_loader,
1010                                      /*out*/ ObjPtr<mirror::Class>* result)
1011       REQUIRES_SHARED(Locks::mutator_lock_)
1012       REQUIRES(!Locks::dex_lock_);
1013 
1014   bool FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa,
1015                                   Thread* self,
1016                                   const char* descriptor,
1017                                   size_t hash,
1018                                   Handle<mirror::ClassLoader> class_loader,
1019                                   /*out*/ ObjPtr<mirror::Class>* result)
1020       REQUIRES_SHARED(Locks::mutator_lock_)
1021       REQUIRES(!Locks::dex_lock_);
1022 
1023   // Finds the class in the classpath of the given class loader. It only searches the class loader
1024   // dex files and does not recurse into its parent.
1025   // The method checks that the provided class loader is either a PathClassLoader or a
1026   // DexClassLoader.
1027   // If the class is found the method returns the resolved class. Otherwise it returns null.
1028   ObjPtr<mirror::Class> FindClassInBaseDexClassLoaderClassPath(
1029           ScopedObjectAccessAlreadyRunnable& soa,
1030           const char* descriptor,
1031           size_t hash,
1032           Handle<mirror::ClassLoader> class_loader)
1033       REQUIRES_SHARED(Locks::mutator_lock_)
1034       REQUIRES(!Locks::dex_lock_);
1035 
1036   // Finds the class in the boot class loader.
1037   // If the class is found the method returns the resolved class. Otherwise it returns null.
1038   ObjPtr<mirror::Class> FindClassInBootClassLoaderClassPath(Thread* self,
1039                                                             const char* descriptor,
1040                                                             size_t hash)
1041       REQUIRES_SHARED(Locks::mutator_lock_)
1042       REQUIRES(!Locks::dex_lock_);
1043 
1044   // Implementation of LookupResolvedType() called when the type was not found in the dex cache.
1045   ObjPtr<mirror::Class> DoLookupResolvedType(dex::TypeIndex type_idx,
1046                                              ObjPtr<mirror::Class> referrer)
1047       REQUIRES_SHARED(Locks::mutator_lock_);
1048   ObjPtr<mirror::Class> DoLookupResolvedType(dex::TypeIndex type_idx,
1049                                              ObjPtr<mirror::DexCache> dex_cache,
1050                                              ObjPtr<mirror::ClassLoader> class_loader)
1051       REQUIRES_SHARED(Locks::mutator_lock_);
1052 
1053   // Implementation of ResolveString() called when the string was not found in the dex cache.
1054   ObjPtr<mirror::String> DoResolveString(dex::StringIndex string_idx,
1055                                          ObjPtr<mirror::DexCache> dex_cache)
1056       REQUIRES_SHARED(Locks::mutator_lock_);
1057   ObjPtr<mirror::String> DoResolveString(dex::StringIndex string_idx,
1058                                          Handle<mirror::DexCache> dex_cache)
1059       REQUIRES_SHARED(Locks::mutator_lock_);
1060 
1061   // Implementation of LookupString() called when the string was not found in the dex cache.
1062   ObjPtr<mirror::String> DoLookupString(dex::StringIndex string_idx,
1063                                         ObjPtr<mirror::DexCache> dex_cache)
1064       REQUIRES_SHARED(Locks::mutator_lock_);
1065 
1066   // Implementation of ResolveType() called when the type was not found in the dex cache. May be
1067   // used with ArtField*, ArtMethod* or ObjPtr<Class>.
1068   template <typename RefType>
1069   ObjPtr<mirror::Class> DoResolveType(dex::TypeIndex type_idx, RefType referrer)
1070       REQUIRES_SHARED(Locks::mutator_lock_)
1071       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
1072   ObjPtr<mirror::Class> DoResolveType(dex::TypeIndex type_idx,
1073                                       Handle<mirror::DexCache> dex_cache,
1074                                       Handle<mirror::ClassLoader> class_loader)
1075       REQUIRES_SHARED(Locks::mutator_lock_)
1076       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
1077 
1078   // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
1079   // by the given 'class_loader'. Uses the provided hash for the descriptor.
1080   ObjPtr<mirror::Class> LookupClass(Thread* self,
1081                                     const char* descriptor,
1082                                     size_t hash,
1083                                     ObjPtr<mirror::ClassLoader> class_loader)
1084       REQUIRES(!Locks::classlinker_classes_lock_)
1085       REQUIRES_SHARED(Locks::mutator_lock_);
1086 
1087   // Find a field by its field index.
1088   ArtField* LookupResolvedField(uint32_t field_idx,
1089                                 ObjPtr<mirror::DexCache> dex_cache,
1090                                 ObjPtr<mirror::ClassLoader> class_loader,
1091                                 bool is_static)
1092       REQUIRES_SHARED(Locks::mutator_lock_);
1093 
1094   void RegisterDexFileLocked(const DexFile& dex_file,
1095                              ObjPtr<mirror::DexCache> dex_cache,
1096                              ObjPtr<mirror::ClassLoader> class_loader)
1097       REQUIRES(Locks::dex_lock_)
1098       REQUIRES_SHARED(Locks::mutator_lock_);
1099   const DexCacheData* FindDexCacheDataLocked(const DexFile& dex_file)
1100       REQUIRES(Locks::dex_lock_)
1101       REQUIRES_SHARED(Locks::mutator_lock_);
1102   static ObjPtr<mirror::DexCache> DecodeDexCacheLocked(Thread* self, const DexCacheData* data)
1103       REQUIRES_SHARED(Locks::dex_lock_, Locks::mutator_lock_);
1104   bool IsSameClassLoader(ObjPtr<mirror::DexCache> dex_cache,
1105                          const DexCacheData* data,
1106                          ObjPtr<mirror::ClassLoader> class_loader)
1107       REQUIRES_SHARED(Locks::dex_lock_, Locks::mutator_lock_);
1108 
1109   bool InitializeDefaultInterfaceRecursive(Thread* self,
1110                                            Handle<mirror::Class> klass,
1111                                            bool can_run_clinit,
1112                                            bool can_init_parents)
1113       REQUIRES(!Locks::dex_lock_)
1114       REQUIRES_SHARED(Locks::mutator_lock_);
1115   bool WaitForInitializeClass(Handle<mirror::Class> klass,
1116                               Thread* self,
1117                               ObjectLock<mirror::Class>& lock);
1118 
1119   bool IsSameDescriptorInDifferentClassContexts(Thread* self,
1120                                                 const char* descriptor,
1121                                                 Handle<mirror::ClassLoader> class_loader1,
1122                                                 Handle<mirror::ClassLoader> class_loader2)
1123       REQUIRES_SHARED(Locks::mutator_lock_);
1124 
1125   bool IsSameMethodSignatureInDifferentClassContexts(Thread* self,
1126                                                      ArtMethod* method,
1127                                                      ObjPtr<mirror::Class> klass1,
1128                                                      ObjPtr<mirror::Class> klass2)
1129       REQUIRES_SHARED(Locks::mutator_lock_);
1130 
1131   bool LinkSuperClass(Handle<mirror::Class> klass)
1132       REQUIRES_SHARED(Locks::mutator_lock_);
1133 
1134   bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
1135       REQUIRES_SHARED(Locks::mutator_lock_)
1136       REQUIRES(!Locks::dex_lock_);
1137 
1138   bool LinkMethods(Thread* self,
1139                    Handle<mirror::Class> klass,
1140                    Handle<mirror::ObjectArray<mirror::Class>> interfaces,
1141                    bool* out_new_conflict,
1142                    ArtMethod** out_imt)
1143       REQUIRES_SHARED(Locks::mutator_lock_);
1144 
1145   ObjPtr<mirror::MethodHandle> ResolveMethodHandleForField(
1146       Thread* self,
1147       const dex::MethodHandleItem& method_handle,
1148       ArtMethod* referrer) REQUIRES_SHARED(Locks::mutator_lock_);
1149 
1150   ObjPtr<mirror::MethodHandle> ResolveMethodHandleForMethod(
1151       Thread* self,
1152       const dex::MethodHandleItem& method_handle,
1153       ArtMethod* referrer) REQUIRES_SHARED(Locks::mutator_lock_);
1154 
1155   // Links the virtual methods for the given class and records any default methods that will need to
1156   // be updated later.
1157   //
1158   // Arguments:
1159   // * self - The current thread.
1160   // * klass - class, whose vtable will be filled in.
1161   // * default_translations - Vtable index to new method map.
1162   //                          Any vtable entries that need to be updated with new default methods
1163   //                          are stored into the default_translations map. The default_translations
1164   //                          map is keyed on the vtable index that needs to be updated. We use this
1165   //                          map because if we override a default method with another default
1166   //                          method we need to update the vtable to point to the new method.
1167   //                          Unfortunately since we copy the ArtMethod* we cannot just do a simple
1168   //                          scan, we therefore store the vtable index's that might need to be
1169   //                          updated with the method they will turn into.
1170   // TODO This whole default_translations thing is very dirty. There should be a better way.
1171   bool LinkVirtualMethods(
1172         Thread* self,
1173         Handle<mirror::Class> klass,
1174         /*out*/HashMap<size_t, MethodTranslation>* default_translations)
1175       REQUIRES_SHARED(Locks::mutator_lock_);
1176 
1177   // Sets up the interface lookup table (IFTable) in the correct order to allow searching for
1178   // default methods.
1179   bool SetupInterfaceLookupTable(Thread* self,
1180                                  Handle<mirror::Class> klass,
1181                                  Handle<mirror::ObjectArray<mirror::Class>> interfaces)
1182       REQUIRES_SHARED(Locks::mutator_lock_);
1183 
1184 
1185   enum class DefaultMethodSearchResult {
1186     kDefaultFound,
1187     kAbstractFound,
1188     kDefaultConflict
1189   };
1190 
1191   // Find the default method implementation for 'interface_method' in 'klass', if one exists.
1192   //
1193   // Arguments:
1194   // * self - The current thread.
1195   // * target_method - The method we are trying to find a default implementation for.
1196   // * klass - The class we are searching for a definition of target_method.
1197   // * out_default_method - The pointer we will store the found default method to on success.
1198   //
1199   // Return value:
1200   // * kDefaultFound - There were no conflicting method implementations found in the class while
1201   //                   searching for target_method. The default method implementation is stored into
1202   //                   out_default_method.
1203   // * kAbstractFound - There were no conflicting method implementations found in the class while
1204   //                   searching for target_method but no default implementation was found either.
1205   //                   out_default_method is set to null and the method should be considered not
1206   //                   implemented.
1207   // * kDefaultConflict - Conflicting method implementations were found when searching for
1208   //                      target_method. The value of *out_default_method is null.
1209   DefaultMethodSearchResult FindDefaultMethodImplementation(
1210       Thread* self,
1211       ArtMethod* target_method,
1212       Handle<mirror::Class> klass,
1213       /*out*/ArtMethod** out_default_method) const
1214       REQUIRES_SHARED(Locks::mutator_lock_);
1215 
1216   // Sets the imt entries and fixes up the vtable for the given class by linking all the interface
1217   // methods. See LinkVirtualMethods for an explanation of what default_translations is.
1218   bool LinkInterfaceMethods(
1219       Thread* self,
1220       Handle<mirror::Class> klass,
1221       const HashMap<size_t, MethodTranslation>& default_translations,
1222       bool* out_new_conflict,
1223       ArtMethod** out_imt)
1224       REQUIRES_SHARED(Locks::mutator_lock_);
1225 
1226   bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size)
1227       REQUIRES_SHARED(Locks::mutator_lock_);
1228   bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass)
1229       REQUIRES_SHARED(Locks::mutator_lock_);
1230   void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
1231       REQUIRES_SHARED(Locks::mutator_lock_);
1232 
1233   void CheckProxyConstructor(ArtMethod* constructor) const
1234       REQUIRES_SHARED(Locks::mutator_lock_);
1235   void CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const
1236       REQUIRES_SHARED(Locks::mutator_lock_);
1237 
GetDexCacheCount()1238   size_t GetDexCacheCount() REQUIRES_SHARED(Locks::mutator_lock_, Locks::dex_lock_) {
1239     return dex_caches_.size();
1240   }
GetDexCachesData()1241   const std::list<DexCacheData>& GetDexCachesData()
1242       REQUIRES_SHARED(Locks::mutator_lock_, Locks::dex_lock_) {
1243     return dex_caches_;
1244   }
1245 
1246   void CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out)
1247       REQUIRES_SHARED(Locks::mutator_lock_);
1248   void CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, ArtMethod* out)
1249       REQUIRES_SHARED(Locks::mutator_lock_);
1250 
1251   // Register a class loader and create its class table and allocator. Should not be called if
1252   // these are already created.
1253   void RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader)
1254       REQUIRES_SHARED(Locks::mutator_lock_)
1255       REQUIRES(Locks::classlinker_classes_lock_);
1256 
1257   // Insert a new class table if not found.
1258   ClassTable* InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader)
1259       REQUIRES_SHARED(Locks::mutator_lock_)
1260       REQUIRES(Locks::classlinker_classes_lock_);
1261 
1262   // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
1263   // before returning it to the caller. Its the responsibility of the thread that placed the class
1264   // in the table to make it resolved. The thread doing resolution must notify on the class' lock
1265   // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
1266   // retire a class, the version of the class in the table is returned and this may differ from
1267   // the class passed in.
1268   ObjPtr<mirror::Class> EnsureResolved(Thread* self,
1269                                        const char* descriptor,
1270                                        ObjPtr<mirror::Class> klass)
1271       WARN_UNUSED
1272       REQUIRES_SHARED(Locks::mutator_lock_)
1273       REQUIRES(!Locks::dex_lock_);
1274 
1275   void FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class,
1276                                     ObjPtr<mirror::Class> new_class)
1277       REQUIRES_SHARED(Locks::mutator_lock_);
1278 
1279   void SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass)
1280       REQUIRES_SHARED(Locks::mutator_lock_);
1281 
1282   // Return the quick generic JNI stub for testing.
1283   const void* GetRuntimeQuickGenericJniStub() const;
1284 
1285   bool CanWeInitializeClass(ObjPtr<mirror::Class> klass,
1286                             bool can_init_statics,
1287                             bool can_init_parents)
1288       REQUIRES_SHARED(Locks::mutator_lock_);
1289 
1290   void UpdateClassMethods(ObjPtr<mirror::Class> klass,
1291                           LengthPrefixedArray<ArtMethod>* new_methods)
1292       REQUIRES_SHARED(Locks::mutator_lock_)
1293       REQUIRES(!Locks::classlinker_classes_lock_);
1294 
1295   // Check that c1 == FindSystemClass(self, descriptor). Abort with class dumps otherwise.
1296   void CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor)
1297       REQUIRES(!Locks::dex_lock_)
1298       REQUIRES_SHARED(Locks::mutator_lock_);
1299 
1300   // Allocate method arrays for interfaces.
1301   bool AllocateIfTableMethodArrays(Thread* self,
1302                                    Handle<mirror::Class> klass,
1303                                    Handle<mirror::IfTable> iftable)
1304       REQUIRES_SHARED(Locks::mutator_lock_);
1305 
1306   // Sets imt_ref appropriately for LinkInterfaceMethods.
1307   // If there is no method in the imt location of imt_ref it will store the given method there.
1308   // Otherwise it will set the conflict method which will figure out which method to use during
1309   // runtime.
1310   void SetIMTRef(ArtMethod* unimplemented_method,
1311                  ArtMethod* imt_conflict_method,
1312                  ArtMethod* current_method,
1313                  /*out*/bool* new_conflict,
1314                  /*out*/ArtMethod** imt_ref) REQUIRES_SHARED(Locks::mutator_lock_);
1315 
1316   void FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table,
1317                           ArtMethod* unimplemented_method,
1318                           ArtMethod* imt_conflict_method,
1319                           ObjPtr<mirror::Class> klass,
1320                           bool create_conflict_tables,
1321                           bool ignore_copied_methods,
1322                           /*out*/bool* new_conflict,
1323                           /*out*/ArtMethod** imt) REQUIRES_SHARED(Locks::mutator_lock_);
1324 
1325   void FillImtFromSuperClass(Handle<mirror::Class> klass,
1326                              ArtMethod* unimplemented_method,
1327                              ArtMethod* imt_conflict_method,
1328                              bool* new_conflict,
1329                              ArtMethod** imt) REQUIRES_SHARED(Locks::mutator_lock_);
1330 
1331   // Check invoke type against the referenced class. Throws IncompatibleClassChangeError
1332   // (if `kThrowOnError`) and returns true on mismatch (kInterface on a non-interface class,
1333   // kVirtual on interface, kDefault on interface for dex files not supporting default methods),
1334   // otherwise returns false.
1335   template <bool kThrowOnError, typename ClassGetter>
1336   static bool CheckInvokeClassMismatch(ObjPtr<mirror::DexCache> dex_cache,
1337                                        InvokeType type,
1338                                        ClassGetter class_getter)
1339       REQUIRES_SHARED(Locks::mutator_lock_);
1340   // Helper that feeds the above function with `ClassGetter` doing `LookupResolvedType()`.
1341   template <bool kThrow>
1342   bool CheckInvokeClassMismatch(ObjPtr<mirror::DexCache> dex_cache,
1343                                 InvokeType type,
1344                                 uint32_t method_idx,
1345                                 ObjPtr<mirror::ClassLoader> class_loader)
1346       REQUIRES_SHARED(Locks::mutator_lock_);
1347 
1348   ObjPtr<mirror::IfTable> GetArrayIfTable() REQUIRES_SHARED(Locks::mutator_lock_);
1349 
1350   std::vector<const DexFile*> boot_class_path_;
1351   std::vector<std::unique_ptr<const DexFile>> boot_dex_files_;
1352 
1353   // JNI weak globals and side data to allow dex caches to get unloaded. We lazily delete weak
1354   // globals when we register new dex files.
1355   std::list<DexCacheData> dex_caches_ GUARDED_BY(Locks::dex_lock_);
1356 
1357   // This contains the class loaders which have class tables. It is populated by
1358   // InsertClassTableForClassLoader.
1359   std::list<ClassLoaderData> class_loaders_
1360       GUARDED_BY(Locks::classlinker_classes_lock_);
1361 
1362   // Boot class path table. Since the class loader for this is null.
1363   std::unique_ptr<ClassTable> boot_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
1364 
1365   // New class roots, only used by CMS since the GC needs to mark these in the pause.
1366   std::vector<GcRoot<mirror::Class>> new_class_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
1367 
1368   // Boot image oat files with new .bss GC roots to be visited in the pause by CMS.
1369   std::vector<const OatFile*> new_bss_roots_boot_oat_files_
1370       GUARDED_BY(Locks::classlinker_classes_lock_);
1371 
1372   // Number of times we've searched dex caches for a class. After a certain number of misses we move
1373   // the classes into the class_table_ to avoid dex cache based searches.
1374   Atomic<uint32_t> failed_dex_cache_class_lookups_;
1375 
1376   // Well known mirror::Class roots.
1377   GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
1378 
1379   // Method hashes for virtual methods from java.lang.Object used
1380   // to avoid recalculating them for each class we link.
1381   uint32_t object_virtual_method_hashes_[mirror::Object::kVTableLength];
1382 
1383   // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
1384   // descriptors for the sake of performing FindClass.
1385   static constexpr size_t kFindArrayCacheSize = 16;
1386   GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
1387   size_t find_array_class_cache_next_victim_;
1388 
1389   bool init_done_;
1390   bool log_new_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
1391 
1392   InternTable* intern_table_;
1393 
1394   const bool fast_class_not_found_exceptions_;
1395 
1396   // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
1397   // patch point within the image. TODO: make these proper relocations.
1398   const void* jni_dlsym_lookup_trampoline_;
1399   const void* jni_dlsym_lookup_critical_trampoline_;
1400   const void* quick_resolution_trampoline_;
1401   const void* quick_imt_conflict_trampoline_;
1402   const void* quick_generic_jni_trampoline_;
1403   const void* quick_to_interpreter_bridge_trampoline_;
1404   const void* nterp_trampoline_;
1405 
1406   // Image pointer size.
1407   PointerSize image_pointer_size_;
1408 
1409   // Classes to transition from ClassStatus::kInitialized to ClassStatus::kVisiblyInitialized.
1410   Mutex visibly_initialized_callback_lock_;
1411   std::unique_ptr<VisiblyInitializedCallback> visibly_initialized_callback_
1412       GUARDED_BY(visibly_initialized_callback_lock_);
1413   IntrusiveForwardList<VisiblyInitializedCallback> running_visibly_initialized_callbacks_
1414       GUARDED_BY(visibly_initialized_callback_lock_);
1415 
1416   // Registered native code for @CriticalNative methods of classes that are not visibly
1417   // initialized. These code pointers cannot be stored in ArtMethod as that would risk
1418   // skipping the class initialization check for direct calls from compiled code.
1419   Mutex critical_native_code_with_clinit_check_lock_;
1420   std::map<ArtMethod*, void*> critical_native_code_with_clinit_check_
1421       GUARDED_BY(critical_native_code_with_clinit_check_lock_);
1422 
1423   std::unique_ptr<ClassHierarchyAnalysis> cha_;
1424 
1425   class FindVirtualMethodHolderVisitor;
1426 
1427   friend class AppImageLoadingHelper;
1428   friend class ImageDumper;  // for DexLock
1429   friend struct linker::CompilationHelper;  // For Compile in ImageTest.
1430   friend class linker::ImageWriter;  // for GetClassRoots
1431   friend class JniCompilerTest;  // for GetRuntimeQuickGenericJniStub
1432   friend class JniInternalTest;  // for GetRuntimeQuickGenericJniStub
1433   friend class VMClassLoader;  // for LookupClass and FindClassInBaseDexClassLoader.
1434   ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName);  // for DexLock, and RegisterDexFileLocked
1435   ART_FRIEND_TEST(mirror::DexCacheMethodHandlesTest, Open);  // for AllocDexCache
1436   ART_FRIEND_TEST(mirror::DexCacheTest, Open);  // for AllocDexCache
1437   DISALLOW_COPY_AND_ASSIGN(ClassLinker);
1438 };
1439 
1440 class ClassLoadCallback {
1441  public:
~ClassLoadCallback()1442   virtual ~ClassLoadCallback() {}
1443 
1444   // Called immediately before beginning class-definition and immediately before returning from it.
BeginDefineClass()1445   virtual void BeginDefineClass() REQUIRES_SHARED(Locks::mutator_lock_) {}
EndDefineClass()1446   virtual void EndDefineClass() REQUIRES_SHARED(Locks::mutator_lock_) {}
1447 
1448   // If set we will replace initial_class_def & initial_dex_file with the final versions. The
1449   // callback author is responsible for ensuring these are allocated in such a way they can be
1450   // cleaned up if another transformation occurs. Note that both must be set or null/unchanged on
1451   // return.
1452   // Note: the class may be temporary, in which case a following ClassPrepare event will be a
1453   //       different object. It is the listener's responsibility to handle this.
1454   // Note: This callback is rarely useful so a default implementation has been given that does
1455   //       nothing.
ClassPreDefine(const char * descriptor ATTRIBUTE_UNUSED,Handle<mirror::Class> klass ATTRIBUTE_UNUSED,Handle<mirror::ClassLoader> class_loader ATTRIBUTE_UNUSED,const DexFile & initial_dex_file ATTRIBUTE_UNUSED,const dex::ClassDef & initial_class_def ATTRIBUTE_UNUSED,DexFile const ** final_dex_file ATTRIBUTE_UNUSED,dex::ClassDef const ** final_class_def ATTRIBUTE_UNUSED)1456   virtual void ClassPreDefine(const char* descriptor ATTRIBUTE_UNUSED,
1457                               Handle<mirror::Class> klass ATTRIBUTE_UNUSED,
1458                               Handle<mirror::ClassLoader> class_loader ATTRIBUTE_UNUSED,
1459                               const DexFile& initial_dex_file ATTRIBUTE_UNUSED,
1460                               const dex::ClassDef& initial_class_def ATTRIBUTE_UNUSED,
1461                               /*out*/DexFile const** final_dex_file ATTRIBUTE_UNUSED,
1462                               /*out*/dex::ClassDef const** final_class_def ATTRIBUTE_UNUSED)
1463       REQUIRES_SHARED(Locks::mutator_lock_) {}
1464 
1465   // A class has been loaded.
1466   // Note: the class may be temporary, in which case a following ClassPrepare event will be a
1467   //       different object. It is the listener's responsibility to handle this.
1468   virtual void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
1469 
1470   // A class has been prepared, i.e., resolved. As the ClassLoad event might have been for a
1471   // temporary class, provide both the former and the current class.
1472   virtual void ClassPrepare(Handle<mirror::Class> temp_klass,
1473                             Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
1474 };
1475 
1476 }  // namespace art
1477 
1478 #endif  // ART_RUNTIME_CLASS_LINKER_H_
1479