blob: e03ca790f49f1338016f8289c271354a594af005 [file] [log] [blame]
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
19
20#include "entrypoint_utils.h"
21
Matthew Gharrity465ecc82016-07-19 21:32:52 +000022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "class_linker-inl.h"
25#include "common_throws.h"
26#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010027#include "entrypoints/quick/callee_save_frame.h"
28#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070029#include "imt_conflict_table.h"
30#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "indirect_reference_table.h"
32#include "invoke_type.h"
33#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "mirror/array.h"
35#include "mirror/class-inl.h"
36#include "mirror/object-inl.h"
37#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010038#include "nth_caller_visitor.h"
39#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010040#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070041#include "thread.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070042#include "well_known_classes.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070043
44namespace art {
45
Mathieu Chartiere401d142015-04-22 13:56:20 -070046inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070047 const MethodInfo& method_info,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010048 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000049 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010050 uint8_t inlining_depth)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010052 // This method is being used by artQuickResolutionTrampoline, before it sets up
53 // the passed parameters in a GC friendly way. Therefore we must never be
54 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070055 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010056
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000057 if (inline_info.EncodesArtMethodAtDepth(encoding, inlining_depth)) {
58 return inline_info.GetArtMethodAtDepth(encoding, inlining_depth);
59 }
60
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070061 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, method_info, inlining_depth);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000062 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
63 // "charAt" special case. It is the only non-leaf method we inline across dex files.
64 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
65 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010066 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070067 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010068
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000069 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010070 ArtMethod* caller = outer_method;
71 if (inlining_depth != 0) {
72 caller = GetResolvedMethod(outer_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070073 method_info,
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010074 inline_info,
75 encoding,
76 inlining_depth - 1);
77 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010078
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000079 // Lookup the declaring class of the inlined method.
80 const DexFile* dex_file = caller->GetDexFile();
81 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
Mathieu Chartier20eb58e2017-02-01 15:50:54 -080082 ArtMethod* inlined_method = caller->GetDexCacheResolvedMethod(method_index, kRuntimePointerSize);
83 if (inlined_method != nullptr && !inlined_method->IsRuntimeMethod()) {
84 return inlined_method;
85 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000086 const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_);
87 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
88 Thread* self = Thread::Current();
89 mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader();
90 mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader);
91 if (klass == nullptr) {
92 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
93 << "the class " << descriptor << " was not found in the class loader of "
94 << caller->PrettyMethod() << ". "
95 << "This must be due to playing wrongly with class loaders";
96 }
97
98 // Lookup the method.
99 const char* method_name = dex_file->GetMethodName(method_id);
100 const Signature signature = dex_file->GetMethodSignature(method_id);
101
Mathieu Chartier20eb58e2017-02-01 15:50:54 -0800102 inlined_method = klass->FindDeclaredDirectMethod(method_name, signature, kRuntimePointerSize);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000103 if (inlined_method == nullptr) {
104 inlined_method = klass->FindDeclaredVirtualMethod(method_name, signature, kRuntimePointerSize);
105 if (inlined_method == nullptr) {
106 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
107 << "the class " << descriptor << " does not have "
108 << method_name << signature << " declared. "
109 << "This must be due to duplicate classes or playing wrongly with class loaders";
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100110 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100111 }
Mathieu Chartier20eb58e2017-02-01 15:50:54 -0800112 caller->SetDexCacheResolvedMethod(method_index, inlined_method, kRuntimePointerSize);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100113
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100114 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100115}
116
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000117ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
118 Thread* self,
119 bool* slow_path)
120 REQUIRES_SHARED(Locks::mutator_lock_)
121 REQUIRES(!Roles::uninterruptible_) {
122 if (UNLIKELY(!klass->IsInstantiable())) {
123 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700124 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000125 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700126 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000127 if (UNLIKELY(klass->IsClassClass())) {
128 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
129 klass->PrettyDescriptor().c_str());
130 *slow_path = true;
131 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700132 }
133 if (UNLIKELY(!klass->IsInitialized())) {
134 StackHandleScope<1> hs(self);
135 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
136 // EnsureInitialized (the class initializer) might cause a GC.
137 // may cause us to suspend meaning that another thread may try to
138 // change the allocator while we are stuck in the entrypoints of
139 // an old allocator. Also, the class initialization may fail. To
140 // handle these cases we mark the slow path boolean as true so
141 // that the caller knows to check the allocator type to see if it
142 // has changed and to null-check the return value in case the
143 // initialization fails.
144 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700146 DCHECK(self->IsExceptionPending());
147 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700148 } else {
149 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700150 }
151 return h_klass.Get();
152 }
153 return klass;
154}
155
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700156ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800157inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
158 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000159 bool* slow_path)
160 REQUIRES_SHARED(Locks::mutator_lock_)
161 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700162 if (UNLIKELY(!klass->IsInitialized())) {
163 StackHandleScope<1> hs(self);
164 Handle<mirror::Class> h_class(hs.NewHandle(klass));
165 // EnsureInitialized (the class initializer) might cause a GC.
166 // may cause us to suspend meaning that another thread may try to
167 // change the allocator while we are stuck in the entrypoints of
168 // an old allocator. Also, the class initialization may fail. To
169 // handle these cases we mark the slow path boolean as true so
170 // that the caller knows to check the allocator type to see if it
171 // has changed and to null-check the return value in case the
172 // initialization fails.
173 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700174 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700175 DCHECK(self->IsExceptionPending());
176 return nullptr; // Failure
177 }
178 return h_class.Get();
179 }
180 return klass;
181}
182
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000183// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
184// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
185template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700186ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000187inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800188 Thread* self,
189 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700190 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000191 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700192 if (UNLIKELY(slow_path)) {
193 if (klass == nullptr) {
194 return nullptr;
195 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800196 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
197 return klass->Alloc</*kInstrumented*/true>(
198 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700199 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700200 }
201 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700202 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700203}
204
205// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700206template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700207ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800208inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
209 Thread* self,
210 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700211 DCHECK(klass != nullptr);
212 bool slow_path = false;
213 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
214 if (UNLIKELY(slow_path)) {
215 if (klass == nullptr) {
216 return nullptr;
217 }
218 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000219 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800220 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
221 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700222 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000224 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700225 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226}
227
228// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700229template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700230ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800231inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
232 Thread* self,
233 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000235 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700236 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237}
238
239
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700240template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700241ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800242inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800243 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800245 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700246 if (UNLIKELY(component_count < 0)) {
247 ThrowNegativeArraySizeException(component_count);
248 *slow_path = true;
249 return nullptr; // Failure
250 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000251 mirror::Class* klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000253 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100254 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700255 *slow_path = true;
256 if (klass == nullptr) { // Error
257 DCHECK(Thread::Current()->IsExceptionPending());
258 return nullptr; // Failure
259 }
David Sehr709b0702016-10-13 09:12:37 -0700260 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700261 }
262 if (kAccessCheck) {
263 mirror::Class* referrer = method->GetDeclaringClass();
264 if (UNLIKELY(!referrer->CanAccess(klass))) {
265 ThrowIllegalAccessErrorClass(referrer, klass);
266 *slow_path = true;
267 return nullptr; // Failure
268 }
269 }
270 return klass;
271}
272
273// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
274// it cannot be resolved, throw an error. If it can, use it to create an array.
275// When verification/compiler hasn't been able to verify access, optionally perform an access
276// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700278ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800279inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800280 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800282 Thread* self,
283 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700284 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800285 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700286 &slow_path);
287 if (UNLIKELY(slow_path)) {
288 if (klass == nullptr) {
289 return nullptr;
290 }
291 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800292 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
293 return mirror::Array::Alloc</*kInstrumented*/true>(self,
294 klass,
295 component_count,
296 klass->GetComponentSizeShift(),
297 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700298 }
299 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700300 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700301}
302
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000303template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700304ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800305inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800306 int32_t component_count,
307 Thread* self,
308 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700309 DCHECK(klass != nullptr);
310 if (UNLIKELY(component_count < 0)) {
311 ThrowNegativeArraySizeException(component_count);
312 return nullptr; // Failure
313 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700314 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
315 // suspension.
316 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700317 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700318}
319
320template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800321inline ArtField* FindFieldFromCode(uint32_t field_idx,
322 ArtMethod* referrer,
323 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700324 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700325 bool is_primitive;
326 bool is_set;
327 bool is_static;
328 switch (type) {
329 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
330 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
331 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
332 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
333 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
334 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
335 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
336 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
337 default: is_primitive = true; is_set = true; is_static = true; break;
338 }
339 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800340
341 ArtField* resolved_field;
342 if (access_check) {
343 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
344 // qualifying type of a field and the resolved run-time qualifying type of a field differed
345 // in their static-ness.
346 //
347 // In particular, don't assume the dex instruction already correctly knows if the
348 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700349 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800350
351 StackHandleScope<2> hs(self);
352 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
353 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
354
355 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
356 field_idx,
357 h_dex_cache,
358 h_class_loader);
359 } else {
360 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
361 // be executing here if there was a static/non-static mismatch.
362 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
363 }
364
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700365 if (UNLIKELY(resolved_field == nullptr)) {
366 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
367 return nullptr; // Failure.
368 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700369 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700370 if (access_check) {
371 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
372 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
373 return nullptr;
374 }
375 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700376 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
377 resolved_field,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700378 field_idx))) {
379 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
380 return nullptr; // Failure.
381 }
382 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
383 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
384 return nullptr; // Failure.
385 } else {
386 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
387 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000388 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700389 "Attempted read of %zd-bit %s on field '%s'",
390 expected_size * (32 / sizeof(int32_t)),
391 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700392 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700393 return nullptr; // Failure.
394 }
395 }
396 }
397 if (!is_static) {
398 // instance fields must be being accessed on an initialized class
399 return resolved_field;
400 } else {
401 // If the class is initialized we're done.
402 if (LIKELY(fields_class->IsInitialized())) {
403 return resolved_field;
404 } else {
405 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700406 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700407 // Otherwise let's ensure the class is initialized before resolving the field.
408 return resolved_field;
409 }
410 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
411 return nullptr; // Failure.
412 }
413 }
414}
415
416// Explicit template declarations of FindFieldFromCode for all field access types.
417#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700418template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700419ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420 ArtMethod* referrer, \
421 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700422
423#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
424 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
425 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
426
427EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
428EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
429EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
430EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
431EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
432EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
433EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
434EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
435
436#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
437#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
438
439template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700440inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700441 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700442 ArtMethod* referrer,
443 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700444 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700445 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700446 if (resolved_method == nullptr) {
447 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700448 ObjPtr<mirror::Object> null_this = nullptr;
449 HandleWrapperObjPtr<mirror::Object> h_this(
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700450 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800451 constexpr ClassLinker::ResolveMode resolve_mode =
452 access_check ? ClassLinker::kForceICCECheck
453 : ClassLinker::kNoICCECheckForCache;
454 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700455 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700456 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700457 if (UNLIKELY(resolved_method == nullptr)) {
458 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
459 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700460 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700461 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700462 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700463 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
464 resolved_method,
465 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700466 if (UNLIKELY(!can_access_resolved_method)) {
467 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
468 return nullptr; // Failure.
469 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100470 // Incompatible class change should have been handled in resolve method.
471 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
472 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
473 referrer);
474 return nullptr; // Failure.
475 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700476 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700477 // Next, null pointer check.
478 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
479 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
480 resolved_method->IsConstructor())) {
481 // Hack for String init:
482 //
483 // We assume that the input of String.<init> in verified code is always
484 // an unitialized reference. If it is a null constant, it must have been
485 // optimized out by the compiler. Do not throw NullPointerException.
486 } else {
487 // Maintain interpreter-like semantics where NullPointerException is thrown
488 // after potential NoSuchMethodError from class linker.
489 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
490 return nullptr; // Failure.
491 }
492 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700493 switch (type) {
494 case kStatic:
495 case kDirect:
496 return resolved_method;
497 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700498 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700499 uint16_t vtable_index = resolved_method->GetMethodIndex();
500 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700501 (!klass->HasVTable() ||
502 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700503 // Behavior to agree with that of the verifier.
504 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
505 resolved_method->GetName(), resolved_method->GetSignature());
506 return nullptr; // Failure.
507 }
David Sehr709b0702016-10-13 09:12:37 -0700508 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700509 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700510 }
511 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700512 // TODO This lookup is quite slow.
513 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
514 // that will actually not be what we want in some cases where there are miranda methods or
515 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
516 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700517 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700518 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700519 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800520 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800521 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700522 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
523 if (UNLIKELY(method_reference_class == nullptr)) {
524 // Bad type idx.
525 CHECK(self->IsExceptionPending());
526 return nullptr;
527 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700528 // It is not an interface. If the referring class is in the class hierarchy of the
529 // referenced class in the bytecode, we use its super class. Otherwise, we throw
530 // a NoSuchMethodError.
531 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700532 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
533 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700534 }
Alex Light705ad492015-09-21 11:36:30 -0700535 uint16_t vtable_index = resolved_method->GetMethodIndex();
536 if (access_check) {
537 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700538 if (super_class == nullptr ||
539 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700540 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
541 // Behavior to agree with that of the verifier.
542 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
543 resolved_method->GetName(), resolved_method->GetSignature());
544 return nullptr; // Failure.
545 }
546 }
547 DCHECK(super_class != nullptr);
548 DCHECK(super_class->HasVTable());
549 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
550 } else {
551 // It is an interface.
552 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700553 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700554 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
555 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700556 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700557 referrer);
558 return nullptr; // Failure.
559 }
560 }
561 // TODO We can do better than this for a (compiled) fastpath.
562 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
563 resolved_method, class_linker->GetImagePointerSize());
564 // Throw an NSME if nullptr;
565 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700566 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
567 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700568 }
Alex Light705ad492015-09-21 11:36:30 -0700569 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700570 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700571 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700572 }
573 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700574 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700575 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000576 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
577 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000578 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700579 if (kIsDebugBuild) {
580 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700581 ArtMethod* method = klass->FindVirtualMethodForInterface(
582 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700583 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
584 << imt_method->PrettyMethod() << " / "
585 << ArtMethod::PrettyMethod(method) << " / "
586 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700587 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700588 return imt_method;
589 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
591 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700592 if (UNLIKELY(interface_method == nullptr)) {
593 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700594 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700595 return nullptr; // Failure.
596 }
597 return interface_method;
598 }
599 }
600 default:
601 LOG(FATAL) << "Unknown invoke type " << type;
602 return nullptr; // Failure.
603 }
604}
605
606// Explicit template declarations of FindMethodFromCode for all invoke types.
607#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700608 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700609 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700610 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700611 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700612 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700613#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
614 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
615 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
616
617EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
618EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
619EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
620EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
621EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
622
623#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
624#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
625
626// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700627inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
628 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700629 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700630 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800631 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700632 if (UNLIKELY(resolved_field == nullptr)) {
633 return nullptr;
634 }
635 // Check for incompatible class change.
636 bool is_primitive;
637 bool is_set;
638 bool is_static;
639 switch (type) {
640 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
641 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
642 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
643 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
644 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
645 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
646 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
647 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
648 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700649 LOG(FATAL) << "UNREACHABLE";
650 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700651 }
652 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
653 // Incompatible class change.
654 return nullptr;
655 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700656 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700657 if (is_static) {
658 // Check class is initialized else fail so that we can contend to initialize the class with
659 // other threads that may be racing to do this.
660 if (UNLIKELY(!fields_class->IsInitialized())) {
661 return nullptr;
662 }
663 }
664 mirror::Class* referring_class = referrer->GetDeclaringClass();
665 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700666 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700667 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
668 // Illegal access.
669 return nullptr;
670 }
671 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
672 resolved_field->FieldSize() != expected_size)) {
673 return nullptr;
674 }
675 return resolved_field;
676}
677
678// Fast path method resolution that can't throw exceptions.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700679inline ArtMethod* FindMethodFast(uint32_t method_idx,
680 ObjPtr<mirror::Object> this_object,
681 ArtMethod* referrer,
682 bool access_check,
683 InvokeType type) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700684 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700685 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
686 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700687 }
Alex Light705ad492015-09-21 11:36:30 -0700688 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700689 ArtMethod* resolved_method =
Alex Lightdba61482016-12-21 08:20:29 -0800690 referrer->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700691 if (UNLIKELY(resolved_method == nullptr)) {
692 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700693 }
694 if (access_check) {
695 // Check for incompatible class change errors and access.
696 bool icce = resolved_method->CheckIncompatibleClassChange(type);
697 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700698 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700699 }
700 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700701 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
702 !referring_class->CanAccessMember(methods_class,
703 resolved_method->GetAccessFlags()))) {
704 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700705 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700706 }
707 }
708 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700709 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
710 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700711 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700712 return resolved_method;
713 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700714 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000715 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
716 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
717 ObjPtr<mirror::Class> method_reference_class = ClassLinker::LookupResolvedType(
718 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700719 if (method_reference_class == nullptr) {
720 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000721 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700722 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700723 // It is not an interface. If the referring class is in the class hierarchy of the
724 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
725 // resolve the method.
726 if (!method_reference_class->IsAssignableFrom(referring_class)) {
727 return nullptr;
728 }
729 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700730 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
731 // The super class does not have the method.
732 return nullptr;
733 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700734 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700735 } else {
736 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700737 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000738 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700739 } else {
740 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700741 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700742 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700743 }
744}
745
Andreas Gampea5b09a62016-11-17 15:21:22 -0800746inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx,
747 ArtMethod* referrer,
748 Thread* self,
749 bool can_run_clinit,
750 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700751 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
752 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
753 if (UNLIKELY(klass == nullptr)) {
754 CHECK(self->IsExceptionPending());
755 return nullptr; // Failure - Indicate to caller to deliver exception
756 }
757 // Perform access check if necessary.
758 mirror::Class* referring_class = referrer->GetDeclaringClass();
759 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
760 ThrowIllegalAccessErrorClass(referring_class, klass);
761 return nullptr; // Failure - Indicate to caller to deliver exception
762 }
763 // If we're just implementing const-class, we shouldn't call <clinit>.
764 if (!can_run_clinit) {
765 return klass;
766 }
767 // If we are the <clinit> of this class, just return our storage.
768 //
769 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
770 // running.
771 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
772 return klass;
773 }
774 StackHandleScope<1> hs(self);
775 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700776 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700777 CHECK(self->IsExceptionPending());
778 return nullptr; // Failure - Indicate to caller to deliver exception
779 }
780 return h_class.Get();
781}
782
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800783static inline mirror::String* ResolveString(ClassLinker* class_linker,
784 dex::StringIndex string_idx,
785 ArtMethod* referrer)
786 REQUIRES_SHARED(Locks::mutator_lock_) {
787 Thread::PoisonObjectPointersIfDebug();
788 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
789 if (UNLIKELY(string == nullptr)) {
790 StackHandleScope<1> hs(Thread::Current());
791 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
792 const DexFile& dex_file = *dex_cache->GetDexFile();
793 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
794 }
795 return string.Ptr();
796}
797
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800798inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800799 Thread::PoisonObjectPointersIfDebug();
800 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
801 if (UNLIKELY(string == nullptr)) {
802 StackHandleScope<1> hs(Thread::Current());
803 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
804 const DexFile& dex_file = *dex_cache->GetDexFile();
805 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
806 string = class_linker->ResolveString(dex_file, string_idx, dex_cache);
807 }
808 return string.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700809}
810
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800811inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700812 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700813 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700814 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000815 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700816 self->ClearException();
817 }
818 // Decode locked object and unlock, before popping local references.
819 self->DecodeJObject(locked)->MonitorExit(self);
820 if (UNLIKELY(self->IsExceptionPending())) {
821 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
822 << saved_exception->Dump()
823 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000824 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700825 }
826 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700827 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000828 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700829 }
830}
831
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700832template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800833inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700834 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
835 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
836 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
837 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
838 if (LIKELY(f > kMinIntAsFloat)) {
839 if (LIKELY(f < kMaxIntAsFloat)) {
840 return static_cast<INT_TYPE>(f);
841 } else {
842 return kMaxInt;
843 }
844 } else {
845 return (f != f) ? 0 : kMinInt; // f != f implies NaN
846 }
847}
848
849} // namespace art
850
851#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_