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