blob: 625e695ce4c660ae5c0ac9064cc0695aa1676b36 [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
22#include "class_linker-inl.h"
23#include "common_throws.h"
24#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010025#include "entrypoints/quick/callee_save_frame.h"
26#include "handle_scope-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070027#include "indirect_reference_table.h"
28#include "invoke_type.h"
29#include "jni_internal.h"
30#include "mirror/art_method.h"
31#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
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010041inline mirror::ArtMethod* GetCalleeSaveMethodCaller(StackReference<mirror::ArtMethod>* sp,
42 Runtime::CalleeSaveType type,
43 bool do_caller_check = false)
Vladimir Marko5ea536a2015-04-20 20:11:30 +010044 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010045 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(type));
Vladimir Marko5ea536a2015-04-20 20:11:30 +010046
47 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
48 auto* caller_sp = reinterpret_cast<StackReference<mirror::ArtMethod>*>(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010049 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Vladimir Marko5ea536a2015-04-20 20:11:30 +010050 auto* caller = caller_sp->AsMirrorPtr();
51
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010052 if (kIsDebugBuild && do_caller_check) {
53 // Note that do_caller_check is optional, as this method can be called by
54 // stubs, and tests without a proper call stack.
55 NthCallerVisitor visitor(Thread::Current(), 1, true);
Vladimir Marko5ea536a2015-04-20 20:11:30 +010056 visitor.WalkStack();
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010057 CHECK_EQ(caller, visitor.caller);
Vladimir Marko5ea536a2015-04-20 20:11:30 +010058 }
59
60 return caller;
61}
62
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010063inline mirror::ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
65 return GetCalleeSaveMethodCaller(
66 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
67}
68
Mingyao Yang98d1cc82014-05-15 17:02:16 -070069template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070070ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -080071inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
72 mirror::ArtMethod* method,
73 Thread* self, bool* slow_path) {
Andreas Gampe58a5af82014-07-31 16:23:49 -070074 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 if (UNLIKELY(klass == nullptr)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070076 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
77 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070078 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070079 DCHECK(self->IsExceptionPending());
80 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -070081 } else {
82 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070083 }
84 }
85 if (kAccessCheck) {
86 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000087 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070088 *slow_path = true;
89 return nullptr; // Failure
90 }
91 mirror::Class* referrer = method->GetDeclaringClass();
92 if (UNLIKELY(!referrer->CanAccess(klass))) {
93 ThrowIllegalAccessErrorClass(referrer, klass);
94 *slow_path = true;
95 return nullptr; // Failure
96 }
97 }
98 if (UNLIKELY(!klass->IsInitialized())) {
99 StackHandleScope<1> hs(self);
100 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
101 // EnsureInitialized (the class initializer) might cause a GC.
102 // may cause us to suspend meaning that another thread may try to
103 // change the allocator while we are stuck in the entrypoints of
104 // an old allocator. Also, the class initialization may fail. To
105 // handle these cases we mark the slow path boolean as true so
106 // that the caller knows to check the allocator type to see if it
107 // has changed and to null-check the return value in case the
108 // initialization fails.
109 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700110 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700111 DCHECK(self->IsExceptionPending());
112 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700113 } else {
114 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700115 }
116 return h_klass.Get();
117 }
118 return klass;
119}
120
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700121ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800122inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
123 Thread* self,
124 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700125 if (UNLIKELY(!klass->IsInitialized())) {
126 StackHandleScope<1> hs(self);
127 Handle<mirror::Class> h_class(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_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700138 DCHECK(self->IsExceptionPending());
139 return nullptr; // Failure
140 }
141 return h_class.Get();
142 }
143 return klass;
144}
145
146// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
147// cannot be resolved, throw an error. If it can, use it to create an instance.
148// When verification/compiler hasn't been able to verify access, optionally perform an access
149// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700150template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700151ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800152inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
153 mirror::ArtMethod* method,
154 Thread* self,
155 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700156 bool slow_path = false;
157 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
158 if (UNLIKELY(slow_path)) {
159 if (klass == nullptr) {
160 return nullptr;
161 }
162 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
163 }
164 DCHECK(klass != nullptr);
165 return klass->Alloc<kInstrumented>(self, allocator_type);
166}
167
168// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700169template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700170ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800171inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
172 Thread* self,
173 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700174 DCHECK(klass != nullptr);
175 bool slow_path = false;
176 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
177 if (UNLIKELY(slow_path)) {
178 if (klass == nullptr) {
179 return nullptr;
180 }
181 gc::Heap* heap = Runtime::Current()->GetHeap();
182 // Pass in false since the object can not be finalizable.
183 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
184 }
185 // Pass in false since the object can not be finalizable.
186 return klass->Alloc<kInstrumented, false>(self, allocator_type);
187}
188
189// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700190template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700191ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800192inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
193 Thread* self,
194 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700195 DCHECK(klass != nullptr);
196 // Pass in false since the object can not be finalizable.
197 return klass->Alloc<kInstrumented, false>(self, allocator_type);
198}
199
200
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700201template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700202ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800203inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800204 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800205 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800206 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700207 if (UNLIKELY(component_count < 0)) {
208 ThrowNegativeArraySizeException(component_count);
209 *slow_path = true;
210 return nullptr; // Failure
211 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700212 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700213 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
214 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
215 *slow_path = true;
216 if (klass == nullptr) { // Error
217 DCHECK(Thread::Current()->IsExceptionPending());
218 return nullptr; // Failure
219 }
220 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
221 }
222 if (kAccessCheck) {
223 mirror::Class* referrer = method->GetDeclaringClass();
224 if (UNLIKELY(!referrer->CanAccess(klass))) {
225 ThrowIllegalAccessErrorClass(referrer, klass);
226 *slow_path = true;
227 return nullptr; // Failure
228 }
229 }
230 return klass;
231}
232
233// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
234// it cannot be resolved, throw an error. If it can, use it to create an array.
235// When verification/compiler hasn't been able to verify access, optionally perform an access
236// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700238ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800239inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800240 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800241 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800242 Thread* self,
243 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800245 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700246 &slow_path);
247 if (UNLIKELY(slow_path)) {
248 if (klass == nullptr) {
249 return nullptr;
250 }
251 gc::Heap* heap = Runtime::Current()->GetHeap();
252 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700253 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700254 heap->GetCurrentAllocator());
255 }
256 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700257 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258}
259
260template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700261ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800262inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800263 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800264 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800265 Thread* self,
266 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267 DCHECK(klass != nullptr);
268 if (UNLIKELY(component_count < 0)) {
269 ThrowNegativeArraySizeException(component_count);
270 return nullptr; // Failure
271 }
272 if (kAccessCheck) {
273 mirror::Class* referrer = method->GetDeclaringClass();
274 if (UNLIKELY(!referrer->CanAccess(klass))) {
275 ThrowIllegalAccessErrorClass(referrer, klass);
276 return nullptr; // Failure
277 }
278 }
279 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
280 // suspension.
281 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700282 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700283}
284
285template<FindFieldType type, bool access_check>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700286inline ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800287 Thread* self, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700288 bool is_primitive;
289 bool is_set;
290 bool is_static;
291 switch (type) {
292 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
293 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
294 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
295 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
296 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
297 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
298 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
299 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
300 default: is_primitive = true; is_set = true; is_static = true; break;
301 }
302 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700303 ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700304 if (UNLIKELY(resolved_field == nullptr)) {
305 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
306 return nullptr; // Failure.
307 }
308 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
309 if (access_check) {
310 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
311 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
312 return nullptr;
313 }
314 mirror::Class* referring_class = referrer->GetDeclaringClass();
315 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
316 field_idx))) {
317 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
318 return nullptr; // Failure.
319 }
320 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
321 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
322 return nullptr; // Failure.
323 } else {
324 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
325 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000326 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700327 "Attempted read of %zd-bit %s on field '%s'",
328 expected_size * (32 / sizeof(int32_t)),
329 is_primitive ? "primitive" : "non-primitive",
330 PrettyField(resolved_field, true).c_str());
331 return nullptr; // Failure.
332 }
333 }
334 }
335 if (!is_static) {
336 // instance fields must be being accessed on an initialized class
337 return resolved_field;
338 } else {
339 // If the class is initialized we're done.
340 if (LIKELY(fields_class->IsInitialized())) {
341 return resolved_field;
342 } else {
343 StackHandleScope<1> hs(self);
344 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700345 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700346 // Otherwise let's ensure the class is initialized before resolving the field.
347 return resolved_field;
348 }
349 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
350 return nullptr; // Failure.
351 }
352 }
353}
354
355// Explicit template declarations of FindFieldFromCode for all field access types.
356#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
357template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700358ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700359 mirror::ArtMethod* referrer, \
360 Thread* self, size_t expected_size) \
361
362#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
363 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
364 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
365
366EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
367EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
368EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
369EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
370EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
371EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
372EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
373EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
374
375#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
376#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
377
378template<InvokeType type, bool access_check>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800379inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
380 mirror::Object** this_object,
381 mirror::ArtMethod** referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700382 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700383 mirror::ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700384 if (resolved_method == nullptr) {
385 StackHandleScope<1> hs(self);
386 mirror::Object* null_this = nullptr;
387 HandleWrapper<mirror::Object> h_this(
388 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
389 resolved_method = class_linker->ResolveMethod(self, method_idx, referrer, type);
390 }
391 if (UNLIKELY(resolved_method == nullptr)) {
392 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
393 return nullptr; // Failure.
394 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
395 // Maintain interpreter-like semantics where NullPointerException is thrown
396 // after potential NoSuchMethodError from class linker.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000397 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700398 return nullptr; // Failure.
399 } else if (access_check) {
400 // Incompatible class change should have been handled in resolve method.
401 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
402 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
403 *referrer);
404 return nullptr; // Failure.
405 }
406 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
407 mirror::Class* referring_class = (*referrer)->GetDeclaringClass();
408 bool can_access_resolved_method =
409 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
410 method_idx);
411 if (UNLIKELY(!can_access_resolved_method)) {
412 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
413 return nullptr; // Failure.
414 }
415 }
416 switch (type) {
417 case kStatic:
418 case kDirect:
419 return resolved_method;
420 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700421 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700422 uint16_t vtable_index = resolved_method->GetMethodIndex();
423 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700424 (!klass->HasVTable() ||
425 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700426 // Behavior to agree with that of the verifier.
427 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
428 resolved_method->GetName(), resolved_method->GetSignature());
429 return nullptr; // Failure.
430 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700431 DCHECK(klass->HasVTable()) << PrettyClass(klass);
432 return klass->GetVTableEntry(vtable_index);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700433 }
434 case kSuper: {
435 mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass();
436 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700437 if (access_check) {
438 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700439 if (super_class == nullptr || !super_class->HasVTable() ||
440 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700441 // Behavior to agree with that of the verifier.
442 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
443 resolved_method->GetName(), resolved_method->GetSignature());
444 return nullptr; // Failure.
445 }
446 } else {
447 // Super class must exist.
448 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700449 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700450 DCHECK(super_class->HasVTable());
451 return super_class->GetVTableEntry(vtable_index);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700452 }
453 case kInterface: {
454 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
455 mirror::ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(imt_index);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700456 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
457 if (kIsDebugBuild) {
458 mirror::Class* klass = (*this_object)->GetClass();
459 mirror::ArtMethod* method = klass->FindVirtualMethodForInterface(resolved_method);
460 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
461 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
462 PrettyClass(klass);
463 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700464 return imt_method;
465 } else {
466 mirror::ArtMethod* interface_method =
467 (*this_object)->GetClass()->FindVirtualMethodForInterface(resolved_method);
468 if (UNLIKELY(interface_method == nullptr)) {
469 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
470 *this_object, *referrer);
471 return nullptr; // Failure.
472 }
473 return interface_method;
474 }
475 }
476 default:
477 LOG(FATAL) << "Unknown invoke type " << type;
478 return nullptr; // Failure.
479 }
480}
481
482// Explicit template declarations of FindMethodFromCode for all invoke types.
483#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
484 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
485 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
486 mirror::Object** this_object, \
487 mirror::ArtMethod** referrer, \
488 Thread* self)
489#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
490 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
491 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
492
493EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
494EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
495EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
496EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
497EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
498
499#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
500#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
501
502// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700503inline ArtField* FindFieldFast(uint32_t field_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800504 mirror::ArtMethod* referrer,
505 FindFieldType type, size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700506 ArtField* resolved_field =
507 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700508 if (UNLIKELY(resolved_field == nullptr)) {
509 return nullptr;
510 }
511 // Check for incompatible class change.
512 bool is_primitive;
513 bool is_set;
514 bool is_static;
515 switch (type) {
516 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
517 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
518 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
519 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
520 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
521 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
522 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
523 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
524 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700525 LOG(FATAL) << "UNREACHABLE";
526 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700527 }
528 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
529 // Incompatible class change.
530 return nullptr;
531 }
532 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
533 if (is_static) {
534 // Check class is initialized else fail so that we can contend to initialize the class with
535 // other threads that may be racing to do this.
536 if (UNLIKELY(!fields_class->IsInitialized())) {
537 return nullptr;
538 }
539 }
540 mirror::Class* referring_class = referrer->GetDeclaringClass();
541 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
542 !referring_class->CanAccessMember(fields_class,
543 resolved_field->GetAccessFlags()) ||
544 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
545 // Illegal access.
546 return nullptr;
547 }
548 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
549 resolved_field->FieldSize() != expected_size)) {
550 return nullptr;
551 }
552 return resolved_field;
553}
554
555// Fast path method resolution that can't throw exceptions.
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800556inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
557 mirror::Object* this_object,
558 mirror::ArtMethod* referrer,
559 bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700560 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
561 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700562 }
563 mirror::ArtMethod* resolved_method =
564 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700565 if (UNLIKELY(resolved_method == nullptr)) {
566 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700567 }
568 if (access_check) {
569 // Check for incompatible class change errors and access.
570 bool icce = resolved_method->CheckIncompatibleClassChange(type);
571 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700572 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700573 }
574 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
575 mirror::Class* referring_class = referrer->GetDeclaringClass();
576 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
577 !referring_class->CanAccessMember(methods_class,
578 resolved_method->GetAccessFlags()))) {
579 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700580 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700581 }
582 }
583 if (type == kInterface) { // Most common form of slow path dispatch.
584 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao207a37d2014-10-29 17:24:25 -0700585 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700586 return resolved_method;
587 } else if (type == kSuper) {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700588 return referrer->GetDeclaringClass()->GetSuperClass()
589 ->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700590 } else {
591 DCHECK(type == kVirtual);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700592 return this_object->GetClass()->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700593 }
594}
595
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800596inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700597 mirror::ArtMethod* referrer,
598 Thread* self, bool can_run_clinit,
Ian Rogerse5877a12014-07-16 12:06:35 -0700599 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700600 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
601 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
602 if (UNLIKELY(klass == nullptr)) {
603 CHECK(self->IsExceptionPending());
604 return nullptr; // Failure - Indicate to caller to deliver exception
605 }
606 // Perform access check if necessary.
607 mirror::Class* referring_class = referrer->GetDeclaringClass();
608 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
609 ThrowIllegalAccessErrorClass(referring_class, klass);
610 return nullptr; // Failure - Indicate to caller to deliver exception
611 }
612 // If we're just implementing const-class, we shouldn't call <clinit>.
613 if (!can_run_clinit) {
614 return klass;
615 }
616 // If we are the <clinit> of this class, just return our storage.
617 //
618 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
619 // running.
620 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
621 return klass;
622 }
623 StackHandleScope<1> hs(self);
624 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700625 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700626 CHECK(self->IsExceptionPending());
627 return nullptr; // Failure - Indicate to caller to deliver exception
628 }
629 return h_class.Get();
630}
631
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800632inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
633 uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700634 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
635 return class_linker->ResolveString(string_idx, referrer);
636}
637
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800638inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700639 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700640 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700641 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000642 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700643 self->ClearException();
644 }
645 // Decode locked object and unlock, before popping local references.
646 self->DecodeJObject(locked)->MonitorExit(self);
647 if (UNLIKELY(self->IsExceptionPending())) {
648 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
649 << saved_exception->Dump()
650 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000651 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700652 }
653 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700654 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000655 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700656 }
657}
658
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700659template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800660inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700661 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
662 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
663 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
664 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
665 if (LIKELY(f > kMinIntAsFloat)) {
666 if (LIKELY(f < kMaxIntAsFloat)) {
667 return static_cast<INT_TYPE>(f);
668 } else {
669 return kMaxInt;
670 }
671 } else {
672 return (f != f) ? 0 : kMinInt; // f != f implies NaN
673 }
674}
675
676} // namespace art
677
678#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_