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