class_linker.h revision 0cd81352a7c06e381951cea1b104fd73516f4341
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 VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
251      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_, dex_lock_);
252
253  mirror::DexCache* FindDexCache(const DexFile& dex_file) const
254      LOCKS_EXCLUDED(dex_lock_)
255      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
256  bool IsDexFileRegistered(const DexFile& dex_file) const
257      LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
258  void FixupDexCaches(mirror::ArtMethod* resolution_method) const
259      LOCKS_EXCLUDED(dex_lock_)
260      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
261
262  // Generate an oat file from a dex file
263  bool GenerateOatFile(const char* dex_filename,
264                       int oat_fd,
265                       const char* oat_cache_filename,
266                       std::string* error_msg)
267      LOCKS_EXCLUDED(Locks::mutator_lock_);
268
269  const OatFile* FindOatFileFromOatLocation(const std::string& location,
270                                            std::string* error_msg)
271      LOCKS_EXCLUDED(dex_lock_);
272
273  // Finds the oat file for a dex location, generating the oat file if
274  // it is missing or out of date. Returns the DexFile from within the
275  // created oat file.
276  const DexFile* FindOrCreateOatFileForDexLocation(const char* dex_location,
277                                                   uint32_t dex_location_checksum,
278                                                   const char* oat_location,
279                                                   std::vector<std::string>* error_msgs)
280      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
281  // Find a DexFile within an OatFile given a DexFile location. Note
282  // that this returns null if the location checksum of the DexFile
283  // does not match the OatFile.
284  const DexFile* FindDexFileInOatFileFromDexLocation(const char* location,
285                                                     const uint32_t* const location_checksum,
286                                                     InstructionSet isa,
287                                                     std::vector<std::string>* error_msgs)
288      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
289
290
291  // Returns true if oat file contains the dex file with the given location and checksum.
292  static bool VerifyOatFileChecksums(const OatFile* oat_file,
293                                     const char* dex_location,
294                                     uint32_t dex_location_checksum,
295                                     InstructionSet instruction_set,
296                                     std::string* error_msg);
297
298  // TODO: replace this with multiple methods that allocate the correct managed type.
299  template <class T>
300  mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
301      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
302
303  mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
304      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
305
306  mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
307      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
308
309  mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
310      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
311
312  mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
313      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
314
315  mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
316      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
317
318  mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
319                                                                              size_t length)
320      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
321
322  void VerifyClass(Handle<mirror::Class> klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
323  bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
324                               mirror::Class::Status& oat_file_class_status)
325      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
326  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
327                                         Handle<mirror::Class> klass)
328      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
329  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
330      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
331
332  mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
333                                  jobjectArray interfaces, jobject loader, jobjectArray methods,
334                                  jobjectArray throws)
335      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
336  std::string GetDescriptorForProxy(mirror::Class* proxy_class)
337      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
338  mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
339                                        mirror::ArtMethod* proxy_method)
340      LOCKS_EXCLUDED(dex_lock_)
341      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
342
343  // Get the oat code for a method when its class isn't yet initialized
344  const void* GetQuickOatCodeFor(mirror::ArtMethod* method)
345      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
346  const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code)
347      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
348
349  // Get the oat code for a method from a method index.
350  const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
351      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
352  const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
353      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
354
355  pid_t GetClassesLockOwner();  // For SignalCatcher.
356  pid_t GetDexLockOwner();  // For SignalCatcher.
357
358  const void* GetPortableResolutionTrampoline() const {
359    return portable_resolution_trampoline_;
360  }
361
362  const void* GetQuickGenericJniTrampoline() const {
363    return quick_generic_jni_trampoline_;
364  }
365
366  const void* GetQuickResolutionTrampoline() const {
367    return quick_resolution_trampoline_;
368  }
369
370  const void* GetPortableImtConflictTrampoline() const {
371    return portable_imt_conflict_trampoline_;
372  }
373
374  const void* GetQuickImtConflictTrampoline() const {
375    return quick_imt_conflict_trampoline_;
376  }
377
378  const void* GetQuickToInterpreterBridgeTrampoline() const {
379    return quick_to_interpreter_bridge_trampoline_;
380  }
381
382  InternTable* GetInternTable() const {
383    return intern_table_;
384  }
385
386  // Attempts to insert a class into a class table.  Returns NULL if
387  // the class was inserted, otherwise returns an existing class with
388  // the same descriptor and ClassLoader.
389  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
390      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
391      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
392
393  // Special code to allocate an art method, use this instead of class->AllocObject.
394  mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
395
396 private:
397  const OatFile::OatMethod GetOatMethodFor(mirror::ArtMethod* method)
398      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
399
400  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
401      LOCKS_EXCLUDED(dex_lock_)
402      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
403
404  void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
405
406  // For early bootstrapping by Init
407  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
408      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
409
410  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
411  // values that are known to the ClassLinker such as
412  // kObjectArrayClass and kJavaLangString etc.
413  mirror::Class* AllocClass(Thread* self, uint32_t class_size)
414      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
415  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
416      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
417  mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
418
419  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
420      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
421  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
422      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
423
424
425  mirror::Class* CreateArrayClass(Thread* self, const char* descriptor,
426                                  Handle<mirror::ClassLoader> class_loader)
427      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
428
429  void AppendToBootClassPath(const DexFile& dex_file)
430      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
431  void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
432      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
433
434  void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
435                         mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
436      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
437
438  uint32_t SizeOfClass(const DexFile& dex_file,
439                     const DexFile::ClassDef& dex_class_def);
440
441  void LoadClass(const DexFile& dex_file,
442                 const DexFile::ClassDef& dex_class_def,
443                 Handle<mirror::Class> klass,
444                 mirror::ClassLoader* class_loader)
445      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
446  void LoadClassMembers(const DexFile& dex_file,
447                        const byte* class_data,
448                        Handle<mirror::Class> klass,
449                        mirror::ClassLoader* class_loader,
450                        const OatFile::OatClass* oat_class)
451      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
452
453  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
454                 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst)
455      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
456
457  mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
458                                const ClassDataItemIterator& dex_method,
459                                Handle<mirror::Class> klass)
460      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
461
462  void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
463
464  // Finds the associated oat class for a dex_file and descriptor
465  OatFile::OatClass GetOatClass(const DexFile& dex_file, uint16_t class_def_idx)
466      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
467
468  void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
469      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
470      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
471  bool IsDexFileRegisteredLocked(const DexFile& dex_file) const
472      SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
473
474  bool InitializeClass(Handle<mirror::Class> klass, bool can_run_clinit,
475                       bool can_init_parents)
476      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
477  bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
478                              ObjectLock<mirror::Class>& lock);
479  bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
480      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
481
482  bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
483                                                Handle<mirror::ClassLoader> class_loader1,
484                                                Handle<mirror::ClassLoader> class_loader2)
485      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
486
487  bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
488                                                     mirror::Class* klass1,
489                                                     mirror::Class* klass2)
490      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
491
492  bool LinkClass(Thread* self, Handle<mirror::Class> klass,
493                 Handle<mirror::ObjectArray<mirror::Class>> interfaces)
494      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
495
496  bool LinkSuperClass(Handle<mirror::Class> klass)
497      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
498
499  bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
500      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
501
502  bool LinkMethods(Handle<mirror::Class> klass,
503                   Handle<mirror::ObjectArray<mirror::Class>> interfaces)
504      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
505
506  bool LinkVirtualMethods(Handle<mirror::Class> klass)
507      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
508
509  bool LinkInterfaceMethods(Handle<mirror::Class> klass,
510                            Handle<mirror::ObjectArray<mirror::Class>> interfaces)
511      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
512
513  bool LinkStaticFields(Handle<mirror::Class> klass)
514      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
515  bool LinkInstanceFields(Handle<mirror::Class> klass)
516      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
517  bool LinkFields(Handle<mirror::Class> klass, bool is_static)
518      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
519  void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
520                const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index)
521      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
522
523
524  void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
525      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
526  void CreateReferenceStaticOffsets(Handle<mirror::Class> klass)
527      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
528  void CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static,
529                              uint32_t reference_offsets)
530      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
531
532  // For use by ImageWriter to find DexCaches for its roots
533  const std::vector<mirror::DexCache*>& GetDexCaches() {
534    return dex_caches_;
535  }
536
537  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
538      LOCKS_EXCLUDED(dex_lock_)
539      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
540  const OatFile* FindOpenedOatFileFromDexLocation(const char* dex_location,
541                                                  const uint32_t* const dex_location_checksum)
542      LOCKS_EXCLUDED(dex_lock_);
543  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
544      LOCKS_EXCLUDED(dex_lock_);
545  const DexFile* FindDexFileInOatLocation(const char* dex_location,
546                                          uint32_t dex_location_checksum,
547                                          const char* oat_location,
548                                          std::string* error_msg)
549      LOCKS_EXCLUDED(dex_lock_);
550
551  const DexFile* VerifyAndOpenDexFileFromOatFile(const std::string& oat_file_location,
552                                                 const char* dex_location,
553                                                 std::string* error_msg,
554                                                 bool* open_failed)
555      LOCKS_EXCLUDED(dex_lock_);
556
557  mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
558                                            mirror::Class* proxy_class)
559      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
560  mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
561                                       Handle<mirror::ArtMethod> prototype)
562      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
563
564  std::vector<const DexFile*> boot_class_path_;
565
566  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
567  std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);;
568  std::vector<mirror::DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
569  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
570
571
572  // multimap from a string hash code of a class descriptor to
573  // mirror::Class* instances. Results should be compared for a matching
574  // Class::descriptor_ and Class::class_loader_.
575  typedef std::multimap<size_t, mirror::Class*> Table;
576  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
577  std::vector<std::pair<size_t, mirror::Class*>> new_class_roots_;
578
579  // Do we need to search dex caches to find image classes?
580  bool dex_cache_image_class_lookup_required_;
581  // Number of times we've searched dex caches for a class. After a certain number of misses we move
582  // the classes into the class_table_ to avoid dex cache based searches.
583  AtomicInteger failed_dex_cache_class_lookups_;
584
585  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
586                                            const mirror::ClassLoader* class_loader,
587                                            size_t hash)
588      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
589
590  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
591      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
592  mirror::Class* LookupClassFromImage(const char* descriptor)
593      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
594
595  // indexes into class_roots_.
596  // needs to be kept in sync with class_roots_descriptors_.
597  enum ClassRoot {
598    kJavaLangClass,
599    kJavaLangObject,
600    kClassArrayClass,
601    kObjectArrayClass,
602    kJavaLangString,
603    kJavaLangDexCache,
604    kJavaLangRefReference,
605    kJavaLangReflectArtField,
606    kJavaLangReflectArtMethod,
607    kJavaLangReflectProxy,
608    kJavaLangStringArrayClass,
609    kJavaLangReflectArtFieldArrayClass,
610    kJavaLangReflectArtMethodArrayClass,
611    kJavaLangClassLoader,
612    kJavaLangThrowable,
613    kJavaLangClassNotFoundException,
614    kJavaLangStackTraceElement,
615    kPrimitiveBoolean,
616    kPrimitiveByte,
617    kPrimitiveChar,
618    kPrimitiveDouble,
619    kPrimitiveFloat,
620    kPrimitiveInt,
621    kPrimitiveLong,
622    kPrimitiveShort,
623    kPrimitiveVoid,
624    kBooleanArrayClass,
625    kByteArrayClass,
626    kCharArrayClass,
627    kDoubleArrayClass,
628    kFloatArrayClass,
629    kIntArrayClass,
630    kLongArrayClass,
631    kShortArrayClass,
632    kJavaLangStackTraceElementArrayClass,
633    kClassRootsMax,
634  };
635  mirror::ObjectArray<mirror::Class>* class_roots_;
636
637  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
638
639  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
640      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
641
642  mirror::ObjectArray<mirror::Class>* GetClassRoots() {
643    DCHECK(class_roots_ != NULL);
644    return class_roots_;
645  }
646
647  static const char* class_roots_descriptors_[];
648
649  const char* GetClassRootDescriptor(ClassRoot class_root) {
650    const char* descriptor = class_roots_descriptors_[class_root];
651    CHECK(descriptor != NULL);
652    return descriptor;
653  }
654
655  // The interface table used by all arrays.
656  mirror::IfTable* array_iftable_;
657
658  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
659  // descriptors for the sake of performing FindClass.
660  static constexpr size_t kFindArrayCacheSize = 16;
661  mirror::Class* find_array_class_cache_[kFindArrayCacheSize];
662  size_t find_array_class_cache_next_victim_;
663
664  bool init_done_;
665  bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
666  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
667
668  InternTable* intern_table_;
669
670  const void* portable_resolution_trampoline_;
671  const void* quick_resolution_trampoline_;
672  const void* portable_imt_conflict_trampoline_;
673  const void* quick_imt_conflict_trampoline_;
674  const void* quick_generic_jni_trampoline_;
675  const void* quick_to_interpreter_bridge_trampoline_;
676
677  friend class ImageWriter;  // for GetClassRoots
678  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
679  FRIEND_TEST(mirror::DexCacheTest, Open);
680  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
681  FRIEND_TEST(ObjectTest, AllocObjectArray);
682  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
683};
684
685}  // namespace art
686
687#endif  // ART_RUNTIME_CLASS_LINKER_H_
688