class_linker.h revision ee39a10e45a6a0880e8b829525c40d6055818560
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_CLASS_LINKER_H_
18#define ART_RUNTIME_CLASS_LINKER_H_
19
20#include <string>
21#include <utility>
22#include <vector>
23
24#include "base/macros.h"
25#include "base/mutex.h"
26#include "dex_file.h"
27#include "gtest/gtest.h"
28#include "root_visitor.h"
29#include "oat_file.h"
30
31namespace art {
32namespace gc {
33namespace space {
34  class ImageSpace;
35}  // namespace space
36}  // namespace gc
37namespace mirror {
38  class ClassLoader;
39  class DexCache;
40  class DexCacheTest_Open_Test;
41  class IfTable;
42  template<class T> class ObjectArray;
43  class StackTraceElement;
44}  // namespace mirror
45
46class InternTable;
47class ObjectLock;
48template<class T> class SirtRef;
49
50typedef bool (ClassVisitor)(mirror::Class* c, void* arg);
51
52class ClassLinker {
53 public:
54  // Creates the class linker by bootstrapping from dex files.
55  static ClassLinker* CreateFromCompiler(const std::vector<const DexFile*>& boot_class_path,
56                                         InternTable* intern_table)
57      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
58
59  // Creates the class linker from an image.
60  static ClassLinker* CreateFromImage(InternTable* intern_table)
61      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
62
63  ~ClassLinker();
64
65  bool IsInBootClassPath(const char* descriptor);
66
67  // Finds a class by its descriptor, loading it if necessary.
68  // If class_loader is null, searches boot_class_path_.
69  mirror::Class* FindClass(const char* descriptor, mirror::ClassLoader* class_loader)
70      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
71
72  mirror::Class* FindSystemClass(const char* descriptor)
73      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
74
75  // Define a new a class based on a ClassDef from a DexFile
76  mirror::Class* DefineClass(const char* descriptor, mirror::ClassLoader* class_loader,
77                             const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
78      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
79
80  // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
81  // by the given 'class_loader'.
82  mirror::Class* LookupClass(const char* descriptor, const mirror::ClassLoader* class_loader)
83      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
84      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
85
86  // Finds all the classes with the given descriptor, regardless of ClassLoader.
87  void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
88      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
89      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
90
91  mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
92
93  // General class unloading is not supported, this is used to prune
94  // unwanted classes during image writing.
95  bool RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader)
96      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
97      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98
99  void DumpAllClasses(int flags)
100      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
101      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102
103  void DumpForSigQuit(std::ostream& os)
104      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
105      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106
107  size_t NumLoadedClasses()
108      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
109      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110
111  // Resolve a String with the given index from the DexFile, storing the
112  // result in the DexCache. The referrer is used to identify the
113  // target DexCache and ClassLoader to use for resolution.
114  mirror::String* ResolveString(uint32_t string_idx, const mirror::ArtMethod* referrer)
115      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
117  // Resolve a String with the given index from the DexFile, storing the
118  // result in the DexCache.
119  mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
120                                mirror::DexCache* dex_cache)
121      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
123  // Resolve a Type with the given index from the DexFile, storing the
124  // result in the DexCache. The referrer is used to identity the
125  // target DexCache and ClassLoader to use for resolution.
126  mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
127                             const mirror::Class* referrer)
128      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
129    return ResolveType(dex_file,
130                       type_idx,
131                       referrer->GetDexCache(),
132                       referrer->GetClassLoader());
133  }
134
135  // Resolve a Type with the given index from the DexFile, storing the
136  // result in the DexCache. The referrer is used to identify the
137  // target DexCache and ClassLoader to use for resolution.
138  mirror::Class* ResolveType(uint16_t type_idx, const mirror::ArtMethod* referrer)
139      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
140
141  mirror::Class* ResolveType(uint16_t type_idx, const mirror::ArtField* referrer)
142      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
143
144  // Resolve a type with the given ID from the DexFile, storing the
145  // result in DexCache. The ClassLoader is used to search for the
146  // type, since it may be referenced from but not contained within
147  // the given DexFile.
148  mirror::Class* ResolveType(const DexFile& dex_file,
149                             uint16_t type_idx,
150                             mirror::DexCache* dex_cache,
151                             mirror::ClassLoader* class_loader)
152      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
153
154  // Resolve a method with a given ID from the DexFile, storing the
155  // result in DexCache. The ClassLinker and ClassLoader are used as
156  // in ResolveType. What is unique is the method type argument which
157  // is used to determine if this method is a direct, static, or
158  // virtual method.
159  mirror::ArtMethod* ResolveMethod(const DexFile& dex_file,
160                                   uint32_t method_idx,
161                                   mirror::DexCache* dex_cache,
162                                   mirror::ClassLoader* class_loader,
163                                   const mirror::ArtMethod* referrer,
164                                   InvokeType type)
165      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
166
167  mirror::ArtMethod* ResolveMethod(uint32_t method_idx, const mirror::ArtMethod* referrer,
168                                   InvokeType type)
169      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
170
171  mirror::ArtField* ResolveField(uint32_t field_idx, const mirror::ArtMethod* referrer,
172                                 bool is_static)
173      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
174
175  // Resolve a field with a given ID from the DexFile, storing the
176  // result in DexCache. The ClassLinker and ClassLoader are used as
177  // in ResolveType. What is unique is the is_static argument which is
178  // used to determine if we are resolving a static or non-static
179  // field.
180  mirror::ArtField* ResolveField(const DexFile& dex_file,
181                                 uint32_t field_idx,
182                                 mirror::DexCache* dex_cache,
183                                 mirror::ClassLoader* class_loader,
184                                 bool is_static)
185      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
186
187  // Resolve a field with a given ID from the DexFile, storing the
188  // result in DexCache. The ClassLinker and ClassLoader are used as
189  // in ResolveType. No is_static argument is provided so that Java
190  // field resolution semantics are followed.
191  mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file,
192                                    uint32_t field_idx,
193                                    mirror::DexCache* dex_cache,
194                                    mirror::ClassLoader* class_loader)
195      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
196
197  // Get shorty from method index without resolution. Used to do handlerization.
198  const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length)
199      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
200
201  // Returns true on success, false if there's an exception pending.
202  // can_run_clinit=false allows the compiler to attempt to init a class,
203  // given the restriction that no <clinit> execution is possible.
204  bool EnsureInitialized(mirror::Class* c, bool can_run_clinit, bool can_init_fields)
205      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
206
207  // Initializes classes that have instances in the image but that have
208  // <clinit> methods so they could not be initialized by the compiler.
209  void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
210
211  void RegisterDexFile(const DexFile& dex_file)
212      LOCKS_EXCLUDED(dex_lock_)
213      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
214  void RegisterDexFile(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache)
215      LOCKS_EXCLUDED(dex_lock_)
216      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
217
218  void RegisterOatFile(const OatFile& oat_file)
219      LOCKS_EXCLUDED(dex_lock_);
220
221  const std::vector<const DexFile*>& GetBootClassPath() {
222    return boot_class_path_;
223  }
224
225  void VisitClasses(ClassVisitor* visitor, void* arg)
226      LOCKS_EXCLUDED(dex_lock_)
227      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228  // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_
229  // when calling the visitor.
230  void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg)
231      LOCKS_EXCLUDED(dex_lock_)
232      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
233
234  void VisitRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty)
235      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_, dex_lock_);
236
237  mirror::DexCache* FindDexCache(const DexFile& dex_file) const
238      LOCKS_EXCLUDED(dex_lock_)
239      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
240  bool IsDexFileRegistered(const DexFile& dex_file) const
241      LOCKS_EXCLUDED(dex_lock_);
242  void FixupDexCaches(mirror::ArtMethod* resolution_method) const
243      LOCKS_EXCLUDED(dex_lock_)
244      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
245
246  // Generate an oat file from a dex file
247  bool GenerateOatFile(const std::string& dex_filename,
248                       int oat_fd,
249                       const std::string& oat_cache_filename);
250
251  const OatFile* FindOatFileFromOatLocation(const std::string& location)
252      LOCKS_EXCLUDED(dex_lock_);
253
254  const OatFile* FindOatFileFromOatLocationLocked(const std::string& location)
255      SHARED_LOCKS_REQUIRED(dex_lock_);
256
257  // Finds the oat file for a dex location, generating the oat file if
258  // it is missing or out of date. Returns the DexFile from within the
259  // created oat file.
260  const DexFile* FindOrCreateOatFileForDexLocation(const std::string& dex_location,
261                                                   const std::string& oat_location)
262      LOCKS_EXCLUDED(dex_lock_)
263      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
264  const DexFile* FindOrCreateOatFileForDexLocationLocked(const std::string& dex_location,
265                                                         const std::string& oat_location)
266      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
267      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
268  // Find a DexFile within an OatFile given a DexFile location. Note
269  // that this returns null if the location checksum of the DexFile
270  // does not match the OatFile.
271  const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location)
272      LOCKS_EXCLUDED(dex_lock_)
273      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
274
275
276  // Returns true if oat file contains the dex file with the given location and checksum.
277  static bool VerifyOatFileChecksums(const OatFile* oat_file,
278                                     const std::string& dex_location,
279                                     uint32_t dex_location_checksum)
280      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
281
282  // TODO: replace this with multiple methods that allocate the correct managed type.
283  template <class T>
284  mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
285      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
286
287  mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
288      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
289
290  mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
291      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
292
293  mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
294      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
295
296  mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
297      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
298
299  mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
300      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
301
302  mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
303                                                                              size_t length)
304      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
305
306  void VerifyClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
307  bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
308                               mirror::Class::Status& oat_file_class_status)
309      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
310  void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, mirror::Class* klass)
311      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
312  void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
313      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
314
315  mirror::Class* CreateProxyClass(mirror::String* name, mirror::ObjectArray<mirror::Class>* interfaces,
316                                  mirror::ClassLoader* loader,
317                                  mirror::ObjectArray<mirror::ArtMethod>* methods,
318                                  mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >* throws)
319      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
320  std::string GetDescriptorForProxy(const mirror::Class* proxy_class)
321      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
322  mirror::ArtMethod* FindMethodForProxy(const mirror::Class* proxy_class,
323                                        const mirror::ArtMethod* proxy_method)
324      LOCKS_EXCLUDED(dex_lock_)
325      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
326
327  // Get the oat code for a method when its class isn't yet initialized
328  const void* GetOatCodeFor(const mirror::ArtMethod* method)
329      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
330
331  // Get the oat code for a method from a method index.
332  const void* GetOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
333      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
334
335  pid_t GetClassesLockOwner();  // For SignalCatcher.
336  pid_t GetDexLockOwner();  // For SignalCatcher.
337
338  const void* GetPortableResolutionTrampoline() const {
339    return portable_resolution_trampoline_;
340  }
341
342  const void* GetQuickResolutionTrampoline() const {
343    return quick_resolution_trampoline_;
344  }
345
346  InternTable* GetInternTable() const {
347    return intern_table_;
348  }
349
350  // Attempts to insert a class into a class table.  Returns NULL if
351  // the class was inserted, otherwise returns an existing class with
352  // the same descriptor and ClassLoader.
353  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
354      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
355      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
356
357 private:
358  explicit ClassLinker(InternTable*);
359
360  const OatFile::OatMethod GetOatMethodFor(const mirror::ArtMethod* method)
361      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
362
363  // Initialize class linker by bootstraping from dex files
364  void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
365      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
366
367  // Initialize class linker from one or more images.
368  void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
369  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
370      LOCKS_EXCLUDED(dex_lock_)
371      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
372
373  void FinishInit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
374
375  // For early bootstrapping by Init
376  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, size_t class_size)
377      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
378
379  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
380  // values that are known to the ClassLinker such as
381  // kObjectArrayClass and kJavaLangString etc.
382  mirror::Class* AllocClass(Thread* self, size_t class_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
383  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
384      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
385  mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
386  mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
387
388  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
389      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
390  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
391      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
392
393
394  mirror::Class* CreateArrayClass(const char* descriptor, mirror::ClassLoader* class_loader)
395      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
396
397  void AppendToBootClassPath(const DexFile& dex_file)
398      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
399  void AppendToBootClassPath(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache)
400      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
401
402  void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
403                         mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
404      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
405
406  size_t SizeOfClass(const DexFile& dex_file,
407                     const DexFile::ClassDef& dex_class_def);
408
409  void LoadClass(const DexFile& dex_file,
410                 const DexFile::ClassDef& dex_class_def,
411                 SirtRef<mirror::Class>& klass,
412                 mirror::ClassLoader* class_loader)
413      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
414
415  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
416                 SirtRef<mirror::Class>& klass, SirtRef<mirror::ArtField>& dst)
417      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
418
419  mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
420                                const ClassDataItemIterator& dex_method,
421                                SirtRef<mirror::Class>& klass)
422      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
423
424  void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
425
426  // Finds the associated oat class for a dex_file and descriptor
427  const OatFile::OatClass* GetOatClass(const DexFile& dex_file, uint16_t class_def_idx)
428      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
429
430  void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache)
431      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
432      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
433  bool IsDexFileRegisteredLocked(const DexFile& dex_file) const SHARED_LOCKS_REQUIRED(dex_lock_);
434  void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
435      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
436
437  bool InitializeClass(mirror::Class* klass, bool can_run_clinit, bool can_init_parents)
438      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
439  bool WaitForInitializeClass(mirror::Class* klass, Thread* self, ObjectLock& lock);
440  bool ValidateSuperClassDescriptors(const mirror::Class* klass)
441      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
442
443  bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
444                                                const mirror::Class* klass1,
445                                                const mirror::Class* klass2)
446      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
447
448  bool IsSameMethodSignatureInDifferentClassContexts(const mirror::ArtMethod* method,
449                                                     const mirror::Class* klass1,
450                                                     const mirror::Class* klass2)
451      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
452
453  bool LinkClass(SirtRef<mirror::Class>& klass, mirror::ObjectArray<mirror::Class>* interfaces,
454                 Thread* self)
455      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
456
457  bool LinkSuperClass(SirtRef<mirror::Class>& klass)
458      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
459
460  bool LoadSuperAndInterfaces(SirtRef<mirror::Class>& klass, const DexFile& dex_file)
461      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
462
463  bool LinkMethods(SirtRef<mirror::Class>& klass, mirror::ObjectArray<mirror::Class>* interfaces)
464      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
465
466  bool LinkVirtualMethods(SirtRef<mirror::Class>& klass)
467      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
468
469  bool LinkInterfaceMethods(SirtRef<mirror::Class>& klass,
470                            mirror::ObjectArray<mirror::Class>* interfaces)
471      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
472
473  bool LinkStaticFields(SirtRef<mirror::Class>& klass)
474      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
475  bool LinkInstanceFields(SirtRef<mirror::Class>& klass)
476      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
477  bool LinkFields(SirtRef<mirror::Class>& klass, bool is_static)
478      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
479
480
481  void CreateReferenceInstanceOffsets(SirtRef<mirror::Class>& klass)
482      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
483  void CreateReferenceStaticOffsets(SirtRef<mirror::Class>& klass)
484      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
485  void CreateReferenceOffsets(SirtRef<mirror::Class>& klass, bool is_static,
486                              uint32_t reference_offsets)
487      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
488
489  // For use by ImageWriter to find DexCaches for its roots
490  const std::vector<mirror::DexCache*>& GetDexCaches() {
491    return dex_caches_;
492  }
493
494  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
495      LOCKS_EXCLUDED(dex_lock_)
496      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
497  const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location)
498      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
499  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
500      SHARED_LOCKS_REQUIRED(dex_lock_);
501  const DexFile* FindDexFileInOatLocation(const std::string& dex_location,
502                                          uint32_t dex_location_checksum,
503                                          const std::string& oat_location)
504      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
505      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
506
507  const DexFile* VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
508                                                 const std::string& dex_location,
509                                                 uint32_t dex_location_checksum)
510      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
511      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
512
513  mirror::ArtMethod* CreateProxyConstructor(Thread* self, SirtRef<mirror::Class>& klass,
514                                            mirror::Class* proxy_class)
515      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
516  mirror::ArtMethod* CreateProxyMethod(Thread* self, SirtRef<mirror::Class>& klass,
517                                       SirtRef<mirror::ArtMethod>& prototype)
518      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
519
520  std::vector<const DexFile*> boot_class_path_;
521
522  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
523  std::vector<mirror::DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
524  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
525
526
527  // multimap from a string hash code of a class descriptor to
528  // mirror::Class* instances. Results should be compared for a matching
529  // Class::descriptor_ and Class::class_loader_.
530  typedef std::multimap<size_t, mirror::Class*> Table;
531  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
532
533  // Do we need to search dex caches to find image classes?
534  bool dex_cache_image_class_lookup_required_;
535  // Number of times we've searched dex caches for a class. After a certain number of misses we move
536  // the classes into the class_table_ to avoid dex cache based searches.
537  AtomicInteger failed_dex_cache_class_lookups_;
538
539  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
540                                            const mirror::ClassLoader* class_loader,
541                                            size_t hash)
542      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
543
544  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
545      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
546  mirror::Class* LookupClassFromImage(const char* descriptor)
547      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
548
549  // indexes into class_roots_.
550  // needs to be kept in sync with class_roots_descriptors_.
551  enum ClassRoot {
552    kJavaLangClass,
553    kJavaLangObject,
554    kClassArrayClass,
555    kObjectArrayClass,
556    kJavaLangString,
557    kJavaLangDexCache,
558    kJavaLangRefReference,
559    kJavaLangReflectArtField,
560    kJavaLangReflectArtMethod,
561    kJavaLangReflectProxy,
562    kJavaLangStringArrayClass,
563    kJavaLangReflectArtFieldArrayClass,
564    kJavaLangReflectArtMethodArrayClass,
565    kJavaLangClassLoader,
566    kJavaLangThrowable,
567    kJavaLangClassNotFoundException,
568    kJavaLangStackTraceElement,
569    kPrimitiveBoolean,
570    kPrimitiveByte,
571    kPrimitiveChar,
572    kPrimitiveDouble,
573    kPrimitiveFloat,
574    kPrimitiveInt,
575    kPrimitiveLong,
576    kPrimitiveShort,
577    kPrimitiveVoid,
578    kBooleanArrayClass,
579    kByteArrayClass,
580    kCharArrayClass,
581    kDoubleArrayClass,
582    kFloatArrayClass,
583    kIntArrayClass,
584    kLongArrayClass,
585    kShortArrayClass,
586    kJavaLangStackTraceElementArrayClass,
587    kClassRootsMax,
588  };
589  mirror::ObjectArray<mirror::Class>* class_roots_;
590
591  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
592
593  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
594      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
595
596  mirror::ObjectArray<mirror::Class>* GetClassRoots() {
597    DCHECK(class_roots_ != NULL);
598    return class_roots_;
599  }
600
601  static const char* class_roots_descriptors_[];
602
603  const char* GetClassRootDescriptor(ClassRoot class_root) {
604    const char* descriptor = class_roots_descriptors_[class_root];
605    CHECK(descriptor != NULL);
606    return descriptor;
607  }
608
609  mirror::IfTable* array_iftable_;
610
611  bool init_done_;
612  bool dex_caches_dirty_ GUARDED_BY(dex_lock_);
613  bool class_table_dirty_ GUARDED_BY(Locks::classlinker_classes_lock_);
614
615  InternTable* intern_table_;
616
617  const void* portable_resolution_trampoline_;
618  const void* quick_resolution_trampoline_;
619
620  friend class ImageWriter;  // for GetClassRoots
621  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
622  FRIEND_TEST(mirror::DexCacheTest, Open);
623  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
624  FRIEND_TEST(ObjectTest, AllocObjectArray);
625  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
626};
627
628}  // namespace art
629
630#endif  // ART_RUNTIME_CLASS_LINKER_H_
631