• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 namespace art {
32 namespace gc {
33 namespace space {
34   class ImageSpace;
35 }  // namespace space
36 }  // namespace gc
37 namespace 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 
46 class InternTable;
47 class ObjectLock;
48 template<class T> class SirtRef;
49 
50 typedef bool (ClassVisitor)(mirror::Class* c, void* arg);
51 
52 class 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.
ResolveType(const DexFile & dex_file,uint16_t type_idx,const mirror::Class * referrer)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 
GetBootClassPath()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                                                    uint32_t dex_location_checksum,
262                                                    const std::string& oat_location)
263       LOCKS_EXCLUDED(dex_lock_)
264       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
265   const DexFile* FindOrCreateOatFileForDexLocationLocked(const std::string& dex_location,
266                                                          uint32_t dex_location_checksum,
267                                                          const std::string& oat_location)
268       EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
269       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
270   // Find a DexFile within an OatFile given a DexFile location. Note
271   // that this returns null if the location checksum of the DexFile
272   // does not match the OatFile.
273   const DexFile* FindDexFileInOatFileFromDexLocation(const std::string& location,
274                                                      uint32_t location_checksum)
275       LOCKS_EXCLUDED(dex_lock_)
276       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
277 
278 
279   // Returns true if oat file contains the dex file with the given location and checksum.
280   static bool VerifyOatFileChecksums(const OatFile* oat_file,
281                                      const std::string& dex_location,
282                                      uint32_t dex_location_checksum)
283       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
284 
285   // TODO: replace this with multiple methods that allocate the correct managed type.
286   template <class T>
287   mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
288       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
289 
290   mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
291       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
292 
293   mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
294       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
295 
296   mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
297       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
298 
299   mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
300       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
301 
302   mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
303       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
304 
305   mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
306                                                                               size_t length)
307       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
308 
309   void VerifyClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
310   bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
311                                mirror::Class::Status& oat_file_class_status)
312       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
313   void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, mirror::Class* klass)
314       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
315   void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
316       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
317 
318   mirror::Class* CreateProxyClass(mirror::String* name, mirror::ObjectArray<mirror::Class>* interfaces,
319                                   mirror::ClassLoader* loader,
320                                   mirror::ObjectArray<mirror::ArtMethod>* methods,
321                                   mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >* throws)
322       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
323   std::string GetDescriptorForProxy(const mirror::Class* proxy_class)
324       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
325   mirror::ArtMethod* FindMethodForProxy(const mirror::Class* proxy_class,
326                                         const mirror::ArtMethod* proxy_method)
327       LOCKS_EXCLUDED(dex_lock_)
328       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
329 
330   // Get the oat code for a method when its class isn't yet initialized
331   const void* GetOatCodeFor(const mirror::ArtMethod* method)
332       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
333 
334   // Get the oat code for a method from a method index.
335   const void* GetOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
336       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
337 
338   pid_t GetClassesLockOwner();  // For SignalCatcher.
339   pid_t GetDexLockOwner();  // For SignalCatcher.
340 
GetPortableResolutionTrampoline()341   const void* GetPortableResolutionTrampoline() const {
342     return portable_resolution_trampoline_;
343   }
344 
GetQuickResolutionTrampoline()345   const void* GetQuickResolutionTrampoline() const {
346     return quick_resolution_trampoline_;
347   }
348 
GetInternTable()349   InternTable* GetInternTable() const {
350     return intern_table_;
351   }
352 
353   // Attempts to insert a class into a class table.  Returns NULL if
354   // the class was inserted, otherwise returns an existing class with
355   // the same descriptor and ClassLoader.
356   mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
357       LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
358       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
359 
360  private:
361   explicit ClassLinker(InternTable*);
362 
363   const OatFile::OatMethod GetOatMethodFor(const mirror::ArtMethod* method)
364       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
365 
366   // Initialize class linker by bootstraping from dex files
367   void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
368       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
369 
370   // Initialize class linker from one or more images.
371   void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
372   OatFile& GetImageOatFile(gc::space::ImageSpace* space)
373       LOCKS_EXCLUDED(dex_lock_)
374       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
375 
376   void FinishInit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
377 
378   // For early bootstrapping by Init
379   mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, size_t class_size)
380       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
381 
382   // Alloc* convenience functions to avoid needing to pass in mirror::Class*
383   // values that are known to the ClassLinker such as
384   // kObjectArrayClass and kJavaLangString etc.
385   mirror::Class* AllocClass(Thread* self, size_t class_size) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
386   mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
387       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
388   mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
389   mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
390 
391   mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
392       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
393   mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
394       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
395 
396 
397   mirror::Class* CreateArrayClass(const char* descriptor, mirror::ClassLoader* class_loader)
398       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
399 
400   void AppendToBootClassPath(const DexFile& dex_file)
401       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
402   void AppendToBootClassPath(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache)
403       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
404 
405   void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
406                          mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
407       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
408 
409   size_t SizeOfClass(const DexFile& dex_file,
410                      const DexFile::ClassDef& dex_class_def);
411 
412   void LoadClass(const DexFile& dex_file,
413                  const DexFile::ClassDef& dex_class_def,
414                  SirtRef<mirror::Class>& klass,
415                  mirror::ClassLoader* class_loader)
416       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
417 
418   void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
419                  SirtRef<mirror::Class>& klass, SirtRef<mirror::ArtField>& dst)
420       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
421 
422   mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
423                                 const ClassDataItemIterator& dex_method,
424                                 SirtRef<mirror::Class>& klass)
425       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
426 
427   void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
428 
429   // Finds the associated oat class for a dex_file and descriptor
430   const OatFile::OatClass* GetOatClass(const DexFile& dex_file, uint16_t class_def_idx)
431       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
432 
433   void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache)
434       EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
435       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
436   bool IsDexFileRegisteredLocked(const DexFile& dex_file) const SHARED_LOCKS_REQUIRED(dex_lock_);
437   void RegisterOatFileLocked(const OatFile& oat_file) EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
438       EXCLUSIVE_LOCKS_REQUIRED(dex_lock_);
439 
440   bool InitializeClass(mirror::Class* klass, bool can_run_clinit, bool can_init_parents)
441       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
442   bool WaitForInitializeClass(mirror::Class* klass, Thread* self, ObjectLock& lock);
443   bool ValidateSuperClassDescriptors(const mirror::Class* klass)
444       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
445 
446   bool IsSameDescriptorInDifferentClassContexts(const char* descriptor,
447                                                 const mirror::Class* klass1,
448                                                 const mirror::Class* klass2)
449       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
450 
451   bool IsSameMethodSignatureInDifferentClassContexts(const mirror::ArtMethod* method,
452                                                      const mirror::Class* klass1,
453                                                      const mirror::Class* klass2)
454       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
455 
456   bool LinkClass(SirtRef<mirror::Class>& klass, mirror::ObjectArray<mirror::Class>* interfaces,
457                  Thread* self)
458       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
459 
460   bool LinkSuperClass(SirtRef<mirror::Class>& klass)
461       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
462 
463   bool LoadSuperAndInterfaces(SirtRef<mirror::Class>& klass, const DexFile& dex_file)
464       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
465 
466   bool LinkMethods(SirtRef<mirror::Class>& klass, mirror::ObjectArray<mirror::Class>* interfaces)
467       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
468 
469   bool LinkVirtualMethods(SirtRef<mirror::Class>& klass)
470       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
471 
472   bool LinkInterfaceMethods(SirtRef<mirror::Class>& klass,
473                             mirror::ObjectArray<mirror::Class>* interfaces)
474       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
475 
476   bool LinkStaticFields(SirtRef<mirror::Class>& klass)
477       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
478   bool LinkInstanceFields(SirtRef<mirror::Class>& klass)
479       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
480   bool LinkFields(SirtRef<mirror::Class>& klass, bool is_static)
481       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
482 
483 
484   void CreateReferenceInstanceOffsets(SirtRef<mirror::Class>& klass)
485       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
486   void CreateReferenceStaticOffsets(SirtRef<mirror::Class>& klass)
487       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
488   void CreateReferenceOffsets(SirtRef<mirror::Class>& klass, bool is_static,
489                               uint32_t reference_offsets)
490       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
491 
492   // For use by ImageWriter to find DexCaches for its roots
GetDexCaches()493   const std::vector<mirror::DexCache*>& GetDexCaches() {
494     return dex_caches_;
495   }
496 
497   const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
498       LOCKS_EXCLUDED(dex_lock_)
499       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
500   const OatFile* FindOpenedOatFileFromDexLocation(const std::string& dex_location,
501                                                   uint32_t dex_location_checksum)
502       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
503   const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
504       SHARED_LOCKS_REQUIRED(dex_lock_);
505   const DexFile* FindDexFileInOatLocation(const std::string& dex_location,
506                                           uint32_t dex_location_checksum,
507                                           const std::string& oat_location)
508       EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
509       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
510 
511   const DexFile* VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
512                                                  const std::string& dex_location,
513                                                  uint32_t dex_location_checksum)
514       EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
515       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
516 
517   mirror::ArtMethod* CreateProxyConstructor(Thread* self, SirtRef<mirror::Class>& klass,
518                                             mirror::Class* proxy_class)
519       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
520   mirror::ArtMethod* CreateProxyMethod(Thread* self, SirtRef<mirror::Class>& klass,
521                                        SirtRef<mirror::ArtMethod>& prototype)
522       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
523 
524   std::vector<const DexFile*> boot_class_path_;
525 
526   mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
527   std::vector<mirror::DexCache*> dex_caches_ GUARDED_BY(dex_lock_);
528   std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
529 
530 
531   // multimap from a string hash code of a class descriptor to
532   // mirror::Class* instances. Results should be compared for a matching
533   // Class::descriptor_ and Class::class_loader_.
534   typedef std::multimap<size_t, mirror::Class*> Table;
535   Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
536 
537   // Do we need to search dex caches to find image classes?
538   bool dex_cache_image_class_lookup_required_;
539   // Number of times we've searched dex caches for a class. After a certain number of misses we move
540   // the classes into the class_table_ to avoid dex cache based searches.
541   AtomicInteger failed_dex_cache_class_lookups_;
542 
543   mirror::Class* LookupClassFromTableLocked(const char* descriptor,
544                                             const mirror::ClassLoader* class_loader,
545                                             size_t hash)
546       SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
547 
548   void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
549       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
550   mirror::Class* LookupClassFromImage(const char* descriptor)
551       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
552 
553   // indexes into class_roots_.
554   // needs to be kept in sync with class_roots_descriptors_.
555   enum ClassRoot {
556     kJavaLangClass,
557     kJavaLangObject,
558     kClassArrayClass,
559     kObjectArrayClass,
560     kJavaLangString,
561     kJavaLangDexCache,
562     kJavaLangRefReference,
563     kJavaLangReflectArtField,
564     kJavaLangReflectArtMethod,
565     kJavaLangReflectProxy,
566     kJavaLangStringArrayClass,
567     kJavaLangReflectArtFieldArrayClass,
568     kJavaLangReflectArtMethodArrayClass,
569     kJavaLangClassLoader,
570     kJavaLangThrowable,
571     kJavaLangClassNotFoundException,
572     kJavaLangStackTraceElement,
573     kPrimitiveBoolean,
574     kPrimitiveByte,
575     kPrimitiveChar,
576     kPrimitiveDouble,
577     kPrimitiveFloat,
578     kPrimitiveInt,
579     kPrimitiveLong,
580     kPrimitiveShort,
581     kPrimitiveVoid,
582     kBooleanArrayClass,
583     kByteArrayClass,
584     kCharArrayClass,
585     kDoubleArrayClass,
586     kFloatArrayClass,
587     kIntArrayClass,
588     kLongArrayClass,
589     kShortArrayClass,
590     kJavaLangStackTraceElementArrayClass,
591     kClassRootsMax,
592   };
593   mirror::ObjectArray<mirror::Class>* class_roots_;
594 
595   mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
596 
597   void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
598       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
599 
GetClassRoots()600   mirror::ObjectArray<mirror::Class>* GetClassRoots() {
601     DCHECK(class_roots_ != NULL);
602     return class_roots_;
603   }
604 
605   static const char* class_roots_descriptors_[];
606 
GetClassRootDescriptor(ClassRoot class_root)607   const char* GetClassRootDescriptor(ClassRoot class_root) {
608     const char* descriptor = class_roots_descriptors_[class_root];
609     CHECK(descriptor != NULL);
610     return descriptor;
611   }
612 
613   mirror::IfTable* array_iftable_;
614 
615   bool init_done_;
616   bool dex_caches_dirty_ GUARDED_BY(dex_lock_);
617   bool class_table_dirty_ GUARDED_BY(Locks::classlinker_classes_lock_);
618 
619   InternTable* intern_table_;
620 
621   const void* portable_resolution_trampoline_;
622   const void* quick_resolution_trampoline_;
623 
624   friend class ImageWriter;  // for GetClassRoots
625   FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
626   FRIEND_TEST(mirror::DexCacheTest, Open);
627   FRIEND_TEST(ExceptionTest, FindExceptionHandler);
628   FRIEND_TEST(ObjectTest, AllocObjectArray);
629   DISALLOW_COPY_AND_ASSIGN(ClassLinker);
630 };
631 
632 }  // namespace art
633 
634 #endif  // ART_RUNTIME_CLASS_LINKER_H_
635