blob: 65472998536c3df85cae3c229f2c80c00ca451e7 [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
Andreas Gampec1d4cd72017-04-21 13:24:52 -070022#include "art_field-inl.h"
Matthew Gharrity465ecc82016-07-19 21:32:52 +000023#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "class_linker-inl.h"
26#include "common_throws.h"
27#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010028#include "entrypoints/quick/callee_save_frame.h"
29#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070030#include "imt_conflict_table.h"
31#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070032#include "indirect_reference_table.h"
33#include "invoke_type.h"
34#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035#include "mirror/array.h"
36#include "mirror/class-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010039#include "nth_caller_visitor.h"
40#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010041#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070042#include "thread.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070043#include "well_known_classes.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070044
45namespace art {
46
Mathieu Chartiere401d142015-04-22 13:56:20 -070047inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070048 const MethodInfo& method_info,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010049 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000050 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010051 uint8_t inlining_depth)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070052 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010053 // This method is being used by artQuickResolutionTrampoline, before it sets up
54 // the passed parameters in a GC friendly way. Therefore we must never be
55 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070056 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010057
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000058 if (inline_info.EncodesArtMethodAtDepth(encoding, inlining_depth)) {
59 return inline_info.GetArtMethodAtDepth(encoding, inlining_depth);
60 }
61
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070062 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, method_info, inlining_depth);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000063 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
64 // "charAt" special case. It is the only non-leaf method we inline across dex files.
65 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
66 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010067 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070068 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010069
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000070 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010071 ArtMethod* caller = outer_method;
72 if (inlining_depth != 0) {
73 caller = GetResolvedMethod(outer_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070074 method_info,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010075 inline_info,
76 encoding,
77 inlining_depth - 1);
78 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010079
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000080 // Lookup the declaring class of the inlined method.
81 const DexFile* dex_file = caller->GetDexFile();
82 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
Mathieu Chartier20eb58e2017-02-01 15:50:54 -080083 ArtMethod* inlined_method = caller->GetDexCacheResolvedMethod(method_index, kRuntimePointerSize);
84 if (inlined_method != nullptr && !inlined_method->IsRuntimeMethod()) {
85 return inlined_method;
86 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000087 const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_);
88 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
89 Thread* self = Thread::Current();
90 mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader();
91 mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader);
92 if (klass == nullptr) {
93 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
94 << "the class " << descriptor << " was not found in the class loader of "
95 << caller->PrettyMethod() << ". "
96 << "This must be due to playing wrongly with class loaders";
97 }
98
99 // Lookup the method.
100 const char* method_name = dex_file->GetMethodName(method_id);
101 const Signature signature = dex_file->GetMethodSignature(method_id);
102
Mathieu Chartier20eb58e2017-02-01 15:50:54 -0800103 inlined_method = klass->FindDeclaredDirectMethod(method_name, signature, kRuntimePointerSize);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000104 if (inlined_method == nullptr) {
105 inlined_method = klass->FindDeclaredVirtualMethod(method_name, signature, kRuntimePointerSize);
106 if (inlined_method == nullptr) {
107 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
108 << "the class " << descriptor << " does not have "
109 << method_name << signature << " declared. "
110 << "This must be due to duplicate classes or playing wrongly with class loaders";
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100111 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100112 }
Mathieu Chartier20eb58e2017-02-01 15:50:54 -0800113 caller->SetDexCacheResolvedMethod(method_index, inlined_method, kRuntimePointerSize);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100114
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100115 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100116}
117
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000118ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
119 Thread* self,
120 bool* slow_path)
121 REQUIRES_SHARED(Locks::mutator_lock_)
122 REQUIRES(!Roles::uninterruptible_) {
123 if (UNLIKELY(!klass->IsInstantiable())) {
124 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700125 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000126 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700127 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000128 if (UNLIKELY(klass->IsClassClass())) {
129 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
130 klass->PrettyDescriptor().c_str());
131 *slow_path = true;
132 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700133 }
134 if (UNLIKELY(!klass->IsInitialized())) {
135 StackHandleScope<1> hs(self);
136 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
137 // EnsureInitialized (the class initializer) might cause a GC.
138 // may cause us to suspend meaning that another thread may try to
139 // change the allocator while we are stuck in the entrypoints of
140 // an old allocator. Also, the class initialization may fail. To
141 // handle these cases we mark the slow path boolean as true so
142 // that the caller knows to check the allocator type to see if it
143 // has changed and to null-check the return value in case the
144 // initialization fails.
145 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700146 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700147 DCHECK(self->IsExceptionPending());
148 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700149 } else {
150 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700151 }
152 return h_klass.Get();
153 }
154 return klass;
155}
156
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700157ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800158inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
159 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000160 bool* slow_path)
161 REQUIRES_SHARED(Locks::mutator_lock_)
162 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700163 if (UNLIKELY(!klass->IsInitialized())) {
164 StackHandleScope<1> hs(self);
165 Handle<mirror::Class> h_class(hs.NewHandle(klass));
166 // EnsureInitialized (the class initializer) might cause a GC.
167 // may cause us to suspend meaning that another thread may try to
168 // change the allocator while we are stuck in the entrypoints of
169 // an old allocator. Also, the class initialization may fail. To
170 // handle these cases we mark the slow path boolean as true so
171 // that the caller knows to check the allocator type to see if it
172 // has changed and to null-check the return value in case the
173 // initialization fails.
174 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700175 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700176 DCHECK(self->IsExceptionPending());
177 return nullptr; // Failure
178 }
179 return h_class.Get();
180 }
181 return klass;
182}
183
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000184// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
185// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
186template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700187ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000188inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800189 Thread* self,
190 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700191 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000192 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700193 if (UNLIKELY(slow_path)) {
194 if (klass == nullptr) {
195 return nullptr;
196 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800197 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
198 return klass->Alloc</*kInstrumented*/true>(
199 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700200 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700201 }
202 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700203 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700204}
205
206// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700207template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700208ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800209inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
210 Thread* self,
211 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700212 DCHECK(klass != nullptr);
213 bool slow_path = false;
214 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
215 if (UNLIKELY(slow_path)) {
216 if (klass == nullptr) {
217 return nullptr;
218 }
219 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000220 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800221 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
222 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700223 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700224 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000225 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700226 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700227}
228
229// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700230template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700231ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800232inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
233 Thread* self,
234 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000236 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700237 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700238}
239
240
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700241template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700242ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800243inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800244 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700245 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800246 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 if (UNLIKELY(component_count < 0)) {
248 ThrowNegativeArraySizeException(component_count);
249 *slow_path = true;
250 return nullptr; // Failure
251 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000252 mirror::Class* klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000254 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100255 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700256 *slow_path = true;
257 if (klass == nullptr) { // Error
258 DCHECK(Thread::Current()->IsExceptionPending());
259 return nullptr; // Failure
260 }
David Sehr709b0702016-10-13 09:12:37 -0700261 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700262 }
263 if (kAccessCheck) {
264 mirror::Class* referrer = method->GetDeclaringClass();
265 if (UNLIKELY(!referrer->CanAccess(klass))) {
266 ThrowIllegalAccessErrorClass(referrer, klass);
267 *slow_path = true;
268 return nullptr; // Failure
269 }
270 }
271 return klass;
272}
273
274// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
275// it cannot be resolved, throw an error. If it can, use it to create an array.
276// When verification/compiler hasn't been able to verify access, optionally perform an access
277// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700278template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700279ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800280inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800281 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800283 Thread* self,
284 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700285 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800286 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700287 &slow_path);
288 if (UNLIKELY(slow_path)) {
289 if (klass == nullptr) {
290 return nullptr;
291 }
292 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800293 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
294 return mirror::Array::Alloc</*kInstrumented*/true>(self,
295 klass,
296 component_count,
297 klass->GetComponentSizeShift(),
298 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700299 }
300 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700301 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700302}
303
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000304template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700305ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800306inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800307 int32_t component_count,
308 Thread* self,
309 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700310 DCHECK(klass != nullptr);
311 if (UNLIKELY(component_count < 0)) {
312 ThrowNegativeArraySizeException(component_count);
313 return nullptr; // Failure
314 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700315 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
316 // suspension.
317 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700318 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700319}
320
321template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800322inline ArtField* FindFieldFromCode(uint32_t field_idx,
323 ArtMethod* referrer,
324 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700325 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700326 bool is_primitive;
327 bool is_set;
328 bool is_static;
329 switch (type) {
330 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
331 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
332 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
333 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
334 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
335 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
336 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
337 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
338 default: is_primitive = true; is_set = true; is_static = true; break;
339 }
340 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800341
342 ArtField* resolved_field;
343 if (access_check) {
344 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
345 // qualifying type of a field and the resolved run-time qualifying type of a field differed
346 // in their static-ness.
347 //
348 // In particular, don't assume the dex instruction already correctly knows if the
349 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700350 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800351
352 StackHandleScope<2> hs(self);
353 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
354 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
355
356 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
357 field_idx,
358 h_dex_cache,
359 h_class_loader);
360 } else {
361 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
362 // be executing here if there was a static/non-static mismatch.
363 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
364 }
365
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700366 if (UNLIKELY(resolved_field == nullptr)) {
367 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
368 return nullptr; // Failure.
369 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700370 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700371 if (access_check) {
372 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
373 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
374 return nullptr;
375 }
376 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700377 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
378 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100379 referrer->GetDexCache(),
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 =
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100465 referrer->GetDeclaringClass()->CheckResolvedMethodAccess(methods_class,
466 resolved_method,
467 referrer->GetDexCache(),
468 method_idx,
469 type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700470 if (UNLIKELY(!can_access_resolved_method)) {
471 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
472 return nullptr; // Failure.
473 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100474 // Incompatible class change should have been handled in resolve method.
475 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
476 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
477 referrer);
478 return nullptr; // Failure.
479 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700480 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700481 // Next, null pointer check.
482 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
483 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
484 resolved_method->IsConstructor())) {
485 // Hack for String init:
486 //
487 // We assume that the input of String.<init> in verified code is always
488 // an unitialized reference. If it is a null constant, it must have been
489 // optimized out by the compiler. Do not throw NullPointerException.
490 } else {
491 // Maintain interpreter-like semantics where NullPointerException is thrown
492 // after potential NoSuchMethodError from class linker.
493 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
494 return nullptr; // Failure.
495 }
496 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700497 switch (type) {
498 case kStatic:
499 case kDirect:
500 return resolved_method;
501 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700502 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700503 uint16_t vtable_index = resolved_method->GetMethodIndex();
504 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700505 (!klass->HasVTable() ||
506 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700507 // Behavior to agree with that of the verifier.
508 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
509 resolved_method->GetName(), resolved_method->GetSignature());
510 return nullptr; // Failure.
511 }
David Sehr709b0702016-10-13 09:12:37 -0700512 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700514 }
515 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700516 // TODO This lookup is quite slow.
517 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
518 // that will actually not be what we want in some cases where there are miranda methods or
519 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
520 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700521 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700522 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700523 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800524 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800525 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700526 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
527 if (UNLIKELY(method_reference_class == nullptr)) {
528 // Bad type idx.
529 CHECK(self->IsExceptionPending());
530 return nullptr;
531 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700532 // It is not an interface. If the referring class is in the class hierarchy of the
533 // referenced class in the bytecode, we use its super class. Otherwise, we throw
534 // a NoSuchMethodError.
535 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700536 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
537 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700538 }
Alex Light705ad492015-09-21 11:36:30 -0700539 uint16_t vtable_index = resolved_method->GetMethodIndex();
540 if (access_check) {
541 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700542 if (super_class == nullptr ||
543 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700544 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
545 // Behavior to agree with that of the verifier.
546 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
547 resolved_method->GetName(), resolved_method->GetSignature());
548 return nullptr; // Failure.
549 }
550 }
551 DCHECK(super_class != nullptr);
552 DCHECK(super_class->HasVTable());
553 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
554 } else {
555 // It is an interface.
556 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700557 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700558 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
559 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700560 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700561 referrer);
562 return nullptr; // Failure.
563 }
564 }
565 // TODO We can do better than this for a (compiled) fastpath.
566 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
567 resolved_method, class_linker->GetImagePointerSize());
568 // Throw an NSME if nullptr;
569 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700570 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
571 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700572 }
Alex Light705ad492015-09-21 11:36:30 -0700573 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700574 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700575 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700576 }
577 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700578 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700579 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000580 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
581 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000582 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700583 if (kIsDebugBuild) {
584 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700585 ArtMethod* method = klass->FindVirtualMethodForInterface(
586 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700587 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
588 << imt_method->PrettyMethod() << " / "
589 << ArtMethod::PrettyMethod(method) << " / "
590 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700591 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700592 return imt_method;
593 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
595 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700596 if (UNLIKELY(interface_method == nullptr)) {
597 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700598 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700599 return nullptr; // Failure.
600 }
601 return interface_method;
602 }
603 }
604 default:
605 LOG(FATAL) << "Unknown invoke type " << type;
606 return nullptr; // Failure.
607 }
608}
609
610// Explicit template declarations of FindMethodFromCode for all invoke types.
611#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700612 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700613 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700614 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700615 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700616 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700617#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
618 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
619 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
620
621EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
622EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
623EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
624EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
625EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
626
627#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
628#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
629
630// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700631inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
632 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700633 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700634 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800635 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700636 if (UNLIKELY(resolved_field == nullptr)) {
637 return nullptr;
638 }
639 // Check for incompatible class change.
640 bool is_primitive;
641 bool is_set;
642 bool is_static;
643 switch (type) {
644 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
645 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
646 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
647 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
648 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
649 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
650 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
651 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
652 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700653 LOG(FATAL) << "UNREACHABLE";
654 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700655 }
656 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
657 // Incompatible class change.
658 return nullptr;
659 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700660 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700661 if (is_static) {
662 // Check class is initialized else fail so that we can contend to initialize the class with
663 // other threads that may be racing to do this.
664 if (UNLIKELY(!fields_class->IsInitialized())) {
665 return nullptr;
666 }
667 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100668 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700669 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700670 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700671 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
672 // Illegal access.
673 return nullptr;
674 }
675 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
676 resolved_field->FieldSize() != expected_size)) {
677 return nullptr;
678 }
679 return resolved_field;
680}
681
682// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100683template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700684inline ArtMethod* FindMethodFast(uint32_t method_idx,
685 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100686 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700687 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700688 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
689 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700690 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100691 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
692 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
693 ArtMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700694 if (UNLIKELY(resolved_method == nullptr)) {
695 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700696 }
697 if (access_check) {
698 // Check for incompatible class change errors and access.
699 bool icce = resolved_method->CheckIncompatibleClassChange(type);
700 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700701 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700702 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100703 ObjPtr<mirror::Class> methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700704 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
705 !referring_class->CanAccessMember(methods_class,
706 resolved_method->GetAccessFlags()))) {
707 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700708 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700709 }
710 }
711 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700712 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
713 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700714 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700715 return resolved_method;
716 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700717 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000718 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
719 ObjPtr<mirror::Class> method_reference_class = ClassLinker::LookupResolvedType(
720 method_type_idx, dex_cache, referrer->GetClassLoader());
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 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100731 ObjPtr<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 Gampec6ea7d02017-02-01 16:46:28 -0800785static inline mirror::String* ResolveString(ClassLinker* class_linker,
786 dex::StringIndex string_idx,
787 ArtMethod* referrer)
788 REQUIRES_SHARED(Locks::mutator_lock_) {
789 Thread::PoisonObjectPointersIfDebug();
790 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
791 if (UNLIKELY(string == nullptr)) {
792 StackHandleScope<1> hs(Thread::Current());
793 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
794 const DexFile& dex_file = *dex_cache->GetDexFile();
795 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
796 }
797 return string.Ptr();
798}
799
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800800inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800801 Thread::PoisonObjectPointersIfDebug();
802 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
803 if (UNLIKELY(string == nullptr)) {
804 StackHandleScope<1> hs(Thread::Current());
805 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
806 const DexFile& dex_file = *dex_cache->GetDexFile();
807 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
808 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
809 }
810 return string.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700811}
812
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800813inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700814 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700815 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700816 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000817 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700818 self->ClearException();
819 }
820 // Decode locked object and unlock, before popping local references.
821 self->DecodeJObject(locked)->MonitorExit(self);
822 if (UNLIKELY(self->IsExceptionPending())) {
823 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
824 << saved_exception->Dump()
825 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000826 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700827 }
828 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700829 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000830 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700831 }
832}
833
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700834template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800835inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700836 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
837 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
838 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
839 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
840 if (LIKELY(f > kMinIntAsFloat)) {
841 if (LIKELY(f < kMaxIntAsFloat)) {
842 return static_cast<INT_TYPE>(f);
843 } else {
844 return kMaxInt;
845 }
846 } else {
847 return (f != f) ? 0 : kMinInt; // f != f implies NaN
848 }
849}
850
851} // namespace art
852
853#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_