blob: b0cbd0288084bf633183a0395a591cc3d664aabe [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "class_linker-inl.h"
24#include "common_throws.h"
25#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010026#include "entrypoints/quick/callee_save_frame.h"
27#include "handle_scope-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "indirect_reference_table.h"
29#include "invoke_type.h"
30#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "mirror/array.h"
32#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
34#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010035#include "nth_caller_visitor.h"
36#include "runtime.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037#include "thread.h"
38
39namespace art {
40
Mathieu Chartiere401d142015-04-22 13:56:20 -070041inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
42 uint32_t method_index,
43 InvokeType invoke_type)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010044 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070045 ArtMethod* caller = outer_method->GetDexCacheResolvedMethod(method_index, sizeof(void*));
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010046 if (!caller->IsRuntimeMethod()) {
47 return caller;
48 }
49
50 // The method in the dex cache can be the runtime method responsible for invoking
51 // the stub that will then update the dex cache. Therefore, we need to do the
52 // resolution ourselves.
53
Mathieu Chartiere401d142015-04-22 13:56:20 -070054 StackHandleScope<2> hs(Thread::Current());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010055 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere401d142015-04-22 13:56:20 -070056 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(outer_method->GetClassLoader()));
57 Handle<mirror::DexCache> dex_cache(hs.NewHandle(outer_method->GetDexCache()));
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010058 return class_linker->ResolveMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 *outer_method->GetDexFile(), method_index, dex_cache, class_loader, nullptr, invoke_type);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010060}
61
Mathieu Chartiere401d142015-04-22 13:56:20 -070062inline ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
63 Runtime::CalleeSaveType type,
64 bool do_caller_check = false)
Vladimir Marko5ea536a2015-04-20 20:11:30 +010065 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070066 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
Vladimir Marko5ea536a2015-04-20 20:11:30 +010067
68 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
Mathieu Chartiere401d142015-04-22 13:56:20 -070069 auto** caller_sp = reinterpret_cast<ArtMethod**>(
70 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
71 ArtMethod* outer_method = *caller_sp;
72 ArtMethod* caller = outer_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010073
74 if ((outer_method != nullptr) && outer_method->IsOptimized(sizeof(void*))) {
75 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
76 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
77 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
78 uintptr_t native_pc_offset = outer_method->NativeQuickPcOffset(caller_pc);
79 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +010080 StackMapEncoding encoding = code_info.ExtractEncoding();
81 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010082 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +010083 if (stack_map.HasInlineInfo(encoding)) {
84 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010085 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inline_info.GetDepth() - 1);
86 InvokeType invoke_type = static_cast<InvokeType>(
87 inline_info.GetInvokeTypeAtDepth(inline_info.GetDepth() - 1));
88 caller = GetResolvedMethod(outer_method, method_index, invoke_type);
89 }
90 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +010091
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010092 if (kIsDebugBuild && do_caller_check) {
93 // Note that do_caller_check is optional, as this method can be called by
94 // stubs, and tests without a proper call stack.
95 NthCallerVisitor visitor(Thread::Current(), 1, true);
Vladimir Marko5ea536a2015-04-20 20:11:30 +010096 visitor.WalkStack();
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010097 CHECK_EQ(caller, visitor.caller);
Vladimir Marko5ea536a2015-04-20 20:11:30 +010098 }
99
100 return caller;
101}
102
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
105 return GetCalleeSaveMethodCaller(
106 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
107}
108
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700109template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700110ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800111inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800113 Thread* self, bool* slow_path) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700114 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 if (UNLIKELY(klass == nullptr)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700116 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
117 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700119 DCHECK(self->IsExceptionPending());
120 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700121 } else {
122 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700123 }
124 }
125 if (kAccessCheck) {
126 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000127 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700128 *slow_path = true;
129 return nullptr; // Failure
130 }
131 mirror::Class* referrer = method->GetDeclaringClass();
132 if (UNLIKELY(!referrer->CanAccess(klass))) {
133 ThrowIllegalAccessErrorClass(referrer, klass);
134 *slow_path = true;
135 return nullptr; // Failure
136 }
137 }
138 if (UNLIKELY(!klass->IsInitialized())) {
139 StackHandleScope<1> hs(self);
140 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
141 // EnsureInitialized (the class initializer) might cause a GC.
142 // may cause us to suspend meaning that another thread may try to
143 // change the allocator while we are stuck in the entrypoints of
144 // an old allocator. Also, the class initialization may fail. To
145 // handle these cases we mark the slow path boolean as true so
146 // that the caller knows to check the allocator type to see if it
147 // has changed and to null-check the return value in case the
148 // initialization fails.
149 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700150 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700151 DCHECK(self->IsExceptionPending());
152 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700153 } else {
154 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700155 }
156 return h_klass.Get();
157 }
158 return klass;
159}
160
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700161ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800162inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
163 Thread* self,
164 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700165 if (UNLIKELY(!klass->IsInitialized())) {
166 StackHandleScope<1> hs(self);
167 Handle<mirror::Class> h_class(hs.NewHandle(klass));
168 // EnsureInitialized (the class initializer) might cause a GC.
169 // may cause us to suspend meaning that another thread may try to
170 // change the allocator while we are stuck in the entrypoints of
171 // an old allocator. Also, the class initialization may fail. To
172 // handle these cases we mark the slow path boolean as true so
173 // that the caller knows to check the allocator type to see if it
174 // has changed and to null-check the return value in case the
175 // initialization fails.
176 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700177 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700178 DCHECK(self->IsExceptionPending());
179 return nullptr; // Failure
180 }
181 return h_class.Get();
182 }
183 return klass;
184}
185
186// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
187// cannot be resolved, throw an error. If it can, use it to create an instance.
188// When verification/compiler hasn't been able to verify access, optionally perform an access
189// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700190template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700191ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800192inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700193 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800194 Thread* self,
195 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700196 bool slow_path = false;
197 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
198 if (UNLIKELY(slow_path)) {
199 if (klass == nullptr) {
200 return nullptr;
201 }
202 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
203 }
204 DCHECK(klass != nullptr);
205 return klass->Alloc<kInstrumented>(self, allocator_type);
206}
207
208// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700209template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700210ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800211inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
212 Thread* self,
213 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700214 DCHECK(klass != nullptr);
215 bool slow_path = false;
216 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
217 if (UNLIKELY(slow_path)) {
218 if (klass == nullptr) {
219 return nullptr;
220 }
221 gc::Heap* heap = Runtime::Current()->GetHeap();
222 // Pass in false since the object can not be finalizable.
223 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
224 }
225 // Pass in false since the object can not be finalizable.
226 return klass->Alloc<kInstrumented, false>(self, allocator_type);
227}
228
229// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700230template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700231ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800232inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
233 Thread* self,
234 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235 DCHECK(klass != nullptr);
236 // Pass in false since the object can not be finalizable.
237 return klass->Alloc<kInstrumented, false>(self, allocator_type);
238}
239
240
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700241template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700242ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800243inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800244 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700245 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800246 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 if (UNLIKELY(component_count < 0)) {
248 ThrowNegativeArraySizeException(component_count);
249 *slow_path = true;
250 return nullptr; // Failure
251 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700252 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
254 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
255 *slow_path = true;
256 if (klass == nullptr) { // Error
257 DCHECK(Thread::Current()->IsExceptionPending());
258 return nullptr; // Failure
259 }
260 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
261 }
262 if (kAccessCheck) {
263 mirror::Class* referrer = method->GetDeclaringClass();
264 if (UNLIKELY(!referrer->CanAccess(klass))) {
265 ThrowIllegalAccessErrorClass(referrer, klass);
266 *slow_path = true;
267 return nullptr; // Failure
268 }
269 }
270 return klass;
271}
272
273// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
274// it cannot be resolved, throw an error. If it can, use it to create an array.
275// When verification/compiler hasn't been able to verify access, optionally perform an access
276// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700278ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800279inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800280 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800282 Thread* self,
283 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700284 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800285 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700286 &slow_path);
287 if (UNLIKELY(slow_path)) {
288 if (klass == nullptr) {
289 return nullptr;
290 }
291 gc::Heap* heap = Runtime::Current()->GetHeap();
292 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700293 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700294 heap->GetCurrentAllocator());
295 }
296 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700297 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700298}
299
300template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700301ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800302inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800303 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800305 Thread* self,
306 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700307 DCHECK(klass != nullptr);
308 if (UNLIKELY(component_count < 0)) {
309 ThrowNegativeArraySizeException(component_count);
310 return nullptr; // Failure
311 }
312 if (kAccessCheck) {
313 mirror::Class* referrer = method->GetDeclaringClass();
314 if (UNLIKELY(!referrer->CanAccess(klass))) {
315 ThrowIllegalAccessErrorClass(referrer, klass);
316 return nullptr; // Failure
317 }
318 }
319 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
320 // suspension.
321 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700322 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700323}
324
325template<FindFieldType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326inline ArtField* FindFieldFromCode(uint32_t field_idx, ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800327 Thread* self, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700328 bool is_primitive;
329 bool is_set;
330 bool is_static;
331 switch (type) {
332 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
333 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
334 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
335 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
336 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
337 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
338 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
339 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
340 default: is_primitive = true; is_set = true; is_static = true; break;
341 }
342 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700343 ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700344 if (UNLIKELY(resolved_field == nullptr)) {
345 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
346 return nullptr; // Failure.
347 }
348 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
349 if (access_check) {
350 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
351 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
352 return nullptr;
353 }
354 mirror::Class* referring_class = referrer->GetDeclaringClass();
355 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
356 field_idx))) {
357 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
358 return nullptr; // Failure.
359 }
360 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
361 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
362 return nullptr; // Failure.
363 } else {
364 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
365 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000366 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700367 "Attempted read of %zd-bit %s on field '%s'",
368 expected_size * (32 / sizeof(int32_t)),
369 is_primitive ? "primitive" : "non-primitive",
370 PrettyField(resolved_field, true).c_str());
371 return nullptr; // Failure.
372 }
373 }
374 }
375 if (!is_static) {
376 // instance fields must be being accessed on an initialized class
377 return resolved_field;
378 } else {
379 // If the class is initialized we're done.
380 if (LIKELY(fields_class->IsInitialized())) {
381 return resolved_field;
382 } else {
383 StackHandleScope<1> hs(self);
384 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700385 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700386 // Otherwise let's ensure the class is initialized before resolving the field.
387 return resolved_field;
388 }
389 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
390 return nullptr; // Failure.
391 }
392 }
393}
394
395// Explicit template declarations of FindFieldFromCode for all field access types.
396#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
397template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700398ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 ArtMethod* referrer, \
400 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700401
402#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
403 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
404 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
405
406EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
407EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
408EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
409EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
410EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
411EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
412EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
413EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
414
415#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
416#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
417
418template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700419inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
420 ArtMethod** referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700421 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700423 if (resolved_method == nullptr) {
424 StackHandleScope<1> hs(self);
425 mirror::Object* null_this = nullptr;
426 HandleWrapper<mirror::Object> h_this(
427 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 resolved_method = class_linker->ResolveMethod(self, method_idx, *referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700429 }
430 if (UNLIKELY(resolved_method == nullptr)) {
431 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
432 return nullptr; // Failure.
433 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
434 // Maintain interpreter-like semantics where NullPointerException is thrown
435 // after potential NoSuchMethodError from class linker.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000436 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700437 return nullptr; // Failure.
438 } else if (access_check) {
439 // Incompatible class change should have been handled in resolve method.
440 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
441 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
442 *referrer);
443 return nullptr; // Failure.
444 }
445 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
446 mirror::Class* referring_class = (*referrer)->GetDeclaringClass();
447 bool can_access_resolved_method =
448 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
449 method_idx);
450 if (UNLIKELY(!can_access_resolved_method)) {
451 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
452 return nullptr; // Failure.
453 }
454 }
455 switch (type) {
456 case kStatic:
457 case kDirect:
458 return resolved_method;
459 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700460 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700461 uint16_t vtable_index = resolved_method->GetMethodIndex();
462 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700463 (!klass->HasVTable() ||
464 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700465 // Behavior to agree with that of the verifier.
466 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
467 resolved_method->GetName(), resolved_method->GetSignature());
468 return nullptr; // Failure.
469 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700470 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700472 }
473 case kSuper: {
474 mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass();
475 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700476 if (access_check) {
477 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700478 if (super_class == nullptr || !super_class->HasVTable() ||
479 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700480 // Behavior to agree with that of the verifier.
481 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
482 resolved_method->GetName(), resolved_method->GetSignature());
483 return nullptr; // Failure.
484 }
485 } else {
486 // Super class must exist.
487 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700488 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700489 DCHECK(super_class->HasVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700491 }
492 case kInterface: {
493 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700494 ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(
495 imt_index, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700496 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
497 if (kIsDebugBuild) {
498 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700499 ArtMethod* method = klass->FindVirtualMethodForInterface(
500 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700501 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
502 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
503 PrettyClass(klass);
504 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700505 return imt_method;
506 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
508 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700509 if (UNLIKELY(interface_method == nullptr)) {
510 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
511 *this_object, *referrer);
512 return nullptr; // Failure.
513 }
514 return interface_method;
515 }
516 }
517 default:
518 LOG(FATAL) << "Unknown invoke type " << type;
519 return nullptr; // Failure.
520 }
521}
522
523// Explicit template declarations of FindMethodFromCode for all invoke types.
524#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
525 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700526 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
527 mirror::Object** this_object, \
528 ArtMethod** referrer, \
529 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700530#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
531 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
532 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
533
534EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
535EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
536EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
537EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
538EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
539
540#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
541#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
542
543// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700544inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
545 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700546 ArtField* resolved_field =
547 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700548 if (UNLIKELY(resolved_field == nullptr)) {
549 return nullptr;
550 }
551 // Check for incompatible class change.
552 bool is_primitive;
553 bool is_set;
554 bool is_static;
555 switch (type) {
556 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
557 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
558 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
559 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
560 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
561 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
562 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
563 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
564 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700565 LOG(FATAL) << "UNREACHABLE";
566 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700567 }
568 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
569 // Incompatible class change.
570 return nullptr;
571 }
572 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
573 if (is_static) {
574 // Check class is initialized else fail so that we can contend to initialize the class with
575 // other threads that may be racing to do this.
576 if (UNLIKELY(!fields_class->IsInitialized())) {
577 return nullptr;
578 }
579 }
580 mirror::Class* referring_class = referrer->GetDeclaringClass();
581 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700582 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700583 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
584 // Illegal access.
585 return nullptr;
586 }
587 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
588 resolved_field->FieldSize() != expected_size)) {
589 return nullptr;
590 }
591 return resolved_field;
592}
593
594// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
596 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700597 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
598 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700599 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600 ArtMethod* resolved_method =
601 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700602 if (UNLIKELY(resolved_method == nullptr)) {
603 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700604 }
605 if (access_check) {
606 // Check for incompatible class change errors and access.
607 bool icce = resolved_method->CheckIncompatibleClassChange(type);
608 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700609 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700610 }
611 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
612 mirror::Class* referring_class = referrer->GetDeclaringClass();
613 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
614 !referring_class->CanAccessMember(methods_class,
615 resolved_method->GetAccessFlags()))) {
616 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700617 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700618 }
619 }
620 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700621 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700622 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700623 return resolved_method;
624 } else if (type == kSuper) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700625 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
626 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700627 } else {
628 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700629 return this_object->GetClass()->GetVTableEntry(
630 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700631 }
632}
633
Mathieu Chartiere401d142015-04-22 13:56:20 -0700634inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
635 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700636 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
637 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
638 if (UNLIKELY(klass == nullptr)) {
639 CHECK(self->IsExceptionPending());
640 return nullptr; // Failure - Indicate to caller to deliver exception
641 }
642 // Perform access check if necessary.
643 mirror::Class* referring_class = referrer->GetDeclaringClass();
644 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
645 ThrowIllegalAccessErrorClass(referring_class, klass);
646 return nullptr; // Failure - Indicate to caller to deliver exception
647 }
648 // If we're just implementing const-class, we shouldn't call <clinit>.
649 if (!can_run_clinit) {
650 return klass;
651 }
652 // If we are the <clinit> of this class, just return our storage.
653 //
654 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
655 // running.
656 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
657 return klass;
658 }
659 StackHandleScope<1> hs(self);
660 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700661 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700662 CHECK(self->IsExceptionPending());
663 return nullptr; // Failure - Indicate to caller to deliver exception
664 }
665 return h_class.Get();
666}
667
Mathieu Chartiere401d142015-04-22 13:56:20 -0700668inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700669 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
670 return class_linker->ResolveString(string_idx, referrer);
671}
672
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800673inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700674 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700675 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700676 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000677 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 self->ClearException();
679 }
680 // Decode locked object and unlock, before popping local references.
681 self->DecodeJObject(locked)->MonitorExit(self);
682 if (UNLIKELY(self->IsExceptionPending())) {
683 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
684 << saved_exception->Dump()
685 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000686 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700687 }
688 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700689 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000690 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700691 }
692}
693
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700694template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800695inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700696 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
697 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
698 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
699 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
700 if (LIKELY(f > kMinIntAsFloat)) {
701 if (LIKELY(f < kMaxIntAsFloat)) {
702 return static_cast<INT_TYPE>(f);
703 } else {
704 return kMaxInt;
705 }
706 } else {
707 return (f != f) ? 0 : kMinInt; // f != f implies NaN
708 }
709}
710
711} // namespace art
712
713#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_