blob: 298b8263072cd00d38cea4ec2fb8c5a8f1d0def8 [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,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010042 const InlineInfo& inline_info,
43 uint8_t inlining_depth)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010044 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010045 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inlining_depth);
46 InvokeType invoke_type = static_cast<InvokeType>(
47 inline_info.GetInvokeTypeAtDepth(inlining_depth));
Mathieu Chartiere401d142015-04-22 13:56:20 -070048 ArtMethod* caller = outer_method->GetDexCacheResolvedMethod(method_index, sizeof(void*));
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010049 if (!caller->IsRuntimeMethod()) {
50 return caller;
51 }
52
53 // The method in the dex cache can be the runtime method responsible for invoking
54 // the stub that will then update the dex cache. Therefore, we need to do the
55 // resolution ourselves.
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010056
57 // We first find the class loader of our caller. If it is the outer method, we can directly
58 // use its class loader. Otherwise, we also need to resolve our caller.
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 StackHandleScope<2> hs(Thread::Current());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010061 MutableHandle<mirror::ClassLoader> class_loader(hs.NewHandle<mirror::Class>(nullptr));
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 Handle<mirror::DexCache> dex_cache(hs.NewHandle(outer_method->GetDexCache()));
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010063 if (inlining_depth == 0) {
64 class_loader.Assign(outer_method->GetClassLoader());
65 } else {
66 caller = GetResolvedMethod(outer_method, inline_info, inlining_depth - 1);
67 class_loader.Assign(caller->GetClassLoader());
68 }
69
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010070 return class_linker->ResolveMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 *outer_method->GetDexFile(), method_index, dex_cache, class_loader, nullptr, invoke_type);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010072}
73
Mathieu Chartiere401d142015-04-22 13:56:20 -070074inline ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
75 Runtime::CalleeSaveType type,
76 bool do_caller_check = false)
Vladimir Marko5ea536a2015-04-20 20:11:30 +010077 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070078 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
Vladimir Marko5ea536a2015-04-20 20:11:30 +010079
80 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
Mathieu Chartiere401d142015-04-22 13:56:20 -070081 auto** caller_sp = reinterpret_cast<ArtMethod**>(
82 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
83 ArtMethod* outer_method = *caller_sp;
84 ArtMethod* caller = outer_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010085
86 if ((outer_method != nullptr) && outer_method->IsOptimized(sizeof(void*))) {
87 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
88 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
89 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
90 uintptr_t native_pc_offset = outer_method->NativeQuickPcOffset(caller_pc);
91 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +010092 StackMapEncoding encoding = code_info.ExtractEncoding();
93 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010094 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +010095 if (stack_map.HasInlineInfo(encoding)) {
96 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010097 caller = GetResolvedMethod(outer_method, inline_info, inline_info.GetDepth() - 1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010098 }
99 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100100
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100101 if (kIsDebugBuild && do_caller_check) {
102 // Note that do_caller_check is optional, as this method can be called by
103 // stubs, and tests without a proper call stack.
104 NthCallerVisitor visitor(Thread::Current(), 1, true);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100105 visitor.WalkStack();
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100106 CHECK_EQ(caller, visitor.caller);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100107 }
108
109 return caller;
110}
111
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
114 return GetCalleeSaveMethodCaller(
115 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
116}
117
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700119ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800120inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800122 Thread* self, bool* slow_path) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700123 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700124 if (UNLIKELY(klass == nullptr)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700125 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
126 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700127 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700128 DCHECK(self->IsExceptionPending());
129 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700130 } else {
131 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700132 }
133 }
134 if (kAccessCheck) {
135 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000136 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700137 *slow_path = true;
138 return nullptr; // Failure
139 }
140 mirror::Class* referrer = method->GetDeclaringClass();
141 if (UNLIKELY(!referrer->CanAccess(klass))) {
142 ThrowIllegalAccessErrorClass(referrer, klass);
143 *slow_path = true;
144 return nullptr; // Failure
145 }
146 }
147 if (UNLIKELY(!klass->IsInitialized())) {
148 StackHandleScope<1> hs(self);
149 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
150 // EnsureInitialized (the class initializer) might cause a GC.
151 // may cause us to suspend meaning that another thread may try to
152 // change the allocator while we are stuck in the entrypoints of
153 // an old allocator. Also, the class initialization may fail. To
154 // handle these cases we mark the slow path boolean as true so
155 // that the caller knows to check the allocator type to see if it
156 // has changed and to null-check the return value in case the
157 // initialization fails.
158 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700159 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700160 DCHECK(self->IsExceptionPending());
161 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700162 } else {
163 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700164 }
165 return h_klass.Get();
166 }
167 return klass;
168}
169
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700170ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800171inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
172 Thread* self,
173 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700174 if (UNLIKELY(!klass->IsInitialized())) {
175 StackHandleScope<1> hs(self);
176 Handle<mirror::Class> h_class(hs.NewHandle(klass));
177 // EnsureInitialized (the class initializer) might cause a GC.
178 // may cause us to suspend meaning that another thread may try to
179 // change the allocator while we are stuck in the entrypoints of
180 // an old allocator. Also, the class initialization may fail. To
181 // handle these cases we mark the slow path boolean as true so
182 // that the caller knows to check the allocator type to see if it
183 // has changed and to null-check the return value in case the
184 // initialization fails.
185 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700186 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700187 DCHECK(self->IsExceptionPending());
188 return nullptr; // Failure
189 }
190 return h_class.Get();
191 }
192 return klass;
193}
194
195// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
196// cannot be resolved, throw an error. If it can, use it to create an instance.
197// When verification/compiler hasn't been able to verify access, optionally perform an access
198// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700199template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700200ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800201inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800203 Thread* self,
204 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700205 bool slow_path = false;
206 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
207 if (UNLIKELY(slow_path)) {
208 if (klass == nullptr) {
209 return nullptr;
210 }
211 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
212 }
213 DCHECK(klass != nullptr);
214 return klass->Alloc<kInstrumented>(self, allocator_type);
215}
216
217// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700218template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700219ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800220inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
221 Thread* self,
222 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223 DCHECK(klass != nullptr);
224 bool slow_path = false;
225 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
226 if (UNLIKELY(slow_path)) {
227 if (klass == nullptr) {
228 return nullptr;
229 }
230 gc::Heap* heap = Runtime::Current()->GetHeap();
231 // Pass in false since the object can not be finalizable.
232 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
233 }
234 // Pass in false since the object can not be finalizable.
235 return klass->Alloc<kInstrumented, false>(self, allocator_type);
236}
237
238// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700239template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700240ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800241inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
242 Thread* self,
243 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 DCHECK(klass != nullptr);
245 // Pass in false since the object can not be finalizable.
246 return klass->Alloc<kInstrumented, false>(self, allocator_type);
247}
248
249
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700250template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700251ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800252inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800253 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800255 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700256 if (UNLIKELY(component_count < 0)) {
257 ThrowNegativeArraySizeException(component_count);
258 *slow_path = true;
259 return nullptr; // Failure
260 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700261 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700262 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
263 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
264 *slow_path = true;
265 if (klass == nullptr) { // Error
266 DCHECK(Thread::Current()->IsExceptionPending());
267 return nullptr; // Failure
268 }
269 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
270 }
271 if (kAccessCheck) {
272 mirror::Class* referrer = method->GetDeclaringClass();
273 if (UNLIKELY(!referrer->CanAccess(klass))) {
274 ThrowIllegalAccessErrorClass(referrer, klass);
275 *slow_path = true;
276 return nullptr; // Failure
277 }
278 }
279 return klass;
280}
281
282// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
283// it cannot be resolved, throw an error. If it can, use it to create an array.
284// When verification/compiler hasn't been able to verify access, optionally perform an access
285// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700286template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700287ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800288inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800289 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800291 Thread* self,
292 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700293 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800294 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700295 &slow_path);
296 if (UNLIKELY(slow_path)) {
297 if (klass == nullptr) {
298 return nullptr;
299 }
300 gc::Heap* heap = Runtime::Current()->GetHeap();
301 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700302 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700303 heap->GetCurrentAllocator());
304 }
305 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700306 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700307}
308
309template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700310ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800311inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800312 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800314 Thread* self,
315 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700316 DCHECK(klass != nullptr);
317 if (UNLIKELY(component_count < 0)) {
318 ThrowNegativeArraySizeException(component_count);
319 return nullptr; // Failure
320 }
321 if (kAccessCheck) {
322 mirror::Class* referrer = method->GetDeclaringClass();
323 if (UNLIKELY(!referrer->CanAccess(klass))) {
324 ThrowIllegalAccessErrorClass(referrer, klass);
325 return nullptr; // Failure
326 }
327 }
328 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
329 // suspension.
330 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700331 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700332}
333
334template<FindFieldType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335inline ArtField* FindFieldFromCode(uint32_t field_idx, ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800336 Thread* self, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700337 bool is_primitive;
338 bool is_set;
339 bool is_static;
340 switch (type) {
341 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
342 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
343 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
344 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
345 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
346 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
347 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
348 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
349 default: is_primitive = true; is_set = true; is_static = true; break;
350 }
351 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700352 ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700353 if (UNLIKELY(resolved_field == nullptr)) {
354 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
355 return nullptr; // Failure.
356 }
357 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
358 if (access_check) {
359 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
360 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
361 return nullptr;
362 }
363 mirror::Class* referring_class = referrer->GetDeclaringClass();
364 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
365 field_idx))) {
366 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
367 return nullptr; // Failure.
368 }
369 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
370 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
371 return nullptr; // Failure.
372 } else {
373 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
374 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000375 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700376 "Attempted read of %zd-bit %s on field '%s'",
377 expected_size * (32 / sizeof(int32_t)),
378 is_primitive ? "primitive" : "non-primitive",
379 PrettyField(resolved_field, true).c_str());
380 return nullptr; // Failure.
381 }
382 }
383 }
384 if (!is_static) {
385 // instance fields must be being accessed on an initialized class
386 return resolved_field;
387 } else {
388 // If the class is initialized we're done.
389 if (LIKELY(fields_class->IsInitialized())) {
390 return resolved_field;
391 } else {
392 StackHandleScope<1> hs(self);
393 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700394 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700395 // Otherwise let's ensure the class is initialized before resolving the field.
396 return resolved_field;
397 }
398 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
399 return nullptr; // Failure.
400 }
401 }
402}
403
404// Explicit template declarations of FindFieldFromCode for all field access types.
405#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
406template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700407ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 ArtMethod* referrer, \
409 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700410
411#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
412 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
413 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
414
415EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
416EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
417EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
418EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
419EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
420EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
421EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
422EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
423
424#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
425#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
426
427template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
429 ArtMethod** referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700430 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700431 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700432 if (resolved_method == nullptr) {
433 StackHandleScope<1> hs(self);
434 mirror::Object* null_this = nullptr;
435 HandleWrapper<mirror::Object> h_this(
436 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 resolved_method = class_linker->ResolveMethod(self, method_idx, *referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700438 }
439 if (UNLIKELY(resolved_method == nullptr)) {
440 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
441 return nullptr; // Failure.
442 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
443 // Maintain interpreter-like semantics where NullPointerException is thrown
444 // after potential NoSuchMethodError from class linker.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000445 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700446 return nullptr; // Failure.
447 } else if (access_check) {
448 // Incompatible class change should have been handled in resolve method.
449 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
450 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
451 *referrer);
452 return nullptr; // Failure.
453 }
454 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
455 mirror::Class* referring_class = (*referrer)->GetDeclaringClass();
456 bool can_access_resolved_method =
457 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
458 method_idx);
459 if (UNLIKELY(!can_access_resolved_method)) {
460 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
461 return nullptr; // Failure.
462 }
463 }
464 switch (type) {
465 case kStatic:
466 case kDirect:
467 return resolved_method;
468 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700469 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700470 uint16_t vtable_index = resolved_method->GetMethodIndex();
471 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700472 (!klass->HasVTable() ||
473 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700474 // Behavior to agree with that of the verifier.
475 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
476 resolved_method->GetName(), resolved_method->GetSignature());
477 return nullptr; // Failure.
478 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700479 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700480 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700481 }
482 case kSuper: {
483 mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass();
484 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700485 if (access_check) {
486 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700487 if (super_class == nullptr || !super_class->HasVTable() ||
488 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700489 // Behavior to agree with that of the verifier.
490 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
491 resolved_method->GetName(), resolved_method->GetSignature());
492 return nullptr; // Failure.
493 }
494 } else {
495 // Super class must exist.
496 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700497 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700498 DCHECK(super_class->HasVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700499 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700500 }
501 case kInterface: {
502 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700503 ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(
504 imt_index, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700505 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
506 if (kIsDebugBuild) {
507 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700508 ArtMethod* method = klass->FindVirtualMethodForInterface(
509 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700510 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
511 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
512 PrettyClass(klass);
513 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700514 return imt_method;
515 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
517 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700518 if (UNLIKELY(interface_method == nullptr)) {
519 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
520 *this_object, *referrer);
521 return nullptr; // Failure.
522 }
523 return interface_method;
524 }
525 }
526 default:
527 LOG(FATAL) << "Unknown invoke type " << type;
528 return nullptr; // Failure.
529 }
530}
531
532// Explicit template declarations of FindMethodFromCode for all invoke types.
533#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
534 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700535 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
536 mirror::Object** this_object, \
537 ArtMethod** referrer, \
538 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700539#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
540 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
541 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
542
543EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
544EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
545EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
546EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
547EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
548
549#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
550#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
551
552// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
554 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700555 ArtField* resolved_field =
556 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700557 if (UNLIKELY(resolved_field == nullptr)) {
558 return nullptr;
559 }
560 // Check for incompatible class change.
561 bool is_primitive;
562 bool is_set;
563 bool is_static;
564 switch (type) {
565 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
566 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
567 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
568 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
569 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
570 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
571 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
572 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
573 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700574 LOG(FATAL) << "UNREACHABLE";
575 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700576 }
577 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
578 // Incompatible class change.
579 return nullptr;
580 }
581 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
582 if (is_static) {
583 // Check class is initialized else fail so that we can contend to initialize the class with
584 // other threads that may be racing to do this.
585 if (UNLIKELY(!fields_class->IsInitialized())) {
586 return nullptr;
587 }
588 }
589 mirror::Class* referring_class = referrer->GetDeclaringClass();
590 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700591 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700592 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
593 // Illegal access.
594 return nullptr;
595 }
596 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
597 resolved_field->FieldSize() != expected_size)) {
598 return nullptr;
599 }
600 return resolved_field;
601}
602
603// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700604inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
605 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700606 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
607 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700608 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700609 ArtMethod* resolved_method =
610 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700611 if (UNLIKELY(resolved_method == nullptr)) {
612 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700613 }
614 if (access_check) {
615 // Check for incompatible class change errors and access.
616 bool icce = resolved_method->CheckIncompatibleClassChange(type);
617 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700618 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700619 }
620 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
621 mirror::Class* referring_class = referrer->GetDeclaringClass();
622 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
623 !referring_class->CanAccessMember(methods_class,
624 resolved_method->GetAccessFlags()))) {
625 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700626 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700627 }
628 }
629 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700630 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700631 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700632 return resolved_method;
633 } else if (type == kSuper) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700634 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
635 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700636 } else {
637 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638 return this_object->GetClass()->GetVTableEntry(
639 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700640 }
641}
642
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
644 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700645 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
646 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
647 if (UNLIKELY(klass == nullptr)) {
648 CHECK(self->IsExceptionPending());
649 return nullptr; // Failure - Indicate to caller to deliver exception
650 }
651 // Perform access check if necessary.
652 mirror::Class* referring_class = referrer->GetDeclaringClass();
653 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
654 ThrowIllegalAccessErrorClass(referring_class, klass);
655 return nullptr; // Failure - Indicate to caller to deliver exception
656 }
657 // If we're just implementing const-class, we shouldn't call <clinit>.
658 if (!can_run_clinit) {
659 return klass;
660 }
661 // If we are the <clinit> of this class, just return our storage.
662 //
663 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
664 // running.
665 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
666 return klass;
667 }
668 StackHandleScope<1> hs(self);
669 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700670 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700671 CHECK(self->IsExceptionPending());
672 return nullptr; // Failure - Indicate to caller to deliver exception
673 }
674 return h_class.Get();
675}
676
Mathieu Chartiere401d142015-04-22 13:56:20 -0700677inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
679 return class_linker->ResolveString(string_idx, referrer);
680}
681
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800682inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700683 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700684 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700685 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000686 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700687 self->ClearException();
688 }
689 // Decode locked object and unlock, before popping local references.
690 self->DecodeJObject(locked)->MonitorExit(self);
691 if (UNLIKELY(self->IsExceptionPending())) {
692 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
693 << saved_exception->Dump()
694 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000695 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700696 }
697 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700698 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000699 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700700 }
701}
702
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700703template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800704inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700705 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
706 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
707 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
708 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
709 if (LIKELY(f > kMinIntAsFloat)) {
710 if (LIKELY(f < kMaxIntAsFloat)) {
711 return static_cast<INT_TYPE>(f);
712 } else {
713 return kMaxInt;
714 }
715 } else {
716 return (f != f) ? 0 : kMinInt; // f != f implies NaN
717 }
718}
719
720} // namespace art
721
722#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_