class_linker.h revision 345c4b19758703793ed31024cfb79940e2c63b75
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 "gc_root.h"
28#include "gtest/gtest.h"
29#include "jni.h"
30#include "oat_file.h"
31#include "object_callbacks.h"
32
33namespace art {
34
35namespace gc {
36namespace space {
37  class ImageSpace;
38}  // namespace space
39}  // namespace gc
40namespace mirror {
41  class ClassLoader;
42  class DexCache;
43  class DexCacheTest_Open_Test;
44  class IfTable;
45  template<class T> class ObjectArray;
46  class StackTraceElement;
47}  // namespace mirror
48
49class InternTable;
50template<class T> class ObjectLock;
51class ScopedObjectAccessAlreadyRunnable;
52template<class T> class Handle;
53
54typedef bool (ClassVisitor)(mirror::Class* c, void* arg);
55
56enum VisitRootFlags : uint8_t;
57
58class ClassLinker {
59 public:
60  explicit ClassLinker(InternTable* intern_table);
61  ~ClassLinker();
62
63  // Initialize class linker by bootstraping from dex files
64  void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
65      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
66
67  // Initialize class linker from one or more images.
68  void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
69
70  bool IsInBootClassPath(const char* descriptor);
71
72  // Finds a class by its descriptor, loading it if necessary.
73  // If class_loader is null, searches boot_class_path_.
74  mirror::Class* FindClass(Thread* self, const char* descriptor,
75                           Handle<mirror::ClassLoader> class_loader)
76      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
77
78  // Finds a class by its descriptor using the "system" class loader, ie by searching the
79  // boot_class_path_.
80  mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
81      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82
83  // Finds the array class given for the element class.
84  mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
85      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
87  // Reutrns true if the class linker is initialized.
88  bool IsInitialized() const;
89
90  // Define a new a class based on a ClassDef from a DexFile
91  mirror::Class* DefineClass(const char* descriptor,
92                             Handle<mirror::ClassLoader> class_loader,
93                             const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
94      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
95
96  // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
97  // by the given 'class_loader'.
98  mirror::Class* LookupClass(const char* descriptor, const mirror::ClassLoader* class_loader)
99      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
100      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101
102  // Finds all the classes with the given descriptor, regardless of ClassLoader.
103  void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
104      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
105      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106
107  mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108
109  // General class unloading is not supported, this is used to prune
110  // unwanted classes during image writing.
111  bool RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader)
112      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
113      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114
115  void DumpAllClasses(int flags)
116      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
117      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
118
119  void DumpForSigQuit(std::ostream& os)
120      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
121      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
123  size_t NumLoadedClasses()
124      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
125      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126
127  // Resolve a String with the given index from the DexFile, storing the
128  // result in the DexCache. The referrer is used to identify the
129  // target DexCache and ClassLoader to use for resolution.
130  mirror::String* ResolveString(uint32_t string_idx, mirror::ArtMethod* referrer)
131      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132
133  // Resolve a String with the given index from the DexFile, storing the
134  // result in the DexCache.
135  mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
136                                Handle<mirror::DexCache> dex_cache)
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 identity the
141  // target DexCache and ClassLoader to use for resolution.
142  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
143      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144
145  // Resolve a Type with the given index from the DexFile, storing the
146  // result in the DexCache. The referrer is used to identify the
147  // target DexCache and ClassLoader to use for resolution.
148  mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer)
149      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
150
151  mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtField* referrer)
152      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
153
154  // Resolve a type with the given ID from the DexFile, storing the
155  // result in DexCache. The ClassLoader is used to search for the
156  // type, since it may be referenced from but not contained within
157  // the given DexFile.
158  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
159                             Handle<mirror::DexCache> dex_cache,
160                             Handle<mirror::ClassLoader> class_loader)
161      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
162
163  // Resolve a method with a given ID from the DexFile, storing the
164  // result in DexCache. The ClassLinker and ClassLoader are used as
165  // in ResolveType. What is unique is the method type argument which
166  // is used to determine if this method is a direct, static, or
167  // virtual method.
168  mirror::ArtMethod* ResolveMethod(const DexFile& dex_file,
169                                   uint32_t method_idx,
170                                   Handle<mirror::DexCache> dex_cache,
171                                   Handle<mirror::ClassLoader> class_loader,
172                                   Handle<mirror::ArtMethod> referrer,
173                                   InvokeType type)
174      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
175
176  mirror::ArtMethod* GetResolvedMethod(uint32_t method_idx, mirror::ArtMethod* referrer,
177                                       InvokeType type)
178      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179  mirror::ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, mirror::ArtMethod** referrer,
180                                   InvokeType type)
181      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
182
183  mirror::ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
184      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
185  mirror::ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
186                                 bool is_static)
187      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
188
189  // Resolve a field with a given ID from the DexFile, storing the
190  // result in DexCache. The ClassLinker and ClassLoader are used as
191  // in ResolveType. What is unique is the is_static argument which is
192  // used to determine if we are resolving a static or non-static
193  // field.
194  mirror::ArtField* ResolveField(const DexFile& dex_file,
195                                 uint32_t field_idx,
196                                 Handle<mirror::DexCache> dex_cache,
197                                 Handle<mirror::ClassLoader> class_loader,
198                                 bool is_static)
199      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
200
201  // Resolve a field with a given ID from the DexFile, storing the
202  // result in DexCache. The ClassLinker and ClassLoader are used as
203  // in ResolveType. No is_static argument is provided so that Java
204  // field resolution semantics are followed.
205  mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file, uint32_t field_idx,
206                                    Handle<mirror::DexCache> dex_cache,
207                                    Handle<mirror::ClassLoader> class_loader)
208      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
209
210  // Get shorty from method index without resolution. Used to do handlerization.
211  const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length)
212      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
213
214  // Returns true on success, false if there's an exception pending.
215  // can_run_clinit=false allows the compiler to attempt to init a class,
216  // given the restriction that no <clinit> execution is possible.
217  bool EnsureInitialized(Handle<mirror::Class> c, 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, Handle<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 VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
248      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
249      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
250  void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
251      LOCKS_EXCLUDED(dex_lock_)
252      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
253
254  mirror::DexCache* FindDexCache(const DexFile& dex_file)
255      LOCKS_EXCLUDED(dex_lock_)
256      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
257  bool IsDexFileRegistered(const DexFile& dex_file)
258      LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
259  void FixupDexCaches(mirror::ArtMethod* resolution_method)
260      LOCKS_EXCLUDED(dex_lock_)
261      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
262
263  // Generate an oat file from a dex file
264  bool GenerateOatFile(const char* dex_filename,
265                       int oat_fd,
266                       const char* oat_cache_filename,
267                       std::string* error_msg)
268      LOCKS_EXCLUDED(Locks::mutator_lock_);
269
270  // Find or create the oat file holding dex_location. Then load all corresponding dex files
271  // (if multidex) into the given vector.
272  bool OpenDexFilesFromOat(const char* dex_location, const char* oat_location,
273                           std::vector<std::string>* error_msgs,
274                           std::vector<const DexFile*>* dex_files)
275      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
276
277  // Returns true if the given oat file has the same image checksum as the image it is paired with.
278  static bool VerifyOatImageChecksum(const OatFile* oat_file, const InstructionSet instruction_set);
279  // Returns true if the oat file checksums match with the image and the offsets are such that it
280  // could be loaded with it.
281  static bool VerifyOatChecksums(const OatFile* oat_file, const InstructionSet instruction_set,
282                                 std::string* error_msg);
283  // Returns true if oat file contains the dex file with the given location and checksum.
284  static bool VerifyOatAndDexFileChecksums(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  mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
389    mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
390    DCHECK(class_roots != NULL);
391    return class_roots;
392  }
393
394 private:
395  const OatFile::OatMethod GetOatMethodFor(mirror::ArtMethod* method)
396      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
397
398  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
399      LOCKS_EXCLUDED(dex_lock_)
400      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
401
402  void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
403
404  // For early bootstrapping by Init
405  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
406      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
407
408  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
409  // values that are known to the ClassLinker such as
410  // kObjectArrayClass and kJavaLangString etc.
411  mirror::Class* AllocClass(Thread* self, uint32_t class_size)
412      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
413  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
414      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
415  mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
416
417  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
418      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
419  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
420      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
421
422
423  mirror::Class* CreateArrayClass(Thread* self, const char* descriptor,
424                                  Handle<mirror::ClassLoader> class_loader)
425      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
426
427  void AppendToBootClassPath(const DexFile& dex_file)
428      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
429  void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
430      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
431
432  void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
433                         mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
434      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
435
436  // Precomputes size needed for Class, in the case of a non-temporary class this size must be
437  // sufficient to hold all static fields.
438  uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
439                                            const DexFile::ClassDef& dex_class_def);
440
441  void LoadClass(const DexFile& dex_file,
442                 const DexFile::ClassDef& dex_class_def,
443                 Handle<mirror::Class> klass,
444                 mirror::ClassLoader* class_loader)
445      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
446  void LoadClassMembers(const DexFile& dex_file,
447                        const byte* class_data,
448                        Handle<mirror::Class> klass,
449                        mirror::ClassLoader* class_loader,
450                        const OatFile::OatClass* oat_class)
451      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
452
453  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
454                 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst)
455      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
456
457  mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
458                                const ClassDataItemIterator& dex_method,
459                                Handle<mirror::Class> klass)
460      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
461
462  void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
463
464  // Finds the associated oat class for a dex_file and descriptor
465  OatFile::OatClass GetOatClass(const DexFile& dex_file, uint16_t class_def_idx)
466      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
467
468  void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
469      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
470      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
471  bool IsDexFileRegisteredLocked(const DexFile& dex_file)
472      SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
473
474  bool InitializeClass(Handle<mirror::Class> klass, bool can_run_clinit,
475                       bool can_init_parents)
476      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
477  bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
478                              ObjectLock<mirror::Class>& lock);
479  bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
480      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
481
482  bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
483                                                Handle<mirror::ClassLoader> class_loader1,
484                                                Handle<mirror::ClassLoader> class_loader2)
485      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
486
487  bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
488                                                     mirror::Class* klass1,
489                                                     mirror::Class* klass2)
490      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
491
492  bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass,
493                 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
494                 mirror::Class** new_class)
495      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
496
497  bool LinkSuperClass(Handle<mirror::Class> klass)
498      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
499
500  bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
501      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
502
503  bool LinkMethods(Thread* self, Handle<mirror::Class> klass,
504                   Handle<mirror::ObjectArray<mirror::Class>> interfaces)
505      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
506
507  bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
508      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
509
510  bool LinkInterfaceMethods(Handle<mirror::Class> klass,
511                            Handle<mirror::ObjectArray<mirror::Class>> interfaces)
512      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
513
514  bool LinkStaticFields(Handle<mirror::Class> klass, size_t* class_size)
515      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
516  bool LinkInstanceFields(Handle<mirror::Class> klass)
517      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
518  bool LinkFields(Handle<mirror::Class> klass, bool is_static, size_t* class_size)
519      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
520  void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
521                const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index)
522      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
523
524  void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
525      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
526  void CreateReferenceStaticOffsets(Handle<mirror::Class> klass)
527      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
528  void CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static,
529                              uint32_t reference_offsets)
530      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
531
532  // For use by ImageWriter to find DexCaches for its roots
533  ReaderWriterMutex* DexLock()
534      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
535    return &dex_lock_;
536  }
537  size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
538    return dex_caches_.size();
539  }
540  mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
541
542  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
543      LOCKS_EXCLUDED(dex_lock_)
544      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
545
546  // Find an opened oat file that contains dex_location. If oat_location is not nullptr, the file
547  // must have that location, else any oat location is accepted.
548  const OatFile* FindOpenedOatFile(const char* oat_location, const char* dex_location,
549                                   const uint32_t* const dex_location_checksum)
550      LOCKS_EXCLUDED(dex_lock_);
551
552  // Will open the oat file directly without relocating, even if we could/should do relocation.
553  const OatFile* FindOatFileFromOatLocation(const std::string& oat_location,
554                                            std::string* error_msg)
555      LOCKS_EXCLUDED(dex_lock_);
556
557  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
558      LOCKS_EXCLUDED(dex_lock_);
559
560  const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location,
561                                            InstructionSet isa,
562                                            bool* already_opened,
563                                            bool* obsolete_file_cleanup_failed,
564                                            std::vector<std::string>* error_msg)
565      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
566
567  const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output,
568                                     const std::string& image_location, InstructionSet isa,
569                                     std::string* error_msg)
570      LOCKS_EXCLUDED(Locks::mutator_lock_);
571
572  bool CheckOatFile(const OatFile* oat_file, InstructionSet isa,
573                    bool* checksum_verified, std::string* error_msg);
574  int32_t GetRequiredDelta(const OatFile* oat_file, InstructionSet isa);
575
576  // Note: will not register the oat file.
577  const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location,
578                                                    uint32_t dex_location_checksum,
579                                                    const char* oat_location,
580                                                    std::string* error_msg)
581      LOCKS_EXCLUDED(dex_lock_);
582
583  // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for
584  // the file to be written, which is assumed to be under a lock.
585  const OatFile* CreateOatFileForDexLocation(const char* dex_location,
586                                             int fd, const char* oat_location,
587                                             std::vector<std::string>* error_msgs)
588      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
589
590  // Finds an OatFile that contains a DexFile for the given a DexFile location.
591  //
592  // Note 1: this will not check open oat files, which are assumed to be stale when this is run.
593  // Note 2: Does not register the oat file. It is the caller's job to register if the file is to
594  //         be kept.
595  const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* location,
596                                                             const uint32_t* const location_checksum,
597                                                             InstructionSet isa,
598                                                             std::vector<std::string>* error_msgs,
599                                                             bool* obsolete_file_cleanup_failed)
600      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
601
602  // verify an oat file with the given dex file. Will return false when the dex file could not be
603  // verified. Will return true otherwise.
604  bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location,
605                            std::string* error_msg);
606
607  mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
608                                            mirror::Class* proxy_class)
609      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
610  mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
611                                       Handle<mirror::ArtMethod> prototype)
612      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
613
614  std::vector<const DexFile*> boot_class_path_;
615
616  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
617  std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);;
618  std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
619  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
620
621
622  // multimap from a string hash code of a class descriptor to
623  // mirror::Class* instances. Results should be compared for a matching
624  // Class::descriptor_ and Class::class_loader_.
625  typedef std::multimap<size_t, GcRoot<mirror::Class>> Table;
626  // This contains strong roots. To enable concurrent root scanning of
627  // the class table, be careful to use a read barrier when accessing this.
628  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
629  std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_;
630
631  // Do we need to search dex caches to find image classes?
632  bool dex_cache_image_class_lookup_required_;
633  // Number of times we've searched dex caches for a class. After a certain number of misses we move
634  // the classes into the class_table_ to avoid dex cache based searches.
635  AtomicInteger failed_dex_cache_class_lookups_;
636
637  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
638                                            const mirror::ClassLoader* class_loader,
639                                            size_t hash)
640      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
641
642  mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
643      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
644      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
645
646  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
647      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
648  mirror::Class* LookupClassFromImage(const char* descriptor)
649      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
650
651  // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
652  // before returning it to the caller. Its the responsibility of the thread that placed the class
653  // in the table to make it resolved. The thread doing resolution must notify on the class' lock
654  // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
655  // retire a class, the version of the class in the table is returned and this may differ from
656  // the class passed in.
657  mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
658      WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
659
660  void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
661      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
662
663  // indexes into class_roots_.
664  // needs to be kept in sync with class_roots_descriptors_.
665  enum ClassRoot {
666    kJavaLangClass,
667    kJavaLangObject,
668    kClassArrayClass,
669    kObjectArrayClass,
670    kJavaLangString,
671    kJavaLangDexCache,
672    kJavaLangRefReference,
673    kJavaLangReflectArtField,
674    kJavaLangReflectArtMethod,
675    kJavaLangReflectProxy,
676    kJavaLangStringArrayClass,
677    kJavaLangReflectArtFieldArrayClass,
678    kJavaLangReflectArtMethodArrayClass,
679    kJavaLangClassLoader,
680    kJavaLangThrowable,
681    kJavaLangClassNotFoundException,
682    kJavaLangStackTraceElement,
683    kPrimitiveBoolean,
684    kPrimitiveByte,
685    kPrimitiveChar,
686    kPrimitiveDouble,
687    kPrimitiveFloat,
688    kPrimitiveInt,
689    kPrimitiveLong,
690    kPrimitiveShort,
691    kPrimitiveVoid,
692    kBooleanArrayClass,
693    kByteArrayClass,
694    kCharArrayClass,
695    kDoubleArrayClass,
696    kFloatArrayClass,
697    kIntArrayClass,
698    kLongArrayClass,
699    kShortArrayClass,
700    kJavaLangStackTraceElementArrayClass,
701    kClassRootsMax,
702  };
703  GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
704
705  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
706
707  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
708      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
709
710  static const char* class_roots_descriptors_[];
711
712  const char* GetClassRootDescriptor(ClassRoot class_root) {
713    const char* descriptor = class_roots_descriptors_[class_root];
714    CHECK(descriptor != NULL);
715    return descriptor;
716  }
717
718  // The interface table used by all arrays.
719  GcRoot<mirror::IfTable> array_iftable_;
720
721  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
722  // descriptors for the sake of performing FindClass.
723  static constexpr size_t kFindArrayCacheSize = 16;
724  GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
725  size_t find_array_class_cache_next_victim_;
726
727  bool init_done_;
728  bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
729  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
730
731  InternTable* intern_table_;
732
733  // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
734  // patch point within the image. TODO: make these proper relocations.
735  const void* portable_resolution_trampoline_;
736  const void* quick_resolution_trampoline_;
737  const void* portable_imt_conflict_trampoline_;
738  const void* quick_imt_conflict_trampoline_;
739  const void* quick_generic_jni_trampoline_;
740  const void* quick_to_interpreter_bridge_trampoline_;
741
742  friend class ImageWriter;  // for GetClassRoots
743  friend class ImageDumper;  // for FindOpenedOatFileFromOatLocation
744  friend class ElfPatcher;  // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation
745  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
746  FRIEND_TEST(mirror::DexCacheTest, Open);
747  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
748  FRIEND_TEST(ObjectTest, AllocObjectArray);
749  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
750};
751
752}  // namespace art
753
754#endif  // ART_RUNTIME_CLASS_LINKER_H_
755