class_linker.h revision ef7d42fca18c16fbaf103822ad16f23246e2905d
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, 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, mirror::Class* referrer)
137      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
138
139  // Resolve a Type with the given index from the DexFile, storing the
140  // result in the DexCache. The referrer is used to identify the
141  // target DexCache and ClassLoader to use for resolution.
142  mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer)
143      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144
145  mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtField* referrer)
146      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147
148  // Resolve a type with the given ID from the DexFile, storing the
149  // result in DexCache. The ClassLoader is used to search for the
150  // type, since it may be referenced from but not contained within
151  // the given DexFile.
152  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
153                             const SirtRef<mirror::DexCache>& dex_cache,
154                             const SirtRef<mirror::ClassLoader>& class_loader)
155      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156
157  // Resolve a method with a given ID from the DexFile, storing the
158  // result in DexCache. The ClassLinker and ClassLoader are used as
159  // in ResolveType. What is unique is the method type argument which
160  // is used to determine if this method is a direct, static, or
161  // virtual method.
162  mirror::ArtMethod* ResolveMethod(const DexFile& dex_file,
163                                   uint32_t method_idx,
164                                   const SirtRef<mirror::DexCache>& dex_cache,
165                                   const SirtRef<mirror::ClassLoader>& class_loader,
166                                   mirror::ArtMethod* referrer,
167                                   InvokeType type)
168      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
169
170  mirror::ArtMethod* ResolveMethod(uint32_t method_idx, mirror::ArtMethod* referrer,
171                                   InvokeType type)
172      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
173
174  mirror::ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
175                                 bool is_static)
176      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
177
178  // Resolve a field with a given ID from the DexFile, storing the
179  // result in DexCache. The ClassLinker and ClassLoader are used as
180  // in ResolveType. What is unique is the is_static argument which is
181  // used to determine if we are resolving a static or non-static
182  // field.
183  mirror::ArtField* ResolveField(const DexFile& dex_file,
184                                 uint32_t field_idx,
185                                 const SirtRef<mirror::DexCache>& dex_cache,
186                                 const SirtRef<mirror::ClassLoader>& class_loader,
187                                 bool is_static)
188      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
189
190  // Resolve a field with a given ID from the DexFile, storing the
191  // result in DexCache. The ClassLinker and ClassLoader are used as
192  // in ResolveType. No is_static argument is provided so that Java
193  // field resolution semantics are followed.
194  mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file,
195                                    uint32_t field_idx,
196                                    const SirtRef<mirror::DexCache>& dex_cache,
197                                    const SirtRef<mirror::ClassLoader>& class_loader)
198      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
199
200  // Get shorty from method index without resolution. Used to do handlerization.
201  const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length)
202      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
203
204  // Returns true on success, false if there's an exception pending.
205  // can_run_clinit=false allows the compiler to attempt to init a class,
206  // given the restriction that no <clinit> execution is possible.
207  bool EnsureInitialized(const SirtRef<mirror::Class>& c,
208                         bool can_init_fields, bool can_init_parents)
209      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
210
211  // Initializes classes that have instances in the image but that have
212  // <clinit> methods so they could not be initialized by the compiler.
213  void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
214
215  void RegisterDexFile(const DexFile& dex_file)
216      LOCKS_EXCLUDED(dex_lock_)
217      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
218  void RegisterDexFile(const DexFile& dex_file, const SirtRef<mirror::DexCache>& dex_cache)
219      LOCKS_EXCLUDED(dex_lock_)
220      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
221
222  const OatFile* RegisterOatFile(const OatFile* oat_file)
223      LOCKS_EXCLUDED(dex_lock_);
224
225  const std::vector<const DexFile*>& GetBootClassPath() {
226    return boot_class_path_;
227  }
228
229  void VisitClasses(ClassVisitor* visitor, void* arg)
230      LOCKS_EXCLUDED(dex_lock_)
231      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
232  // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_
233  // when calling the visitor.
234  void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg)
235      LOCKS_EXCLUDED(dex_lock_)
236      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
237
238  void VisitRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty)
239      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_, dex_lock_);
240
241  mirror::DexCache* FindDexCache(const DexFile& dex_file) const
242      LOCKS_EXCLUDED(dex_lock_)
243      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
244  bool IsDexFileRegistered(const DexFile& dex_file) const
245      LOCKS_EXCLUDED(dex_lock_);
246  void FixupDexCaches(mirror::ArtMethod* resolution_method) const
247      LOCKS_EXCLUDED(dex_lock_)
248      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
249
250  // Generate an oat file from a dex file
251  bool GenerateOatFile(const char* dex_filename,
252                       int oat_fd,
253                       const char* oat_cache_filename,
254                       std::string* error_msg);
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                                                     const uint32_t* const 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(mirror::Class* proxy_class)
322      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
323  mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
324                                        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* GetQuickOatCodeFor(mirror::ArtMethod* method)
330      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
331  const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code)
332      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
333
334  // Get the oat code for a method from a method index.
335  const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
336      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
337  const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
338      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
339
340  pid_t GetClassesLockOwner();  // For SignalCatcher.
341  pid_t GetDexLockOwner();  // For SignalCatcher.
342
343  const void* GetPortableResolutionTrampoline() const {
344    return portable_resolution_trampoline_;
345  }
346
347  const void* GetQuickResolutionTrampoline() const {
348    return quick_resolution_trampoline_;
349  }
350
351  const void* GetPortableImtConflictTrampoline() const {
352    return portable_imt_conflict_trampoline_;
353  }
354
355  const void* GetQuickImtConflictTrampoline() const {
356    return quick_imt_conflict_trampoline_;
357  }
358
359  InternTable* GetInternTable() const {
360    return intern_table_;
361  }
362
363  // Attempts to insert a class into a class table.  Returns NULL if
364  // the class was inserted, otherwise returns an existing class with
365  // the same descriptor and ClassLoader.
366  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
367      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
368      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
369
370  // Special code to allocate an art method, use this instead of class->AllocObject.
371  mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
372
373 private:
374  const OatFile::OatMethod GetOatMethodFor(mirror::ArtMethod* method)
375      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
376
377  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
378      LOCKS_EXCLUDED(dex_lock_)
379      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
380
381  void FinishInit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
382
383  // For early bootstrapping by Init
384  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, size_t class_size)
385      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
386
387  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
388  // values that are known to the ClassLinker such as
389  // kObjectArrayClass and kJavaLangString etc.
390  mirror::Class* AllocClass(Thread* self, size_t class_size)
391      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
392  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
393      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
394  mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
395
396  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
397      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
398  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
399      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
400
401
402  mirror::Class* CreateArrayClass(const char* descriptor,
403                                  const SirtRef<mirror::ClassLoader>& class_loader)
404      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
405
406  void AppendToBootClassPath(const DexFile& dex_file)
407      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
408  void AppendToBootClassPath(const DexFile& dex_file, const SirtRef<mirror::DexCache>& dex_cache)
409      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
410
411  void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
412                         mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
413      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
414
415  size_t SizeOfClass(const DexFile& dex_file,
416                     const DexFile::ClassDef& dex_class_def);
417
418  void LoadClass(const DexFile& dex_file,
419                 const DexFile::ClassDef& dex_class_def,
420                 const SirtRef<mirror::Class>& klass,
421                 mirror::ClassLoader* class_loader)
422      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
423
424  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
425                 const SirtRef<mirror::Class>& klass, const SirtRef<mirror::ArtField>& dst)
426      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
427
428  mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
429                                const ClassDataItemIterator& dex_method,
430                                const SirtRef<mirror::Class>& klass)
431      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
432
433  void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
434
435  // Finds the associated oat class for a dex_file and descriptor
436  const OatFile::OatClass* GetOatClass(const DexFile& dex_file, uint16_t class_def_idx)
437      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
438
439  void RegisterDexFileLocked(const DexFile& dex_file, const SirtRef<mirror::DexCache>& dex_cache)
440      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
441      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
442  bool IsDexFileRegisteredLocked(const DexFile& dex_file) const SHARED_LOCKS_REQUIRED(dex_lock_);
443
444  bool InitializeClass(const SirtRef<mirror::Class>& klass, bool can_run_clinit,
445                       bool can_init_parents)
446      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
447  bool WaitForInitializeClass(const SirtRef<mirror::Class>& klass, Thread* self,
448                              ObjectLock<mirror::Class>& lock);
449  bool ValidateSuperClassDescriptors(const SirtRef<mirror::Class>& klass)
450      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
451
452  bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
453                                                SirtRef<mirror::ClassLoader>& class_loader1,
454                                                SirtRef<mirror::ClassLoader>& class_loader2)
455      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
456
457  bool IsSameMethodSignatureInDifferentClassContexts(mirror::ArtMethod* method,
458                                                     mirror::Class* klass1,
459                                                     mirror::Class* klass2)
460      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
461
462  bool LinkClass(Thread* self, const SirtRef<mirror::Class>& klass,
463                 const SirtRef<mirror::ObjectArray<mirror::Class> >& interfaces)
464      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
465
466  bool LinkSuperClass(const SirtRef<mirror::Class>& klass)
467      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
468
469  bool LoadSuperAndInterfaces(const SirtRef<mirror::Class>& klass, const DexFile& dex_file)
470      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
471
472  bool LinkMethods(const SirtRef<mirror::Class>& klass,
473                   const SirtRef<mirror::ObjectArray<mirror::Class> >& interfaces)
474      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
475
476  bool LinkVirtualMethods(const SirtRef<mirror::Class>& klass)
477      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
478
479  bool LinkInterfaceMethods(const SirtRef<mirror::Class>& klass,
480                            const SirtRef<mirror::ObjectArray<mirror::Class> >& interfaces)
481      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
482
483  bool LinkStaticFields(const SirtRef<mirror::Class>& klass)
484      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
485  bool LinkInstanceFields(const SirtRef<mirror::Class>& klass)
486      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
487  bool LinkFields(const SirtRef<mirror::Class>& klass, bool is_static)
488      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
489
490
491  void CreateReferenceInstanceOffsets(const SirtRef<mirror::Class>& klass)
492      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
493  void CreateReferenceStaticOffsets(const SirtRef<mirror::Class>& klass)
494      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
495  void CreateReferenceOffsets(const SirtRef<mirror::Class>& klass, bool is_static,
496                              uint32_t reference_offsets)
497      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
498
499  // For use by ImageWriter to find DexCaches for its roots
500  const std::vector<mirror::DexCache*>& GetDexCaches() {
501    return dex_caches_;
502  }
503
504  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
505      LOCKS_EXCLUDED(dex_lock_)
506      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
507  const OatFile* FindOpenedOatFileFromDexLocation(const char* dex_location,
508                                                  const uint32_t* const dex_location_checksum)
509      LOCKS_EXCLUDED(dex_lock);
510  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
511      LOCKS_EXCLUDED(dex_lock_);
512  const DexFile* FindDexFileInOatLocation(const char* dex_location,
513                                          uint32_t dex_location_checksum,
514                                          const char* oat_location,
515                                          std::string* error_msg)
516      LOCKS_EXCLUDED(dex_lock_);
517
518  const DexFile* VerifyAndOpenDexFileFromOatFile(const std::string& oat_file_location,
519                                                 const char* dex_location,
520                                                 std::string* error_msg,
521                                                 bool* open_failed)
522      LOCKS_EXCLUDED(dex_lock_);
523
524  mirror::ArtMethod* CreateProxyConstructor(Thread* self, const SirtRef<mirror::Class>& klass,
525                                            mirror::Class* proxy_class)
526      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
527  mirror::ArtMethod* CreateProxyMethod(Thread* self, const SirtRef<mirror::Class>& klass,
528                                       const SirtRef<mirror::ArtMethod>& prototype)
529      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
530
531  std::vector<const DexFile*> boot_class_path_;
532
533  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
534  std::vector<mirror::DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
535  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
536
537
538  // multimap from a string hash code of a class descriptor to
539  // mirror::Class* instances. Results should be compared for a matching
540  // Class::descriptor_ and Class::class_loader_.
541  typedef std::multimap<size_t, mirror::Class*> Table;
542  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
543
544  // Do we need to search dex caches to find image classes?
545  bool dex_cache_image_class_lookup_required_;
546  // Number of times we've searched dex caches for a class. After a certain number of misses we move
547  // the classes into the class_table_ to avoid dex cache based searches.
548  AtomicInteger failed_dex_cache_class_lookups_;
549
550  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
551                                            const mirror::ClassLoader* class_loader,
552                                            size_t hash)
553      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
554
555  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
556      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
557  mirror::Class* LookupClassFromImage(const char* descriptor)
558      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
559
560  // indexes into class_roots_.
561  // needs to be kept in sync with class_roots_descriptors_.
562  enum ClassRoot {
563    kJavaLangClass,
564    kJavaLangObject,
565    kClassArrayClass,
566    kObjectArrayClass,
567    kJavaLangString,
568    kJavaLangDexCache,
569    kJavaLangRefReference,
570    kJavaLangReflectArtField,
571    kJavaLangReflectArtMethod,
572    kJavaLangReflectProxy,
573    kJavaLangStringArrayClass,
574    kJavaLangReflectArtFieldArrayClass,
575    kJavaLangReflectArtMethodArrayClass,
576    kJavaLangClassLoader,
577    kJavaLangThrowable,
578    kJavaLangClassNotFoundException,
579    kJavaLangStackTraceElement,
580    kPrimitiveBoolean,
581    kPrimitiveByte,
582    kPrimitiveChar,
583    kPrimitiveDouble,
584    kPrimitiveFloat,
585    kPrimitiveInt,
586    kPrimitiveLong,
587    kPrimitiveShort,
588    kPrimitiveVoid,
589    kBooleanArrayClass,
590    kByteArrayClass,
591    kCharArrayClass,
592    kDoubleArrayClass,
593    kFloatArrayClass,
594    kIntArrayClass,
595    kLongArrayClass,
596    kShortArrayClass,
597    kJavaLangStackTraceElementArrayClass,
598    kClassRootsMax,
599  };
600  mirror::ObjectArray<mirror::Class>* class_roots_;
601
602  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
603
604  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
605      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
606
607  mirror::ObjectArray<mirror::Class>* GetClassRoots() {
608    DCHECK(class_roots_ != NULL);
609    return class_roots_;
610  }
611
612  static const char* class_roots_descriptors_[];
613
614  const char* GetClassRootDescriptor(ClassRoot class_root) {
615    const char* descriptor = class_roots_descriptors_[class_root];
616    CHECK(descriptor != NULL);
617    return descriptor;
618  }
619
620  mirror::IfTable* array_iftable_;
621
622  bool init_done_;
623  bool dex_caches_dirty_ GUARDED_BY(dex_lock_);
624  bool class_table_dirty_ GUARDED_BY(Locks::classlinker_classes_lock_);
625
626  InternTable* intern_table_;
627
628  const void* portable_resolution_trampoline_;
629  const void* quick_resolution_trampoline_;
630  const void* portable_imt_conflict_trampoline_;
631  const void* quick_imt_conflict_trampoline_;
632
633  friend class ImageWriter;  // for GetClassRoots
634  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
635  FRIEND_TEST(mirror::DexCacheTest, Open);
636  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
637  FRIEND_TEST(ObjectTest, AllocObjectArray);
638  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
639};
640
641}  // namespace art
642
643#endif  // ART_RUNTIME_CLASS_LINKER_H_
644