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