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