blob: 828148a8b1e2b0b4593d80ace649a72f3e29146a [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);
87 if (inlined_method != nullptr && !inlined_method->IsRuntimeMethod()) {
88 return inlined_method;
89 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000090 const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_);
91 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
92 Thread* self = Thread::Current();
93 mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader();
94 mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader);
95 if (klass == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +010096 LOG(FATAL) << "Could not find an inlined method from an .oat file: the class " << descriptor
97 << " was not found in the class loader of " << caller->PrettyMethod() << ". "
98 << "This must be due to playing wrongly with class loaders";
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000099 }
100
Vladimir Markoba118822017-06-12 15:41:56 +0100101 inlined_method = klass->FindClassMethod(dex_cache, method_index, kRuntimePointerSize);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000102 if (inlined_method == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +0100103 LOG(FATAL) << "Could not find an inlined method from an .oat file: the class " << descriptor
104 << " does not have " << dex_file->GetMethodName(method_id)
105 << dex_file->GetMethodSignature(method_id) << " declared. "
106 << "This must be due to duplicate classes or playing wrongly with class loaders";
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100107 }
Mathieu Chartier20eb58e2017-02-01 15:50:54 -0800108 caller->SetDexCacheResolvedMethod(method_index, inlined_method, kRuntimePointerSize);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100109
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100110 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100111}
112
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000113ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
114 Thread* self,
115 bool* slow_path)
116 REQUIRES_SHARED(Locks::mutator_lock_)
117 REQUIRES(!Roles::uninterruptible_) {
118 if (UNLIKELY(!klass->IsInstantiable())) {
119 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700120 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000121 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700122 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000123 if (UNLIKELY(klass->IsClassClass())) {
124 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
125 klass->PrettyDescriptor().c_str());
126 *slow_path = true;
127 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700128 }
129 if (UNLIKELY(!klass->IsInitialized())) {
130 StackHandleScope<1> hs(self);
131 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
132 // EnsureInitialized (the class initializer) might cause a GC.
133 // may cause us to suspend meaning that another thread may try to
134 // change the allocator while we are stuck in the entrypoints of
135 // an old allocator. Also, the class initialization may fail. To
136 // handle these cases we mark the slow path boolean as true so
137 // that the caller knows to check the allocator type to see if it
138 // has changed and to null-check the return value in case the
139 // initialization fails.
140 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700141 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700142 DCHECK(self->IsExceptionPending());
143 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700144 } else {
145 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700146 }
147 return h_klass.Get();
148 }
149 return klass;
150}
151
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700152ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800153inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
154 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000155 bool* slow_path)
156 REQUIRES_SHARED(Locks::mutator_lock_)
157 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700158 if (UNLIKELY(!klass->IsInitialized())) {
159 StackHandleScope<1> hs(self);
160 Handle<mirror::Class> h_class(hs.NewHandle(klass));
161 // EnsureInitialized (the class initializer) might cause a GC.
162 // may cause us to suspend meaning that another thread may try to
163 // change the allocator while we are stuck in the entrypoints of
164 // an old allocator. Also, the class initialization may fail. To
165 // handle these cases we mark the slow path boolean as true so
166 // that the caller knows to check the allocator type to see if it
167 // has changed and to null-check the return value in case the
168 // initialization fails.
169 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700170 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700171 DCHECK(self->IsExceptionPending());
172 return nullptr; // Failure
173 }
174 return h_class.Get();
175 }
176 return klass;
177}
178
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000179// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
180// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
181template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700182ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000183inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800184 Thread* self,
185 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700186 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000187 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700188 if (UNLIKELY(slow_path)) {
189 if (klass == nullptr) {
190 return nullptr;
191 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800192 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
193 return klass->Alloc</*kInstrumented*/true>(
194 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700195 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700196 }
197 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700198 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700199}
200
201// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700202template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700203ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800204inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
205 Thread* self,
206 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700207 DCHECK(klass != nullptr);
208 bool slow_path = false;
209 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
210 if (UNLIKELY(slow_path)) {
211 if (klass == nullptr) {
212 return nullptr;
213 }
214 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000215 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800216 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
217 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700218 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700219 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000220 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700221 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700222}
223
224// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700225template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700226ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800227inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
228 Thread* self,
229 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700230 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000231 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700232 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700233}
234
235
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700236template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700237ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800238inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800239 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700240 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800241 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 if (UNLIKELY(component_count < 0)) {
243 ThrowNegativeArraySizeException(component_count);
244 *slow_path = true;
245 return nullptr; // Failure
246 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000247 mirror::Class* klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700248 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000249 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100250 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700251 *slow_path = true;
252 if (klass == nullptr) { // Error
253 DCHECK(Thread::Current()->IsExceptionPending());
254 return nullptr; // Failure
255 }
David Sehr709b0702016-10-13 09:12:37 -0700256 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700257 }
258 if (kAccessCheck) {
259 mirror::Class* referrer = method->GetDeclaringClass();
260 if (UNLIKELY(!referrer->CanAccess(klass))) {
261 ThrowIllegalAccessErrorClass(referrer, klass);
262 *slow_path = true;
263 return nullptr; // Failure
264 }
265 }
266 return klass;
267}
268
269// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
270// it cannot be resolved, throw an error. If it can, use it to create an array.
271// When verification/compiler hasn't been able to verify access, optionally perform an access
272// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700273template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700274ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800275inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800276 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800278 Thread* self,
279 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700280 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800281 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700282 &slow_path);
283 if (UNLIKELY(slow_path)) {
284 if (klass == nullptr) {
285 return nullptr;
286 }
287 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800288 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
289 return mirror::Array::Alloc</*kInstrumented*/true>(self,
290 klass,
291 component_count,
292 klass->GetComponentSizeShift(),
293 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700294 }
295 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700296 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700297}
298
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000299template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700300ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800301inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800302 int32_t component_count,
303 Thread* self,
304 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700305 DCHECK(klass != nullptr);
306 if (UNLIKELY(component_count < 0)) {
307 ThrowNegativeArraySizeException(component_count);
308 return nullptr; // Failure
309 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700310 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
311 // suspension.
312 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700313 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700314}
315
316template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800317inline ArtField* FindFieldFromCode(uint32_t field_idx,
318 ArtMethod* referrer,
319 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700320 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700321 bool is_primitive;
322 bool is_set;
323 bool is_static;
324 switch (type) {
325 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
326 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
327 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
328 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
329 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
330 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
331 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
332 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
333 default: is_primitive = true; is_set = true; is_static = true; break;
334 }
335 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800336
337 ArtField* resolved_field;
338 if (access_check) {
339 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
340 // qualifying type of a field and the resolved run-time qualifying type of a field differed
341 // in their static-ness.
342 //
343 // In particular, don't assume the dex instruction already correctly knows if the
344 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700345 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800346
347 StackHandleScope<2> hs(self);
348 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
349 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
350
351 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
352 field_idx,
353 h_dex_cache,
354 h_class_loader);
355 } else {
356 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
357 // be executing here if there was a static/non-static mismatch.
358 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
359 }
360
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700361 if (UNLIKELY(resolved_field == nullptr)) {
362 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
363 return nullptr; // Failure.
364 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700365 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700366 if (access_check) {
367 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
368 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
369 return nullptr;
370 }
371 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700372 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
373 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100374 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700375 field_idx))) {
376 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
377 return nullptr; // Failure.
378 }
379 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
380 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
381 return nullptr; // Failure.
382 } else {
383 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
384 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000385 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700386 "Attempted read of %zd-bit %s on field '%s'",
387 expected_size * (32 / sizeof(int32_t)),
388 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700389 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700390 return nullptr; // Failure.
391 }
392 }
393 }
394 if (!is_static) {
395 // instance fields must be being accessed on an initialized class
396 return resolved_field;
397 } else {
398 // If the class is initialized we're done.
399 if (LIKELY(fields_class->IsInitialized())) {
400 return resolved_field;
401 } else {
402 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700403 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700404 // Otherwise let's ensure the class is initialized before resolving the field.
405 return resolved_field;
406 }
407 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
408 return nullptr; // Failure.
409 }
410 }
411}
412
413// Explicit template declarations of FindFieldFromCode for all field access types.
414#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700415template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700416ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417 ArtMethod* referrer, \
418 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700419
420#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
421 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
422 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
423
424EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
425EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
426EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
427EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
428EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
429EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
430EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
431EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
432
433#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
434#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
435
436template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700437inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700438 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700439 ArtMethod* referrer,
440 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700441 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100442 constexpr ClassLinker::ResolveMode resolve_mode =
443 access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE
444 : ClassLinker::ResolveMode::kNoChecks;
445 ArtMethod* resolved_method;
446 if (type == kStatic) {
447 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
448 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700449 StackHandleScope<1> hs(self);
Vladimir Markoba118822017-06-12 15:41:56 +0100450 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800451 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700452 }
453 if (UNLIKELY(resolved_method == nullptr)) {
454 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
455 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700456 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700457 // Next, null pointer check.
458 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
459 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
460 resolved_method->IsConstructor())) {
461 // Hack for String init:
462 //
463 // We assume that the input of String.<init> in verified code is always
464 // an unitialized reference. If it is a null constant, it must have been
465 // optimized out by the compiler. Do not throw NullPointerException.
466 } else {
467 // Maintain interpreter-like semantics where NullPointerException is thrown
468 // after potential NoSuchMethodError from class linker.
469 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
470 return nullptr; // Failure.
471 }
472 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700473 switch (type) {
474 case kStatic:
475 case kDirect:
476 return resolved_method;
477 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700478 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700479 uint16_t vtable_index = resolved_method->GetMethodIndex();
480 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700481 (!klass->HasVTable() ||
482 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700483 // Behavior to agree with that of the verifier.
484 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
485 resolved_method->GetName(), resolved_method->GetSignature());
486 return nullptr; // Failure.
487 }
David Sehr709b0702016-10-13 09:12:37 -0700488 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700489 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700490 }
491 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700492 // TODO This lookup is quite slow.
493 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
494 // that will actually not be what we want in some cases where there are miranda methods or
495 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
496 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700497 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700498 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700499 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800500 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800501 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700502 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
503 if (UNLIKELY(method_reference_class == nullptr)) {
504 // Bad type idx.
505 CHECK(self->IsExceptionPending());
506 return nullptr;
507 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700508 // It is not an interface. If the referring class is in the class hierarchy of the
509 // referenced class in the bytecode, we use its super class. Otherwise, we throw
510 // a NoSuchMethodError.
511 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700512 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
513 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700514 }
Alex Light705ad492015-09-21 11:36:30 -0700515 uint16_t vtable_index = resolved_method->GetMethodIndex();
516 if (access_check) {
517 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700518 if (super_class == nullptr ||
519 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700520 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
521 // Behavior to agree with that of the verifier.
522 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
523 resolved_method->GetName(), resolved_method->GetSignature());
524 return nullptr; // Failure.
525 }
526 }
527 DCHECK(super_class != nullptr);
528 DCHECK(super_class->HasVTable());
529 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
530 } else {
531 // It is an interface.
532 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700533 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700534 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
535 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700536 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700537 referrer);
538 return nullptr; // Failure.
539 }
540 }
541 // TODO We can do better than this for a (compiled) fastpath.
542 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
543 resolved_method, class_linker->GetImagePointerSize());
544 // Throw an NSME if nullptr;
545 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700546 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
547 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700548 }
Alex Light705ad492015-09-21 11:36:30 -0700549 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700550 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700551 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700552 }
553 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700554 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700555 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000556 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
557 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000558 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700559 if (kIsDebugBuild) {
560 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700561 ArtMethod* method = klass->FindVirtualMethodForInterface(
562 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700563 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
564 << imt_method->PrettyMethod() << " / "
565 << ArtMethod::PrettyMethod(method) << " / "
566 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700567 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700568 return imt_method;
569 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700570 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
571 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700572 if (UNLIKELY(interface_method == nullptr)) {
573 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700574 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700575 return nullptr; // Failure.
576 }
577 return interface_method;
578 }
579 }
580 default:
581 LOG(FATAL) << "Unknown invoke type " << type;
582 return nullptr; // Failure.
583 }
584}
585
586// Explicit template declarations of FindMethodFromCode for all invoke types.
587#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700588 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700589 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700590 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700591 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700593#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
594 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
595 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
596
597EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
598EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
599EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
600EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
601EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
602
603#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
604#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
605
606// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700607inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
608 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700609 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700610 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800611 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700612 if (UNLIKELY(resolved_field == nullptr)) {
613 return nullptr;
614 }
615 // Check for incompatible class change.
616 bool is_primitive;
617 bool is_set;
618 bool is_static;
619 switch (type) {
620 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
621 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
622 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
623 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
624 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
625 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
626 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
627 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
628 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700629 LOG(FATAL) << "UNREACHABLE";
630 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700631 }
632 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
633 // Incompatible class change.
634 return nullptr;
635 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700636 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700637 if (is_static) {
638 // Check class is initialized else fail so that we can contend to initialize the class with
639 // other threads that may be racing to do this.
640 if (UNLIKELY(!fields_class->IsInitialized())) {
641 return nullptr;
642 }
643 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100644 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700645 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700646 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700647 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
648 // Illegal access.
649 return nullptr;
650 }
651 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
652 resolved_field->FieldSize() != expected_size)) {
653 return nullptr;
654 }
655 return resolved_field;
656}
657
658// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100659template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700660inline ArtMethod* FindMethodFast(uint32_t method_idx,
661 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100662 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700663 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700664 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
665 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700666 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100667 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
668 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100669 constexpr ClassLinker::ResolveMode resolve_mode = access_check
670 ? ClassLinker::ResolveMode::kCheckICCEAndIAE
671 : ClassLinker::ResolveMode::kNoChecks;
672 ClassLinker* linker = Runtime::Current()->GetClassLinker();
673 ArtMethod* resolved_method = linker->GetResolvedMethod<type, resolve_mode>(method_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700674 if (UNLIKELY(resolved_method == nullptr)) {
675 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700676 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700677 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700678 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
679 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700680 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700681 return resolved_method;
682 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700683 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000684 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
685 ObjPtr<mirror::Class> method_reference_class = ClassLinker::LookupResolvedType(
686 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700687 if (method_reference_class == nullptr) {
688 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000689 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700690 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700691 // It is not an interface. If the referring class is in the class hierarchy of the
692 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
693 // resolve the method.
694 if (!method_reference_class->IsAssignableFrom(referring_class)) {
695 return nullptr;
696 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100697 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700698 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
699 // The super class does not have the method.
700 return nullptr;
701 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700702 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700703 } else {
704 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700705 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000706 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700707 } else {
708 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700709 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700710 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700711 }
712}
713
Andreas Gampea5b09a62016-11-17 15:21:22 -0800714inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx,
715 ArtMethod* referrer,
716 Thread* self,
717 bool can_run_clinit,
718 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700719 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
720 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
721 if (UNLIKELY(klass == nullptr)) {
722 CHECK(self->IsExceptionPending());
723 return nullptr; // Failure - Indicate to caller to deliver exception
724 }
725 // Perform access check if necessary.
726 mirror::Class* referring_class = referrer->GetDeclaringClass();
727 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
728 ThrowIllegalAccessErrorClass(referring_class, klass);
729 return nullptr; // Failure - Indicate to caller to deliver exception
730 }
731 // If we're just implementing const-class, we shouldn't call <clinit>.
732 if (!can_run_clinit) {
733 return klass;
734 }
735 // If we are the <clinit> of this class, just return our storage.
736 //
737 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
738 // running.
739 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
740 return klass;
741 }
742 StackHandleScope<1> hs(self);
743 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700744 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700745 CHECK(self->IsExceptionPending());
746 return nullptr; // Failure - Indicate to caller to deliver exception
747 }
748 return h_class.Get();
749}
750
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800751static inline mirror::String* ResolveString(ClassLinker* class_linker,
752 dex::StringIndex string_idx,
753 ArtMethod* referrer)
754 REQUIRES_SHARED(Locks::mutator_lock_) {
755 Thread::PoisonObjectPointersIfDebug();
756 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
757 if (UNLIKELY(string == nullptr)) {
758 StackHandleScope<1> hs(Thread::Current());
759 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
760 const DexFile& dex_file = *dex_cache->GetDexFile();
761 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
762 }
763 return string.Ptr();
764}
765
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800766inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800767 Thread::PoisonObjectPointersIfDebug();
768 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
769 if (UNLIKELY(string == nullptr)) {
770 StackHandleScope<1> hs(Thread::Current());
771 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
772 const DexFile& dex_file = *dex_cache->GetDexFile();
773 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
774 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
775 }
776 return string.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700777}
778
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800779inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700780 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700781 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700782 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000783 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700784 self->ClearException();
785 }
786 // Decode locked object and unlock, before popping local references.
787 self->DecodeJObject(locked)->MonitorExit(self);
788 if (UNLIKELY(self->IsExceptionPending())) {
789 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
790 << saved_exception->Dump()
791 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000792 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700793 }
794 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700795 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000796 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700797 }
798}
799
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700800template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800801inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700802 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
803 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
804 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
805 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
806 if (LIKELY(f > kMinIntAsFloat)) {
807 if (LIKELY(f < kMaxIntAsFloat)) {
808 return static_cast<INT_TYPE>(f);
809 } else {
810 return kMaxInt;
811 }
812 } else {
813 return (f != f) ? 0 : kMinInt; // f != f implies NaN
814 }
815}
816
817} // namespace art
818
819#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_