blob: 404c5357bf0b5feea9bd33de5a59380ac8085c2b [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"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file.h"
David Sehr8c0961f2018-01-23 16:11:38 -080028#include "dex/invoke_type.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010029#include "entrypoints/quick/callee_save_frame.h"
30#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070031#include "imt_conflict_table.h"
32#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033#include "indirect_reference_table.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#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);
Vladimir Marko5122e6b2017-08-17 16:10:09 +010086 ArtMethod* inlined_method = dex_cache->GetResolvedMethod(method_index, kRuntimePointerSize);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010087 if (inlined_method != nullptr) {
88 DCHECK(!inlined_method->IsRuntimeMethod());
Mathieu Chartier20eb58e2017-02-01 15:50:54 -080089 return inlined_method;
90 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000091 const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_);
92 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
93 Thread* self = Thread::Current();
94 mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader();
95 mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader);
96 if (klass == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +010097 LOG(FATAL) << "Could not find an inlined method from an .oat file: the class " << descriptor
98 << " was not found in the class loader of " << caller->PrettyMethod() << ". "
99 << "This must be due to playing wrongly with class loaders";
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000100 }
101
Nicolas Geoffrayea179f42018-02-08 22:30:18 +0000102 inlined_method = class_linker->FindResolvedMethod(klass, dex_cache, class_loader, method_index);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000103 if (inlined_method == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +0100104 LOG(FATAL) << "Could not find an inlined method from an .oat file: the class " << descriptor
105 << " does not have " << dex_file->GetMethodName(method_id)
106 << dex_file->GetMethodSignature(method_id) << " declared. "
107 << "This must be due to duplicate classes or playing wrongly with class loaders";
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100108 }
109
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 Marko28e012a2017-12-07 11:22:59 +0000247 ObjPtr<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 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000266 return klass.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267}
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
Vladimir Markoe11dd502017-12-08 14:09:45 +0000351 resolved_field = class_linker->ResolveFieldJLS(field_idx,
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800352 h_dex_cache,
353 h_class_loader);
354 } else {
355 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
356 // be executing here if there was a static/non-static mismatch.
357 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
358 }
359
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700360 if (UNLIKELY(resolved_field == nullptr)) {
361 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
362 return nullptr; // Failure.
363 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700364 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700365 if (access_check) {
366 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
367 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
368 return nullptr;
369 }
370 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700371 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
372 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100373 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700374 field_idx))) {
375 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
376 return nullptr; // Failure.
377 }
378 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
379 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
380 return nullptr; // Failure.
381 } else {
382 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
383 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000384 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700385 "Attempted read of %zd-bit %s on field '%s'",
386 expected_size * (32 / sizeof(int32_t)),
387 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700388 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700389 return nullptr; // Failure.
390 }
391 }
392 }
393 if (!is_static) {
394 // instance fields must be being accessed on an initialized class
395 return resolved_field;
396 } else {
397 // If the class is initialized we're done.
398 if (LIKELY(fields_class->IsInitialized())) {
399 return resolved_field;
400 } else {
401 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700402 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700403 // Otherwise let's ensure the class is initialized before resolving the field.
404 return resolved_field;
405 }
406 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
407 return nullptr; // Failure.
408 }
409 }
410}
411
412// Explicit template declarations of FindFieldFromCode for all field access types.
413#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700414template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700415ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700416 ArtMethod* referrer, \
417 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700418
419#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
420 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
421 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
422
423EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
424EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
425EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
426EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
427EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
428EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
429EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
430EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
431
432#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
433#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
434
435template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700436inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700437 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700438 ArtMethod* referrer,
439 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700440 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100441 constexpr ClassLinker::ResolveMode resolve_mode =
442 access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE
443 : ClassLinker::ResolveMode::kNoChecks;
444 ArtMethod* resolved_method;
445 if (type == kStatic) {
446 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
447 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700448 StackHandleScope<1> hs(self);
Vladimir Markoba118822017-06-12 15:41:56 +0100449 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800450 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700451 }
452 if (UNLIKELY(resolved_method == nullptr)) {
453 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
454 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700455 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700456 // Next, null pointer check.
457 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
458 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
459 resolved_method->IsConstructor())) {
460 // Hack for String init:
461 //
462 // We assume that the input of String.<init> in verified code is always
463 // an unitialized reference. If it is a null constant, it must have been
464 // optimized out by the compiler. Do not throw NullPointerException.
465 } else {
466 // Maintain interpreter-like semantics where NullPointerException is thrown
467 // after potential NoSuchMethodError from class linker.
468 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
469 return nullptr; // Failure.
470 }
471 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700472 switch (type) {
473 case kStatic:
474 case kDirect:
475 return resolved_method;
476 case kVirtual: {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100477 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700478 uint16_t vtable_index = resolved_method->GetMethodIndex();
479 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700480 (!klass->HasVTable() ||
481 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700482 // Behavior to agree with that of the verifier.
483 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
484 resolved_method->GetName(), resolved_method->GetSignature());
485 return nullptr; // Failure.
486 }
David Sehr709b0702016-10-13 09:12:37 -0700487 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700488 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700489 }
490 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700491 // TODO This lookup is quite slow.
492 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
493 // that will actually not be what we want in some cases where there are miranda methods or
494 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
495 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700496 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700497 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700498 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800499 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800500 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000501 ObjPtr<mirror::Class> method_reference_class =
502 class_linker->ResolveType(method_type_idx, referrer);
Alex Light705ad492015-09-21 11:36:30 -0700503 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.
Vladimir Marko302f69c2017-07-25 15:27:15 +0100511 ObjPtr<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();
Vladimir Marko302f69c2017-07-25 15:27:15 +0100556 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
557 ArtMethod* imt_method = klass->GetImt(pointer_size)->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) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700560 ArtMethod* method = klass->FindVirtualMethodForInterface(
561 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700562 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
563 << imt_method->PrettyMethod() << " / "
564 << ArtMethod::PrettyMethod(method) << " / "
565 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700566 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700567 return imt_method;
568 } else {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100569 ArtMethod* interface_method = klass->FindVirtualMethodForInterface(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700570 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700571 if (UNLIKELY(interface_method == nullptr)) {
572 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700573 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700574 return nullptr; // Failure.
575 }
576 return interface_method;
577 }
578 }
579 default:
580 LOG(FATAL) << "Unknown invoke type " << type;
581 return nullptr; // Failure.
582 }
583}
584
585// Explicit template declarations of FindMethodFromCode for all invoke types.
586#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700587 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700588 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700589 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700590 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700591 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700592#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
593 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
594 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
595
596EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
597EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
598EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
599EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
600EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
601
602#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
603#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
604
605// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700606inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
607 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700608 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700609 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800610 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700611 if (UNLIKELY(resolved_field == nullptr)) {
612 return nullptr;
613 }
614 // Check for incompatible class change.
615 bool is_primitive;
616 bool is_set;
617 bool is_static;
618 switch (type) {
619 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
620 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
621 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
622 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
623 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
624 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
625 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
626 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
627 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700628 LOG(FATAL) << "UNREACHABLE";
629 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700630 }
631 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
632 // Incompatible class change.
633 return nullptr;
634 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700635 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700636 if (is_static) {
637 // Check class is initialized else fail so that we can contend to initialize the class with
638 // other threads that may be racing to do this.
639 if (UNLIKELY(!fields_class->IsInitialized())) {
640 return nullptr;
641 }
642 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100643 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700644 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700646 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
647 // Illegal access.
648 return nullptr;
649 }
650 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
651 resolved_field->FieldSize() != expected_size)) {
652 return nullptr;
653 }
654 return resolved_field;
655}
656
657// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100658template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700659inline ArtMethod* FindMethodFast(uint32_t method_idx,
660 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100661 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700662 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700663 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
664 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700665 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100666 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
667 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100668 constexpr ClassLinker::ResolveMode resolve_mode = access_check
669 ? ClassLinker::ResolveMode::kCheckICCEAndIAE
670 : ClassLinker::ResolveMode::kNoChecks;
671 ClassLinker* linker = Runtime::Current()->GetClassLinker();
672 ArtMethod* resolved_method = linker->GetResolvedMethod<type, resolve_mode>(method_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700673 if (UNLIKELY(resolved_method == nullptr)) {
674 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700675 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700676 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700677 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
678 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700679 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700680 return resolved_method;
681 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700682 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000683 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000684 ObjPtr<mirror::Class> method_reference_class = linker->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000685 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700686 if (method_reference_class == nullptr) {
687 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000688 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700689 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700690 // It is not an interface. If the referring class is in the class hierarchy of the
691 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
692 // resolve the method.
693 if (!method_reference_class->IsAssignableFrom(referring_class)) {
694 return nullptr;
695 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100696 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700697 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
698 // The super class does not have the method.
699 return nullptr;
700 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700701 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700702 } else {
703 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700704 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000705 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700706 } else {
707 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700708 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700709 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700710 }
711}
712
Vladimir Marko28e012a2017-12-07 11:22:59 +0000713inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
714 ArtMethod* referrer,
715 Thread* self,
716 bool can_run_clinit,
717 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700718 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +0000719 ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700720 if (UNLIKELY(klass == nullptr)) {
721 CHECK(self->IsExceptionPending());
722 return nullptr; // Failure - Indicate to caller to deliver exception
723 }
724 // Perform access check if necessary.
725 mirror::Class* referring_class = referrer->GetDeclaringClass();
726 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
727 ThrowIllegalAccessErrorClass(referring_class, klass);
728 return nullptr; // Failure - Indicate to caller to deliver exception
729 }
730 // If we're just implementing const-class, we shouldn't call <clinit>.
731 if (!can_run_clinit) {
732 return klass;
733 }
734 // If we are the <clinit> of this class, just return our storage.
735 //
736 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
737 // running.
738 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
739 return klass;
740 }
741 StackHandleScope<1> hs(self);
742 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700743 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700744 CHECK(self->IsExceptionPending());
745 return nullptr; // Failure - Indicate to caller to deliver exception
746 }
747 return h_class.Get();
748}
749
Vladimir Marko28e012a2017-12-07 11:22:59 +0000750static inline ObjPtr<mirror::String> ResolveString(ClassLinker* class_linker,
751 dex::StringIndex string_idx,
752 ArtMethod* referrer)
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800753 REQUIRES_SHARED(Locks::mutator_lock_) {
754 Thread::PoisonObjectPointersIfDebug();
755 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
756 if (UNLIKELY(string == nullptr)) {
757 StackHandleScope<1> hs(Thread::Current());
758 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000759 string = class_linker->ResolveString(string_idx, dex_cache);
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800760 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000761 return string;
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800762}
763
Vladimir Marko28e012a2017-12-07 11:22:59 +0000764inline ObjPtr<mirror::String> ResolveStringFromCode(ArtMethod* referrer,
765 dex::StringIndex string_idx) {
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800766 Thread::PoisonObjectPointersIfDebug();
767 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
768 if (UNLIKELY(string == nullptr)) {
769 StackHandleScope<1> hs(Thread::Current());
770 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800771 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000772 string = class_linker->ResolveString(string_idx, dex_cache);
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800773 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000774 return string;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700775}
776
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800777inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700778 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700779 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700780 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000781 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700782 self->ClearException();
783 }
784 // Decode locked object and unlock, before popping local references.
785 self->DecodeJObject(locked)->MonitorExit(self);
786 if (UNLIKELY(self->IsExceptionPending())) {
787 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
788 << saved_exception->Dump()
789 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000790 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700791 }
792 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700793 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000794 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700795 }
796}
797
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700798template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800799inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700800 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
801 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
802 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
803 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
804 if (LIKELY(f > kMinIntAsFloat)) {
805 if (LIKELY(f < kMaxIntAsFloat)) {
806 return static_cast<INT_TYPE>(f);
807 } else {
808 return kMaxInt;
809 }
810 } else {
811 return (f != f) ? 0 : kMinInt; // f != f implies NaN
812 }
813}
814
815} // namespace art
816
817#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_