blob: caf2e5dfeec8a1e87e1c7e084aeb38ddb5ad1528 [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,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070046 const MethodInfo& method_info,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010047 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000048 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010049 uint8_t inlining_depth)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070050 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010051 // This method is being used by artQuickResolutionTrampoline, before it sets up
52 // the passed parameters in a GC friendly way. Therefore we must never be
53 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070054 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010055
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000056 if (inline_info.EncodesArtMethodAtDepth(encoding, inlining_depth)) {
57 return inline_info.GetArtMethodAtDepth(encoding, inlining_depth);
58 }
59
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070060 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, method_info, inlining_depth);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000061 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
62 // "charAt" special case. It is the only non-leaf method we inline across dex files.
63 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
64 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010065 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070066 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010067
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000068 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010069 ArtMethod* caller = outer_method;
70 if (inlining_depth != 0) {
71 caller = GetResolvedMethod(outer_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070072 method_info,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010073 inline_info,
74 encoding,
75 inlining_depth - 1);
76 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010077
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000078 // Lookup the declaring class of the inlined method.
79 const DexFile* dex_file = caller->GetDexFile();
80 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
Mathieu Chartier20eb58e2017-02-01 15:50:54 -080081 ArtMethod* inlined_method = caller->GetDexCacheResolvedMethod(method_index, kRuntimePointerSize);
82 if (inlined_method != nullptr && !inlined_method->IsRuntimeMethod()) {
83 return inlined_method;
84 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000085 const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_);
86 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
87 Thread* self = Thread::Current();
88 mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader();
89 mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader);
90 if (klass == nullptr) {
91 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
92 << "the class " << descriptor << " was not found in the class loader of "
93 << caller->PrettyMethod() << ". "
94 << "This must be due to playing wrongly with class loaders";
95 }
96
97 // Lookup the method.
98 const char* method_name = dex_file->GetMethodName(method_id);
99 const Signature signature = dex_file->GetMethodSignature(method_id);
100
Mathieu Chartier20eb58e2017-02-01 15:50:54 -0800101 inlined_method = klass->FindDeclaredDirectMethod(method_name, signature, kRuntimePointerSize);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000102 if (inlined_method == nullptr) {
103 inlined_method = klass->FindDeclaredVirtualMethod(method_name, signature, kRuntimePointerSize);
104 if (inlined_method == nullptr) {
105 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
106 << "the class " << descriptor << " does not have "
107 << method_name << signature << " declared. "
108 << "This must be due to duplicate classes or playing wrongly with class loaders";
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100109 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100110 }
Mathieu Chartier20eb58e2017-02-01 15:50:54 -0800111 caller->SetDexCacheResolvedMethod(method_index, inlined_method, kRuntimePointerSize);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100112
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100113 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100114}
115
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000116ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
117 Thread* self,
118 bool* slow_path)
119 REQUIRES_SHARED(Locks::mutator_lock_)
120 REQUIRES(!Roles::uninterruptible_) {
121 if (UNLIKELY(!klass->IsInstantiable())) {
122 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700123 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000124 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700125 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000126 if (UNLIKELY(klass->IsClassClass())) {
127 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
128 klass->PrettyDescriptor().c_str());
129 *slow_path = true;
130 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700131 }
132 if (UNLIKELY(!klass->IsInitialized())) {
133 StackHandleScope<1> hs(self);
134 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
135 // EnsureInitialized (the class initializer) might cause a GC.
136 // may cause us to suspend meaning that another thread may try to
137 // change the allocator while we are stuck in the entrypoints of
138 // an old allocator. Also, the class initialization may fail. To
139 // handle these cases we mark the slow path boolean as true so
140 // that the caller knows to check the allocator type to see if it
141 // has changed and to null-check the return value in case the
142 // initialization fails.
143 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700144 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700145 DCHECK(self->IsExceptionPending());
146 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700147 } else {
148 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700149 }
150 return h_klass.Get();
151 }
152 return klass;
153}
154
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700155ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800156inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
157 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000158 bool* slow_path)
159 REQUIRES_SHARED(Locks::mutator_lock_)
160 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700161 if (UNLIKELY(!klass->IsInitialized())) {
162 StackHandleScope<1> hs(self);
163 Handle<mirror::Class> h_class(hs.NewHandle(klass));
164 // EnsureInitialized (the class initializer) might cause a GC.
165 // may cause us to suspend meaning that another thread may try to
166 // change the allocator while we are stuck in the entrypoints of
167 // an old allocator. Also, the class initialization may fail. To
168 // handle these cases we mark the slow path boolean as true so
169 // that the caller knows to check the allocator type to see if it
170 // has changed and to null-check the return value in case the
171 // initialization fails.
172 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700173 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700174 DCHECK(self->IsExceptionPending());
175 return nullptr; // Failure
176 }
177 return h_class.Get();
178 }
179 return klass;
180}
181
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000182// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
183// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
184template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700185ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000186inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800187 Thread* self,
188 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700189 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000190 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700191 if (UNLIKELY(slow_path)) {
192 if (klass == nullptr) {
193 return nullptr;
194 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800195 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
196 return klass->Alloc</*kInstrumented*/true>(
197 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700198 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700199 }
200 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700201 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700202}
203
204// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700205template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700206ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800207inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
208 Thread* self,
209 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700210 DCHECK(klass != nullptr);
211 bool slow_path = false;
212 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
213 if (UNLIKELY(slow_path)) {
214 if (klass == nullptr) {
215 return nullptr;
216 }
217 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000218 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800219 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
220 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700221 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700222 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000223 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700224 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700225}
226
227// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700228template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700229ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800230inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
231 Thread* self,
232 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700233 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000234 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700235 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700236}
237
238
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700239template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700240ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800241inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800242 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800244 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700245 if (UNLIKELY(component_count < 0)) {
246 ThrowNegativeArraySizeException(component_count);
247 *slow_path = true;
248 return nullptr; // Failure
249 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000250 mirror::Class* klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700251 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000252 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100253 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700254 *slow_path = true;
255 if (klass == nullptr) { // Error
256 DCHECK(Thread::Current()->IsExceptionPending());
257 return nullptr; // Failure
258 }
David Sehr709b0702016-10-13 09:12:37 -0700259 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700260 }
261 if (kAccessCheck) {
262 mirror::Class* referrer = method->GetDeclaringClass();
263 if (UNLIKELY(!referrer->CanAccess(klass))) {
264 ThrowIllegalAccessErrorClass(referrer, klass);
265 *slow_path = true;
266 return nullptr; // Failure
267 }
268 }
269 return klass;
270}
271
272// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
273// it cannot be resolved, throw an error. If it can, use it to create an array.
274// When verification/compiler hasn't been able to verify access, optionally perform an access
275// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700276template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700277ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800278inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800279 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700280 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800281 Thread* self,
282 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700283 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800284 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700285 &slow_path);
286 if (UNLIKELY(slow_path)) {
287 if (klass == nullptr) {
288 return nullptr;
289 }
290 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800291 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
292 return mirror::Array::Alloc</*kInstrumented*/true>(self,
293 klass,
294 component_count,
295 klass->GetComponentSizeShift(),
296 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700297 }
298 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700299 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700300}
301
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000302template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700303ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800304inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800305 int32_t component_count,
306 Thread* self,
307 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700308 DCHECK(klass != nullptr);
309 if (UNLIKELY(component_count < 0)) {
310 ThrowNegativeArraySizeException(component_count);
311 return nullptr; // Failure
312 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700313 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
314 // suspension.
315 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700316 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700317}
318
319template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800320inline ArtField* FindFieldFromCode(uint32_t field_idx,
321 ArtMethod* referrer,
322 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700323 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700324 bool is_primitive;
325 bool is_set;
326 bool is_static;
327 switch (type) {
328 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
329 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
330 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
331 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
332 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
333 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
334 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
335 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
336 default: is_primitive = true; is_set = true; is_static = true; break;
337 }
338 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800339
340 ArtField* resolved_field;
341 if (access_check) {
342 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
343 // qualifying type of a field and the resolved run-time qualifying type of a field differed
344 // in their static-ness.
345 //
346 // In particular, don't assume the dex instruction already correctly knows if the
347 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700348 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800349
350 StackHandleScope<2> hs(self);
351 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
352 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
353
354 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
355 field_idx,
356 h_dex_cache,
357 h_class_loader);
358 } else {
359 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
360 // be executing here if there was a static/non-static mismatch.
361 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
362 }
363
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700364 if (UNLIKELY(resolved_field == nullptr)) {
365 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
366 return nullptr; // Failure.
367 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700368 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700369 if (access_check) {
370 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
371 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
372 return nullptr;
373 }
374 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700375 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
376 resolved_field,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700377 field_idx))) {
378 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
379 return nullptr; // Failure.
380 }
381 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
382 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
383 return nullptr; // Failure.
384 } else {
385 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
386 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000387 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700388 "Attempted read of %zd-bit %s on field '%s'",
389 expected_size * (32 / sizeof(int32_t)),
390 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700391 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700392 return nullptr; // Failure.
393 }
394 }
395 }
396 if (!is_static) {
397 // instance fields must be being accessed on an initialized class
398 return resolved_field;
399 } else {
400 // If the class is initialized we're done.
401 if (LIKELY(fields_class->IsInitialized())) {
402 return resolved_field;
403 } else {
404 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700405 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700406 // Otherwise let's ensure the class is initialized before resolving the field.
407 return resolved_field;
408 }
409 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
410 return nullptr; // Failure.
411 }
412 }
413}
414
415// Explicit template declarations of FindFieldFromCode for all field access types.
416#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700417template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700418ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700419 ArtMethod* referrer, \
420 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700421
422#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
423 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
424 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
425
426EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
427EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
428EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
429EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
430EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
431EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
432EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
433EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
434
435#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
436#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
437
438template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700439inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700440 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700441 ArtMethod* referrer,
442 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700443 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700444 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700445 if (resolved_method == nullptr) {
446 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700447 ObjPtr<mirror::Object> null_this = nullptr;
448 HandleWrapperObjPtr<mirror::Object> h_this(
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700449 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800450 constexpr ClassLinker::ResolveMode resolve_mode =
451 access_check ? ClassLinker::kForceICCECheck
452 : ClassLinker::kNoICCECheckForCache;
453 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700454 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700455 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700456 if (UNLIKELY(resolved_method == nullptr)) {
457 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
458 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700459 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700460 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700461 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700462 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
463 resolved_method,
464 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700465 if (UNLIKELY(!can_access_resolved_method)) {
466 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
467 return nullptr; // Failure.
468 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100469 // Incompatible class change should have been handled in resolve method.
470 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
471 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
472 referrer);
473 return nullptr; // Failure.
474 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700475 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700476 // Next, null pointer check.
477 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
478 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
479 resolved_method->IsConstructor())) {
480 // Hack for String init:
481 //
482 // We assume that the input of String.<init> in verified code is always
483 // an unitialized reference. If it is a null constant, it must have been
484 // optimized out by the compiler. Do not throw NullPointerException.
485 } else {
486 // Maintain interpreter-like semantics where NullPointerException is thrown
487 // after potential NoSuchMethodError from class linker.
488 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
489 return nullptr; // Failure.
490 }
491 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700492 switch (type) {
493 case kStatic:
494 case kDirect:
495 return resolved_method;
496 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700497 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700498 uint16_t vtable_index = resolved_method->GetMethodIndex();
499 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700500 (!klass->HasVTable() ||
501 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700502 // Behavior to agree with that of the verifier.
503 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
504 resolved_method->GetName(), resolved_method->GetSignature());
505 return nullptr; // Failure.
506 }
David Sehr709b0702016-10-13 09:12:37 -0700507 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700508 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700509 }
510 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700511 // TODO This lookup is quite slow.
512 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
513 // that will actually not be what we want in some cases where there are miranda methods or
514 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
515 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700516 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700517 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700518 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800519 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800520 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700521 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
522 if (UNLIKELY(method_reference_class == nullptr)) {
523 // Bad type idx.
524 CHECK(self->IsExceptionPending());
525 return nullptr;
526 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700527 // It is not an interface. If the referring class is in the class hierarchy of the
528 // referenced class in the bytecode, we use its super class. Otherwise, we throw
529 // a NoSuchMethodError.
530 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700531 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
532 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700533 }
Alex Light705ad492015-09-21 11:36:30 -0700534 uint16_t vtable_index = resolved_method->GetMethodIndex();
535 if (access_check) {
536 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700537 if (super_class == nullptr ||
538 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700539 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
540 // Behavior to agree with that of the verifier.
541 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
542 resolved_method->GetName(), resolved_method->GetSignature());
543 return nullptr; // Failure.
544 }
545 }
546 DCHECK(super_class != nullptr);
547 DCHECK(super_class->HasVTable());
548 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
549 } else {
550 // It is an interface.
551 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700552 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700553 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
554 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700555 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700556 referrer);
557 return nullptr; // Failure.
558 }
559 }
560 // TODO We can do better than this for a (compiled) fastpath.
561 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
562 resolved_method, class_linker->GetImagePointerSize());
563 // Throw an NSME if nullptr;
564 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700565 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
566 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700567 }
Alex Light705ad492015-09-21 11:36:30 -0700568 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700569 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700570 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700571 }
572 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700573 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700574 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000575 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
576 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000577 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700578 if (kIsDebugBuild) {
579 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700580 ArtMethod* method = klass->FindVirtualMethodForInterface(
581 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700582 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
583 << imt_method->PrettyMethod() << " / "
584 << ArtMethod::PrettyMethod(method) << " / "
585 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700586 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700587 return imt_method;
588 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700589 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
590 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700591 if (UNLIKELY(interface_method == nullptr)) {
592 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700593 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700594 return nullptr; // Failure.
595 }
596 return interface_method;
597 }
598 }
599 default:
600 LOG(FATAL) << "Unknown invoke type " << type;
601 return nullptr; // Failure.
602 }
603}
604
605// Explicit template declarations of FindMethodFromCode for all invoke types.
606#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700607 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700608 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700609 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700610 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700611 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700612#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
613 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
614 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
615
616EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
617EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
618EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
619EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
620EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
621
622#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
623#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
624
625// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700626inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
627 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700628 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700629 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800630 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700631 if (UNLIKELY(resolved_field == nullptr)) {
632 return nullptr;
633 }
634 // Check for incompatible class change.
635 bool is_primitive;
636 bool is_set;
637 bool is_static;
638 switch (type) {
639 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
640 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
641 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
642 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
643 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
644 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
645 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
646 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
647 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700648 LOG(FATAL) << "UNREACHABLE";
649 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700650 }
651 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
652 // Incompatible class change.
653 return nullptr;
654 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700655 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700656 if (is_static) {
657 // Check class is initialized else fail so that we can contend to initialize the class with
658 // other threads that may be racing to do this.
659 if (UNLIKELY(!fields_class->IsInitialized())) {
660 return nullptr;
661 }
662 }
663 mirror::Class* referring_class = referrer->GetDeclaringClass();
664 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700665 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700666 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
667 // Illegal access.
668 return nullptr;
669 }
670 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
671 resolved_field->FieldSize() != expected_size)) {
672 return nullptr;
673 }
674 return resolved_field;
675}
676
677// Fast path method resolution that can't throw exceptions.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700678inline ArtMethod* FindMethodFast(uint32_t method_idx,
679 ObjPtr<mirror::Object> this_object,
680 ArtMethod* referrer,
681 bool access_check,
682 InvokeType type) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700683 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700684 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
685 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700686 }
Alex Light705ad492015-09-21 11:36:30 -0700687 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700688 ArtMethod* resolved_method =
Alex Lightdba61482016-12-21 08:20:29 -0800689 referrer->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700690 if (UNLIKELY(resolved_method == nullptr)) {
691 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700692 }
693 if (access_check) {
694 // Check for incompatible class change errors and access.
695 bool icce = resolved_method->CheckIncompatibleClassChange(type);
696 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700697 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700698 }
699 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700700 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
701 !referring_class->CanAccessMember(methods_class,
702 resolved_method->GetAccessFlags()))) {
703 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700704 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700705 }
706 }
707 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700708 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
709 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700710 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700711 return resolved_method;
712 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700713 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000714 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
715 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
716 ObjPtr<mirror::Class> method_reference_class = ClassLinker::LookupResolvedType(
717 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700718 if (method_reference_class == nullptr) {
719 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000720 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700721 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700722 // It is not an interface. If the referring class is in the class hierarchy of the
723 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
724 // resolve the method.
725 if (!method_reference_class->IsAssignableFrom(referring_class)) {
726 return nullptr;
727 }
728 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700729 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
730 // The super class does not have the method.
731 return nullptr;
732 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700733 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700734 } else {
735 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700736 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000737 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700738 } else {
739 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700740 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700741 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700742 }
743}
744
Andreas Gampea5b09a62016-11-17 15:21:22 -0800745inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx,
746 ArtMethod* referrer,
747 Thread* self,
748 bool can_run_clinit,
749 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700750 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
751 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
752 if (UNLIKELY(klass == nullptr)) {
753 CHECK(self->IsExceptionPending());
754 return nullptr; // Failure - Indicate to caller to deliver exception
755 }
756 // Perform access check if necessary.
757 mirror::Class* referring_class = referrer->GetDeclaringClass();
758 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
759 ThrowIllegalAccessErrorClass(referring_class, klass);
760 return nullptr; // Failure - Indicate to caller to deliver exception
761 }
762 // If we're just implementing const-class, we shouldn't call <clinit>.
763 if (!can_run_clinit) {
764 return klass;
765 }
766 // If we are the <clinit> of this class, just return our storage.
767 //
768 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
769 // running.
770 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
771 return klass;
772 }
773 StackHandleScope<1> hs(self);
774 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700775 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700776 CHECK(self->IsExceptionPending());
777 return nullptr; // Failure - Indicate to caller to deliver exception
778 }
779 return h_class.Get();
780}
781
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800782static inline mirror::String* ResolveString(ClassLinker* class_linker,
783 dex::StringIndex string_idx,
784 ArtMethod* referrer)
785 REQUIRES_SHARED(Locks::mutator_lock_) {
786 Thread::PoisonObjectPointersIfDebug();
787 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
788 if (UNLIKELY(string == nullptr)) {
789 StackHandleScope<1> hs(Thread::Current());
790 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
791 const DexFile& dex_file = *dex_cache->GetDexFile();
792 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
793 }
794 return string.Ptr();
795}
796
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800797inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800798 Thread::PoisonObjectPointersIfDebug();
799 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
800 if (UNLIKELY(string == nullptr)) {
801 StackHandleScope<1> hs(Thread::Current());
802 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
803 const DexFile& dex_file = *dex_cache->GetDexFile();
804 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
805 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
806 }
807 return string.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700808}
809
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800810inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700811 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700812 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700813 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000814 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700815 self->ClearException();
816 }
817 // Decode locked object and unlock, before popping local references.
818 self->DecodeJObject(locked)->MonitorExit(self);
819 if (UNLIKELY(self->IsExceptionPending())) {
820 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
821 << saved_exception->Dump()
822 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000823 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700824 }
825 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700826 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000827 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700828 }
829}
830
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700831template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800832inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700833 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
834 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
835 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
836 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
837 if (LIKELY(f > kMinIntAsFloat)) {
838 if (LIKELY(f < kMaxIntAsFloat)) {
839 return static_cast<INT_TYPE>(f);
840 } else {
841 return kMaxInt;
842 }
843 } else {
844 return (f != f) ? 0 : kMinInt; // f != f implies NaN
845 }
846}
847
848} // namespace art
849
850#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_