class_linker.h revision eb8167a4f4d27fce0530f6724ab8032610cd146b
1eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer/* 2eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * Copyright (C) 2011 The Android Open Source Project 3eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * 4eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * Licensed under the Apache License, Version 2.0 (the "License"); 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner * you may not use this file except in compliance with the License. 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner * You may obtain a copy of the License at 7eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * 8eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * http://www.apache.org/licenses/LICENSE-2.0 9eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * 10eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * Unless required by applicable law or agreed to in writing, software 11eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * distributed under the License is distributed on an "AS IS" BASIS, 12eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * See the License for the specific language governing permissions and 14eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer * limitations under the License. 15eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer */ 16eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 17dec628eead87b20773c98a00830580df211acc98Chris Lattner#ifndef ART_RUNTIME_CLASS_LINKER_H_ 180b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#define ART_RUNTIME_CLASS_LINKER_H_ 191f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer 20eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer#include <string> 21eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer#include <utility> 2217fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner#include <vector> 231e348549d930f1a3e636dcb2c78bad95636daec2Chris Lattner 241e348549d930f1a3e636dcb2c78bad95636daec2Chris Lattner#include "base/macros.h" 251e348549d930f1a3e636dcb2c78bad95636daec2Chris Lattner#include "base/mutex.h" 2628bc9d88260a3e153ead4311c9129e3d3ad07736Devang Patel#include "dex_file.h" 271e348549d930f1a3e636dcb2c78bad95636daec2Chris Lattner#include "gtest/gtest.h" 28bc8d813f715e47afd27b73b0b7abd00978e0ba51Daniel Dunbar#include "jni.h" 290386f01e061513094504bc11f8352a40173cada7Devang Patel#include "oat_file.h" 30eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer#include "object_callbacks.h" 31eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 32eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencernamespace art { 33eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencernamespace gc { 34eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencernamespace space { 351e348549d930f1a3e636dcb2c78bad95636daec2Chris Lattner class ImageSpace; 3617fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner} // namespace space 3717fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner} // namespace gc 3817fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattnernamespace mirror { 3917fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner class ClassLoader; 4017fcdd5e1b78b829068ca657c97357a39d6e768bChris Lattner class DexCache; 418b0a8c84da2030ee8f4440d5b60a8033de691222Anton Korobeynikov class DexCacheTest_Open_Test; 42eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer class IfTable; 43eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer template<class T> class ObjectArray; 44eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer class StackTraceElement; 45eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer} // namespace mirror 464f95d2bda4235d36acd95688d0744d7363214706Chris Lattner 47eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencerclass InternTable; 48eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencertemplate<class T> class ObjectLock; 49eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencerclass ScopedObjectAccess; 50eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencertemplate<class T> class Handle; 51eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 52eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencertypedef bool (ClassVisitor)(mirror::Class* c, void* arg); 53eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 54eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencerenum VisitRootFlags : uint8_t; 55eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 56eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencerclass ClassLinker { 57eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer public: 58eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Interface method table size. Increasing this value reduces the chance of two interface methods 59cb917f7abd1d929ea1e2558c6fd5860ed23339c2Chris Lattner // colliding in the interface method table but increases the size of classes that implement 60eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // (non-marker) interfaces. 61eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer static constexpr size_t kImtSize = 64; 62eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 63eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer explicit ClassLinker(InternTable* intern_table); 64eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer ~ClassLinker(); 65eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 66eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Initialize class linker by bootstraping from dex files 67bc8d813f715e47afd27b73b0b7abd00978e0ba51Daniel Dunbar void InitFromCompiler(const std::vector<const DexFile*>& boot_class_path) 68eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 69bc8d813f715e47afd27b73b0b7abd00978e0ba51Daniel Dunbar 70eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Initialize class linker from one or more images. 712928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar void InitFromImage() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 72eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 7394c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru bool IsInBootClassPath(const char* descriptor); 74eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 75eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Finds a class by its descriptor, loading it if necessary. 76eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // If class_loader is null, searches boot_class_path_. 77eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer mirror::Class* FindClass(Thread* self, const char* descriptor, 78eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer const Handle<mirror::ClassLoader>& class_loader) 79eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 80eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 81eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Finds a class by its descriptor using the "system" class loader, ie by searching the 82eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // boot_class_path_. 83eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer mirror::Class* FindSystemClass(Thread* self, const char* descriptor) 84eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 85eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 86eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Finds the array class given for the element class. 87eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer mirror::Class* FindArrayClass(Thread* self, mirror::Class* element_class) 88eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 89eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 90eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Reutrns true if the class linker is initialized. 91eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer bool IsInitialized() const; 92eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 93eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Define a new a class based on a ClassDef from a DexFile 94eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer mirror::Class* DefineClass(const char* descriptor, 95eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer const Handle<mirror::ClassLoader>& class_loader, 96eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer const DexFile& dex_file, const DexFile::ClassDef& dex_class_def) 97eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 98eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 99eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Finds a class by its descriptor, returning NULL if it isn't wasn't loaded 100dec628eead87b20773c98a00830580df211acc98Chris Lattner // by the given 'class_loader'. 101eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer mirror::Class* LookupClass(const char* descriptor, const mirror::ClassLoader* class_loader) 102eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 103eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1041e348549d930f1a3e636dcb2c78bad95636daec2Chris Lattner 105eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Finds all the classes with the given descriptor, regardless of ClassLoader. 106eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer void LookupClasses(const char* descriptor, std::vector<mirror::Class*>& classes) 107dec628eead87b20773c98a00830580df211acc98Chris Lattner LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 108eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 109dec628eead87b20773c98a00830580df211acc98Chris Lattner 110dec628eead87b20773c98a00830580df211acc98Chris Lattner mirror::Class* FindPrimitiveClass(char type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 111dec628eead87b20773c98a00830580df211acc98Chris Lattner 112dec628eead87b20773c98a00830580df211acc98Chris Lattner // General class unloading is not supported, this is used to prune 113dec628eead87b20773c98a00830580df211acc98Chris Lattner // unwanted classes during image writing. 1142928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar bool RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader) 115dec628eead87b20773c98a00830580df211acc98Chris Lattner LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 116dec628eead87b20773c98a00830580df211acc98Chris Lattner SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 117dec628eead87b20773c98a00830580df211acc98Chris Lattner 118dec628eead87b20773c98a00830580df211acc98Chris Lattner void DumpAllClasses(int flags) 119dec628eead87b20773c98a00830580df211acc98Chris Lattner LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 12056920a0eccb281e4f7cc71cb69c0d23c9d531a84Chris Lattner SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 121eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 122eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer void DumpForSigQuit(std::ostream& os) 123eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 124eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 125eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 1269e3264a1eaf7ed2340859938ab386985a413d602Andrew Lenharth size_t NumLoadedClasses() 127eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 128eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 129eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer 130eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // Resolve a String with the given index from the DexFile, storing the 131eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // result in the DexCache. The referrer is used to identify the 132eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer // target DexCache and ClassLoader to use for resolution. 133eb7116bb08a99897c69570a3789af97343bff9f2Reid Spencer mirror::String* ResolveString(uint32_t string_idx, mirror::ArtMethod* referrer) 134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 135 136 // Resolve a String with the given index from the DexFile, storing the 137 // result in the DexCache. 138 mirror::String* ResolveString(const DexFile& dex_file, uint32_t string_idx, 139 const Handle<mirror::DexCache>& dex_cache) 140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 141 142 // Resolve a Type with the given index from the DexFile, storing the 143 // result in the DexCache. The referrer is used to identity the 144 // target DexCache and ClassLoader to use for resolution. 145 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, mirror::Class* referrer) 146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 147 148 // Resolve a Type with the given index from the DexFile, storing the 149 // result in the DexCache. The referrer is used to identify the 150 // target DexCache and ClassLoader to use for resolution. 151 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtMethod* referrer) 152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 153 154 mirror::Class* ResolveType(uint16_t type_idx, mirror::ArtField* referrer) 155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 156 157 // Resolve a type with the given ID from the DexFile, storing the 158 // result in DexCache. The ClassLoader is used to search for the 159 // type, since it may be referenced from but not contained within 160 // the given DexFile. 161 mirror::Class* ResolveType(const DexFile& dex_file, uint16_t type_idx, 162 const Handle<mirror::DexCache>& dex_cache, 163 const Handle<mirror::ClassLoader>& class_loader) 164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 165 166 // Resolve a method with a given ID from the DexFile, storing the 167 // result in DexCache. The ClassLinker and ClassLoader are used as 168 // in ResolveType. What is unique is the method type argument which 169 // is used to determine if this method is a direct, static, or 170 // virtual method. 171 mirror::ArtMethod* ResolveMethod(const DexFile& dex_file, 172 uint32_t method_idx, 173 const Handle<mirror::DexCache>& dex_cache, 174 const Handle<mirror::ClassLoader>& class_loader, 175 mirror::ArtMethod* referrer, 176 InvokeType type) 177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 178 179 mirror::ArtMethod* ResolveMethod(uint32_t method_idx, mirror::ArtMethod* referrer, 180 InvokeType type) 181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 182 183 mirror::ArtField* ResolveField(uint32_t field_idx, mirror::ArtMethod* referrer, 184 bool is_static) 185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 186 187 // Resolve a field with a given ID from the DexFile, storing the 188 // result in DexCache. The ClassLinker and ClassLoader are used as 189 // in ResolveType. What is unique is the is_static argument which is 190 // used to determine if we are resolving a static or non-static 191 // field. 192 mirror::ArtField* ResolveField(const DexFile& dex_file, 193 uint32_t field_idx, 194 const Handle<mirror::DexCache>& dex_cache, 195 const Handle<mirror::ClassLoader>& class_loader, 196 bool is_static) 197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 198 199 // Resolve a field with a given ID from the DexFile, storing the 200 // result in DexCache. The ClassLinker and ClassLoader are used as 201 // in ResolveType. No is_static argument is provided so that Java 202 // field resolution semantics are followed. 203 mirror::ArtField* ResolveFieldJLS(const DexFile& dex_file, 204 uint32_t field_idx, 205 const Handle<mirror::DexCache>& dex_cache, 206 const Handle<mirror::ClassLoader>& class_loader) 207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 208 209 // Get shorty from method index without resolution. Used to do handlerization. 210 const char* MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, uint32_t* length) 211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 212 213 // Returns true on success, false if there's an exception pending. 214 // can_run_clinit=false allows the compiler to attempt to init a class, 215 // given the restriction that no <clinit> execution is possible. 216 bool EnsureInitialized(const Handle<mirror::Class>& c, 217 bool can_init_fields, bool can_init_parents) 218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 219 220 // Initializes classes that have instances in the image but that have 221 // <clinit> methods so they could not be initialized by the compiler. 222 void RunRootClinits() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 223 224 void RegisterDexFile(const DexFile& dex_file) 225 LOCKS_EXCLUDED(dex_lock_) 226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 227 void RegisterDexFile(const DexFile& dex_file, const Handle<mirror::DexCache>& dex_cache) 228 LOCKS_EXCLUDED(dex_lock_) 229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 230 231 const OatFile* RegisterOatFile(const OatFile* oat_file) 232 LOCKS_EXCLUDED(dex_lock_); 233 234 const std::vector<const DexFile*>& GetBootClassPath() { 235 return boot_class_path_; 236 } 237 238 void VisitClasses(ClassVisitor* visitor, void* arg) 239 LOCKS_EXCLUDED(dex_lock_) 240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 241 // Less efficient variant of VisitClasses that doesn't hold the classlinker_classes_lock_ 242 // when calling the visitor. 243 void VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) 244 LOCKS_EXCLUDED(dex_lock_) 245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 246 247 void VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) 248 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_, dex_lock_); 249 250 mirror::DexCache* FindDexCache(const DexFile& dex_file) const 251 LOCKS_EXCLUDED(dex_lock_) 252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 253 bool IsDexFileRegistered(const DexFile& dex_file) const 254 LOCKS_EXCLUDED(dex_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 255 void FixupDexCaches(mirror::ArtMethod* resolution_method) const 256 LOCKS_EXCLUDED(dex_lock_) 257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 258 259 // Generate an oat file from a dex file 260 bool GenerateOatFile(const char* dex_filename, 261 int oat_fd, 262 const char* oat_cache_filename, 263 std::string* error_msg) 264 LOCKS_EXCLUDED(Locks::mutator_lock_); 265 266 const OatFile* FindOatFileFromOatLocation(const std::string& location, 267 std::string* error_msg) 268 LOCKS_EXCLUDED(dex_lock_); 269 270 // Finds the oat file for a dex location, generating the oat file if 271 // it is missing or out of date. Returns the DexFile from within the 272 // created oat file. 273 const DexFile* FindOrCreateOatFileForDexLocation(const char* dex_location, 274 uint32_t dex_location_checksum, 275 const char* oat_location, 276 std::vector<std::string>* error_msgs) 277 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 278 // Find a DexFile within an OatFile given a DexFile location. Note 279 // that this returns null if the location checksum of the DexFile 280 // does not match the OatFile. 281 const DexFile* FindDexFileInOatFileFromDexLocation(const char* location, 282 const uint32_t* const location_checksum, 283 std::vector<std::string>* error_msgs) 284 LOCKS_EXCLUDED(dex_lock_, Locks::mutator_lock_); 285 286 287 // Returns true if oat file contains the dex file with the given location and checksum. 288 static bool VerifyOatFileChecksums(const OatFile* oat_file, 289 const char* dex_location, 290 uint32_t dex_location_checksum, 291 const InstructionSet instruction_set, 292 std::string* error_msg); 293 294 // TODO: replace this with multiple methods that allocate the correct managed type. 295 template <class T> 296 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length) 297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 298 299 mirror::ObjectArray<mirror::Class>* AllocClassArray(Thread* self, size_t length) 300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 301 302 mirror::ObjectArray<mirror::String>* AllocStringArray(Thread* self, size_t length) 303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 304 305 mirror::ObjectArray<mirror::ArtMethod>* AllocArtMethodArray(Thread* self, size_t length) 306 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 307 308 mirror::IfTable* AllocIfTable(Thread* self, size_t ifcount) 309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 310 311 mirror::ObjectArray<mirror::ArtField>* AllocArtFieldArray(Thread* self, size_t length) 312 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 318 void VerifyClass(const Handle<mirror::Class>& klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 319 bool VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, 320 mirror::Class::Status& oat_file_class_status) 321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 322 void ResolveClassExceptionHandlerTypes(const DexFile& dex_file, 323 const Handle<mirror::Class>& klass) 324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 325 void ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, mirror::ArtMethod* klass) 326 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 327 328 mirror::Class* CreateProxyClass(ScopedObjectAccess& soa, jstring name, jobjectArray interfaces, 329 jobject loader, jobjectArray methods, jobjectArray throws) 330 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 331 std::string GetDescriptorForProxy(mirror::Class* proxy_class) 332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 333 mirror::ArtMethod* FindMethodForProxy(mirror::Class* proxy_class, 334 mirror::ArtMethod* proxy_method) 335 LOCKS_EXCLUDED(dex_lock_) 336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 337 338 // Get the oat code for a method when its class isn't yet initialized 339 const void* GetQuickOatCodeFor(mirror::ArtMethod* method) 340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 341 const void* GetPortableOatCodeFor(mirror::ArtMethod* method, bool* have_portable_code) 342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 343 344 // Get the oat code for a method from a method index. 345 const void* GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx) 346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 347 const void* GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, uint32_t method_idx) 348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 349 350 pid_t GetClassesLockOwner(); // For SignalCatcher. 351 pid_t GetDexLockOwner(); // For SignalCatcher. 352 353 const void* GetPortableResolutionTrampoline() const { 354 return portable_resolution_trampoline_; 355 } 356 357 const void* GetQuickGenericJniTrampoline() const { 358 return quick_generic_jni_trampoline_; 359 } 360 361 const void* GetQuickResolutionTrampoline() const { 362 return quick_resolution_trampoline_; 363 } 364 365 const void* GetPortableImtConflictTrampoline() const { 366 return portable_imt_conflict_trampoline_; 367 } 368 369 const void* GetQuickImtConflictTrampoline() const { 370 return quick_imt_conflict_trampoline_; 371 } 372 373 const void* GetQuickToInterpreterBridgeTrampoline() const { 374 return quick_to_interpreter_bridge_trampoline_; 375 } 376 377 InternTable* GetInternTable() const { 378 return intern_table_; 379 } 380 381 // Attempts to insert a class into a class table. Returns NULL if 382 // the class was inserted, otherwise returns an existing class with 383 // the same descriptor and ClassLoader. 384 mirror::Class* InsertClass(const char* descriptor, mirror::Class* klass, size_t hash) 385 LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 386 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 387 388 // Special code to allocate an art method, use this instead of class->AllocObject. 389 mirror::ArtMethod* AllocArtMethod(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 390 391 private: 392 const OatFile::OatMethod GetOatMethodFor(mirror::ArtMethod* method) 393 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 394 395 OatFile& GetImageOatFile(gc::space::ImageSpace* space) 396 LOCKS_EXCLUDED(dex_lock_) 397 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 398 399 void FinishInit(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 400 401 // For early bootstrapping by Init 402 mirror::Class* AllocClass(Thread* self, mirror::Class* java_lang_Class, uint32_t class_size) 403 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 404 405 // Alloc* convenience functions to avoid needing to pass in mirror::Class* 406 // values that are known to the ClassLinker such as 407 // kObjectArrayClass and kJavaLangString etc. 408 mirror::Class* AllocClass(Thread* self, uint32_t class_size) 409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 410 mirror::DexCache* AllocDexCache(Thread* self, const DexFile& dex_file) 411 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 412 mirror::ArtField* AllocArtField(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 413 414 mirror::Class* CreatePrimitiveClass(Thread* self, Primitive::Type type) 415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 416 mirror::Class* InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type) 417 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 418 419 420 mirror::Class* CreateArrayClass(Thread* self, const char* descriptor, 421 const Handle<mirror::ClassLoader>& class_loader) 422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 423 424 void AppendToBootClassPath(const DexFile& dex_file) 425 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 426 void AppendToBootClassPath(const DexFile& dex_file, const Handle<mirror::DexCache>& dex_cache) 427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 428 429 void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, 430 mirror::Class* c, SafeMap<uint32_t, mirror::ArtField*>& field_map) 431 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 432 433 uint32_t SizeOfClass(const DexFile& dex_file, 434 const DexFile::ClassDef& dex_class_def); 435 436 void LoadClass(const DexFile& dex_file, 437 const DexFile::ClassDef& dex_class_def, 438 const Handle<mirror::Class>& klass, 439 mirror::ClassLoader* class_loader) 440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 441 void LoadClassMembers(const DexFile& dex_file, 442 const byte* class_data, 443 const Handle<mirror::Class>& klass, 444 mirror::ClassLoader* class_loader, 445 const OatFile::OatClass* oat_class) 446 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 447 448 void LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, 449 const Handle<mirror::Class>& klass, const Handle<mirror::ArtField>& dst) 450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 451 452 mirror::ArtMethod* LoadMethod(Thread* self, const DexFile& dex_file, 453 const ClassDataItemIterator& dex_method, 454 const Handle<mirror::Class>& klass) 455 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 456 457 void FixupStaticTrampolines(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 458 459 // Finds the associated oat class for a dex_file and descriptor 460 OatFile::OatClass GetOatClass(const DexFile& dex_file, uint16_t class_def_idx) 461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 462 463 void RegisterDexFileLocked(const DexFile& dex_file, const Handle<mirror::DexCache>& dex_cache) 464 EXCLUSIVE_LOCKS_REQUIRED(dex_lock_) 465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 466 bool IsDexFileRegisteredLocked(const DexFile& dex_file) const 467 SHARED_LOCKS_REQUIRED(dex_lock_, Locks::mutator_lock_); 468 469 bool InitializeClass(const Handle<mirror::Class>& klass, bool can_run_clinit, 470 bool can_init_parents) 471 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 472 bool WaitForInitializeClass(const Handle<mirror::Class>& klass, Thread* self, 473 ObjectLock<mirror::Class>& lock); 474 bool ValidateSuperClassDescriptors(const Handle<mirror::Class>& klass) 475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 476 477 bool IsSameDescriptorInDifferentClassContexts(Thread* self, const char* descriptor, 478 Handle<mirror::ClassLoader>& class_loader1, 479 Handle<mirror::ClassLoader>& class_loader2) 480 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 481 482 bool IsSameMethodSignatureInDifferentClassContexts(Thread* self, mirror::ArtMethod* method, 483 mirror::Class* klass1, 484 mirror::Class* klass2) 485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 486 487 bool LinkClass(Thread* self, const Handle<mirror::Class>& klass, 488 const Handle<mirror::ObjectArray<mirror::Class> >& interfaces) 489 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 490 491 bool LinkSuperClass(const Handle<mirror::Class>& klass) 492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 493 494 bool LoadSuperAndInterfaces(const Handle<mirror::Class>& klass, const DexFile& dex_file) 495 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 496 497 bool LinkMethods(const Handle<mirror::Class>& klass, 498 const Handle<mirror::ObjectArray<mirror::Class> >& interfaces) 499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 500 501 bool LinkVirtualMethods(const Handle<mirror::Class>& klass) 502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 503 504 bool LinkInterfaceMethods(const Handle<mirror::Class>& klass, 505 const Handle<mirror::ObjectArray<mirror::Class> >& interfaces) 506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 507 508 bool LinkStaticFields(const Handle<mirror::Class>& klass) 509 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 510 bool LinkInstanceFields(const Handle<mirror::Class>& klass) 511 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 512 bool LinkFields(const Handle<mirror::Class>& klass, bool is_static) 513 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 514 515 516 void CreateReferenceInstanceOffsets(const Handle<mirror::Class>& klass) 517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 518 void CreateReferenceStaticOffsets(const Handle<mirror::Class>& klass) 519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 520 void CreateReferenceOffsets(const Handle<mirror::Class>& klass, bool is_static, 521 uint32_t reference_offsets) 522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 523 524 // For use by ImageWriter to find DexCaches for its roots 525 const std::vector<mirror::DexCache*>& GetDexCaches() { 526 return dex_caches_; 527 } 528 529 const OatFile* FindOpenedOatFileForDexFile(const DexFile& dex_file) 530 LOCKS_EXCLUDED(dex_lock_) 531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 532 const OatFile* FindOpenedOatFileFromDexLocation(const char* dex_location, 533 const uint32_t* const dex_location_checksum) 534 LOCKS_EXCLUDED(dex_lock_); 535 const OatFile* FindOpenedOatFileFromOatLocation(const std::string& oat_location) 536 LOCKS_EXCLUDED(dex_lock_); 537 const DexFile* FindDexFileInOatLocation(const char* dex_location, 538 uint32_t dex_location_checksum, 539 const char* oat_location, 540 std::string* error_msg) 541 LOCKS_EXCLUDED(dex_lock_); 542 543 const DexFile* VerifyAndOpenDexFileFromOatFile(const std::string& oat_file_location, 544 const char* dex_location, 545 std::string* error_msg, 546 bool* open_failed) 547 LOCKS_EXCLUDED(dex_lock_); 548 549 mirror::ArtMethod* CreateProxyConstructor(Thread* self, const Handle<mirror::Class>& klass, 550 mirror::Class* proxy_class) 551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 552 mirror::ArtMethod* CreateProxyMethod(Thread* self, const Handle<mirror::Class>& klass, 553 const Handle<mirror::ArtMethod>& prototype) 554 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 555 556 std::vector<const DexFile*> boot_class_path_; 557 558 mutable ReaderWriterMutex dex_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 559 std::vector<size_t> new_dex_cache_roots_ GUARDED_BY(dex_lock_);; 560 std::vector<mirror::DexCache*> dex_caches_ GUARDED_BY(dex_lock_); 561 std::vector<const OatFile*> oat_files_ GUARDED_BY(dex_lock_); 562 563 564 // multimap from a string hash code of a class descriptor to 565 // mirror::Class* instances. Results should be compared for a matching 566 // Class::descriptor_ and Class::class_loader_. 567 typedef std::multimap<size_t, mirror::Class*> Table; 568 Table class_table_ GUARDED_BY(Locks::classlinker_classes_lock_); 569 std::vector<std::pair<size_t, mirror::Class*> > new_class_roots_; 570 571 // Do we need to search dex caches to find image classes? 572 bool dex_cache_image_class_lookup_required_; 573 // Number of times we've searched dex caches for a class. After a certain number of misses we move 574 // the classes into the class_table_ to avoid dex cache based searches. 575 AtomicInteger failed_dex_cache_class_lookups_; 576 577 mirror::Class* LookupClassFromTableLocked(const char* descriptor, 578 const mirror::ClassLoader* class_loader, 579 size_t hash) 580 SHARED_LOCKS_REQUIRED(Locks::classlinker_classes_lock_, Locks::mutator_lock_); 581 582 void MoveImageClassesToClassTable() LOCKS_EXCLUDED(Locks::classlinker_classes_lock_) 583 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 584 mirror::Class* LookupClassFromImage(const char* descriptor) 585 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 586 587 // indexes into class_roots_. 588 // needs to be kept in sync with class_roots_descriptors_. 589 enum ClassRoot { 590 kJavaLangClass, 591 kJavaLangObject, 592 kClassArrayClass, 593 kObjectArrayClass, 594 kJavaLangString, 595 kJavaLangDexCache, 596 kJavaLangRefReference, 597 kJavaLangReflectArtField, 598 kJavaLangReflectArtMethod, 599 kJavaLangReflectProxy, 600 kJavaLangStringArrayClass, 601 kJavaLangReflectArtFieldArrayClass, 602 kJavaLangReflectArtMethodArrayClass, 603 kJavaLangClassLoader, 604 kJavaLangThrowable, 605 kJavaLangClassNotFoundException, 606 kJavaLangStackTraceElement, 607 kPrimitiveBoolean, 608 kPrimitiveByte, 609 kPrimitiveChar, 610 kPrimitiveDouble, 611 kPrimitiveFloat, 612 kPrimitiveInt, 613 kPrimitiveLong, 614 kPrimitiveShort, 615 kPrimitiveVoid, 616 kBooleanArrayClass, 617 kByteArrayClass, 618 kCharArrayClass, 619 kDoubleArrayClass, 620 kFloatArrayClass, 621 kIntArrayClass, 622 kLongArrayClass, 623 kShortArrayClass, 624 kJavaLangStackTraceElementArrayClass, 625 kClassRootsMax, 626 }; 627 mirror::ObjectArray<mirror::Class>* class_roots_; 628 629 mirror::Class* GetClassRoot(ClassRoot class_root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 630 631 void SetClassRoot(ClassRoot class_root, mirror::Class* klass) 632 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 633 634 mirror::ObjectArray<mirror::Class>* GetClassRoots() { 635 DCHECK(class_roots_ != NULL); 636 return class_roots_; 637 } 638 639 static const char* class_roots_descriptors_[]; 640 641 const char* GetClassRootDescriptor(ClassRoot class_root) { 642 const char* descriptor = class_roots_descriptors_[class_root]; 643 CHECK(descriptor != NULL); 644 return descriptor; 645 } 646 647 // The interface table used by all arrays. 648 mirror::IfTable* array_iftable_; 649 650 // A cache of the last FindArrayClass results. The cache serves to avoid creating array class 651 // descriptors for the sake of performing FindClass. 652 static constexpr size_t kFindArrayCacheSize = 16; 653 mirror::Class* find_array_class_cache_[kFindArrayCacheSize]; 654 size_t find_array_class_cache_next_victim_; 655 656 bool init_done_; 657 bool log_new_dex_caches_roots_ GUARDED_BY(dex_lock_); 658 bool log_new_class_table_roots_ GUARDED_BY(Locks::classlinker_classes_lock_); 659 660 InternTable* intern_table_; 661 662 const void* portable_resolution_trampoline_; 663 const void* quick_resolution_trampoline_; 664 const void* portable_imt_conflict_trampoline_; 665 const void* quick_imt_conflict_trampoline_; 666 const void* quick_generic_jni_trampoline_; 667 const void* quick_to_interpreter_bridge_trampoline_; 668 669 friend class ImageWriter; // for GetClassRoots 670 FRIEND_TEST(ClassLinkerTest, ClassRootDescriptors); 671 FRIEND_TEST(mirror::DexCacheTest, Open); 672 FRIEND_TEST(ExceptionTest, FindExceptionHandler); 673 FRIEND_TEST(ObjectTest, AllocObjectArray); 674 DISALLOW_COPY_AND_ASSIGN(ClassLinker); 675}; 676 677} // namespace art 678 679#endif // ART_RUNTIME_CLASS_LINKER_H_ 680