blob: 6fc0f0e2f2bcb33659c53575e4cff8628bf3f96d [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>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070021#include <utility>
22#include <vector>
23
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080025#include "base/mutex.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026#include "dex_file.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070027#include "gc_root.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080028#include "gtest/gtest.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070029#include "jni.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070030#include "oat_file.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080031#include "object_callbacks.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070032
33namespace art {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034
Ian Rogers1d54e732013-05-02 21:10:01 -070035namespace gc {
36namespace space {
37 class ImageSpace;
38} // namespace space
39} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040namespace mirror {
Ian Rogers33e95662013-05-20 20:29:14 -070041 class ClassLoader;
42 class DexCache;
43 class DexCacheTest_Open_Test;
44 class IfTable;
45 template<class T> class ObjectArray;
46 class StackTraceElement;
47} // namespace mirror
Ian Rogers1d54e732013-05-02 21:10:01 -070048
Elliott Hughescf4c6c42011-09-01 15:16:42 -070049class InternTable;
Mathieu Chartierc528dba2013-11-26 12:00:11 -080050template<class T> class ObjectLock;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070051class ScopedObjectAccessAlreadyRunnable;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070052template<class T> class Handle;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070053
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054typedef bool (ClassVisitor)(mirror::Class* c, void* arg);
Elliott Hughesa2155262011-11-16 16:26:58 -080055
Mathieu Chartier893263b2014-03-04 11:07:42 -080056enum VisitRootFlags : uint8_t;
57
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070058class ClassLinker {
59 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070060 explicit ClassLinker(InternTable* intern_table);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070061 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070062
Mathieu Chartier590fee92013-09-13 13:46:47 -070063 // Initialize class linker by bootstraping from dex files
64 void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
66
67 // Initialize class linker from one or more images.
68 void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
69
Ian Rogersbe7149f2013-08-20 09:29:39 -070070 bool IsInBootClassPath(const char* descriptor);
71
Elliott Hughes64bf5a32011-09-20 14:43:12 -070072 // Finds a class by its descriptor, loading it if necessary.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070073 // If class_loader is null, searches boot_class_path_.
Ian Rogers98379392014-02-24 16:53:16 -080074 mirror::Class* FindClass(Thread* self, const char* descriptor,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070075 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -070076 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -070077
Ian Rogers98379392014-02-24 16:53:16 -080078 // Finds a class by its descriptor using the "system" class loader, ie by searching the
79 // boot_class_path_.
80 mirror::Class* FindSystemClass(Thread* self, const char* descriptor)
81 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82
83 // Finds the array class given for the element class.
Mathieu Chartierb74cd292014-05-29 14:31:33 -070084 mirror::Class* FindArrayClass(Thread* self, mirror::Class** element_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -070086
Mathieu Chartier590fee92013-09-13 13:46:47 -070087 // Reutrns true if the class linker is initialized.
88 bool IsInitialized() const;
89
Brian Carlstromaded5f72011-10-07 17:15:04 -070090 // Define a new a class based on a ClassDef from a DexFile
Mathieu Chartierc528dba2013-11-26 12:00:11 -080091 mirror::Class* DefineClass(const char* descriptor,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070092 Handle<mirror::ClassLoader> class_loader,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -070094 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070095
96 // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded
97 // by the given 'class_loader'.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 mirror::Class* LookupClass(const char* descriptor, const mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -070099 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700101
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800102 // Finds all the classes with the given descriptor, regardless of ClassLoader.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700104 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes6fa602d2011-12-02 17:54:25 -0800106
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700108
Brian Carlstromae826982011-11-09 01:33:42 -0800109 // General class unloading is not supported, this is used to prune
110 // unwanted classes during image writing.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 bool RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700112 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -0800114
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700115 void DumpAllClasses(int flags)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700116 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700118
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700119 void DumpForSigQuit(std::ostream& os)
120 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -0700122
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700123 size_t NumLoadedClasses()
124 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughese27955c2011-08-26 15:21:24 -0700126
Brian Carlstromb63ec392011-08-27 17:38:27 -0700127 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstromaded5f72011-10-07 17:15:04 -0700128 // result in the DexCache. The referrer is used to identify the
129 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130 mirror::String* ResolveString(uint32_t string_idx, mirror::ArtMethod* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700132
133 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700134 // result in the DexCache.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700136 Handle<mirror::DexCache> dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700138
Brian Carlstromb63ec392011-08-27 17:38:27 -0700139 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700140 // result in the DexCache. The referrer is used to identity the
141 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700144
Brian Carlstromb63ec392011-08-27 17:38:27 -0700145 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700146 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -0700147 // target DexCache and ClassLoader to use for resolution.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtField* referrer)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700153
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700154 // Resolve a type with the given ID from the DexFile, storing the
155 // result in DexCache. The ClassLoader is used to search for the
156 // type, since it may be referenced from but not contained within
157 // the given DexFile.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700159 Handle<mirror::DexCache> dex_cache,
160 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700162
163 // Resolve a method with a given ID from the DexFile, storing the
164 // result in DexCache. The ClassLinker and ClassLoader are used as
165 // in ResolveType. What is unique is the method type argument which
166 // is used to determine if this method is a direct, static, or
167 // virtual method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700168 mirror::ArtMethod* ResolveMethod(const DexFile& dex_file,
169 uint32_t method_idx,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700170 Handle<mirror::DexCache> dex_cache,
171 Handle<mirror::ClassLoader> class_loader,
172 Handle<mirror::ArtMethod> referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700173 InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700175
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700176 mirror::ArtMethod* GetResolvedMethod(uint32_t method_idx, mirror::ArtMethod* referrer,
177 InvokeType type)
178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179 mirror::ArtMethod* ResolveMethod(Thread* self, uint32_t method_idx, mirror::ArtMethod** referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700180 InvokeType type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom16192862011-09-12 17:50:06 -0700182
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700183 mirror::ArtField* GetResolvedField(uint32_t field_idx, mirror::Class* field_declaring_class)
184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185 mirror::ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700186 bool is_static)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700188
Brian Carlstrom16192862011-09-12 17:50:06 -0700189 // Resolve a field with a given ID from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700190 // result in DexCache. The ClassLinker and ClassLoader are used as
191 // in ResolveType. What is unique is the is_static argument which is
192 // used to determine if we are resolving a static or non-static
193 // field.
Brian Carlstromea46f952013-07-30 01:26:50 -0700194 mirror::ArtField* ResolveField(const DexFile& dex_file,
195 uint32_t field_idx,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700196 Handle<mirror::DexCache> dex_cache,
197 Handle<mirror::ClassLoader> class_loader,
Brian Carlstromea46f952013-07-30 01:26:50 -0700198 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700200
Ian Rogersb067ac22011-12-13 18:05:09 -0800201 // Resolve a field with a given ID from the DexFile, storing the
202 // result in DexCache. The ClassLinker and ClassLoader are used as
203 // in ResolveType. No is_static argument is provided so that Java
204 // field resolution semantics are followed.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700205 mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file, uint32_t field_idx,
206 Handle<mirror::DexCache> dex_cache,
207 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb067ac22011-12-13 18:05:09 -0800209
Ian Rogersad25ac52011-10-04 19:13:33 -0700210 // Get shorty from method index without resolution. Used to do handlerization.
Brian Carlstromea46f952013-07-30 01:26:50 -0700211 const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad25ac52011-10-04 19:13:33 -0700213
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700214 // Returns true on success, false if there's an exception pending.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700215 // can_run_clinit=false allows the compiler to attempt to init a class,
216 // given the restriction that no <clinit> execution is possible.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700217 bool EnsureInitialized(Handle<mirror::Class> c, bool can_init_fields, bool can_init_parents)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700219
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700220 // Initializes classes that have instances in the image but that have
221 // <clinit> methods so they could not be initialized by the compiler.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700222 void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700223
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 void RegisterDexFile(const DexFile& dex_file)
225 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700227 void RegisterDexFile(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700230
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700231 const OatFile* RegisterOatFile(const OatFile* oat_file)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 LOCKS_EXCLUDED(dex_lock_);
Brian Carlstrom866c8622012-01-06 16:35:13 -0800233
Brian Carlstrom8a487412011-08-29 20:08:52 -0700234 const std::vector<const DexFile*>& GetBootClassPath() {
235 return boot_class_path_;
236 }
237
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700238 void VisitClasses(ClassVisitor* visitor, void* arg)
239 LOCKS_EXCLUDED(dex_lock_)
240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241 // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_
242 // when calling the visitor.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700243 void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg)
244 LOCKS_EXCLUDED(dex_lock_)
245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesa2155262011-11-16 16:26:58 -0800246
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700247 void VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700248 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier893263b2014-03-04 11:07:42 -0800250 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags)
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700251 LOCKS_EXCLUDED(dex_lock_)
252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700253
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700254 mirror::DexCache* FindDexCache(const DexFile& dex_file)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700257 bool IsDexFileRegistered(const DexFile& dex_file)
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700258 LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700259 void FixupDexCaches(mirror::ArtMethod* resolution_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700262
jeffhao262bf462011-10-20 18:36:32 -0700263 // Generate an oat file from a dex file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700264 bool GenerateOatFile(const char* dex_filename,
Brian Carlstromd601af82012-01-06 10:15:19 -0800265 int oat_fd,
Vladimir Marko60836d52014-01-16 15:53:38 +0000266 const char* oat_cache_filename,
Ian Rogers719d1a32014-03-06 12:13:39 -0800267 std::string* error_msg)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700268 LOCKS_EXCLUDED(Locks::mutator_lock_);
jeffhao262bf462011-10-20 18:36:32 -0700269
Andreas Gampe833a4852014-05-21 18:46:59 -0700270 // Find or create the oat file holding dex_location. Then load all corresponding dex files
271 // (if multidex) into the given vector.
272 bool OpenDexFilesFromOat(const char* dex_location, const char* oat_location,
273 std::vector<std::string>* error_msgs,
274 std::vector<const DexFile*>* dex_files)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700275 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
jeffhaof6174e82012-01-31 16:14:17 -0800276
Alex Light6e183f22014-07-18 14:57:04 -0700277 // Returns true if the given oat file has the same image checksum as the image it is paired with.
278 static bool VerifyOatImageChecksum(const OatFile* oat_file, const InstructionSet instruction_set);
279 // Returns true if the oat file checksums match with the image and the offsets are such that it
280 // could be loaded with it.
281 static bool VerifyOatChecksums(const OatFile* oat_file, const InstructionSet instruction_set,
282 std::string* error_msg);
Brian Carlstrom28db0122012-10-18 16:20:41 -0700283 // Returns true if oat file contains the dex file with the given location and checksum.
Alex Light6e183f22014-07-18 14:57:04 -0700284 static bool VerifyOatAndDexFileChecksums(const OatFile* oat_file,
285 const char* dex_location,
286 uint32_t dex_location_checksum,
287 InstructionSet instruction_set,
288 std::string* error_msg);
Brian Carlstromafe25512012-06-27 17:02:58 -0700289
Elliott Hughes418d20f2011-09-22 14:00:39 -0700290 // TODO: replace this with multiple methods that allocate the correct managed type.
Shih-wei Liao44175362011-08-28 16:59:17 -0700291 template <class T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800292 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700294
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao98eacac2011-09-14 16:11:53 -0700297
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300
Brian Carlstromea46f952013-07-30 01:26:50 -0700301 mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
303
304 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount)
305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
306
Brian Carlstromea46f952013-07-30 01:26:50 -0700307 mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
309
310 mirror::ObjectArray<mirror::StackTraceElement>* AllocStackTraceElementArray(Thread* self,
311 size_t length)
312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
313
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700314 void VerifyClass(Handle<mirror::Class> klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass,
316 mirror::Class::Status& oat_file_class_status)
317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800318 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700319 Handle<mirror::Class> klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -0700321 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
323
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700324 mirror::Class* CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name,
325 jobjectArray interfaces, jobject loader, jobjectArray methods,
326 jobjectArray throws)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800328 std::string GetDescriptorForProxy(mirror::Class* proxy_class)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800330 mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class,
331 mirror::ArtMethod* proxy_method)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700332 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400334
Ian Rogers19846512012-02-24 11:42:47 -0800335 // Get the oat code for a method when its class isn't yet initialized
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336 const void* GetQuickOatCodeFor(mirror::ArtMethod* method)
337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
338 const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700339 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800340
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700341 // Get the oat code for a method from a method index.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800342 const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
344 const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx)
Ian Rogers33e95662013-05-20 20:29:14 -0700345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700346
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700347 pid_t GetClassesLockOwner(); // For SignalCatcher.
348 pid_t GetDexLockOwner(); // For SignalCatcher.
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700349
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700350 const void* GetPortableResolutionTrampoline() const {
351 return portable_resolution_trampoline_;
352 }
353
Andreas Gampe2da88232014-02-27 12:26:20 -0800354 const void* GetQuickGenericJniTrampoline() const {
Dmitry Petrochenkof0972a42014-05-16 17:43:39 +0700355 return quick_generic_jni_trampoline_;
356 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800357
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700358 const void* GetQuickResolutionTrampoline() const {
359 return quick_resolution_trampoline_;
360 }
361
Jeff Hao88474b42013-10-23 16:24:40 -0700362 const void* GetPortableImtConflictTrampoline() const {
363 return portable_imt_conflict_trampoline_;
364 }
365
366 const void* GetQuickImtConflictTrampoline() const {
367 return quick_imt_conflict_trampoline_;
368 }
369
Vladimir Marko8a630572014-04-09 18:45:35 +0100370 const void* GetQuickToInterpreterBridgeTrampoline() const {
371 return quick_to_interpreter_bridge_trampoline_;
372 }
373
Jeff Hao88474b42013-10-23 16:24:40 -0700374 InternTable* GetInternTable() const {
375 return intern_table_;
376 }
377
Ian Rogers848871b2013-08-05 10:56:33 -0700378 // Attempts to insert a class into a class table. Returns NULL if
379 // the class was inserted, otherwise returns an existing class with
380 // the same descriptor and ClassLoader.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700381 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash)
Ian Rogers848871b2013-08-05 10:56:33 -0700382 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
384
Mathieu Chartier590fee92013-09-13 13:46:47 -0700385 // Special code to allocate an art method, use this instead of class->AllocObject.
386 mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700387
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700388 mirror::ObjectArray<mirror::Class>* GetClassRoots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700389 mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700390 DCHECK(class_roots != NULL);
391 return class_roots;
392 }
393
Mathieu Chartier590fee92013-09-13 13:46:47 -0700394 private:
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100395 bool FindOatMethodFor(mirror::ArtMethod* method, OatFile::OatMethod* oat_method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700396 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa12785321912012-04-01 15:24:56 -0700397
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700398 OatFile& GetImageOatFile(gc::space::ImageSpace* space)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700399 LOCKS_EXCLUDED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700400 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700401
Ian Rogers98379392014-02-24 16:53:16 -0800402 void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700403
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700404 // For early bootstrapping by Init
Ian Rogers6fac4472014-02-25 17:01:10 -0800405 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700406 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700407
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 // Alloc* convenience functions to avoid needing to pass in mirror::Class*
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700409 // values that are known to the ClassLinker such as
410 // kObjectArrayClass and kJavaLangString etc.
Ian Rogers6fac4472014-02-25 17:01:10 -0800411 mirror::Class* AllocClass(Thread* self, uint32_t class_size)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800412 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700414 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -0700415 mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700416
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type)
418 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
419 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700420 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700421
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700422
Ian Rogers98379392014-02-24 16:53:16 -0800423 mirror::Class* CreateArrayClass(Thread* self, const char* descriptor,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700424 Handle<mirror::ClassLoader> class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700425 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700426
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700427 void AppendToBootClassPath(const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700428 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700429 void AppendToBootClassPath(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700431
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700432 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Brian Carlstromea46f952013-07-30 01:26:50 -0700433 mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700434 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700435
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700436 // Precomputes size needed for Class, in the case of a non-temporary class this size must be
437 // sufficient to hold all static fields.
438 uint32_t SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file,
439 const DexFile::ClassDef& dex_class_def);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700440
Brian Carlstromf615a612011-07-23 12:50:34 -0700441 void LoadClass(const DexFile& dex_file,
442 const DexFile::ClassDef& dex_class_def,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700443 Handle<mirror::Class> klass,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444 mirror::ClassLoader* class_loader)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700445 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100446 void LoadClassMembers(const DexFile& dex_file,
447 const byte* class_data,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700448 Handle<mirror::Class> klass,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100449 mirror::ClassLoader* class_loader,
450 const OatFile::OatClass* oat_class)
451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700452
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700454 Handle<mirror::Class> klass, Handle<mirror::ArtField> dst)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700456
Brian Carlstromea46f952013-07-30 01:26:50 -0700457 mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file,
458 const ClassDataItemIterator& dex_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700459 Handle<mirror::Class> klass)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700461
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800462 void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800463
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100464 // Finds the associated oat class for a dex_file and descriptor. Returns whether the class
465 // was found, and sets the data in oat_class.
466 bool FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, OatFile::OatClass* oat_class)
Ian Rogers33e95662013-05-20 20:29:14 -0700467 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers19846512012-02-24 11:42:47 -0800468
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700469 void RegisterDexFileLocked(const DexFile& dex_file, Handle<mirror::DexCache> dex_cache)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700471 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700472 bool IsDexFileRegisteredLocked(const DexFile& dex_file)
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700473 SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700474
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700475 bool InitializeClass(Handle<mirror::Class> klass, bool can_run_clinit,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800476 bool can_init_parents)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700477 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700478 bool WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800479 ObjectLock<mirror::Class>& lock);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700480 bool ValidateSuperClassDescriptors(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700481 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700482
Ian Rogers98379392014-02-24 16:53:16 -0800483 bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700484 Handle<mirror::ClassLoader> class_loader1,
485 Handle<mirror::ClassLoader> class_loader2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700487
Ian Rogers98379392014-02-24 16:53:16 -0800488 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800489 mirror::Class* klass1,
490 mirror::Class* klass2)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700491 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700492
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700493 bool LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass,
494 Handle<mirror::ObjectArray<mirror::Class>> interfaces,
495 mirror::Class** new_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700496 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700497
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700498 bool LinkSuperClass(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700500
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700501 bool LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700503
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700504 bool LinkMethods(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700505 Handle<mirror::ObjectArray<mirror::Class>> interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700507
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700508 bool LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700509 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700511 bool LinkInterfaceMethods(Handle<mirror::Class> klass,
512 Handle<mirror::ObjectArray<mirror::Class>> interfaces)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700513 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700514
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700515 bool LinkStaticFields(Handle<mirror::Class> klass, size_t* class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700516 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700517 bool LinkInstanceFields(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700519 bool LinkFields(Handle<mirror::Class> klass, bool is_static, size_t* class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700520 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700521 void LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class,
Dmitry Petrochenkof0972a42014-05-16 17:43:39 +0700522 const DexFile& dex_file, uint32_t dex_method_index, uint32_t method_index)
523 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700524
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700525 void CreateReferenceInstanceOffsets(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700527 void CreateReferenceStaticOffsets(Handle<mirror::Class> klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700529 void CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 uint32_t reference_offsets)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700532
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700533 // For use by ImageWriter to find DexCaches for its roots
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700534 ReaderWriterMutex* DexLock()
535 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCK_RETURNED(dex_lock_) {
536 return &dex_lock_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700537 }
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700538 size_t GetDexCacheCount() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_) {
539 return dex_caches_.size();
540 }
541 mirror::DexCache* GetDexCache(size_t idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, dex_lock_);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700542
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file)
Ian Rogers33e95662013-05-20 20:29:14 -0700544 LOCKS_EXCLUDED(dex_lock_)
545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700546
547 // Find an opened oat file that contains dex_location. If oat_location is not nullptr, the file
548 // must have that location, else any oat location is accepted.
549 const OatFile* FindOpenedOatFile(const char* oat_location, const char* dex_location,
550 const uint32_t* const dex_location_checksum)
Ian Rogers719d1a32014-03-06 12:13:39 -0800551 LOCKS_EXCLUDED(dex_lock_);
Alex Lighta59dd802014-07-02 16:28:08 -0700552
553 // Will open the oat file directly without relocating, even if we could/should do relocation.
554 const OatFile* FindOatFileFromOatLocation(const std::string& oat_location,
555 std::string* error_msg)
556 LOCKS_EXCLUDED(dex_lock_);
557
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700559 LOCKS_EXCLUDED(dex_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700560
Alex Lighta59dd802014-07-02 16:28:08 -0700561 const OatFile* OpenOatFileFromDexLocation(const std::string& dex_location,
562 InstructionSet isa,
563 bool* already_opened,
564 bool* obsolete_file_cleanup_failed,
565 std::vector<std::string>* error_msg)
566 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
567
568 const OatFile* PatchAndRetrieveOat(const std::string& input, const std::string& output,
569 const std::string& image_location, InstructionSet isa,
570 std::string* error_msg)
571 LOCKS_EXCLUDED(Locks::mutator_lock_);
572
573 bool CheckOatFile(const OatFile* oat_file, InstructionSet isa,
574 bool* checksum_verified, std::string* error_msg);
575 int32_t GetRequiredDelta(const OatFile* oat_file, InstructionSet isa);
576
Andreas Gampe833a4852014-05-21 18:46:59 -0700577 // Note: will not register the oat file.
578 const OatFile* FindOatFileInOatLocationForDexFile(const char* dex_location,
579 uint32_t dex_location_checksum,
580 const char* oat_location,
581 std::string* error_msg)
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700582 LOCKS_EXCLUDED(dex_lock_);
Ian Rogerse3359f72013-06-11 15:14:11 -0700583
Andreas Gampe833a4852014-05-21 18:46:59 -0700584 // Creates the oat file from the dex_location to the oat_location. Needs a file descriptor for
585 // the file to be written, which is assumed to be under a lock.
586 const OatFile* CreateOatFileForDexLocation(const char* dex_location,
587 int fd, const char* oat_location,
588 std::vector<std::string>* error_msgs)
589 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
590
591 // Finds an OatFile that contains a DexFile for the given a DexFile location.
592 //
593 // Note 1: this will not check open oat files, which are assumed to be stale when this is run.
594 // Note 2: Does not register the oat file. It is the caller's job to register if the file is to
595 // be kept.
596 const OatFile* FindOatFileContainingDexFileFromDexLocation(const char* location,
597 const uint32_t* const location_checksum,
598 InstructionSet isa,
Calin Juravlec54aea72014-07-16 14:45:03 +0100599 std::vector<std::string>* error_msgs,
600 bool* obsolete_file_cleanup_failed)
Andreas Gampe833a4852014-05-21 18:46:59 -0700601 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_);
602
Alex Lighta59dd802014-07-02 16:28:08 -0700603 // verify an oat file with the given dex file. Will return false when the dex file could not be
604 // verified. Will return true otherwise.
605 bool VerifyOatWithDexFile(const OatFile* oat_file, const char* dex_location,
606 std::string* error_msg);
Brian Carlstromfad71432011-10-16 20:25:10 -0700607
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700608 mirror::ArtMethod* CreateProxyConstructor(Thread* self, Handle<mirror::Class> klass,
Brian Carlstromea46f952013-07-30 01:26:50 -0700609 mirror::Class* proxy_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700610 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700611 mirror::ArtMethod* CreateProxyMethod(Thread* self, Handle<mirror::Class> klass,
612 Handle<mirror::ArtMethod> prototype)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700613 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilson95caa792011-10-12 18:14:17 -0400614
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700615 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700616
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700617 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier893263b2014-03-04 11:07:42 -0800618 std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700619 std::vector<GcRoot<mirror::DexCache>> dex_caches_ GUARDED_BY(dex_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700620 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -0700621
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622
Brian Carlstromaded5f72011-10-07 17:15:04 -0700623 // multimap from a string hash code of a class descriptor to
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800624 // mirror::Class* instances. Results should be compared for a matching
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700625 // Class::descriptor_ and Class::class_loader_.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700626 typedef std::multimap<size_t, GcRoot<mirror::Class>> Table;
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700627 // This contains strong roots. To enable concurrent root scanning of
628 // the class table, be careful to use a read barrier when accessing this.
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700629 Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700630 std::vector<std::pair<size_t, GcRoot<mirror::Class>>> new_class_roots_;
Elliott Hughesf8349362012-06-18 15:00:06 -0700631
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700632 // Do we need to search dex caches to find image classes?
633 bool dex_cache_image_class_lookup_required_;
634 // Number of times we've searched dex caches for a class. After a certain number of misses we move
635 // the classes into the class_table_ to avoid dex cache based searches.
636 AtomicInteger failed_dex_cache_class_lookups_;
637
638 mirror::Class* LookupClassFromTableLocked(const char* descriptor,
639 const mirror::ClassLoader* class_loader,
640 size_t hash)
641 SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_);
642
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700643 mirror::Class* UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash)
644 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
645 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
646
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700647 void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_)
648 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
649 mirror::Class* LookupClassFromImage(const char* descriptor)
650 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700651
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700652 // EnsureResolved is called to make sure that a class in the class_table_ has been resolved
653 // before returning it to the caller. Its the responsibility of the thread that placed the class
654 // in the table to make it resolved. The thread doing resolution must notify on the class' lock
655 // when resolution has occurred. This happens in mirror::Class::SetStatus. As resolution may
656 // retire a class, the version of the class in the table is returned and this may differ from
657 // the class passed in.
658 mirror::Class* EnsureResolved(Thread* self, const char* descriptor, mirror::Class* klass)
Mathieu Chartier4c13a3f2014-07-14 14:57:16 -0700659 WARN_UNUSED SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700660
661 void FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class)
662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
663
Brian Carlstroma663ea52011-08-19 23:33:41 -0700664 // indexes into class_roots_.
665 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700666 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700667 kJavaLangClass,
668 kJavaLangObject,
Elliott Hughes418d20f2011-09-22 14:00:39 -0700669 kClassArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700670 kObjectArrayClass,
671 kJavaLangString,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700672 kJavaLangDexCache,
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700673 kJavaLangRefReference,
Brian Carlstromea46f952013-07-30 01:26:50 -0700674 kJavaLangReflectArtField,
675 kJavaLangReflectArtMethod,
Ian Rogers466bb252011-10-14 03:29:56 -0700676 kJavaLangReflectProxy,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700677 kJavaLangStringArrayClass,
Brian Carlstromea46f952013-07-30 01:26:50 -0700678 kJavaLangReflectArtFieldArrayClass,
679 kJavaLangReflectArtMethodArrayClass,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700680 kJavaLangClassLoader,
Ian Rogers5167c972012-02-03 10:41:20 -0800681 kJavaLangThrowable,
jeffhao8cd6dda2012-02-22 10:15:34 -0800682 kJavaLangClassNotFoundException,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700683 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700684 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700685 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700686 kPrimitiveChar,
687 kPrimitiveDouble,
688 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700689 kPrimitiveInt,
690 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700691 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700692 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700693 kBooleanArrayClass,
694 kByteArrayClass,
695 kCharArrayClass,
696 kDoubleArrayClass,
697 kFloatArrayClass,
698 kIntArrayClass,
699 kLongArrayClass,
700 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700701 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700702 kClassRootsMax,
703 };
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700704 GcRoot<mirror::ObjectArray<mirror::Class>> class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700705
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800706 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700707
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800708 void SetClassRoot(ClassRoot class_root, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700709 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700710
Elliott Hughes418d20f2011-09-22 14:00:39 -0700711 static const char* class_roots_descriptors_[];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700712
713 const char* GetClassRootDescriptor(ClassRoot class_root) {
714 const char* descriptor = class_roots_descriptors_[class_root];
715 CHECK(descriptor != NULL);
716 return descriptor;
717 }
718
Ian Rogers98379392014-02-24 16:53:16 -0800719 // The interface table used by all arrays.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700720 GcRoot<mirror::IfTable> array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700721
Ian Rogers98379392014-02-24 16:53:16 -0800722 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class
723 // descriptors for the sake of performing FindClass.
724 static constexpr size_t kFindArrayCacheSize = 16;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700725 GcRoot<mirror::Class> find_array_class_cache_[kFindArrayCacheSize];
Ian Rogers98379392014-02-24 16:53:16 -0800726 size_t find_array_class_cache_next_victim_;
727
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700728 bool init_done_;
Mathieu Chartier893263b2014-03-04 11:07:42 -0800729 bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_);
730 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700731
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700732 InternTable* intern_table_;
733
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700734 // Trampolines within the image the bounce to runtime entrypoints. Done so that there is a single
735 // patch point within the image. TODO: make these proper relocations.
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700736 const void* portable_resolution_trampoline_;
737 const void* quick_resolution_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700738 const void* portable_imt_conflict_trampoline_;
739 const void* quick_imt_conflict_trampoline_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800740 const void* quick_generic_jni_trampoline_;
Vladimir Marko8a630572014-04-09 18:45:35 +0100741 const void* quick_to_interpreter_bridge_trampoline_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700742
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700743 friend class ImageWriter; // for GetClassRoots
Alex Lighta59dd802014-07-02 16:28:08 -0700744 friend class ImageDumper; // for FindOpenedOatFileFromOatLocation
745 friend class ElfPatcher; // for FindOpenedOatFileForDexFile & FindOpenedOatFileFromOatLocation
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100746 friend class NoDex2OatTest; // for FindOpenedOatFileForDexFile
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800747 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800748 FRIEND_TEST(mirror::DexCacheTest, Open);
Brian Carlstromae826982011-11-09 01:33:42 -0800749 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
750 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700751 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
752};
753
754} // namespace art
755
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700756#endif // ART_RUNTIME_CLASS_LINKER_H_