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
17 #include "art_method-inl.h"
18 #include "base/callee_save_type.h"
19 #include "callee_save_frame.h"
20 #include "class_linker-inl.h"
21 #include "class_table-inl.h"
22 #include "dex/dex_file-inl.h"
23 #include "dex/dex_file_types.h"
24 #include "entrypoints/entrypoint_utils-inl.h"
25 #include "gc/heap.h"
26 #include "jvalue-inl.h"
27 #include "mirror/class-inl.h"
28 #include "mirror/class_loader.h"
29 #include "mirror/object-inl.h"
30 #include "mirror/object_array-inl.h"
31 #include "oat_file.h"
32 #include "oat_file-inl.h"
33 #include "runtime.h"
34
35 namespace art {
36
StoreObjectInBss(ArtMethod * outer_method,const OatFile * oat_file,size_t bss_offset,ObjPtr<mirror::Object> object)37 static void StoreObjectInBss(ArtMethod* outer_method,
38 const OatFile* oat_file,
39 size_t bss_offset,
40 ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_) {
41 // Used for storing Class or String in .bss GC roots.
42 static_assert(sizeof(GcRoot<mirror::Class>) == sizeof(GcRoot<mirror::Object>), "Size check.");
43 static_assert(sizeof(GcRoot<mirror::String>) == sizeof(GcRoot<mirror::Object>), "Size check.");
44 DCHECK_NE(bss_offset, IndexBssMappingLookup::npos);
45 DCHECK_ALIGNED(bss_offset, sizeof(GcRoot<mirror::Object>));
46 DCHECK_NE(oat_file, nullptr);
47 if (UNLIKELY(!oat_file->IsExecutable())) {
48 // There are situations where we execute bytecode tied to an oat file opened
49 // as non-executable (i.e. the AOT-compiled code cannot be executed) and we
50 // can JIT that bytecode and get here without the .bss being mmapped.
51 return;
52 }
53 GcRoot<mirror::Object>* slot = reinterpret_cast<GcRoot<mirror::Object>*>(
54 const_cast<uint8_t*>(oat_file->BssBegin() + bss_offset));
55 DCHECK_GE(slot, oat_file->GetBssGcRoots().data());
56 DCHECK_LT(slot, oat_file->GetBssGcRoots().data() + oat_file->GetBssGcRoots().size());
57 if (slot->IsNull()) {
58 // This may race with another thread trying to store the very same value but that's OK.
59 std::atomic<GcRoot<mirror::Object>>* atomic_slot =
60 reinterpret_cast<std::atomic<GcRoot<mirror::Object>>*>(slot);
61 static_assert(sizeof(*slot) == sizeof(*atomic_slot), "Size check");
62 atomic_slot->store(GcRoot<mirror::Object>(object), std::memory_order_release);
63 // We need a write barrier for the class loader that holds the GC roots in the .bss.
64 ObjPtr<mirror::ClassLoader> class_loader = outer_method->GetClassLoader();
65 Runtime* runtime = Runtime::Current();
66 if (kIsDebugBuild) {
67 ClassTable* class_table = runtime->GetClassLinker()->ClassTableForClassLoader(class_loader);
68 CHECK(class_table != nullptr && !class_table->InsertOatFile(oat_file))
69 << "Oat file with .bss GC roots was not registered in class table: "
70 << oat_file->GetLocation() << ", " << outer_method->PrettyMethod();
71 }
72 if (class_loader != nullptr) {
73 WriteBarrier::ForEveryFieldWrite(class_loader);
74 } else {
75 runtime->GetClassLinker()->WriteBarrierForBootOatFileBssRoots(oat_file);
76 }
77 } else {
78 // Each slot serves to store exactly one Class or String.
79 DCHECK_EQ(object, slot->Read());
80 }
81 }
82
StoreTypeInBss(ArtMethod * caller,dex::TypeIndex type_idx,ObjPtr<mirror::Class> resolved_type,ArtMethod * outer_method)83 static inline void StoreTypeInBss(ArtMethod* caller,
84 dex::TypeIndex type_idx,
85 ObjPtr<mirror::Class> resolved_type,
86 ArtMethod* outer_method) REQUIRES_SHARED(Locks::mutator_lock_) {
87 const DexFile* dex_file = caller->GetDexFile();
88 DCHECK_NE(dex_file, nullptr);
89
90 if (outer_method->GetDexFile()->GetOatDexFile() == nullptr ||
91 outer_method->GetDexFile()->GetOatDexFile()->GetOatFile() == nullptr) {
92 // No OatFile to update.
93 return;
94 }
95 const OatFile* outer_oat_file = outer_method->GetDexFile()->GetOatDexFile()->GetOatFile();
96
97 // DexFiles compiled together to an oat file case.
98 const OatDexFile* oat_dex_file = dex_file->GetOatDexFile();
99 const IndexBssMapping* type_mapping = nullptr;
100 const IndexBssMapping* public_type_mapping = nullptr;
101 const IndexBssMapping* package_type_mapping = nullptr;
102 if (oat_dex_file != nullptr && oat_dex_file->GetOatFile() == outer_oat_file) {
103 type_mapping = oat_dex_file->GetTypeBssMapping();
104 public_type_mapping = oat_dex_file->GetPublicTypeBssMapping();
105 package_type_mapping = oat_dex_file->GetPackageTypeBssMapping();
106 } else {
107 // Try to find the DexFile in the BCP of the outer_method.
108 const OatFile::BssMappingInfo* mapping_info = outer_oat_file->FindBcpMappingInfo(dex_file);
109 if (mapping_info != nullptr) {
110 type_mapping = mapping_info->type_bss_mapping;
111 public_type_mapping = mapping_info->public_type_bss_mapping;
112 package_type_mapping = mapping_info->package_type_bss_mapping;
113 }
114 }
115
116 // Perform the update if we found a mapping.
117 auto store = [=](const IndexBssMapping* mapping) REQUIRES_SHARED(Locks::mutator_lock_) {
118 if (mapping != nullptr) {
119 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
120 mapping, type_idx.index_, dex_file->NumTypeIds(), sizeof(GcRoot<mirror::Class>));
121 if (bss_offset != IndexBssMappingLookup::npos) {
122 StoreObjectInBss(outer_method, outer_oat_file, bss_offset, resolved_type);
123 }
124 }
125 };
126 store(type_mapping);
127 if (resolved_type->IsPublic()) {
128 store(public_type_mapping);
129 }
130 if (resolved_type->IsPublic() || resolved_type->GetClassLoader() == caller->GetClassLoader()) {
131 store(package_type_mapping);
132 }
133 }
134
StoreStringInBss(ArtMethod * caller,dex::StringIndex string_idx,ObjPtr<mirror::String> resolved_string,ArtMethod * outer_method)135 static inline void StoreStringInBss(ArtMethod* caller,
136 dex::StringIndex string_idx,
137 ObjPtr<mirror::String> resolved_string,
138 ArtMethod* outer_method) REQUIRES_SHARED(Locks::mutator_lock_) {
139 const DexFile* dex_file = caller->GetDexFile();
140 DCHECK_NE(dex_file, nullptr);
141
142 if (outer_method->GetDexFile()->GetOatDexFile() == nullptr ||
143 outer_method->GetDexFile()->GetOatDexFile()->GetOatFile() == nullptr) {
144 // No OatFile to update.
145 return;
146 }
147 const OatFile* outer_oat_file = outer_method->GetDexFile()->GetOatDexFile()->GetOatFile();
148
149 const OatDexFile* oat_dex_file = dex_file->GetOatDexFile();
150 const IndexBssMapping* mapping = nullptr;
151 if (oat_dex_file != nullptr && oat_dex_file->GetOatFile() == outer_oat_file) {
152 // DexFiles compiled together to an oat file case.
153 mapping = oat_dex_file->GetStringBssMapping();
154 } else {
155 // Try to find the DexFile in the BCP of the outer_method.
156 const OatFile::BssMappingInfo* mapping_info = outer_oat_file->FindBcpMappingInfo(dex_file);
157 if (mapping_info != nullptr) {
158 mapping = mapping_info->string_bss_mapping;
159 }
160 }
161
162 // Perform the update if we found a mapping.
163 if (mapping != nullptr) {
164 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
165 mapping, string_idx.index_, dex_file->NumStringIds(), sizeof(GcRoot<mirror::Class>));
166 if (bss_offset != IndexBssMappingLookup::npos) {
167 StoreObjectInBss(outer_method, outer_oat_file, bss_offset, resolved_string);
168 }
169 }
170 }
171
artInitializeStaticStorageFromCode(mirror::Class * klass,Thread * self)172 extern "C" mirror::Class* artInitializeStaticStorageFromCode(mirror::Class* klass, Thread* self)
173 REQUIRES_SHARED(Locks::mutator_lock_) {
174 // Called to ensure static storage base is initialized for direct static field reads and writes.
175 // A class may be accessing another class' fields when it doesn't have access, as access has been
176 // given by inheritance.
177 ScopedQuickEntrypointChecks sqec(self);
178 DCHECK(klass != nullptr);
179 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
180 StackHandleScope<1> hs(self);
181 Handle<mirror::Class> h_klass = hs.NewHandle(klass);
182 bool success = class_linker->EnsureInitialized(
183 self, h_klass, /* can_init_fields= */ true, /* can_init_parents= */ true);
184 if (UNLIKELY(!success)) {
185 return nullptr;
186 }
187 return h_klass.Get();
188 }
189
artResolveTypeFromCode(uint32_t type_idx,Thread * self)190 extern "C" mirror::Class* artResolveTypeFromCode(uint32_t type_idx, Thread* self)
191 REQUIRES_SHARED(Locks::mutator_lock_) {
192 // Called when the .bss slot was empty or for main-path runtime call.
193 ScopedQuickEntrypointChecks sqec(self);
194 auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(
195 self, CalleeSaveType::kSaveEverythingForClinit);
196 ArtMethod* caller = caller_and_outer.caller;
197 ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
198 caller,
199 self,
200 /* can_run_clinit= */ false,
201 /* verify_access= */ false);
202 ArtMethod* outer_method = caller_and_outer.outer_method;
203 if (LIKELY(result != nullptr)) {
204 StoreTypeInBss(caller, dex::TypeIndex(type_idx), result, outer_method);
205 }
206 return result.Ptr();
207 }
208
artResolveTypeAndVerifyAccessFromCode(uint32_t type_idx,Thread * self)209 extern "C" mirror::Class* artResolveTypeAndVerifyAccessFromCode(uint32_t type_idx, Thread* self)
210 REQUIRES_SHARED(Locks::mutator_lock_) {
211 // Called when caller isn't guaranteed to have access to a type.
212 ScopedQuickEntrypointChecks sqec(self);
213 auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
214 CalleeSaveType::kSaveEverything);
215 ArtMethod* caller = caller_and_outer.caller;
216 ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
217 caller,
218 self,
219 /* can_run_clinit= */ false,
220 /* verify_access= */ true);
221 ArtMethod* outer_method = caller_and_outer.outer_method;
222 if (LIKELY(result != nullptr)) {
223 StoreTypeInBss(caller, dex::TypeIndex(type_idx), result, outer_method);
224 }
225 return result.Ptr();
226 }
227
artResolveMethodHandleFromCode(uint32_t method_handle_idx,Thread * self)228 extern "C" mirror::MethodHandle* artResolveMethodHandleFromCode(uint32_t method_handle_idx,
229 Thread* self)
230 REQUIRES_SHARED(Locks::mutator_lock_) {
231 ScopedQuickEntrypointChecks sqec(self);
232 auto caller_and_outer =
233 GetCalleeSaveMethodCallerAndOuterMethod(self, CalleeSaveType::kSaveEverything);
234 ArtMethod* caller = caller_and_outer.caller;
235 ObjPtr<mirror::MethodHandle> result = ResolveMethodHandleFromCode(caller, method_handle_idx);
236 return result.Ptr();
237 }
238
artResolveMethodTypeFromCode(uint32_t proto_idx,Thread * self)239 extern "C" mirror::MethodType* artResolveMethodTypeFromCode(uint32_t proto_idx, Thread* self)
240 REQUIRES_SHARED(Locks::mutator_lock_) {
241 ScopedQuickEntrypointChecks sqec(self);
242 auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
243 CalleeSaveType::kSaveEverything);
244 ArtMethod* caller = caller_and_outer.caller;
245 ObjPtr<mirror::MethodType> result = ResolveMethodTypeFromCode(caller, dex::ProtoIndex(proto_idx));
246 return result.Ptr();
247 }
248
artResolveStringFromCode(int32_t string_idx,Thread * self)249 extern "C" mirror::String* artResolveStringFromCode(int32_t string_idx, Thread* self)
250 REQUIRES_SHARED(Locks::mutator_lock_) {
251 ScopedQuickEntrypointChecks sqec(self);
252 auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
253 CalleeSaveType::kSaveEverything);
254 ArtMethod* caller = caller_and_outer.caller;
255 ObjPtr<mirror::String> result =
256 Runtime::Current()->GetClassLinker()->ResolveString(dex::StringIndex(string_idx), caller);
257 ArtMethod* outer_method = caller_and_outer.outer_method;
258 if (LIKELY(result != nullptr)) {
259 StoreStringInBss(caller, dex::StringIndex(string_idx), result, outer_method);
260 }
261 return result.Ptr();
262 }
263
264 } // namespace art
265