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 "mirror/class-inl.h"
27 #include "mirror/class_loader.h"
28 #include "mirror/object-inl.h"
29 #include "mirror/object_array-inl.h"
30 #include "oat_file.h"
31 #include "runtime.h"
32 
33 namespace art {
34 
StoreObjectInBss(ArtMethod * outer_method,const OatFile * oat_file,size_t bss_offset,ObjPtr<mirror::Object> object)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   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   }
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.
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);
60     // We need a write barrier for the class loader that holds the GC roots in the .bss.
61     ObjPtr<mirror::ClassLoader> class_loader = outer_method->GetClassLoader();
62     Runtime* runtime = Runtime::Current();
63     if (kIsDebugBuild) {
64       ClassTable* class_table = runtime->GetClassLinker()->ClassTableForClassLoader(class_loader);
65       CHECK(class_table != nullptr && !class_table->InsertOatFile(oat_file))
66           << "Oat file with .bss GC roots was not registered in class table: "
67           << oat_file->GetLocation();
68     }
69     if (class_loader != nullptr) {
70       WriteBarrier::ForEveryFieldWrite(class_loader);
71     } else {
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 
StoreTypeInBss(ArtMethod * outer_method,dex::TypeIndex type_idx,ObjPtr<mirror::Class> resolved_type)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) {
88     size_t bss_offset = IndexBssMappingLookup::GetBssOffset(oat_dex_file->GetTypeBssMapping(),
89                                                             type_idx.index_,
90                                                             dex_file->NumTypeIds(),
91                                                             sizeof(GcRoot<mirror::Class>));
92     if (bss_offset != IndexBssMappingLookup::npos) {
93       StoreObjectInBss(outer_method, oat_dex_file->GetOatFile(), bss_offset, resolved_type);
94     }
95   }
96 }
97 
StoreStringInBss(ArtMethod * outer_method,dex::StringIndex string_idx,ObjPtr<mirror::String> resolved_string)98 static inline void StoreStringInBss(ArtMethod* outer_method,
99                                     dex::StringIndex string_idx,
100                                     ObjPtr<mirror::String> resolved_string)
101     REQUIRES_SHARED(Locks::mutator_lock_) {
102   const DexFile* dex_file = outer_method->GetDexFile();
103   DCHECK(dex_file != nullptr);
104   const OatDexFile* oat_dex_file = dex_file->GetOatDexFile();
105   if (oat_dex_file != nullptr) {
106     size_t bss_offset = IndexBssMappingLookup::GetBssOffset(oat_dex_file->GetStringBssMapping(),
107                                                             string_idx.index_,
108                                                             dex_file->NumStringIds(),
109                                                             sizeof(GcRoot<mirror::Class>));
110     if (bss_offset != IndexBssMappingLookup::npos) {
111       StoreObjectInBss(outer_method, oat_dex_file->GetOatFile(), bss_offset, resolved_string);
112     }
113   }
114 }
115 
CanReferenceBss(ArtMethod * outer_method,ArtMethod * caller)116 static ALWAYS_INLINE bool CanReferenceBss(ArtMethod* outer_method, ArtMethod* caller)
117     REQUIRES_SHARED(Locks::mutator_lock_) {
118   // .bss references are used only for AOT-compiled code and only when the instruction
119   // originates from the outer method's dex file and the type or string index is tied to
120   // that dex file. As we do not want to check if the call is coming from AOT-compiled
121   // code (that could be expensive), simply check if the caller has the same dex file.
122   //
123   // If we've accepted running AOT-compiled code despite the runtime class loader
124   // resolving the caller to a different dex file, this check shall prevent us from
125   // filling the .bss slot and we shall keep going through the slow path. This is slow
126   // but correct; we do not really care that much about performance in this odd case.
127   //
128   // JIT can inline throwing instructions across dex files and this check prevents
129   // looking up the index in the wrong dex file in that case. If the caller and outer
130   // method have the same dex file, we may or may not find a .bss slot to update;
131   // if we do, this can still benefit AOT-compiled code executed later.
132   return outer_method->GetDexFile() == caller->GetDexFile();
133 }
134 
artInitializeStaticStorageFromCode(mirror::Class * klass,Thread * self)135 extern "C" mirror::Class* artInitializeStaticStorageFromCode(mirror::Class* klass, Thread* self)
136     REQUIRES_SHARED(Locks::mutator_lock_) {
137   // Called to ensure static storage base is initialized for direct static field reads and writes.
138   // A class may be accessing another class' fields when it doesn't have access, as access has been
139   // given by inheritance.
140   ScopedQuickEntrypointChecks sqec(self);
141   DCHECK(klass != nullptr);
142   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
143   StackHandleScope<1> hs(self);
144   Handle<mirror::Class> h_klass = hs.NewHandle(klass);
145   bool success = class_linker->EnsureInitialized(
146       self, h_klass, /* can_init_fields= */ true, /* can_init_parents= */ true);
147   if (UNLIKELY(!success)) {
148     return nullptr;
149   }
150   return h_klass.Get();
151 }
152 
artResolveTypeFromCode(uint32_t type_idx,Thread * self)153 extern "C" mirror::Class* artResolveTypeFromCode(uint32_t type_idx, Thread* self)
154     REQUIRES_SHARED(Locks::mutator_lock_) {
155   // Called when the .bss slot was empty or for main-path runtime call.
156   ScopedQuickEntrypointChecks sqec(self);
157   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(
158       self, CalleeSaveType::kSaveEverythingForClinit);
159   ArtMethod* caller = caller_and_outer.caller;
160   ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
161                                                         caller,
162                                                         self,
163                                                         /* can_run_clinit= */ false,
164                                                         /* verify_access= */ false);
165   if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) {
166     StoreTypeInBss(caller_and_outer.outer_method, dex::TypeIndex(type_idx), result);
167   }
168   return result.Ptr();
169 }
170 
artResolveTypeAndVerifyAccessFromCode(uint32_t type_idx,Thread * self)171 extern "C" mirror::Class* artResolveTypeAndVerifyAccessFromCode(uint32_t type_idx, Thread* self)
172     REQUIRES_SHARED(Locks::mutator_lock_) {
173   // Called when caller isn't guaranteed to have access to a type.
174   ScopedQuickEntrypointChecks sqec(self);
175   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
176                                                                   CalleeSaveType::kSaveEverything);
177   ArtMethod* caller = caller_and_outer.caller;
178   ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
179                                                         caller,
180                                                         self,
181                                                         /* can_run_clinit= */ false,
182                                                         /* verify_access= */ true);
183   // Do not StoreTypeInBss(); access check entrypoint is never used together with .bss.
184   return result.Ptr();
185 }
186 
artResolveMethodHandleFromCode(uint32_t method_handle_idx,Thread * self)187 extern "C" mirror::MethodHandle* artResolveMethodHandleFromCode(uint32_t method_handle_idx,
188                                                                 Thread* self)
189     REQUIRES_SHARED(Locks::mutator_lock_) {
190   ScopedQuickEntrypointChecks sqec(self);
191   auto caller_and_outer =
192       GetCalleeSaveMethodCallerAndOuterMethod(self, CalleeSaveType::kSaveEverything);
193   ArtMethod* caller = caller_and_outer.caller;
194   ObjPtr<mirror::MethodHandle> result = ResolveMethodHandleFromCode(caller, method_handle_idx);
195   return result.Ptr();
196 }
197 
artResolveMethodTypeFromCode(uint32_t proto_idx,Thread * self)198 extern "C" mirror::MethodType* artResolveMethodTypeFromCode(uint32_t proto_idx, Thread* self)
199     REQUIRES_SHARED(Locks::mutator_lock_) {
200   ScopedQuickEntrypointChecks sqec(self);
201   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
202                                                                   CalleeSaveType::kSaveEverything);
203   ArtMethod* caller = caller_and_outer.caller;
204   ObjPtr<mirror::MethodType> result = ResolveMethodTypeFromCode(caller, dex::ProtoIndex(proto_idx));
205   return result.Ptr();
206 }
207 
artResolveStringFromCode(int32_t string_idx,Thread * self)208 extern "C" mirror::String* artResolveStringFromCode(int32_t string_idx, Thread* self)
209     REQUIRES_SHARED(Locks::mutator_lock_) {
210   ScopedQuickEntrypointChecks sqec(self);
211   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
212                                                                   CalleeSaveType::kSaveEverything);
213   ArtMethod* caller = caller_and_outer.caller;
214   ObjPtr<mirror::String> result =
215       Runtime::Current()->GetClassLinker()->ResolveString(dex::StringIndex(string_idx), caller);
216   if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) {
217     StoreStringInBss(caller_and_outer.outer_method, dex::StringIndex(string_idx), result);
218   }
219   return result.Ptr();
220 }
221 
222 }  // namespace art
223