class_linker.h revision 52e4b43d62896b56f8c2bd041e528472bb4a0d8d
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_CLASS_LINKER_H_
18#define ART_RUNTIME_CLASS_LINKER_H_
19
20#include <string>
21#include <utility>
22#include <vector>
23
24#include "base/macros.h"
25#include "base/mutex.h"
26#include "dex_file.h"
27#include "gtest/gtest.h"
28#include "jni.h"
29#include "oat_file.h"
30#include "object_callbacks.h"
31
32namespace art {
33namespace gc {
34namespace space {
35  class ImageSpace;
36}  // namespace space
37}  // namespace gc
38namespace mirror {
39  class ClassLoader;
40  class DexCache;
41  class DexCacheTest_Open_Test;
42  class IfTable;
43  template<class T> class ObjectArray;
44  class StackTraceElement;
45}  // namespace mirror
46
47class InternTable;
48template<class T> class ObjectLock;
49class ScopedObjectAccessAlreadyRunnable;
50template<class T> class Handle;
51
52typedef bool (ClassVisitor)(mirror::Class* c, void* arg);
53
54enum VisitRootFlags : uint8_t;
55
56class ClassLinker {
57 public:
58  // Interface method table size. Increasing this value reduces the chance of two interface methods
59  // colliding in the interface method table but increases the size of classes that implement
60  // (non-marker) interfaces.
61  static constexpr size_t kImtSize = 64;
62
63  explicit ClassLinker(InternTable* intern_table);
64  ~ClassLinker();
65
66  // Initialize class linker by bootstraping from dex files
67  void InitFromCompiler(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  bool IsInBootClassPath(const char* descriptor);
74
75  // Finds a class by its descriptor, loading it if necessary.
76  // If class_loader is null, searches boot_class_path_.
77  mirror::Class* FindClass(Thread* self, const char* descriptor,
78                           Handle<mirror::ClassLoader> class_loader)
79      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80
81  // Finds a class by its descriptor using the "system" class loader, ie by searching the
82  // boot_class_path_.
83  mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
84      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
85
86  // Finds the array class given for the element class.
87  mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
88      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89
90  // Reutrns true if the class linker is initialized.
91  bool IsInitialized() const;
92
93  // Define a new a class based on a ClassDef from a DexFile
94  mirror::Class* DefineClass(const char* descriptor,
95                             Handle<mirror::ClassLoader> class_loader,
96                             const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
97      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98
99  // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
100  // by the given 'class_loader'.
101  mirror::Class* LookupClass(const char* descriptor, const mirror::ClassLoader* class_loader)
102      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
103      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
104
105  // Finds all the classes with the given descriptor, regardless of ClassLoader.
106  void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
107      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
108      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
109
110  mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111
112  // General class unloading is not supported, this is used to prune
113  // unwanted classes during image writing.
114  bool RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader)
115      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
116      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
117
118  void DumpAllClasses(int flags)
119      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
120      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121
122  void DumpForSigQuit(std::ostream& os)
123      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
124      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
125
126  size_t NumLoadedClasses()
127      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
128      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129
130  // Resolve a String with the given index from the DexFile, storing the
131  // result in the DexCache. The referrer is used to identify the
132  // target DexCache and ClassLoader to use for resolution.
133  mirror::String* ResolveString(uint32_t string_idx, mirror::ArtMethod* referrer)
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.
138  mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
139                                Handle<mirror::DexCache> dex_cache)
140      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
141
142  // Resolve a Type with the given index from the DexFile, storing the
143  // result in the DexCache. The referrer is used to identity the
144  // target DexCache and ClassLoader to use for resolution.
145  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
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 identify the
150  // target DexCache and ClassLoader to use for resolution.
151  mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer)
152      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
153
154  mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtField* referrer)
155      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156
157  // Resolve a type with the given ID from the DexFile, storing the
158  // result in DexCache. The ClassLoader is used to search for the
159  // type, since it may be referenced from but not contained within
160  // the given DexFile.
161  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
162                             Handle<mirror::DexCache> dex_cache,
163                             Handle<mirror::ClassLoader> class_loader)
164      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
165
166  // Resolve a method with a given ID from the DexFile, storing the
167  // result in DexCache. The ClassLinker and ClassLoader are used as
168  // in ResolveType. What is unique is the method type argument which
169  // is used to determine if this method is a direct, static, or
170  // virtual method.
171  mirror::ArtMethod* ResolveMethod(const DexFile& dex_file,
172                                   uint32_t method_idx,
173                                   Handle<mirror::DexCache> dex_cache,
174                                   Handle<mirror::ClassLoader> class_loader,
175                                   Handle<mirror::ArtMethod> referrer,
176                                   InvokeType type)
177      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
178
179  mirror::ArtMethod* GetResolvedMethod(uint32_t method_idx, mirror::ArtMethod* referrer,
180                                       InvokeType type)
181      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
182  mirror::ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, mirror::ArtMethod** referrer,
183                                   InvokeType type)
184      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
185
186  mirror::ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
187      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
188  mirror::ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
189                                 bool is_static)
190      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
191
192  // Resolve a field with a given ID from the DexFile, storing the
193  // result in DexCache. The ClassLinker and ClassLoader are used as
194  // in ResolveType. What is unique is the is_static argument which is
195  // used to determine if we are resolving a static or non-static
196  // field.
197  mirror::ArtField* ResolveField(const DexFile& dex_file,
198                                 uint32_t field_idx,
199                                 Handle<mirror::DexCache> dex_cache,
200                                 Handle<mirror::ClassLoader> class_loader,
201                                 bool is_static)
202      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
203
204  // Resolve a field with a given ID from the DexFile, storing the
205  // result in DexCache. The ClassLinker and ClassLoader are used as
206  // in ResolveType. No is_static argument is provided so that Java
207  // field resolution semantics are followed.
208  mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file, uint32_t field_idx,
209                                    Handle<mirror::DexCache> dex_cache,
210                                    Handle<mirror::ClassLoader> class_loader)
211      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
212
213  // Get shorty from method index without resolution. Used to do handlerization.
214  const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length)
215      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
216
217  // Returns true on success, false if there's an exception pending.
218  // can_run_clinit=false allows the compiler to attempt to init a class,
219  // given the restriction that no <clinit> execution is possible.
220  bool EnsureInitialized(Handle<mirror::Class> c, bool can_init_fields, bool can_init_parents)
221      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
222
223  // Initializes classes that have instances in the image but that have
224  // <clinit> methods so they could not be initialized by the compiler.
225  void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
226
227  void RegisterDexFile(const DexFile& dex_file)
228      LOCKS_EXCLUDED(dex_lock_)
229      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
230  void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
231      LOCKS_EXCLUDED(dex_lock_)
232      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
233
234  const OatFile* RegisterOatFile(const OatFile* oat_file)
235      LOCKS_EXCLUDED(dex_lock_);
236
237  const std::vector<const DexFile*>& GetBootClassPath() {
238    return boot_class_path_;
239  }
240
241  void VisitClasses(ClassVisitor* visitor, void* arg)
242      LOCKS_EXCLUDED(dex_lock_)
243      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
244  // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_
245  // when calling the visitor.
246  void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg)
247      LOCKS_EXCLUDED(dex_lock_)
248      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
249
250  void VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
251      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_);
252  void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
253      LOCKS_EXCLUDED(dex_lock_);
254
255  mirror::DexCache* FindDexCache(const DexFile& dex_file) const
256      LOCKS_EXCLUDED(dex_lock_)
257      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
258  bool IsDexFileRegistered(const DexFile& dex_file) const
259      LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
260  void FixupDexCaches(mirror::ArtMethod* resolution_method) const
261      LOCKS_EXCLUDED(dex_lock_)
262      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
263
264  // Generate an oat file from a dex file
265  bool GenerateOatFile(const char* dex_filename,
266                       int oat_fd,
267                       const char* oat_cache_filename,
268                       std::string* error_msg)
269      LOCKS_EXCLUDED(Locks::mutator_lock_);
270
271  const OatFile* FindOatFileFromOatLocation(const std::string& location,
272                                            std::string* error_msg)
273      LOCKS_EXCLUDED(dex_lock_);
274
275  // Finds the oat file for a dex location, generating the oat file if
276  // it is missing or out of date. Returns the DexFile from within the
277  // created oat file.
278  const DexFile* FindOrCreateOatFileForDexLocation(const char* dex_location,
279                                                   uint32_t dex_location_checksum,
280                                                   const char* oat_location,
281                                                   std::vector<std::string>* error_msgs)
282      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
283  // Find a DexFile within an OatFile given a DexFile location. Note
284  // that this returns null if the location checksum of the DexFile
285  // does not match the OatFile.
286  const DexFile* FindDexFileInOatFileFromDexLocation(const char* location,
287                                                     const uint32_t* const location_checksum,
288                                                     InstructionSet isa,
289                                                     std::vector<std::string>* error_msgs)
290      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
291
292
293  // Returns true if oat file contains the dex file with the given location and checksum.
294  static bool VerifyOatFileChecksums(const OatFile* oat_file,
295                                     const char* dex_location,
296                                     uint32_t dex_location_checksum,
297                                     InstructionSet instruction_set,
298                                     std::string* error_msg);
299
300  // TODO: replace this with multiple methods that allocate the correct managed type.
301  template <class T>
302  mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
303      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
304
305  mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
306      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
307
308  mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
309      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
310
311  mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
312      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
313
314  mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
315      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
316
317  mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
318      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
319
320  mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
321                                                                              size_t length)
322      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
323
324  void VerifyClass(Handle<mirror::Class> klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
325  bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
326                               mirror::Class::Status& oat_file_class_status)
327      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
328  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
329                                         Handle<mirror::Class> klass)
330      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
331  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
332      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
333
334  mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
335                                  jobjectArray interfaces, jobject loader, jobjectArray methods,
336                                  jobjectArray throws)
337      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
338  std::string GetDescriptorForProxy(mirror::Class* proxy_class)
339      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
340  mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
341                                        mirror::ArtMethod* proxy_method)
342      LOCKS_EXCLUDED(dex_lock_)
343      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
344
345  // Get the oat code for a method when its class isn't yet initialized
346  const void* GetQuickOatCodeFor(mirror::ArtMethod* method)
347      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
348  const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code)
349      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
350
351  // Get the oat code for a method from a method index.
352  const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
353      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
354  const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
355      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
356
357  pid_t GetClassesLockOwner();  // For SignalCatcher.
358  pid_t GetDexLockOwner();  // For SignalCatcher.
359
360  const void* GetPortableResolutionTrampoline() const {
361    return portable_resolution_trampoline_;
362  }
363
364  const void* GetQuickGenericJniTrampoline() const {
365    return quick_generic_jni_trampoline_;
366  }
367
368  const void* GetQuickResolutionTrampoline() const {
369    return quick_resolution_trampoline_;
370  }
371
372  const void* GetPortableImtConflictTrampoline() const {
373    return portable_imt_conflict_trampoline_;
374  }
375
376  const void* GetQuickImtConflictTrampoline() const {
377    return quick_imt_conflict_trampoline_;
378  }
379
380  const void* GetQuickToInterpreterBridgeTrampoline() const {
381    return quick_to_interpreter_bridge_trampoline_;
382  }
383
384  InternTable* GetInternTable() const {
385    return intern_table_;
386  }
387
388  // Attempts to insert a class into a class table.  Returns NULL if
389  // the class was inserted, otherwise returns an existing class with
390  // the same descriptor and ClassLoader.
391  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
392      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
393      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
394
395  // Special code to allocate an art method, use this instead of class->AllocObject.
396  mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
397
398 private:
399  const OatFile::OatMethod GetOatMethodFor(mirror::ArtMethod* method)
400      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
401
402  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
403      LOCKS_EXCLUDED(dex_lock_)
404      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
405
406  void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
407
408  // For early bootstrapping by Init
409  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
410      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
411
412  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
413  // values that are known to the ClassLinker such as
414  // kObjectArrayClass and kJavaLangString etc.
415  mirror::Class* AllocClass(Thread* self, uint32_t class_size)
416      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
417  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
418      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
419  mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
420
421  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
422      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
423  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
424      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
425
426
427  mirror::Class* CreateArrayClass(Thread* self, const char* descriptor,
428                                  Handle<mirror::ClassLoader> class_loader)
429      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
430
431  void AppendToBootClassPath(const DexFile& dex_file)
432      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
433  void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
434      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
435
436  void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
437                         mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
438      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
439
440  uint32_t SizeOfClass(const DexFile& dex_file,
441                     const DexFile::ClassDef& dex_class_def);
442
443  void LoadClass(const DexFile& dex_file,
444                 const DexFile::ClassDef& dex_class_def,
445                 Handle<mirror::Class> klass,
446                 mirror::ClassLoader* class_loader)
447      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
448  void LoadClassMembers(const DexFile& dex_file,
449                        const byte* class_data,
450                        Handle<mirror::Class> klass,
451                        mirror::ClassLoader* class_loader,
452                        const OatFile::OatClass* oat_class)
453      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
454
455  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
456                 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst)
457      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
458
459  mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
460                                const ClassDataItemIterator& dex_method,
461                                Handle<mirror::Class> klass)
462      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
463
464  void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
465
466  // Finds the associated oat class for a dex_file and descriptor
467  OatFile::OatClass GetOatClass(const DexFile& dex_file, uint16_t class_def_idx)
468      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
469
470  void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
471      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
472      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
473  bool IsDexFileRegisteredLocked(const DexFile& dex_file) const
474      SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
475
476  bool InitializeClass(Handle<mirror::Class> klass, bool can_run_clinit,
477                       bool can_init_parents)
478      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
479  bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
480                              ObjectLock<mirror::Class>& lock);
481  bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
482      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
483
484  bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
485                                                Handle<mirror::ClassLoader> class_loader1,
486                                                Handle<mirror::ClassLoader> class_loader2)
487      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
488
489  bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
490                                                     mirror::Class* klass1,
491                                                     mirror::Class* klass2)
492      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
493
494  bool LinkClass(Thread* self, Handle<mirror::Class> klass,
495                 Handle<mirror::ObjectArray<mirror::Class>> interfaces)
496      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
497
498  bool LinkSuperClass(Handle<mirror::Class> klass)
499      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
500
501  bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
502      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
503
504  bool LinkMethods(Thread* self, Handle<mirror::Class> klass,
505                   Handle<mirror::ObjectArray<mirror::Class>> interfaces)
506      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
507
508  bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
509      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
510
511  bool LinkInterfaceMethods(Handle<mirror::Class> klass,
512                            Handle<mirror::ObjectArray<mirror::Class>> interfaces)
513      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
514
515  bool LinkStaticFields(Handle<mirror::Class> klass)
516      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
517  bool LinkInstanceFields(Handle<mirror::Class> klass)
518      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
519  bool LinkFields(Handle<mirror::Class> klass, bool is_static)
520      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
521  void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
522                const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index)
523      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
524
525
526  void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
527      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
528  void CreateReferenceStaticOffsets(Handle<mirror::Class> klass)
529      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
530  void CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static,
531                              uint32_t reference_offsets)
532      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
533
534  // For use by ImageWriter to find DexCaches for its roots
535  const std::vector<mirror::DexCache*>& GetDexCaches() {
536    return dex_caches_;
537  }
538
539  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
540      LOCKS_EXCLUDED(dex_lock_)
541      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
542  const OatFile* FindOpenedOatFileFromDexLocation(const char* dex_location,
543                                                  const uint32_t* const dex_location_checksum)
544      LOCKS_EXCLUDED(dex_lock_);
545  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
546      LOCKS_EXCLUDED(dex_lock_);
547  const DexFile* FindDexFileInOatLocation(const char* dex_location,
548                                          uint32_t dex_location_checksum,
549                                          const char* oat_location,
550                                          std::string* error_msg)
551      LOCKS_EXCLUDED(dex_lock_);
552
553  const DexFile* VerifyAndOpenDexFileFromOatFile(const std::string& oat_file_location,
554                                                 const char* dex_location,
555                                                 std::string* error_msg,
556                                                 bool* open_failed)
557      LOCKS_EXCLUDED(dex_lock_);
558
559  mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
560                                            mirror::Class* proxy_class)
561      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
562  mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
563                                       Handle<mirror::ArtMethod> prototype)
564      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
565
566  std::vector<const DexFile*> boot_class_path_;
567
568  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
569  std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);;
570  std::vector<mirror::DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
571  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
572
573
574  // multimap from a string hash code of a class descriptor to
575  // mirror::Class* instances. Results should be compared for a matching
576  // Class::descriptor_ and Class::class_loader_.
577  typedef std::multimap<size_t, mirror::Class*> Table;
578  // This contains strong roots. To enable concurrent root scanning of
579  // the class table, be careful to use a read barrier when accessing this.
580  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
581  std::vector<std::pair<size_t, mirror::Class*>> new_class_roots_;
582
583  // Do we need to search dex caches to find image classes?
584  bool dex_cache_image_class_lookup_required_;
585  // Number of times we've searched dex caches for a class. After a certain number of misses we move
586  // the classes into the class_table_ to avoid dex cache based searches.
587  AtomicInteger failed_dex_cache_class_lookups_;
588
589  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
590                                            const mirror::ClassLoader* class_loader,
591                                            size_t hash)
592      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
593
594  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
595      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
596  mirror::Class* LookupClassFromImage(const char* descriptor)
597      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
598
599  // indexes into class_roots_.
600  // needs to be kept in sync with class_roots_descriptors_.
601  enum ClassRoot {
602    kJavaLangClass,
603    kJavaLangObject,
604    kClassArrayClass,
605    kObjectArrayClass,
606    kJavaLangString,
607    kJavaLangDexCache,
608    kJavaLangRefReference,
609    kJavaLangReflectArtField,
610    kJavaLangReflectArtMethod,
611    kJavaLangReflectProxy,
612    kJavaLangStringArrayClass,
613    kJavaLangReflectArtFieldArrayClass,
614    kJavaLangReflectArtMethodArrayClass,
615    kJavaLangClassLoader,
616    kJavaLangThrowable,
617    kJavaLangClassNotFoundException,
618    kJavaLangStackTraceElement,
619    kPrimitiveBoolean,
620    kPrimitiveByte,
621    kPrimitiveChar,
622    kPrimitiveDouble,
623    kPrimitiveFloat,
624    kPrimitiveInt,
625    kPrimitiveLong,
626    kPrimitiveShort,
627    kPrimitiveVoid,
628    kBooleanArrayClass,
629    kByteArrayClass,
630    kCharArrayClass,
631    kDoubleArrayClass,
632    kFloatArrayClass,
633    kIntArrayClass,
634    kLongArrayClass,
635    kShortArrayClass,
636    kJavaLangStackTraceElementArrayClass,
637    kClassRootsMax,
638  };
639  mirror::ObjectArray<mirror::Class>* class_roots_;
640
641  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
642
643  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
644      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
645
646  mirror::ObjectArray<mirror::Class>* GetClassRoots() {
647    DCHECK(class_roots_ != NULL);
648    return class_roots_;
649  }
650
651  static const char* class_roots_descriptors_[];
652
653  const char* GetClassRootDescriptor(ClassRoot class_root) {
654    const char* descriptor = class_roots_descriptors_[class_root];
655    CHECK(descriptor != NULL);
656    return descriptor;
657  }
658
659  // The interface table used by all arrays.
660  mirror::IfTable* array_iftable_;
661
662  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
663  // descriptors for the sake of performing FindClass.
664  static constexpr size_t kFindArrayCacheSize = 16;
665  mirror::Class* find_array_class_cache_[kFindArrayCacheSize];
666  size_t find_array_class_cache_next_victim_;
667
668  bool init_done_;
669  bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
670  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
671
672  InternTable* intern_table_;
673
674  const void* portable_resolution_trampoline_;
675  const void* quick_resolution_trampoline_;
676  const void* portable_imt_conflict_trampoline_;
677  const void* quick_imt_conflict_trampoline_;
678  const void* quick_generic_jni_trampoline_;
679  const void* quick_to_interpreter_bridge_trampoline_;
680
681  friend class ImageWriter;  // for GetClassRoots
682  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
683  FRIEND_TEST(mirror::DexCacheTest, Open);
684  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
685  FRIEND_TEST(ObjectTest, AllocObjectArray);
686  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
687};
688
689}  // namespace art
690
691#endif  // ART_RUNTIME_CLASS_LINKER_H_
692