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