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