blob: a7a68b75467fe46ea1e25915cf393fb72de89167 [file] [log] [blame]
Elliott Hughes418d20f2011-09-22 14:00:39 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_CLASS_LINKER_H_
18#define ART_RUNTIME_CLASS_LINKER_H_
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
Fred Shih37f05ef2014-07-16 18:38:08 -070020#include <deque>
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include <string>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022#include <utility>
23#include <vector>
24
Mathieu Chartierbad02672014-08-25 13:08:22 -070025#include "base/allocator.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070028#include "dex_file.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070029#include "gc_root.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080030#include "gtest/gtest.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
Mathieu Chartierbf99f772014-08-23 16:37:27 -070051template<class T> class ConstHandle;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070052class InternTable;
Mathieu Chartierc528dba2013-11-26 12:00:11 -080053template<class T> class ObjectLock;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070054class ScopedObjectAccessAlreadyRunnable;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070055template<class T> class Handle;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057typedef bool (ClassVisitor)(mirror::Class* c, void* arg);
Elliott Hughesa2155262011-11-16 16:26:58 -080058
Mathieu Chartier893263b2014-03-04 11:07:42 -080059enum VisitRootFlags : uint8_t;
60
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070061class ClassLinker {
62 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070063 explicit ClassLinker(InternTable* intern_table);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070064 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070065
Alex Light64ad14d2014-08-19 14:23:13 -070066 // Initialize class linker by bootstraping from dex files.
67 void InitWithoutImage(const std::vector<const DexFile*>& boot_class_path)
Mathieu Chartier590fee92013-09-13 13:46:47 -070068 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
69
70 // Initialize class linker from one or more images.
71 void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
72
Elliott Hughes64bf5a32011-09-20 14:43:12 -070073 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070074 // If class_loader is null, searches boot_class_path_.
Ian Rogers98379392014-02-24 16:53:16 -080075 mirror::Class* FindClass(Thread* self, const char* descriptor,
Mathieu Chartierbf99f772014-08-23 16:37:27 -070076 ConstHandle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -070077 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -070078
Ian Rogers98379392014-02-24 16:53:16 -080079 // Finds a class by its descriptor using the "system" class loader, ie by searching the
80 // boot_class_path_.
81 mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
83
84 // Finds the array class given for the element class.
Mathieu Chartierb74cd292014-05-29 14:31:33 -070085 mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -070087
Ian Rogers63557452014-06-04 16:57:15 -070088 // Returns true if the class linker is initialized.
Mathieu Chartier590fee92013-09-13 13:46:47 -070089 bool IsInitialized() const;
90
Brian Carlstromaded5f72011-10-07 17:15:04 -070091 // Define a new a class based on a ClassDef from a DexFile
Mathieu Chartierc528dba2013-11-26 12:00:11 -080092 mirror::Class* DefineClass(const char* descriptor,
Mathieu Chartierbf99f772014-08-23 16:37:27 -070093 ConstHandle<mirror::ClassLoader> class_loader,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070096
97 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
98 // by the given 'class_loader'.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 mirror::Class* LookupClass(const char* descriptor, const mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700100 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700102
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800103 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800107
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700109
Brian Carlstromae826982011-11-09 01:33:42 -0800110 // General class unloading is not supported, this is used to prune
111 // unwanted classes during image writing.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 bool RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800115
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700116 void DumpAllClasses(int flags)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700117 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700119
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700120 void DumpForSigQuit(std::ostream& os)
121 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -0700123
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700124 size_t NumLoadedClasses()
125 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -0700127
Brian Carlstromb63ec392011-08-27 17:38:27 -0700128 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129 // result in the DexCache. The referrer is used to identify the
130 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 mirror::String* ResolveString(uint32_t string_idx, mirror::ArtMethod* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700133
134 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700135 // result in the DexCache.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700137 ConstHandle<mirror::DexCache> dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700139
Brian Carlstromb63ec392011-08-27 17:38:27 -0700140 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700141 // result in the DexCache. The referrer is used to identity the
142 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800143 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700144 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700145
Brian Carlstromb63ec392011-08-27 17:38:27 -0700146 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700147 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700148 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700151
Ian Rogersef7d42f2014-01-06 12:55:46 -0800152 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtField* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700154
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700155 // Resolve a type with the given ID from the DexFile, storing the
156 // result in DexCache. The ClassLoader is used to search for the
157 // type, since it may be referenced from but not contained within
158 // the given DexFile.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700159 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700160 ConstHandle<mirror::DexCache> dex_cache,
161 ConstHandle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700163
164 // Resolve a method with a given ID from the DexFile, storing the
165 // result in DexCache. The ClassLinker and ClassLoader are used as
166 // in ResolveType. What is unique is the method type argument which
167 // is used to determine if this method is a direct, static, or
168 // virtual method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700169 mirror::ArtMethod* ResolveMethod(const DexFile& dex_file,
170 uint32_t method_idx,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700171 ConstHandle<mirror::DexCache> dex_cache,
172 ConstHandle<mirror::ClassLoader> class_loader,
173 ConstHandle<mirror::ArtMethod> referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700174 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700176
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700177 mirror::ArtMethod* GetResolvedMethod(uint32_t method_idx, mirror::ArtMethod* referrer,
178 InvokeType type)
179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
180 mirror::ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, mirror::ArtMethod** referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700181 InvokeType type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom16192862011-09-12 17:50:06 -0700183
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700184 mirror::ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800186 mirror::ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700187 bool is_static)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700189
Brian Carlstrom16192862011-09-12 17:50:06 -0700190 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700191 // result in DexCache. The ClassLinker and ClassLoader are used as
192 // in ResolveType. What is unique is the is_static argument which is
193 // used to determine if we are resolving a static or non-static
194 // field.
Brian Carlstromea46f952013-07-30 01:26:50 -0700195 mirror::ArtField* ResolveField(const DexFile& dex_file,
196 uint32_t field_idx,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700197 ConstHandle<mirror::DexCache> dex_cache,
198 ConstHandle<mirror::ClassLoader> class_loader,
Brian Carlstromea46f952013-07-30 01:26:50 -0700199 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700200 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700201
Ian Rogersb067ac22011-12-13 18:05:09 -0800202 // Resolve a field with a given ID from the DexFile, storing the
203 // result in DexCache. The ClassLinker and ClassLoader are used as
204 // in ResolveType. No is_static argument is provided so that Java
205 // field resolution semantics are followed.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700206 mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file, uint32_t field_idx,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700207 ConstHandle<mirror::DexCache> dex_cache,
208 ConstHandle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb067ac22011-12-13 18:05:09 -0800210
Ian Rogersad25ac52011-10-04 19:13:33 -0700211 // Get shorty from method index without resolution. Used to do handlerization.
Brian Carlstromea46f952013-07-30 01:26:50 -0700212 const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700213 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad25ac52011-10-04 19:13:33 -0700214
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700215 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700216 // can_run_clinit=false allows the compiler to attempt to init a class,
217 // given the restriction that no <clinit> execution is possible.
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700218 bool EnsureInitialized(ConstHandle<mirror::Class> c, bool can_init_fields, bool can_init_parents)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700221 // Initializes classes that have instances in the image but that have
222 // <clinit> methods so they could not be initialized by the compiler.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700224
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 void RegisterDexFile(const DexFile& dex_file)
226 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700228 void RegisterDexFile(const DexFile& dex_file, ConstHandle<mirror::DexCache> dex_cache)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700232 const OatFile* RegisterOatFile(const OatFile* oat_file)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 LOCKS_EXCLUDED(dex_lock_);
Brian Carlstrom866c8622012-01-06 16:35:13 -0800234
Brian Carlstrom8a487412011-08-29 20:08:52 -0700235 const std::vector<const DexFile*>& GetBootClassPath() {
236 return boot_class_path_;
237 }
238
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700239 void VisitClasses(ClassVisitor* visitor, void* arg)
240 LOCKS_EXCLUDED(dex_lock_)
241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_
243 // when calling the visitor.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700244 void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg)
245 LOCKS_EXCLUDED(dex_lock_)
246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa2155262011-11-16 16:26:58 -0800247
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700248 void VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700249 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800251 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700252 LOCKS_EXCLUDED(dex_lock_)
253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700254
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700255 mirror::DexCache* FindDexCache(const DexFile& dex_file)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700258 bool IsDexFileRegistered(const DexFile& dex_file)
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700259 LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700260 void FixupDexCaches(mirror::ArtMethod* resolution_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700263
jeffhao262bf462011-10-20 18:36:32 -0700264 // Generate an oat file from a dex file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700265 bool GenerateOatFile(const char* dex_filename,
Brian Carlstromd601af82012-01-06 10:15:19 -0800266 int oat_fd,
Vladimir Marko60836d52014-01-16 15:53:38 +0000267 const char* oat_cache_filename,
Ian Rogers719d1a32014-03-06 12:13:39 -0800268 std::string* error_msg)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700269 LOCKS_EXCLUDED(Locks::mutator_lock_);
jeffhao262bf462011-10-20 18:36:32 -0700270
Andreas Gampe833a4852014-05-21 18:46:59 -0700271 // Find or create the oat file holding dex_location. Then load all corresponding dex files
272 // (if multidex) into the given vector.
273 bool OpenDexFilesFromOat(const char* dex_location, const char* oat_location,
274 std::vector<std::string>* error_msgs,
275 std::vector<const DexFile*>* dex_files)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700276 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
jeffhaof6174e82012-01-31 16:14:17 -0800277
Alex Light6e183f22014-07-18 14:57:04 -0700278 // Returns true if the given oat file has the same image checksum as the image it is paired with.
279 static bool VerifyOatImageChecksum(const OatFile* oat_file, const InstructionSet instruction_set);
280 // Returns true if the oat file checksums match with the image and the offsets are such that it
281 // could be loaded with it.
282 static bool VerifyOatChecksums(const OatFile* oat_file, const InstructionSet instruction_set,
283 std::string* error_msg);
Brian Carlstrom28db0122012-10-18 16:20:41 -0700284 // Returns true if oat file contains the dex file with the given location and checksum.
Alex Light6e183f22014-07-18 14:57:04 -0700285 static bool VerifyOatAndDexFileChecksums(const OatFile* oat_file,
286 const char* dex_location,
287 uint32_t dex_location_checksum,
288 InstructionSet instruction_set,
289 std::string* error_msg);
Brian Carlstromafe25512012-06-27 17:02:58 -0700290
Ian Rogersc0542af2014-09-03 16:16:56 -0700291 // Allocate an instance of a java.lang.Object.
292 mirror::Object* AllocObject(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
293
Elliott Hughes418d20f2011-09-22 14:00:39 -0700294 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700295 template <class T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700298
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao98eacac2011-09-14 16:11:53 -0700301
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304
Brian Carlstromea46f952013-07-30 01:26:50 -0700305 mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
307
308 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
310
Brian Carlstromea46f952013-07-30 01:26:50 -0700311 mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
313
314 mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
315 size_t length)
316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
317
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700318 void VerifyClass(ConstHandle<mirror::Class> klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
320 mirror::Class::Status& oat_file_class_status)
321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800322 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700323 ConstHandle<mirror::Class> klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -0700325 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
327
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700328 mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
329 jobjectArray interfaces, jobject loader, jobjectArray methods,
330 jobjectArray throws)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332 std::string GetDescriptorForProxy(mirror::Class* proxy_class)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334 mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
335 mirror::ArtMethod* proxy_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400338
Ian Rogers19846512012-02-24 11:42:47 -0800339 // Get the oat code for a method when its class isn't yet initialized
Ian Rogersef7d42f2014-01-06 12:55:46 -0800340 const void* GetQuickOatCodeFor(mirror::ArtMethod* method)
341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
342 const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800344
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700345 // Get the oat code for a method from a method index.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800346 const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
347 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
348 const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
Ian Rogers33e95662013-05-20 20:29:14 -0700349 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700350
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700351 // Get compiled code for a method, return null if no code
352 // exists. This is unlike Get..OatCodeFor which will return a bridge
353 // or interpreter entrypoint.
354 const void* GetOatMethodQuickCodeFor(mirror::ArtMethod* method)
355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
356 const void* GetOatMethodPortableCodeFor(mirror::ArtMethod* method)
357 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
358
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700359 pid_t GetClassesLockOwner(); // For SignalCatcher.
360 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700361
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700362 const void* GetPortableResolutionTrampoline() const {
363 return portable_resolution_trampoline_;
364 }
365
Andreas Gampe2da88232014-02-27 12:26:20 -0800366 const void* GetQuickGenericJniTrampoline() const {
Dmitry Petrochenkof0972a42014-05-16 17:43:39 +0700367 return quick_generic_jni_trampoline_;
368 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800369
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700370 const void* GetQuickResolutionTrampoline() const {
371 return quick_resolution_trampoline_;
372 }
373
Jeff Hao88474b42013-10-23 16:24:40 -0700374 const void* GetPortableImtConflictTrampoline() const {
375 return portable_imt_conflict_trampoline_;
376 }
377
378 const void* GetQuickImtConflictTrampoline() const {
379 return quick_imt_conflict_trampoline_;
380 }
381
Vladimir Marko8a630572014-04-09 18:45:35 +0100382 const void* GetQuickToInterpreterBridgeTrampoline() const {
383 return quick_to_interpreter_bridge_trampoline_;
384 }
385
Jeff Hao88474b42013-10-23 16:24:40 -0700386 InternTable* GetInternTable() const {
387 return intern_table_;
388 }
389
Ian Rogers848871b2013-08-05 10:56:33 -0700390 // Attempts to insert a class into a class table. Returns NULL if
391 // the class was inserted, otherwise returns an existing class with
392 // the same descriptor and ClassLoader.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700393 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
Ian Rogers848871b2013-08-05 10:56:33 -0700394 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
396
Mathieu Chartier590fee92013-09-13 13:46:47 -0700397 // Special code to allocate an art method, use this instead of class->AllocObject.
398 mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700399
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700400 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700401 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700402 DCHECK(class_roots != NULL);
403 return class_roots;
404 }
405
Mathieu Chartier590fee92013-09-13 13:46:47 -0700406 private:
Ian Rogers97b52f82014-08-14 11:34:07 -0700407 const OatFile::OatMethod FindOatMethodFor(mirror::ArtMethod* method, bool* found)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700408 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa12785321912012-04-01 15:24:56 -0700409
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700410 OatFile& GetImageOatFile(gc::space::ImageSpace* space)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700411 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700412 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700413
Ian Rogers98379392014-02-24 16:53:16 -0800414 void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700415
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700416 // For early bootstrapping by Init
Ian Rogers6fac4472014-02-25 17:01:10 -0800417 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700418 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700419
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420 // Alloc* convenience functions to avoid needing to pass in mirror::Class*
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700421 // values that are known to the ClassLinker such as
422 // kObjectArrayClass and kJavaLangString etc.
Ian Rogers6fac4472014-02-25 17:01:10 -0800423 mirror::Class* AllocClass(Thread* self, uint32_t class_size)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800424 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -0700427 mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700428
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
431 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700432 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700433
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700434
Ian Rogers98379392014-02-24 16:53:16 -0800435 mirror::Class* CreateArrayClass(Thread* self, const char* descriptor,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700436 ConstHandle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700437 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700438
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 void AppendToBootClassPath(const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700441 void AppendToBootClassPath(const DexFile& dex_file, ConstHandle<mirror::DexCache> dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700442 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700443
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700444 // Precomputes size needed for Class, in the case of a non-temporary class this size must be
445 // sufficient to hold all static fields.
446 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
447 const DexFile::ClassDef& dex_class_def);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700448
Brian Carlstromf615a612011-07-23 12:50:34 -0700449 void LoadClass(const DexFile& dex_file,
450 const DexFile::ClassDef& dex_class_def,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700451 ConstHandle<mirror::Class> klass,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800452 mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100454 void LoadClassMembers(const DexFile& dex_file,
455 const byte* class_data,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700456 ConstHandle<mirror::Class> klass,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100457 mirror::ClassLoader* class_loader,
458 const OatFile::OatClass* oat_class)
459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700460
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700462 ConstHandle<mirror::Class> klass, ConstHandle<mirror::ArtField> dst)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800463 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700464
Brian Carlstromea46f952013-07-30 01:26:50 -0700465 mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
466 const ClassDataItemIterator& dex_method,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700467 ConstHandle<mirror::Class> klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700469
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800470 void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800471
Ian Rogers97b52f82014-08-14 11:34:07 -0700472 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
473 // error and sets found to false.
474 OatFile::OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found)
Ian Rogers33e95662013-05-20 20:29:14 -0700475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800476
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700477 void RegisterDexFileLocked(const DexFile& dex_file, ConstHandle<mirror::DexCache> dex_cache)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700480 bool IsDexFileRegisteredLocked(const DexFile& dex_file)
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700481 SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700482
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700483 bool InitializeClass(ConstHandle<mirror::Class> klass, bool can_run_clinit,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800484 bool can_init_parents)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700486 bool WaitForInitializeClass(ConstHandle<mirror::Class> klass, Thread* self,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800487 ObjectLock<mirror::Class>& lock);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700488 bool ValidateSuperClassDescriptors(ConstHandle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700489 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700490
Ian Rogers98379392014-02-24 16:53:16 -0800491 bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700492 ConstHandle<mirror::ClassLoader> class_loader1,
493 ConstHandle<mirror::ClassLoader> class_loader2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700495
Ian Rogers98379392014-02-24 16:53:16 -0800496 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497 mirror::Class* klass1,
498 mirror::Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700500
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700501 bool LinkClass(Thread* self, const char* descriptor, ConstHandle<mirror::Class> klass,
502 ConstHandle<mirror::ObjectArray<mirror::Class>> interfaces,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700503 mirror::Class** new_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700504 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700505
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700506 bool LinkSuperClass(ConstHandle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700507 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700508
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700509 bool LoadSuperAndInterfaces(ConstHandle<mirror::Class> klass, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700510 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700511
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700512 bool LinkMethods(Thread* self, ConstHandle<mirror::Class> klass,
513 ConstHandle<mirror::ObjectArray<mirror::Class>> interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700514 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700515
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700516 bool LinkVirtualMethods(Thread* self, ConstHandle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700518
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700519 bool LinkInterfaceMethods(ConstHandle<mirror::Class> klass,
520 ConstHandle<mirror::ObjectArray<mirror::Class>> interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700522
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700523 bool LinkStaticFields(ConstHandle<mirror::Class> klass, size_t* class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700525 bool LinkInstanceFields(ConstHandle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700527 bool LinkFields(ConstHandle<mirror::Class> klass, bool is_static, size_t* class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700529 void LinkCode(ConstHandle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
Dmitry Petrochenkof0972a42014-05-16 17:43:39 +0700530 const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index)
531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700532 void CreateReferenceInstanceOffsets(ConstHandle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700533 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700534 void CreateReferenceStaticOffsets(ConstHandle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700535 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700536 void CreateReferenceOffsets(ConstHandle<mirror::Class> klass, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 uint32_t reference_offsets)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700538 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700539
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700540 // For use by ImageWriter to find DexCaches for its roots
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700541 ReaderWriterMutex* DexLock()
542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
543 return &dex_lock_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700544 }
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700545 size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
546 return dex_caches_.size();
547 }
548 mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700549
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700550 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
Ian Rogers33e95662013-05-20 20:29:14 -0700551 LOCKS_EXCLUDED(dex_lock_)
552 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700553
554 // Find an opened oat file that contains dex_location. If oat_location is not nullptr, the file
555 // must have that location, else any oat location is accepted.
556 const OatFile* FindOpenedOatFile(const char* oat_location, const char* dex_location,
557 const uint32_t* const dex_location_checksum)
Ian Rogers719d1a32014-03-06 12:13:39 -0800558 LOCKS_EXCLUDED(dex_lock_);
Alex Lighta59dd802014-07-02 16:28:08 -0700559
560 // Will open the oat file directly without relocating, even if we could/should do relocation.
561 const OatFile* FindOatFileFromOatLocation(const std::string& oat_location,
562 std::string* error_msg)
563 LOCKS_EXCLUDED(dex_lock_);
564
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700566 LOCKS_EXCLUDED(dex_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700567
Alex Lighta59dd802014-07-02 16:28:08 -0700568 const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location,
569 InstructionSet isa,
570 bool* already_opened,
571 bool* obsolete_file_cleanup_failed,
572 std::vector<std::string>* error_msg)
573 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
574
Alex Light9dcc4572014-08-14 14:16:26 -0700575 const OatFile* GetInterpretedOnlyOat(const std::string& oat_path,
576 InstructionSet isa,
577 std::string* error_msg);
578
Alex Lighta59dd802014-07-02 16:28:08 -0700579 const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output,
580 const std::string& image_location, InstructionSet isa,
581 std::string* error_msg)
582 LOCKS_EXCLUDED(Locks::mutator_lock_);
583
584 bool CheckOatFile(const OatFile* oat_file, InstructionSet isa,
585 bool* checksum_verified, std::string* error_msg);
586 int32_t GetRequiredDelta(const OatFile* oat_file, InstructionSet isa);
587
Andreas Gampe833a4852014-05-21 18:46:59 -0700588 // Note: will not register the oat file.
589 const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location,
590 uint32_t dex_location_checksum,
591 const char* oat_location,
592 std::string* error_msg)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700593 LOCKS_EXCLUDED(dex_lock_);
Ian Rogerse3359f72013-06-11 15:14:11 -0700594
Andreas Gampe833a4852014-05-21 18:46:59 -0700595 // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for
596 // the file to be written, which is assumed to be under a lock.
597 const OatFile* CreateOatFileForDexLocation(const char* dex_location,
598 int fd, const char* oat_location,
599 std::vector<std::string>* error_msgs)
600 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
601
602 // Finds an OatFile that contains a DexFile for the given a DexFile location.
603 //
604 // Note 1: this will not check open oat files, which are assumed to be stale when this is run.
605 // Note 2: Does not register the oat file. It is the caller's job to register if the file is to
606 // be kept.
607 const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* location,
608 const uint32_t* const location_checksum,
609 InstructionSet isa,
Calin Juravlec54aea72014-07-16 14:45:03 +0100610 std::vector<std::string>* error_msgs,
611 bool* obsolete_file_cleanup_failed)
Andreas Gampe833a4852014-05-21 18:46:59 -0700612 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
613
Alex Lighta59dd802014-07-02 16:28:08 -0700614 // verify an oat file with the given dex file. Will return false when the dex file could not be
615 // verified. Will return true otherwise.
616 bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location,
617 std::string* error_msg);
Brian Carlstromfad71432011-10-16 20:25:10 -0700618
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700619 mirror::ArtMethod* CreateProxyConstructor(Thread* self, ConstHandle<mirror::Class> klass,
Brian Carlstromea46f952013-07-30 01:26:50 -0700620 mirror::Class* proxy_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700621 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700622 mirror::ArtMethod* CreateProxyMethod(Thread* self, ConstHandle<mirror::Class> klass,
623 ConstHandle<mirror::ArtMethod> prototype)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700624 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400625
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700626 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700627
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700628 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier893263b2014-03-04 11:07:42 -0800629 std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700630 std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700631 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700632
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700633
Brian Carlstromaded5f72011-10-07 17:15:04 -0700634 // multimap from a string hash code of a class descriptor to
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800635 // mirror::Class* instances. Results should be compared for a matching
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700636 // Class::descriptor_ and Class::class_loader_.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700637 typedef AllocationTrackingMultiMap<size_t, GcRoot<mirror::Class>, kAllocatorTagClassTable> Table;
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700638 // This contains strong roots. To enable concurrent root scanning of
639 // the class table, be careful to use a read barrier when accessing this.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700640 Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700641 std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_;
Elliott Hughesf8349362012-06-18 15:00:06 -0700642
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700643 // Do we need to search dex caches to find image classes?
644 bool dex_cache_image_class_lookup_required_;
645 // Number of times we've searched dex caches for a class. After a certain number of misses we move
646 // the classes into the class_table_ to avoid dex cache based searches.
Ian Rogers68b56852014-08-29 20:19:11 -0700647 Atomic<uint32_t> failed_dex_cache_class_lookups_;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700648
649 mirror::Class* LookupClassFromTableLocked(const char* descriptor,
650 const mirror::ClassLoader* class_loader,
651 size_t hash)
652 SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
653
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700654 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
655 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
656 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
657
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700658 void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
659 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
660 mirror::Class* LookupClassFromImage(const char* descriptor)
661 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700662
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700663 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
664 // before returning it to the caller. Its the responsibility of the thread that placed the class
665 // in the table to make it resolved. The thread doing resolution must notify on the class' lock
666 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
667 // retire a class, the version of the class in the table is returned and this may differ from
668 // the class passed in.
669 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700670 WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700671
672 void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
673 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
674
Brian Carlstroma663ea52011-08-19 23:33:41 -0700675 // indexes into class_roots_.
676 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700677 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700678 kJavaLangClass,
679 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700680 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700681 kObjectArrayClass,
682 kJavaLangString,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700683 kJavaLangDexCache,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700684 kJavaLangRefReference,
Brian Carlstromea46f952013-07-30 01:26:50 -0700685 kJavaLangReflectArtField,
686 kJavaLangReflectArtMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700687 kJavaLangReflectProxy,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700688 kJavaLangStringArrayClass,
Brian Carlstromea46f952013-07-30 01:26:50 -0700689 kJavaLangReflectArtFieldArrayClass,
690 kJavaLangReflectArtMethodArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700691 kJavaLangClassLoader,
Ian Rogers5167c972012-02-03 10:41:20 -0800692 kJavaLangThrowable,
jeffhao8cd6dda2012-02-22 10:15:34 -0800693 kJavaLangClassNotFoundException,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700694 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700695 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700696 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700697 kPrimitiveChar,
698 kPrimitiveDouble,
699 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700700 kPrimitiveInt,
701 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700702 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700703 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700704 kBooleanArrayClass,
705 kByteArrayClass,
706 kCharArrayClass,
707 kDoubleArrayClass,
708 kFloatArrayClass,
709 kIntArrayClass,
710 kLongArrayClass,
711 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700712 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700713 kClassRootsMax,
714 };
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700715 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700716
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800717 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700718
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800719 void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700720 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700721
Elliott Hughes418d20f2011-09-22 14:00:39 -0700722 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700723
724 const char* GetClassRootDescriptor(ClassRoot class_root) {
725 const char* descriptor = class_roots_descriptors_[class_root];
726 CHECK(descriptor != NULL);
727 return descriptor;
728 }
729
Ian Rogers98379392014-02-24 16:53:16 -0800730 // The interface table used by all arrays.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700731 GcRoot<mirror::IfTable> array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700732
Ian Rogers98379392014-02-24 16:53:16 -0800733 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
734 // descriptors for the sake of performing FindClass.
735 static constexpr size_t kFindArrayCacheSize = 16;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700736 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
Ian Rogers98379392014-02-24 16:53:16 -0800737 size_t find_array_class_cache_next_victim_;
738
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700739 bool init_done_;
Mathieu Chartier893263b2014-03-04 11:07:42 -0800740 bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
741 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700742
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700743 InternTable* intern_table_;
744
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700745 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
746 // patch point within the image. TODO: make these proper relocations.
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700747 const void* portable_resolution_trampoline_;
748 const void* quick_resolution_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700749 const void* portable_imt_conflict_trampoline_;
750 const void* quick_imt_conflict_trampoline_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800751 const void* quick_generic_jni_trampoline_;
Vladimir Marko8a630572014-04-09 18:45:35 +0100752 const void* quick_to_interpreter_bridge_trampoline_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700753
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700754 friend class ImageWriter; // for GetClassRoots
Alex Lighta59dd802014-07-02 16:28:08 -0700755 friend class ImageDumper; // for FindOpenedOatFileFromOatLocation
756 friend class ElfPatcher; // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100757 friend class NoDex2OatTest; // for FindOpenedOatFileForDexFile
Alex Light9dcc4572014-08-14 14:16:26 -0700758 friend class NoPatchoatTest; // for FindOpenedOatFileForDexFile
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800759 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800760 FRIEND_TEST(mirror::DexCacheTest, Open);
Brian Carlstromae826982011-11-09 01:33:42 -0800761 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
762 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700763 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
764};
765
766} // namespace art
767
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700768#endif // ART_RUNTIME_CLASS_LINKER_H_