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