class_linker.h revision 7dfb28c066159e6cde8181720f0c451a700ef966
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 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, 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  bool IsDirty() const {
339    return is_dirty_;
340  }
341
342  void Dirty() {
343    is_dirty_ = true;
344  }
345
346  const void* GetPortableResolutionTrampoline() const {
347    return portable_resolution_trampoline_;
348  }
349
350  const void* GetQuickResolutionTrampoline() const {
351    return quick_resolution_trampoline_;
352  }
353
354  InternTable* GetInternTable() const {
355    return intern_table_;
356  }
357
358  // Attempts to insert a class into a class table.  Returns NULL if
359  // the class was inserted, otherwise returns an existing class with
360  // the same descriptor and ClassLoader.
361  mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
362      LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
363      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
364
365 private:
366  explicit ClassLinker(InternTable*);
367
368  const OatFile::OatMethod GetOatMethodFor(const mirror::ArtMethod* method)
369      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
370
371  // Initialize class linker by bootstraping from dex files
372  void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
373      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
374
375  // Initialize class linker from one or more images.
376  void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
377  OatFile& GetImageOatFile(gc::space::ImageSpace* space)
378      LOCKS_EXCLUDED(dex_lock_)
379      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
380
381  void FinishInit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
382
383  // For early bootstrapping by Init
384  mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, size_t class_size)
385      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
386
387  // Alloc* convenience functions to avoid needing to pass in mirror::Class*
388  // values that are known to the ClassLinker such as
389  // kObjectArrayClass and kJavaLangString etc.
390  mirror::Class* AllocClass(Thread* self, size_t class_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
391  mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
392      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
393  mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
394  mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
395
396  mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
397      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
398  mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
399      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
400
401
402  mirror::Class* CreateArrayClass(const char* descriptor, mirror::ClassLoader* class_loader)
403      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
404
405  void AppendToBootClassPath(const DexFile& dex_file)
406      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
407  void AppendToBootClassPath(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache)
408      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
409
410  void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
411                         mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
412      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
413
414  size_t SizeOfClass(const DexFile& dex_file,
415                     const DexFile::ClassDef& dex_class_def);
416
417  void LoadClass(const DexFile& dex_file,
418                 const DexFile::ClassDef& dex_class_def,
419                 SirtRef<mirror::Class>& klass,
420                 mirror::ClassLoader* class_loader)
421      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
422
423  void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
424                 SirtRef<mirror::Class>& klass, SirtRef<mirror::ArtField>& dst)
425      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
426
427  mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
428                                const ClassDataItemIterator& dex_method,
429                                SirtRef<mirror::Class>& klass)
430      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
431
432  void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
433
434  // Finds the associated oat class for a dex_file and descriptor
435  const OatFile::OatClass* GetOatClass(const DexFile& dex_file, const char* descriptor)
436      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
437
438  void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache)
439      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
440      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
441  bool IsDexFileRegisteredLocked(const DexFile& dex_file) const SHARED_LOCKS_REQUIRED(dex_lock_);
442  void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
443      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
444
445  bool InitializeClass(mirror::Class* klass, bool can_run_clinit, bool can_init_parents)
446      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
447  bool WaitForInitializeClass(mirror::Class* klass, Thread* self, ObjectLock& lock);
448  bool ValidateSuperClassDescriptors(const mirror::Class* klass)
449      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
450
451  bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
452                                                const mirror::Class* klass1,
453                                                const mirror::Class* klass2)
454      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
455
456  bool IsSameMethodSignatureInDifferentClassContexts(const mirror::ArtMethod* method,
457                                                     const mirror::Class* klass1,
458                                                     const mirror::Class* klass2)
459      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
460
461  bool LinkClass(SirtRef<mirror::Class>& klass, mirror::ObjectArray<mirror::Class>* interfaces,
462                 Thread* self)
463      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
464
465  bool LinkSuperClass(SirtRef<mirror::Class>& klass)
466      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
467
468  bool LoadSuperAndInterfaces(SirtRef<mirror::Class>& klass, const DexFile& dex_file)
469      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
470
471  bool LinkMethods(SirtRef<mirror::Class>& klass, mirror::ObjectArray<mirror::Class>* interfaces)
472      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
473
474  bool LinkVirtualMethods(SirtRef<mirror::Class>& klass)
475      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
476
477  bool LinkInterfaceMethods(SirtRef<mirror::Class>& klass,
478                            mirror::ObjectArray<mirror::Class>* interfaces)
479      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
480
481  bool LinkStaticFields(SirtRef<mirror::Class>& klass)
482      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
483  bool LinkInstanceFields(SirtRef<mirror::Class>& klass)
484      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
485  bool LinkFields(SirtRef<mirror::Class>& klass, bool is_static)
486      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
487
488
489  void CreateReferenceInstanceOffsets(SirtRef<mirror::Class>& klass)
490      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
491  void CreateReferenceStaticOffsets(SirtRef<mirror::Class>& klass)
492      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
493  void CreateReferenceOffsets(SirtRef<mirror::Class>& klass, bool is_static,
494                              uint32_t reference_offsets)
495      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
496
497  // For use by ImageWriter to find DexCaches for its roots
498  const std::vector<mirror::DexCache*>& GetDexCaches() {
499    return dex_caches_;
500  }
501
502  const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
503      LOCKS_EXCLUDED(dex_lock_)
504      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
505  const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location)
506      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
507  const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
508      SHARED_LOCKS_REQUIRED(dex_lock_);
509  const DexFile* FindDexFileInOatLocation(const std::string& dex_location,
510                                          uint32_t dex_location_checksum,
511                                          const std::string& oat_location)
512      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
513      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
514
515  const DexFile* VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
516                                                 const std::string& dex_location,
517                                                 uint32_t dex_location_checksum)
518      EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
519      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
520
521  mirror::ArtMethod* CreateProxyConstructor(Thread* self, SirtRef<mirror::Class>& klass,
522                                            mirror::Class* proxy_class)
523      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
524  mirror::ArtMethod* CreateProxyMethod(Thread* self, SirtRef<mirror::Class>& klass,
525                                       SirtRef<mirror::ArtMethod>& prototype)
526      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
527
528  std::vector<const DexFile*> boot_class_path_;
529
530  mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
531  std::vector<mirror::DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
532  std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
533
534
535  // multimap from a string hash code of a class descriptor to
536  // mirror::Class* instances. Results should be compared for a matching
537  // Class::descriptor_ and Class::class_loader_.
538  typedef std::multimap<size_t, mirror::Class*> Table;
539  Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
540
541  // Do we need to search dex caches to find image classes?
542  bool dex_cache_image_class_lookup_required_;
543  // Number of times we've searched dex caches for a class. After a certain number of misses we move
544  // the classes into the class_table_ to avoid dex cache based searches.
545  AtomicInteger failed_dex_cache_class_lookups_;
546
547  mirror::Class* LookupClassFromTableLocked(const char* descriptor,
548                                            const mirror::ClassLoader* class_loader,
549                                            size_t hash)
550      SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
551
552  void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
553      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
554  mirror::Class* LookupClassFromImage(const char* descriptor)
555      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
556
557  // indexes into class_roots_.
558  // needs to be kept in sync with class_roots_descriptors_.
559  enum ClassRoot {
560    kJavaLangClass,
561    kJavaLangObject,
562    kClassArrayClass,
563    kObjectArrayClass,
564    kJavaLangString,
565    kJavaLangDexCache,
566    kJavaLangRefReference,
567    kJavaLangReflectArtField,
568    kJavaLangReflectArtMethod,
569    kJavaLangReflectProxy,
570    kJavaLangStringArrayClass,
571    kJavaLangReflectArtFieldArrayClass,
572    kJavaLangReflectArtMethodArrayClass,
573    kJavaLangClassLoader,
574    kJavaLangThrowable,
575    kJavaLangClassNotFoundException,
576    kJavaLangStackTraceElement,
577    kPrimitiveBoolean,
578    kPrimitiveByte,
579    kPrimitiveChar,
580    kPrimitiveDouble,
581    kPrimitiveFloat,
582    kPrimitiveInt,
583    kPrimitiveLong,
584    kPrimitiveShort,
585    kPrimitiveVoid,
586    kBooleanArrayClass,
587    kByteArrayClass,
588    kCharArrayClass,
589    kDoubleArrayClass,
590    kFloatArrayClass,
591    kIntArrayClass,
592    kLongArrayClass,
593    kShortArrayClass,
594    kJavaLangStackTraceElementArrayClass,
595    kClassRootsMax,
596  };
597  mirror::ObjectArray<mirror::Class>* class_roots_;
598
599  mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
600
601  void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
602      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
603
604  mirror::ObjectArray<mirror::Class>* GetClassRoots() {
605    DCHECK(class_roots_ != NULL);
606    return class_roots_;
607  }
608
609  static const char* class_roots_descriptors_[];
610
611  const char* GetClassRootDescriptor(ClassRoot class_root) {
612    const char* descriptor = class_roots_descriptors_[class_root];
613    CHECK(descriptor != NULL);
614    return descriptor;
615  }
616
617  mirror::IfTable* array_iftable_;
618
619  bool init_done_;
620  bool is_dirty_;
621
622  InternTable* intern_table_;
623
624  const void* portable_resolution_trampoline_;
625  const void* quick_resolution_trampoline_;
626
627  friend class ImageWriter;  // for GetClassRoots
628  FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
629  FRIEND_TEST(mirror::DexCacheTest, Open);
630  FRIEND_TEST(ExceptionTest, FindExceptionHandler);
631  FRIEND_TEST(ObjectTest, AllocObjectArray);
632  DISALLOW_COPY_AND_ASSIGN(ClassLinker);
633};
634
635}  // namespace art
636
637#endif  // ART_RUNTIME_CLASS_LINKER_H_
638