• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     *slot = GcRoot<mirror::Object>(object);
57     // We need a write barrier for the class loader that holds the GC roots in the .bss.
58     ObjPtr<mirror::ClassLoader> class_loader = outer_method->GetClassLoader();
59     Runtime* runtime = Runtime::Current();
60     if (kIsDebugBuild) {
61       ClassTable* class_table = runtime->GetClassLinker()->ClassTableForClassLoader(class_loader);
62       CHECK(class_table != nullptr && !class_table->InsertOatFile(oat_file))
63           << "Oat file with .bss GC roots was not registered in class table: "
64           << oat_file->GetLocation();
65     }
66     if (class_loader != nullptr) {
67       runtime->GetHeap()->WriteBarrierEveryFieldOf(class_loader);
68     } else {
69       runtime->GetClassLinker()->WriteBarrierForBootOatFileBssRoots(oat_file);
70     }
71   } else {
72     // Each slot serves to store exactly one Class or String.
73     DCHECK_EQ(object, slot->Read());
74   }
75 }
76 
StoreTypeInBss(ArtMethod * outer_method,dex::TypeIndex type_idx,ObjPtr<mirror::Class> resolved_type)77 static inline void StoreTypeInBss(ArtMethod* outer_method,
78                                   dex::TypeIndex type_idx,
79                                   ObjPtr<mirror::Class> resolved_type)
80     REQUIRES_SHARED(Locks::mutator_lock_) {
81   const DexFile* dex_file = outer_method->GetDexFile();
82   DCHECK(dex_file != nullptr);
83   const OatDexFile* oat_dex_file = dex_file->GetOatDexFile();
84   if (oat_dex_file != nullptr) {
85     size_t bss_offset = IndexBssMappingLookup::GetBssOffset(oat_dex_file->GetTypeBssMapping(),
86                                                             type_idx.index_,
87                                                             dex_file->NumTypeIds(),
88                                                             sizeof(GcRoot<mirror::Class>));
89     if (bss_offset != IndexBssMappingLookup::npos) {
90       StoreObjectInBss(outer_method, oat_dex_file->GetOatFile(), bss_offset, resolved_type);
91     }
92   }
93 }
94 
StoreStringInBss(ArtMethod * outer_method,dex::StringIndex string_idx,ObjPtr<mirror::String> resolved_string)95 static inline void StoreStringInBss(ArtMethod* outer_method,
96                                     dex::StringIndex string_idx,
97                                     ObjPtr<mirror::String> resolved_string)
98     REQUIRES_SHARED(Locks::mutator_lock_) __attribute__((optnone)) {
99   const DexFile* dex_file = outer_method->GetDexFile();
100   DCHECK(dex_file != nullptr);
101   const OatDexFile* oat_dex_file = dex_file->GetOatDexFile();
102   if (oat_dex_file != nullptr) {
103     size_t bss_offset = IndexBssMappingLookup::GetBssOffset(oat_dex_file->GetStringBssMapping(),
104                                                             string_idx.index_,
105                                                             dex_file->NumStringIds(),
106                                                             sizeof(GcRoot<mirror::Class>));
107     if (bss_offset != IndexBssMappingLookup::npos) {
108       StoreObjectInBss(outer_method, oat_dex_file->GetOatFile(), bss_offset, resolved_string);
109     }
110   }
111 }
112 
CanReferenceBss(ArtMethod * outer_method,ArtMethod * caller)113 static ALWAYS_INLINE bool CanReferenceBss(ArtMethod* outer_method, ArtMethod* caller)
114     REQUIRES_SHARED(Locks::mutator_lock_) {
115   // .bss references are used only for AOT-compiled code and only when the instruction
116   // originates from the outer method's dex file and the type or string index is tied to
117   // that dex file. As we do not want to check if the call is coming from AOT-compiled
118   // code (that could be expensive), simply check if the caller has the same dex file.
119   //
120   // If we've accepted running AOT-compiled code despite the runtime class loader
121   // resolving the caller to a different dex file, this check shall prevent us from
122   // filling the .bss slot and we shall keep going through the slow path. This is slow
123   // but correct; we do not really care that much about performance in this odd case.
124   //
125   // JIT can inline throwing instructions across dex files and this check prevents
126   // looking up the index in the wrong dex file in that case. If the caller and outer
127   // method have the same dex file, we may or may not find a .bss slot to update;
128   // if we do, this can still benefit AOT-compiled code executed later.
129   return outer_method->GetDexFile() == caller->GetDexFile();
130 }
131 
artInitializeStaticStorageFromCode(uint32_t type_idx,Thread * self)132 extern "C" mirror::Class* artInitializeStaticStorageFromCode(uint32_t type_idx, Thread* self)
133     REQUIRES_SHARED(Locks::mutator_lock_) {
134   // Called to ensure static storage base is initialized for direct static field reads and writes.
135   // A class may be accessing another class' fields when it doesn't have access, as access has been
136   // given by inheritance.
137   ScopedQuickEntrypointChecks sqec(self);
138   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(
139       self, CalleeSaveType::kSaveEverythingForClinit);
140   ArtMethod* caller = caller_and_outer.caller;
141   ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
142                                                         caller,
143                                                         self,
144                                                         /* can_run_clinit */ true,
145                                                         /* verify_access */ false);
146   if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) {
147     StoreTypeInBss(caller_and_outer.outer_method, dex::TypeIndex(type_idx), result);
148   }
149   return result.Ptr();
150 }
151 
artInitializeTypeFromCode(uint32_t type_idx,Thread * self)152 extern "C" mirror::Class* artInitializeTypeFromCode(uint32_t type_idx, Thread* self)
153     REQUIRES_SHARED(Locks::mutator_lock_) {
154   // Called when the .bss slot was empty or for main-path runtime call.
155   ScopedQuickEntrypointChecks sqec(self);
156   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(
157       self, CalleeSaveType::kSaveEverythingForClinit);
158   ArtMethod* caller = caller_and_outer.caller;
159   ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
160                                                         caller,
161                                                         self,
162                                                         /* can_run_clinit */ false,
163                                                         /* verify_access */ false);
164   if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) {
165     StoreTypeInBss(caller_and_outer.outer_method, dex::TypeIndex(type_idx), result);
166   }
167   return result.Ptr();
168 }
169 
artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,Thread * self)170 extern "C" mirror::Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx, Thread* self)
171     REQUIRES_SHARED(Locks::mutator_lock_) {
172   // Called when caller isn't guaranteed to have access to a type.
173   ScopedQuickEntrypointChecks sqec(self);
174   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
175                                                                   CalleeSaveType::kSaveEverything);
176   ArtMethod* caller = caller_and_outer.caller;
177   ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
178                                                         caller,
179                                                         self,
180                                                         /* can_run_clinit */ false,
181                                                         /* verify_access */ true);
182   // Do not StoreTypeInBss(); access check entrypoint is never used together with .bss.
183   return result.Ptr();
184 }
185 
artResolveStringFromCode(int32_t string_idx,Thread * self)186 extern "C" mirror::String* artResolveStringFromCode(int32_t string_idx, Thread* self)
187     REQUIRES_SHARED(Locks::mutator_lock_) {
188   ScopedQuickEntrypointChecks sqec(self);
189   auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
190                                                                   CalleeSaveType::kSaveEverything);
191   ArtMethod* caller = caller_and_outer.caller;
192   ObjPtr<mirror::String> result = ResolveStringFromCode(caller, dex::StringIndex(string_idx));
193   if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) {
194     StoreStringInBss(caller_and_outer.outer_method, dex::StringIndex(string_idx), result);
195   }
196   return result.Ptr();
197 }
198 
199 }  // namespace art
200