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