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