blob: 947e15210b6e4b8e142a0d693111f8e60f015cda [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"
Mathieu Chartierc2e20622014-11-03 11:41:47 -080026#include "base/hash_set.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080028#include "base/mutex.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029#include "dex_file.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070030#include "gc_root.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070031#include "jni.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070032#include "oat_file.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080033#include "object_callbacks.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
35namespace art {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036
Ian Rogers1d54e732013-05-02 21:10:01 -070037namespace gc {
38namespace space {
39 class ImageSpace;
40} // namespace space
41} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042namespace mirror {
Ian Rogers33e95662013-05-20 20:29:14 -070043 class ClassLoader;
44 class DexCache;
45 class DexCacheTest_Open_Test;
46 class IfTable;
47 template<class T> class ObjectArray;
48 class StackTraceElement;
49} // namespace mirror
Ian Rogers1d54e732013-05-02 21:10:01 -070050
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070051template<class T> class Handle;
Hiroshi Yamauchi679b1cf2015-05-21 12:05:27 -070052template<class T> class MutableHandle;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070053class InternTable;
Mathieu Chartierc528dba2013-11-26 12:00:11 -080054template<class T> class ObjectLock;
Andreas Gampe7ba64962014-10-23 11:37:40 -070055class Runtime;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070056class ScopedObjectAccessAlreadyRunnable;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070057template<size_t kNumReferences> class PACKED(4) StackHandleScope;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070058
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059typedef bool (ClassVisitor)(mirror::Class* c, void* arg);
Elliott Hughesa2155262011-11-16 16:26:58 -080060
Mathieu Chartier893263b2014-03-04 11:07:42 -080061enum VisitRootFlags : uint8_t;
62
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070063class ClassLinker {
64 public:
Ian Rogers6f3dbba2014-10-14 17:41:57 -070065 // Well known mirror::Class roots accessed via GetClassRoot.
66 enum ClassRoot {
67 kJavaLangClass,
68 kJavaLangObject,
69 kClassArrayClass,
70 kObjectArrayClass,
71 kJavaLangString,
72 kJavaLangDexCache,
73 kJavaLangRefReference,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070074 kJavaLangReflectArtMethod,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070075 kJavaLangReflectConstructor,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070076 kJavaLangReflectField,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070077 kJavaLangReflectMethod,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070078 kJavaLangReflectProxy,
79 kJavaLangStringArrayClass,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070080 kJavaLangReflectArtMethodArrayClass,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070081 kJavaLangReflectConstructorArrayClass,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070082 kJavaLangReflectFieldArrayClass,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070083 kJavaLangReflectMethodArrayClass,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070084 kJavaLangClassLoader,
85 kJavaLangThrowable,
86 kJavaLangClassNotFoundException,
87 kJavaLangStackTraceElement,
88 kPrimitiveBoolean,
89 kPrimitiveByte,
90 kPrimitiveChar,
91 kPrimitiveDouble,
92 kPrimitiveFloat,
93 kPrimitiveInt,
94 kPrimitiveLong,
95 kPrimitiveShort,
96 kPrimitiveVoid,
97 kBooleanArrayClass,
98 kByteArrayClass,
99 kCharArrayClass,
100 kDoubleArrayClass,
101 kFloatArrayClass,
102 kIntArrayClass,
103 kLongArrayClass,
104 kShortArrayClass,
105 kJavaLangStackTraceElementArrayClass,
106 kClassRootsMax,
107 };
108
Mathieu Chartier590fee92013-09-13 13:46:47 -0700109 explicit ClassLinker(InternTable* intern_table);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700110 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -0700111
Alex Light64ad14d2014-08-19 14:23:13 -0700112 // Initialize class linker by bootstraping from dex files.
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800113 void InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
115
116 // Initialize class linker from one or more images.
117 void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
118
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700119 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700120 // If class_loader is null, searches boot_class_path_.
Ian Rogers98379392014-02-24 16:53:16 -0800121 mirror::Class* FindClass(Thread* self, const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700122 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700124
Andreas Gampef865ea92015-04-13 22:14:19 -0700125 // Finds a class in the path class loader, loading it if necessary without using JNI. Hash
126 // function is supposed to be ComputeModifiedUtf8Hash(descriptor). Returns true if the
127 // class-loader chain could be handled, false otherwise, i.e., a non-supported class-loader
128 // was encountered while walking the parent chain (currently only BootClassLoader and
129 // PathClassLoader are supported).
130 bool FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
131 Thread* self, const char* descriptor, size_t hash,
132 Handle<mirror::ClassLoader> class_loader,
133 mirror::Class** result)
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135
Ian Rogers98379392014-02-24 16:53:16 -0800136 // Finds a class by its descriptor using the "system" class loader, ie by searching the
137 // boot_class_path_.
138 mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
140
141 // Finds the array class given for the element class.
Mathieu Chartierb74cd292014-05-29 14:31:33 -0700142 mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700144
Ian Rogers63557452014-06-04 16:57:15 -0700145 // Returns true if the class linker is initialized.
Ian Rogers7b078e82014-09-10 14:44:24 -0700146 bool IsInitialized() const {
147 return init_done_;
148 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700149
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 // Define a new a class based on a ClassDef from a DexFile
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800151 mirror::Class* DefineClass(Thread* self, const char* descriptor, size_t hash,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700152 Handle<mirror::ClassLoader> class_loader,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700155
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 // Finds a class by its descriptor, returning null if it isn't wasn't loaded
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700157 // by the given 'class_loader'.
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800158 mirror::Class* LookupClass(Thread* self, const char* descriptor, size_t hash,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800159 mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700160 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700162
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800163 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
166 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800167
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700169
Brian Carlstromae826982011-11-09 01:33:42 -0800170 // General class unloading is not supported, this is used to prune
171 // unwanted classes during image writing.
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800172 bool RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700173 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800175
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700176 void DumpAllClasses(int flags)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700179
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700180 void DumpForSigQuit(std::ostream& os)
Ian Rogers7b078e82014-09-10 14:44:24 -0700181 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -0700182
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700183 size_t NumLoadedClasses()
184 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -0700186
Brian Carlstromb63ec392011-08-27 17:38:27 -0700187 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -0700188 // result in the DexCache. The referrer is used to identify the
189 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800190 mirror::String* ResolveString(uint32_t string_idx, mirror::ArtMethod* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700192
193 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700194 // result in the DexCache.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700196 Handle<mirror::DexCache> dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700198
Brian Carlstromb63ec392011-08-27 17:38:27 -0700199 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700200 // result in the DexCache. The referrer is used to identity the
201 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800202 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700204
Brian Carlstromb63ec392011-08-27 17:38:27 -0700205 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700207 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800208 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210
Mathieu Chartierc7853442015-03-27 14:35:38 -0700211 mirror::Class* ResolveType(uint16_t type_idx, ArtField* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700213
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700214 // Resolve a type with the given ID from the DexFile, storing the
215 // result in DexCache. The ClassLoader is used to search for the
216 // type, since it may be referenced from but not contained within
217 // the given DexFile.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700218 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700219 Handle<mirror::DexCache> dex_cache,
220 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700222
223 // Resolve a method with a given ID from the DexFile, storing the
224 // result in DexCache. The ClassLinker and ClassLoader are used as
225 // in ResolveType. What is unique is the method type argument which
226 // is used to determine if this method is a direct, static, or
227 // virtual method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700228 mirror::ArtMethod* ResolveMethod(const DexFile& dex_file,
229 uint32_t method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700230 Handle<mirror::DexCache> dex_cache,
231 Handle<mirror::ClassLoader> class_loader,
232 Handle<mirror::ArtMethod> referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700233 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700235
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700236 mirror::ArtMethod* GetResolvedMethod(uint32_t method_idx, mirror::ArtMethod* referrer)
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
238 mirror::ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, mirror::ArtMethod** referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700239 InvokeType type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom16192862011-09-12 17:50:06 -0700241
Mathieu Chartierc7853442015-03-27 14:35:38 -0700242 ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700244 ArtField* GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache)
245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
246 ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer, bool is_static)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700248
Brian Carlstrom16192862011-09-12 17:50:06 -0700249 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700250 // result in DexCache. The ClassLinker and ClassLoader are used as
251 // in ResolveType. What is unique is the is_static argument which is
252 // used to determine if we are resolving a static or non-static
253 // field.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700254 ArtField* ResolveField(const DexFile& dex_file,
Brian Carlstromea46f952013-07-30 01:26:50 -0700255 uint32_t field_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700256 Handle<mirror::DexCache> dex_cache,
257 Handle<mirror::ClassLoader> class_loader,
Brian Carlstromea46f952013-07-30 01:26:50 -0700258 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700260
Ian Rogersb067ac22011-12-13 18:05:09 -0800261 // Resolve a field with a given ID from the DexFile, storing the
262 // result in DexCache. The ClassLinker and ClassLoader are used as
263 // in ResolveType. No is_static argument is provided so that Java
264 // field resolution semantics are followed.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700265 ArtField* ResolveFieldJLS(const DexFile& dex_file, uint32_t field_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700266 Handle<mirror::DexCache> dex_cache,
267 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb067ac22011-12-13 18:05:09 -0800269
Ian Rogersad25ac52011-10-04 19:13:33 -0700270 // Get shorty from method index without resolution. Used to do handlerization.
Brian Carlstromea46f952013-07-30 01:26:50 -0700271 const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad25ac52011-10-04 19:13:33 -0700273
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700274 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700275 // can_run_clinit=false allows the compiler to attempt to init a class,
276 // given the restriction that no <clinit> execution is possible.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700277 bool EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields,
Ian Rogers7b078e82014-09-10 14:44:24 -0700278 bool can_init_parents)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700280
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700281 // Initializes classes that have instances in the image but that have
282 // <clinit> methods so they could not be initialized by the compiler.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700283 void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700284
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 void RegisterDexFile(const DexFile& dex_file)
286 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700288 void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700290 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700291
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700292 const OatFile* RegisterOatFile(const OatFile* oat_file)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 LOCKS_EXCLUDED(dex_lock_);
Brian Carlstrom866c8622012-01-06 16:35:13 -0800294
Brian Carlstrom8a487412011-08-29 20:08:52 -0700295 const std::vector<const DexFile*>& GetBootClassPath() {
296 return boot_class_path_;
297 }
298
Sebastien Hertz0de11332015-05-13 12:14:05 +0200299 // Returns the first non-image oat file in the class path.
300 const OatFile* GetPrimaryOatFile()
301 LOCKS_EXCLUDED(dex_lock_);
302
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700303 void VisitClasses(ClassVisitor* visitor, void* arg)
Ian Rogersdbf3be02014-08-29 15:40:08 -0700304 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersdbf3be02014-08-29 15:40:08 -0700306
307 // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage
308 // so that it can visit individual classes without holding the doesn't hold the
309 // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code
310 // can race with insertion and deletion of classes while the visitor is being called.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700311 void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg)
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa2155262011-11-16 16:26:58 -0800313
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700314 void VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700315 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700317 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700318 LOCKS_EXCLUDED(dex_lock_)
319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700320
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700321 mirror::DexCache* FindDexCache(const DexFile& dex_file)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700322 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700324 bool IsDexFileRegistered(const DexFile& dex_file)
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700325 LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700326 void FixupDexCaches(mirror::ArtMethod* resolution_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700329
Richard Uhler66d874d2015-01-15 09:37:19 -0800330 // Finds or creates the oat file holding dex_location. Then loads and returns
331 // all corresponding dex files (there may be more than one dex file loaded
332 // in the case of multidex).
333 // This may return the original, unquickened dex files if the oat file could
334 // not be generated.
335 //
336 // Returns an empty vector if the dex files could not be loaded. In this
337 // case, there will be at least one error message returned describing why no
338 // dex files could not be loaded. The 'error_msgs' argument must not be
339 // null, regardless of whether there is an error or not.
340 //
341 // This method should not be called with the mutator_lock_ held, because it
342 // could end up starving GC if we need to generate or relocate any oat
343 // files.
344 std::vector<std::unique_ptr<const DexFile>> OpenDexFilesFromOat(
345 const char* dex_location, const char* oat_location,
346 std::vector<std::string>* error_msgs)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700347 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
jeffhaof6174e82012-01-31 16:14:17 -0800348
Ian Rogersc0542af2014-09-03 16:16:56 -0700349 // Allocate an instance of a java.lang.Object.
350 mirror::Object* AllocObject(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
351
Elliott Hughes418d20f2011-09-22 14:00:39 -0700352 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700353 template <class T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700356
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700358 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao98eacac2011-09-14 16:11:53 -0700359
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700361 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362
Brian Carlstromea46f952013-07-30 01:26:50 -0700363 mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800364 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
365
366 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
368
Mathieu Chartierc7853442015-03-27 14:35:38 -0700369 ArtField* AllocArtFieldArray(Thread* self, size_t length)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
371
372 mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
373 size_t length)
374 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
375
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700376 void VerifyClass(Thread* self, Handle<mirror::Class> klass)
Ian Rogers7b078e82014-09-10 14:44:24 -0700377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
379 mirror::Class::Status& oat_file_class_status)
380 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800381 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700382 Handle<mirror::Class> klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -0700384 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
386
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700387 mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
388 jobjectArray interfaces, jobject loader, jobjectArray methods,
389 jobjectArray throws)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800391 std::string GetDescriptorForProxy(mirror::Class* proxy_class)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800393 mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
394 mirror::ArtMethod* proxy_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700396 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400397
Ian Rogers19846512012-02-24 11:42:47 -0800398 // Get the oat code for a method when its class isn't yet initialized
Ian Rogersef7d42f2014-01-06 12:55:46 -0800399 const void* GetQuickOatCodeFor(mirror::ArtMethod* method)
400 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800401
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700402 // Get the oat code for a method from a method index.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800403 const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
404 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700405
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700406 // Get compiled code for a method, return null if no code
407 // exists. This is unlike Get..OatCodeFor which will return a bridge
408 // or interpreter entrypoint.
409 const void* GetOatMethodQuickCodeFor(mirror::ArtMethod* method)
410 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700411
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700412 pid_t GetClassesLockOwner(); // For SignalCatcher.
413 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700414
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700415 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700416
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700417 static const char* GetClassRootDescriptor(ClassRoot class_root);
Andreas Gampe2da88232014-02-27 12:26:20 -0800418
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700419 // Is the given entry point quick code to run the resolution stub?
420 bool IsQuickResolutionStub(const void* entry_point) const;
Jeff Hao88474b42013-10-23 16:24:40 -0700421
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700422 // Is the given entry point quick code to bridge into the interpreter?
423 bool IsQuickToInterpreterBridge(const void* entry_point) const;
424
425 // Is the given entry point quick code to run the generic JNI stub?
426 bool IsQuickGenericJniStub(const void* entry_point) const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100427
Jeff Hao88474b42013-10-23 16:24:40 -0700428 InternTable* GetInternTable() const {
429 return intern_table_;
430 }
431
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700432 // Set the entrypoints up for method to the given code.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800433 void SetEntryPointsToCompiledCode(mirror::ArtMethod* method, const void* method_code) const
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700434 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
435
436 // Set the entrypoints up for method to the enter the interpreter.
437 void SetEntryPointsToInterpreter(mirror::ArtMethod* method) const
438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
439
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700440 // Attempts to insert a class into a class table. Returns null if
Ian Rogers848871b2013-08-05 10:56:33 -0700441 // the class was inserted, otherwise returns an existing class with
442 // the same descriptor and ClassLoader.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700443 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
Ian Rogers848871b2013-08-05 10:56:33 -0700444 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
445 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
446
Mathieu Chartier590fee92013-09-13 13:46:47 -0700447 // Special code to allocate an art method, use this instead of class->AllocObject.
448 mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700449
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700450 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700451 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700452 DCHECK(class_roots != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700453 return class_roots;
454 }
455
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800456 // Move all of the image classes into the class table for faster lookups.
457 void MoveImageClassesToClassTable()
458 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
460 // Move the class table to the pre-zygote table to reduce memory usage. This works by ensuring
461 // that no more classes are ever added to the pre zygote table which makes it that the pages
462 // always remain shared dirty instead of private dirty.
463 void MoveClassTableToPreZygote()
464 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
466
Sebastien Hertz6963e442014-11-26 22:11:27 +0100467 // Returns true if the method can be called with its direct code pointer, false otherwise.
468 bool MayBeCalledWithDirectCodePointer(mirror::ArtMethod* m)
469 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
470
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700471 // Creates a GlobalRef PathClassLoader that can be used to load classes from the given dex files.
472 // Note: the objects are not completely set up. Do not use this outside of tests and the compiler.
473 jobject CreatePathClassLoader(Thread* self, std::vector<const DexFile*>& dex_files)
474 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
475
Mathieu Chartier590fee92013-09-13 13:46:47 -0700476 private:
Mathieu Chartiera89d7ed2014-12-05 10:57:13 -0800477 static void InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg)
478 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
479
Ian Rogers97b52f82014-08-14 11:34:07 -0700480 const OatFile::OatMethod FindOatMethodFor(mirror::ArtMethod* method, bool* found)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700481 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa12785321912012-04-01 15:24:56 -0700482
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700483 OatFile& GetImageOatFile(gc::space::ImageSpace* space)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700486
Ian Rogers98379392014-02-24 16:53:16 -0800487 void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700488
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700489 // For early bootstrapping by Init
Ian Rogers6fac4472014-02-25 17:01:10 -0800490 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700491 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700492
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493 // Alloc* convenience functions to avoid needing to pass in mirror::Class*
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700494 // values that are known to the ClassLinker such as
495 // kObjectArrayClass and kJavaLangString etc.
Ian Rogers6fac4472014-02-25 17:01:10 -0800496 mirror::Class* AllocClass(Thread* self, uint32_t class_size)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700500
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800501 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
503 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700504 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700505
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700506
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800507 mirror::Class* CreateArrayClass(Thread* self, const char* descriptor, size_t hash,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700508 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700509 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700510
Ian Rogers7b078e82014-09-10 14:44:24 -0700511 void AppendToBootClassPath(Thread* self, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700512 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700513 void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700514 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700515
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700516 // Precomputes size needed for Class, in the case of a non-temporary class this size must be
517 // sufficient to hold all static fields.
518 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
519 const DexFile::ClassDef& dex_class_def);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700520
Mathieu Chartierc7853442015-03-27 14:35:38 -0700521 // Setup the classloader, class def index, type idx so that we can insert this class in the class
522 // table.
523 void SetupClass(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
524 Handle<mirror::Class> klass, mirror::ClassLoader* class_loader)
525 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
526
Ian Rogers7b078e82014-09-10 14:44:24 -0700527 void LoadClass(Thread* self, const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700528 Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700529 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers13735952014-10-08 12:43:28 -0700530 void LoadClassMembers(Thread* self, const DexFile& dex_file, const uint8_t* class_data,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700531 Handle<mirror::Class> klass, const OatFile::OatClass* oat_class)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100532 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700533
Mathieu Chartierc7853442015-03-27 14:35:38 -0700534 void LoadField(const ClassDataItemIterator& it, Handle<mirror::Class> klass,
535 ArtField* dst)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700537
Brian Carlstromea46f952013-07-30 01:26:50 -0700538 mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
539 const ClassDataItemIterator& dex_method,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700540 Handle<mirror::Class> klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700542
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800543 void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800544
Ian Rogers97b52f82014-08-14 11:34:07 -0700545 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
546 // error and sets found to false.
547 OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found)
Ian Rogers33e95662013-05-20 20:29:14 -0700548 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800549
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700550 void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700551 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700552 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700553 bool IsDexFileRegisteredLocked(const DexFile& dex_file)
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700554 SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700555
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700556 bool InitializeClass(Thread* self, Handle<mirror::Class> klass, bool can_run_clinit,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800557 bool can_init_parents)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700558 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700559 bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800560 ObjectLock<mirror::Class>& lock);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700561 bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700563
Ian Rogers98379392014-02-24 16:53:16 -0800564 bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700565 Handle<mirror::ClassLoader> class_loader1,
566 Handle<mirror::ClassLoader> class_loader2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700567 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568
Ian Rogers98379392014-02-24 16:53:16 -0800569 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800570 mirror::Class* klass1,
571 mirror::Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700572 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700573
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700574 bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass,
575 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
Hiroshi Yamauchi679b1cf2015-05-21 12:05:27 -0700576 MutableHandle<mirror::Class>* h_new_class_out)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700577 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700578
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700579 bool LinkSuperClass(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700581
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700582 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700583 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700584
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700585 bool LinkMethods(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700586 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
587 StackHandleScope<mirror::Class::kImtSize>* out_imt)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700588 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700589
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700590 bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700591 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700592
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700593 bool LinkInterfaceMethods(Thread* const self, Handle<mirror::Class> klass,
594 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
595 StackHandleScope<mirror::Class::kImtSize>* out_imt)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700596 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700597
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700598 bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700600 bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700602 bool LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, size_t* class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700603 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700604 void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700605 uint32_t class_def_method_index)
Dmitry Petrochenkof0972a42014-05-16 17:43:39 +0700606 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700607 void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700608 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700609
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700610 // For use by ImageWriter to find DexCaches for its roots
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700611 ReaderWriterMutex* DexLock()
612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
613 return &dex_lock_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700614 }
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700615 size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
616 return dex_caches_.size();
617 }
618 mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700619
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700621 LOCKS_EXCLUDED(dex_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700622
Sebastien Hertz0de11332015-05-13 12:14:05 +0200623 // Returns the boot image oat file.
624 const OatFile* GetBootOatFile() SHARED_LOCKS_REQUIRED(dex_lock_);
625
Hiroshi Yamauchi679b1cf2015-05-21 12:05:27 -0700626 mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700627 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700628 mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
629 Handle<mirror::ArtMethod> prototype)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400631
Andreas Gampe48498592014-09-10 19:48:05 -0700632 // Ensures that methods have the kAccPreverified bit set. We use the kAccPreverfied bit on the
633 // class access flags to determine whether this has been done before.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700634 void EnsurePreverifiedMethods(Handle<mirror::Class> c)
Andreas Gampe48498592014-09-10 19:48:05 -0700635 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
636
Ian Rogersdbf3be02014-08-29 15:40:08 -0700637 mirror::Class* LookupClassFromTableLocked(const char* descriptor,
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800638 mirror::ClassLoader* class_loader,
Ian Rogersdbf3be02014-08-29 15:40:08 -0700639 size_t hash)
640 SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
641
642 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
643 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
645
Ian Rogersdbf3be02014-08-29 15:40:08 -0700646 mirror::Class* LookupClassFromImage(const char* descriptor)
647 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
648
649 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
650 // before returning it to the caller. Its the responsibility of the thread that placed the class
651 // in the table to make it resolved. The thread doing resolution must notify on the class' lock
652 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
653 // retire a class, the version of the class in the table is returned and this may differ from
654 // the class passed in.
655 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
656 WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
657
658 void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
659 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
660
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700661 void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
663
664 // Return the quick generic JNI stub for testing.
665 const void* GetRuntimeQuickGenericJniStub() const;
666
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700667 // Throw the class initialization failure recorded when first trying to initialize the given
668 // class.
669 // Note: Currently we only store the descriptor, so we cannot throw the exact throwable, only
670 // a recreation with a custom string.
671 void ThrowEarlierClassFailure(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
672
Andreas Gampeb9aec2c2015-04-23 22:23:47 -0700673 // Check for duplicate class definitions of the given oat file against all open oat files.
674 bool HasCollisions(const OatFile* oat_file, std::string* error_msg) LOCKS_EXCLUDED(dex_lock_);
675
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700676 std::vector<const DexFile*> boot_class_path_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800677 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700678
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700679 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700680 std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700681 std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700682 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700683
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800684 class ClassDescriptorHashEquals {
685 public:
686 // Same class loader and descriptor.
687 std::size_t operator()(const GcRoot<mirror::Class>& root) const NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier47f867a2015-03-18 10:39:00 -0700688 bool operator()(const GcRoot<mirror::Class>& a, const GcRoot<mirror::Class>& b) const
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800689 NO_THREAD_SAFETY_ANALYSIS;
690 // Same class loader and descriptor.
691 std::size_t operator()(const std::pair<const char*, mirror::ClassLoader*>& element) const
692 NO_THREAD_SAFETY_ANALYSIS;
693 bool operator()(const GcRoot<mirror::Class>& a,
Mathieu Chartier47f867a2015-03-18 10:39:00 -0700694 const std::pair<const char*, mirror::ClassLoader*>& b) const
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800695 NO_THREAD_SAFETY_ANALYSIS;
696 // Same descriptor.
Mathieu Chartier47f867a2015-03-18 10:39:00 -0700697 bool operator()(const GcRoot<mirror::Class>& a, const char* descriptor) const
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800698 NO_THREAD_SAFETY_ANALYSIS;
699 std::size_t operator()(const char* descriptor) const NO_THREAD_SAFETY_ANALYSIS;
700 };
701 class GcRootEmptyFn {
702 public:
703 void MakeEmpty(GcRoot<mirror::Class>& item) const {
704 item = GcRoot<mirror::Class>();
705 }
706 bool IsEmpty(const GcRoot<mirror::Class>& item) const {
707 return item.IsNull();
708 }
709 };
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700710
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800711 // hash set which hashes class descriptor, and compares descriptors nad class loaders. Results
712 // should be compared for a matching Class descriptor and class loader.
713 typedef HashSet<GcRoot<mirror::Class>, GcRootEmptyFn, ClassDescriptorHashEquals,
714 ClassDescriptorHashEquals, TrackingAllocator<GcRoot<mirror::Class>, kAllocatorTagClassTable>>
715 Table;
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700716 // This contains strong roots. To enable concurrent root scanning of
717 // the class table, be careful to use a read barrier when accessing this.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700718 Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800719 Table pre_zygote_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
720 std::vector<GcRoot<mirror::Class>> new_class_roots_;
Elliott Hughesf8349362012-06-18 15:00:06 -0700721
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700722 // Do we need to search dex caches to find image classes?
723 bool dex_cache_image_class_lookup_required_;
724 // Number of times we've searched dex caches for a class. After a certain number of misses we move
725 // the classes into the class_table_ to avoid dex cache based searches.
Ian Rogers68b56852014-08-29 20:19:11 -0700726 Atomic<uint32_t> failed_dex_cache_class_lookups_;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700727
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700728 // Well known mirror::Class roots.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700729 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700730
Ian Rogers98379392014-02-24 16:53:16 -0800731 // The interface table used by all arrays.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700732 GcRoot<mirror::IfTable> array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700733
Ian Rogers98379392014-02-24 16:53:16 -0800734 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
735 // descriptors for the sake of performing FindClass.
736 static constexpr size_t kFindArrayCacheSize = 16;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700737 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
Ian Rogers98379392014-02-24 16:53:16 -0800738 size_t find_array_class_cache_next_victim_;
739
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700740 bool init_done_;
Mathieu Chartier893263b2014-03-04 11:07:42 -0800741 bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
742 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700743
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700744 InternTable* intern_table_;
745
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700746 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
747 // patch point within the image. TODO: make these proper relocations.
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700748 const void* quick_resolution_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700749 const void* quick_imt_conflict_trampoline_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800750 const void* quick_generic_jni_trampoline_;
Vladimir Marko8a630572014-04-09 18:45:35 +0100751 const void* quick_to_interpreter_bridge_trampoline_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700752
Mathieu Chartier2d721012014-11-10 11:08:06 -0800753 // Image pointer size.
754 size_t image_pointer_size_;
755
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700756 friend class ImageWriter; // for GetClassRoots
Alex Lighta59dd802014-07-02 16:28:08 -0700757 friend class ImageDumper; // for FindOpenedOatFileFromOatLocation
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700758 friend class JniCompilerTest; // for GetRuntimeQuickGenericJniStub
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700759 ART_FRIEND_TEST(mirror::DexCacheTest, Open); // for AllocDexCache
760
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700761 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
762};
763
764} // namespace art
765
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700766#endif // ART_RUNTIME_CLASS_LINKER_H_