blob: 8034d620aa44e584ae3ac87a5ae2bcb7ce23bbde [file] [log] [blame]
Elliott Hughes418d20f2011-09-22 14:00:39 -07001/*
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 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_CLASS_LINKER_H_
18#define ART_RUNTIME_CLASS_LINKER_H_
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
Fred Shih37f05ef2014-07-16 18:38:08 -070020#include <deque>
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include <string>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022#include <utility>
23#include <vector>
24
Mathieu Chartierbad02672014-08-25 13:08:22 -070025#include "base/allocator.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070028#include "dex_file.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070029#include "gc_root.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070030#include "jni.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070031#include "oat_file.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080032#include "object_callbacks.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070033
34namespace art {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035
Ian Rogers1d54e732013-05-02 21:10:01 -070036namespace gc {
37namespace space {
38 class ImageSpace;
39} // namespace space
40} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041namespace mirror {
Ian Rogers33e95662013-05-20 20:29:14 -070042 class ClassLoader;
43 class DexCache;
44 class DexCacheTest_Open_Test;
45 class IfTable;
46 template<class T> class ObjectArray;
47 class StackTraceElement;
48} // namespace mirror
Ian Rogers1d54e732013-05-02 21:10:01 -070049
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070050template<class T> class Handle;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070051class InternTable;
Mathieu Chartierc528dba2013-11-26 12:00:11 -080052template<class T> class ObjectLock;
Andreas Gampe7ba64962014-10-23 11:37:40 -070053class Runtime;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070054class ScopedObjectAccessAlreadyRunnable;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070055template<size_t kNumReferences> class PACKED(4) StackHandleScope;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057typedef bool (ClassVisitor)(mirror::Class* c, void* arg);
Elliott Hughesa2155262011-11-16 16:26:58 -080058
Mathieu Chartier893263b2014-03-04 11:07:42 -080059enum VisitRootFlags : uint8_t;
60
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070061class ClassLinker {
62 public:
Ian Rogers6f3dbba2014-10-14 17:41:57 -070063 // Well known mirror::Class roots accessed via GetClassRoot.
64 enum ClassRoot {
65 kJavaLangClass,
66 kJavaLangObject,
67 kClassArrayClass,
68 kObjectArrayClass,
69 kJavaLangString,
70 kJavaLangDexCache,
71 kJavaLangRefReference,
72 kJavaLangReflectArtField,
73 kJavaLangReflectArtMethod,
74 kJavaLangReflectProxy,
75 kJavaLangStringArrayClass,
76 kJavaLangReflectArtFieldArrayClass,
77 kJavaLangReflectArtMethodArrayClass,
78 kJavaLangClassLoader,
79 kJavaLangThrowable,
80 kJavaLangClassNotFoundException,
81 kJavaLangStackTraceElement,
82 kPrimitiveBoolean,
83 kPrimitiveByte,
84 kPrimitiveChar,
85 kPrimitiveDouble,
86 kPrimitiveFloat,
87 kPrimitiveInt,
88 kPrimitiveLong,
89 kPrimitiveShort,
90 kPrimitiveVoid,
91 kBooleanArrayClass,
92 kByteArrayClass,
93 kCharArrayClass,
94 kDoubleArrayClass,
95 kFloatArrayClass,
96 kIntArrayClass,
97 kLongArrayClass,
98 kShortArrayClass,
99 kJavaLangStackTraceElementArrayClass,
100 kClassRootsMax,
101 };
102
Mathieu Chartier590fee92013-09-13 13:46:47 -0700103 explicit ClassLinker(InternTable* intern_table);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700104 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -0700105
Alex Light64ad14d2014-08-19 14:23:13 -0700106 // Initialize class linker by bootstraping from dex files.
107 void InitWithoutImage(const std::vector<const DexFile*>& boot_class_path)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
109
110 // Initialize class linker from one or more images.
111 void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700113 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700114 // If class_loader is null, searches boot_class_path_.
Ian Rogers98379392014-02-24 16:53:16 -0800115 mirror::Class* FindClass(Thread* self, const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700116 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700119 // Find a class in the path class loader, loading it if necessary.
120 mirror::Class* FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
121 Thread* self, const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700122 Handle<mirror::ClassLoader> class_loader)
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
124
Ian Rogers98379392014-02-24 16:53:16 -0800125 // Finds a class by its descriptor using the "system" class loader, ie by searching the
126 // boot_class_path_.
127 mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129
130 // Finds the array class given for the element class.
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700131 mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700133
Ian Rogers63557452014-06-04 16:57:15 -0700134 // Returns true if the class linker is initialized.
Ian Rogers7b078e82014-09-10 14:44:24 -0700135 bool IsInitialized() const {
136 return init_done_;
137 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700138
Brian Carlstromaded5f72011-10-07 17:15:04 -0700139 // Define a new a class based on a ClassDef from a DexFile
Ian Rogers7b078e82014-09-10 14:44:24 -0700140 mirror::Class* DefineClass(Thread* self, const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700141 Handle<mirror::ClassLoader> class_loader,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700144
145 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
146 // by the given 'class_loader'.
Ian Rogers7b078e82014-09-10 14:44:24 -0700147 mirror::Class* LookupClass(Thread* self, const char* descriptor,
148 const mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700151
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800152 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800156
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700158
Brian Carlstromae826982011-11-09 01:33:42 -0800159 // General class unloading is not supported, this is used to prune
160 // unwanted classes during image writing.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 bool RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800164
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700165 void DumpAllClasses(int flags)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700166 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700168
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700169 void DumpForSigQuit(std::ostream& os)
Ian Rogers7b078e82014-09-10 14:44:24 -0700170 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -0700171
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700172 size_t NumLoadedClasses()
173 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -0700175
Brian Carlstromb63ec392011-08-27 17:38:27 -0700176 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -0700177 // result in the DexCache. The referrer is used to identify the
178 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800179 mirror::String* ResolveString(uint32_t string_idx, mirror::ArtMethod* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700181
182 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700183 // result in the DexCache.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700185 Handle<mirror::DexCache> dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700187
Brian Carlstromb63ec392011-08-27 17:38:27 -0700188 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700189 // result in the DexCache. The referrer is used to identity the
190 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800191 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700193
Brian Carlstromb63ec392011-08-27 17:38:27 -0700194 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700196 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800197 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199
Ian Rogersef7d42f2014-01-06 12:55:46 -0800200 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtField* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700202
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700203 // Resolve a type with the given ID from the DexFile, storing the
204 // result in DexCache. The ClassLoader is used to search for the
205 // type, since it may be referenced from but not contained within
206 // the given DexFile.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700207 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700208 Handle<mirror::DexCache> dex_cache,
209 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700211
212 // Resolve a method with a given ID from the DexFile, storing the
213 // result in DexCache. The ClassLinker and ClassLoader are used as
214 // in ResolveType. What is unique is the method type argument which
215 // is used to determine if this method is a direct, static, or
216 // virtual method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700217 mirror::ArtMethod* ResolveMethod(const DexFile& dex_file,
218 uint32_t method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700219 Handle<mirror::DexCache> dex_cache,
220 Handle<mirror::ClassLoader> class_loader,
221 Handle<mirror::ArtMethod> referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700222 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700224
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700225 mirror::ArtMethod* GetResolvedMethod(uint32_t method_idx, mirror::ArtMethod* referrer,
226 InvokeType type)
227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228 mirror::ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, mirror::ArtMethod** referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700229 InvokeType type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom16192862011-09-12 17:50:06 -0700231
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700232 mirror::ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800234 mirror::ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700235 bool is_static)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700237
Brian Carlstrom16192862011-09-12 17:50:06 -0700238 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700239 // result in DexCache. The ClassLinker and ClassLoader are used as
240 // in ResolveType. What is unique is the is_static argument which is
241 // used to determine if we are resolving a static or non-static
242 // field.
Brian Carlstromea46f952013-07-30 01:26:50 -0700243 mirror::ArtField* ResolveField(const DexFile& dex_file,
244 uint32_t field_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700245 Handle<mirror::DexCache> dex_cache,
246 Handle<mirror::ClassLoader> class_loader,
Brian Carlstromea46f952013-07-30 01:26:50 -0700247 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700249
Ian Rogersb067ac22011-12-13 18:05:09 -0800250 // Resolve a field with a given ID from the DexFile, storing the
251 // result in DexCache. The ClassLinker and ClassLoader are used as
252 // in ResolveType. No is_static argument is provided so that Java
253 // field resolution semantics are followed.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700254 mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file, uint32_t field_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700255 Handle<mirror::DexCache> dex_cache,
256 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb067ac22011-12-13 18:05:09 -0800258
Ian Rogersad25ac52011-10-04 19:13:33 -0700259 // Get shorty from method index without resolution. Used to do handlerization.
Brian Carlstromea46f952013-07-30 01:26:50 -0700260 const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad25ac52011-10-04 19:13:33 -0700262
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700263 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700264 // can_run_clinit=false allows the compiler to attempt to init a class,
265 // given the restriction that no <clinit> execution is possible.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700266 bool EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields,
Ian Rogers7b078e82014-09-10 14:44:24 -0700267 bool can_init_parents)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700269
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700270 // Initializes classes that have instances in the image but that have
271 // <clinit> methods so they could not be initialized by the compiler.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700273
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 void RegisterDexFile(const DexFile& dex_file)
275 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700277 void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700280
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 const OatFile* RegisterOatFile(const OatFile* oat_file)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 LOCKS_EXCLUDED(dex_lock_);
Brian Carlstrom866c8622012-01-06 16:35:13 -0800283
Brian Carlstrom8a487412011-08-29 20:08:52 -0700284 const std::vector<const DexFile*>& GetBootClassPath() {
285 return boot_class_path_;
286 }
287
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700288 void VisitClasses(ClassVisitor* visitor, void* arg)
Ian Rogersdbf3be02014-08-29 15:40:08 -0700289 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700290 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersdbf3be02014-08-29 15:40:08 -0700291
292 // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage
293 // so that it can visit individual classes without holding the doesn't hold the
294 // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code
295 // can race with insertion and deletion of classes while the visitor is being called.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700296 void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg)
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa2155262011-11-16 16:26:58 -0800298
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700299 void VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700300 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800302 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700303 LOCKS_EXCLUDED(dex_lock_)
304 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700305
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700306 mirror::DexCache* FindDexCache(const DexFile& dex_file)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700309 bool IsDexFileRegistered(const DexFile& dex_file)
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700310 LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700311 void FixupDexCaches(mirror::ArtMethod* resolution_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700313 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700314
jeffhao262bf462011-10-20 18:36:32 -0700315 // Generate an oat file from a dex file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700316 bool GenerateOatFile(const char* dex_filename,
Brian Carlstromd601af82012-01-06 10:15:19 -0800317 int oat_fd,
Vladimir Marko60836d52014-01-16 15:53:38 +0000318 const char* oat_cache_filename,
Ian Rogers719d1a32014-03-06 12:13:39 -0800319 std::string* error_msg)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700320 LOCKS_EXCLUDED(Locks::mutator_lock_);
jeffhao262bf462011-10-20 18:36:32 -0700321
Andreas Gampe833a4852014-05-21 18:46:59 -0700322 // Find or create the oat file holding dex_location. Then load all corresponding dex files
323 // (if multidex) into the given vector.
324 bool OpenDexFilesFromOat(const char* dex_location, const char* oat_location,
325 std::vector<std::string>* error_msgs,
326 std::vector<const DexFile*>* dex_files)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700327 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
jeffhaof6174e82012-01-31 16:14:17 -0800328
Alex Light6e183f22014-07-18 14:57:04 -0700329 // Returns true if the given oat file has the same image checksum as the image it is paired with.
330 static bool VerifyOatImageChecksum(const OatFile* oat_file, const InstructionSet instruction_set);
331 // Returns true if the oat file checksums match with the image and the offsets are such that it
332 // could be loaded with it.
333 static bool VerifyOatChecksums(const OatFile* oat_file, const InstructionSet instruction_set,
334 std::string* error_msg);
Brian Carlstrom28db0122012-10-18 16:20:41 -0700335 // Returns true if oat file contains the dex file with the given location and checksum.
Alex Light6e183f22014-07-18 14:57:04 -0700336 static bool VerifyOatAndDexFileChecksums(const OatFile* oat_file,
337 const char* dex_location,
338 uint32_t dex_location_checksum,
339 InstructionSet instruction_set,
340 std::string* error_msg);
Brian Carlstromafe25512012-06-27 17:02:58 -0700341
Ian Rogersc0542af2014-09-03 16:16:56 -0700342 // Allocate an instance of a java.lang.Object.
343 mirror::Object* AllocObject(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
344
Elliott Hughes418d20f2011-09-22 14:00:39 -0700345 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700346 template <class T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700349
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao98eacac2011-09-14 16:11:53 -0700352
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355
Brian Carlstromea46f952013-07-30 01:26:50 -0700356 mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
358
359 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
360 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
361
Brian Carlstromea46f952013-07-30 01:26:50 -0700362 mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
364
365 mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
366 size_t length)
367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
368
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700369 void VerifyClass(Thread* self, Handle<mirror::Class> klass)
Ian Rogers7b078e82014-09-10 14:44:24 -0700370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
372 mirror::Class::Status& oat_file_class_status)
373 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800374 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700375 Handle<mirror::Class> klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -0700377 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
379
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700380 mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
381 jobjectArray interfaces, jobject loader, jobjectArray methods,
382 jobjectArray throws)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800384 std::string GetDescriptorForProxy(mirror::Class* proxy_class)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800386 mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
387 mirror::ArtMethod* proxy_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400390
Ian Rogers19846512012-02-24 11:42:47 -0800391 // Get the oat code for a method when its class isn't yet initialized
Ian Rogersef7d42f2014-01-06 12:55:46 -0800392 const void* GetQuickOatCodeFor(mirror::ArtMethod* method)
393 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
394 const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800396
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700397 // Get the oat code for a method from a method index.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398 const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
399 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
400 const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
Ian Rogers33e95662013-05-20 20:29:14 -0700401 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700402
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700403 // Get compiled code for a method, return null if no code
404 // exists. This is unlike Get..OatCodeFor which will return a bridge
405 // or interpreter entrypoint.
406 const void* GetOatMethodQuickCodeFor(mirror::ArtMethod* method)
407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
408 const void* GetOatMethodPortableCodeFor(mirror::ArtMethod* method)
409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
410
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700411 pid_t GetClassesLockOwner(); // For SignalCatcher.
412 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700413
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700414 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700415
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700416 static const char* GetClassRootDescriptor(ClassRoot class_root);
Andreas Gampe2da88232014-02-27 12:26:20 -0800417
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700418 // Is the given entry point portable code to run the resolution stub?
419 bool IsPortableResolutionStub(const void* entry_point) const;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700420
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700421 // Is the given entry point quick code to run the resolution stub?
422 bool IsQuickResolutionStub(const void* entry_point) const;
Jeff Hao88474b42013-10-23 16:24:40 -0700423
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700424 // Is the given entry point portable code to bridge into the interpreter?
425 bool IsPortableToInterpreterBridge(const void* entry_point) const;
Jeff Hao88474b42013-10-23 16:24:40 -0700426
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700427 // Is the given entry point quick code to bridge into the interpreter?
428 bool IsQuickToInterpreterBridge(const void* entry_point) const;
429
430 // Is the given entry point quick code to run the generic JNI stub?
431 bool IsQuickGenericJniStub(const void* entry_point) const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100432
Jeff Hao88474b42013-10-23 16:24:40 -0700433 InternTable* GetInternTable() const {
434 return intern_table_;
435 }
436
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700437 // Set the entrypoints up for method to the given code.
438 void SetEntryPointsToCompiledCode(mirror::ArtMethod* method, const void* method_code,
439 bool is_portable) const
440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
441
442 // Set the entrypoints up for method to the enter the interpreter.
443 void SetEntryPointsToInterpreter(mirror::ArtMethod* method) const
444 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
445
Ian Rogers848871b2013-08-05 10:56:33 -0700446 // Attempts to insert a class into a class table. Returns NULL if
447 // the class was inserted, otherwise returns an existing class with
448 // the same descriptor and ClassLoader.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700449 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
Ian Rogers848871b2013-08-05 10:56:33 -0700450 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
452
Mathieu Chartier590fee92013-09-13 13:46:47 -0700453 // Special code to allocate an art method, use this instead of class->AllocObject.
454 mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700455
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700456 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700457 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700458 DCHECK(class_roots != NULL);
459 return class_roots;
460 }
461
Mathieu Chartier590fee92013-09-13 13:46:47 -0700462 private:
Ian Rogers97b52f82014-08-14 11:34:07 -0700463 const OatFile::OatMethod FindOatMethodFor(mirror::ArtMethod* method, bool* found)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa12785321912012-04-01 15:24:56 -0700465
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700466 OatFile& GetImageOatFile(gc::space::ImageSpace* space)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700467 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700469
Ian Rogers98379392014-02-24 16:53:16 -0800470 void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700471
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700472 // For early bootstrapping by Init
Ian Rogers6fac4472014-02-25 17:01:10 -0800473 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700474 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700475
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476 // Alloc* convenience functions to avoid needing to pass in mirror::Class*
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700477 // values that are known to the ClassLinker such as
478 // kObjectArrayClass and kJavaLangString etc.
Ian Rogers6fac4472014-02-25 17:01:10 -0800479 mirror::Class* AllocClass(Thread* self, uint32_t class_size)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800480 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -0700483 mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700484
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
487 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700488 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700489
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700490
Ian Rogers98379392014-02-24 16:53:16 -0800491 mirror::Class* CreateArrayClass(Thread* self, const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700492 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700493 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700494
Ian Rogers7b078e82014-09-10 14:44:24 -0700495 void AppendToBootClassPath(Thread* self, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700496 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700497 void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700498 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700499
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700500 // Precomputes size needed for Class, in the case of a non-temporary class this size must be
501 // sufficient to hold all static fields.
502 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
503 const DexFile::ClassDef& dex_class_def);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700504
Ian Rogers7b078e82014-09-10 14:44:24 -0700505 void LoadClass(Thread* self, const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700506 Handle<mirror::Class> klass, mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700507 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers13735952014-10-08 12:43:28 -0700508 void LoadClassMembers(Thread* self, const DexFile& dex_file, const uint8_t* class_data,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700509 Handle<mirror::Class> klass, mirror::ClassLoader* class_loader,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100510 const OatFile::OatClass* oat_class)
511 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700512
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700514 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700516
Brian Carlstromea46f952013-07-30 01:26:50 -0700517 mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
518 const ClassDataItemIterator& dex_method,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700519 Handle<mirror::Class> klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700521
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800522 void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800523
Ian Rogers97b52f82014-08-14 11:34:07 -0700524 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
525 // error and sets found to false.
526 OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found)
Ian Rogers33e95662013-05-20 20:29:14 -0700527 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800528
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700529 void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700532 bool IsDexFileRegisteredLocked(const DexFile& dex_file)
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700533 SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700534
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700535 bool InitializeClass(Thread* self, Handle<mirror::Class> klass, bool can_run_clinit,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800536 bool can_init_parents)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700537 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700538 bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800539 ObjectLock<mirror::Class>& lock);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700540 bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700541 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700542
Ian Rogers98379392014-02-24 16:53:16 -0800543 bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700544 Handle<mirror::ClassLoader> class_loader1,
545 Handle<mirror::ClassLoader> class_loader2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700546 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700547
Ian Rogers98379392014-02-24 16:53:16 -0800548 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800549 mirror::Class* klass1,
550 mirror::Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700552
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700553 bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass,
554 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700555 mirror::Class** new_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700556 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700558 bool LinkSuperClass(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700559 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700560
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700561 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700563
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700564 bool LinkMethods(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700565 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
566 StackHandleScope<mirror::Class::kImtSize>* out_imt)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700567 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700569 bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700571
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700572 bool LinkInterfaceMethods(Thread* const self, Handle<mirror::Class> klass,
573 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
574 StackHandleScope<mirror::Class::kImtSize>* out_imt)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700576
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700577 bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700578 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700579 bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700581 bool LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, size_t* class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700582 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700583 void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
Dmitry Petrochenkof0972a42014-05-16 17:43:39 +0700584 const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index)
585 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700586 void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700587 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700588
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700589 // For use by ImageWriter to find DexCaches for its roots
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700590 ReaderWriterMutex* DexLock()
591 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
592 return &dex_lock_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700593 }
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700594 size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
595 return dex_caches_.size();
596 }
597 mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700598
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100599 const OatFile::OatDexFile* FindOpenedOatDexFileForDexFile(const DexFile& dex_file)
Ian Rogers33e95662013-05-20 20:29:14 -0700600 LOCKS_EXCLUDED(dex_lock_)
601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700602
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100603 // Find an opened oat dex file that contains dex_location. If oat_location is not nullptr,
604 // the file must have that location, else any oat location is accepted.
605 const OatFile::OatDexFile* FindOpenedOatDexFile(const char* oat_location,
606 const char* dex_location,
607 const uint32_t* dex_location_checksum)
Ian Rogers719d1a32014-03-06 12:13:39 -0800608 LOCKS_EXCLUDED(dex_lock_);
Alex Lighta59dd802014-07-02 16:28:08 -0700609
610 // Will open the oat file directly without relocating, even if we could/should do relocation.
611 const OatFile* FindOatFileFromOatLocation(const std::string& oat_location,
612 std::string* error_msg)
613 LOCKS_EXCLUDED(dex_lock_);
614
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700616 LOCKS_EXCLUDED(dex_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700617
Alex Lighta59dd802014-07-02 16:28:08 -0700618 const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location,
619 InstructionSet isa,
620 bool* already_opened,
621 bool* obsolete_file_cleanup_failed,
622 std::vector<std::string>* error_msg)
623 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
624
Alex Light9dcc4572014-08-14 14:16:26 -0700625 const OatFile* GetInterpretedOnlyOat(const std::string& oat_path,
626 InstructionSet isa,
627 std::string* error_msg);
628
Alex Lighta59dd802014-07-02 16:28:08 -0700629 const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output,
630 const std::string& image_location, InstructionSet isa,
631 std::string* error_msg)
632 LOCKS_EXCLUDED(Locks::mutator_lock_);
633
Andreas Gampe7ba64962014-10-23 11:37:40 -0700634 bool CheckOatFile(const Runtime* runtime, const OatFile* oat_file, InstructionSet isa,
Alex Lighta59dd802014-07-02 16:28:08 -0700635 bool* checksum_verified, std::string* error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700636
Andreas Gampe833a4852014-05-21 18:46:59 -0700637 // Note: will not register the oat file.
638 const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location,
639 uint32_t dex_location_checksum,
640 const char* oat_location,
641 std::string* error_msg)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700642 LOCKS_EXCLUDED(dex_lock_);
Ian Rogerse3359f72013-06-11 15:14:11 -0700643
Andreas Gampe833a4852014-05-21 18:46:59 -0700644 // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for
645 // the file to be written, which is assumed to be under a lock.
646 const OatFile* CreateOatFileForDexLocation(const char* dex_location,
647 int fd, const char* oat_location,
648 std::vector<std::string>* error_msgs)
649 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
650
651 // Finds an OatFile that contains a DexFile for the given a DexFile location.
652 //
653 // Note 1: this will not check open oat files, which are assumed to be stale when this is run.
654 // Note 2: Does not register the oat file. It is the caller's job to register if the file is to
655 // be kept.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100656 const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* dex_location,
657 const uint32_t* dex_location_checksum,
Andreas Gampe833a4852014-05-21 18:46:59 -0700658 InstructionSet isa,
Calin Juravlec54aea72014-07-16 14:45:03 +0100659 std::vector<std::string>* error_msgs,
660 bool* obsolete_file_cleanup_failed)
Andreas Gampe833a4852014-05-21 18:46:59 -0700661 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
662
Calin Juravle621962a2014-09-02 15:53:55 +0100663 // Verifies:
664 // - that the oat file contains the dex file (with a matching checksum, which may be null if the
665 // file was pre-opted)
666 // - the checksums of the oat file (against the image space)
667 // - the checksum of the dex file against dex_location_checksum
668 // - that the dex file can be opened
669 // Returns true iff all verification succeed.
670 //
671 // The dex_location is the dex location as stored in the oat file header.
672 // (see DexFile::GetDexCanonicalLocation for a description of location conventions)
Alex Lighta59dd802014-07-02 16:28:08 -0700673 bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location,
Calin Juravle621962a2014-09-02 15:53:55 +0100674 const uint32_t* dex_location_checksum,
Alex Lighta59dd802014-07-02 16:28:08 -0700675 std::string* error_msg);
Brian Carlstromfad71432011-10-16 20:25:10 -0700676
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700677 mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
Brian Carlstromea46f952013-07-30 01:26:50 -0700678 mirror::Class* proxy_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700679 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700680 mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
681 Handle<mirror::ArtMethod> prototype)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700682 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400683
Andreas Gampe48498592014-09-10 19:48:05 -0700684 // Ensures that methods have the kAccPreverified bit set. We use the kAccPreverfied bit on the
685 // class access flags to determine whether this has been done before.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700686 void EnsurePreverifiedMethods(Handle<mirror::Class> c)
Andreas Gampe48498592014-09-10 19:48:05 -0700687 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
688
Ian Rogersdbf3be02014-08-29 15:40:08 -0700689 mirror::Class* LookupClassFromTableLocked(const char* descriptor,
690 const mirror::ClassLoader* class_loader,
691 size_t hash)
692 SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
693
694 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
695 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
696 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
697
698 void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
699 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
701 mirror::Class* LookupClassFromImage(const char* descriptor)
702 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
703
704 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
705 // before returning it to the caller. Its the responsibility of the thread that placed the class
706 // in the table to make it resolved. The thread doing resolution must notify on the class' lock
707 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
708 // retire a class, the version of the class in the table is returned and this may differ from
709 // the class passed in.
710 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
711 WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
712
713 void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
714 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
715
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700716 void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
717 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
718
719 // Return the quick generic JNI stub for testing.
720 const void* GetRuntimeQuickGenericJniStub() const;
721
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700722 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700723
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700724 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700725 std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700726 std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700727 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700728
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700729
Brian Carlstromaded5f72011-10-07 17:15:04 -0700730 // multimap from a string hash code of a class descriptor to
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800731 // mirror::Class* instances. Results should be compared for a matching
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700732 // Class::descriptor_ and Class::class_loader_.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700733 typedef AllocationTrackingMultiMap<size_t, GcRoot<mirror::Class>, kAllocatorTagClassTable> Table;
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700734 // This contains strong roots. To enable concurrent root scanning of
735 // the class table, be careful to use a read barrier when accessing this.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700736 Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700737 std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_;
Elliott Hughesf8349362012-06-18 15:00:06 -0700738
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700739 // Do we need to search dex caches to find image classes?
740 bool dex_cache_image_class_lookup_required_;
741 // Number of times we've searched dex caches for a class. After a certain number of misses we move
742 // the classes into the class_table_ to avoid dex cache based searches.
Ian Rogers68b56852014-08-29 20:19:11 -0700743 Atomic<uint32_t> failed_dex_cache_class_lookups_;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700744
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700745 // Well known mirror::Class roots.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700746 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700747
Ian Rogers98379392014-02-24 16:53:16 -0800748 // The interface table used by all arrays.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700749 GcRoot<mirror::IfTable> array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700750
Ian Rogers98379392014-02-24 16:53:16 -0800751 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
752 // descriptors for the sake of performing FindClass.
753 static constexpr size_t kFindArrayCacheSize = 16;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700754 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
Ian Rogers98379392014-02-24 16:53:16 -0800755 size_t find_array_class_cache_next_victim_;
756
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700757 bool init_done_;
Mathieu Chartier893263b2014-03-04 11:07:42 -0800758 bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
759 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700760
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700761 InternTable* intern_table_;
762
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700763 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
764 // patch point within the image. TODO: make these proper relocations.
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700765 const void* portable_resolution_trampoline_;
766 const void* quick_resolution_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700767 const void* portable_imt_conflict_trampoline_;
768 const void* quick_imt_conflict_trampoline_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800769 const void* quick_generic_jni_trampoline_;
Vladimir Marko8a630572014-04-09 18:45:35 +0100770 const void* quick_to_interpreter_bridge_trampoline_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700771
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700772 friend class ImageWriter; // for GetClassRoots
Alex Lighta59dd802014-07-02 16:28:08 -0700773 friend class ImageDumper; // for FindOpenedOatFileFromOatLocation
774 friend class ElfPatcher; // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700775 friend class JniCompilerTest; // for GetRuntimeQuickGenericJniStub
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100776 friend class NoDex2OatTest; // for FindOpenedOatFileForDexFile
Alex Light9dcc4572014-08-14 14:16:26 -0700777 friend class NoPatchoatTest; // for FindOpenedOatFileForDexFile
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700778 ART_FRIEND_TEST(mirror::DexCacheTest, Open); // for AllocDexCache
779
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
781};
782
783} // namespace art
784
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700785#endif // ART_RUNTIME_CLASS_LINKER_H_