blob: 7d6f866617f04e0d3bbdfc3af991d767a53a9f7f [file] [log] [blame]
Mingyao Yang98d1cc82014-05-15 17:02:16 -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
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
Matthew Gharrity465ecc82016-07-19 21:32:52 +000022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "class_linker-inl.h"
25#include "common_throws.h"
26#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010027#include "entrypoints/quick/callee_save_frame.h"
28#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070029#include "imt_conflict_table.h"
30#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "indirect_reference_table.h"
32#include "invoke_type.h"
33#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "mirror/array.h"
35#include "mirror/class-inl.h"
36#include "mirror/object-inl.h"
37#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010038#include "nth_caller_visitor.h"
39#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010040#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070041#include "thread.h"
42
43namespace art {
44
Mathieu Chartiere401d142015-04-22 13:56:20 -070045inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010046 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000047 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010048 uint8_t inlining_depth)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070049 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010050 // This method is being used by artQuickResolutionTrampoline, before it sets up
51 // the passed parameters in a GC friendly way. Therefore we must never be
52 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070053 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010054
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000055 if (inline_info.EncodesArtMethodAtDepth(encoding, inlining_depth)) {
56 return inline_info.GetArtMethodAtDepth(encoding, inlining_depth);
57 }
58
David Srbecky61b28a12016-02-25 21:55:03 +000059 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, inlining_depth);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000060 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
61 // "charAt" special case. It is the only non-leaf method we inline across dex files.
62 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
63 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010064 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070065 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010066
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000067 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010068 ArtMethod* caller = outer_method;
69 if (inlining_depth != 0) {
70 caller = GetResolvedMethod(outer_method,
71 inline_info,
72 encoding,
73 inlining_depth - 1);
74 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010075
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000076 // Lookup the declaring class of the inlined method.
77 const DexFile* dex_file = caller->GetDexFile();
78 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
79 const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_);
80 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
81 Thread* self = Thread::Current();
82 mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader();
83 mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader);
84 if (klass == nullptr) {
85 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
86 << "the class " << descriptor << " was not found in the class loader of "
87 << caller->PrettyMethod() << ". "
88 << "This must be due to playing wrongly with class loaders";
89 }
90
91 // Lookup the method.
92 const char* method_name = dex_file->GetMethodName(method_id);
93 const Signature signature = dex_file->GetMethodSignature(method_id);
94
95 ArtMethod* inlined_method =
96 klass->FindDeclaredDirectMethod(method_name, signature, kRuntimePointerSize);
97 if (inlined_method == nullptr) {
98 inlined_method = klass->FindDeclaredVirtualMethod(method_name, signature, kRuntimePointerSize);
99 if (inlined_method == nullptr) {
100 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
101 << "the class " << descriptor << " does not have "
102 << method_name << signature << " declared. "
103 << "This must be due to duplicate classes or playing wrongly with class loaders";
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100104 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100105 }
106
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100107 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100108}
109
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000110ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
111 Thread* self,
112 bool* slow_path)
113 REQUIRES_SHARED(Locks::mutator_lock_)
114 REQUIRES(!Roles::uninterruptible_) {
115 if (UNLIKELY(!klass->IsInstantiable())) {
116 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700117 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000118 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700119 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000120 if (UNLIKELY(klass->IsClassClass())) {
121 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
122 klass->PrettyDescriptor().c_str());
123 *slow_path = true;
124 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700125 }
126 if (UNLIKELY(!klass->IsInitialized())) {
127 StackHandleScope<1> hs(self);
128 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
129 // EnsureInitialized (the class initializer) might cause a GC.
130 // may cause us to suspend meaning that another thread may try to
131 // change the allocator while we are stuck in the entrypoints of
132 // an old allocator. Also, the class initialization may fail. To
133 // handle these cases we mark the slow path boolean as true so
134 // that the caller knows to check the allocator type to see if it
135 // has changed and to null-check the return value in case the
136 // initialization fails.
137 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700138 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139 DCHECK(self->IsExceptionPending());
140 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700141 } else {
142 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700143 }
144 return h_klass.Get();
145 }
146 return klass;
147}
148
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700149ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800150inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
151 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000152 bool* slow_path)
153 REQUIRES_SHARED(Locks::mutator_lock_)
154 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700155 if (UNLIKELY(!klass->IsInitialized())) {
156 StackHandleScope<1> hs(self);
157 Handle<mirror::Class> h_class(hs.NewHandle(klass));
158 // EnsureInitialized (the class initializer) might cause a GC.
159 // may cause us to suspend meaning that another thread may try to
160 // change the allocator while we are stuck in the entrypoints of
161 // an old allocator. Also, the class initialization may fail. To
162 // handle these cases we mark the slow path boolean as true so
163 // that the caller knows to check the allocator type to see if it
164 // has changed and to null-check the return value in case the
165 // initialization fails.
166 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700167 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700168 DCHECK(self->IsExceptionPending());
169 return nullptr; // Failure
170 }
171 return h_class.Get();
172 }
173 return klass;
174}
175
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000176// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
177// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
178template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700179ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000180inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800181 Thread* self,
182 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700183 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000184 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700185 if (UNLIKELY(slow_path)) {
186 if (klass == nullptr) {
187 return nullptr;
188 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800189 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
190 return klass->Alloc</*kInstrumented*/true>(
191 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700192 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700193 }
194 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700195 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700196}
197
198// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700199template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700200ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800201inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
202 Thread* self,
203 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700204 DCHECK(klass != nullptr);
205 bool slow_path = false;
206 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
207 if (UNLIKELY(slow_path)) {
208 if (klass == nullptr) {
209 return nullptr;
210 }
211 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000212 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800213 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
214 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700215 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700216 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000217 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700218 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700219}
220
221// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700222template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700223ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800224inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
225 Thread* self,
226 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700227 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000228 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700229 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700230}
231
232
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700233template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700234ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800235inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800236 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700237 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800238 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700239 if (UNLIKELY(component_count < 0)) {
240 ThrowNegativeArraySizeException(component_count);
241 *slow_path = true;
242 return nullptr; // Failure
243 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100244 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700245 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +0100246 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100248 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700249 *slow_path = true;
250 if (klass == nullptr) { // Error
251 DCHECK(Thread::Current()->IsExceptionPending());
252 return nullptr; // Failure
253 }
David Sehr709b0702016-10-13 09:12:37 -0700254 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700255 }
256 if (kAccessCheck) {
257 mirror::Class* referrer = method->GetDeclaringClass();
258 if (UNLIKELY(!referrer->CanAccess(klass))) {
259 ThrowIllegalAccessErrorClass(referrer, klass);
260 *slow_path = true;
261 return nullptr; // Failure
262 }
263 }
264 return klass;
265}
266
267// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
268// it cannot be resolved, throw an error. If it can, use it to create an array.
269// When verification/compiler hasn't been able to verify access, optionally perform an access
270// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700271template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700272ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800273inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800274 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800276 Thread* self,
277 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700278 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800279 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700280 &slow_path);
281 if (UNLIKELY(slow_path)) {
282 if (klass == nullptr) {
283 return nullptr;
284 }
285 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800286 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
287 return mirror::Array::Alloc</*kInstrumented*/true>(self,
288 klass,
289 component_count,
290 klass->GetComponentSizeShift(),
291 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700292 }
293 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700294 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700295}
296
297template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700298ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800299inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800300 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800302 Thread* self,
303 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700304 DCHECK(klass != nullptr);
305 if (UNLIKELY(component_count < 0)) {
306 ThrowNegativeArraySizeException(component_count);
307 return nullptr; // Failure
308 }
309 if (kAccessCheck) {
310 mirror::Class* referrer = method->GetDeclaringClass();
311 if (UNLIKELY(!referrer->CanAccess(klass))) {
312 ThrowIllegalAccessErrorClass(referrer, klass);
313 return nullptr; // Failure
314 }
315 }
316 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
317 // suspension.
318 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700319 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700320}
321
322template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800323inline ArtField* FindFieldFromCode(uint32_t field_idx,
324 ArtMethod* referrer,
325 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700326 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700327 bool is_primitive;
328 bool is_set;
329 bool is_static;
330 switch (type) {
331 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
332 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
333 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
334 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
335 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
336 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
337 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
338 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
339 default: is_primitive = true; is_set = true; is_static = true; break;
340 }
341 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800342
343 ArtField* resolved_field;
344 if (access_check) {
345 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
346 // qualifying type of a field and the resolved run-time qualifying type of a field differed
347 // in their static-ness.
348 //
349 // In particular, don't assume the dex instruction already correctly knows if the
350 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700351 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800352
353 StackHandleScope<2> hs(self);
354 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
355 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
356
357 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
358 field_idx,
359 h_dex_cache,
360 h_class_loader);
361 } else {
362 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
363 // be executing here if there was a static/non-static mismatch.
364 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
365 }
366
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700367 if (UNLIKELY(resolved_field == nullptr)) {
368 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
369 return nullptr; // Failure.
370 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700371 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700372 if (access_check) {
373 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
374 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
375 return nullptr;
376 }
377 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700378 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
379 resolved_field,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700380 field_idx))) {
381 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
382 return nullptr; // Failure.
383 }
384 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
385 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
386 return nullptr; // Failure.
387 } else {
388 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
389 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000390 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700391 "Attempted read of %zd-bit %s on field '%s'",
392 expected_size * (32 / sizeof(int32_t)),
393 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700394 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700395 return nullptr; // Failure.
396 }
397 }
398 }
399 if (!is_static) {
400 // instance fields must be being accessed on an initialized class
401 return resolved_field;
402 } else {
403 // If the class is initialized we're done.
404 if (LIKELY(fields_class->IsInitialized())) {
405 return resolved_field;
406 } else {
407 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700408 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700409 // Otherwise let's ensure the class is initialized before resolving the field.
410 return resolved_field;
411 }
412 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
413 return nullptr; // Failure.
414 }
415 }
416}
417
418// Explicit template declarations of FindFieldFromCode for all field access types.
419#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700420template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700421ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 ArtMethod* referrer, \
423 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700424
425#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
426 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
427 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
428
429EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
430EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
431EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
432EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
433EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
434EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
435EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
436EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
437
438#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
439#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
440
441template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700442inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700443 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700444 ArtMethod* referrer,
445 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700446 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700447 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700448 if (resolved_method == nullptr) {
449 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700450 ObjPtr<mirror::Object> null_this = nullptr;
451 HandleWrapperObjPtr<mirror::Object> h_this(
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700452 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800453 constexpr ClassLinker::ResolveMode resolve_mode =
454 access_check ? ClassLinker::kForceICCECheck
455 : ClassLinker::kNoICCECheckForCache;
456 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700457 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700458 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700459 if (UNLIKELY(resolved_method == nullptr)) {
460 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
461 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700462 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700463 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700464 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700465 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
466 resolved_method,
467 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700468 if (UNLIKELY(!can_access_resolved_method)) {
469 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
470 return nullptr; // Failure.
471 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100472 // Incompatible class change should have been handled in resolve method.
473 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
474 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
475 referrer);
476 return nullptr; // Failure.
477 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700478 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700479 // Next, null pointer check.
480 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
481 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
482 resolved_method->IsConstructor())) {
483 // Hack for String init:
484 //
485 // We assume that the input of String.<init> in verified code is always
486 // an unitialized reference. If it is a null constant, it must have been
487 // optimized out by the compiler. Do not throw NullPointerException.
488 } else {
489 // Maintain interpreter-like semantics where NullPointerException is thrown
490 // after potential NoSuchMethodError from class linker.
491 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
492 return nullptr; // Failure.
493 }
494 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700495 switch (type) {
496 case kStatic:
497 case kDirect:
498 return resolved_method;
499 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700500 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700501 uint16_t vtable_index = resolved_method->GetMethodIndex();
502 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700503 (!klass->HasVTable() ||
504 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700505 // Behavior to agree with that of the verifier.
506 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
507 resolved_method->GetName(), resolved_method->GetSignature());
508 return nullptr; // Failure.
509 }
David Sehr709b0702016-10-13 09:12:37 -0700510 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700512 }
513 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700514 // TODO This lookup is quite slow.
515 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
516 // that will actually not be what we want in some cases where there are miranda methods or
517 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
518 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700519 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700520 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700521 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800522 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800523 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700524 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
525 if (UNLIKELY(method_reference_class == nullptr)) {
526 // Bad type idx.
527 CHECK(self->IsExceptionPending());
528 return nullptr;
529 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700530 // It is not an interface. If the referring class is in the class hierarchy of the
531 // referenced class in the bytecode, we use its super class. Otherwise, we throw
532 // a NoSuchMethodError.
533 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700534 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
535 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700536 }
Alex Light705ad492015-09-21 11:36:30 -0700537 uint16_t vtable_index = resolved_method->GetMethodIndex();
538 if (access_check) {
539 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700540 if (super_class == nullptr ||
541 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700542 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
543 // Behavior to agree with that of the verifier.
544 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
545 resolved_method->GetName(), resolved_method->GetSignature());
546 return nullptr; // Failure.
547 }
548 }
549 DCHECK(super_class != nullptr);
550 DCHECK(super_class->HasVTable());
551 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
552 } else {
553 // It is an interface.
554 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700555 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700556 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
557 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700558 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700559 referrer);
560 return nullptr; // Failure.
561 }
562 }
563 // TODO We can do better than this for a (compiled) fastpath.
564 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
565 resolved_method, class_linker->GetImagePointerSize());
566 // Throw an NSME if nullptr;
567 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700568 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
569 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700570 }
Alex Light705ad492015-09-21 11:36:30 -0700571 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700572 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700573 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700574 }
575 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700576 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700577 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000578 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
579 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000580 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700581 if (kIsDebugBuild) {
582 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700583 ArtMethod* method = klass->FindVirtualMethodForInterface(
584 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700585 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
586 << imt_method->PrettyMethod() << " / "
587 << ArtMethod::PrettyMethod(method) << " / "
588 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700589 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700590 return imt_method;
591 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
593 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700594 if (UNLIKELY(interface_method == nullptr)) {
595 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700596 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700597 return nullptr; // Failure.
598 }
599 return interface_method;
600 }
601 }
602 default:
603 LOG(FATAL) << "Unknown invoke type " << type;
604 return nullptr; // Failure.
605 }
606}
607
608// Explicit template declarations of FindMethodFromCode for all invoke types.
609#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700610 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700611 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700612 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700613 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700614 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700615#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
616 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
617 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
618
619EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
620EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
621EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
622EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
623EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
624
625#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
626#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
627
628// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700629inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
630 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700631 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700632 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800633 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700634 if (UNLIKELY(resolved_field == nullptr)) {
635 return nullptr;
636 }
637 // Check for incompatible class change.
638 bool is_primitive;
639 bool is_set;
640 bool is_static;
641 switch (type) {
642 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
643 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
644 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
645 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
646 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
647 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
648 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
649 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
650 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700651 LOG(FATAL) << "UNREACHABLE";
652 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700653 }
654 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
655 // Incompatible class change.
656 return nullptr;
657 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700658 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700659 if (is_static) {
660 // Check class is initialized else fail so that we can contend to initialize the class with
661 // other threads that may be racing to do this.
662 if (UNLIKELY(!fields_class->IsInitialized())) {
663 return nullptr;
664 }
665 }
666 mirror::Class* referring_class = referrer->GetDeclaringClass();
667 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700668 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700669 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
670 // Illegal access.
671 return nullptr;
672 }
673 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
674 resolved_field->FieldSize() != expected_size)) {
675 return nullptr;
676 }
677 return resolved_field;
678}
679
680// Fast path method resolution that can't throw exceptions.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700681inline ArtMethod* FindMethodFast(uint32_t method_idx,
682 ObjPtr<mirror::Object> this_object,
683 ArtMethod* referrer,
684 bool access_check,
685 InvokeType type) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700686 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700687 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
688 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700689 }
Alex Light705ad492015-09-21 11:36:30 -0700690 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691 ArtMethod* resolved_method =
Alex Lightdba61482016-12-21 08:20:29 -0800692 referrer->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700693 if (UNLIKELY(resolved_method == nullptr)) {
694 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700695 }
696 if (access_check) {
697 // Check for incompatible class change errors and access.
698 bool icce = resolved_method->CheckIncompatibleClassChange(type);
699 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700700 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700701 }
702 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700703 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
704 !referring_class->CanAccessMember(methods_class,
705 resolved_method->GetAccessFlags()))) {
706 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700707 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700708 }
709 }
710 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700711 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
712 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700713 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700714 return resolved_method;
715 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700716 // TODO This lookup is rather slow.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800717 dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800718 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700719 mirror::Class* method_reference_class =
Alex Lightdba61482016-12-21 08:20:29 -0800720 referrer->GetDexCache()->GetResolvedType(method_type_idx);
Alex Light705ad492015-09-21 11:36:30 -0700721 if (method_reference_class == nullptr) {
722 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000723 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700724 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700725 // It is not an interface. If the referring class is in the class hierarchy of the
726 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
727 // resolve the method.
728 if (!method_reference_class->IsAssignableFrom(referring_class)) {
729 return nullptr;
730 }
731 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700732 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
733 // The super class does not have the method.
734 return nullptr;
735 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700736 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700737 } else {
738 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700739 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000740 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700741 } else {
742 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700743 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700744 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700745 }
746}
747
Andreas Gampea5b09a62016-11-17 15:21:22 -0800748inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx,
749 ArtMethod* referrer,
750 Thread* self,
751 bool can_run_clinit,
752 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700753 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
754 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
755 if (UNLIKELY(klass == nullptr)) {
756 CHECK(self->IsExceptionPending());
757 return nullptr; // Failure - Indicate to caller to deliver exception
758 }
759 // Perform access check if necessary.
760 mirror::Class* referring_class = referrer->GetDeclaringClass();
761 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
762 ThrowIllegalAccessErrorClass(referring_class, klass);
763 return nullptr; // Failure - Indicate to caller to deliver exception
764 }
765 // If we're just implementing const-class, we shouldn't call <clinit>.
766 if (!can_run_clinit) {
767 return klass;
768 }
769 // If we are the <clinit> of this class, just return our storage.
770 //
771 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
772 // running.
773 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
774 return klass;
775 }
776 StackHandleScope<1> hs(self);
777 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700778 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700779 CHECK(self->IsExceptionPending());
780 return nullptr; // Failure - Indicate to caller to deliver exception
781 }
782 return h_class.Get();
783}
784
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800785inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700786 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
787 return class_linker->ResolveString(string_idx, referrer);
788}
789
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800790inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700791 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700792 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700793 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000794 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700795 self->ClearException();
796 }
797 // Decode locked object and unlock, before popping local references.
798 self->DecodeJObject(locked)->MonitorExit(self);
799 if (UNLIKELY(self->IsExceptionPending())) {
800 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
801 << saved_exception->Dump()
802 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000803 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700804 }
805 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700806 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000807 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700808 }
809}
810
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700811template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800812inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700813 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
814 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
815 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
816 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
817 if (LIKELY(f > kMinIntAsFloat)) {
818 if (LIKELY(f < kMaxIntAsFloat)) {
819 return static_cast<INT_TYPE>(f);
820 } else {
821 return kMaxInt;
822 }
823 } else {
824 return (f != f) ? 0 : kMinInt; // f != f implies NaN
825 }
826}
827
828} // namespace art
829
830#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_