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