class_linker.h revision 47f867a0ae34d743f6159c2261e5b11e39693e15
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 private:
458  static void InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg)
459      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
460
461  const OatFile::OatMethod FindOatMethodFor(mirror::ArtMethod* method, bool* found)
462      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
463
464  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
465      LOCKS_EXCLUDED(dex_lock_)
466      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
467
468  void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
469
470  // For early bootstrapping by Init
471  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
472      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
473
474  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
475  // values that are known to the ClassLinker such as
476  // kObjectArrayClass and kJavaLangString etc.
477  mirror::Class* AllocClass(Thread* self, uint32_t class_size)
478      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
479  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
480      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
481  mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
482
483  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
484      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
485  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
486      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
487
488
489  mirror::Class* CreateArrayClass(Thread* self, const char* descriptor, size_t hash,
490                                  Handle<mirror::ClassLoader> class_loader)
491      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
492
493  void AppendToBootClassPath(Thread* self, const DexFile& dex_file)
494      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
495  void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
496      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
497
498  // Precomputes size needed for Class, in the case of a non-temporary class this size must be
499  // sufficient to hold all static fields.
500  uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
501                                            const DexFile::ClassDef& dex_class_def);
502
503  void LoadClass(Thread* self, const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
504                 Handle<mirror::Class> klass, mirror::ClassLoader* class_loader)
505      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
506  void LoadClassMembers(Thread* self, const DexFile& dex_file, const uint8_t* class_data,
507                        Handle<mirror::Class> klass, const OatFile::OatClass* oat_class)
508      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
509
510  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
511                 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst)
512      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
513
514  mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
515                                const ClassDataItemIterator& dex_method,
516                                Handle<mirror::Class> klass)
517      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
518
519  void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
520
521  // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
522  // error and sets found to false.
523  OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found)
524      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
525
526  void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
527      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
528      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
529  bool IsDexFileRegisteredLocked(const DexFile& dex_file)
530      SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
531
532  bool InitializeClass(Thread* self, Handle<mirror::Class> klass, bool can_run_clinit,
533                       bool can_init_parents)
534      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
535  bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
536                              ObjectLock<mirror::Class>& lock);
537  bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
538      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
539
540  bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
541                                                Handle<mirror::ClassLoader> class_loader1,
542                                                Handle<mirror::ClassLoader> class_loader2)
543      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
544
545  bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
546                                                     mirror::Class* klass1,
547                                                     mirror::Class* klass2)
548      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
549
550  bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass,
551                 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
552                 mirror::Class** new_class)
553      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
554
555  bool LinkSuperClass(Handle<mirror::Class> klass)
556      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
557
558  bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
559      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
560
561  bool LinkMethods(Thread* self, Handle<mirror::Class> klass,
562                   Handle<mirror::ObjectArray<mirror::Class>> interfaces,
563                   StackHandleScope<mirror::Class::kImtSize>* out_imt)
564      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
565
566  bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
567      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
568
569  bool LinkInterfaceMethods(Thread* const self, Handle<mirror::Class> klass,
570                            Handle<mirror::ObjectArray<mirror::Class>> interfaces,
571                            StackHandleScope<mirror::Class::kImtSize>* out_imt)
572      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
573
574  bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size)
575      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
576  bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass)
577      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
578  bool LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, size_t* class_size)
579      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
580  void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
581                uint32_t class_def_method_index)
582      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
583  void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
584      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
585
586  // For use by ImageWriter to find DexCaches for its roots
587  ReaderWriterMutex* DexLock()
588      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
589    return &dex_lock_;
590  }
591  size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
592    return dex_caches_.size();
593  }
594  mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
595
596  const OatFile::OatDexFile* FindOpenedOatDexFileForDexFile(const DexFile& dex_file)
597      LOCKS_EXCLUDED(dex_lock_)
598      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
599
600  // Find an opened oat dex file that contains dex_location. If oat_location is not nullptr,
601  // the file must have that location, else any oat location is accepted.
602  const OatFile::OatDexFile* FindOpenedOatDexFile(const char* oat_location,
603                                                  const char* dex_location,
604                                                  const uint32_t* dex_location_checksum)
605      LOCKS_EXCLUDED(dex_lock_);
606
607  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
608      LOCKS_EXCLUDED(dex_lock_);
609
610  mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
611                                            mirror::Class* proxy_class)
612      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
613  mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
614                                       Handle<mirror::ArtMethod> prototype)
615      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
616
617  // Ensures that methods have the kAccPreverified bit set. We use the kAccPreverfied bit on the
618  // class access flags to determine whether this has been done before.
619  void EnsurePreverifiedMethods(Handle<mirror::Class> c)
620      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
621
622  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
623                                            mirror::ClassLoader* class_loader,
624                                            size_t hash)
625      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
626
627  mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
628      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
629      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
630
631  mirror::Class* LookupClassFromImage(const char* descriptor)
632      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
633
634  // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
635  // before returning it to the caller. Its the responsibility of the thread that placed the class
636  // in the table to make it resolved. The thread doing resolution must notify on the class' lock
637  // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
638  // retire a class, the version of the class in the table is returned and this may differ from
639  // the class passed in.
640  mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
641      WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
642
643  void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
644      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
645
646  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
647      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
648
649  // Return the quick generic JNI stub for testing.
650  const void* GetRuntimeQuickGenericJniStub() const;
651
652  std::vector<const DexFile*> boot_class_path_;
653  std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
654
655  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
656  std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);
657  std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
658  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
659
660  class ClassDescriptorHashEquals {
661   public:
662    // Same class loader and descriptor.
663    std::size_t operator()(const GcRoot<mirror::Class>& root) const NO_THREAD_SAFETY_ANALYSIS;
664    bool operator()(const GcRoot<mirror::Class>& a, const GcRoot<mirror::Class>& b) const
665        NO_THREAD_SAFETY_ANALYSIS;
666    // Same class loader and descriptor.
667    std::size_t operator()(const std::pair<const char*, mirror::ClassLoader*>& element) const
668        NO_THREAD_SAFETY_ANALYSIS;
669    bool operator()(const GcRoot<mirror::Class>& a,
670                    const std::pair<const char*, mirror::ClassLoader*>& b) const
671        NO_THREAD_SAFETY_ANALYSIS;
672    // Same descriptor.
673    bool operator()(const GcRoot<mirror::Class>& a, const char* descriptor) const
674        NO_THREAD_SAFETY_ANALYSIS;
675    std::size_t operator()(const char* descriptor) const NO_THREAD_SAFETY_ANALYSIS;
676  };
677  class GcRootEmptyFn {
678   public:
679    void MakeEmpty(GcRoot<mirror::Class>& item) const {
680      item = GcRoot<mirror::Class>();
681    }
682    bool IsEmpty(const GcRoot<mirror::Class>& item) const {
683      return item.IsNull();
684    }
685  };
686
687  // hash set which hashes class descriptor, and compares descriptors nad class loaders. Results
688  // should be compared for a matching Class descriptor and class loader.
689  typedef HashSet<GcRoot<mirror::Class>, GcRootEmptyFn, ClassDescriptorHashEquals,
690      ClassDescriptorHashEquals, TrackingAllocator<GcRoot<mirror::Class>, kAllocatorTagClassTable>>
691      Table;
692  // This contains strong roots. To enable concurrent root scanning of
693  // the class table, be careful to use a read barrier when accessing this.
694  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
695  Table pre_zygote_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
696  std::vector<GcRoot<mirror::Class>> new_class_roots_;
697
698  // Do we need to search dex caches to find image classes?
699  bool dex_cache_image_class_lookup_required_;
700  // Number of times we've searched dex caches for a class. After a certain number of misses we move
701  // the classes into the class_table_ to avoid dex cache based searches.
702  Atomic<uint32_t> failed_dex_cache_class_lookups_;
703
704  // Well known mirror::Class roots.
705  GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
706
707  // The interface table used by all arrays.
708  GcRoot<mirror::IfTable> array_iftable_;
709
710  // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
711  // descriptors for the sake of performing FindClass.
712  static constexpr size_t kFindArrayCacheSize = 16;
713  GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
714  size_t find_array_class_cache_next_victim_;
715
716  bool init_done_;
717  bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
718  bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
719
720  InternTable* intern_table_;
721
722  // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
723  // patch point within the image. TODO: make these proper relocations.
724  const void* quick_resolution_trampoline_;
725  const void* quick_imt_conflict_trampoline_;
726  const void* quick_generic_jni_trampoline_;
727  const void* quick_to_interpreter_bridge_trampoline_;
728
729  // Image pointer size.
730  size_t image_pointer_size_;
731
732  friend class ImageWriter;  // for GetClassRoots
733  friend class ImageDumper;  // for FindOpenedOatFileFromOatLocation
734  friend class JniCompilerTest;  // for GetRuntimeQuickGenericJniStub
735  friend class NoDex2OatTest;  // for FindOpenedOatFileForDexFile
736  friend class NoPatchoatTest;  // for FindOpenedOatFileForDexFile
737  ART_FRIEND_TEST(mirror::DexCacheTest, Open);  // for AllocDexCache
738
739  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
740};
741
742}  // namespace art
743
744#endif  // ART_RUNTIME_CLASS_LINKER_H_
745