class_linker.h revision c8ccf68b805c92674545f63e0341ba47e8d9701c
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 byte* 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  void CreateReferenceOffsets(Handle<mirror::Class> klass, uint32_t reference_offsets)
543      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
544
545  // For use by ImageWriter to find DexCaches for its roots
546  ReaderWriterMutex* DexLock()
547      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
548    return &dex_lock_;
549  }
550  size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
551    return dex_caches_.size();
552  }
553  mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
554
555  const OatFile::OatDexFile* FindOpenedOatDexFileForDexFile(const DexFile& dex_file)
556      LOCKS_EXCLUDED(dex_lock_)
557      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
558
559  // Find an opened oat dex file that contains dex_location. If oat_location is not nullptr,
560  // the file must have that location, else any oat location is accepted.
561  const OatFile::OatDexFile* FindOpenedOatDexFile(const char* oat_location,
562                                                  const char* dex_location,
563                                                  const uint32_t* dex_location_checksum)
564      LOCKS_EXCLUDED(dex_lock_);
565
566  // Will open the oat file directly without relocating, even if we could/should do relocation.
567  const OatFile* FindOatFileFromOatLocation(const std::string& oat_location,
568                                            std::string* error_msg)
569      LOCKS_EXCLUDED(dex_lock_);
570
571  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
572      LOCKS_EXCLUDED(dex_lock_);
573
574  const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location,
575                                            InstructionSet isa,
576                                            bool* already_opened,
577                                            bool* obsolete_file_cleanup_failed,
578                                            std::vector<std::string>* error_msg)
579      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
580
581  const OatFile* GetInterpretedOnlyOat(const std::string& oat_path,
582                                       InstructionSet isa,
583                                       std::string* error_msg);
584
585  const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output,
586                                     const std::string& image_location, InstructionSet isa,
587                                     std::string* error_msg)
588      LOCKS_EXCLUDED(Locks::mutator_lock_);
589
590  bool CheckOatFile(const OatFile* oat_file, InstructionSet isa,
591                    bool* checksum_verified, std::string* error_msg);
592  int32_t GetRequiredDelta(const OatFile* oat_file, InstructionSet isa);
593
594  // Note: will not register the oat file.
595  const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location,
596                                                    uint32_t dex_location_checksum,
597                                                    const char* oat_location,
598                                                    std::string* error_msg)
599      LOCKS_EXCLUDED(dex_lock_);
600
601  // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for
602  // the file to be written, which is assumed to be under a lock.
603  const OatFile* CreateOatFileForDexLocation(const char* dex_location,
604                                             int fd, const char* oat_location,
605                                             std::vector<std::string>* error_msgs)
606      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
607
608  // Finds an OatFile that contains a DexFile for the given a DexFile location.
609  //
610  // Note 1: this will not check open oat files, which are assumed to be stale when this is run.
611  // Note 2: Does not register the oat file. It is the caller's job to register if the file is to
612  //         be kept.
613  const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* dex_location,
614                                                             const uint32_t* dex_location_checksum,
615                                                             InstructionSet isa,
616                                                             std::vector<std::string>* error_msgs,
617                                                             bool* obsolete_file_cleanup_failed)
618      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
619
620  // Verifies:
621  //  - that the oat file contains the dex file (with a matching checksum, which may be null if the
622  // file was pre-opted)
623  //  - the checksums of the oat file (against the image space)
624  //  - the checksum of the dex file against dex_location_checksum
625  //  - that the dex file can be opened
626  // Returns true iff all verification succeed.
627  //
628  // The dex_location is the dex location as stored in the oat file header.
629  // (see DexFile::GetDexCanonicalLocation for a description of location conventions)
630  bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location,
631                            const uint32_t* dex_location_checksum,
632                            std::string* error_msg);
633
634  mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
635                                            mirror::Class* proxy_class)
636      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
637  mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
638                                       Handle<mirror::ArtMethod> prototype)
639      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
640
641  // Ensures that methods have the kAccPreverified bit set. We use the kAccPreverfied bit on the
642  // class access flags to determine whether this has been done before.
643  void EnsurePreverifiedMethods(Handle<mirror::Class> c)
644      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
645
646  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
647                                            const mirror::ClassLoader* class_loader,
648                                            size_t hash)
649      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
650
651  mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
652      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
653      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
654
655  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
656      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
657      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
658  mirror::Class* LookupClassFromImage(const char* descriptor)
659      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
660
661  // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
662  // before returning it to the caller. Its the responsibility of the thread that placed the class
663  // in the table to make it resolved. The thread doing resolution must notify on the class' lock
664  // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
665  // retire a class, the version of the class in the table is returned and this may differ from
666  // the class passed in.
667  mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
668      WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
669
670  void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
671      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
672
673  std::vector<const DexFile*> boot_class_path_;
674
675  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
676  std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);
677  std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
678  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
679
680
681  // multimap from a string hash code of a class descriptor to
682  // mirror::Class* instances. Results should be compared for a matching
683  // Class::descriptor_ and Class::class_loader_.
684  typedef AllocationTrackingMultiMap<size_t, GcRoot<mirror::Class>, kAllocatorTagClassTable> Table;
685  // This contains strong roots. To enable concurrent root scanning of
686  // the class table, be careful to use a read barrier when accessing this.
687  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
688  std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_;
689
690  // Do we need to search dex caches to find image classes?
691  bool dex_cache_image_class_lookup_required_;
692  // Number of times we've searched dex caches for a class. After a certain number of misses we move
693  // the classes into the class_table_ to avoid dex cache based searches.
694  Atomic<uint32_t> failed_dex_cache_class_lookups_;
695
696  // indexes into class_roots_.
697  // needs to be kept in sync with class_roots_descriptors_.
698  enum ClassRoot {
699    kJavaLangClass,
700    kJavaLangObject,
701    kClassArrayClass,
702    kObjectArrayClass,
703    kJavaLangString,
704    kJavaLangDexCache,
705    kJavaLangRefReference,
706    kJavaLangReflectArtField,
707    kJavaLangReflectArtMethod,
708    kJavaLangReflectProxy,
709    kJavaLangStringArrayClass,
710    kJavaLangReflectArtFieldArrayClass,
711    kJavaLangReflectArtMethodArrayClass,
712    kJavaLangClassLoader,
713    kJavaLangThrowable,
714    kJavaLangClassNotFoundException,
715    kJavaLangStackTraceElement,
716    kPrimitiveBoolean,
717    kPrimitiveByte,
718    kPrimitiveChar,
719    kPrimitiveDouble,
720    kPrimitiveFloat,
721    kPrimitiveInt,
722    kPrimitiveLong,
723    kPrimitiveShort,
724    kPrimitiveVoid,
725    kBooleanArrayClass,
726    kByteArrayClass,
727    kCharArrayClass,
728    kDoubleArrayClass,
729    kFloatArrayClass,
730    kIntArrayClass,
731    kLongArrayClass,
732    kShortArrayClass,
733    kJavaLangStackTraceElementArrayClass,
734    kClassRootsMax,
735  };
736  GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
737
738  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
739
740  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
741      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
742
743  static const char* class_roots_descriptors_[];
744
745  const char* GetClassRootDescriptor(ClassRoot class_root) {
746    const char* descriptor = class_roots_descriptors_[class_root];
747    CHECK(descriptor != NULL);
748    return descriptor;
749  }
750
751  // The interface table used by all arrays.
752  GcRoot<mirror::IfTable> array_iftable_;
753
754  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
755  // descriptors for the sake of performing FindClass.
756  static constexpr size_t kFindArrayCacheSize = 16;
757  GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
758  size_t find_array_class_cache_next_victim_;
759
760  bool init_done_;
761  bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
762  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
763
764  InternTable* intern_table_;
765
766  // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
767  // patch point within the image. TODO: make these proper relocations.
768  const void* portable_resolution_trampoline_;
769  const void* quick_resolution_trampoline_;
770  const void* portable_imt_conflict_trampoline_;
771  const void* quick_imt_conflict_trampoline_;
772  const void* quick_generic_jni_trampoline_;
773  const void* quick_to_interpreter_bridge_trampoline_;
774
775  friend class ImageWriter;  // for GetClassRoots
776  friend class ImageDumper;  // for FindOpenedOatFileFromOatLocation
777  friend class ElfPatcher;  // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation
778  friend class NoDex2OatTest;  // for FindOpenedOatFileForDexFile
779  friend class NoPatchoatTest;  // for FindOpenedOatFileForDexFile
780  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
781  FRIEND_TEST(mirror::DexCacheTest, Open);
782  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
783  FRIEND_TEST(ObjectTest, AllocObjectArray);
784  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
785};
786
787}  // namespace art
788
789#endif  // ART_RUNTIME_CLASS_LINKER_H_
790