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