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" |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 22 | #include "dex_file-inl.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 23 | #include "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>)); |
| 44 | GcRoot<mirror::Object>* slot = reinterpret_cast<GcRoot<mirror::Object>*>( |
| 45 | const_cast<uint8_t*>(oat_file->BssBegin() + bss_offset)); |
| 46 | DCHECK_GE(slot, oat_file->GetBssGcRoots().data()); |
| 47 | DCHECK_LT(slot, oat_file->GetBssGcRoots().data() + oat_file->GetBssGcRoots().size()); |
| 48 | if (slot->IsNull()) { |
| 49 | // This may race with another thread trying to store the very same value but that's OK. |
| 50 | *slot = GcRoot<mirror::Object>(object); |
| 51 | // 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] | 52 | ObjPtr<mirror::ClassLoader> class_loader = outer_method->GetClassLoader(); |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 53 | Runtime* runtime = Runtime::Current(); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 54 | if (kIsDebugBuild) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 55 | ClassTable* class_table = runtime->GetClassLinker()->ClassTableForClassLoader(class_loader); |
| 56 | CHECK(class_table != nullptr && !class_table->InsertOatFile(oat_file)) |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 57 | << "Oat file with .bss GC roots was not registered in class table: " |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 58 | << oat_file->GetLocation(); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 59 | } |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 60 | if (class_loader != nullptr) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 61 | runtime->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 62 | } else { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 63 | runtime->GetClassLinker()->WriteBarrierForBootOatFileBssRoots(oat_file); |
| 64 | } |
| 65 | } else { |
| 66 | // Each slot serves to store exactly one Class or String. |
| 67 | DCHECK_EQ(object, slot->Read()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static inline void StoreTypeInBss(ArtMethod* outer_method, |
| 72 | dex::TypeIndex type_idx, |
| 73 | ObjPtr<mirror::Class> resolved_type) |
| 74 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 75 | const DexFile* dex_file = outer_method->GetDexFile(); |
| 76 | DCHECK(dex_file != nullptr); |
| 77 | const OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 78 | if (oat_dex_file != nullptr) { |
| 79 | size_t bss_offset = IndexBssMappingLookup::GetBssOffset(oat_dex_file->GetTypeBssMapping(), |
| 80 | type_idx.index_, |
| 81 | dex_file->NumTypeIds(), |
| 82 | sizeof(GcRoot<mirror::Class>)); |
| 83 | if (bss_offset != IndexBssMappingLookup::npos) { |
| 84 | StoreObjectInBss(outer_method, oat_dex_file->GetOatFile(), bss_offset, resolved_type); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static inline void StoreStringInBss(ArtMethod* outer_method, |
| 90 | dex::StringIndex string_idx, |
| 91 | ObjPtr<mirror::String> resolved_string) |
| 92 | REQUIRES_SHARED(Locks::mutator_lock_) __attribute__((optnone)) { |
| 93 | const DexFile* dex_file = outer_method->GetDexFile(); |
| 94 | DCHECK(dex_file != nullptr); |
| 95 | const OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 96 | if (oat_dex_file != nullptr) { |
| 97 | size_t bss_offset = IndexBssMappingLookup::GetBssOffset(oat_dex_file->GetStringBssMapping(), |
| 98 | string_idx.index_, |
| 99 | dex_file->NumStringIds(), |
| 100 | sizeof(GcRoot<mirror::Class>)); |
| 101 | if (bss_offset != IndexBssMappingLookup::npos) { |
| 102 | StoreObjectInBss(outer_method, oat_dex_file->GetOatFile(), bss_offset, resolved_string); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 103 | } |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 107 | extern "C" mirror::Class* artInitializeStaticStorageFromCode(uint32_t type_idx, Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 108 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 109 | // Called to ensure static storage base is initialized for direct static field reads and writes. |
| 110 | // A class may be accessing another class' fields when it doesn't have access, as access has been |
| 111 | // given by inheritance. |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 112 | ScopedQuickEntrypointChecks sqec(self); |
Mingyao Yang | 0a87a65 | 2017-04-12 13:43:15 -0700 | [diff] [blame] | 113 | auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod( |
| 114 | self, CalleeSaveType::kSaveEverythingForClinit); |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 115 | ArtMethod* caller = caller_and_outer.caller; |
| 116 | mirror::Class* result = |
| 117 | ResolveVerifyAndClinit(dex::TypeIndex(type_idx), caller, self, true, false); |
| 118 | if (LIKELY(result != nullptr)) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 119 | StoreTypeInBss(caller_and_outer.outer_method, dex::TypeIndex(type_idx), result); |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 120 | } |
| 121 | return result; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 124 | extern "C" mirror::Class* artInitializeTypeFromCode(uint32_t type_idx, Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 125 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 126 | // 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] | 127 | ScopedQuickEntrypointChecks sqec(self); |
Mingyao Yang | 0a87a65 | 2017-04-12 13:43:15 -0700 | [diff] [blame] | 128 | auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod( |
| 129 | self, CalleeSaveType::kSaveEverythingForClinit); |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 130 | ArtMethod* caller = caller_and_outer.caller; |
| 131 | mirror::Class* result = |
| 132 | ResolveVerifyAndClinit(dex::TypeIndex(type_idx), caller, self, false, false); |
| 133 | if (LIKELY(result != nullptr)) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 134 | StoreTypeInBss(caller_and_outer.outer_method, dex::TypeIndex(type_idx), result); |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 135 | } |
| 136 | return result; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 139 | extern "C" mirror::Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx, Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 140 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 141 | // Called when caller isn't guaranteed to have access to a type. |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 142 | ScopedQuickEntrypointChecks sqec(self); |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 143 | auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self, |
| 144 | CalleeSaveType::kSaveEverything); |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 145 | ArtMethod* caller = caller_and_outer.caller; |
| 146 | mirror::Class* result = |
| 147 | ResolveVerifyAndClinit(dex::TypeIndex(type_idx), caller, self, false, true); |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 148 | // Do not StoreTypeInBss(); access check entrypoint is never used together with .bss. |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 149 | return result; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 152 | extern "C" mirror::String* artResolveStringFromCode(int32_t string_idx, Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 153 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 154 | ScopedQuickEntrypointChecks sqec(self); |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 155 | auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self, |
| 156 | CalleeSaveType::kSaveEverything); |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 157 | ArtMethod* caller = caller_and_outer.caller; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 158 | mirror::String* result = ResolveStringFromCode(caller, dex::StringIndex(string_idx)); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 159 | if (LIKELY(result != nullptr)) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame^] | 160 | StoreStringInBss(caller_and_outer.outer_method, dex::StringIndex(string_idx), result); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 161 | } |
| 162 | return result; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } // namespace art |