class_linker.h revision 97d7e1cd7f733cb33a0e238bec6d7ed525638cd1
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 <set>
21#include <string>
22#include <unordered_map>
23#include <unordered_set>
24#include <utility>
25#include <vector>
26
27#include "base/allocator.h"
28#include "base/enums.h"
29#include "base/hash_set.h"
30#include "base/macros.h"
31#include "base/mutex.h"
32#include "class_table.h"
33#include "dex_cache_resolved_classes.h"
34#include "dex_file.h"
35#include "gc_root.h"
36#include "jni.h"
37#include "mirror/class.h"
38#include "object_callbacks.h"
39#include "verifier/verifier_log_mode.h"
40
41namespace art {
42
43namespace gc {
44namespace space {
45  class ImageSpace;
46}  // namespace space
47}  // namespace gc
48namespace mirror {
49  class ClassLoader;
50  class DexCache;
51  class DexCachePointerArray;
52  class DexCacheMethodHandlesTest_Open_Test;
53  class DexCacheTest_Open_Test;
54  class IfTable;
55  class MethodType;
56  template<class T> class ObjectArray;
57  class StackTraceElement;
58}  // namespace mirror
59
60template<class T> class Handle;
61class ImtConflictTable;
62template<typename T> class LengthPrefixedArray;
63template<class T> class MutableHandle;
64class InternTable;
65template<class T> class ObjectLock;
66class Runtime;
67class ScopedObjectAccessAlreadyRunnable;
68template<size_t kNumReferences> class PACKED(4) StackHandleScope;
69
70enum VisitRootFlags : uint8_t;
71
72class ClassVisitor {
73 public:
74  virtual ~ClassVisitor() {}
75  // Return true to continue visiting.
76  virtual bool operator()(mirror::Class* klass) = 0;
77};
78
79class ClassLoaderVisitor {
80 public:
81  virtual ~ClassLoaderVisitor() {}
82  virtual void Visit(mirror::ClassLoader* class_loader)
83      REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) = 0;
84};
85
86class ClassLinker {
87 public:
88  // Well known mirror::Class roots accessed via GetClassRoot.
89  enum ClassRoot {
90    kJavaLangClass,
91    kJavaLangObject,
92    kClassArrayClass,
93    kObjectArrayClass,
94    kJavaLangString,
95    kJavaLangDexCache,
96    kJavaLangRefReference,
97    kJavaLangReflectConstructor,
98    kJavaLangReflectField,
99    kJavaLangReflectMethod,
100    kJavaLangReflectProxy,
101    kJavaLangStringArrayClass,
102    kJavaLangReflectConstructorArrayClass,
103    kJavaLangReflectFieldArrayClass,
104    kJavaLangReflectMethodArrayClass,
105    kJavaLangInvokeMethodHandleImpl,
106    kJavaLangInvokeMethodType,
107    kJavaLangClassLoader,
108    kJavaLangThrowable,
109    kJavaLangClassNotFoundException,
110    kJavaLangStackTraceElement,
111    kPrimitiveBoolean,
112    kPrimitiveByte,
113    kPrimitiveChar,
114    kPrimitiveDouble,
115    kPrimitiveFloat,
116    kPrimitiveInt,
117    kPrimitiveLong,
118    kPrimitiveShort,
119    kPrimitiveVoid,
120    kBooleanArrayClass,
121    kByteArrayClass,
122    kCharArrayClass,
123    kDoubleArrayClass,
124    kFloatArrayClass,
125    kIntArrayClass,
126    kLongArrayClass,
127    kShortArrayClass,
128    kJavaLangStackTraceElementArrayClass,
129    kClassRootsMax,
130  };
131
132  explicit ClassLinker(InternTable* intern_table);
133  ~ClassLinker();
134
135  // Initialize class linker by bootstraping from dex files.
136  bool InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,
137                        std::string* error_msg)
138      REQUIRES_SHARED(Locks::mutator_lock_)
139      REQUIRES(!dex_lock_);
140
141  // Initialize class linker from one or more boot images.
142  bool InitFromBootImage(std::string* error_msg)
143      REQUIRES_SHARED(Locks::mutator_lock_)
144      REQUIRES(!dex_lock_);
145
146  // Add an image space to the class linker, may fix up classloader fields and dex cache fields.
147  // The dex files that were newly opened for the space are placed in the out argument
148  // out_dex_files. Returns true if the operation succeeded.
149  // The space must be already added to the heap before calling AddImageSpace since we need to
150  // properly handle read barriers and object marking.
151  bool AddImageSpace(gc::space::ImageSpace* space,
152                     Handle<mirror::ClassLoader> class_loader,
153                     jobjectArray dex_elements,
154                     const char* dex_location,
155                     std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
156                     std::string* error_msg)
157      REQUIRES(!dex_lock_)
158      REQUIRES_SHARED(Locks::mutator_lock_);
159
160  bool OpenImageDexFiles(gc::space::ImageSpace* space,
161                         std::vector<std::unique_ptr<const DexFile>>* out_dex_files,
162                         std::string* error_msg)
163      REQUIRES(!dex_lock_)
164      REQUIRES_SHARED(Locks::mutator_lock_);
165
166  // Finds a class by its descriptor, loading it if necessary.
167  // If class_loader is null, searches boot_class_path_.
168  mirror::Class* FindClass(Thread* self,
169                           const char* descriptor,
170                           Handle<mirror::ClassLoader> class_loader)
171      REQUIRES_SHARED(Locks::mutator_lock_)
172      REQUIRES(!dex_lock_);
173
174  // Finds a class in the path class loader, loading it if necessary without using JNI. Hash
175  // function is supposed to be ComputeModifiedUtf8Hash(descriptor). Returns true if the
176  // class-loader chain could be handled, false otherwise, i.e., a non-supported class-loader
177  // was encountered while walking the parent chain (currently only BootClassLoader and
178  // PathClassLoader are supported).
179  bool FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
180                                  Thread* self,
181                                  const char* descriptor,
182                                  size_t hash,
183                                  Handle<mirror::ClassLoader> class_loader,
184                                  mirror::Class** result)
185      REQUIRES_SHARED(Locks::mutator_lock_)
186      REQUIRES(!dex_lock_);
187
188  // Finds a class by its descriptor using the "system" class loader, ie by searching the
189  // boot_class_path_.
190  mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
191      REQUIRES_SHARED(Locks::mutator_lock_)
192      REQUIRES(!dex_lock_);
193
194  // Finds the array class given for the element class.
195  mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
196      REQUIRES_SHARED(Locks::mutator_lock_)
197      REQUIRES(!dex_lock_);
198
199  // Returns true if the class linker is initialized.
200  bool IsInitialized() const {
201    return init_done_;
202  }
203
204  // Define a new a class based on a ClassDef from a DexFile
205  mirror::Class* DefineClass(Thread* self,
206                             const char* descriptor,
207                             size_t hash,
208                             Handle<mirror::ClassLoader> class_loader,
209                             const DexFile& dex_file,
210                             const DexFile::ClassDef& dex_class_def)
211      REQUIRES_SHARED(Locks::mutator_lock_)
212      REQUIRES(!dex_lock_);
213
214  // Finds a class by its descriptor, returning null if it isn't wasn't loaded
215  // by the given 'class_loader'.
216  mirror::Class* LookupClass(Thread* self,
217                             const char* descriptor,
218                             size_t hash,
219                             mirror::ClassLoader* class_loader)
220      REQUIRES(!Locks::classlinker_classes_lock_)
221      REQUIRES_SHARED(Locks::mutator_lock_);
222
223  // Finds all the classes with the given descriptor, regardless of ClassLoader.
224  void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
225      REQUIRES(!Locks::classlinker_classes_lock_)
226      REQUIRES_SHARED(Locks::mutator_lock_);
227
228  mirror::Class* FindPrimitiveClass(char type) REQUIRES_SHARED(Locks::mutator_lock_);
229
230  // General class unloading is not supported, this is used to prune
231  // unwanted classes during image writing.
232  bool RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader)
233      REQUIRES(!Locks::classlinker_classes_lock_)
234      REQUIRES_SHARED(Locks::mutator_lock_);
235
236  void DumpAllClasses(int flags)
237      REQUIRES(!Locks::classlinker_classes_lock_)
238      REQUIRES_SHARED(Locks::mutator_lock_);
239
240  void DumpForSigQuit(std::ostream& os) REQUIRES(!Locks::classlinker_classes_lock_);
241
242  size_t NumLoadedClasses()
243      REQUIRES(!Locks::classlinker_classes_lock_)
244      REQUIRES_SHARED(Locks::mutator_lock_);
245
246  // Resolve a String with the given index from the DexFile, storing the
247  // result in the DexCache. The referrer is used to identify the
248  // target DexCache and ClassLoader to use for resolution.
249  mirror::String* ResolveString(uint32_t string_idx, ArtMethod* referrer)
250      REQUIRES_SHARED(Locks::mutator_lock_);
251
252  // Resolve a String with the given index from the DexFile, storing the
253  // result in the DexCache.
254  mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
255                                Handle<mirror::DexCache> dex_cache)
256      REQUIRES_SHARED(Locks::mutator_lock_);
257
258  // Find a String with the given index from the DexFile, storing the
259  // result in the DexCache if found. Return null if not found.
260  mirror::String* LookupString(const DexFile& dex_file, uint32_t string_idx,
261                               Handle<mirror::DexCache> dex_cache)
262      REQUIRES_SHARED(Locks::mutator_lock_);
263
264  // Resolve a Type with the given index from the DexFile, storing the
265  // result in the DexCache. The referrer is used to identify the
266  // target DexCache and ClassLoader to use for resolution.
267  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
268      REQUIRES_SHARED(Locks::mutator_lock_)
269      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
270
271  // Resolve a Type with the given index from the DexFile, storing the
272  // result in the DexCache. The referrer is used to identify the
273  // target DexCache and ClassLoader to use for resolution.
274  mirror::Class* ResolveType(uint16_t type_idx, ArtMethod* referrer)
275      REQUIRES_SHARED(Locks::mutator_lock_)
276      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
277
278  mirror::Class* ResolveType(uint16_t type_idx, ArtField* referrer)
279      REQUIRES_SHARED(Locks::mutator_lock_)
280      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
281
282  // Look up a resolved type with the given ID from the DexFile. The ClassLoader is used to search
283  // for the type, since it may be referenced from but not contained within the given DexFile.
284  ObjPtr<mirror::Class> LookupResolvedType(const DexFile& dex_file,
285                                           uint16_t type_idx,
286                                           ObjPtr<mirror::DexCache> dex_cache,
287                                           ObjPtr<mirror::ClassLoader> class_loader)
288      REQUIRES_SHARED(Locks::mutator_lock_);
289
290  // Resolve a type with the given ID from the DexFile, storing the
291  // result in DexCache. The ClassLoader is used to search for the
292  // type, since it may be referenced from but not contained within
293  // the given DexFile.
294  mirror::Class* ResolveType(const DexFile& dex_file,
295                             uint16_t type_idx,
296                             Handle<mirror::DexCache> dex_cache,
297                             Handle<mirror::ClassLoader> class_loader)
298      REQUIRES_SHARED(Locks::mutator_lock_)
299      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
300
301  // Determine whether a dex cache result should be trusted, or an IncompatibleClassChangeError
302  // check should be performed even after a hit.
303  enum ResolveMode {  // private.
304    kNoICCECheckForCache,
305    kForceICCECheck
306  };
307
308  // Resolve a method with a given ID from the DexFile, storing the
309  // result in DexCache. The ClassLinker and ClassLoader are used as
310  // in ResolveType. What is unique is the method type argument which
311  // is used to determine if this method is a direct, static, or
312  // virtual method.
313  template <ResolveMode kResolveMode>
314  ArtMethod* ResolveMethod(const DexFile& dex_file,
315                           uint32_t method_idx,
316                           Handle<mirror::DexCache> dex_cache,
317                           Handle<mirror::ClassLoader> class_loader,
318                           ArtMethod* referrer,
319                           InvokeType type)
320      REQUIRES_SHARED(Locks::mutator_lock_)
321      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
322
323  ArtMethod* GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer)
324      REQUIRES_SHARED(Locks::mutator_lock_);
325
326  // This returns the class referred to by GetMethodId(method_idx).class_idx_. This might be
327  // different then the declaring class of the resolved method due to copied
328  // miranda/default/conflict methods.
329  mirror::Class* ResolveReferencedClassOfMethod(uint32_t method_idx,
330                                                Handle<mirror::DexCache> dex_cache,
331                                                Handle<mirror::ClassLoader> class_loader)
332      REQUIRES_SHARED(Locks::mutator_lock_)
333      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
334  template <ResolveMode kResolveMode>
335  ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, ArtMethod* referrer, InvokeType type)
336      REQUIRES_SHARED(Locks::mutator_lock_)
337      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
338  ArtMethod* ResolveMethodWithoutInvokeType(const DexFile& dex_file,
339                                            uint32_t method_idx,
340                                            Handle<mirror::DexCache> dex_cache,
341                                            Handle<mirror::ClassLoader> class_loader)
342      REQUIRES_SHARED(Locks::mutator_lock_)
343      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
344
345  ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
346      REQUIRES_SHARED(Locks::mutator_lock_);
347  ArtField* GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache)
348      REQUIRES_SHARED(Locks::mutator_lock_);
349  ArtField* ResolveField(uint32_t field_idx, ArtMethod* referrer, bool is_static)
350      REQUIRES_SHARED(Locks::mutator_lock_)
351      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
352
353  // Resolve a field with a given ID from the DexFile, storing the
354  // result in DexCache. The ClassLinker and ClassLoader are used as
355  // in ResolveType. What is unique is the is_static argument which is
356  // used to determine if we are resolving a static or non-static
357  // field.
358  ArtField* ResolveField(const DexFile& dex_file, uint32_t field_idx,
359                         Handle<mirror::DexCache> dex_cache,
360                         Handle<mirror::ClassLoader> class_loader, bool is_static)
361      REQUIRES_SHARED(Locks::mutator_lock_)
362      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
363
364  // Resolve a field with a given ID from the DexFile, storing the
365  // result in DexCache. The ClassLinker and ClassLoader are used as
366  // in ResolveType. No is_static argument is provided so that Java
367  // field resolution semantics are followed.
368  ArtField* ResolveFieldJLS(const DexFile& dex_file,
369                            uint32_t field_idx,
370                            Handle<mirror::DexCache> dex_cache,
371                            Handle<mirror::ClassLoader> class_loader)
372      REQUIRES_SHARED(Locks::mutator_lock_)
373      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
374
375  // Resolve a method type with a given ID from the DexFile, storing
376  // the result in the DexCache.
377  mirror::MethodType* ResolveMethodType(const DexFile& dex_file,
378                                        uint32_t proto_idx,
379                                        Handle<mirror::DexCache> dex_cache,
380                                        Handle<mirror::ClassLoader> class_loader)
381      REQUIRES_SHARED(Locks::mutator_lock_)
382      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
383
384  // Get shorty from method index without resolution. Used to do handlerization.
385  const char* MethodShorty(uint32_t method_idx, ArtMethod* referrer, uint32_t* length)
386      REQUIRES_SHARED(Locks::mutator_lock_);
387
388  // Returns true on success, false if there's an exception pending.
389  // can_run_clinit=false allows the compiler to attempt to init a class,
390  // given the restriction that no <clinit> execution is possible.
391  bool EnsureInitialized(Thread* self,
392                         Handle<mirror::Class> c,
393                         bool can_init_fields,
394                         bool can_init_parents)
395      REQUIRES_SHARED(Locks::mutator_lock_)
396      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
397
398  // Initializes classes that have instances in the image but that have
399  // <clinit> methods so they could not be initialized by the compiler.
400  void RunRootClinits()
401      REQUIRES_SHARED(Locks::mutator_lock_)
402      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
403
404  mirror::DexCache* RegisterDexFile(const DexFile& dex_file,
405                                    mirror::ClassLoader* class_loader)
406      REQUIRES(!dex_lock_)
407      REQUIRES_SHARED(Locks::mutator_lock_);
408  void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
409      REQUIRES(!dex_lock_)
410      REQUIRES_SHARED(Locks::mutator_lock_);
411
412  const std::vector<const DexFile*>& GetBootClassPath() {
413    return boot_class_path_;
414  }
415
416  void VisitClasses(ClassVisitor* visitor)
417      REQUIRES(!Locks::classlinker_classes_lock_)
418      REQUIRES_SHARED(Locks::mutator_lock_);
419
420  // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage
421  // so that it can visit individual classes without holding the doesn't hold the
422  // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code
423  // can race with insertion and deletion of classes while the visitor is being called.
424  void VisitClassesWithoutClassesLock(ClassVisitor* visitor)
425      REQUIRES_SHARED(Locks::mutator_lock_)
426      REQUIRES(!dex_lock_);
427
428  void VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags)
429      REQUIRES(!Locks::classlinker_classes_lock_)
430      REQUIRES_SHARED(Locks::mutator_lock_);
431  void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
432      REQUIRES(!dex_lock_)
433      REQUIRES_SHARED(Locks::mutator_lock_);
434
435  mirror::DexCache* FindDexCache(Thread* self,
436                                 const DexFile& dex_file,
437                                 bool allow_failure = false)
438      REQUIRES(!dex_lock_)
439      REQUIRES_SHARED(Locks::mutator_lock_);
440  void FixupDexCaches(ArtMethod* resolution_method)
441      REQUIRES(!dex_lock_)
442      REQUIRES_SHARED(Locks::mutator_lock_);
443
444  // Allocate an instance of a java.lang.Object.
445  mirror::Object* AllocObject(Thread* self)
446      REQUIRES_SHARED(Locks::mutator_lock_)
447      REQUIRES(!Roles::uninterruptible_);
448
449  // TODO: replace this with multiple methods that allocate the correct managed type.
450  template <class T>
451  mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
452      REQUIRES_SHARED(Locks::mutator_lock_)
453      REQUIRES(!Roles::uninterruptible_);
454
455  mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
456      REQUIRES_SHARED(Locks::mutator_lock_)
457      REQUIRES(!Roles::uninterruptible_);
458
459  mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
460      REQUIRES_SHARED(Locks::mutator_lock_)
461      REQUIRES(!Roles::uninterruptible_);
462
463  LengthPrefixedArray<ArtField>* AllocArtFieldArray(Thread* self,
464                                                    LinearAlloc* allocator,
465                                                    size_t length);
466
467  LengthPrefixedArray<ArtMethod>* AllocArtMethodArray(Thread* self,
468                                                      LinearAlloc* allocator,
469                                                      size_t length);
470
471  mirror::PointerArray* AllocPointerArray(Thread* self, size_t length)
472      REQUIRES_SHARED(Locks::mutator_lock_)
473      REQUIRES(!Roles::uninterruptible_);
474
475  mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
476      REQUIRES_SHARED(Locks::mutator_lock_)
477      REQUIRES(!Roles::uninterruptible_);
478
479  mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
480                                                                              size_t length)
481      REQUIRES_SHARED(Locks::mutator_lock_)
482      REQUIRES(!Roles::uninterruptible_);
483
484  void VerifyClass(Thread* self,
485                   Handle<mirror::Class> klass,
486                   verifier::HardFailLogMode log_level = verifier::HardFailLogMode::kLogNone)
487      REQUIRES_SHARED(Locks::mutator_lock_)
488      REQUIRES(!dex_lock_);
489  bool VerifyClassUsingOatFile(const DexFile& dex_file,
490                               mirror::Class* klass,
491                               mirror::Class::Status& oat_file_class_status)
492      REQUIRES_SHARED(Locks::mutator_lock_)
493      REQUIRES(!dex_lock_);
494  void ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass)
495      REQUIRES_SHARED(Locks::mutator_lock_)
496      REQUIRES(!dex_lock_);
497  void ResolveMethodExceptionHandlerTypes(ArtMethod* klass)
498      REQUIRES_SHARED(Locks::mutator_lock_)
499      REQUIRES(!dex_lock_);
500
501  mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa,
502                                  jstring name,
503                                  jobjectArray interfaces,
504                                  jobject loader,
505                                  jobjectArray methods,
506                                  jobjectArray throws)
507      REQUIRES_SHARED(Locks::mutator_lock_);
508  std::string GetDescriptorForProxy(mirror::Class* proxy_class)
509      REQUIRES_SHARED(Locks::mutator_lock_);
510  template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
511  ArtMethod* FindMethodForProxy(mirror::Class* proxy_class, ArtMethod* proxy_method)
512      REQUIRES(!dex_lock_)
513      REQUIRES_SHARED(Locks::mutator_lock_);
514
515  // Get the oat code for a method when its class isn't yet initialized.
516  const void* GetQuickOatCodeFor(ArtMethod* method)
517      REQUIRES_SHARED(Locks::mutator_lock_);
518
519  pid_t GetClassesLockOwner();  // For SignalCatcher.
520  pid_t GetDexLockOwner();  // For SignalCatcher.
521
522  mirror::Class* GetClassRoot(ClassRoot class_root) REQUIRES_SHARED(Locks::mutator_lock_);
523
524  static const char* GetClassRootDescriptor(ClassRoot class_root);
525
526  // Is the given entry point quick code to run the resolution stub?
527  bool IsQuickResolutionStub(const void* entry_point) const;
528
529  // Is the given entry point quick code to bridge into the interpreter?
530  bool IsQuickToInterpreterBridge(const void* entry_point) const;
531
532  // Is the given entry point quick code to run the generic JNI stub?
533  bool IsQuickGenericJniStub(const void* entry_point) const;
534
535  const void* GetQuickToInterpreterBridgeTrampoline() const {
536    return quick_to_interpreter_bridge_trampoline_;
537  }
538
539  InternTable* GetInternTable() const {
540    return intern_table_;
541  }
542
543  // Set the entrypoints up for method to the given code.
544  void SetEntryPointsToCompiledCode(ArtMethod* method, const void* method_code) const
545      REQUIRES_SHARED(Locks::mutator_lock_);
546
547  // Set the entrypoints up for method to the enter the interpreter.
548  void SetEntryPointsToInterpreter(ArtMethod* method) const
549      REQUIRES_SHARED(Locks::mutator_lock_);
550
551  // Attempts to insert a class into a class table.  Returns null if
552  // the class was inserted, otherwise returns an existing class with
553  // the same descriptor and ClassLoader.
554  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
555      REQUIRES(!Locks::classlinker_classes_lock_)
556      REQUIRES_SHARED(Locks::mutator_lock_);
557
558  mirror::ObjectArray<mirror::Class>* GetClassRoots() REQUIRES_SHARED(Locks::mutator_lock_) {
559    mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
560    DCHECK(class_roots != nullptr);
561    return class_roots;
562  }
563
564  // Move all of the boot image classes into the class table for faster lookups.
565  void AddBootImageClassesToClassTable()
566      REQUIRES(!Locks::classlinker_classes_lock_)
567      REQUIRES_SHARED(Locks::mutator_lock_);
568
569  // Add image classes to the class table.
570  void AddImageClassesToClassTable(std::vector<gc::space::ImageSpace*> image_spaces,
571                                   mirror::ClassLoader* class_loader)
572      REQUIRES(!Locks::classlinker_classes_lock_)
573      REQUIRES_SHARED(Locks::mutator_lock_);
574
575  // Move the class table to the pre-zygote table to reduce memory usage. This works by ensuring
576  // that no more classes are ever added to the pre zygote table which makes it that the pages
577  // always remain shared dirty instead of private dirty.
578  void MoveClassTableToPreZygote()
579      REQUIRES(!Locks::classlinker_classes_lock_)
580      REQUIRES_SHARED(Locks::mutator_lock_);
581
582  // Creates a GlobalRef PathClassLoader that can be used to load classes from the given dex files.
583  // Note: the objects are not completely set up. Do not use this outside of tests and the compiler.
584  jobject CreatePathClassLoader(Thread* self, const std::vector<const DexFile*>& dex_files)
585      REQUIRES_SHARED(Locks::mutator_lock_)
586      REQUIRES(!dex_lock_);
587
588  PointerSize GetImagePointerSize() const {
589    return image_pointer_size_;
590  }
591
592  // Used by image writer for checking.
593  bool ClassInClassTable(mirror::Class* klass)
594      REQUIRES(Locks::classlinker_classes_lock_)
595      REQUIRES_SHARED(Locks::mutator_lock_);
596
597  ArtMethod* CreateRuntimeMethod(LinearAlloc* linear_alloc);
598
599  // Clear the ArrayClass cache. This is necessary when cleaning up for the image, as the cache
600  // entries are roots, but potentially not image classes.
601  void DropFindArrayClassCache() REQUIRES_SHARED(Locks::mutator_lock_);
602
603  // Clean up class loaders, this needs to happen after JNI weak globals are cleared.
604  void CleanupClassLoaders()
605      REQUIRES(!Locks::classlinker_classes_lock_)
606      REQUIRES_SHARED(Locks::mutator_lock_);
607
608  // Unlike GetOrCreateAllocatorForClassLoader, GetAllocatorForClassLoader asserts that the
609  // allocator for this class loader is already created.
610  LinearAlloc* GetAllocatorForClassLoader(mirror::ClassLoader* class_loader)
611      REQUIRES_SHARED(Locks::mutator_lock_);
612
613  // Return the linear alloc for a class loader if it is already allocated, otherwise allocate and
614  // set it. TODO: Consider using a lock other than classlinker_classes_lock_.
615  LinearAlloc* GetOrCreateAllocatorForClassLoader(mirror::ClassLoader* class_loader)
616      REQUIRES(!Locks::classlinker_classes_lock_)
617      REQUIRES_SHARED(Locks::mutator_lock_);
618
619  // May be called with null class_loader due to legacy code. b/27954959
620  void InsertDexFileInToClassLoader(mirror::Object* dex_file, mirror::ClassLoader* class_loader)
621      REQUIRES(!Locks::classlinker_classes_lock_)
622      REQUIRES_SHARED(Locks::mutator_lock_);
623
624  static bool ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code)
625      REQUIRES_SHARED(Locks::mutator_lock_);
626
627  std::set<DexCacheResolvedClasses> GetResolvedClasses(bool ignore_boot_classes)
628      REQUIRES(!dex_lock_);
629
630  std::unordered_set<std::string> GetClassDescriptorsForProfileKeys(
631      const std::set<DexCacheResolvedClasses>& classes)
632      REQUIRES(!dex_lock_);
633
634  static bool IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
635                                mirror::ClassLoader* class_loader)
636      REQUIRES_SHARED(Locks::mutator_lock_);
637
638  ArtMethod* AddMethodToConflictTable(mirror::Class* klass,
639                                      ArtMethod* conflict_method,
640                                      ArtMethod* interface_method,
641                                      ArtMethod* method,
642                                      bool force_new_conflict_method)
643      REQUIRES_SHARED(Locks::mutator_lock_);
644
645  // Create a conflict table with a specified capacity.
646  ImtConflictTable* CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc);
647
648  // Static version for when the class linker is not yet created.
649  static ImtConflictTable* CreateImtConflictTable(size_t count,
650                                                  LinearAlloc* linear_alloc,
651                                                  PointerSize pointer_size);
652
653
654  // Create the IMT and conflict tables for a class.
655  void FillIMTAndConflictTables(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
656
657  // Clear class table strong roots (other than classes themselves). This is done by dex2oat to
658  // allow pruning dex caches.
659  void ClearClassTableStrongRoots() const
660      REQUIRES(!Locks::classlinker_classes_lock_)
661      REQUIRES_SHARED(Locks::mutator_lock_);
662
663  // Throw the class initialization failure recorded when first trying to initialize the given
664  // class.
665  void ThrowEarlierClassFailure(mirror::Class* c, bool wrap_in_no_class_def = false)
666      REQUIRES_SHARED(Locks::mutator_lock_)
667      REQUIRES(!dex_lock_);
668
669  // Get the actual holding class for a copied method. Pretty slow, don't call often.
670  mirror::Class* GetHoldingClassOfCopiedMethod(ArtMethod* method)
671      REQUIRES_SHARED(Locks::mutator_lock_);
672
673  struct DexCacheData {
674    // Weak root to the DexCache. Note: Do not decode this unnecessarily or else class unloading may
675    // not work properly.
676    jweak weak_root;
677    // The following two fields are caches to the DexCache's fields and here to avoid unnecessary
678    // jweak decode that triggers read barriers (and mark them alive unnecessarily and mess with
679    // class unloading.)
680    const DexFile* dex_file;
681    GcRoot<mirror::Class>* resolved_types;
682  };
683
684 private:
685  struct ClassLoaderData {
686    jweak weak_root;  // Weak root to enable class unloading.
687    ClassTable* class_table;
688    LinearAlloc* allocator;
689  };
690
691  // Ensures that the supertype of 'klass' ('supertype') is verified. Returns false and throws
692  // appropriate exceptions if verification failed hard. Returns true for successful verification or
693  // soft-failures.
694  bool AttemptSupertypeVerification(Thread* self,
695                                    Handle<mirror::Class> klass,
696                                    Handle<mirror::Class> supertype)
697      REQUIRES(!dex_lock_)
698      REQUIRES_SHARED(Locks::mutator_lock_);
699
700  static void DeleteClassLoader(Thread* self, const ClassLoaderData& data)
701      REQUIRES_SHARED(Locks::mutator_lock_);
702
703  void VisitClassLoaders(ClassLoaderVisitor* visitor) const
704      REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
705
706  void VisitClassesInternal(ClassVisitor* visitor)
707      REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
708
709  // Returns the number of zygote and image classes.
710  size_t NumZygoteClasses() const
711      REQUIRES(Locks::classlinker_classes_lock_)
712      REQUIRES_SHARED(Locks::mutator_lock_);
713
714  // Returns the number of non zygote nor image classes.
715  size_t NumNonZygoteClasses() const
716      REQUIRES(Locks::classlinker_classes_lock_)
717      REQUIRES_SHARED(Locks::mutator_lock_);
718
719  void FinishInit(Thread* self)
720      REQUIRES_SHARED(Locks::mutator_lock_)
721      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
722
723  // For early bootstrapping by Init
724  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
725      REQUIRES_SHARED(Locks::mutator_lock_)
726      REQUIRES(!Roles::uninterruptible_);
727
728  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
729  // values that are known to the ClassLinker such as
730  // kObjectArrayClass and kJavaLangString etc.
731  mirror::Class* AllocClass(Thread* self, uint32_t class_size)
732      REQUIRES_SHARED(Locks::mutator_lock_)
733      REQUIRES(!Roles::uninterruptible_);
734  mirror::DexCache* AllocDexCache(Thread* self,
735                                  const DexFile& dex_file,
736                                  LinearAlloc* linear_alloc)
737      REQUIRES_SHARED(Locks::mutator_lock_)
738      REQUIRES(!Roles::uninterruptible_);
739
740  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
741      REQUIRES_SHARED(Locks::mutator_lock_)
742      REQUIRES(!Roles::uninterruptible_);
743  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
744      REQUIRES_SHARED(Locks::mutator_lock_)
745      REQUIRES(!Roles::uninterruptible_);
746
747  mirror::Class* CreateArrayClass(Thread* self,
748                                  const char* descriptor,
749                                  size_t hash,
750                                  Handle<mirror::ClassLoader> class_loader)
751      REQUIRES_SHARED(Locks::mutator_lock_)
752      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
753
754  void AppendToBootClassPath(Thread* self, const DexFile& dex_file)
755      REQUIRES_SHARED(Locks::mutator_lock_)
756      REQUIRES(!dex_lock_);
757  void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
758      REQUIRES_SHARED(Locks::mutator_lock_)
759      REQUIRES(!dex_lock_);
760
761  // Precomputes size needed for Class, in the case of a non-temporary class this size must be
762  // sufficient to hold all static fields.
763  uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
764                                            const DexFile::ClassDef& dex_class_def);
765
766  // Setup the classloader, class def index, type idx so that we can insert this class in the class
767  // table.
768  void SetupClass(const DexFile& dex_file,
769                  const DexFile::ClassDef& dex_class_def,
770                  Handle<mirror::Class> klass,
771                  mirror::ClassLoader* class_loader)
772      REQUIRES_SHARED(Locks::mutator_lock_);
773
774  void LoadClass(Thread* self,
775                 const DexFile& dex_file,
776                 const DexFile::ClassDef& dex_class_def,
777                 Handle<mirror::Class> klass)
778      REQUIRES_SHARED(Locks::mutator_lock_);
779  void LoadClassMembers(Thread* self,
780                        const DexFile& dex_file,
781                        const uint8_t* class_data,
782                        Handle<mirror::Class> klass)
783      REQUIRES_SHARED(Locks::mutator_lock_);
784
785  void LoadField(const ClassDataItemIterator& it, Handle<mirror::Class> klass, ArtField* dst)
786      REQUIRES_SHARED(Locks::mutator_lock_);
787
788  void LoadMethod(const DexFile& dex_file,
789                  const ClassDataItemIterator& it,
790                  Handle<mirror::Class> klass, ArtMethod* dst)
791      REQUIRES_SHARED(Locks::mutator_lock_);
792
793  void FixupStaticTrampolines(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
794
795  void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
796      REQUIRES(dex_lock_)
797      REQUIRES_SHARED(Locks::mutator_lock_);
798  mirror::DexCache* FindDexCacheLocked(Thread* self, const DexFile& dex_file, bool allow_failure)
799      REQUIRES(dex_lock_)
800      REQUIRES_SHARED(Locks::mutator_lock_);
801
802  bool InitializeClass(Thread* self,
803                       Handle<mirror::Class> klass,
804                       bool can_run_clinit,
805                       bool can_init_parents)
806      REQUIRES_SHARED(Locks::mutator_lock_)
807      REQUIRES(!dex_lock_);
808  bool InitializeDefaultInterfaceRecursive(Thread* self,
809                                           Handle<mirror::Class> klass,
810                                           bool can_run_clinit,
811                                           bool can_init_parents)
812      REQUIRES(!dex_lock_)
813      REQUIRES_SHARED(Locks::mutator_lock_);
814  bool WaitForInitializeClass(Handle<mirror::Class> klass,
815                              Thread* self,
816                              ObjectLock<mirror::Class>& lock);
817  bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
818      REQUIRES_SHARED(Locks::mutator_lock_);
819
820  bool IsSameDescriptorInDifferentClassContexts(Thread* self,
821                                                const char* descriptor,
822                                                Handle<mirror::ClassLoader> class_loader1,
823                                                Handle<mirror::ClassLoader> class_loader2)
824      REQUIRES_SHARED(Locks::mutator_lock_);
825
826  bool IsSameMethodSignatureInDifferentClassContexts(Thread* self,
827                                                     ArtMethod* method,
828                                                     mirror::Class* klass1,
829                                                     mirror::Class* klass2)
830      REQUIRES_SHARED(Locks::mutator_lock_);
831
832  bool LinkClass(Thread* self,
833                 const char* descriptor,
834                 Handle<mirror::Class> klass,
835                 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
836                 MutableHandle<mirror::Class>* h_new_class_out)
837      REQUIRES_SHARED(Locks::mutator_lock_)
838      REQUIRES(!Locks::classlinker_classes_lock_);
839
840  bool LinkSuperClass(Handle<mirror::Class> klass)
841      REQUIRES_SHARED(Locks::mutator_lock_);
842
843  bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
844      REQUIRES_SHARED(Locks::mutator_lock_)
845      REQUIRES(!dex_lock_);
846
847  bool LinkMethods(Thread* self,
848                   Handle<mirror::Class> klass,
849                   Handle<mirror::ObjectArray<mirror::Class>> interfaces,
850                   bool* out_new_conflict,
851                   ArtMethod** out_imt)
852      REQUIRES_SHARED(Locks::mutator_lock_);
853
854  // A wrapper class representing the result of a method translation used for linking methods and
855  // updating superclass default methods. For each method in a classes vtable there are 4 states it
856  // could be in:
857  // 1) No translation is necessary. In this case there is no MethodTranslation object for it. This
858  //    is the standard case and is true when the method is not overridable by a default method,
859  //    the class defines a concrete implementation of the method, the default method implementation
860  //    remains the same, or an abstract method stayed abstract.
861  // 2) The method must be translated to a different default method. We note this with
862  //    CreateTranslatedMethod.
863  // 3) The method must be replaced with a conflict method. This happens when a superclass
864  //    implements an interface with a default method and this class implements an unrelated
865  //    interface that also defines that default method. We note this with CreateConflictingMethod.
866  // 4) The method must be replaced with an abstract miranda method. This happens when a superclass
867  //    implements an interface with a default method and this class implements a subinterface of
868  //    the superclass's interface which declares the default method abstract. We note this with
869  //    CreateAbstractMethod.
870  //
871  // When a method translation is unnecessary (case #1), we don't put it into the
872  // default_translation maps. So an instance of MethodTranslation must be in one of #2-#4.
873  class MethodTranslation {
874   public:
875    // This slot must become a default conflict method.
876    static MethodTranslation CreateConflictingMethod() {
877      return MethodTranslation(Type::kConflict, /*translation*/nullptr);
878    }
879
880    // This slot must become an abstract method.
881    static MethodTranslation CreateAbstractMethod() {
882      return MethodTranslation(Type::kAbstract, /*translation*/nullptr);
883    }
884
885    // Use the given method as the current value for this vtable slot during translation.
886    static MethodTranslation CreateTranslatedMethod(ArtMethod* new_method) {
887      return MethodTranslation(Type::kTranslation, new_method);
888    }
889
890    // Returns true if this is a method that must become a conflict method.
891    bool IsInConflict() const {
892      return type_ == Type::kConflict;
893    }
894
895    // Returns true if this is a method that must become an abstract method.
896    bool IsAbstract() const {
897      return type_ == Type::kAbstract;
898    }
899
900    // Returns true if this is a method that must become a different method.
901    bool IsTranslation() const {
902      return type_ == Type::kTranslation;
903    }
904
905    // Get the translated version of this method.
906    ArtMethod* GetTranslation() const {
907      DCHECK(IsTranslation());
908      DCHECK(translation_ != nullptr);
909      return translation_;
910    }
911
912   private:
913    enum class Type {
914      kTranslation,
915      kConflict,
916      kAbstract,
917    };
918
919    MethodTranslation(Type type, ArtMethod* translation)
920        : translation_(translation), type_(type) {}
921
922    ArtMethod* const translation_;
923    const Type type_;
924  };
925
926  // Links the virtual methods for the given class and records any default methods that will need to
927  // be updated later.
928  //
929  // Arguments:
930  // * self - The current thread.
931  // * klass - class, whose vtable will be filled in.
932  // * default_translations - Vtable index to new method map.
933  //                          Any vtable entries that need to be updated with new default methods
934  //                          are stored into the default_translations map. The default_translations
935  //                          map is keyed on the vtable index that needs to be updated. We use this
936  //                          map because if we override a default method with another default
937  //                          method we need to update the vtable to point to the new method.
938  //                          Unfortunately since we copy the ArtMethod* we cannot just do a simple
939  //                          scan, we therefore store the vtable index's that might need to be
940  //                          updated with the method they will turn into.
941  // TODO This whole default_translations thing is very dirty. There should be a better way.
942  bool LinkVirtualMethods(
943        Thread* self,
944        Handle<mirror::Class> klass,
945        /*out*/std::unordered_map<size_t, MethodTranslation>* default_translations)
946      REQUIRES_SHARED(Locks::mutator_lock_);
947
948  // Sets up the interface lookup table (IFTable) in the correct order to allow searching for
949  // default methods.
950  bool SetupInterfaceLookupTable(Thread* self,
951                                 Handle<mirror::Class> klass,
952                                 Handle<mirror::ObjectArray<mirror::Class>> interfaces)
953      REQUIRES_SHARED(Locks::mutator_lock_);
954
955
956  enum class DefaultMethodSearchResult {
957    kDefaultFound,
958    kAbstractFound,
959    kDefaultConflict
960  };
961
962  // Find the default method implementation for 'interface_method' in 'klass', if one exists.
963  //
964  // Arguments:
965  // * self - The current thread.
966  // * target_method - The method we are trying to find a default implementation for.
967  // * klass - The class we are searching for a definition of target_method.
968  // * out_default_method - The pointer we will store the found default method to on success.
969  //
970  // Return value:
971  // * kDefaultFound - There were no conflicting method implementations found in the class while
972  //                   searching for target_method. The default method implementation is stored into
973  //                   out_default_method.
974  // * kAbstractFound - There were no conflicting method implementations found in the class while
975  //                   searching for target_method but no default implementation was found either.
976  //                   out_default_method is set to null and the method should be considered not
977  //                   implemented.
978  // * kDefaultConflict - Conflicting method implementations were found when searching for
979  //                      target_method. The value of *out_default_method is null.
980  DefaultMethodSearchResult FindDefaultMethodImplementation(
981      Thread* self,
982      ArtMethod* target_method,
983      Handle<mirror::Class> klass,
984      /*out*/ArtMethod** out_default_method) const
985      REQUIRES_SHARED(Locks::mutator_lock_);
986
987  // Sets the imt entries and fixes up the vtable for the given class by linking all the interface
988  // methods. See LinkVirtualMethods for an explanation of what default_translations is.
989  bool LinkInterfaceMethods(
990      Thread* self,
991      Handle<mirror::Class> klass,
992      const std::unordered_map<size_t, MethodTranslation>& default_translations,
993      bool* out_new_conflict,
994      ArtMethod** out_imt)
995      REQUIRES_SHARED(Locks::mutator_lock_);
996
997  bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size)
998      REQUIRES_SHARED(Locks::mutator_lock_);
999  bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass)
1000      REQUIRES_SHARED(Locks::mutator_lock_);
1001  bool LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, size_t* class_size)
1002      REQUIRES_SHARED(Locks::mutator_lock_);
1003  void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
1004      REQUIRES_SHARED(Locks::mutator_lock_);
1005
1006  void CheckProxyConstructor(ArtMethod* constructor) const
1007      REQUIRES_SHARED(Locks::mutator_lock_);
1008  void CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const
1009      REQUIRES_SHARED(Locks::mutator_lock_);
1010
1011  // For use by ImageWriter to find DexCaches for its roots
1012  ReaderWriterMutex* DexLock()
1013      REQUIRES_SHARED(Locks::mutator_lock_)
1014      LOCK_RETURNED(dex_lock_) {
1015    return &dex_lock_;
1016  }
1017  size_t GetDexCacheCount() REQUIRES_SHARED(Locks::mutator_lock_, dex_lock_) {
1018    return dex_caches_.size();
1019  }
1020  const std::list<DexCacheData>& GetDexCachesData()
1021      REQUIRES_SHARED(Locks::mutator_lock_, dex_lock_) {
1022    return dex_caches_;
1023  }
1024
1025  void CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out)
1026      REQUIRES_SHARED(Locks::mutator_lock_);
1027  void CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, ArtMethod* out)
1028      REQUIRES_SHARED(Locks::mutator_lock_);
1029
1030  // Ensures that methods have the kAccSkipAccessChecks bit set. We use the
1031  // kAccVerificationAttempted bit on the class access flags to determine whether this has been done
1032  // before.
1033  void EnsureSkipAccessChecksMethods(Handle<mirror::Class> c)
1034      REQUIRES_SHARED(Locks::mutator_lock_);
1035
1036  mirror::Class* LookupClassFromBootImage(const char* descriptor)
1037      REQUIRES_SHARED(Locks::mutator_lock_);
1038
1039  // Register a class loader and create its class table and allocator. Should not be called if
1040  // these are already created.
1041  void RegisterClassLoader(mirror::ClassLoader* class_loader)
1042      REQUIRES_SHARED(Locks::mutator_lock_)
1043      REQUIRES(Locks::classlinker_classes_lock_);
1044
1045  // Returns null if not found.
1046  ClassTable* ClassTableForClassLoader(mirror::ClassLoader* class_loader)
1047      REQUIRES_SHARED(Locks::mutator_lock_);
1048
1049  // Insert a new class table if not found.
1050  ClassTable* InsertClassTableForClassLoader(mirror::ClassLoader* class_loader)
1051      REQUIRES_SHARED(Locks::mutator_lock_)
1052      REQUIRES(Locks::classlinker_classes_lock_);
1053
1054  // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
1055  // before returning it to the caller. Its the responsibility of the thread that placed the class
1056  // in the table to make it resolved. The thread doing resolution must notify on the class' lock
1057  // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
1058  // retire a class, the version of the class in the table is returned and this may differ from
1059  // the class passed in.
1060  mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
1061      WARN_UNUSED
1062      REQUIRES_SHARED(Locks::mutator_lock_)
1063      REQUIRES(!dex_lock_);
1064
1065  void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
1066      REQUIRES_SHARED(Locks::mutator_lock_);
1067
1068  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
1069      REQUIRES_SHARED(Locks::mutator_lock_);
1070
1071  // Return the quick generic JNI stub for testing.
1072  const void* GetRuntimeQuickGenericJniStub() const;
1073
1074  bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, bool can_init_parents)
1075      REQUIRES_SHARED(Locks::mutator_lock_);
1076
1077  void UpdateClassMethods(mirror::Class* klass,
1078                          LengthPrefixedArray<ArtMethod>* new_methods)
1079      REQUIRES_SHARED(Locks::mutator_lock_)
1080      REQUIRES(!Locks::classlinker_classes_lock_);
1081
1082  // new_class_set is the set of classes that were read from the class table section in the image.
1083  // If there was no class table section, it is null.
1084  bool UpdateAppImageClassLoadersAndDexCaches(
1085      gc::space::ImageSpace* space,
1086      Handle<mirror::ClassLoader> class_loader,
1087      Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches,
1088      ClassTable::ClassSet* new_class_set,
1089      bool* out_forward_dex_cache_array,
1090      std::string* out_error_msg)
1091      REQUIRES(!dex_lock_)
1092      REQUIRES_SHARED(Locks::mutator_lock_);
1093
1094  // Check that c1 == FindSystemClass(self, descriptor). Abort with class dumps otherwise.
1095  void CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor)
1096      REQUIRES(!dex_lock_)
1097      REQUIRES_SHARED(Locks::mutator_lock_);
1098
1099  // Sets imt_ref appropriately for LinkInterfaceMethods.
1100  // If there is no method in the imt location of imt_ref it will store the given method there.
1101  // Otherwise it will set the conflict method which will figure out which method to use during
1102  // runtime.
1103  void SetIMTRef(ArtMethod* unimplemented_method,
1104                 ArtMethod* imt_conflict_method,
1105                 ArtMethod* current_method,
1106                 /*out*/bool* new_conflict,
1107                 /*out*/ArtMethod** imt_ref) REQUIRES_SHARED(Locks::mutator_lock_);
1108
1109  void FillIMTFromIfTable(mirror::IfTable* if_table,
1110                          ArtMethod* unimplemented_method,
1111                          ArtMethod* imt_conflict_method,
1112                          mirror::Class* klass,
1113                          bool create_conflict_tables,
1114                          bool ignore_copied_methods,
1115                          /*out*/bool* new_conflict,
1116                          /*out*/ArtMethod** imt) REQUIRES_SHARED(Locks::mutator_lock_);
1117
1118  void FillImtFromSuperClass(Handle<mirror::Class> klass,
1119                             ArtMethod* unimplemented_method,
1120                             ArtMethod* imt_conflict_method,
1121                             bool* new_conflict,
1122                             ArtMethod** imt) REQUIRES_SHARED(Locks::mutator_lock_);
1123
1124  std::vector<const DexFile*> boot_class_path_;
1125  std::vector<std::unique_ptr<const DexFile>> boot_dex_files_;
1126
1127  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
1128  // JNI weak globals and side data to allow dex caches to get unloaded. We lazily delete weak
1129  // globals when we register new dex files.
1130  std::list<DexCacheData> dex_caches_ GUARDED_BY(dex_lock_);
1131
1132  // This contains the class loaders which have class tables. It is populated by
1133  // InsertClassTableForClassLoader.
1134  std::list<ClassLoaderData> class_loaders_
1135      GUARDED_BY(Locks::classlinker_classes_lock_);
1136
1137  // Boot class path table. Since the class loader for this is null.
1138  ClassTable boot_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
1139
1140  // New class roots, only used by CMS since the GC needs to mark these in the pause.
1141  std::vector<GcRoot<mirror::Class>> new_class_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
1142
1143  // Do we need to search dex caches to find boot image classes?
1144  bool dex_cache_boot_image_class_lookup_required_;
1145  // Number of times we've searched dex caches for a class. After a certain number of misses we move
1146  // the classes into the class_table_ to avoid dex cache based searches.
1147  Atomic<uint32_t> failed_dex_cache_class_lookups_;
1148
1149  // Well known mirror::Class roots.
1150  GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
1151
1152  // The interface table used by all arrays.
1153  GcRoot<mirror::IfTable> array_iftable_;
1154
1155  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
1156  // descriptors for the sake of performing FindClass.
1157  static constexpr size_t kFindArrayCacheSize = 16;
1158  GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
1159  size_t find_array_class_cache_next_victim_;
1160
1161  bool init_done_;
1162  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
1163
1164  InternTable* intern_table_;
1165
1166  // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
1167  // patch point within the image. TODO: make these proper relocations.
1168  const void* quick_resolution_trampoline_;
1169  const void* quick_imt_conflict_trampoline_;
1170  const void* quick_generic_jni_trampoline_;
1171  const void* quick_to_interpreter_bridge_trampoline_;
1172
1173  // Image pointer size.
1174  PointerSize image_pointer_size_;
1175
1176  class FindVirtualMethodHolderVisitor;
1177  friend struct CompilationHelper;  // For Compile in ImageTest.
1178  friend class ImageDumper;  // for DexLock
1179  friend class ImageWriter;  // for GetClassRoots
1180  friend class JniCompilerTest;  // for GetRuntimeQuickGenericJniStub
1181  friend class JniInternalTest;  // for GetRuntimeQuickGenericJniStub
1182  ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName);  // for DexLock, and RegisterDexFileLocked
1183  ART_FRIEND_TEST(mirror::DexCacheMethodHandlesTest, Open);  // for AllocDexCache
1184  ART_FRIEND_TEST(mirror::DexCacheTest, Open);  // for AllocDexCache
1185  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
1186};
1187
1188}  // namespace art
1189
1190#endif  // ART_RUNTIME_CLASS_LINKER_H_
1191