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