blob: f073cd8170cc5e563a504d7e5c8438f3950f0210 [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
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include <string>
Alex Lighteb7c1442015-08-31 13:17:42 -070021#include <unordered_map>
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"
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -070029#include "class_table.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070030#include "dex_file.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070031#include "gc_root.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070032#include "jni.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070033#include "oat_file.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080034#include "object_callbacks.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035
36namespace art {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037
Ian Rogers1d54e732013-05-02 21:10:01 -070038namespace gc {
39namespace space {
40 class ImageSpace;
41} // namespace space
42} // namespace gc
Igor Murashkin457e8742015-10-22 17:37:50 -070043
44namespace lambda {
45 class BoxClassTable;
46} // namespace lambda
47
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048namespace mirror {
Ian Rogers33e95662013-05-20 20:29:14 -070049 class ClassLoader;
50 class DexCache;
Mathieu Chartiere401d142015-04-22 13:56:20 -070051 class DexCachePointerArray;
Ian Rogers33e95662013-05-20 20:29:14 -070052 class DexCacheTest_Open_Test;
53 class IfTable;
54 template<class T> class ObjectArray;
55 class StackTraceElement;
56} // namespace mirror
Ian Rogers1d54e732013-05-02 21:10:01 -070057
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070058template<class T> class Handle;
Hiroshi Yamauchi679b1cf2015-05-21 12:05:27 -070059template<class T> class MutableHandle;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060class InternTable;
Mathieu Chartierc528dba2013-11-26 12:00:11 -080061template<class T> class ObjectLock;
Andreas Gampe7ba64962014-10-23 11:37:40 -070062class Runtime;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070063class ScopedObjectAccessAlreadyRunnable;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070064template<size_t kNumReferences> class PACKED(4) StackHandleScope;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070065
Mathieu Chartier893263b2014-03-04 11:07:42 -080066enum VisitRootFlags : uint8_t;
67
Mathieu Chartier9b1c71e2015-09-02 18:51:54 -070068class ClassLoaderVisitor {
69 public:
70 virtual ~ClassLoaderVisitor() {}
71 virtual void Visit(mirror::ClassLoader* class_loader)
72 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_) = 0;
73};
74
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070075class ClassLinker {
76 public:
Ian Rogers6f3dbba2014-10-14 17:41:57 -070077 // Well known mirror::Class roots accessed via GetClassRoot.
78 enum ClassRoot {
79 kJavaLangClass,
80 kJavaLangObject,
81 kClassArrayClass,
82 kObjectArrayClass,
83 kJavaLangString,
84 kJavaLangDexCache,
85 kJavaLangRefReference,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070086 kJavaLangReflectConstructor,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070087 kJavaLangReflectField,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070088 kJavaLangReflectMethod,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070089 kJavaLangReflectProxy,
Igor Murashkin457e8742015-10-22 17:37:50 -070090 kJavaLangLambdaProxy,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070091 kJavaLangStringArrayClass,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070092 kJavaLangReflectConstructorArrayClass,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070093 kJavaLangReflectFieldArrayClass,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070094 kJavaLangReflectMethodArrayClass,
Ian Rogers6f3dbba2014-10-14 17:41:57 -070095 kJavaLangClassLoader,
96 kJavaLangThrowable,
97 kJavaLangClassNotFoundException,
98 kJavaLangStackTraceElement,
99 kPrimitiveBoolean,
100 kPrimitiveByte,
101 kPrimitiveChar,
102 kPrimitiveDouble,
103 kPrimitiveFloat,
104 kPrimitiveInt,
105 kPrimitiveLong,
106 kPrimitiveShort,
107 kPrimitiveVoid,
108 kBooleanArrayClass,
109 kByteArrayClass,
110 kCharArrayClass,
111 kDoubleArrayClass,
112 kFloatArrayClass,
113 kIntArrayClass,
114 kLongArrayClass,
115 kShortArrayClass,
116 kJavaLangStackTraceElementArrayClass,
117 kClassRootsMax,
118 };
119
Mathieu Chartier590fee92013-09-13 13:46:47 -0700120 explicit ClassLinker(InternTable* intern_table);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700121 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -0700122
Alex Light64ad14d2014-08-19 14:23:13 -0700123 // Initialize class linker by bootstraping from dex files.
Andreas Gampe3db9c5d2015-11-17 11:52:46 -0800124 bool InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path,
125 std::string* error_msg)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700126 SHARED_REQUIRES(Locks::mutator_lock_)
127 REQUIRES(!dex_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700128
129 // Initialize class linker from one or more images.
Andreas Gampe3db9c5d2015-11-17 11:52:46 -0800130 bool InitFromImage(std::string* error_msg)
131 SHARED_REQUIRES(Locks::mutator_lock_)
132 REQUIRES(!dex_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700134 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700135 // If class_loader is null, searches boot_class_path_.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700136 mirror::Class* FindClass(Thread* self,
137 const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700138 Handle<mirror::ClassLoader> class_loader)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700139 SHARED_REQUIRES(Locks::mutator_lock_)
140 REQUIRES(!dex_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700141
Andreas Gampef865ea92015-04-13 22:14:19 -0700142 // Finds a class in the path class loader, loading it if necessary without using JNI. Hash
143 // function is supposed to be ComputeModifiedUtf8Hash(descriptor). Returns true if the
144 // class-loader chain could be handled, false otherwise, i.e., a non-supported class-loader
145 // was encountered while walking the parent chain (currently only BootClassLoader and
146 // PathClassLoader are supported).
147 bool FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700148 Thread* self,
149 const char* descriptor,
150 size_t hash,
Andreas Gampef865ea92015-04-13 22:14:19 -0700151 Handle<mirror::ClassLoader> class_loader,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700152 mirror::Class** result)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700153 SHARED_REQUIRES(Locks::mutator_lock_)
154 REQUIRES(!dex_lock_);
Mathieu Chartierab0ed822014-09-11 14:21:41 -0700155
Ian Rogers98379392014-02-24 16:53:16 -0800156 // Finds a class by its descriptor using the "system" class loader, ie by searching the
157 // boot_class_path_.
158 mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700159 SHARED_REQUIRES(Locks::mutator_lock_)
160 REQUIRES(!dex_lock_);
Ian Rogers98379392014-02-24 16:53:16 -0800161
162 // Finds the array class given for the element class.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700163 mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700164 SHARED_REQUIRES(Locks::mutator_lock_)
165 REQUIRES(!dex_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166
Ian Rogers63557452014-06-04 16:57:15 -0700167 // Returns true if the class linker is initialized.
Ian Rogers7b078e82014-09-10 14:44:24 -0700168 bool IsInitialized() const {
169 return init_done_;
170 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700171
Brian Carlstromaded5f72011-10-07 17:15:04 -0700172 // Define a new a class based on a ClassDef from a DexFile
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700173 mirror::Class* DefineClass(Thread* self,
174 const char* descriptor,
175 size_t hash,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700176 Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700177 const DexFile& dex_file,
178 const DexFile::ClassDef& dex_class_def)
179 SHARED_REQUIRES(Locks::mutator_lock_)
180 REQUIRES(!dex_lock_);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700181
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700182 // Finds a class by its descriptor, returning null if it isn't wasn't loaded
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700183 // by the given 'class_loader'.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700184 mirror::Class* LookupClass(Thread* self,
185 const char* descriptor,
186 size_t hash,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700187 mirror::ClassLoader* class_loader)
Mathieu Chartier90443472015-07-16 20:32:27 -0700188 REQUIRES(!Locks::classlinker_classes_lock_)
189 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700190
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800191 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700192 void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 REQUIRES(!Locks::classlinker_classes_lock_)
194 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800195
Mathieu Chartier90443472015-07-16 20:32:27 -0700196 mirror::Class* FindPrimitiveClass(char type) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700197
Brian Carlstromae826982011-11-09 01:33:42 -0800198 // General class unloading is not supported, this is used to prune
199 // unwanted classes during image writing.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700200 bool RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700201 REQUIRES(!Locks::classlinker_classes_lock_)
202 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800203
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700204 void DumpAllClasses(int flags)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700205 REQUIRES(!Locks::classlinker_classes_lock_)
206 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700207
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700208 void DumpForSigQuit(std::ostream& os) REQUIRES(!Locks::classlinker_classes_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -0700209
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700210 size_t NumLoadedClasses()
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700211 REQUIRES(!Locks::classlinker_classes_lock_)
212 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -0700213
Brian Carlstromb63ec392011-08-27 17:38:27 -0700214 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -0700215 // result in the DexCache. The referrer is used to identify the
216 // target DexCache and ClassLoader to use for resolution.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 mirror::String* ResolveString(uint32_t string_idx, ArtMethod* referrer)
Mathieu Chartier90443472015-07-16 20:32:27 -0700218 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700219
220 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700221 // result in the DexCache.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700222 mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700223 Handle<mirror::DexCache> dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700224 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700225
Brian Carlstromb63ec392011-08-27 17:38:27 -0700226 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700227 // result in the DexCache. The referrer is used to identity the
228 // target DexCache and ClassLoader to use for resolution.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700229 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700230 SHARED_REQUIRES(Locks::mutator_lock_)
231 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700232
Brian Carlstromb63ec392011-08-27 17:38:27 -0700233 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700235 // target DexCache and ClassLoader to use for resolution.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700236 mirror::Class* ResolveType(uint16_t type_idx, ArtMethod* referrer)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700237 SHARED_REQUIRES(Locks::mutator_lock_)
238 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700240 mirror::Class* ResolveType(uint16_t type_idx, ArtField* referrer)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700241 SHARED_REQUIRES(Locks::mutator_lock_)
242 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700243
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700244 // Resolve a type with the given ID from the DexFile, storing the
245 // result in DexCache. The ClassLoader is used to search for the
246 // type, since it may be referenced from but not contained within
247 // the given DexFile.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700248 mirror::Class* ResolveType(const DexFile& dex_file,
249 uint16_t type_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700250 Handle<mirror::DexCache> dex_cache,
251 Handle<mirror::ClassLoader> class_loader)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700252 SHARED_REQUIRES(Locks::mutator_lock_)
253 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700254
255 // Resolve a method with a given ID from the DexFile, storing the
256 // result in DexCache. The ClassLinker and ClassLoader are used as
257 // in ResolveType. What is unique is the method type argument which
258 // is used to determine if this method is a direct, static, or
259 // virtual method.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700260 ArtMethod* ResolveMethod(const DexFile& dex_file,
261 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 Handle<mirror::DexCache> dex_cache,
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700263 Handle<mirror::ClassLoader> class_loader,
264 ArtMethod* referrer,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 InvokeType type)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700266 SHARED_REQUIRES(Locks::mutator_lock_)
267 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700268
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 ArtMethod* GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer)
Mathieu Chartier90443472015-07-16 20:32:27 -0700270 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, ArtMethod* referrer, InvokeType type)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700272 SHARED_REQUIRES(Locks::mutator_lock_)
273 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Jeff Hao0042c6d2015-07-29 20:14:10 -0700274 ArtMethod* ResolveMethodWithoutInvokeType(const DexFile& dex_file,
275 uint32_t method_idx,
276 Handle<mirror::DexCache> dex_cache,
277 Handle<mirror::ClassLoader> class_loader)
278 SHARED_REQUIRES(Locks::mutator_lock_)
279 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstrom16192862011-09-12 17:50:06 -0700280
Mathieu Chartierc7853442015-03-27 14:35:38 -0700281 ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700283 ArtField* GetResolvedField(uint32_t field_idx, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700284 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700285 ArtField* ResolveField(uint32_t field_idx, ArtMethod* referrer, bool is_static)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700286 SHARED_REQUIRES(Locks::mutator_lock_)
287 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700288
Brian Carlstrom16192862011-09-12 17:50:06 -0700289 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700290 // result in DexCache. The ClassLinker and ClassLoader are used as
291 // in ResolveType. What is unique is the is_static argument which is
292 // used to determine if we are resolving a static or non-static
293 // field.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700294 ArtField* ResolveField(const DexFile& dex_file, uint32_t field_idx,
Mathieu Chartier90443472015-07-16 20:32:27 -0700295 Handle<mirror::DexCache> dex_cache,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700296 Handle<mirror::ClassLoader> class_loader, bool is_static)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700297 SHARED_REQUIRES(Locks::mutator_lock_)
298 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700299
Ian Rogersb067ac22011-12-13 18:05:09 -0800300 // Resolve a field with a given ID from the DexFile, storing the
301 // result in DexCache. The ClassLinker and ClassLoader are used as
302 // in ResolveType. No is_static argument is provided so that Java
303 // field resolution semantics are followed.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700304 ArtField* ResolveFieldJLS(const DexFile& dex_file,
305 uint32_t field_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 Handle<mirror::DexCache> dex_cache,
307 Handle<mirror::ClassLoader> class_loader)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700308 SHARED_REQUIRES(Locks::mutator_lock_)
309 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Ian Rogersb067ac22011-12-13 18:05:09 -0800310
Ian Rogersad25ac52011-10-04 19:13:33 -0700311 // Get shorty from method index without resolution. Used to do handlerization.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 const char* MethodShorty(uint32_t method_idx, ArtMethod* referrer, uint32_t* length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700313 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersad25ac52011-10-04 19:13:33 -0700314
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700315 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700316 // can_run_clinit=false allows the compiler to attempt to init a class,
317 // given the restriction that no <clinit> execution is possible.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700318 bool EnsureInitialized(Thread* self,
319 Handle<mirror::Class> c,
320 bool can_init_fields,
Ian Rogers7b078e82014-09-10 14:44:24 -0700321 bool can_init_parents)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700322 SHARED_REQUIRES(Locks::mutator_lock_)
323 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700324
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700325 // Initializes classes that have instances in the image but that have
326 // <clinit> methods so they could not be initialized by the compiler.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700327 void RunRootClinits()
328 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700329 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700330
Mathieu Chartierd57d4542015-10-14 10:55:30 -0700331 mirror::DexCache* RegisterDexFile(const DexFile& dex_file, LinearAlloc* linear_alloc)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700332 REQUIRES(!dex_lock_)
333 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700334 void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700335 REQUIRES(!dex_lock_)
336 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700337
Brian Carlstrom8a487412011-08-29 20:08:52 -0700338 const std::vector<const DexFile*>& GetBootClassPath() {
339 return boot_class_path_;
340 }
341
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700342 void VisitClasses(ClassVisitor* visitor)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700343 REQUIRES(!Locks::classlinker_classes_lock_)
344 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersdbf3be02014-08-29 15:40:08 -0700345
346 // Less efficient variant of VisitClasses that copies the class_table_ into secondary storage
347 // so that it can visit individual classes without holding the doesn't hold the
348 // Locks::classlinker_classes_lock_. As the Locks::classlinker_classes_lock_ isn't held this code
349 // can race with insertion and deletion of classes while the visitor is being called.
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700350 void VisitClassesWithoutClassesLock(ClassVisitor* visitor)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700351 SHARED_REQUIRES(Locks::mutator_lock_)
352 REQUIRES(!dex_lock_);
Elliott Hughesa2155262011-11-16 16:26:58 -0800353
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700354 void VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700355 REQUIRES(!Locks::classlinker_classes_lock_)
356 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700357 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700358 REQUIRES(!dex_lock_)
359 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700360
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700361 mirror::DexCache* FindDexCache(Thread* self,
362 const DexFile& dex_file,
363 bool allow_failure = false)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700364 REQUIRES(!dex_lock_)
365 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 void FixupDexCaches(ArtMethod* resolution_method)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700367 REQUIRES(!dex_lock_)
368 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700369
Ian Rogersc0542af2014-09-03 16:16:56 -0700370 // Allocate an instance of a java.lang.Object.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700371 mirror::Object* AllocObject(Thread* self)
372 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700373 REQUIRES(!Roles::uninterruptible_);
Ian Rogersc0542af2014-09-03 16:16:56 -0700374
Elliott Hughes418d20f2011-09-22 14:00:39 -0700375 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700376 template <class T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700378 SHARED_REQUIRES(Locks::mutator_lock_)
379 REQUIRES(!Roles::uninterruptible_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700380
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800381 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700382 SHARED_REQUIRES(Locks::mutator_lock_)
383 REQUIRES(!Roles::uninterruptible_);
jeffhao98eacac2011-09-14 16:11:53 -0700384
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700386 SHARED_REQUIRES(Locks::mutator_lock_)
387 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700388
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700389 LengthPrefixedArray<ArtField>* AllocArtFieldArray(Thread* self,
390 LinearAlloc* allocator,
391 size_t length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700393 LengthPrefixedArray<ArtMethod>* AllocArtMethodArray(Thread* self,
394 LinearAlloc* allocator,
395 size_t length);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396
397 mirror::PointerArray* AllocPointerArray(Thread* self, size_t length)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700398 SHARED_REQUIRES(Locks::mutator_lock_)
399 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400
401 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700402 SHARED_REQUIRES(Locks::mutator_lock_)
403 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800404
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700405 mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
406 size_t length)
407 SHARED_REQUIRES(Locks::mutator_lock_)
408 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700410 void VerifyClass(Thread* self, Handle<mirror::Class> klass)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700411 SHARED_REQUIRES(Locks::mutator_lock_)
412 REQUIRES(!dex_lock_);
413 bool VerifyClassUsingOatFile(const DexFile& dex_file,
414 mirror::Class* klass,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800415 mirror::Class::Status& oat_file_class_status)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700416 SHARED_REQUIRES(Locks::mutator_lock_)
417 REQUIRES(!dex_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800418 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700419 Handle<mirror::Class> klass)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700420 SHARED_REQUIRES(Locks::mutator_lock_)
421 REQUIRES(!dex_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, ArtMethod* klass)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700423 SHARED_REQUIRES(Locks::mutator_lock_)
424 REQUIRES(!dex_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700426 mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa,
427 jstring name,
428 jobjectArray interfaces,
429 jobject loader,
430 jobjectArray methods,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700431 jobjectArray throws)
Mathieu Chartier90443472015-07-16 20:32:27 -0700432 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkin457e8742015-10-22 17:37:50 -0700433
434 // Get the long type descriptor, e.g. "LProxyName$1234;" for the requested proxy class.
435 static std::string GetDescriptorForAnyProxy(mirror::Class* proxy_class)
Mathieu Chartier90443472015-07-16 20:32:27 -0700436 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700437 ArtMethod* FindMethodForProxy(mirror::Class* proxy_class, ArtMethod* proxy_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700438 REQUIRES(!dex_lock_)
439 SHARED_REQUIRES(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400440
Igor Murashkin457e8742015-10-22 17:37:50 -0700441 // Create a lambda proxy class.
442 // -- Nominally used when boxing an innate lambda, since that has no corresponding class.
443 //
444 // * name must be a fully-qualified class name (and dotted), e.g. "java.lang.Runnable"
445 // * interfaces is an array of java.lang.Class for interfaces that will be the supertype
446 // (note that there must be exactly 1 element here for a lambda interface since lambda
447 // types can only target 1 interface).
448 // * loader must be a java.lang.ClassLoader where the proxy class will be created
449 // * methods must be an array of java.lang.reflect.Method that consists of the
450 // deduplicated methods from all of the interfaces specified.
451 // * throws must be an array of java.lang.Class[] where each index corresponds to that of
452 // methods, and it signifies the "throws" keyword of each method
453 // (this is not directly used by the runtime itself, but it is available via reflection).
454 //
455 // Returns a non-null pointer to a class upon success, otherwise null and throws an exception.
456 //
457 // If the class was already created previously (with the same name but potentially different
458 // parameters), already_exists is set to true; otherwise already_exists is set to false.
459 // The already_exists value is undefined when an exception was thrown.
460 //
461 // Sidenote: interfaces is an array to simplify the libcore code which creates a Java
462 // array in an attempt to reduce code duplication.
463 // TODO: this should probably also take the target single-abstract-method as well.
464 mirror::Class* CreateLambdaProxyClass(ScopedObjectAccessAlreadyRunnable& soa,
465 jstring name,
466 jobjectArray interfaces,
467 jobject loader,
468 jobjectArray methods,
469 jobjectArray throws,
470 /*out*/bool* already_exists)
471 SHARED_REQUIRES(Locks::mutator_lock_);
472
Ian Rogers19846512012-02-24 11:42:47 -0800473 // Get the oat code for a method when its class isn't yet initialized
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 const void* GetQuickOatCodeFor(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700475 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800476
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700477 // Get the oat code for a method from a method index.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700478 const void* GetQuickOatCodeFor(const DexFile& dex_file,
479 uint16_t class_def_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700480 uint32_t method_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700481 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700482
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700483 // Get compiled code for a method, return null if no code
484 // exists. This is unlike Get..OatCodeFor which will return a bridge
485 // or interpreter entrypoint.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486 const void* GetOatMethodQuickCodeFor(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700487 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700488
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700489 const OatFile::OatMethod FindOatMethodFor(ArtMethod* method, bool* found)
Mathieu Chartier90443472015-07-16 20:32:27 -0700490 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000491
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700492 pid_t GetClassesLockOwner(); // For SignalCatcher.
493 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700494
Mathieu Chartier90443472015-07-16 20:32:27 -0700495 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700496
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700497 static const char* GetClassRootDescriptor(ClassRoot class_root);
Andreas Gampe2da88232014-02-27 12:26:20 -0800498
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700499 // Is the given entry point quick code to run the resolution stub?
500 bool IsQuickResolutionStub(const void* entry_point) const;
Jeff Hao88474b42013-10-23 16:24:40 -0700501
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700502 // Is the given entry point quick code to bridge into the interpreter?
503 bool IsQuickToInterpreterBridge(const void* entry_point) const;
504
505 // Is the given entry point quick code to run the generic JNI stub?
506 bool IsQuickGenericJniStub(const void* entry_point) const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100507
Jeff Hao88474b42013-10-23 16:24:40 -0700508 InternTable* GetInternTable() const {
509 return intern_table_;
510 }
511
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700512 // Set the entrypoints up for method to the given code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513 void SetEntryPointsToCompiledCode(ArtMethod* method, const void* method_code) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700514 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700515
516 // Set the entrypoints up for method to the enter the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 void SetEntryPointsToInterpreter(ArtMethod* method) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700518 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700519
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700520 // Attempts to insert a class into a class table. Returns null if
Ian Rogers848871b2013-08-05 10:56:33 -0700521 // the class was inserted, otherwise returns an existing class with
522 // the same descriptor and ClassLoader.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700523 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
Mathieu Chartier90443472015-07-16 20:32:27 -0700524 REQUIRES(!Locks::classlinker_classes_lock_)
525 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700526
Mathieu Chartier90443472015-07-16 20:32:27 -0700527 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700528 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700529 DCHECK(class_roots != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700530 return class_roots;
531 }
532
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800533 // Move all of the boot image classes into the class table for faster lookups.
534 void AddBootImageClassesToClassTable()
Mathieu Chartier90443472015-07-16 20:32:27 -0700535 REQUIRES(!Locks::classlinker_classes_lock_)
536 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800537
538 // Add image classes to the class table.
539 void AddImageClassesToClassTable(gc::space::ImageSpace* image_space,
540 mirror::ClassLoader* class_loader)
541 REQUIRES(!Locks::classlinker_classes_lock_)
542 SHARED_REQUIRES(Locks::mutator_lock_);
543
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800544 // Move the class table to the pre-zygote table to reduce memory usage. This works by ensuring
545 // that no more classes are ever added to the pre zygote table which makes it that the pages
546 // always remain shared dirty instead of private dirty.
547 void MoveClassTableToPreZygote()
Mathieu Chartier90443472015-07-16 20:32:27 -0700548 REQUIRES(!Locks::classlinker_classes_lock_)
549 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800550
Sebastien Hertz6963e442014-11-26 22:11:27 +0100551 // Returns true if the method can be called with its direct code pointer, false otherwise.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700552 bool MayBeCalledWithDirectCodePointer(ArtMethod* m)
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700553 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!dex_lock_);
Sebastien Hertz6963e442014-11-26 22:11:27 +0100554
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700555 // Creates a GlobalRef PathClassLoader that can be used to load classes from the given dex files.
556 // Note: the objects are not completely set up. Do not use this outside of tests and the compiler.
Mathieu Chartierd37d3642015-11-19 16:05:58 -0800557 // If parent_loader is null then we use the boot class loader.
558 jobject CreatePathClassLoader(Thread* self,
559 std::vector<const DexFile*>& dex_files,
560 jobject parent_loader)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700561 SHARED_REQUIRES(Locks::mutator_lock_)
562 REQUIRES(!dex_lock_);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700563
Mathieu Chartiere401d142015-04-22 13:56:20 -0700564 size_t GetImagePointerSize() const {
565 DCHECK(ValidPointerSize(image_pointer_size_)) << image_pointer_size_;
566 return image_pointer_size_;
567 }
568
569 // Used by image writer for checking.
570 bool ClassInClassTable(mirror::Class* klass)
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700571 REQUIRES(Locks::classlinker_classes_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700572 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiera89d7ed2014-12-05 10:57:13 -0800573
Mathieu Chartiere401d142015-04-22 13:56:20 -0700574 ArtMethod* CreateRuntimeMethod();
575
Andreas Gampe8ac75952015-06-02 21:01:45 -0700576 // Clear the ArrayClass cache. This is necessary when cleaning up for the image, as the cache
577 // entries are roots, but potentially not image classes.
Mathieu Chartier90443472015-07-16 20:32:27 -0700578 void DropFindArrayClassCache() SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe8ac75952015-06-02 21:01:45 -0700579
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700580 // Clean up class loaders, this needs to happen after JNI weak globals are cleared.
581 void CleanupClassLoaders()
Mathieu Chartier00310e02015-10-17 12:46:42 -0700582 REQUIRES(!Locks::classlinker_classes_lock_)
583 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700584
Mathieu Chartier32cc9ee2015-10-15 09:19:15 -0700585 // Unlike GetOrCreateAllocatorForClassLoader, GetAllocatorForClassLoader asserts that the
586 // allocator for this class loader is already created.
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700587 static LinearAlloc* GetAllocatorForClassLoader(mirror::ClassLoader* class_loader)
Nicolas Geoffray7de5dfe2015-09-22 09:48:02 +0000588 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700589
Mathieu Chartierd57d4542015-10-14 10:55:30 -0700590 // Return the linear alloc for a class loader if it is already allocated, otherwise allocate and
591 // set it. TODO: Consider using a lock other than classlinker_classes_lock_.
592 static LinearAlloc* GetOrCreateAllocatorForClassLoader(mirror::ClassLoader* class_loader)
Mathieu Chartier00310e02015-10-17 12:46:42 -0700593 REQUIRES(!Locks::classlinker_classes_lock_)
594 SHARED_REQUIRES(Locks::mutator_lock_);
595
596 void InsertDexFileInToClassLoader(mirror::Object* dex_file, mirror::ClassLoader* class_loader)
597 REQUIRES(!Locks::classlinker_classes_lock_)
598 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierd57d4542015-10-14 10:55:30 -0700599
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800600 struct DexCacheData {
601 // Weak root to the DexCache. Note: Do not decode this unnecessarily or else class unloading may
602 // not work properly.
603 jweak weak_root;
604 // The following two fields are caches to the DexCache's fields and here to avoid unnecessary
605 // jweak decode that triggers read barriers (and mark them alive unnecessarily and mess with
606 // class unloading.)
607 const DexFile* dex_file;
608 GcRoot<mirror::Class>* resolved_types;
609 };
610
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700611 private:
612 struct ClassLoaderData {
Mathieu Chartierc3fcd412015-09-25 16:54:59 -0700613 jweak weak_root; // Weak root to enable class unloading.
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700614 ClassTable* class_table;
615 LinearAlloc* allocator;
Igor Murashkin457e8742015-10-22 17:37:50 -0700616 lambda::BoxClassTable* lambda_box_class_table;
Mathieu Chartier951ec2c2015-09-22 08:50:05 -0700617 };
618
Alex Lightf1f10492015-10-07 16:08:36 -0700619 // Ensures that the supertype of 'klass' ('supertype') is verified. Returns false and throws
620 // appropriate exceptions if verification failed hard. Returns true for successful verification or
621 // soft-failures.
622 bool AttemptSupertypeVerification(Thread* self,
623 Handle<mirror::Class> klass,
624 Handle<mirror::Class> supertype)
625 REQUIRES(!dex_lock_)
626 SHARED_REQUIRES(Locks::mutator_lock_);
627
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100628 static void DeleteClassLoader(Thread* self, const ClassLoaderData& data)
629 REQUIRES(Locks::classlinker_classes_lock_)
630 SHARED_REQUIRES(Locks::mutator_lock_);
631
Mathieu Chartier9b1c71e2015-09-02 18:51:54 -0700632 void VisitClassLoaders(ClassLoaderVisitor* visitor) const
633 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
634
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700635 void VisitClassesInternal(ClassVisitor* visitor)
Mathieu Chartier9b1c71e2015-09-02 18:51:54 -0700636 SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700637
638 // Returns the number of zygote and image classes.
Mathieu Chartier6b069532015-08-05 15:08:12 -0700639 size_t NumZygoteClasses() const
640 REQUIRES(Locks::classlinker_classes_lock_)
641 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700642
643 // Returns the number of non zygote nor image classes.
Mathieu Chartier6b069532015-08-05 15:08:12 -0700644 size_t NumNonZygoteClasses() const
645 REQUIRES(Locks::classlinker_classes_lock_)
646 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700647
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700648 void FinishInit(Thread* self)
Mathieu Chartiera808bac2015-11-05 16:33:15 -0800649 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier4e2cb092015-07-22 16:17:51 -0700650 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700651
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700652 // For early bootstrapping by Init
Ian Rogers6fac4472014-02-25 17:01:10 -0800653 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700654 SHARED_REQUIRES(Locks::mutator_lock_)
655 REQUIRES(!Roles::uninterruptible_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700656
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800657 // Alloc* convenience functions to avoid needing to pass in mirror::Class*
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700658 // values that are known to the ClassLinker such as
659 // kObjectArrayClass and kJavaLangString etc.
Ian Rogers6fac4472014-02-25 17:01:10 -0800660 mirror::Class* AllocClass(Thread* self, uint32_t class_size)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700661 SHARED_REQUIRES(Locks::mutator_lock_)
662 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartierd57d4542015-10-14 10:55:30 -0700663 mirror::DexCache* AllocDexCache(Thread* self,
664 const DexFile& dex_file,
665 LinearAlloc* linear_alloc)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700666 SHARED_REQUIRES(Locks::mutator_lock_)
667 REQUIRES(!Roles::uninterruptible_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700668
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800669 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700670 SHARED_REQUIRES(Locks::mutator_lock_)
671 REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800672 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700673 SHARED_REQUIRES(Locks::mutator_lock_)
674 REQUIRES(!Roles::uninterruptible_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700675
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700676 mirror::Class* CreateArrayClass(Thread* self,
677 const char* descriptor,
678 size_t hash,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700679 Handle<mirror::ClassLoader> class_loader)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700680 SHARED_REQUIRES(Locks::mutator_lock_)
681 REQUIRES(!dex_lock_, !Roles::uninterruptible_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700682
Ian Rogers7b078e82014-09-10 14:44:24 -0700683 void AppendToBootClassPath(Thread* self, const DexFile& dex_file)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700684 SHARED_REQUIRES(Locks::mutator_lock_)
685 REQUIRES(!dex_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700686 void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700687 SHARED_REQUIRES(Locks::mutator_lock_)
688 REQUIRES(!dex_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700689
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700690 // Precomputes size needed for Class, in the case of a non-temporary class this size must be
691 // sufficient to hold all static fields.
692 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
693 const DexFile::ClassDef& dex_class_def);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700694
Mathieu Chartierc7853442015-03-27 14:35:38 -0700695 // Setup the classloader, class def index, type idx so that we can insert this class in the class
696 // table.
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700697 void SetupClass(const DexFile& dex_file,
698 const DexFile::ClassDef& dex_class_def,
699 Handle<mirror::Class> klass,
700 mirror::ClassLoader* class_loader)
Mathieu Chartier90443472015-07-16 20:32:27 -0700701 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700702
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700703 void LoadClass(Thread* self,
704 const DexFile& dex_file,
705 const DexFile::ClassDef& dex_class_def,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700706 Handle<mirror::Class> klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700707 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700708 void LoadClassMembers(Thread* self,
709 const DexFile& dex_file,
710 const uint8_t* class_data,
711 Handle<mirror::Class> klass,
712 const OatFile::OatClass* oat_class)
Mathieu Chartier90443472015-07-16 20:32:27 -0700713 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700714
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700715 void LoadField(const ClassDataItemIterator& it, Handle<mirror::Class> klass, ArtField* dst)
Mathieu Chartier90443472015-07-16 20:32:27 -0700716 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700717
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700718 void LoadMethod(Thread* self,
719 const DexFile& dex_file,
720 const ClassDataItemIterator& it,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700721 Handle<mirror::Class> klass, ArtMethod* dst)
Mathieu Chartier90443472015-07-16 20:32:27 -0700722 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700723
Mathieu Chartier90443472015-07-16 20:32:27 -0700724 void FixupStaticTrampolines(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800725
Ian Rogers97b52f82014-08-14 11:34:07 -0700726 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
727 // error and sets found to false.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700728 OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found)
Mathieu Chartier90443472015-07-16 20:32:27 -0700729 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800730
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700731 void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700732 REQUIRES(dex_lock_)
733 SHARED_REQUIRES(Locks::mutator_lock_);
734 mirror::DexCache* FindDexCacheLocked(Thread* self, const DexFile& dex_file, bool allow_failure)
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700735 REQUIRES(dex_lock_)
736 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700737
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700738 bool InitializeClass(Thread* self,
739 Handle<mirror::Class> klass,
740 bool can_run_clinit,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800741 bool can_init_parents)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700742 SHARED_REQUIRES(Locks::mutator_lock_)
743 REQUIRES(!dex_lock_);
Alex Lighteb7c1442015-08-31 13:17:42 -0700744 bool InitializeDefaultInterfaceRecursive(Thread* self,
745 Handle<mirror::Class> klass,
746 bool can_run_clinit,
747 bool can_init_parents)
748 REQUIRES(!dex_lock_)
749 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700750 bool WaitForInitializeClass(Handle<mirror::Class> klass,
751 Thread* self,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700752 ObjectLock<mirror::Class>& lock);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700753 bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700754 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700755
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700756 bool IsSameDescriptorInDifferentClassContexts(Thread* self,
757 const char* descriptor,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700758 Handle<mirror::ClassLoader> class_loader1,
759 Handle<mirror::ClassLoader> class_loader2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700760 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700761
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700762 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self,
763 ArtMethod* method,
764 mirror::Class* klass1,
765 mirror::Class* klass2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700766 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700767
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700768 bool LinkClass(Thread* self,
769 const char* descriptor,
770 Handle<mirror::Class> klass,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700771 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700772 MutableHandle<mirror::Class>* h_new_class_out)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700773 SHARED_REQUIRES(Locks::mutator_lock_)
774 REQUIRES(!Locks::classlinker_classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700775
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700776 bool LinkSuperClass(Handle<mirror::Class> klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700777 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700778
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700779 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700780 SHARED_REQUIRES(Locks::mutator_lock_)
781 REQUIRES(!dex_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700782
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700783 bool LinkMethods(Thread* self,
784 Handle<mirror::Class> klass,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700785 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700786 ArtMethod** out_imt)
Mathieu Chartier90443472015-07-16 20:32:27 -0700787 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700788
Alex Light9139e002015-10-09 15:59:48 -0700789 // Does anything needed to make sure that the compiler will not generate a direct invoke to this
790 // method. Should only be called on non-invokable methods.
791 void EnsureThrowsInvocationError(ArtMethod* method)
792 SHARED_REQUIRES(Locks::mutator_lock_);
793
794 // A wrapper class representing the result of a method translation used for linking methods and
795 // updating superclass default methods. For each method in a classes vtable there are 4 states it
796 // could be in:
797 // 1) No translation is necessary. In this case there is no MethodTranslation object for it. This
798 // is the standard case and is true when the method is not overridable by a default method,
799 // the class defines a concrete implementation of the method, the default method implementation
800 // remains the same, or an abstract method stayed abstract.
801 // 2) The method must be translated to a different default method. We note this with
802 // CreateTranslatedMethod.
803 // 3) The method must be replaced with a conflict method. This happens when a superclass
804 // implements an interface with a default method and this class implements an unrelated
805 // interface that also defines that default method. We note this with CreateConflictingMethod.
806 // 4) The method must be replaced with an abstract miranda method. This happens when a superclass
807 // implements an interface with a default method and this class implements a subinterface of
808 // the superclass's interface which declares the default method abstract. We note this with
809 // CreateAbstractMethod.
810 //
811 // When a method translation is unnecessary (case #1), we don't put it into the
812 // default_translation maps. So an instance of MethodTranslation must be in one of #2-#4.
813 class MethodTranslation {
814 public:
815 // This slot must become a default conflict method.
816 static MethodTranslation CreateConflictingMethod() {
817 return MethodTranslation(Type::kConflict, /*translation*/nullptr);
818 }
819
820 // This slot must become an abstract method.
821 static MethodTranslation CreateAbstractMethod() {
822 return MethodTranslation(Type::kAbstract, /*translation*/nullptr);
823 }
824
825 // Use the given method as the current value for this vtable slot during translation.
826 static MethodTranslation CreateTranslatedMethod(ArtMethod* new_method) {
827 return MethodTranslation(Type::kTranslation, new_method);
828 }
829
830 // Returns true if this is a method that must become a conflict method.
831 bool IsInConflict() const {
832 return type_ == Type::kConflict;
833 }
834
835 // Returns true if this is a method that must become an abstract method.
836 bool IsAbstract() const {
837 return type_ == Type::kAbstract;
838 }
839
840 // Returns true if this is a method that must become a different method.
841 bool IsTranslation() const {
842 return type_ == Type::kTranslation;
843 }
844
845 // Get the translated version of this method.
846 ArtMethod* GetTranslation() const {
847 DCHECK(IsTranslation());
848 DCHECK(translation_ != nullptr);
849 return translation_;
850 }
851
852 private:
853 enum class Type {
854 kTranslation,
855 kConflict,
856 kAbstract,
857 };
858
859 MethodTranslation(Type type, ArtMethod* translation)
860 : translation_(translation), type_(type) {}
861
862 ArtMethod* const translation_;
863 const Type type_;
864 };
865
Alex Lighteb7c1442015-08-31 13:17:42 -0700866 // Links the virtual methods for the given class and records any default methods that will need to
867 // be updated later.
868 //
869 // Arguments:
870 // * self - The current thread.
871 // * klass - class, whose vtable will be filled in.
872 // * default_translations - Vtable index to new method map.
873 // Any vtable entries that need to be updated with new default methods
874 // are stored into the default_translations map. The default_translations
875 // map is keyed on the vtable index that needs to be updated. We use this
876 // map because if we override a default method with another default
877 // method we need to update the vtable to point to the new method.
878 // Unfortunately since we copy the ArtMethod* we cannot just do a simple
879 // scan, we therefore store the vtable index's that might need to be
880 // updated with the method they will turn into.
881 // TODO This whole default_translations thing is very dirty. There should be a better way.
Alex Light9139e002015-10-09 15:59:48 -0700882 bool LinkVirtualMethods(
883 Thread* self,
884 Handle<mirror::Class> klass,
885 /*out*/std::unordered_map<size_t, MethodTranslation>* default_translations)
Mathieu Chartier90443472015-07-16 20:32:27 -0700886 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700887
Alex Lighteb7c1442015-08-31 13:17:42 -0700888 // Sets up the interface lookup table (IFTable) in the correct order to allow searching for
889 // default methods.
890 bool SetupInterfaceLookupTable(Thread* self,
891 Handle<mirror::Class> klass,
892 Handle<mirror::ObjectArray<mirror::Class>> interfaces)
893 SHARED_REQUIRES(Locks::mutator_lock_);
894
Alex Light9139e002015-10-09 15:59:48 -0700895
896 enum class DefaultMethodSearchResult {
897 kDefaultFound,
898 kAbstractFound,
899 kDefaultConflict
900 };
901
Alex Lighteb7c1442015-08-31 13:17:42 -0700902 // Find the default method implementation for 'interface_method' in 'klass', if one exists.
903 //
904 // Arguments:
905 // * self - The current thread.
906 // * target_method - The method we are trying to find a default implementation for.
907 // * klass - The class we are searching for a definition of target_method.
908 // * out_default_method - The pointer we will store the found default method to on success.
Alex Lighteb7c1442015-08-31 13:17:42 -0700909 //
910 // Return value:
Alex Light9139e002015-10-09 15:59:48 -0700911 // * kDefaultFound - There were no conflicting method implementations found in the class while
912 // searching for target_method. The default method implementation is stored into
913 // out_default_method.
914 // * kAbstractFound - There were no conflicting method implementations found in the class while
915 // searching for target_method but no default implementation was found either.
916 // out_default_method is set to null and the method should be considered not
917 // implemented.
918 // * kDefaultConflict - Conflicting method implementations were found when searching for
919 // target_method. The value of *out_default_method is null.
920 DefaultMethodSearchResult FindDefaultMethodImplementation(
921 Thread* self,
922 ArtMethod* target_method,
923 Handle<mirror::Class> klass,
924 /*out*/ArtMethod** out_default_method) const
Alex Lighteb7c1442015-08-31 13:17:42 -0700925 SHARED_REQUIRES(Locks::mutator_lock_);
926
927 // Sets the imt entries and fixes up the vtable for the given class by linking all the interface
928 // methods. See LinkVirtualMethods for an explanation of what default_translations is.
Alex Light9139e002015-10-09 15:59:48 -0700929 bool LinkInterfaceMethods(
930 Thread* self,
931 Handle<mirror::Class> klass,
932 const std::unordered_map<size_t, MethodTranslation>& default_translations,
933 ArtMethod** out_imt)
Mathieu Chartier90443472015-07-16 20:32:27 -0700934 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700935
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700936 bool LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700937 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700938 bool LinkInstanceFields(Thread* self, Handle<mirror::Class> klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700939 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700940 bool LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, size_t* class_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700941 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700942 void LinkCode(ArtMethod* method,
943 const OatFile::OatClass* oat_class,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700944 uint32_t class_def_method_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700945 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700946 void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
Mathieu Chartier90443472015-07-16 20:32:27 -0700947 SHARED_REQUIRES(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700948
Mathieu Chartiere401d142015-04-22 13:56:20 -0700949 void CheckProxyConstructor(ArtMethod* constructor) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700950 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkin457e8742015-10-22 17:37:50 -0700951 void CheckLambdaProxyConstructor(ArtMethod* constructor) const
952 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700953 void CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700954 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkin457e8742015-10-22 17:37:50 -0700955 void CheckLambdaProxyMethod(ArtMethod* method, ArtMethod* prototype) const
956 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700957
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700958 // For use by ImageWriter to find DexCaches for its roots
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700959 ReaderWriterMutex* DexLock()
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700960 SHARED_REQUIRES(Locks::mutator_lock_)
961 LOCK_RETURNED(dex_lock_) {
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700962 return &dex_lock_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700963 }
Mathieu Chartier90443472015-07-16 20:32:27 -0700964 size_t GetDexCacheCount() SHARED_REQUIRES(Locks::mutator_lock_, dex_lock_) {
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700965 return dex_caches_.size();
966 }
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800967 const std::list<DexCacheData>& GetDexCachesData()
968 SHARED_REQUIRES(Locks::mutator_lock_, dex_lock_) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700969 return dex_caches_;
970 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700971
Mathieu Chartiere401d142015-04-22 13:56:20 -0700972 void CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out)
Mathieu Chartier90443472015-07-16 20:32:27 -0700973 SHARED_REQUIRES(Locks::mutator_lock_);
Igor Murashkin457e8742015-10-22 17:37:50 -0700974
975 // Copy the constructor from java.lang.LambdaProxy into the 'klass'.
976 // The copy is written into 'method_constructor'.
977 void CreateLambdaProxyConstructor(Handle<mirror::Class> klass,
978 /*out*/ArtMethod* method_constructor)
979 SHARED_REQUIRES(Locks::mutator_lock_);
980
Mathieu Chartiere401d142015-04-22 13:56:20 -0700981 void CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, ArtMethod* out)
Mathieu Chartier90443472015-07-16 20:32:27 -0700982 SHARED_REQUIRES(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400983
Igor Murashkin457e8742015-10-22 17:37:50 -0700984 void CreateLambdaProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, ArtMethod* out)
985 SHARED_REQUIRES(Locks::mutator_lock_);
986
Andreas Gampe48498592014-09-10 19:48:05 -0700987 // Ensures that methods have the kAccPreverified bit set. We use the kAccPreverfied bit on the
988 // class access flags to determine whether this has been done before.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700989 void EnsurePreverifiedMethods(Handle<mirror::Class> c)
Mathieu Chartier90443472015-07-16 20:32:27 -0700990 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe48498592014-09-10 19:48:05 -0700991
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800992 mirror::Class* LookupClassFromBootImage(const char* descriptor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700993 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersdbf3be02014-08-29 15:40:08 -0700994
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -0700995 // Returns null if not found.
996 ClassTable* ClassTableForClassLoader(mirror::ClassLoader* class_loader)
997 SHARED_REQUIRES(Locks::mutator_lock_, Locks::classlinker_classes_lock_);
Igor Murashkin457e8742015-10-22 17:37:50 -0700998
999 // Insert a new class table if not found. Uses bootclasspath if class_loader is null.
1000 // Returns either the existing table, or the new one if there wasn't one previously
1001 // (the return value is always non-null).
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -07001002 ClassTable* InsertClassTableForClassLoader(mirror::ClassLoader* class_loader)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -07001003 SHARED_REQUIRES(Locks::mutator_lock_)
1004 REQUIRES(Locks::classlinker_classes_lock_);
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -07001005
Ian Rogersdbf3be02014-08-29 15:40:08 -07001006 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
1007 // before returning it to the caller. Its the responsibility of the thread that placed the class
1008 // in the table to make it resolved. The thread doing resolution must notify on the class' lock
1009 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
1010 // retire a class, the version of the class in the table is returned and this may differ from
1011 // the class passed in.
1012 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -07001013 WARN_UNUSED
1014 SHARED_REQUIRES(Locks::mutator_lock_)
1015 REQUIRES(!dex_lock_);
Ian Rogersdbf3be02014-08-29 15:40:08 -07001016
1017 void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
Mathieu Chartier90443472015-07-16 20:32:27 -07001018 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersdbf3be02014-08-29 15:40:08 -07001019
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001020 void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001021 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001022
1023 // Return the quick generic JNI stub for testing.
1024 const void* GetRuntimeQuickGenericJniStub() const;
1025
Andreas Gampebfdcdc12015-04-22 18:10:36 -07001026 // Throw the class initialization failure recorded when first trying to initialize the given
1027 // class.
Andreas Gampecb086952015-11-02 16:20:00 -08001028 void ThrowEarlierClassFailure(mirror::Class* c, bool wrap_in_no_class_def = false)
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -07001029 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -07001030 REQUIRES(!dex_lock_);
Andreas Gampebfdcdc12015-04-22 18:10:36 -07001031
Mathieu Chartiere401d142015-04-22 13:56:20 -07001032 bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, bool can_init_parents)
Mathieu Chartier90443472015-07-16 20:32:27 -07001033 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001034
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001035 void UpdateClassVirtualMethods(mirror::Class* klass,
1036 LengthPrefixedArray<ArtMethod>* new_methods)
1037 SHARED_REQUIRES(Locks::mutator_lock_)
1038 REQUIRES(!Locks::classlinker_classes_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001039
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001040 std::vector<const DexFile*> boot_class_path_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001041 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001042
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07001043 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001044 // JNI weak globals and side data to allow dex caches to get unloaded. We lazily delete weak
1045 // globals when we register new dex files.
1046 std::list<DexCacheData> dex_caches_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -07001047
Mathieu Chartier9b1c71e2015-09-02 18:51:54 -07001048 // This contains the class loaders which have class tables. It is populated by
Mathieu Chartier951ec2c2015-09-22 08:50:05 -07001049 // InsertClassTableForClassLoader.
1050 std::list<ClassLoaderData> class_loaders_
Mathieu Chartier6b069532015-08-05 15:08:12 -07001051 GUARDED_BY(Locks::classlinker_classes_lock_);
1052
1053 // Boot class path table. Since the class loader for this is null.
1054 ClassTable boot_class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001055
Mathieu Chartiercc5ebdf2015-07-27 11:19:43 -07001056 // New class roots, only used by CMS since the GC needs to mark these in the pause.
1057 std::vector<GcRoot<mirror::Class>> new_class_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001058
Mathieu Chartier073b16c2015-11-10 14:13:23 -08001059 // Do we need to search dex caches to find boot image classes?
1060 bool dex_cache_boot_image_class_lookup_required_;
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001061 // Number of times we've searched dex caches for a class. After a certain number of misses we move
1062 // the classes into the class_table_ to avoid dex cache based searches.
Ian Rogers68b56852014-08-29 20:19:11 -07001063 Atomic<uint32_t> failed_dex_cache_class_lookups_;
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001064
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001065 // Well known mirror::Class roots.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -07001066 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001067
Ian Rogers98379392014-02-24 16:53:16 -08001068 // The interface table used by all arrays.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -07001069 GcRoot<mirror::IfTable> array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -07001070
Ian Rogers98379392014-02-24 16:53:16 -08001071 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
1072 // descriptors for the sake of performing FindClass.
1073 static constexpr size_t kFindArrayCacheSize = 16;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -07001074 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
Ian Rogers98379392014-02-24 16:53:16 -08001075 size_t find_array_class_cache_next_victim_;
1076
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001077 bool init_done_;
Mathieu Chartier893263b2014-03-04 11:07:42 -08001078 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001079
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001080 InternTable* intern_table_;
1081
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001082 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
1083 // patch point within the image. TODO: make these proper relocations.
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001084 const void* quick_resolution_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -07001085 const void* quick_imt_conflict_trampoline_;
Andreas Gampe2da88232014-02-27 12:26:20 -08001086 const void* quick_generic_jni_trampoline_;
Vladimir Marko8a630572014-04-09 18:45:35 +01001087 const void* quick_to_interpreter_bridge_trampoline_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001088
Mathieu Chartier2d721012014-11-10 11:08:06 -08001089 // Image pointer size.
1090 size_t image_pointer_size_;
1091
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001092 friend class ImageDumper; // for DexLock
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001093 friend class ImageWriter; // for GetClassRoots
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001094 friend class JniCompilerTest; // for GetRuntimeQuickGenericJniStub
Andreas Gampe5f4a09a2015-09-28 13:16:33 -07001095 friend class JniInternalTest; // for GetRuntimeQuickGenericJniStub
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001096 ART_FRIEND_TEST(mirror::DexCacheTest, Open); // for AllocDexCache
1097
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
1099};
1100
1101} // namespace art
1102
Brian Carlstromfc0e3212013-07-17 14:40:12 -07001103#endif // ART_RUNTIME_CLASS_LINKER_H_