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/oat_file.h"
32 #include "oat/oat_file-inl.h"
33 #include "runtime.h"
34 
35 namespace art HIDDEN {
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::String>));
166     if (bss_offset != IndexBssMappingLookup::npos) {
167       StoreObjectInBss(outer_method, outer_oat_file, bss_offset, resolved_string);
168     }
169   }
170 }
171 
StoreMethodTypeInBss(ArtMethod * caller,dex::ProtoIndex proto_idx,ObjPtr<mirror::MethodType> resolved_method_type,ArtMethod * outer_method)172 static inline void StoreMethodTypeInBss(ArtMethod* caller,
173                                         dex::ProtoIndex proto_idx,
174                                         ObjPtr<mirror::MethodType> resolved_method_type,
175                                         ArtMethod* outer_method)
176     REQUIRES_SHARED(Locks::mutator_lock_) {
177   const DexFile* dex_file = caller->GetDexFile();
178   DCHECK_NE(dex_file, nullptr);
179 
180   if (outer_method->GetDexFile()->GetOatDexFile() == nullptr ||
181       outer_method->GetDexFile()->GetOatDexFile()->GetOatFile() == nullptr) {
182     // No OatFile to update.
183     return;
184   }
185   const OatFile* outer_oat_file = outer_method->GetDexFile()->GetOatDexFile()->GetOatFile();
186 
187   const OatDexFile* oat_dex_file = dex_file->GetOatDexFile();
188   const IndexBssMapping* mapping = nullptr;
189   if (oat_dex_file != nullptr && oat_dex_file->GetOatFile() == outer_oat_file) {
190     // DexFiles compiled together to an oat file case.
191     mapping = oat_dex_file->GetMethodTypeBssMapping();
192   } else {
193     // Try to find the DexFile in the BCP of the outer_method.
194     const OatFile::BssMappingInfo* mapping_info = outer_oat_file->FindBcpMappingInfo(dex_file);
195     if (mapping_info != nullptr) {
196       mapping = mapping_info->method_type_bss_mapping;
197     }
198   }
199 
200   // Perform the update if we found a mapping.
201   if (mapping != nullptr) {
202     size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
203         mapping, proto_idx.index_, dex_file->NumProtoIds(), sizeof(GcRoot<mirror::MethodType>));
204     if (bss_offset != IndexBssMappingLookup::npos) {
205       StoreObjectInBss(outer_method, outer_oat_file, bss_offset, resolved_method_type);
206     }
207   }
208 }
209 
artInitializeStaticStorageFromCode(mirror::Class * klass,Thread * self)210 extern "C" mirror::Class* artInitializeStaticStorageFromCode(mirror::Class* klass, Thread* self)
211     REQUIRES_SHARED(Locks::mutator_lock_) {
212   // Called to ensure static storage base is initialized for direct static field reads and writes.
213   // A class may be accessing another class' fields when it doesn't have access, as access has been
214   // given by inheritance.
215   ScopedQuickEntrypointChecks sqec(self);
216   DCHECK(klass != nullptr);
217   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
218   StackHandleScope<1> hs(self);
219   Handle<mirror::Class> h_klass = hs.NewHandle(klass);
220   bool success = class_linker->EnsureInitialized(
221       self, h_klass, /* can_init_fields= */ true, /* can_init_parents= */ true);
222   if (UNLIKELY(!success)) {
223     return nullptr;
224   }
225   return h_klass.Get();
226 }
227 
artResolveTypeFromCode(uint32_t type_idx,Thread * self)228 extern "C" mirror::Class* artResolveTypeFromCode(uint32_t type_idx, Thread* self)
229     REQUIRES_SHARED(Locks::mutator_lock_) {
230   // Called when the .bss slot was empty or for main-path runtime call.
231   ScopedQuickEntrypointChecks sqec(self);
232   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(
233       self, CalleeSaveType::kSaveEverythingForClinit);
234   ArtMethod* caller = caller_and_outer.caller;
235   ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
236                                                         caller,
237                                                         self,
238                                                         /* can_run_clinit= */ false,
239                                                         /* verify_access= */ false);
240   ArtMethod* outer_method = caller_and_outer.outer_method;
241   if (LIKELY(result != nullptr)) {
242     StoreTypeInBss(caller, dex::TypeIndex(type_idx), result, outer_method);
243   }
244   return result.Ptr();
245 }
246 
artResolveTypeAndVerifyAccessFromCode(uint32_t type_idx,Thread * self)247 extern "C" mirror::Class* artResolveTypeAndVerifyAccessFromCode(uint32_t type_idx, Thread* self)
248     REQUIRES_SHARED(Locks::mutator_lock_) {
249   // Called when caller isn't guaranteed to have access to a type.
250   ScopedQuickEntrypointChecks sqec(self);
251   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
252                                                                   CalleeSaveType::kSaveEverything);
253   ArtMethod* caller = caller_and_outer.caller;
254   ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
255                                                         caller,
256                                                         self,
257                                                         /* can_run_clinit= */ false,
258                                                         /* verify_access= */ true);
259   ArtMethod* outer_method = caller_and_outer.outer_method;
260   if (LIKELY(result != nullptr)) {
261     StoreTypeInBss(caller, dex::TypeIndex(type_idx), result, outer_method);
262   }
263   return result.Ptr();
264 }
265 
artResolveMethodHandleFromCode(uint32_t method_handle_idx,Thread * self)266 extern "C" mirror::MethodHandle* artResolveMethodHandleFromCode(uint32_t method_handle_idx,
267                                                                 Thread* self)
268     REQUIRES_SHARED(Locks::mutator_lock_) {
269   ScopedQuickEntrypointChecks sqec(self);
270   auto caller_and_outer =
271       GetCalleeSaveMethodCallerAndOuterMethod(self, CalleeSaveType::kSaveEverything);
272   ArtMethod* caller = caller_and_outer.caller;
273   ObjPtr<mirror::MethodHandle> result = ResolveMethodHandleFromCode(caller, method_handle_idx);
274   return result.Ptr();
275 }
276 
artResolveMethodTypeFromCode(uint32_t proto_idx,Thread * self)277 extern "C" mirror::MethodType* artResolveMethodTypeFromCode(uint32_t proto_idx, Thread* self)
278     REQUIRES_SHARED(Locks::mutator_lock_) {
279   ScopedQuickEntrypointChecks sqec(self);
280   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
281                                                                   CalleeSaveType::kSaveEverything);
282   ArtMethod* caller = caller_and_outer.caller;
283   ObjPtr<mirror::MethodType> result = ResolveMethodTypeFromCode(caller, dex::ProtoIndex(proto_idx));
284   ArtMethod* outer_method = caller_and_outer.outer_method;
285   if (LIKELY(result != nullptr)) {
286     StoreMethodTypeInBss(caller, dex::ProtoIndex(proto_idx), result, outer_method);
287   }
288   return result.Ptr();
289 }
290 
artResolveStringFromCode(int32_t string_idx,Thread * self)291 extern "C" mirror::String* artResolveStringFromCode(int32_t string_idx, Thread* self)
292     REQUIRES_SHARED(Locks::mutator_lock_) {
293   ScopedQuickEntrypointChecks sqec(self);
294   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
295                                                                   CalleeSaveType::kSaveEverything);
296   ArtMethod* caller = caller_and_outer.caller;
297   ObjPtr<mirror::String> result =
298       Runtime::Current()->GetClassLinker()->ResolveString(dex::StringIndex(string_idx), caller);
299   ArtMethod* outer_method = caller_and_outer.outer_method;
300   if (LIKELY(result != nullptr)) {
301     StoreStringInBss(caller, dex::StringIndex(string_idx), result, outer_method);
302   }
303   return result.Ptr();
304 }
305 
306 }  // namespace art
307