blob: a6c5d6c7aa79676e4938faaa1d5bc7e69e1984c1 [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_) {
Vladimir Markoba118822017-06-12 15:41:56 +010053 DCHECK(!outer_method->IsObsolete());
54
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010055 // This method is being used by artQuickResolutionTrampoline, before it sets up
56 // the passed parameters in a GC friendly way. Therefore we must never be
57 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070058 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010059
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000060 if (inline_info.EncodesArtMethodAtDepth(encoding, inlining_depth)) {
61 return inline_info.GetArtMethodAtDepth(encoding, inlining_depth);
62 }
63
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070064 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, method_info, inlining_depth);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000065 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
66 // "charAt" special case. It is the only non-leaf method we inline across dex files.
67 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
68 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010069 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070070 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010071
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000072 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010073 ArtMethod* caller = outer_method;
74 if (inlining_depth != 0) {
75 caller = GetResolvedMethod(outer_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070076 method_info,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010077 inline_info,
78 encoding,
79 inlining_depth - 1);
80 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010081
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000082 // Lookup the declaring class of the inlined method.
Vladimir Markoba118822017-06-12 15:41:56 +010083 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
84 const DexFile* dex_file = dex_cache->GetDexFile();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000085 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
Mathieu Chartier20eb58e2017-02-01 15:50:54 -080086 ArtMethod* inlined_method = caller->GetDexCacheResolvedMethod(method_index, kRuntimePointerSize);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010087 if (inlined_method != nullptr) {
88 DCHECK(!inlined_method->IsRuntimeMethod());
Mathieu Chartier20eb58e2017-02-01 15:50:54 -080089 return inlined_method;
90 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000091 const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_);
92 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
93 Thread* self = Thread::Current();
94 mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader();
95 mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader);
96 if (klass == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +010097 LOG(FATAL) << "Could not find an inlined method from an .oat file: the class " << descriptor
98 << " was not found in the class loader of " << caller->PrettyMethod() << ". "
99 << "This must be due to playing wrongly with class loaders";
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000100 }
101
Vladimir Markoba118822017-06-12 15:41:56 +0100102 inlined_method = klass->FindClassMethod(dex_cache, method_index, kRuntimePointerSize);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000103 if (inlined_method == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +0100104 LOG(FATAL) << "Could not find an inlined method from an .oat file: the class " << descriptor
105 << " does not have " << dex_file->GetMethodName(method_id)
106 << dex_file->GetMethodSignature(method_id) << " declared. "
107 << "This must be due to duplicate classes or playing wrongly with class loaders";
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100108 }
Mathieu Chartier20eb58e2017-02-01 15:50:54 -0800109 caller->SetDexCacheResolvedMethod(method_index, inlined_method, kRuntimePointerSize);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100110
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100111 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100112}
113
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000114ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
115 Thread* self,
116 bool* slow_path)
117 REQUIRES_SHARED(Locks::mutator_lock_)
118 REQUIRES(!Roles::uninterruptible_) {
119 if (UNLIKELY(!klass->IsInstantiable())) {
120 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700121 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000122 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700123 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000124 if (UNLIKELY(klass->IsClassClass())) {
125 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
126 klass->PrettyDescriptor().c_str());
127 *slow_path = true;
128 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700129 }
130 if (UNLIKELY(!klass->IsInitialized())) {
131 StackHandleScope<1> hs(self);
132 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
133 // EnsureInitialized (the class initializer) might cause a GC.
134 // may cause us to suspend meaning that another thread may try to
135 // change the allocator while we are stuck in the entrypoints of
136 // an old allocator. Also, the class initialization may fail. To
137 // handle these cases we mark the slow path boolean as true so
138 // that the caller knows to check the allocator type to see if it
139 // has changed and to null-check the return value in case the
140 // initialization fails.
141 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700143 DCHECK(self->IsExceptionPending());
144 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700145 } else {
146 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700147 }
148 return h_klass.Get();
149 }
150 return klass;
151}
152
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700153ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800154inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
155 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000156 bool* slow_path)
157 REQUIRES_SHARED(Locks::mutator_lock_)
158 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700159 if (UNLIKELY(!klass->IsInitialized())) {
160 StackHandleScope<1> hs(self);
161 Handle<mirror::Class> h_class(hs.NewHandle(klass));
162 // EnsureInitialized (the class initializer) might cause a GC.
163 // may cause us to suspend meaning that another thread may try to
164 // change the allocator while we are stuck in the entrypoints of
165 // an old allocator. Also, the class initialization may fail. To
166 // handle these cases we mark the slow path boolean as true so
167 // that the caller knows to check the allocator type to see if it
168 // has changed and to null-check the return value in case the
169 // initialization fails.
170 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700171 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700172 DCHECK(self->IsExceptionPending());
173 return nullptr; // Failure
174 }
175 return h_class.Get();
176 }
177 return klass;
178}
179
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000180// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
181// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
182template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700183ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000184inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800185 Thread* self,
186 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700187 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000188 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700189 if (UNLIKELY(slow_path)) {
190 if (klass == nullptr) {
191 return nullptr;
192 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800193 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
194 return klass->Alloc</*kInstrumented*/true>(
195 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700196 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700197 }
198 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700199 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700200}
201
202// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700203template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700204ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800205inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
206 Thread* self,
207 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700208 DCHECK(klass != nullptr);
209 bool slow_path = false;
210 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
211 if (UNLIKELY(slow_path)) {
212 if (klass == nullptr) {
213 return nullptr;
214 }
215 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000216 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800217 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
218 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700219 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700220 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000221 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700222 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223}
224
225// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700227ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800228inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
229 Thread* self,
230 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700231 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000232 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700233 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234}
235
236
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700238ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800239inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800240 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800242 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700243 if (UNLIKELY(component_count < 0)) {
244 ThrowNegativeArraySizeException(component_count);
245 *slow_path = true;
246 return nullptr; // Failure
247 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000248 mirror::Class* klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700249 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000250 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100251 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252 *slow_path = true;
253 if (klass == nullptr) { // Error
254 DCHECK(Thread::Current()->IsExceptionPending());
255 return nullptr; // Failure
256 }
David Sehr709b0702016-10-13 09:12:37 -0700257 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258 }
259 if (kAccessCheck) {
260 mirror::Class* referrer = method->GetDeclaringClass();
261 if (UNLIKELY(!referrer->CanAccess(klass))) {
262 ThrowIllegalAccessErrorClass(referrer, klass);
263 *slow_path = true;
264 return nullptr; // Failure
265 }
266 }
267 return klass;
268}
269
270// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
271// it cannot be resolved, throw an error. If it can, use it to create an array.
272// When verification/compiler hasn't been able to verify access, optionally perform an access
273// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700274template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700275ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800276inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800277 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800279 Thread* self,
280 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700281 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800282 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700283 &slow_path);
284 if (UNLIKELY(slow_path)) {
285 if (klass == nullptr) {
286 return nullptr;
287 }
288 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800289 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
290 return mirror::Array::Alloc</*kInstrumented*/true>(self,
291 klass,
292 component_count,
293 klass->GetComponentSizeShift(),
294 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700295 }
296 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700297 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700298}
299
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000300template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700301ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800302inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800303 int32_t component_count,
304 Thread* self,
305 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700306 DCHECK(klass != nullptr);
307 if (UNLIKELY(component_count < 0)) {
308 ThrowNegativeArraySizeException(component_count);
309 return nullptr; // Failure
310 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700311 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
312 // suspension.
313 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700314 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700315}
316
317template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800318inline ArtField* FindFieldFromCode(uint32_t field_idx,
319 ArtMethod* referrer,
320 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700321 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700322 bool is_primitive;
323 bool is_set;
324 bool is_static;
325 switch (type) {
326 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
327 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
328 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
329 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
330 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
331 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
332 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
333 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
334 default: is_primitive = true; is_set = true; is_static = true; break;
335 }
336 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800337
338 ArtField* resolved_field;
339 if (access_check) {
340 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
341 // qualifying type of a field and the resolved run-time qualifying type of a field differed
342 // in their static-ness.
343 //
344 // In particular, don't assume the dex instruction already correctly knows if the
345 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700346 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800347
348 StackHandleScope<2> hs(self);
349 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
350 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
351
352 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
353 field_idx,
354 h_dex_cache,
355 h_class_loader);
356 } else {
357 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
358 // be executing here if there was a static/non-static mismatch.
359 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
360 }
361
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700362 if (UNLIKELY(resolved_field == nullptr)) {
363 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
364 return nullptr; // Failure.
365 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700366 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700367 if (access_check) {
368 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
369 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
370 return nullptr;
371 }
372 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700373 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
374 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100375 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700376 field_idx))) {
377 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
378 return nullptr; // Failure.
379 }
380 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
381 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
382 return nullptr; // Failure.
383 } else {
384 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
385 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000386 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700387 "Attempted read of %zd-bit %s on field '%s'",
388 expected_size * (32 / sizeof(int32_t)),
389 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700390 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700391 return nullptr; // Failure.
392 }
393 }
394 }
395 if (!is_static) {
396 // instance fields must be being accessed on an initialized class
397 return resolved_field;
398 } else {
399 // If the class is initialized we're done.
400 if (LIKELY(fields_class->IsInitialized())) {
401 return resolved_field;
402 } else {
403 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700404 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700405 // Otherwise let's ensure the class is initialized before resolving the field.
406 return resolved_field;
407 }
408 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
409 return nullptr; // Failure.
410 }
411 }
412}
413
414// Explicit template declarations of FindFieldFromCode for all field access types.
415#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700416template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700417ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700418 ArtMethod* referrer, \
419 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700420
421#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
422 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
423 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
424
425EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
426EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
427EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
428EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
429EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
430EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
431EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
432EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
433
434#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
435#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
436
437template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700438inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700439 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700440 ArtMethod* referrer,
441 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700442 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100443 constexpr ClassLinker::ResolveMode resolve_mode =
444 access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE
445 : ClassLinker::ResolveMode::kNoChecks;
446 ArtMethod* resolved_method;
447 if (type == kStatic) {
448 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
449 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700450 StackHandleScope<1> hs(self);
Vladimir Markoba118822017-06-12 15:41:56 +0100451 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800452 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700453 }
454 if (UNLIKELY(resolved_method == nullptr)) {
455 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
456 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700457 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700458 // Next, null pointer check.
459 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
460 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
461 resolved_method->IsConstructor())) {
462 // Hack for String init:
463 //
464 // We assume that the input of String.<init> in verified code is always
465 // an unitialized reference. If it is a null constant, it must have been
466 // optimized out by the compiler. Do not throw NullPointerException.
467 } else {
468 // Maintain interpreter-like semantics where NullPointerException is thrown
469 // after potential NoSuchMethodError from class linker.
470 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
471 return nullptr; // Failure.
472 }
473 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700474 switch (type) {
475 case kStatic:
476 case kDirect:
477 return resolved_method;
478 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700479 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700480 uint16_t vtable_index = resolved_method->GetMethodIndex();
481 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700482 (!klass->HasVTable() ||
483 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700484 // Behavior to agree with that of the verifier.
485 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
486 resolved_method->GetName(), resolved_method->GetSignature());
487 return nullptr; // Failure.
488 }
David Sehr709b0702016-10-13 09:12:37 -0700489 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700491 }
492 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700493 // TODO This lookup is quite slow.
494 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
495 // that will actually not be what we want in some cases where there are miranda methods or
496 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
497 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700498 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700499 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700500 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800501 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800502 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700503 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
504 if (UNLIKELY(method_reference_class == nullptr)) {
505 // Bad type idx.
506 CHECK(self->IsExceptionPending());
507 return nullptr;
508 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700509 // It is not an interface. If the referring class is in the class hierarchy of the
510 // referenced class in the bytecode, we use its super class. Otherwise, we throw
511 // a NoSuchMethodError.
512 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700513 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
514 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700515 }
Alex Light705ad492015-09-21 11:36:30 -0700516 uint16_t vtable_index = resolved_method->GetMethodIndex();
517 if (access_check) {
518 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700519 if (super_class == nullptr ||
520 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700521 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
522 // Behavior to agree with that of the verifier.
523 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
524 resolved_method->GetName(), resolved_method->GetSignature());
525 return nullptr; // Failure.
526 }
527 }
528 DCHECK(super_class != nullptr);
529 DCHECK(super_class->HasVTable());
530 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
531 } else {
532 // It is an interface.
533 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700534 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700535 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
536 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700537 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700538 referrer);
539 return nullptr; // Failure.
540 }
541 }
542 // TODO We can do better than this for a (compiled) fastpath.
543 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
544 resolved_method, class_linker->GetImagePointerSize());
545 // Throw an NSME if nullptr;
546 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700547 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
548 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700549 }
Alex Light705ad492015-09-21 11:36:30 -0700550 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700551 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700552 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700553 }
554 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700555 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700556 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000557 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
558 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000559 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700560 if (kIsDebugBuild) {
561 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700562 ArtMethod* method = klass->FindVirtualMethodForInterface(
563 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700564 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
565 << imt_method->PrettyMethod() << " / "
566 << ArtMethod::PrettyMethod(method) << " / "
567 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700568 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700569 return imt_method;
570 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700571 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
572 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700573 if (UNLIKELY(interface_method == nullptr)) {
574 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700575 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700576 return nullptr; // Failure.
577 }
578 return interface_method;
579 }
580 }
581 default:
582 LOG(FATAL) << "Unknown invoke type " << type;
583 return nullptr; // Failure.
584 }
585}
586
587// Explicit template declarations of FindMethodFromCode for all invoke types.
588#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700589 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700591 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700592 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700593 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700594#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
595 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
596 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
597
598EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
599EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
600EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
601EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
602EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
603
604#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
605#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
606
607// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700608inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
609 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700610 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700611 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800612 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700613 if (UNLIKELY(resolved_field == nullptr)) {
614 return nullptr;
615 }
616 // Check for incompatible class change.
617 bool is_primitive;
618 bool is_set;
619 bool is_static;
620 switch (type) {
621 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
622 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
623 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
624 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
625 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
626 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
627 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
628 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
629 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700630 LOG(FATAL) << "UNREACHABLE";
631 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700632 }
633 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
634 // Incompatible class change.
635 return nullptr;
636 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700637 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700638 if (is_static) {
639 // Check class is initialized else fail so that we can contend to initialize the class with
640 // other threads that may be racing to do this.
641 if (UNLIKELY(!fields_class->IsInitialized())) {
642 return nullptr;
643 }
644 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100645 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700646 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700648 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
649 // Illegal access.
650 return nullptr;
651 }
652 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
653 resolved_field->FieldSize() != expected_size)) {
654 return nullptr;
655 }
656 return resolved_field;
657}
658
659// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100660template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700661inline ArtMethod* FindMethodFast(uint32_t method_idx,
662 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100663 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700664 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700665 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
666 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700667 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100668 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
669 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100670 constexpr ClassLinker::ResolveMode resolve_mode = access_check
671 ? ClassLinker::ResolveMode::kCheckICCEAndIAE
672 : ClassLinker::ResolveMode::kNoChecks;
673 ClassLinker* linker = Runtime::Current()->GetClassLinker();
674 ArtMethod* resolved_method = linker->GetResolvedMethod<type, resolve_mode>(method_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700675 if (UNLIKELY(resolved_method == nullptr)) {
676 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700677 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700679 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
680 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700681 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700682 return resolved_method;
683 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700684 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000685 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
686 ObjPtr<mirror::Class> method_reference_class = ClassLinker::LookupResolvedType(
687 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700688 if (method_reference_class == nullptr) {
689 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000690 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700691 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700692 // It is not an interface. If the referring class is in the class hierarchy of the
693 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
694 // resolve the method.
695 if (!method_reference_class->IsAssignableFrom(referring_class)) {
696 return nullptr;
697 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100698 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700699 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
700 // The super class does not have the method.
701 return nullptr;
702 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700703 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700704 } else {
705 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700706 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000707 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700708 } else {
709 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700710 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700711 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700712 }
713}
714
Andreas Gampea5b09a62016-11-17 15:21:22 -0800715inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx,
716 ArtMethod* referrer,
717 Thread* self,
718 bool can_run_clinit,
719 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700720 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
721 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
722 if (UNLIKELY(klass == nullptr)) {
723 CHECK(self->IsExceptionPending());
724 return nullptr; // Failure - Indicate to caller to deliver exception
725 }
726 // Perform access check if necessary.
727 mirror::Class* referring_class = referrer->GetDeclaringClass();
728 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
729 ThrowIllegalAccessErrorClass(referring_class, klass);
730 return nullptr; // Failure - Indicate to caller to deliver exception
731 }
732 // If we're just implementing const-class, we shouldn't call <clinit>.
733 if (!can_run_clinit) {
734 return klass;
735 }
736 // If we are the <clinit> of this class, just return our storage.
737 //
738 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
739 // running.
740 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
741 return klass;
742 }
743 StackHandleScope<1> hs(self);
744 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700745 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700746 CHECK(self->IsExceptionPending());
747 return nullptr; // Failure - Indicate to caller to deliver exception
748 }
749 return h_class.Get();
750}
751
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800752static inline mirror::String* ResolveString(ClassLinker* class_linker,
753 dex::StringIndex string_idx,
754 ArtMethod* referrer)
755 REQUIRES_SHARED(Locks::mutator_lock_) {
756 Thread::PoisonObjectPointersIfDebug();
757 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
758 if (UNLIKELY(string == nullptr)) {
759 StackHandleScope<1> hs(Thread::Current());
760 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
761 const DexFile& dex_file = *dex_cache->GetDexFile();
762 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
763 }
764 return string.Ptr();
765}
766
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800767inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800768 Thread::PoisonObjectPointersIfDebug();
769 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
770 if (UNLIKELY(string == nullptr)) {
771 StackHandleScope<1> hs(Thread::Current());
772 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
773 const DexFile& dex_file = *dex_cache->GetDexFile();
774 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
775 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
776 }
777 return string.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700778}
779
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800780inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700781 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700782 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700783 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000784 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700785 self->ClearException();
786 }
787 // Decode locked object and unlock, before popping local references.
788 self->DecodeJObject(locked)->MonitorExit(self);
789 if (UNLIKELY(self->IsExceptionPending())) {
790 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
791 << saved_exception->Dump()
792 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000793 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700794 }
795 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700796 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000797 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700798 }
799}
800
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700801template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800802inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700803 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
804 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
805 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
806 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
807 if (LIKELY(f > kMinIntAsFloat)) {
808 if (LIKELY(f < kMaxIntAsFloat)) {
809 return static_cast<INT_TYPE>(f);
810 } else {
811 return kMaxInt;
812 }
813 } else {
814 return (f != f) ? 0 : kMinInt; // f != f implies NaN
815 }
816}
817
818} // namespace art
819
820#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_