blob: f6b1c732309d4b4fde693c488b01268d7238a8e9 [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"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010034#include "jni/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,
50 uint8_t inlining_depth)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoba118822017-06-12 15:41:56 +010052 DCHECK(!outer_method->IsObsolete());
53
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010054 // This method is being used by artQuickResolutionTrampoline, before it sets up
55 // the passed parameters in a GC friendly way. Therefore we must never be
56 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070057 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010058
David Srbecky052f8ca2018-04-26 15:42:54 +010059 if (inline_info.EncodesArtMethodAtDepth(inlining_depth)) {
60 return inline_info.GetArtMethodAtDepth(inlining_depth);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000061 }
62
David Srbecky052f8ca2018-04-26 15:42:54 +010063 uint32_t method_index = inline_info.GetMethodIndexAtDepth(method_info, inlining_depth);
64 if (inline_info.GetDexPcAtDepth(inlining_depth) == static_cast<uint32_t>(-1)) {
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000065 // "charAt" special case. It is the only non-leaf method we inline across dex files.
66 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
67 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010068 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070069 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010070
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000071 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000072 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko63a9f3e2018-04-26 09:18:10 +010073 ArtMethod* method = outer_method;
74 for (uint32_t depth = 0, end = inlining_depth + 1u; depth != end; ++depth) {
David Srbecky052f8ca2018-04-26 15:42:54 +010075 DCHECK(!inline_info.EncodesArtMethodAtDepth(depth));
76 DCHECK_NE(inline_info.GetDexPcAtDepth(depth), static_cast<uint32_t>(-1));
77 method_index = inline_info.GetMethodIndexAtDepth(method_info, depth);
Vladimir Marko63a9f3e2018-04-26 09:18:10 +010078 ArtMethod* inlined_method = class_linker->LookupResolvedMethod(method_index,
79 method->GetDexCache(),
80 method->GetClassLoader());
81 if (UNLIKELY(inlined_method == nullptr)) {
82 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
83 << method->GetDexFile()->PrettyMethod(method_index) << " . "
84 << "This must be due to duplicate classes or playing wrongly with class loaders";
85 UNREACHABLE();
86 }
87 DCHECK(!inlined_method->IsRuntimeMethod());
88 if (UNLIKELY(inlined_method->GetDexFile() != method->GetDexFile())) {
89 // TODO: We could permit inlining within a multi-dex oat file and the boot image,
90 // even going back from boot image methods to the same oat file. However, this is
91 // not currently implemented in the compiler. Therefore crossing dex file boundary
92 // indicates that the inlined definition is not the same as the one used at runtime.
93 LOG(FATAL) << "Inlined method resolution crossed dex file boundary: from "
94 << method->PrettyMethod()
95 << " in " << method->GetDexFile()->GetLocation() << "/"
96 << static_cast<const void*>(method->GetDexFile())
97 << " to " << inlined_method->PrettyMethod()
98 << " in " << inlined_method->GetDexFile()->GetLocation() << "/"
99 << static_cast<const void*>(inlined_method->GetDexFile()) << ". "
100 << "This must be due to duplicate classes or playing wrongly with class loaders";
101 UNREACHABLE();
102 }
103 method = inlined_method;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000104 }
105
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100106 return method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100107}
108
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000109ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
110 Thread* self,
111 bool* slow_path)
112 REQUIRES_SHARED(Locks::mutator_lock_)
113 REQUIRES(!Roles::uninterruptible_) {
114 if (UNLIKELY(!klass->IsInstantiable())) {
115 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700116 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000117 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000119 if (UNLIKELY(klass->IsClassClass())) {
120 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
121 klass->PrettyDescriptor().c_str());
122 *slow_path = true;
123 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700124 }
125 if (UNLIKELY(!klass->IsInitialized())) {
126 StackHandleScope<1> hs(self);
127 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
128 // EnsureInitialized (the class initializer) might cause a GC.
129 // may cause us to suspend meaning that another thread may try to
130 // change the allocator while we are stuck in the entrypoints of
131 // an old allocator. Also, the class initialization may fail. To
132 // handle these cases we mark the slow path boolean as true so
133 // that the caller knows to check the allocator type to see if it
134 // has changed and to null-check the return value in case the
135 // initialization fails.
136 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700137 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700138 DCHECK(self->IsExceptionPending());
139 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700140 } else {
141 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700142 }
143 return h_klass.Get();
144 }
145 return klass;
146}
147
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700148ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800149inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
150 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000151 bool* slow_path)
152 REQUIRES_SHARED(Locks::mutator_lock_)
153 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700154 if (UNLIKELY(!klass->IsInitialized())) {
155 StackHandleScope<1> hs(self);
156 Handle<mirror::Class> h_class(hs.NewHandle(klass));
157 // EnsureInitialized (the class initializer) might cause a GC.
158 // may cause us to suspend meaning that another thread may try to
159 // change the allocator while we are stuck in the entrypoints of
160 // an old allocator. Also, the class initialization may fail. To
161 // handle these cases we mark the slow path boolean as true so
162 // that the caller knows to check the allocator type to see if it
163 // has changed and to null-check the return value in case the
164 // initialization fails.
165 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700166 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700167 DCHECK(self->IsExceptionPending());
168 return nullptr; // Failure
169 }
170 return h_class.Get();
171 }
172 return klass;
173}
174
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000175// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
176// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
177template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700178ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000179inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800180 Thread* self,
181 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700182 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000183 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700184 if (UNLIKELY(slow_path)) {
185 if (klass == nullptr) {
186 return nullptr;
187 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800188 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
189 return klass->Alloc</*kInstrumented*/true>(
190 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700191 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700192 }
193 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700194 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700195}
196
197// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700198template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700199ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800200inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
201 Thread* self,
202 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700203 DCHECK(klass != nullptr);
204 bool slow_path = false;
205 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
206 if (UNLIKELY(slow_path)) {
207 if (klass == nullptr) {
208 return nullptr;
209 }
210 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000211 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800212 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
213 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700214 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700215 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000216 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700217 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700218}
219
220// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700222ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800223inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
224 Thread* self,
225 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000227 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700228 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700229}
230
231
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700232template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700233ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800234inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800235 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800237 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700238 if (UNLIKELY(component_count < 0)) {
239 ThrowNegativeArraySizeException(component_count);
240 *slow_path = true;
241 return nullptr; // Failure
242 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000243 ObjPtr<mirror::Class> klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000245 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100246 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 *slow_path = true;
248 if (klass == nullptr) { // Error
249 DCHECK(Thread::Current()->IsExceptionPending());
250 return nullptr; // Failure
251 }
David Sehr709b0702016-10-13 09:12:37 -0700252 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 }
254 if (kAccessCheck) {
255 mirror::Class* referrer = method->GetDeclaringClass();
256 if (UNLIKELY(!referrer->CanAccess(klass))) {
257 ThrowIllegalAccessErrorClass(referrer, klass);
258 *slow_path = true;
259 return nullptr; // Failure
260 }
261 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000262 return klass.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700263}
264
265// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
266// it cannot be resolved, throw an error. If it can, use it to create an array.
267// When verification/compiler hasn't been able to verify access, optionally perform an access
268// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700269template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700270ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800271inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800272 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800274 Thread* self,
275 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700276 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800277 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700278 &slow_path);
279 if (UNLIKELY(slow_path)) {
280 if (klass == nullptr) {
281 return nullptr;
282 }
283 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800284 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
285 return mirror::Array::Alloc</*kInstrumented*/true>(self,
286 klass,
287 component_count,
288 klass->GetComponentSizeShift(),
289 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700290 }
291 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700292 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700293}
294
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000295template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700296ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800297inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800298 int32_t component_count,
299 Thread* self,
300 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700301 DCHECK(klass != nullptr);
302 if (UNLIKELY(component_count < 0)) {
303 ThrowNegativeArraySizeException(component_count);
304 return nullptr; // Failure
305 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700306 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
307 // suspension.
308 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700309 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700310}
311
312template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800313inline ArtField* FindFieldFromCode(uint32_t field_idx,
314 ArtMethod* referrer,
315 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700316 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700317 bool is_primitive;
318 bool is_set;
319 bool is_static;
320 switch (type) {
321 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
322 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
323 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
324 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
325 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
326 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
327 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
328 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
329 default: is_primitive = true; is_set = true; is_static = true; break;
330 }
331 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800332
333 ArtField* resolved_field;
334 if (access_check) {
335 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
336 // qualifying type of a field and the resolved run-time qualifying type of a field differed
337 // in their static-ness.
338 //
339 // In particular, don't assume the dex instruction already correctly knows if the
340 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700341 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800342
343 StackHandleScope<2> hs(self);
344 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
345 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
346
Vladimir Markoe11dd502017-12-08 14:09:45 +0000347 resolved_field = class_linker->ResolveFieldJLS(field_idx,
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800348 h_dex_cache,
349 h_class_loader);
350 } else {
351 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
352 // be executing here if there was a static/non-static mismatch.
353 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
354 }
355
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700356 if (UNLIKELY(resolved_field == nullptr)) {
357 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
358 return nullptr; // Failure.
359 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700360 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700361 if (access_check) {
362 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
363 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
364 return nullptr;
365 }
366 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700367 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
368 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100369 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700370 field_idx))) {
371 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
372 return nullptr; // Failure.
373 }
374 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
375 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
376 return nullptr; // Failure.
377 } else {
378 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
379 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000380 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700381 "Attempted read of %zd-bit %s on field '%s'",
382 expected_size * (32 / sizeof(int32_t)),
383 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700384 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700385 return nullptr; // Failure.
386 }
387 }
388 }
389 if (!is_static) {
390 // instance fields must be being accessed on an initialized class
391 return resolved_field;
392 } else {
393 // If the class is initialized we're done.
394 if (LIKELY(fields_class->IsInitialized())) {
395 return resolved_field;
396 } else {
397 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700398 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700399 // Otherwise let's ensure the class is initialized before resolving the field.
400 return resolved_field;
401 }
402 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
403 return nullptr; // Failure.
404 }
405 }
406}
407
408// Explicit template declarations of FindFieldFromCode for all field access types.
409#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700410template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700411ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 ArtMethod* referrer, \
413 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700414
415#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
416 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
417 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
418
419EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
420EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
421EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
422EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
423EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
424EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
425EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
426EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
427
428#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
429#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
430
431template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700432inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700433 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700434 ArtMethod* referrer,
435 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700436 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100437 constexpr ClassLinker::ResolveMode resolve_mode =
438 access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE
439 : ClassLinker::ResolveMode::kNoChecks;
440 ArtMethod* resolved_method;
441 if (type == kStatic) {
442 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
443 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700444 StackHandleScope<1> hs(self);
Vladimir Markoba118822017-06-12 15:41:56 +0100445 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800446 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700447 }
448 if (UNLIKELY(resolved_method == nullptr)) {
449 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
450 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700451 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700452 // Next, null pointer check.
453 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
454 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
455 resolved_method->IsConstructor())) {
456 // Hack for String init:
457 //
458 // We assume that the input of String.<init> in verified code is always
459 // an unitialized reference. If it is a null constant, it must have been
460 // optimized out by the compiler. Do not throw NullPointerException.
461 } else {
462 // Maintain interpreter-like semantics where NullPointerException is thrown
463 // after potential NoSuchMethodError from class linker.
464 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
465 return nullptr; // Failure.
466 }
467 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700468 switch (type) {
469 case kStatic:
470 case kDirect:
471 return resolved_method;
472 case kVirtual: {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100473 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700474 uint16_t vtable_index = resolved_method->GetMethodIndex();
475 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700476 (!klass->HasVTable() ||
477 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700478 // Behavior to agree with that of the verifier.
479 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
480 resolved_method->GetName(), resolved_method->GetSignature());
481 return nullptr; // Failure.
482 }
David Sehr709b0702016-10-13 09:12:37 -0700483 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700485 }
486 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700487 // TODO This lookup is quite slow.
488 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
489 // that will actually not be what we want in some cases where there are miranda methods or
490 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
491 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700492 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700493 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700494 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800495 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800496 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000497 ObjPtr<mirror::Class> method_reference_class =
498 class_linker->ResolveType(method_type_idx, referrer);
Alex Light705ad492015-09-21 11:36:30 -0700499 if (UNLIKELY(method_reference_class == nullptr)) {
500 // Bad type idx.
501 CHECK(self->IsExceptionPending());
502 return nullptr;
503 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700504 // It is not an interface. If the referring class is in the class hierarchy of the
505 // referenced class in the bytecode, we use its super class. Otherwise, we throw
506 // a NoSuchMethodError.
Vladimir Marko302f69c2017-07-25 15:27:15 +0100507 ObjPtr<mirror::Class> super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700508 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
509 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700510 }
Alex Light705ad492015-09-21 11:36:30 -0700511 uint16_t vtable_index = resolved_method->GetMethodIndex();
512 if (access_check) {
513 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700514 if (super_class == nullptr ||
515 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700516 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
517 // Behavior to agree with that of the verifier.
518 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
519 resolved_method->GetName(), resolved_method->GetSignature());
520 return nullptr; // Failure.
521 }
522 }
523 DCHECK(super_class != nullptr);
524 DCHECK(super_class->HasVTable());
525 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
526 } else {
527 // It is an interface.
528 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700529 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700530 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
531 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700532 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700533 referrer);
534 return nullptr; // Failure.
535 }
536 }
537 // TODO We can do better than this for a (compiled) fastpath.
538 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
539 resolved_method, class_linker->GetImagePointerSize());
540 // Throw an NSME if nullptr;
541 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700542 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
543 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700544 }
Alex Light705ad492015-09-21 11:36:30 -0700545 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700546 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700547 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700548 }
549 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700550 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700551 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko302f69c2017-07-25 15:27:15 +0100552 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
553 ArtMethod* imt_method = klass->GetImt(pointer_size)->Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000554 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700555 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700556 ArtMethod* method = klass->FindVirtualMethodForInterface(
557 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700558 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
559 << imt_method->PrettyMethod() << " / "
560 << ArtMethod::PrettyMethod(method) << " / "
561 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700562 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700563 return imt_method;
564 } else {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100565 ArtMethod* interface_method = klass->FindVirtualMethodForInterface(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700566 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700567 if (UNLIKELY(interface_method == nullptr)) {
568 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700569 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700570 return nullptr; // Failure.
571 }
572 return interface_method;
573 }
574 }
575 default:
576 LOG(FATAL) << "Unknown invoke type " << type;
577 return nullptr; // Failure.
578 }
579}
580
581// Explicit template declarations of FindMethodFromCode for all invoke types.
582#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700583 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700584 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700585 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700586 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700587 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700588#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
589 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
590 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
591
592EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
593EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
594EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
595EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
596EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
597
598#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
599#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
600
601// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700602inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
603 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700604 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700605 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800606 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700607 if (UNLIKELY(resolved_field == nullptr)) {
608 return nullptr;
609 }
610 // Check for incompatible class change.
611 bool is_primitive;
612 bool is_set;
613 bool is_static;
614 switch (type) {
615 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
616 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
617 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
618 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
619 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
620 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
621 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
622 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
623 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700624 LOG(FATAL) << "UNREACHABLE";
625 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700626 }
627 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
628 // Incompatible class change.
629 return nullptr;
630 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700631 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700632 if (is_static) {
633 // Check class is initialized else fail so that we can contend to initialize the class with
634 // other threads that may be racing to do this.
635 if (UNLIKELY(!fields_class->IsInitialized())) {
636 return nullptr;
637 }
638 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100639 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700640 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700642 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
643 // Illegal access.
644 return nullptr;
645 }
646 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
647 resolved_field->FieldSize() != expected_size)) {
648 return nullptr;
649 }
650 return resolved_field;
651}
652
653// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100654template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700655inline ArtMethod* FindMethodFast(uint32_t method_idx,
656 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100657 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700658 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700659 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
660 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700661 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100662 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
663 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100664 constexpr ClassLinker::ResolveMode resolve_mode = access_check
665 ? ClassLinker::ResolveMode::kCheckICCEAndIAE
666 : ClassLinker::ResolveMode::kNoChecks;
667 ClassLinker* linker = Runtime::Current()->GetClassLinker();
668 ArtMethod* resolved_method = linker->GetResolvedMethod<type, resolve_mode>(method_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700669 if (UNLIKELY(resolved_method == nullptr)) {
670 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700671 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700672 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700673 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
674 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700675 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700676 return resolved_method;
677 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700678 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000679 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000680 ObjPtr<mirror::Class> method_reference_class = linker->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000681 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700682 if (method_reference_class == nullptr) {
683 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000684 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700685 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700686 // It is not an interface. If the referring class is in the class hierarchy of the
687 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
688 // resolve the method.
689 if (!method_reference_class->IsAssignableFrom(referring_class)) {
690 return nullptr;
691 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100692 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700693 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
694 // The super class does not have the method.
695 return nullptr;
696 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700697 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700698 } else {
699 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700700 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000701 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700702 } else {
703 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700704 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700705 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700706 }
707}
708
Vladimir Marko28e012a2017-12-07 11:22:59 +0000709inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
710 ArtMethod* referrer,
711 Thread* self,
712 bool can_run_clinit,
713 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700714 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +0000715 ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700716 if (UNLIKELY(klass == nullptr)) {
717 CHECK(self->IsExceptionPending());
718 return nullptr; // Failure - Indicate to caller to deliver exception
719 }
720 // Perform access check if necessary.
721 mirror::Class* referring_class = referrer->GetDeclaringClass();
722 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
723 ThrowIllegalAccessErrorClass(referring_class, klass);
724 return nullptr; // Failure - Indicate to caller to deliver exception
725 }
726 // If we're just implementing const-class, we shouldn't call <clinit>.
727 if (!can_run_clinit) {
728 return klass;
729 }
730 // If we are the <clinit> of this class, just return our storage.
731 //
732 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
733 // running.
734 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
735 return klass;
736 }
737 StackHandleScope<1> hs(self);
738 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700739 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700740 CHECK(self->IsExceptionPending());
741 return nullptr; // Failure - Indicate to caller to deliver exception
742 }
743 return h_class.Get();
744}
745
Vladimir Marko28e012a2017-12-07 11:22:59 +0000746static inline ObjPtr<mirror::String> ResolveString(ClassLinker* class_linker,
747 dex::StringIndex string_idx,
748 ArtMethod* referrer)
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800749 REQUIRES_SHARED(Locks::mutator_lock_) {
750 Thread::PoisonObjectPointersIfDebug();
751 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
752 if (UNLIKELY(string == nullptr)) {
753 StackHandleScope<1> hs(Thread::Current());
754 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000755 string = class_linker->ResolveString(string_idx, dex_cache);
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800756 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000757 return string;
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800758}
759
Vladimir Marko28e012a2017-12-07 11:22:59 +0000760inline ObjPtr<mirror::String> ResolveStringFromCode(ArtMethod* referrer,
761 dex::StringIndex string_idx) {
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800762 Thread::PoisonObjectPointersIfDebug();
763 ObjPtr<mirror::String> string = referrer->GetDexCache()->GetResolvedString(string_idx);
764 if (UNLIKELY(string == nullptr)) {
765 StackHandleScope<1> hs(Thread::Current());
766 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800767 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000768 string = class_linker->ResolveString(string_idx, dex_cache);
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800769 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000770 return string;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700771}
772
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800773inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700774 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700775 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700776 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000777 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700778 self->ClearException();
779 }
780 // Decode locked object and unlock, before popping local references.
781 self->DecodeJObject(locked)->MonitorExit(self);
782 if (UNLIKELY(self->IsExceptionPending())) {
783 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
784 << saved_exception->Dump()
785 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000786 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700787 }
788 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700789 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000790 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700791 }
792}
793
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700794template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800795inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700796 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
797 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
798 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
799 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
800 if (LIKELY(f > kMinIntAsFloat)) {
801 if (LIKELY(f < kMaxIntAsFloat)) {
802 return static_cast<INT_TYPE>(f);
803 } else {
804 return kMaxInt;
805 }
806 } else {
807 return (f != f) ? 0 : kMinInt; // f != f implies NaN
808 }
809}
810
811} // namespace art
812
813#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_