class_linker.h revision 94f7b49578b6aaa80de8ffed230648d601393905
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 oat file contains the dex file with the given location and checksum.
278  static bool VerifyOatFileChecksums(const OatFile* oat_file,
279                                     const char* dex_location,
280                                     uint32_t dex_location_checksum,
281                                     InstructionSet instruction_set,
282                                     std::string* error_msg);
283
284  // TODO: replace this with multiple methods that allocate the correct managed type.
285  template <class T>
286  mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
287      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
288
289  mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
290      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
291
292  mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
293      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
294
295  mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
296      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
297
298  mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
299      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
300
301  mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
302      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
303
304  mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
305                                                                              size_t length)
306      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
307
308  void VerifyClass(Handle<mirror::Class> klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
309  bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
310                               mirror::Class::Status& oat_file_class_status)
311      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
312  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
313                                         Handle<mirror::Class> klass)
314      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
315  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
316      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
317
318  mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
319                                  jobjectArray interfaces, jobject loader, jobjectArray methods,
320                                  jobjectArray throws)
321      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
322  std::string GetDescriptorForProxy(mirror::Class* proxy_class)
323      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
324  mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
325                                        mirror::ArtMethod* proxy_method)
326      LOCKS_EXCLUDED(dex_lock_)
327      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
328
329  // Get the oat code for a method when its class isn't yet initialized
330  const void* GetQuickOatCodeFor(mirror::ArtMethod* method)
331      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
332  const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code)
333      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
334
335  // Get the oat code for a method from a method index.
336  const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
337      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
338  const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
339      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
340
341  pid_t GetClassesLockOwner();  // For SignalCatcher.
342  pid_t GetDexLockOwner();  // For SignalCatcher.
343
344  const void* GetPortableResolutionTrampoline() const {
345    return portable_resolution_trampoline_;
346  }
347
348  const void* GetQuickGenericJniTrampoline() const {
349    return quick_generic_jni_trampoline_;
350  }
351
352  const void* GetQuickResolutionTrampoline() const {
353    return quick_resolution_trampoline_;
354  }
355
356  const void* GetPortableImtConflictTrampoline() const {
357    return portable_imt_conflict_trampoline_;
358  }
359
360  const void* GetQuickImtConflictTrampoline() const {
361    return quick_imt_conflict_trampoline_;
362  }
363
364  const void* GetQuickToInterpreterBridgeTrampoline() const {
365    return quick_to_interpreter_bridge_trampoline_;
366  }
367
368  InternTable* GetInternTable() const {
369    return intern_table_;
370  }
371
372  // Attempts to insert a class into a class table.  Returns NULL if
373  // the class was inserted, otherwise returns an existing class with
374  // the same descriptor and ClassLoader.
375  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
376      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
377      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
378
379  // Special code to allocate an art method, use this instead of class->AllocObject.
380  mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
381
382  mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
383    mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
384    DCHECK(class_roots != NULL);
385    return class_roots;
386  }
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  // Precomputes size needed for Class, in the case of a non-temporary class this size must be
431  // sufficient to hold all static fields.
432  uint32_t SizeOfClassWithoutEmbeddedTables(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                 Handle<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                        Handle<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                 Handle<mirror::Class> klass, Handle<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                                Handle<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, Handle<mirror::DexCache> dex_cache)
463      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
464      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
465  bool IsDexFileRegisteredLocked(const DexFile& dex_file)
466      SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
467
468  bool InitializeClass(Handle<mirror::Class> klass, bool can_run_clinit,
469                       bool can_init_parents)
470      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
471  bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
472                              ObjectLock<mirror::Class>& lock);
473  bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
474      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
475
476  bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
477                                                Handle<mirror::ClassLoader> class_loader1,
478                                                Handle<mirror::ClassLoader> class_loader2)
479      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
480
481  bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
482                                                     mirror::Class* klass1,
483                                                     mirror::Class* klass2)
484      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
485
486  bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass,
487                 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
488                 mirror::Class** new_class)
489      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
490
491  bool LinkSuperClass(Handle<mirror::Class> klass)
492      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
493
494  bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
495      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
496
497  bool LinkMethods(Thread* self, Handle<mirror::Class> klass,
498                   Handle<mirror::ObjectArray<mirror::Class>> interfaces)
499      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
500
501  bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
502      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
503
504  bool LinkInterfaceMethods(Handle<mirror::Class> klass,
505                            Handle<mirror::ObjectArray<mirror::Class>> interfaces)
506      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
507
508  bool LinkStaticFields(Handle<mirror::Class> klass, size_t* class_size)
509      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
510  bool LinkInstanceFields(Handle<mirror::Class> klass)
511      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
512  bool LinkFields(Handle<mirror::Class> klass, bool is_static, size_t* class_size)
513      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
514  void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
515                const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index)
516      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
517
518  void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
519      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
520  void CreateReferenceStaticOffsets(Handle<mirror::Class> klass)
521      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
522  void CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static,
523                              uint32_t reference_offsets)
524      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
525
526  // For use by ImageWriter to find DexCaches for its roots
527  ReaderWriterMutex* DexLock()
528      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
529    return &dex_lock_;
530  }
531  size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
532    return dex_caches_.size();
533  }
534  mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
535
536  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
537      LOCKS_EXCLUDED(dex_lock_)
538      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
539
540  // Find an opened oat file that contains dex_location. If oat_location is not nullptr, the file
541  // must have that location, else any oat location is accepted.
542  const OatFile* FindOpenedOatFile(const char* oat_location, const char* dex_location,
543                                   const uint32_t* const dex_location_checksum)
544      LOCKS_EXCLUDED(dex_lock_);
545
546  // Will open the oat file directly without relocating, even if we could/should do relocation.
547  const OatFile* FindOatFileFromOatLocation(const std::string& oat_location,
548                                            std::string* error_msg)
549      LOCKS_EXCLUDED(dex_lock_);
550
551  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
552      LOCKS_EXCLUDED(dex_lock_);
553
554  const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location,
555                                            InstructionSet isa,
556                                            bool* already_opened,
557                                            bool* obsolete_file_cleanup_failed,
558                                            std::vector<std::string>* error_msg)
559      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
560
561  const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output,
562                                     const std::string& image_location, InstructionSet isa,
563                                     std::string* error_msg)
564      LOCKS_EXCLUDED(Locks::mutator_lock_);
565
566  bool CheckOatFile(const OatFile* oat_file, InstructionSet isa,
567                    bool* checksum_verified, std::string* error_msg);
568  int32_t GetRequiredDelta(const OatFile* oat_file, InstructionSet isa);
569
570  // Note: will not register the oat file.
571  const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location,
572                                                    uint32_t dex_location_checksum,
573                                                    const char* oat_location,
574                                                    std::string* error_msg)
575      LOCKS_EXCLUDED(dex_lock_);
576
577  // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for
578  // the file to be written, which is assumed to be under a lock.
579  const OatFile* CreateOatFileForDexLocation(const char* dex_location,
580                                             int fd, const char* oat_location,
581                                             std::vector<std::string>* error_msgs)
582      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
583
584  // Finds an OatFile that contains a DexFile for the given a DexFile location.
585  //
586  // Note 1: this will not check open oat files, which are assumed to be stale when this is run.
587  // Note 2: Does not register the oat file. It is the caller's job to register if the file is to
588  //         be kept.
589  const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* location,
590                                                             const uint32_t* const location_checksum,
591                                                             InstructionSet isa,
592                                                             std::vector<std::string>* error_msgs,
593                                                             bool* obsolete_file_cleanup_failed)
594      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
595
596  // verify an oat file with the given dex file. Will return false when the dex file could not be
597  // verified. Will return true otherwise.
598  bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location,
599                            std::string* error_msg);
600
601  mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
602                                            mirror::Class* proxy_class)
603      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
604  mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
605                                       Handle<mirror::ArtMethod> prototype)
606      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
607
608  std::vector<const DexFile*> boot_class_path_;
609
610  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
611  std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);;
612  std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
613  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
614
615
616  // multimap from a string hash code of a class descriptor to
617  // mirror::Class* instances. Results should be compared for a matching
618  // Class::descriptor_ and Class::class_loader_.
619  typedef std::multimap<size_t, GcRoot<mirror::Class>> Table;
620  // This contains strong roots. To enable concurrent root scanning of
621  // the class table, be careful to use a read barrier when accessing this.
622  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
623  std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_;
624
625  // Do we need to search dex caches to find image classes?
626  bool dex_cache_image_class_lookup_required_;
627  // Number of times we've searched dex caches for a class. After a certain number of misses we move
628  // the classes into the class_table_ to avoid dex cache based searches.
629  AtomicInteger failed_dex_cache_class_lookups_;
630
631  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
632                                            const mirror::ClassLoader* class_loader,
633                                            size_t hash)
634      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
635
636  mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
637      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
638      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
639
640  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
641      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
642  mirror::Class* LookupClassFromImage(const char* descriptor)
643      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
644
645  // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
646  // before returning it to the caller. Its the responsibility of the thread that placed the class
647  // in the table to make it resolved. The thread doing resolution must notify on the class' lock
648  // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
649  // retire a class, the version of the class in the table is returned and this may differ from
650  // the class passed in.
651  mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
652      WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
653
654  void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
655      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
656
657  // indexes into class_roots_.
658  // needs to be kept in sync with class_roots_descriptors_.
659  enum ClassRoot {
660    kJavaLangClass,
661    kJavaLangObject,
662    kClassArrayClass,
663    kObjectArrayClass,
664    kJavaLangString,
665    kJavaLangDexCache,
666    kJavaLangRefReference,
667    kJavaLangReflectArtField,
668    kJavaLangReflectArtMethod,
669    kJavaLangReflectProxy,
670    kJavaLangStringArrayClass,
671    kJavaLangReflectArtFieldArrayClass,
672    kJavaLangReflectArtMethodArrayClass,
673    kJavaLangClassLoader,
674    kJavaLangThrowable,
675    kJavaLangClassNotFoundException,
676    kJavaLangStackTraceElement,
677    kPrimitiveBoolean,
678    kPrimitiveByte,
679    kPrimitiveChar,
680    kPrimitiveDouble,
681    kPrimitiveFloat,
682    kPrimitiveInt,
683    kPrimitiveLong,
684    kPrimitiveShort,
685    kPrimitiveVoid,
686    kBooleanArrayClass,
687    kByteArrayClass,
688    kCharArrayClass,
689    kDoubleArrayClass,
690    kFloatArrayClass,
691    kIntArrayClass,
692    kLongArrayClass,
693    kShortArrayClass,
694    kJavaLangStackTraceElementArrayClass,
695    kClassRootsMax,
696  };
697  GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
698
699  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
700
701  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
702      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
703
704  static const char* class_roots_descriptors_[];
705
706  const char* GetClassRootDescriptor(ClassRoot class_root) {
707    const char* descriptor = class_roots_descriptors_[class_root];
708    CHECK(descriptor != NULL);
709    return descriptor;
710  }
711
712  // The interface table used by all arrays.
713  GcRoot<mirror::IfTable> array_iftable_;
714
715  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
716  // descriptors for the sake of performing FindClass.
717  static constexpr size_t kFindArrayCacheSize = 16;
718  GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
719  size_t find_array_class_cache_next_victim_;
720
721  bool init_done_;
722  bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
723  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
724
725  InternTable* intern_table_;
726
727  // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
728  // patch point within the image. TODO: make these proper relocations.
729  const void* portable_resolution_trampoline_;
730  const void* quick_resolution_trampoline_;
731  const void* portable_imt_conflict_trampoline_;
732  const void* quick_imt_conflict_trampoline_;
733  const void* quick_generic_jni_trampoline_;
734  const void* quick_to_interpreter_bridge_trampoline_;
735
736  friend class ImageWriter;  // for GetClassRoots
737  friend class ImageDumper;  // for FindOpenedOatFileFromOatLocation
738  friend class ElfPatcher;  // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation
739  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
740  FRIEND_TEST(mirror::DexCacheTest, Open);
741  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
742  FRIEND_TEST(ObjectTest, AllocObjectArray);
743  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
744};
745
746}  // namespace art
747
748#endif  // ART_RUNTIME_CLASS_LINKER_H_
749