class_linker.h revision 39c86bc9de106e3641ecab2374a24e41d0430694
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 {
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  void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
250      LOCKS_EXCLUDED(dex_lock_);
251
252  mirror::DexCache* FindDexCache(const DexFile& dex_file)
253      LOCKS_EXCLUDED(dex_lock_)
254      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
255  bool IsDexFileRegistered(const DexFile& dex_file)
256      LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
257  void FixupDexCaches(mirror::ArtMethod* resolution_method)
258      LOCKS_EXCLUDED(dex_lock_)
259      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
260
261  // Generate an oat file from a dex file
262  bool GenerateOatFile(const char* dex_filename,
263                       int oat_fd,
264                       const char* oat_cache_filename,
265                       std::string* error_msg)
266      LOCKS_EXCLUDED(Locks::mutator_lock_);
267
268  const OatFile* FindOatFileFromOatLocation(const std::string& location,
269                                            std::string* error_msg)
270      LOCKS_EXCLUDED(dex_lock_);
271
272  // Find or create the oat file holding dex_location. Then load all corresponding dex files
273  // (if multidex) into the given vector.
274  bool OpenDexFilesFromOat(const char* dex_location, const char* oat_location,
275                           std::vector<std::string>* error_msgs,
276                           std::vector<const DexFile*>* dex_files)
277      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
278
279  // Returns true if oat file contains the dex file with the given location and checksum.
280  static bool VerifyOatFileChecksums(const OatFile* oat_file,
281                                     const char* dex_location,
282                                     uint32_t dex_location_checksum,
283                                     InstructionSet instruction_set,
284                                     std::string* error_msg);
285
286  // TODO: replace this with multiple methods that allocate the correct managed type.
287  template <class T>
288  mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
289      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
290
291  mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
292      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
293
294  mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
295      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
296
297  mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
298      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
299
300  mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
301      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
302
303  mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
304      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
305
306  mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
307                                                                              size_t length)
308      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
309
310  void VerifyClass(Handle<mirror::Class> klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
311  bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
312                               mirror::Class::Status& oat_file_class_status)
313      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
314  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
315                                         Handle<mirror::Class> klass)
316      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
317  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
318      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
319
320  mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
321                                  jobjectArray interfaces, jobject loader, jobjectArray methods,
322                                  jobjectArray throws)
323      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
324  std::string GetDescriptorForProxy(mirror::Class* proxy_class)
325      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
326  mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
327                                        mirror::ArtMethod* proxy_method)
328      LOCKS_EXCLUDED(dex_lock_)
329      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
330
331  // Get the oat code for a method when its class isn't yet initialized
332  const void* GetQuickOatCodeFor(mirror::ArtMethod* method)
333      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
334  const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code)
335      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
336
337  // Get the oat code for a method from a method index.
338  const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
339      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
340  const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
341      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
342
343  pid_t GetClassesLockOwner();  // For SignalCatcher.
344  pid_t GetDexLockOwner();  // For SignalCatcher.
345
346  const void* GetPortableResolutionTrampoline() const {
347    return portable_resolution_trampoline_;
348  }
349
350  const void* GetQuickGenericJniTrampoline() const {
351    return quick_generic_jni_trampoline_;
352  }
353
354  const void* GetQuickResolutionTrampoline() const {
355    return quick_resolution_trampoline_;
356  }
357
358  const void* GetPortableImtConflictTrampoline() const {
359    return portable_imt_conflict_trampoline_;
360  }
361
362  const void* GetQuickImtConflictTrampoline() const {
363    return quick_imt_conflict_trampoline_;
364  }
365
366  const void* GetQuickToInterpreterBridgeTrampoline() const {
367    return quick_to_interpreter_bridge_trampoline_;
368  }
369
370  InternTable* GetInternTable() const {
371    return intern_table_;
372  }
373
374  // Attempts to insert a class into a class table.  Returns NULL if
375  // the class was inserted, otherwise returns an existing class with
376  // the same descriptor and ClassLoader.
377  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
378      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
379      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
380
381  // Special code to allocate an art method, use this instead of class->AllocObject.
382  mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
383
384  mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
385    mirror::ObjectArray<mirror::Class>* class_roots =
386        ReadBarrier::BarrierForRoot<mirror::ObjectArray<mirror::Class>, kWithReadBarrier>(
387            &class_roots_);
388    DCHECK(class_roots != NULL);
389    return class_roots;
390  }
391
392 private:
393  const OatFile::OatMethod GetOatMethodFor(mirror::ArtMethod* method)
394      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
395
396  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
397      LOCKS_EXCLUDED(dex_lock_)
398      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
399
400  void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
401
402  // For early bootstrapping by Init
403  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
404      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
405
406  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
407  // values that are known to the ClassLinker such as
408  // kObjectArrayClass and kJavaLangString etc.
409  mirror::Class* AllocClass(Thread* self, uint32_t class_size)
410      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
411  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
412      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
413  mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
414
415  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
416      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
417  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
418      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
419
420
421  mirror::Class* CreateArrayClass(Thread* self, const char* descriptor,
422                                  Handle<mirror::ClassLoader> class_loader)
423      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
424
425  void AppendToBootClassPath(const DexFile& dex_file)
426      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
427  void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
428      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
429
430  void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
431                         mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
432      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
433
434  // Precomputes size needed for Class, in the case of a non-temporary class this size must be
435  // sufficient to hold all static fields.
436  uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
437                                            const DexFile::ClassDef& dex_class_def);
438
439  void LoadClass(const DexFile& dex_file,
440                 const DexFile::ClassDef& dex_class_def,
441                 Handle<mirror::Class> klass,
442                 mirror::ClassLoader* class_loader)
443      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
444  void LoadClassMembers(const DexFile& dex_file,
445                        const byte* class_data,
446                        Handle<mirror::Class> klass,
447                        mirror::ClassLoader* class_loader,
448                        const OatFile::OatClass* oat_class)
449      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
450
451  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
452                 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst)
453      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
454
455  mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
456                                const ClassDataItemIterator& dex_method,
457                                Handle<mirror::Class> klass)
458      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
459
460  void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
461
462  // Finds the associated oat class for a dex_file and descriptor
463  OatFile::OatClass GetOatClass(const DexFile& dex_file, uint16_t class_def_idx)
464      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
465
466  void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
467      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
468      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
469  bool IsDexFileRegisteredLocked(const DexFile& dex_file)
470      SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
471
472  bool InitializeClass(Handle<mirror::Class> klass, bool can_run_clinit,
473                       bool can_init_parents)
474      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
475  bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
476                              ObjectLock<mirror::Class>& lock);
477  bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
478      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
479
480  bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
481                                                Handle<mirror::ClassLoader> class_loader1,
482                                                Handle<mirror::ClassLoader> class_loader2)
483      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
484
485  bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
486                                                     mirror::Class* klass1,
487                                                     mirror::Class* klass2)
488      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
489
490  bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass,
491                 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
492                 mirror::Class** new_class)
493      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
494
495  bool LinkSuperClass(Handle<mirror::Class> klass)
496      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
497
498  bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
499      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
500
501  bool LinkMethods(Thread* self, Handle<mirror::Class> klass,
502                   Handle<mirror::ObjectArray<mirror::Class>> interfaces)
503      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
504
505  bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
506      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
507
508  bool LinkInterfaceMethods(Handle<mirror::Class> klass,
509                            Handle<mirror::ObjectArray<mirror::Class>> interfaces)
510      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
511
512  bool LinkStaticFields(Handle<mirror::Class> klass, size_t* class_size)
513      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
514  bool LinkInstanceFields(Handle<mirror::Class> klass)
515      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
516  bool LinkFields(Handle<mirror::Class> klass, bool is_static, size_t* class_size)
517      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
518  void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
519                const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index)
520      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
521
522  void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
523      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
524  void CreateReferenceStaticOffsets(Handle<mirror::Class> klass)
525      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
526  void CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static,
527                              uint32_t reference_offsets)
528      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
529
530  // For use by ImageWriter to find DexCaches for its roots
531  ReaderWriterMutex* DexLock()
532      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
533    return &dex_lock_;
534  }
535  size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
536    return dex_caches_.size();
537  }
538  mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
539
540  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
541      LOCKS_EXCLUDED(dex_lock_)
542      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
543
544  // Find an opened oat file that contains dex_location. If oat_location is not nullptr, the file
545  // must have that location, else any oat location is accepted.
546  const OatFile* FindOpenedOatFile(const char* oat_location, const char* dex_location,
547                                   const uint32_t* const dex_location_checksum)
548      LOCKS_EXCLUDED(dex_lock_);
549  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
550      LOCKS_EXCLUDED(dex_lock_);
551
552  // Note: will not register the oat file.
553  const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location,
554                                                    uint32_t dex_location_checksum,
555                                                    const char* oat_location,
556                                                    std::string* error_msg)
557      LOCKS_EXCLUDED(dex_lock_);
558
559  // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for
560  // the file to be written, which is assumed to be under a lock.
561  const OatFile* CreateOatFileForDexLocation(const char* dex_location,
562                                             int fd, const char* oat_location,
563                                             std::vector<std::string>* error_msgs)
564      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
565
566  // Finds an OatFile that contains a DexFile for the given a DexFile location.
567  //
568  // Note 1: this will not check open oat files, which are assumed to be stale when this is run.
569  // Note 2: Does not register the oat file. It is the caller's job to register if the file is to
570  //         be kept.
571  const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* location,
572                                                             const uint32_t* const location_checksum,
573                                                             InstructionSet isa,
574                                                             std::vector<std::string>* error_msgs,
575                                                             bool* obsolete_file_cleanup_failed)
576      LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
577
578  // Find a verify an oat file with the given dex file. Will return nullptr when the oat file
579  // was not found or the dex file could not be verified.
580  // Note: Does not register the oat file.
581  const OatFile* LoadOatFileAndVerifyDexFile(const std::string& oat_file_location,
582                                             const char* dex_location,
583                                             std::string* error_msg,
584                                             bool* open_failed)
585      LOCKS_EXCLUDED(dex_lock_);
586
587  mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
588                                            mirror::Class* proxy_class)
589      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
590  mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
591                                       Handle<mirror::ArtMethod> prototype)
592      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
593
594  std::vector<const DexFile*> boot_class_path_;
595
596  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
597  std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);;
598  std::vector<mirror::DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
599  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
600
601
602  // multimap from a string hash code of a class descriptor to
603  // mirror::Class* instances. Results should be compared for a matching
604  // Class::descriptor_ and Class::class_loader_.
605  typedef std::multimap<size_t, mirror::Class*> Table;
606  // This contains strong roots. To enable concurrent root scanning of
607  // the class table, be careful to use a read barrier when accessing this.
608  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
609  std::vector<std::pair<size_t, mirror::Class*>> new_class_roots_;
610
611  // Do we need to search dex caches to find image classes?
612  bool dex_cache_image_class_lookup_required_;
613  // Number of times we've searched dex caches for a class. After a certain number of misses we move
614  // the classes into the class_table_ to avoid dex cache based searches.
615  AtomicInteger failed_dex_cache_class_lookups_;
616
617  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
618                                            const mirror::ClassLoader* class_loader,
619                                            size_t hash)
620      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
621
622  mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
623      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
624      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
625
626  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
627      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
628  mirror::Class* LookupClassFromImage(const char* descriptor)
629      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
630
631  // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
632  // before returning it to the caller. Its the responsibility of the thread that placed the class
633  // in the table to make it resolved. The thread doing resolution must notify on the class' lock
634  // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
635  // retire a class, the version of the class in the table is returned and this may differ from
636  // the class passed in.
637  mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
638      WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
639
640  void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
641      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
642
643  // indexes into class_roots_.
644  // needs to be kept in sync with class_roots_descriptors_.
645  enum ClassRoot {
646    kJavaLangClass,
647    kJavaLangObject,
648    kClassArrayClass,
649    kObjectArrayClass,
650    kJavaLangString,
651    kJavaLangDexCache,
652    kJavaLangRefReference,
653    kJavaLangReflectArtField,
654    kJavaLangReflectArtMethod,
655    kJavaLangReflectProxy,
656    kJavaLangStringArrayClass,
657    kJavaLangReflectArtFieldArrayClass,
658    kJavaLangReflectArtMethodArrayClass,
659    kJavaLangClassLoader,
660    kJavaLangThrowable,
661    kJavaLangClassNotFoundException,
662    kJavaLangStackTraceElement,
663    kPrimitiveBoolean,
664    kPrimitiveByte,
665    kPrimitiveChar,
666    kPrimitiveDouble,
667    kPrimitiveFloat,
668    kPrimitiveInt,
669    kPrimitiveLong,
670    kPrimitiveShort,
671    kPrimitiveVoid,
672    kBooleanArrayClass,
673    kByteArrayClass,
674    kCharArrayClass,
675    kDoubleArrayClass,
676    kFloatArrayClass,
677    kIntArrayClass,
678    kLongArrayClass,
679    kShortArrayClass,
680    kJavaLangStackTraceElementArrayClass,
681    kClassRootsMax,
682  };
683  mirror::ObjectArray<mirror::Class>* class_roots_;
684
685  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
686
687  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
688      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
689
690  static const char* class_roots_descriptors_[];
691
692  const char* GetClassRootDescriptor(ClassRoot class_root) {
693    const char* descriptor = class_roots_descriptors_[class_root];
694    CHECK(descriptor != NULL);
695    return descriptor;
696  }
697
698  // The interface table used by all arrays.
699  mirror::IfTable* array_iftable_;
700
701  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
702  // descriptors for the sake of performing FindClass.
703  static constexpr size_t kFindArrayCacheSize = 16;
704  mirror::Class* find_array_class_cache_[kFindArrayCacheSize];
705  size_t find_array_class_cache_next_victim_;
706
707  bool init_done_;
708  bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
709  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
710
711  InternTable* intern_table_;
712
713  // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
714  // patch point within the image. TODO: make these proper relocations.
715  const void* portable_resolution_trampoline_;
716  const void* quick_resolution_trampoline_;
717  const void* portable_imt_conflict_trampoline_;
718  const void* quick_imt_conflict_trampoline_;
719  const void* quick_generic_jni_trampoline_;
720  const void* quick_to_interpreter_bridge_trampoline_;
721
722  friend class ImageWriter;  // for GetClassRoots
723  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
724  FRIEND_TEST(mirror::DexCacheTest, Open);
725  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
726  FRIEND_TEST(ObjectTest, AllocObjectArray);
727  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
728};
729
730}  // namespace art
731
732#endif  // ART_RUNTIME_CLASS_LINKER_H_
733