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