class_linker.h revision 0042c6d49b8488c78f0b937063e316e8d6244439
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 <string>
21#include <utility>
22#include <vector>
23
24#include "base/allocator.h"
25#include "base/hash_set.h"
26#include "base/macros.h"
27#include "base/mutex.h"
28#include "class_table.h"
29#include "dex_file.h"
30#include "gc_root.h"
31#include "jni.h"
32#include "oat_file.h"
33#include "object_callbacks.h"
34
35namespace art {
36
37namespace gc {
38namespace space {
39  class ImageSpace;
40}  // namespace space
41}  // namespace gc
42namespace mirror {
43  class ClassLoader;
44  class DexCache;
45  class DexCachePointerArray;
46  class DexCacheTest_Open_Test;
47  class IfTable;
48  template<class T> class ObjectArray;
49  class StackTraceElement;
50}  // namespace mirror
51
52template<class T> class Handle;
53template<class T> class MutableHandle;
54class InternTable;
55template<class T> class ObjectLock;
56class Runtime;
57class ScopedObjectAccessAlreadyRunnable;
58template<size_t kNumReferences> class PACKED(4) StackHandleScope;
59
60enum VisitRootFlags : uint8_t;
61
62class ClassLinker {
63 public:
64  // Well known mirror::Class roots accessed via GetClassRoot.
65  enum ClassRoot {
66    kJavaLangClass,
67    kJavaLangObject,
68    kClassArrayClass,
69    kObjectArrayClass,
70    kJavaLangString,
71    kJavaLangDexCache,
72    kJavaLangRefReference,
73    kJavaLangReflectConstructor,
74    kJavaLangReflectField,
75    kJavaLangReflectMethod,
76    kJavaLangReflectProxy,
77    kJavaLangStringArrayClass,
78    kJavaLangReflectConstructorArrayClass,
79    kJavaLangReflectFieldArrayClass,
80    kJavaLangReflectMethodArrayClass,
81    kJavaLangClassLoader,
82    kJavaLangThrowable,
83    kJavaLangClassNotFoundException,
84    kJavaLangStackTraceElement,
85    kPrimitiveBoolean,
86    kPrimitiveByte,
87    kPrimitiveChar,
88    kPrimitiveDouble,
89    kPrimitiveFloat,
90    kPrimitiveInt,
91    kPrimitiveLong,
92    kPrimitiveShort,
93    kPrimitiveVoid,
94    kBooleanArrayClass,
95    kByteArrayClass,
96    kCharArrayClass,
97    kDoubleArrayClass,
98    kFloatArrayClass,
99    kIntArrayClass,
100    kLongArrayClass,
101    kShortArrayClass,
102    kJavaLangStackTraceElementArrayClass,
103    kClassRootsMax,
104  };
105
106  explicit ClassLinker(InternTable* intern_table);
107  ~ClassLinker();
108
109  // Initialize class linker by bootstraping from dex files.
110  void InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path)
111      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
112
113  // Initialize class linker from one or more images.
114  void InitFromImage() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
115
116  // Finds a class by its descriptor, loading it if necessary.
117  // If class_loader is null, searches boot_class_path_.
118  mirror::Class* FindClass(Thread* self, const char* descriptor,
119                           Handle<mirror::ClassLoader> class_loader)
120      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
121
122  // Finds a class in the path class loader, loading it if necessary without using JNI. Hash
123  // function is supposed to be ComputeModifiedUtf8Hash(descriptor). Returns true if the
124  // class-loader chain could be handled, false otherwise, i.e., a non-supported class-loader
125  // was encountered while walking the parent chain (currently only BootClassLoader and
126  // PathClassLoader are supported).
127  bool FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
128                                  Thread* self, const char* descriptor, size_t hash,
129                                  Handle<mirror::ClassLoader> class_loader,
130                                  mirror::Class** result)
131      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
132
133  // Finds a class by its descriptor using the "system" class loader, ie by searching the
134  // boot_class_path_.
135  mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
136      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
137
138  // Finds the array class given for the element class.
139  mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
140      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
141
142  // Returns true if the class linker is initialized.
143  bool IsInitialized() const {
144    return init_done_;
145  }
146
147  // Define a new a class based on a ClassDef from a DexFile
148  mirror::Class* DefineClass(Thread* self, const char* descriptor, size_t hash,
149                             Handle<mirror::ClassLoader> class_loader,
150                             const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
151      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
152
153  // Finds a class by its descriptor, returning null if it isn't wasn't loaded
154  // by the given 'class_loader'.
155  mirror::Class* LookupClass(Thread* self, const char* descriptor, size_t hash,
156                             mirror::ClassLoader* class_loader)
157      REQUIRES(!Locks::classlinker_classes_lock_)
158      SHARED_REQUIRES(Locks::mutator_lock_);
159
160  // Finds all the classes with the given descriptor, regardless of ClassLoader.
161  void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
162      REQUIRES(!Locks::classlinker_classes_lock_)
163      SHARED_REQUIRES(Locks::mutator_lock_);
164
165  mirror::Class* FindPrimitiveClass(char type) SHARED_REQUIRES(Locks::mutator_lock_);
166
167  // General class unloading is not supported, this is used to prune
168  // unwanted classes during image writing.
169  bool RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader)
170      REQUIRES(!Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
171
172  void DumpAllClasses(int flags)
173      REQUIRES(!Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
174
175  void DumpForSigQuit(std::ostream& os)
176      REQUIRES(!Locks::classlinker_classes_lock_);
177
178  size_t NumLoadedClasses()
179      REQUIRES(!Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
180
181  // Resolve a String with the given index from the DexFile, storing the
182  // result in the DexCache. The referrer is used to identify the
183  // target DexCache and ClassLoader to use for resolution.
184  mirror::String* ResolveString(uint32_t string_idx, ArtMethod* referrer)
185      SHARED_REQUIRES(Locks::mutator_lock_);
186
187  // Resolve a String with the given index from the DexFile, storing the
188  // result in the DexCache.
189  mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
190                                Handle<mirror::DexCache> dex_cache)
191      SHARED_REQUIRES(Locks::mutator_lock_);
192
193  // Resolve a Type with the given index from the DexFile, storing the
194  // result in the DexCache. The referrer is used to identity the
195  // target DexCache and ClassLoader to use for resolution.
196  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
197      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
198
199  // Resolve a Type with the given index from the DexFile, storing the
200  // result in the DexCache. The referrer is used to identify the
201  // target DexCache and ClassLoader to use for resolution.
202  mirror::Class* ResolveType(uint16_t type_idx, ArtMethod* referrer)
203      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
204
205  mirror::Class* ResolveType(uint16_t type_idx, ArtField* referrer)
206      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
207
208  // Resolve a type with the given ID from the DexFile, storing the
209  // result in DexCache. The ClassLoader is used to search for the
210  // type, since it may be referenced from but not contained within
211  // the given DexFile.
212  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
213                             Handle<mirror::DexCache> dex_cache,
214                             Handle<mirror::ClassLoader> class_loader)
215      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
216
217  // Resolve a method with a given ID from the DexFile, storing the
218  // result in DexCache. The ClassLinker and ClassLoader are used as
219  // in ResolveType. What is unique is the method type argument which
220  // is used to determine if this method is a direct, static, or
221  // virtual method.
222  ArtMethod* ResolveMethod(const DexFile& dex_file, uint32_t method_idx,
223                           Handle<mirror::DexCache> dex_cache,
224                           Handle<mirror::ClassLoader> class_loader, ArtMethod* referrer,
225                           InvokeType type)
226      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
227
228  ArtMethod* GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer)
229      SHARED_REQUIRES(Locks::mutator_lock_);
230  ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, ArtMethod* referrer, InvokeType type)
231      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
232  ArtMethod* ResolveMethodWithoutInvokeType(const DexFile& dex_file,
233                                            uint32_t method_idx,
234                                            Handle<mirror::DexCache> dex_cache,
235                                            Handle<mirror::ClassLoader> class_loader)
236      SHARED_REQUIRES(Locks::mutator_lock_)
237      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
238
239  ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
240      SHARED_REQUIRES(Locks::mutator_lock_);
241  ArtField* GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache)
242      SHARED_REQUIRES(Locks::mutator_lock_);
243  ArtField* ResolveField(uint32_t field_idx, ArtMethod* referrer, bool is_static)
244      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
245
246  // Resolve a field with a given ID from the DexFile, storing the
247  // result in DexCache. The ClassLinker and ClassLoader are used as
248  // in ResolveType. What is unique is the is_static argument which is
249  // used to determine if we are resolving a static or non-static
250  // field.
251  ArtField* ResolveField(const DexFile& dex_file, uint32_t field_idx,
252                         Handle<mirror::DexCache> dex_cache,
253                         Handle<mirror::ClassLoader> class_loader, bool is_static)
254      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
255
256  // Resolve a field with a given ID from the DexFile, storing the
257  // result in DexCache. The ClassLinker and ClassLoader are used as
258  // in ResolveType. No is_static argument is provided so that Java
259  // field resolution semantics are followed.
260  ArtField* ResolveFieldJLS(const DexFile& dex_file, uint32_t field_idx,
261                            Handle<mirror::DexCache> dex_cache,
262                            Handle<mirror::ClassLoader> class_loader)
263      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
264
265  // Get shorty from method index without resolution. Used to do handlerization.
266  const char* MethodShorty(uint32_t method_idx, ArtMethod* referrer, uint32_t* length)
267      SHARED_REQUIRES(Locks::mutator_lock_);
268
269  // Returns true on success, false if there's an exception pending.
270  // can_run_clinit=false allows the compiler to attempt to init a class,
271  // given the restriction that no <clinit> execution is possible.
272  bool EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields,
273                         bool can_init_parents)
274      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
275
276  // Initializes classes that have instances in the image but that have
277  // <clinit> methods so they could not be initialized by the compiler.
278  void RunRootClinits() SHARED_REQUIRES(Locks::mutator_lock_)
279      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
280
281  void RegisterDexFile(const DexFile& dex_file)
282      REQUIRES(!dex_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
283  void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
284      REQUIRES(!dex_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
285
286  const OatFile* RegisterOatFile(const OatFile* oat_file)
287      REQUIRES(!dex_lock_);
288
289  const std::vector<const DexFile*>& GetBootClassPath() {
290    return boot_class_path_;
291  }
292
293  // Returns the first non-image oat file in the class path.
294  const OatFile* GetPrimaryOatFile()
295      REQUIRES(!dex_lock_);
296
297  void VisitClasses(ClassVisitor* visitor)
298      REQUIRES(!Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
299
300  // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage
301  // so that it can visit individual classes without holding the doesn't hold the
302  // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code
303  // can race with insertion and deletion of classes while the visitor is being called.
304  void VisitClassesWithoutClassesLock(ClassVisitor* visitor)
305      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
306
307  void VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags)
308      REQUIRES(!Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
309  void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
310      REQUIRES(!dex_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
311
312  mirror::DexCache* FindDexCache(const DexFile& dex_file)
313      REQUIRES(!dex_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
314  bool IsDexFileRegistered(const DexFile& dex_file)
315      REQUIRES(!dex_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
316  void FixupDexCaches(ArtMethod* resolution_method)
317      REQUIRES(!dex_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
318
319  // Finds or creates the oat file holding dex_location. Then loads and returns
320  // all corresponding dex files (there may be more than one dex file loaded
321  // in the case of multidex).
322  // This may return the original, unquickened dex files if the oat file could
323  // not be generated.
324  //
325  // Returns an empty vector if the dex files could not be loaded. In this
326  // case, there will be at least one error message returned describing why no
327  // dex files could not be loaded. The 'error_msgs' argument must not be
328  // null, regardless of whether there is an error or not.
329  //
330  // This method should not be called with the mutator_lock_ held, because it
331  // could end up starving GC if we need to generate or relocate any oat
332  // files.
333  std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
334      const char* dex_location, const char* oat_location,
335      std::vector<std::string>* error_msgs)
336      REQUIRES(!dex_lock_, !Locks::mutator_lock_);
337
338  // Allocate an instance of a java.lang.Object.
339  mirror::Object* AllocObject(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_)
340      REQUIRES(!Roles::uninterruptible_);
341
342  // TODO: replace this with multiple methods that allocate the correct managed type.
343  template <class T>
344  mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
345      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
346
347  mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
348      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
349
350  mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
351      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
352
353  LengthPrefixedArray<ArtField>* AllocArtFieldArray(Thread* self, size_t length);
354
355  LengthPrefixedArray<ArtMethod>* AllocArtMethodArray(Thread* self, size_t length);
356
357  mirror::PointerArray* AllocPointerArray(Thread* self, size_t length)
358      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
359
360  mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
361      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
362
363  mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(
364      Thread* self, size_t length) SHARED_REQUIRES(Locks::mutator_lock_)
365          REQUIRES(!Roles::uninterruptible_);
366
367  void VerifyClass(Thread* self, Handle<mirror::Class> klass)
368      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
369  bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
370                               mirror::Class::Status& oat_file_class_status)
371      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
372  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
373                                         Handle<mirror::Class> klass)
374      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
375  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, ArtMethod* klass)
376      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
377
378  mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
379                                  jobjectArray interfaces, jobject loader, jobjectArray methods,
380                                  jobjectArray throws)
381      SHARED_REQUIRES(Locks::mutator_lock_);
382  std::string GetDescriptorForProxy(mirror::Class* proxy_class)
383      SHARED_REQUIRES(Locks::mutator_lock_);
384  ArtMethod* FindMethodForProxy(mirror::Class* proxy_class, ArtMethod* proxy_method)
385      REQUIRES(!dex_lock_)
386      SHARED_REQUIRES(Locks::mutator_lock_);
387
388  // Get the oat code for a method when its class isn't yet initialized
389  const void* GetQuickOatCodeFor(ArtMethod* method)
390      SHARED_REQUIRES(Locks::mutator_lock_);
391
392  // Get the oat code for a method from a method index.
393  const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx,
394                                 uint32_t method_idx)
395      SHARED_REQUIRES(Locks::mutator_lock_);
396
397  // Get compiled code for a method, return null if no code
398  // exists. This is unlike Get..OatCodeFor which will return a bridge
399  // or interpreter entrypoint.
400  const void* GetOatMethodQuickCodeFor(ArtMethod* method)
401      SHARED_REQUIRES(Locks::mutator_lock_);
402
403  const OatFile::OatMethod FindOatMethodFor(ArtMethod* method, bool* found)
404      SHARED_REQUIRES(Locks::mutator_lock_);
405
406  pid_t GetClassesLockOwner();  // For SignalCatcher.
407  pid_t GetDexLockOwner();  // For SignalCatcher.
408
409  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_REQUIRES(Locks::mutator_lock_);
410
411  static const char* GetClassRootDescriptor(ClassRoot class_root);
412
413  // Is the given entry point quick code to run the resolution stub?
414  bool IsQuickResolutionStub(const void* entry_point) const;
415
416  // Is the given entry point quick code to bridge into the interpreter?
417  bool IsQuickToInterpreterBridge(const void* entry_point) const;
418
419  // Is the given entry point quick code to run the generic JNI stub?
420  bool IsQuickGenericJniStub(const void* entry_point) const;
421
422  InternTable* GetInternTable() const {
423    return intern_table_;
424  }
425
426  // Set the entrypoints up for method to the given code.
427  void SetEntryPointsToCompiledCode(ArtMethod* method, const void* method_code) const
428      SHARED_REQUIRES(Locks::mutator_lock_);
429
430  // Set the entrypoints up for method to the enter the interpreter.
431  void SetEntryPointsToInterpreter(ArtMethod* method) const
432      SHARED_REQUIRES(Locks::mutator_lock_);
433
434  // Attempts to insert a class into a class table.  Returns null if
435  // the class was inserted, otherwise returns an existing class with
436  // the same descriptor and ClassLoader.
437  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
438      REQUIRES(!Locks::classlinker_classes_lock_)
439      SHARED_REQUIRES(Locks::mutator_lock_);
440
441  mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_REQUIRES(Locks::mutator_lock_) {
442    mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
443    DCHECK(class_roots != nullptr);
444    return class_roots;
445  }
446
447  // Move all of the image classes into the class table for faster lookups.
448  void MoveImageClassesToClassTable()
449      REQUIRES(!Locks::classlinker_classes_lock_)
450      SHARED_REQUIRES(Locks::mutator_lock_);
451  // Move the class table to the pre-zygote table to reduce memory usage. This works by ensuring
452  // that no more classes are ever added to the pre zygote table which makes it that the pages
453  // always remain shared dirty instead of private dirty.
454  void MoveClassTableToPreZygote()
455      REQUIRES(!Locks::classlinker_classes_lock_)
456      SHARED_REQUIRES(Locks::mutator_lock_);
457
458  // Returns true if the method can be called with its direct code pointer, false otherwise.
459  bool MayBeCalledWithDirectCodePointer(ArtMethod* m)
460      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
461
462  // Creates a GlobalRef PathClassLoader that can be used to load classes from the given dex files.
463  // Note: the objects are not completely set up. Do not use this outside of tests and the compiler.
464  jobject CreatePathClassLoader(Thread* self, std::vector<const DexFile*>& dex_files)
465      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
466
467  size_t GetImagePointerSize() const {
468    DCHECK(ValidPointerSize(image_pointer_size_)) << image_pointer_size_;
469    return image_pointer_size_;
470  }
471
472  // Used by image writer for checking.
473  bool ClassInClassTable(mirror::Class* klass)
474      REQUIRES(!Locks::classlinker_classes_lock_)
475      SHARED_REQUIRES(Locks::mutator_lock_);
476
477  ArtMethod* CreateRuntimeMethod();
478
479  // Clear the ArrayClass cache. This is necessary when cleaning up for the image, as the cache
480  // entries are roots, but potentially not image classes.
481  void DropFindArrayClassCache() SHARED_REQUIRES(Locks::mutator_lock_);
482
483 private:
484  void VisitClassesInternal(ClassVisitor* visitor)
485      REQUIRES(Locks::classlinker_classes_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
486
487  // Returns the number of zygote and image classes.
488  size_t NumZygoteClasses() const
489      REQUIRES(Locks::classlinker_classes_lock_)
490      SHARED_REQUIRES(Locks::mutator_lock_);
491
492  // Returns the number of non zygote nor image classes.
493  size_t NumNonZygoteClasses() const
494      REQUIRES(Locks::classlinker_classes_lock_)
495      SHARED_REQUIRES(Locks::mutator_lock_);
496
497  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
498      REQUIRES(!dex_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
499
500  void FinishInit(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_)
501      REQUIRES(!dex_lock_, !Roles::uninterruptible_);
502
503  // For early bootstrapping by Init
504  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
505      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
506
507  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
508  // values that are known to the ClassLinker such as
509  // kObjectArrayClass and kJavaLangString etc.
510  mirror::Class* AllocClass(Thread* self, uint32_t class_size)
511      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
512  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
513      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
514
515  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
516      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
517  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
518      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
519
520  mirror::Class* CreateArrayClass(Thread* self, const char* descriptor, size_t hash,
521                                  Handle<mirror::ClassLoader> class_loader)
522      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_, !Roles::uninterruptible_);
523
524  void AppendToBootClassPath(Thread* self, const DexFile& dex_file)
525      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
526  void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
527      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
528
529  // Precomputes size needed for Class, in the case of a non-temporary class this size must be
530  // sufficient to hold all static fields.
531  uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
532                                            const DexFile::ClassDef& dex_class_def);
533
534  // Setup the classloader, class def index, type idx so that we can insert this class in the class
535  // table.
536  void SetupClass(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
537                  Handle<mirror::Class> klass, mirror::ClassLoader* class_loader)
538      SHARED_REQUIRES(Locks::mutator_lock_);
539
540  void LoadClass(Thread* self, const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
541                 Handle<mirror::Class> klass)
542      SHARED_REQUIRES(Locks::mutator_lock_);
543  void LoadClassMembers(Thread* self, const DexFile& dex_file, const uint8_t* class_data,
544                        Handle<mirror::Class> klass, const OatFile::OatClass* oat_class)
545      SHARED_REQUIRES(Locks::mutator_lock_);
546
547  void LoadField(const ClassDataItemIterator& it, Handle<mirror::Class> klass,
548                 ArtField* dst)
549      SHARED_REQUIRES(Locks::mutator_lock_);
550
551  void LoadMethod(Thread* self, const DexFile& dex_file, const ClassDataItemIterator& it,
552                  Handle<mirror::Class> klass, ArtMethod* dst)
553      SHARED_REQUIRES(Locks::mutator_lock_);
554
555  void FixupStaticTrampolines(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
556
557  // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
558  // error and sets found to false.
559  OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found)
560      SHARED_REQUIRES(Locks::mutator_lock_);
561
562  void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
563      REQUIRES(dex_lock_) SHARED_REQUIRES(Locks::mutator_lock_);
564  bool IsDexFileRegisteredLocked(const DexFile& dex_file)
565      SHARED_REQUIRES(dex_lock_, Locks::mutator_lock_);
566
567  bool InitializeClass(Thread* self, Handle<mirror::Class> klass, bool can_run_clinit,
568                       bool can_init_parents)
569      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
570  bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
571                              ObjectLock<mirror::Class>& lock);
572  bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
573      SHARED_REQUIRES(Locks::mutator_lock_);
574
575  bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
576                                                Handle<mirror::ClassLoader> class_loader1,
577                                                Handle<mirror::ClassLoader> class_loader2)
578      SHARED_REQUIRES(Locks::mutator_lock_);
579
580  bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, ArtMethod* method,
581                                                     mirror::Class* klass1, mirror::Class* klass2)
582      SHARED_REQUIRES(Locks::mutator_lock_);
583
584  bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass,
585                 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
586                 MutableHandle<mirror::Class>* h_new_class_out)
587      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::classlinker_classes_lock_);
588
589  bool LinkSuperClass(Handle<mirror::Class> klass)
590      SHARED_REQUIRES(Locks::mutator_lock_);
591
592  bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
593      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
594
595  bool LinkMethods(Thread* self,
596                   Handle<mirror::Class> klass,
597                   Handle<mirror::ObjectArray<mirror::Class>> interfaces,
598                   ArtMethod** out_imt)
599      SHARED_REQUIRES(Locks::mutator_lock_);
600
601  bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
602      SHARED_REQUIRES(Locks::mutator_lock_);
603
604  bool LinkInterfaceMethods(Thread* self, Handle<mirror::Class> klass,
605                            Handle<mirror::ObjectArray<mirror::Class>> interfaces,
606                            ArtMethod** out_imt)
607      SHARED_REQUIRES(Locks::mutator_lock_);
608
609  bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size)
610      SHARED_REQUIRES(Locks::mutator_lock_);
611  bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass)
612      SHARED_REQUIRES(Locks::mutator_lock_);
613  bool LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, size_t* class_size)
614      SHARED_REQUIRES(Locks::mutator_lock_);
615  void LinkCode(ArtMethod* method, const OatFile::OatClass* oat_class,
616                uint32_t class_def_method_index)
617      SHARED_REQUIRES(Locks::mutator_lock_);
618  void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
619      SHARED_REQUIRES(Locks::mutator_lock_);
620
621  void CheckProxyConstructor(ArtMethod* constructor) const
622      SHARED_REQUIRES(Locks::mutator_lock_);
623  void CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const
624      SHARED_REQUIRES(Locks::mutator_lock_);
625
626  // For use by ImageWriter to find DexCaches for its roots
627  ReaderWriterMutex* DexLock()
628      SHARED_REQUIRES(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
629    return &dex_lock_;
630  }
631  size_t GetDexCacheCount() SHARED_REQUIRES(Locks::mutator_lock_, dex_lock_) {
632    return dex_caches_.size();
633  }
634  mirror::DexCache* GetDexCache(size_t idx) SHARED_REQUIRES(Locks::mutator_lock_, dex_lock_);
635
636  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
637      REQUIRES(!dex_lock_);
638
639  // Returns the boot image oat file.
640  const OatFile* GetBootOatFile() SHARED_REQUIRES(dex_lock_);
641
642  void CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out)
643      SHARED_REQUIRES(Locks::mutator_lock_);
644  void CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, ArtMethod* out)
645      SHARED_REQUIRES(Locks::mutator_lock_);
646
647  // Ensures that methods have the kAccPreverified bit set. We use the kAccPreverfied bit on the
648  // class access flags to determine whether this has been done before.
649  void EnsurePreverifiedMethods(Handle<mirror::Class> c)
650      SHARED_REQUIRES(Locks::mutator_lock_);
651
652  mirror::Class* LookupClassFromImage(const char* descriptor)
653      SHARED_REQUIRES(Locks::mutator_lock_);
654
655  // Returns null if not found.
656  ClassTable* ClassTableForClassLoader(mirror::ClassLoader* class_loader)
657      SHARED_REQUIRES(Locks::mutator_lock_, Locks::classlinker_classes_lock_);
658  // Insert a new class table if not found.
659  ClassTable* InsertClassTableForClassLoader(mirror::ClassLoader* class_loader)
660      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::classlinker_classes_lock_);
661
662  // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
663  // before returning it to the caller. Its the responsibility of the thread that placed the class
664  // in the table to make it resolved. The thread doing resolution must notify on the class' lock
665  // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
666  // retire a class, the version of the class in the table is returned and this may differ from
667  // the class passed in.
668  mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
669      WARN_UNUSED SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
670
671  void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
672      SHARED_REQUIRES(Locks::mutator_lock_);
673
674  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
675      SHARED_REQUIRES(Locks::mutator_lock_);
676
677  // Return the quick generic JNI stub for testing.
678  const void* GetRuntimeQuickGenericJniStub() const;
679
680  // Throw the class initialization failure recorded when first trying to initialize the given
681  // class.
682  // Note: Currently we only store the descriptor, so we cannot throw the exact throwable, only
683  //       a recreation with a custom string.
684  void ThrowEarlierClassFailure(mirror::Class* c) SHARED_REQUIRES(Locks::mutator_lock_)
685      REQUIRES(!dex_lock_);
686
687  // Check for duplicate class definitions of the given oat file against all open oat files.
688  bool HasCollisions(const OatFile* oat_file, std::string* error_msg) REQUIRES(!dex_lock_);
689
690  bool HasInitWithString(Thread* self, const char* descriptor)
691      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
692
693  bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, bool can_init_parents)
694      SHARED_REQUIRES(Locks::mutator_lock_);
695
696  void UpdateClassVirtualMethods(mirror::Class* klass,
697                                 LengthPrefixedArray<ArtMethod>* new_methods)
698      SHARED_REQUIRES(Locks::mutator_lock_)
699      REQUIRES(!Locks::classlinker_classes_lock_);
700
701  std::vector<const DexFile*> boot_class_path_;
702  std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
703
704  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
705  std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);
706  std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
707  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
708
709  // This contains the class laoders which have class tables. It is populated by
710  // InsertClassTableForClassLoader.
711  std::vector<GcRoot<mirror::ClassLoader>> class_loaders_
712      GUARDED_BY(Locks::classlinker_classes_lock_);
713
714  // Boot class path table. Since the class loader for this is null.
715  ClassTable boot_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
716
717  // New class roots, only used by CMS since the GC needs to mark these in the pause.
718  std::vector<GcRoot<mirror::Class>> new_class_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
719
720  // Do we need to search dex caches to find image classes?
721  bool dex_cache_image_class_lookup_required_;
722  // Number of times we've searched dex caches for a class. After a certain number of misses we move
723  // the classes into the class_table_ to avoid dex cache based searches.
724  Atomic<uint32_t> failed_dex_cache_class_lookups_;
725
726  // Well known mirror::Class roots.
727  GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
728
729  // The interface table used by all arrays.
730  GcRoot<mirror::IfTable> array_iftable_;
731
732  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
733  // descriptors for the sake of performing FindClass.
734  static constexpr size_t kFindArrayCacheSize = 16;
735  GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
736  size_t find_array_class_cache_next_victim_;
737
738  bool init_done_;
739  bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
740  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
741
742  InternTable* intern_table_;
743
744  // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
745  // patch point within the image. TODO: make these proper relocations.
746  const void* quick_resolution_trampoline_;
747  const void* quick_imt_conflict_trampoline_;
748  const void* quick_generic_jni_trampoline_;
749  const void* quick_to_interpreter_bridge_trampoline_;
750
751  // Image pointer size.
752  size_t image_pointer_size_;
753
754  friend class ImageWriter;  // for GetClassRoots
755  friend class ImageDumper;  // for FindOpenedOatFileFromOatLocation
756  friend class JniCompilerTest;  // for GetRuntimeQuickGenericJniStub
757  ART_FRIEND_TEST(mirror::DexCacheTest, Open);  // for AllocDexCache
758
759  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
760};
761
762}  // namespace art
763
764#endif  // ART_RUNTIME_CLASS_LINKER_H_
765