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