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