| Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | */ |
| 16 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 17 | #include "art_method-inl.h" |
| Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 18 | #include "base/callee_save_type.h" |
| Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 19 | #include "callee_save_frame.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "class_linker-inl.h" |
| Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 21 | #include "class_table-inl.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/dex_file-inl.h" |
| 23 | #include "dex/dex_file_types.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "entrypoints/entrypoint_utils-inl.h" |
| Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 25 | #include "gc/heap.h" |
| 26 | #include "mirror/class-inl.h" |
| 27 | #include "mirror/class_loader.h" |
| Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 28 | #include "mirror/object-inl.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 29 | #include "mirror/object_array-inl.h" |
| Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 30 | #include "oat_file.h" |
| 31 | #include "runtime.h" |
| Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 35 | static void StoreObjectInBss(ArtMethod* outer_method, |
| 36 | const OatFile* oat_file, |
| 37 | size_t bss_offset, |
| 38 | ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 39 | // Used for storing Class or String in .bss GC roots. |
| 40 | static_assert(sizeof(GcRoot<mirror::Class>) == sizeof(GcRoot<mirror::Object>), "Size check."); |
| 41 | static_assert(sizeof(GcRoot<mirror::String>) == sizeof(GcRoot<mirror::Object>), "Size check."); |
| 42 | DCHECK_NE(bss_offset, IndexBssMappingLookup::npos); |
| 43 | DCHECK_ALIGNED(bss_offset, sizeof(GcRoot<mirror::Object>)); |
| Vladimir Marko | 8bb72b6 | 2017-11-30 17:09:50 +0000 | [diff] [blame] | 44 | if (UNLIKELY(!oat_file->IsExecutable())) { |
| 45 | // There are situations where we execute bytecode tied to an oat file opened |
| 46 | // as non-executable (i.e. the AOT-compiled code cannot be executed) and we |
| 47 | // can JIT that bytecode and get here without the .bss being mmapped. |
| 48 | return; |
| 49 | } |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 50 | GcRoot<mirror::Object>* slot = reinterpret_cast<GcRoot<mirror::Object>*>( |
| 51 | const_cast<uint8_t*>(oat_file->BssBegin() + bss_offset)); |
| 52 | DCHECK_GE(slot, oat_file->GetBssGcRoots().data()); |
| 53 | DCHECK_LT(slot, oat_file->GetBssGcRoots().data() + oat_file->GetBssGcRoots().size()); |
| 54 | if (slot->IsNull()) { |
| 55 | // This may race with another thread trying to store the very same value but that's OK. |
| Vladimir Marko | d5fd5c3 | 2019-07-02 14:46:32 +0100 | [diff] [blame] | 56 | std::atomic<GcRoot<mirror::Object>>* atomic_slot = |
| 57 | reinterpret_cast<std::atomic<GcRoot<mirror::Object>>*>(slot); |
| 58 | static_assert(sizeof(*slot) == sizeof(*atomic_slot), "Size check"); |
| 59 | atomic_slot->store(GcRoot<mirror::Object>(object), std::memory_order_release); |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 60 | // We need a write barrier for the class loader that holds the GC roots in the .bss. |
| Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 61 | ObjPtr<mirror::ClassLoader> class_loader = outer_method->GetClassLoader(); |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 62 | Runtime* runtime = Runtime::Current(); |
| Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 63 | if (kIsDebugBuild) { |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 64 | ClassTable* class_table = runtime->GetClassLinker()->ClassTableForClassLoader(class_loader); |
| 65 | CHECK(class_table != nullptr && !class_table->InsertOatFile(oat_file)) |
| Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 66 | << "Oat file with .bss GC roots was not registered in class table: " |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 67 | << oat_file->GetLocation(); |
| Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 68 | } |
| Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 69 | if (class_loader != nullptr) { |
| Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 70 | WriteBarrier::ForEveryFieldWrite(class_loader); |
| Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 71 | } else { |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 72 | runtime->GetClassLinker()->WriteBarrierForBootOatFileBssRoots(oat_file); |
| 73 | } |
| 74 | } else { |
| 75 | // Each slot serves to store exactly one Class or String. |
| 76 | DCHECK_EQ(object, slot->Read()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static inline void StoreTypeInBss(ArtMethod* outer_method, |
| 81 | dex::TypeIndex type_idx, |
| 82 | ObjPtr<mirror::Class> resolved_type) |
| 83 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 84 | const DexFile* dex_file = outer_method->GetDexFile(); |
| 85 | DCHECK(dex_file != nullptr); |
| 86 | const OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 87 | if (oat_dex_file != nullptr) { |
| Vladimir Marko | 8f63f10 | 2020-09-28 12:10:28 +0100 | [diff] [blame] | 88 | auto store = [=](const IndexBssMapping* mapping) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 89 | size_t bss_offset = IndexBssMappingLookup::GetBssOffset(mapping, |
| 90 | type_idx.index_, |
| 91 | dex_file->NumTypeIds(), |
| 92 | sizeof(GcRoot<mirror::Class>)); |
| 93 | if (bss_offset != IndexBssMappingLookup::npos) { |
| 94 | StoreObjectInBss(outer_method, oat_dex_file->GetOatFile(), bss_offset, resolved_type); |
| 95 | } |
| 96 | }; |
| 97 | store(oat_dex_file->GetTypeBssMapping()); |
| 98 | if (resolved_type->IsPublic()) { |
| 99 | store(oat_dex_file->GetPublicTypeBssMapping()); |
| 100 | } |
| 101 | if (resolved_type->IsPublic() || |
| 102 | resolved_type->GetClassLoader() == outer_method->GetClassLoader()) { |
| 103 | store(oat_dex_file->GetPackageTypeBssMapping()); |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static inline void StoreStringInBss(ArtMethod* outer_method, |
| 109 | dex::StringIndex string_idx, |
| 110 | ObjPtr<mirror::String> resolved_string) |
| Vladimir Marko | a9f303c | 2018-07-20 16:43:56 +0100 | [diff] [blame] | 111 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 112 | const DexFile* dex_file = outer_method->GetDexFile(); |
| 113 | DCHECK(dex_file != nullptr); |
| 114 | const OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 115 | if (oat_dex_file != nullptr) { |
| 116 | size_t bss_offset = IndexBssMappingLookup::GetBssOffset(oat_dex_file->GetStringBssMapping(), |
| 117 | string_idx.index_, |
| 118 | dex_file->NumStringIds(), |
| 119 | sizeof(GcRoot<mirror::Class>)); |
| 120 | if (bss_offset != IndexBssMappingLookup::npos) { |
| 121 | StoreObjectInBss(outer_method, oat_dex_file->GetOatFile(), bss_offset, resolved_string); |
| Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 122 | } |
| Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| Vladimir Marko | 4e08fad | 2017-11-23 12:52:36 +0000 | [diff] [blame] | 126 | static ALWAYS_INLINE bool CanReferenceBss(ArtMethod* outer_method, ArtMethod* caller) |
| 127 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 128 | // .bss references are used only for AOT-compiled code and only when the instruction |
| 129 | // originates from the outer method's dex file and the type or string index is tied to |
| 130 | // that dex file. As we do not want to check if the call is coming from AOT-compiled |
| 131 | // code (that could be expensive), simply check if the caller has the same dex file. |
| 132 | // |
| 133 | // If we've accepted running AOT-compiled code despite the runtime class loader |
| 134 | // resolving the caller to a different dex file, this check shall prevent us from |
| 135 | // filling the .bss slot and we shall keep going through the slow path. This is slow |
| 136 | // but correct; we do not really care that much about performance in this odd case. |
| 137 | // |
| 138 | // JIT can inline throwing instructions across dex files and this check prevents |
| 139 | // looking up the index in the wrong dex file in that case. If the caller and outer |
| 140 | // method have the same dex file, we may or may not find a .bss slot to update; |
| 141 | // if we do, this can still benefit AOT-compiled code executed later. |
| 142 | return outer_method->GetDexFile() == caller->GetDexFile(); |
| 143 | } |
| 144 | |
| Vladimir Marko | a9f303c | 2018-07-20 16:43:56 +0100 | [diff] [blame] | 145 | extern "C" mirror::Class* artInitializeStaticStorageFromCode(mirror::Class* klass, Thread* self) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 146 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 147 | // Called to ensure static storage base is initialized for direct static field reads and writes. |
| 148 | // A class may be accessing another class' fields when it doesn't have access, as access has been |
| 149 | // given by inheritance. |
| Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 150 | ScopedQuickEntrypointChecks sqec(self); |
| Vladimir Marko | a9f303c | 2018-07-20 16:43:56 +0100 | [diff] [blame] | 151 | DCHECK(klass != nullptr); |
| 152 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 153 | StackHandleScope<1> hs(self); |
| 154 | Handle<mirror::Class> h_klass = hs.NewHandle(klass); |
| 155 | bool success = class_linker->EnsureInitialized( |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 156 | self, h_klass, /* can_init_fields= */ true, /* can_init_parents= */ true); |
| Vladimir Marko | a9f303c | 2018-07-20 16:43:56 +0100 | [diff] [blame] | 157 | if (UNLIKELY(!success)) { |
| 158 | return nullptr; |
| Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 159 | } |
| Vladimir Marko | a9f303c | 2018-07-20 16:43:56 +0100 | [diff] [blame] | 160 | return h_klass.Get(); |
| Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| Vladimir Marko | 9d47925 | 2018-07-24 11:35:20 +0100 | [diff] [blame] | 163 | extern "C" mirror::Class* artResolveTypeFromCode(uint32_t type_idx, Thread* self) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 164 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 165 | // Called when the .bss slot was empty or for main-path runtime call. |
| Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 166 | ScopedQuickEntrypointChecks sqec(self); |
| Mingyao Yang | 0a87a65 | 2017-04-12 13:43:15 -0700 | [diff] [blame] | 167 | auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod( |
| 168 | self, CalleeSaveType::kSaveEverythingForClinit); |
| Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 169 | ArtMethod* caller = caller_and_outer.caller; |
| Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 170 | ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx), |
| 171 | caller, |
| 172 | self, |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 173 | /* can_run_clinit= */ false, |
| 174 | /* verify_access= */ false); |
| Vladimir Marko | 4e08fad | 2017-11-23 12:52:36 +0000 | [diff] [blame] | 175 | if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) { |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 176 | StoreTypeInBss(caller_and_outer.outer_method, dex::TypeIndex(type_idx), result); |
| Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 177 | } |
| Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 178 | return result.Ptr(); |
| Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| Vladimir Marko | 9d47925 | 2018-07-24 11:35:20 +0100 | [diff] [blame] | 181 | extern "C" mirror::Class* artResolveTypeAndVerifyAccessFromCode(uint32_t type_idx, Thread* self) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 182 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 183 | // Called when caller isn't guaranteed to have access to a type. |
| Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 184 | ScopedQuickEntrypointChecks sqec(self); |
| Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 185 | auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self, |
| 186 | CalleeSaveType::kSaveEverything); |
| Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 187 | ArtMethod* caller = caller_and_outer.caller; |
| Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 188 | ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx), |
| 189 | caller, |
| 190 | self, |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 191 | /* can_run_clinit= */ false, |
| 192 | /* verify_access= */ true); |
| Vladimir Marko | 8f63f10 | 2020-09-28 12:10:28 +0100 | [diff] [blame] | 193 | if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) { |
| 194 | StoreTypeInBss(caller_and_outer.outer_method, dex::TypeIndex(type_idx), result); |
| 195 | } |
| Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 196 | return result.Ptr(); |
| Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| Orion Hodson | dbaa5c7 | 2018-05-10 08:22:46 +0100 | [diff] [blame] | 199 | extern "C" mirror::MethodHandle* artResolveMethodHandleFromCode(uint32_t method_handle_idx, |
| 200 | Thread* self) |
| 201 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 202 | ScopedQuickEntrypointChecks sqec(self); |
| 203 | auto caller_and_outer = |
| 204 | GetCalleeSaveMethodCallerAndOuterMethod(self, CalleeSaveType::kSaveEverything); |
| 205 | ArtMethod* caller = caller_and_outer.caller; |
| 206 | ObjPtr<mirror::MethodHandle> result = ResolveMethodHandleFromCode(caller, method_handle_idx); |
| 207 | return result.Ptr(); |
| 208 | } |
| 209 | |
| Orion Hodson | 18259d7 | 2018-04-12 11:18:23 +0100 | [diff] [blame] | 210 | extern "C" mirror::MethodType* artResolveMethodTypeFromCode(uint32_t proto_idx, Thread* self) |
| 211 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 212 | ScopedQuickEntrypointChecks sqec(self); |
| 213 | auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self, |
| 214 | CalleeSaveType::kSaveEverything); |
| 215 | ArtMethod* caller = caller_and_outer.caller; |
| Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 216 | ObjPtr<mirror::MethodType> result = ResolveMethodTypeFromCode(caller, dex::ProtoIndex(proto_idx)); |
| Orion Hodson | 18259d7 | 2018-04-12 11:18:23 +0100 | [diff] [blame] | 217 | return result.Ptr(); |
| 218 | } |
| 219 | |
| Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 220 | extern "C" mirror::String* artResolveStringFromCode(int32_t string_idx, Thread* self) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 221 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 222 | ScopedQuickEntrypointChecks sqec(self); |
| Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 223 | auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self, |
| 224 | CalleeSaveType::kSaveEverything); |
| Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 225 | ArtMethod* caller = caller_and_outer.caller; |
| Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 226 | ObjPtr<mirror::String> result = |
| 227 | Runtime::Current()->GetClassLinker()->ResolveString(dex::StringIndex(string_idx), caller); |
| Vladimir Marko | 4e08fad | 2017-11-23 12:52:36 +0000 | [diff] [blame] | 228 | if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) { |
| Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 229 | StoreStringInBss(caller_and_outer.outer_method, dex::StringIndex(string_idx), result); |
| Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 230 | } |
| Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 231 | return result.Ptr(); |
| Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | } // namespace art |