blob: c4d85a3ef8ec31fcd16c03a85230b97f30acbde3 [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
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 Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070018#include "base/callee_save_type.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070019#include "callee_save_frame.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "class_linker-inl.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000021#include "class_table-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/dex_file-inl.h"
23#include "dex/dex_file_types.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "entrypoints/entrypoint_utils-inl.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000025#include "gc/heap.h"
26#include "mirror/class-inl.h"
27#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "mirror/object_array-inl.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000030#include "oat_file.h"
31#include "runtime.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070032
33namespace art {
34
Vladimir Markof3c52b42017-11-17 17:32:12 +000035static 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>));
Vladimir Marko8bb72b62017-11-30 17:09:50 +000044 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 }
Vladimir Markof3c52b42017-11-17 17:32:12 +000050 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.
Vladimir Marko9b03cb42017-02-16 16:37:03 +000058 ObjPtr<mirror::ClassLoader> class_loader = outer_method->GetClassLoader();
Vladimir Markof3c52b42017-11-17 17:32:12 +000059 Runtime* runtime = Runtime::Current();
Vladimir Marko9b03cb42017-02-16 16:37:03 +000060 if (kIsDebugBuild) {
Vladimir Markof3c52b42017-11-17 17:32:12 +000061 ClassTable* class_table = runtime->GetClassLinker()->ClassTableForClassLoader(class_loader);
62 CHECK(class_table != nullptr && !class_table->InsertOatFile(oat_file))
Vladimir Marko1998cd02017-01-13 13:02:58 +000063 << "Oat file with .bss GC roots was not registered in class table: "
Vladimir Markof3c52b42017-11-17 17:32:12 +000064 << oat_file->GetLocation();
Vladimir Marko9b03cb42017-02-16 16:37:03 +000065 }
Mathieu Chartiera1467d02017-02-22 09:22:50 -080066 if (class_loader != nullptr) {
Mathieu Chartier88ea61e2018-06-20 17:45:41 -070067 WriteBarrier::ForEveryFieldWrite(class_loader);
Mathieu Chartiera1467d02017-02-22 09:22:50 -080068 } else {
Vladimir Markof3c52b42017-11-17 17:32:12 +000069 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
77static 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
95static inline void StoreStringInBss(ArtMethod* outer_method,
96 dex::StringIndex string_idx,
97 ObjPtr<mirror::String> resolved_string)
Vladimir Markoa9f303c2018-07-20 16:43:56 +010098 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markof3c52b42017-11-17 17:32:12 +000099 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);
Vladimir Marko1998cd02017-01-13 13:02:58 +0000109 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000110 }
111}
112
Vladimir Marko4e08fad2017-11-23 12:52:36 +0000113static 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
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100132extern "C" mirror::Class* artInitializeStaticStorageFromCode(mirror::Class* klass, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700133 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogerse2645d32012-04-11 14:42:42 -0700134 // 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.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700137 ScopedQuickEntrypointChecks sqec(self);
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100138 DCHECK(klass != nullptr);
139 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
140 StackHandleScope<1> hs(self);
141 Handle<mirror::Class> h_klass = hs.NewHandle(klass);
142 bool success = class_linker->EnsureInitialized(
143 self, h_klass, /* can_init_fields */ true, /* can_init_parents */ true);
144 if (UNLIKELY(!success)) {
145 return nullptr;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000146 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100147 return h_klass.Get();
Ian Rogers57b86d42012-03-27 16:05:41 -0700148}
149
Vladimir Marko9d479252018-07-24 11:35:20 +0100150extern "C" mirror::Class* artResolveTypeFromCode(uint32_t type_idx, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markof3c52b42017-11-17 17:32:12 +0000152 // Called when the .bss slot was empty or for main-path runtime call.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700153 ScopedQuickEntrypointChecks sqec(self);
Mingyao Yang0a87a652017-04-12 13:43:15 -0700154 auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(
155 self, CalleeSaveType::kSaveEverythingForClinit);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000156 ArtMethod* caller = caller_and_outer.caller;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000157 ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
158 caller,
159 self,
160 /* can_run_clinit */ false,
161 /* verify_access */ false);
Vladimir Marko4e08fad2017-11-23 12:52:36 +0000162 if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) {
Vladimir Markof3c52b42017-11-17 17:32:12 +0000163 StoreTypeInBss(caller_and_outer.outer_method, dex::TypeIndex(type_idx), result);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000164 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000165 return result.Ptr();
Ian Rogers57b86d42012-03-27 16:05:41 -0700166}
167
Vladimir Marko9d479252018-07-24 11:35:20 +0100168extern "C" mirror::Class* artResolveTypeAndVerifyAccessFromCode(uint32_t type_idx, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markof3c52b42017-11-17 17:32:12 +0000170 // Called when caller isn't guaranteed to have access to a type.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700171 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700172 auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
173 CalleeSaveType::kSaveEverything);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000174 ArtMethod* caller = caller_and_outer.caller;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000175 ObjPtr<mirror::Class> result = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
176 caller,
177 self,
178 /* can_run_clinit */ false,
179 /* verify_access */ true);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000180 // Do not StoreTypeInBss(); access check entrypoint is never used together with .bss.
Vladimir Marko28e012a2017-12-07 11:22:59 +0000181 return result.Ptr();
Ian Rogers57b86d42012-03-27 16:05:41 -0700182}
183
Orion Hodsondbaa5c72018-05-10 08:22:46 +0100184extern "C" mirror::MethodHandle* artResolveMethodHandleFromCode(uint32_t method_handle_idx,
185 Thread* self)
186 REQUIRES_SHARED(Locks::mutator_lock_) {
187 ScopedQuickEntrypointChecks sqec(self);
188 auto caller_and_outer =
189 GetCalleeSaveMethodCallerAndOuterMethod(self, CalleeSaveType::kSaveEverything);
190 ArtMethod* caller = caller_and_outer.caller;
191 ObjPtr<mirror::MethodHandle> result = ResolveMethodHandleFromCode(caller, method_handle_idx);
192 return result.Ptr();
193}
194
Orion Hodson18259d72018-04-12 11:18:23 +0100195extern "C" mirror::MethodType* artResolveMethodTypeFromCode(uint32_t proto_idx, Thread* self)
196 REQUIRES_SHARED(Locks::mutator_lock_) {
197 ScopedQuickEntrypointChecks sqec(self);
198 auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
199 CalleeSaveType::kSaveEverything);
200 ArtMethod* caller = caller_and_outer.caller;
Orion Hodson06d10a72018-05-14 08:53:38 +0100201 ObjPtr<mirror::MethodType> result = ResolveMethodTypeFromCode(caller, dex::ProtoIndex(proto_idx));
Orion Hodson18259d72018-04-12 11:18:23 +0100202 return result.Ptr();
203}
204
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100205extern "C" mirror::String* artResolveStringFromCode(int32_t string_idx, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700207 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700208 auto caller_and_outer = GetCalleeSaveMethodCallerAndOuterMethod(self,
209 CalleeSaveType::kSaveEverything);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000210 ArtMethod* caller = caller_and_outer.caller;
Vladimir Marko18090d12018-06-01 16:53:12 +0100211 ObjPtr<mirror::String> result =
212 Runtime::Current()->GetClassLinker()->ResolveString(dex::StringIndex(string_idx), caller);
Vladimir Marko4e08fad2017-11-23 12:52:36 +0000213 if (LIKELY(result != nullptr) && CanReferenceBss(caller_and_outer.outer_method, caller)) {
Vladimir Markof3c52b42017-11-17 17:32:12 +0000214 StoreStringInBss(caller_and_outer.outer_method, dex::StringIndex(string_idx), result);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000215 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000216 return result.Ptr();
Ian Rogers57b86d42012-03-27 16:05:41 -0700217}
218
219} // namespace art