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 #ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
18 #define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
19
20 #include "entrypoint_utils.h"
21
22 #include <sstream>
23
24 #include "art_field-inl.h"
25 #include "art_method-inl.h"
26 #include "base/enums.h"
27 #include "base/sdk_version.h"
28 #include "class_linker-inl.h"
29 #include "common_throws.h"
30 #include "dex/dex_file.h"
31 #include "dex/invoke_type.h"
32 #include "entrypoints/quick/callee_save_frame.h"
33 #include "handle_scope-inl.h"
34 #include "imt_conflict_table.h"
35 #include "imtable-inl.h"
36 #include "indirect_reference_table.h"
37 #include "mirror/array-alloc-inl.h"
38 #include "mirror/class-alloc-inl.h"
39 #include "mirror/class-inl.h"
40 #include "mirror/object-inl.h"
41 #include "mirror/throwable.h"
42 #include "nth_caller_visitor.h"
43 #include "oat_file.h"
44 #include "reflective_handle_scope-inl.h"
45 #include "runtime.h"
46 #include "stack_map.h"
47 #include "thread.h"
48 #include "well_known_classes.h"
49
50 namespace art {
51
GetResolvedMethodErrorString(ClassLinker * class_linker,ArtMethod * inlined_method,ArtMethod * parent_method,ArtMethod * outer_method,ObjPtr<mirror::DexCache> dex_cache,MethodInfo method_info)52 inline std::string GetResolvedMethodErrorString(ClassLinker* class_linker,
53 ArtMethod* inlined_method,
54 ArtMethod* parent_method,
55 ArtMethod* outer_method,
56 ObjPtr<mirror::DexCache> dex_cache,
57 MethodInfo method_info)
58 REQUIRES_SHARED(Locks::mutator_lock_) {
59 const uint32_t method_index = method_info.GetMethodIndex();
60
61 std::stringstream error_ss;
62 std::string separator = "";
63 error_ss << "BCP vector {";
64 for (const DexFile* df : class_linker->GetBootClassPath()) {
65 error_ss << separator << df << "(" << df->GetLocation() << ")";
66 separator = ", ";
67 }
68 error_ss << "}. oat_dex_files vector: {";
69 separator = "";
70 for (const OatDexFile* odf_value :
71 parent_method->GetDexFile()->GetOatDexFile()->GetOatFile()->GetOatDexFiles()) {
72 error_ss << separator << odf_value << "(" << odf_value->GetDexFileLocation() << ")";
73 separator = ", ";
74 }
75 error_ss << "}. ";
76 if (inlined_method != nullptr) {
77 error_ss << "Inlined method: " << inlined_method->PrettyMethod() << " ("
78 << inlined_method->GetDexFile()->GetLocation() << "/"
79 << static_cast<const void*>(inlined_method->GetDexFile()) << "). ";
80 } else if (dex_cache != nullptr) {
81 error_ss << "Could not find an inlined method from an .oat file, using dex_cache to print the "
82 "inlined method: "
83 << dex_cache->GetDexFile()->PrettyMethod(method_index) << " ("
84 << dex_cache->GetDexFile()->GetLocation() << "/"
85 << static_cast<const void*>(dex_cache->GetDexFile()) << "). ";
86 } else {
87 error_ss << "Both inlined_method and dex_cache are null. This means that we had an OOB access "
88 << "to either bcp_dex_files or oat_dex_files. ";
89 }
90 error_ss << "The outer method is: " << parent_method->PrettyMethod() << " ("
91 << parent_method->GetDexFile()->GetLocation() << "/"
92 << static_cast<const void*>(parent_method->GetDexFile())
93 << "). The outermost method in the chain is: " << outer_method->PrettyMethod() << " ("
94 << outer_method->GetDexFile()->GetLocation() << "/"
95 << static_cast<const void*>(outer_method->GetDexFile())
96 << "). MethodInfo: method_index=" << std::dec << method_index
97 << ", is_in_bootclasspath=" << std::boolalpha
98 << (method_info.GetDexFileIndexKind() == MethodInfo::kKindBCP) << std::noboolalpha
99 << ", dex_file_index=" << std::dec << method_info.GetDexFileIndex() << ".";
100 return error_ss.str();
101 }
102
GetResolvedMethod(ArtMethod * outer_method,const CodeInfo & code_info,const BitTableRange<InlineInfo> & inline_infos)103 inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
104 const CodeInfo& code_info,
105 const BitTableRange<InlineInfo>& inline_infos)
106 REQUIRES_SHARED(Locks::mutator_lock_) {
107 DCHECK(!outer_method->IsObsolete());
108
109 // This method is being used by artQuickResolutionTrampoline, before it sets up
110 // the passed parameters in a GC friendly way. Therefore we must never be
111 // suspended while executing it.
112 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
113
114 {
115 InlineInfo inline_info = inline_infos.back();
116
117 if (inline_info.EncodesArtMethod()) {
118 return inline_info.GetArtMethod();
119 }
120
121 uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
122 if (inline_info.GetDexPc() == static_cast<uint32_t>(-1)) {
123 // "charAt" special case. It is the only non-leaf method we inline across dex files.
124 ArtMethod* inlined_method = WellKnownClasses::java_lang_String_charAt;
125 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
126 return inlined_method;
127 }
128 }
129
130 // Find which method did the call in the inlining hierarchy.
131 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
132 ArtMethod* method = outer_method;
133 for (InlineInfo inline_info : inline_infos) {
134 DCHECK(!inline_info.EncodesArtMethod());
135 DCHECK_NE(inline_info.GetDexPc(), static_cast<uint32_t>(-1));
136 MethodInfo method_info = code_info.GetMethodInfoOf(inline_info);
137 uint32_t method_index = method_info.GetMethodIndex();
138 const uint32_t dex_file_index = method_info.GetDexFileIndex();
139 ArtMethod* inlined_method = nullptr;
140 ObjPtr<mirror::DexCache> dex_cache = nullptr;
141 if (method_info.HasDexFileIndex()) {
142 if (method_info.GetDexFileIndexKind() == MethodInfo::kKindBCP) {
143 ArrayRef<const DexFile* const> bcp_dex_files(class_linker->GetBootClassPath());
144 DCHECK_LT(dex_file_index, bcp_dex_files.size())
145 << "OOB access to bcp_dex_files. Dumping info: "
146 << GetResolvedMethodErrorString(
147 class_linker, inlined_method, method, outer_method, dex_cache, method_info);
148 const DexFile* dex_file = bcp_dex_files[dex_file_index];
149 DCHECK_NE(dex_file, nullptr);
150 dex_cache = class_linker->FindDexCache(Thread::Current(), *dex_file);
151 } else {
152 ArrayRef<const OatDexFile* const> oat_dex_files(
153 outer_method->GetDexFile()->GetOatDexFile()->GetOatFile()->GetOatDexFiles());
154 DCHECK_LT(dex_file_index, oat_dex_files.size())
155 << "OOB access to oat_dex_files. Dumping info: "
156 << GetResolvedMethodErrorString(
157 class_linker, inlined_method, method, outer_method, dex_cache, method_info);
158 const OatDexFile* odf = oat_dex_files[dex_file_index];
159 DCHECK_NE(odf, nullptr);
160 dex_cache = class_linker->FindDexCache(Thread::Current(), *odf);
161 }
162 } else {
163 dex_cache = outer_method->GetDexCache();
164 }
165 inlined_method =
166 class_linker->LookupResolvedMethod(method_index, dex_cache, dex_cache->GetClassLoader());
167
168 if (UNLIKELY(inlined_method == nullptr)) {
169 LOG(FATAL) << GetResolvedMethodErrorString(
170 class_linker, inlined_method, method, outer_method, dex_cache, method_info);
171 UNREACHABLE();
172 }
173 DCHECK(!inlined_method->IsRuntimeMethod());
174 DCHECK_EQ(inlined_method->GetDexFile() == outer_method->GetDexFile(),
175 dex_file_index == MethodInfo::kSameDexFile)
176 << GetResolvedMethodErrorString(
177 class_linker, inlined_method, method, outer_method, dex_cache, method_info);
178 method = inlined_method;
179 }
180
181 return method;
182 }
183
184 ALWAYS_INLINE
CheckClassInitializedForObjectAlloc(ObjPtr<mirror::Class> klass,Thread * self,bool * slow_path)185 inline ObjPtr<mirror::Class> CheckClassInitializedForObjectAlloc(ObjPtr<mirror::Class> klass,
186 Thread* self,
187 bool* slow_path)
188 REQUIRES_SHARED(Locks::mutator_lock_)
189 REQUIRES(!Roles::uninterruptible_) {
190 if (UNLIKELY(!klass->IsVisiblyInitialized())) {
191 StackHandleScope<1> hs(self);
192 Handle<mirror::Class> h_class(hs.NewHandle(klass));
193 // EnsureInitialized (the class initializer) might cause a GC.
194 // may cause us to suspend meaning that another thread may try to
195 // change the allocator while we are stuck in the entrypoints of
196 // an old allocator. Also, the class initialization may fail. To
197 // handle these cases we mark the slow path boolean as true so
198 // that the caller knows to check the allocator type to see if it
199 // has changed and to null-check the return value in case the
200 // initialization fails.
201 *slow_path = true;
202 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
203 DCHECK(self->IsExceptionPending());
204 return nullptr; // Failure
205 } else {
206 DCHECK(!self->IsExceptionPending());
207 }
208 return h_class.Get();
209 }
210 return klass;
211 }
212
CheckObjectAlloc(ObjPtr<mirror::Class> klass,Thread * self,bool * slow_path)213 ALWAYS_INLINE inline ObjPtr<mirror::Class> CheckObjectAlloc(ObjPtr<mirror::Class> klass,
214 Thread* self,
215 bool* slow_path)
216 REQUIRES_SHARED(Locks::mutator_lock_)
217 REQUIRES(!Roles::uninterruptible_) {
218 if (UNLIKELY(!klass->IsInstantiable())) {
219 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
220 *slow_path = true;
221 return nullptr; // Failure
222 }
223 if (UNLIKELY(klass->IsClassClass())) {
224 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
225 klass->PrettyDescriptor().c_str());
226 *slow_path = true;
227 return nullptr; // Failure
228 }
229 return CheckClassInitializedForObjectAlloc(klass, self, slow_path);
230 }
231
232 // Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
233 // or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
234 template <bool kInstrumented>
235 ALWAYS_INLINE
AllocObjectFromCode(ObjPtr<mirror::Class> klass,Thread * self,gc::AllocatorType allocator_type)236 inline ObjPtr<mirror::Object> AllocObjectFromCode(ObjPtr<mirror::Class> klass,
237 Thread* self,
238 gc::AllocatorType allocator_type) {
239 bool slow_path = false;
240 klass = CheckObjectAlloc(klass, self, &slow_path);
241 if (UNLIKELY(slow_path)) {
242 if (klass == nullptr) {
243 return nullptr;
244 }
245 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
246 return klass->Alloc</*kInstrumented=*/true>(
247 self,
248 Runtime::Current()->GetHeap()->GetCurrentAllocator());
249 }
250 DCHECK(klass != nullptr);
251 return klass->Alloc<kInstrumented>(self, allocator_type);
252 }
253
254 // Given the context of a calling Method and a resolved class, create an instance.
255 template <bool kInstrumented>
256 ALWAYS_INLINE
AllocObjectFromCodeResolved(ObjPtr<mirror::Class> klass,Thread * self,gc::AllocatorType allocator_type)257 inline ObjPtr<mirror::Object> AllocObjectFromCodeResolved(ObjPtr<mirror::Class> klass,
258 Thread* self,
259 gc::AllocatorType allocator_type) {
260 DCHECK(klass != nullptr);
261 bool slow_path = false;
262 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
263 if (UNLIKELY(slow_path)) {
264 if (klass == nullptr) {
265 return nullptr;
266 }
267 gc::Heap* heap = Runtime::Current()->GetHeap();
268 // Pass in kNoAddFinalizer since the object cannot be finalizable.
269 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
270 // instrumented.
271 return klass->Alloc</*kInstrumented=*/true, mirror::Class::AddFinalizer::kNoAddFinalizer>(
272 self, heap->GetCurrentAllocator());
273 }
274 // Pass in kNoAddFinalizer since the object cannot be finalizable.
275 return klass->Alloc<kInstrumented,
276 mirror::Class::AddFinalizer::kNoAddFinalizer>(self, allocator_type);
277 }
278
279 // Given the context of a calling Method and an initialized class, create an instance.
280 template <bool kInstrumented>
281 ALWAYS_INLINE
AllocObjectFromCodeInitialized(ObjPtr<mirror::Class> klass,Thread * self,gc::AllocatorType allocator_type)282 inline ObjPtr<mirror::Object> AllocObjectFromCodeInitialized(ObjPtr<mirror::Class> klass,
283 Thread* self,
284 gc::AllocatorType allocator_type) {
285 DCHECK(klass != nullptr);
286 // Pass in kNoAddFinalizer since the object cannot be finalizable.
287 return klass->Alloc<kInstrumented,
288 mirror::Class::AddFinalizer::kNoAddFinalizer>(self, allocator_type);
289 }
290
291
292 ALWAYS_INLINE
CheckArrayAlloc(dex::TypeIndex type_idx,int32_t component_count,ArtMethod * method,bool * slow_path)293 inline ObjPtr<mirror::Class> CheckArrayAlloc(dex::TypeIndex type_idx,
294 int32_t component_count,
295 ArtMethod* method,
296 bool* slow_path) {
297 if (UNLIKELY(component_count < 0)) {
298 ThrowNegativeArraySizeException(component_count);
299 *slow_path = true;
300 return nullptr; // Failure
301 }
302 ObjPtr<mirror::Class> klass = method->GetDexCache()->GetResolvedType(type_idx);
303 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
304 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
305 klass = class_linker->ResolveType(type_idx, method);
306 *slow_path = true;
307 if (klass == nullptr) { // Error
308 DCHECK(Thread::Current()->IsExceptionPending());
309 return nullptr; // Failure
310 }
311 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
312 }
313 if (!method->SkipAccessChecks()) {
314 ObjPtr<mirror::Class> referrer = method->GetDeclaringClass();
315 if (UNLIKELY(!referrer->CanAccess(klass))) {
316 ThrowIllegalAccessErrorClass(referrer, klass);
317 *slow_path = true;
318 return nullptr; // Failure
319 }
320 }
321 return klass;
322 }
323
324 // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
325 // it cannot be resolved, throw an error. If it can, use it to create an array.
326 // When verification/compiler hasn't been able to verify access, optionally perform an access
327 // check.
328 template <bool kInstrumented>
329 ALWAYS_INLINE
AllocArrayFromCode(dex::TypeIndex type_idx,int32_t component_count,ArtMethod * method,Thread * self,gc::AllocatorType allocator_type)330 inline ObjPtr<mirror::Array> AllocArrayFromCode(dex::TypeIndex type_idx,
331 int32_t component_count,
332 ArtMethod* method,
333 Thread* self,
334 gc::AllocatorType allocator_type) {
335 bool slow_path = false;
336 ObjPtr<mirror::Class> klass = CheckArrayAlloc(type_idx, component_count, method, &slow_path);
337 if (UNLIKELY(slow_path)) {
338 if (klass == nullptr) {
339 return nullptr;
340 }
341 gc::Heap* heap = Runtime::Current()->GetHeap();
342 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
343 return mirror::Array::Alloc</*kInstrumented=*/true>(self,
344 klass,
345 component_count,
346 klass->GetComponentSizeShift(),
347 heap->GetCurrentAllocator());
348 }
349 return mirror::Array::Alloc<kInstrumented>(self,
350 klass,
351 component_count,
352 klass->GetComponentSizeShift(),
353 allocator_type);
354 }
355
356 template <bool kInstrumented>
357 ALWAYS_INLINE
AllocArrayFromCodeResolved(ObjPtr<mirror::Class> klass,int32_t component_count,Thread * self,gc::AllocatorType allocator_type)358 inline ObjPtr<mirror::Array> AllocArrayFromCodeResolved(ObjPtr<mirror::Class> klass,
359 int32_t component_count,
360 Thread* self,
361 gc::AllocatorType allocator_type) {
362 DCHECK(klass != nullptr);
363 if (UNLIKELY(component_count < 0)) {
364 ThrowNegativeArraySizeException(component_count);
365 return nullptr; // Failure
366 }
367 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
368 // suspension.
369 return mirror::Array::Alloc<kInstrumented>(self,
370 klass,
371 component_count,
372 klass->GetComponentSizeShift(),
373 allocator_type);
374 }
375
376 FLATTEN
ResolveFieldWithAccessChecks(Thread * self,ClassLinker * class_linker,uint16_t field_index,ArtMethod * caller,bool is_static,bool is_put,size_t resolve_field_type)377 inline ArtField* ResolveFieldWithAccessChecks(Thread* self,
378 ClassLinker* class_linker,
379 uint16_t field_index,
380 ArtMethod* caller,
381 bool is_static,
382 bool is_put,
383 size_t resolve_field_type) // Resolve if not zero
384 REQUIRES_SHARED(Locks::mutator_lock_) {
385 if (caller->SkipAccessChecks()) {
386 return class_linker->ResolveField(field_index, caller, is_static);
387 }
388
389 caller = caller->GetInterfaceMethodIfProxy(class_linker->GetImagePointerSize());
390
391 StackHandleScope<2> hs(self);
392 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(caller->GetDexCache()));
393 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(caller->GetClassLoader()));
394
395 ArtField* resolved_field = class_linker->ResolveFieldJLS(field_index,
396 h_dex_cache,
397 h_class_loader);
398 if (resolved_field == nullptr) {
399 return nullptr;
400 }
401
402 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
403 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
404 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, caller);
405 return nullptr;
406 }
407 ObjPtr<mirror::Class> referring_class = caller->GetDeclaringClass();
408 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
409 resolved_field,
410 caller->GetDexCache(),
411 field_index))) {
412 DCHECK(self->IsExceptionPending());
413 return nullptr;
414 }
415 if (UNLIKELY(is_put && !resolved_field->CanBeChangedBy(caller))) {
416 ThrowIllegalAccessErrorFinalField(caller, resolved_field);
417 return nullptr;
418 }
419
420 if (resolve_field_type != 0u) {
421 StackArtFieldHandleScope<1> rhs(self);
422 ReflectiveHandle<ArtField> field_handle(rhs.NewHandle(resolved_field));
423 if (resolved_field->ResolveType().IsNull()) {
424 DCHECK(self->IsExceptionPending());
425 return nullptr;
426 }
427 resolved_field = field_handle.Get();
428 }
429 return resolved_field;
430 }
431
432 template<FindFieldType type>
433 inline ArtField* FindFieldFromCode(uint32_t field_idx,
434 ArtMethod* referrer,
435 Thread* self,
436 bool should_resolve_type = false) {
437 constexpr bool is_set = (type & FindFieldFlags::WriteBit) != 0;
438 constexpr bool is_static = (type & FindFieldFlags::StaticBit) != 0;
439 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
440 ArtField* resolved_field = ResolveFieldWithAccessChecks(
441 self, class_linker, field_idx, referrer, is_static, is_set, should_resolve_type ? 1u : 0u);
442 if (!is_static || resolved_field == nullptr) {
443 // instance fields must be being accessed on an initialized class
444 return resolved_field;
445 } else {
446 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
447 // If the class is initialized we're done.
448 if (LIKELY(fields_class->IsVisiblyInitialized())) {
449 return resolved_field;
450 } else {
451 StackHandleScope<1> hs(self);
452 StackArtFieldHandleScope<1> rhs(self);
453 ReflectiveHandle<ArtField> resolved_field_handle(rhs.NewHandle(resolved_field));
454 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
455 // Otherwise let's ensure the class is initialized before resolving the field.
456 return resolved_field_handle.Get();
457 }
458 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
459 return nullptr; // Failure.
460 }
461 }
462 }
463
464 // NOLINTBEGIN(bugprone-macro-parentheses)
465 // Explicit template declarations of FindFieldFromCode for all field access types.
466 #define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type) \
467 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
468 ArtField* FindFieldFromCode<_type>(uint32_t field_idx, \
469 ArtMethod* referrer, \
470 Thread* self, \
471 bool should_resolve_type = false) \
472
473 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(InstanceObjectRead);
474 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(InstanceObjectWrite);
475 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(InstancePrimitiveRead);
476 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(InstancePrimitiveWrite);
477 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(StaticObjectRead);
478 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(StaticObjectWrite);
479 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(StaticPrimitiveRead);
480 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(StaticPrimitiveWrite);
481
482 #undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
483 // NOLINTEND(bugprone-macro-parentheses)
484
IsStringInit(const DexFile * dex_file,uint32_t method_idx)485 static inline bool IsStringInit(const DexFile* dex_file, uint32_t method_idx)
486 REQUIRES_SHARED(Locks::mutator_lock_) {
487 const dex::MethodId& method_id = dex_file->GetMethodId(method_idx);
488 const char* class_name = dex_file->StringByTypeIdx(method_id.class_idx_);
489 const char* method_name = dex_file->GetMethodName(method_id);
490 // Instead of calling ResolveMethod() which has suspend point and can trigger
491 // GC, look up the method symbolically.
492 // Compare method's class name and method name against string init.
493 // It's ok since it's not allowed to create your own java/lang/String.
494 // TODO: verify that assumption.
495 if ((strcmp(class_name, "Ljava/lang/String;") == 0) &&
496 (strcmp(method_name, "<init>") == 0)) {
497 return true;
498 }
499 return false;
500 }
501
IsStringInit(const Instruction & instr,ArtMethod * caller)502 static inline bool IsStringInit(const Instruction& instr, ArtMethod* caller)
503 REQUIRES_SHARED(Locks::mutator_lock_) {
504 if (instr.Opcode() == Instruction::INVOKE_DIRECT ||
505 instr.Opcode() == Instruction::INVOKE_DIRECT_RANGE) {
506 uint16_t callee_method_idx = (instr.Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
507 instr.VRegB_3rc() : instr.VRegB_35c();
508 return IsStringInit(caller->GetDexFile(), callee_method_idx);
509 }
510 return false;
511 }
512
513 extern "C" size_t NterpGetMethod(Thread* self, ArtMethod* caller, const uint16_t* dex_pc_ptr);
514
515 template <InvokeType type>
FindMethodToCall(Thread * self,ArtMethod * caller,ObjPtr<mirror::Object> * this_object,const Instruction & inst,bool only_lookup_tls_cache,bool * string_init)516 ArtMethod* FindMethodToCall(Thread* self,
517 ArtMethod* caller,
518 ObjPtr<mirror::Object>* this_object,
519 const Instruction& inst,
520 bool only_lookup_tls_cache,
521 /*out*/ bool* string_init)
522 REQUIRES_SHARED(Locks::mutator_lock_) {
523 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
524
525 // Try to find the method in thread-local cache.
526 size_t tls_value = 0u;
527 if (!self->GetInterpreterCache()->Get(self, &inst, &tls_value)) {
528 if (only_lookup_tls_cache) {
529 return nullptr;
530 }
531 DCHECK(!self->IsExceptionPending());
532 // NterpGetMethod can suspend, so save this_object.
533 StackHandleScope<1> hs(self);
534 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
535 tls_value = NterpGetMethod(self, caller, reinterpret_cast<const uint16_t*>(&inst));
536 if (self->IsExceptionPending()) {
537 return nullptr;
538 }
539 }
540
541 if (type != kStatic && UNLIKELY((*this_object) == nullptr)) {
542 if (UNLIKELY(IsStringInit(inst, caller))) {
543 // Hack for String init:
544 //
545 // We assume that the input of String.<init> in verified code is always
546 // an uninitialized reference. If it is a null constant, it must have been
547 // optimized out by the compiler and we arrive here after deoptimization.
548 // Do not throw NullPointerException.
549 } else {
550 // Maintain interpreter-like semantics where NullPointerException is thrown
551 // after potential NoSuchMethodError from class linker.
552 const uint32_t method_idx = inst.VRegB();
553 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
554 return nullptr;
555 }
556 }
557
558 static constexpr size_t kStringInitMethodFlag = 0b1;
559 static constexpr size_t kInvokeInterfaceOnObjectMethodFlag = 0b1;
560 static constexpr size_t kMethodMask = ~0b11;
561
562 ArtMethod* called_method = nullptr;
563 switch (type) {
564 case kDirect:
565 case kSuper:
566 case kStatic:
567 // Note: for the interpreter, the String.<init> special casing for invocation is handled
568 // in DoCallCommon.
569 *string_init = ((tls_value & kStringInitMethodFlag) != 0);
570 DCHECK_EQ(*string_init, IsStringInit(inst, caller));
571 called_method = reinterpret_cast<ArtMethod*>(tls_value & kMethodMask);
572 break;
573 case kInterface:
574 if ((tls_value & kInvokeInterfaceOnObjectMethodFlag) != 0) {
575 // invokeinterface on a j.l.Object method.
576 uint16_t method_index = tls_value >> 16;
577 called_method = (*this_object)->GetClass()->GetVTableEntry(method_index, pointer_size);
578 } else {
579 ArtMethod* interface_method = reinterpret_cast<ArtMethod*>(tls_value & kMethodMask);
580 called_method = (*this_object)->GetClass()->GetImt(pointer_size)->Get(
581 interface_method->GetImtIndex(), pointer_size);
582 if (called_method->IsRuntimeMethod()) {
583 called_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
584 interface_method, pointer_size);
585 if (UNLIKELY(called_method == nullptr)) {
586 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
587 interface_method, *this_object, caller);
588 return nullptr;
589 }
590 }
591 }
592 break;
593 case kVirtual:
594 called_method = (*this_object)->GetClass()->GetVTableEntry(tls_value, pointer_size);
595 break;
596 }
597
598 if (UNLIKELY(!called_method->IsInvokable())) {
599 called_method->ThrowInvocationTimeError((type == kStatic) ? nullptr : *this_object);
600 return nullptr;
601 }
602 DCHECK(!called_method->IsRuntimeMethod()) << called_method->PrettyMethod();
603 return called_method;
604 }
605
606 template<bool access_check>
FindSuperMethodToCall(uint32_t method_idx,ArtMethod * resolved_method,ArtMethod * referrer,Thread * self)607 ALWAYS_INLINE ArtMethod* FindSuperMethodToCall(uint32_t method_idx,
608 ArtMethod* resolved_method,
609 ArtMethod* referrer,
610 Thread* self)
611 REQUIRES_SHARED(Locks::mutator_lock_) {
612 // TODO This lookup is quite slow.
613 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
614 // that will actually not be what we want in some cases where there are miranda methods or
615 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
616 // this method is coming from.
617 ClassLinker* linker = Runtime::Current()->GetClassLinker();
618 dex::TypeIndex type_idx = referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
619 ObjPtr<mirror::Class> referenced_class = linker->ResolveType(type_idx, referrer);
620 if (UNLIKELY(referenced_class == nullptr)) {
621 DCHECK(self->IsExceptionPending());
622 return nullptr;
623 }
624
625 if (access_check) {
626 if (!referenced_class->IsAssignableFrom(referrer->GetDeclaringClass())) {
627 ThrowNoSuchMethodError(kSuper,
628 resolved_method->GetDeclaringClass(),
629 resolved_method->GetName(),
630 resolved_method->GetSignature());
631 return nullptr;
632 }
633 }
634
635 if (referenced_class->IsInterface()) {
636 // TODO We can do better than this for a (compiled) fastpath.
637 ArtMethod* found_method = referenced_class->FindVirtualMethodForInterfaceSuper(
638 resolved_method, linker->GetImagePointerSize());
639 DCHECK(found_method != nullptr);
640 return found_method;
641 }
642
643 DCHECK(resolved_method->IsCopied() ||
644 !resolved_method->GetDeclaringClass()->IsInterface());
645
646 uint16_t vtable_index = resolved_method->GetMethodIndex();
647 ObjPtr<mirror::Class> super_class = referrer->GetDeclaringClass()->GetSuperClass();
648 if (access_check) {
649 DCHECK(super_class == nullptr || super_class->HasVTable());
650 // Check existence of super class.
651 if (super_class == nullptr ||
652 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
653 // Behavior to agree with that of the verifier.
654 ThrowNoSuchMethodError(kSuper,
655 resolved_method->GetDeclaringClass(),
656 resolved_method->GetName(),
657 resolved_method->GetSignature());
658 return nullptr; // Failure.
659 }
660 }
661 DCHECK(super_class != nullptr);
662 DCHECK(super_class->HasVTable());
663 return super_class->GetVTableEntry(vtable_index, linker->GetImagePointerSize());
664 }
665
ResolveVerifyAndClinit(dex::TypeIndex type_idx,ArtMethod * referrer,Thread * self,bool can_run_clinit,bool verify_access)666 inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
667 ArtMethod* referrer,
668 Thread* self,
669 bool can_run_clinit,
670 bool verify_access) {
671 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
672 ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
673 if (UNLIKELY(klass == nullptr)) {
674 CHECK(self->IsExceptionPending());
675 return nullptr; // Failure - Indicate to caller to deliver exception
676 }
677 // Perform access check if necessary.
678 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
679 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
680 ThrowIllegalAccessErrorClass(referring_class, klass);
681 return nullptr; // Failure - Indicate to caller to deliver exception
682 }
683 // If we're just implementing const-class, we shouldn't call <clinit>.
684 if (!can_run_clinit) {
685 return klass;
686 }
687 // If we are the <clinit> of this class, just return our storage.
688 //
689 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
690 // running.
691 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
692 return klass;
693 }
694 StackHandleScope<1> hs(self);
695 Handle<mirror::Class> h_class(hs.NewHandle(klass));
696 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
697 CHECK(self->IsExceptionPending());
698 return nullptr; // Failure - Indicate to caller to deliver exception
699 }
700 return h_class.Get();
701 }
702
703 template <typename INT_TYPE, typename FLOAT_TYPE>
art_float_to_integral(FLOAT_TYPE f)704 inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
705 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
706 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
707 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
708 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
709 if (LIKELY(f > kMinIntAsFloat)) {
710 if (LIKELY(f < kMaxIntAsFloat)) {
711 return static_cast<INT_TYPE>(f);
712 } else {
713 return kMaxInt;
714 }
715 } else {
716 return (f != f) ? 0 : kMinInt; // f != f implies NaN
717 }
718 }
719
GetGenericJniSynchronizationObject(Thread * self,ArtMethod * called)720 inline ObjPtr<mirror::Object> GetGenericJniSynchronizationObject(Thread* self, ArtMethod* called)
721 REQUIRES_SHARED(Locks::mutator_lock_) {
722 DCHECK(!called->IsCriticalNative());
723 DCHECK(!called->IsFastNative());
724 DCHECK(self->GetManagedStack()->GetTopQuickFrame() != nullptr);
725 DCHECK_EQ(*self->GetManagedStack()->GetTopQuickFrame(), called);
726 // We do not need read barriers here.
727 // On method entry, all reference arguments are to-space references and we mark the
728 // declaring class of a static native method if needed. When visiting thread roots at
729 // the start of a GC, we visit all these references to ensure they point to the to-space.
730 if (called->IsStatic()) {
731 // Static methods synchronize on the declaring class object.
732 return called->GetDeclaringClass<kWithoutReadBarrier>();
733 } else {
734 // Instance methods synchronize on the `this` object.
735 // The `this` reference is stored in the first out vreg in the caller's frame.
736 uint8_t* sp = reinterpret_cast<uint8_t*>(self->GetManagedStack()->GetTopQuickFrame());
737 size_t frame_size = RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
738 StackReference<mirror::Object>* this_ref = reinterpret_cast<StackReference<mirror::Object>*>(
739 sp + frame_size + static_cast<size_t>(kRuntimePointerSize));
740 return this_ref->AsMirrorPtr();
741 }
742 }
743
744 } // namespace art
745
746 #endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
747