blob: 9292cff88ed326decc445da04ca945e2a8280ace [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
Vladimir Marko5ea536a2015-04-20 20:11:30 +010041inline mirror::ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
42 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
43 auto* refs_only_sp = self->GetManagedStack()->GetTopQuickFrame();
44 DCHECK_EQ(refs_only_sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(type));
45
46 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
47 auto* caller_sp = reinterpret_cast<StackReference<mirror::ArtMethod>*>(
48 reinterpret_cast<uintptr_t>(refs_only_sp) + callee_frame_size);
49 auto* caller = caller_sp->AsMirrorPtr();
50
51 if (kIsDebugBuild) {
52 NthCallerVisitor visitor(self, 1, true);
53 visitor.WalkStack();
54 CHECK(caller == visitor.caller);
55 }
56
57 return caller;
58}
59
Mingyao Yang98d1cc82014-05-15 17:02:16 -070060template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070061ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -080062inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
63 mirror::ArtMethod* method,
64 Thread* self, bool* slow_path) {
Andreas Gampe58a5af82014-07-31 16:23:49 -070065 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 if (UNLIKELY(klass == nullptr)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070067 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
68 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070070 DCHECK(self->IsExceptionPending());
71 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -070072 } else {
73 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070074 }
75 }
76 if (kAccessCheck) {
77 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000078 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070079 *slow_path = true;
80 return nullptr; // Failure
81 }
82 mirror::Class* referrer = method->GetDeclaringClass();
83 if (UNLIKELY(!referrer->CanAccess(klass))) {
84 ThrowIllegalAccessErrorClass(referrer, klass);
85 *slow_path = true;
86 return nullptr; // Failure
87 }
88 }
89 if (UNLIKELY(!klass->IsInitialized())) {
90 StackHandleScope<1> hs(self);
91 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
92 // EnsureInitialized (the class initializer) might cause a GC.
93 // may cause us to suspend meaning that another thread may try to
94 // change the allocator while we are stuck in the entrypoints of
95 // an old allocator. Also, the class initialization may fail. To
96 // handle these cases we mark the slow path boolean as true so
97 // that the caller knows to check the allocator type to see if it
98 // has changed and to null-check the return value in case the
99 // initialization fails.
100 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700101 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700102 DCHECK(self->IsExceptionPending());
103 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700104 } else {
105 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700106 }
107 return h_klass.Get();
108 }
109 return klass;
110}
111
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700112ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800113inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
114 Thread* self,
115 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700116 if (UNLIKELY(!klass->IsInitialized())) {
117 StackHandleScope<1> hs(self);
118 Handle<mirror::Class> h_class(hs.NewHandle(klass));
119 // EnsureInitialized (the class initializer) might cause a GC.
120 // may cause us to suspend meaning that another thread may try to
121 // change the allocator while we are stuck in the entrypoints of
122 // an old allocator. Also, the class initialization may fail. To
123 // handle these cases we mark the slow path boolean as true so
124 // that the caller knows to check the allocator type to see if it
125 // has changed and to null-check the return value in case the
126 // initialization fails.
127 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700128 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700129 DCHECK(self->IsExceptionPending());
130 return nullptr; // Failure
131 }
132 return h_class.Get();
133 }
134 return klass;
135}
136
137// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
138// cannot be resolved, throw an error. If it can, use it to create an instance.
139// When verification/compiler hasn't been able to verify access, optionally perform an access
140// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700141template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700142ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800143inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
144 mirror::ArtMethod* method,
145 Thread* self,
146 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700147 bool slow_path = false;
148 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
149 if (UNLIKELY(slow_path)) {
150 if (klass == nullptr) {
151 return nullptr;
152 }
153 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
154 }
155 DCHECK(klass != nullptr);
156 return klass->Alloc<kInstrumented>(self, allocator_type);
157}
158
159// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700160template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700161ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800162inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
163 Thread* self,
164 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700165 DCHECK(klass != nullptr);
166 bool slow_path = false;
167 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
168 if (UNLIKELY(slow_path)) {
169 if (klass == nullptr) {
170 return nullptr;
171 }
172 gc::Heap* heap = Runtime::Current()->GetHeap();
173 // Pass in false since the object can not be finalizable.
174 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
175 }
176 // Pass in false since the object can not be finalizable.
177 return klass->Alloc<kInstrumented, false>(self, allocator_type);
178}
179
180// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700181template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700182ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800183inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
184 Thread* self,
185 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700186 DCHECK(klass != nullptr);
187 // Pass in false since the object can not be finalizable.
188 return klass->Alloc<kInstrumented, false>(self, allocator_type);
189}
190
191
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700192template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700193ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800194inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800195 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800196 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800197 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700198 if (UNLIKELY(component_count < 0)) {
199 ThrowNegativeArraySizeException(component_count);
200 *slow_path = true;
201 return nullptr; // Failure
202 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700203 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700204 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
205 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
206 *slow_path = true;
207 if (klass == nullptr) { // Error
208 DCHECK(Thread::Current()->IsExceptionPending());
209 return nullptr; // Failure
210 }
211 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
212 }
213 if (kAccessCheck) {
214 mirror::Class* referrer = method->GetDeclaringClass();
215 if (UNLIKELY(!referrer->CanAccess(klass))) {
216 ThrowIllegalAccessErrorClass(referrer, klass);
217 *slow_path = true;
218 return nullptr; // Failure
219 }
220 }
221 return klass;
222}
223
224// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
225// it cannot be resolved, throw an error. If it can, use it to create an array.
226// When verification/compiler hasn't been able to verify access, optionally perform an access
227// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700228template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700229ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800230inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800231 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800232 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800233 Thread* self,
234 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800236 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237 &slow_path);
238 if (UNLIKELY(slow_path)) {
239 if (klass == nullptr) {
240 return nullptr;
241 }
242 gc::Heap* heap = Runtime::Current()->GetHeap();
243 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700244 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700245 heap->GetCurrentAllocator());
246 }
247 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700248 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700249}
250
251template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700252ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800253inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800254 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800256 Thread* self,
257 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258 DCHECK(klass != nullptr);
259 if (UNLIKELY(component_count < 0)) {
260 ThrowNegativeArraySizeException(component_count);
261 return nullptr; // Failure
262 }
263 if (kAccessCheck) {
264 mirror::Class* referrer = method->GetDeclaringClass();
265 if (UNLIKELY(!referrer->CanAccess(klass))) {
266 ThrowIllegalAccessErrorClass(referrer, klass);
267 return nullptr; // Failure
268 }
269 }
270 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
271 // suspension.
272 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700273 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700274}
275
276template<FindFieldType type, bool access_check>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700277inline ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800278 Thread* self, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700279 bool is_primitive;
280 bool is_set;
281 bool is_static;
282 switch (type) {
283 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
284 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
285 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
286 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
287 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
288 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
289 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
290 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
291 default: is_primitive = true; is_set = true; is_static = true; break;
292 }
293 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700294 ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700295 if (UNLIKELY(resolved_field == nullptr)) {
296 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
297 return nullptr; // Failure.
298 }
299 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
300 if (access_check) {
301 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
302 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
303 return nullptr;
304 }
305 mirror::Class* referring_class = referrer->GetDeclaringClass();
306 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
307 field_idx))) {
308 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
309 return nullptr; // Failure.
310 }
311 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
312 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
313 return nullptr; // Failure.
314 } else {
315 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
316 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000317 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700318 "Attempted read of %zd-bit %s on field '%s'",
319 expected_size * (32 / sizeof(int32_t)),
320 is_primitive ? "primitive" : "non-primitive",
321 PrettyField(resolved_field, true).c_str());
322 return nullptr; // Failure.
323 }
324 }
325 }
326 if (!is_static) {
327 // instance fields must be being accessed on an initialized class
328 return resolved_field;
329 } else {
330 // If the class is initialized we're done.
331 if (LIKELY(fields_class->IsInitialized())) {
332 return resolved_field;
333 } else {
334 StackHandleScope<1> hs(self);
335 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700336 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700337 // Otherwise let's ensure the class is initialized before resolving the field.
338 return resolved_field;
339 }
340 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
341 return nullptr; // Failure.
342 }
343 }
344}
345
346// Explicit template declarations of FindFieldFromCode for all field access types.
347#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
348template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700349ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700350 mirror::ArtMethod* referrer, \
351 Thread* self, size_t expected_size) \
352
353#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
354 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
355 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
356
357EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
358EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
359EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
360EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
361EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
362EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
363EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
364EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
365
366#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
367#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
368
369template<InvokeType type, bool access_check>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800370inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
371 mirror::Object** this_object,
372 mirror::ArtMethod** referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700373 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700374 mirror::ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700375 if (resolved_method == nullptr) {
376 StackHandleScope<1> hs(self);
377 mirror::Object* null_this = nullptr;
378 HandleWrapper<mirror::Object> h_this(
379 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
380 resolved_method = class_linker->ResolveMethod(self, method_idx, referrer, type);
381 }
382 if (UNLIKELY(resolved_method == nullptr)) {
383 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
384 return nullptr; // Failure.
385 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
386 // Maintain interpreter-like semantics where NullPointerException is thrown
387 // after potential NoSuchMethodError from class linker.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000388 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700389 return nullptr; // Failure.
390 } else if (access_check) {
391 // Incompatible class change should have been handled in resolve method.
392 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
393 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
394 *referrer);
395 return nullptr; // Failure.
396 }
397 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
398 mirror::Class* referring_class = (*referrer)->GetDeclaringClass();
399 bool can_access_resolved_method =
400 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
401 method_idx);
402 if (UNLIKELY(!can_access_resolved_method)) {
403 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
404 return nullptr; // Failure.
405 }
406 }
407 switch (type) {
408 case kStatic:
409 case kDirect:
410 return resolved_method;
411 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700412 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700413 uint16_t vtable_index = resolved_method->GetMethodIndex();
414 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700415 (!klass->HasVTable() ||
416 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700417 // Behavior to agree with that of the verifier.
418 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
419 resolved_method->GetName(), resolved_method->GetSignature());
420 return nullptr; // Failure.
421 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700422 DCHECK(klass->HasVTable()) << PrettyClass(klass);
423 return klass->GetVTableEntry(vtable_index);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700424 }
425 case kSuper: {
426 mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass();
427 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700428 if (access_check) {
429 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700430 if (super_class == nullptr || !super_class->HasVTable() ||
431 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700432 // Behavior to agree with that of the verifier.
433 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
434 resolved_method->GetName(), resolved_method->GetSignature());
435 return nullptr; // Failure.
436 }
437 } else {
438 // Super class must exist.
439 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700440 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700441 DCHECK(super_class->HasVTable());
442 return super_class->GetVTableEntry(vtable_index);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700443 }
444 case kInterface: {
445 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
446 mirror::ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(imt_index);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700447 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
448 if (kIsDebugBuild) {
449 mirror::Class* klass = (*this_object)->GetClass();
450 mirror::ArtMethod* method = klass->FindVirtualMethodForInterface(resolved_method);
451 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
452 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
453 PrettyClass(klass);
454 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700455 return imt_method;
456 } else {
457 mirror::ArtMethod* interface_method =
458 (*this_object)->GetClass()->FindVirtualMethodForInterface(resolved_method);
459 if (UNLIKELY(interface_method == nullptr)) {
460 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
461 *this_object, *referrer);
462 return nullptr; // Failure.
463 }
464 return interface_method;
465 }
466 }
467 default:
468 LOG(FATAL) << "Unknown invoke type " << type;
469 return nullptr; // Failure.
470 }
471}
472
473// Explicit template declarations of FindMethodFromCode for all invoke types.
474#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
475 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
476 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
477 mirror::Object** this_object, \
478 mirror::ArtMethod** referrer, \
479 Thread* self)
480#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
481 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
482 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
483
484EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
485EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
486EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
487EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
488EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
489
490#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
491#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
492
493// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700494inline ArtField* FindFieldFast(uint32_t field_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800495 mirror::ArtMethod* referrer,
496 FindFieldType type, size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700497 ArtField* resolved_field =
498 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700499 if (UNLIKELY(resolved_field == nullptr)) {
500 return nullptr;
501 }
502 // Check for incompatible class change.
503 bool is_primitive;
504 bool is_set;
505 bool is_static;
506 switch (type) {
507 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
508 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
509 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
510 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
511 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
512 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
513 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
514 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
515 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700516 LOG(FATAL) << "UNREACHABLE";
517 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700518 }
519 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
520 // Incompatible class change.
521 return nullptr;
522 }
523 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
524 if (is_static) {
525 // Check class is initialized else fail so that we can contend to initialize the class with
526 // other threads that may be racing to do this.
527 if (UNLIKELY(!fields_class->IsInitialized())) {
528 return nullptr;
529 }
530 }
531 mirror::Class* referring_class = referrer->GetDeclaringClass();
532 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
533 !referring_class->CanAccessMember(fields_class,
534 resolved_field->GetAccessFlags()) ||
535 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
536 // Illegal access.
537 return nullptr;
538 }
539 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
540 resolved_field->FieldSize() != expected_size)) {
541 return nullptr;
542 }
543 return resolved_field;
544}
545
546// Fast path method resolution that can't throw exceptions.
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800547inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
548 mirror::Object* this_object,
549 mirror::ArtMethod* referrer,
550 bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700551 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
552 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700553 }
554 mirror::ArtMethod* resolved_method =
555 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700556 if (UNLIKELY(resolved_method == nullptr)) {
557 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700558 }
559 if (access_check) {
560 // Check for incompatible class change errors and access.
561 bool icce = resolved_method->CheckIncompatibleClassChange(type);
562 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700563 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700564 }
565 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
566 mirror::Class* referring_class = referrer->GetDeclaringClass();
567 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
568 !referring_class->CanAccessMember(methods_class,
569 resolved_method->GetAccessFlags()))) {
570 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700571 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700572 }
573 }
574 if (type == kInterface) { // Most common form of slow path dispatch.
575 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao207a37d2014-10-29 17:24:25 -0700576 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700577 return resolved_method;
578 } else if (type == kSuper) {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700579 return referrer->GetDeclaringClass()->GetSuperClass()
580 ->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700581 } else {
582 DCHECK(type == kVirtual);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700583 return this_object->GetClass()->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700584 }
585}
586
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800587inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700588 mirror::ArtMethod* referrer,
589 Thread* self, bool can_run_clinit,
Ian Rogerse5877a12014-07-16 12:06:35 -0700590 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700591 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
592 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
593 if (UNLIKELY(klass == nullptr)) {
594 CHECK(self->IsExceptionPending());
595 return nullptr; // Failure - Indicate to caller to deliver exception
596 }
597 // Perform access check if necessary.
598 mirror::Class* referring_class = referrer->GetDeclaringClass();
599 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
600 ThrowIllegalAccessErrorClass(referring_class, klass);
601 return nullptr; // Failure - Indicate to caller to deliver exception
602 }
603 // If we're just implementing const-class, we shouldn't call <clinit>.
604 if (!can_run_clinit) {
605 return klass;
606 }
607 // If we are the <clinit> of this class, just return our storage.
608 //
609 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
610 // running.
611 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
612 return klass;
613 }
614 StackHandleScope<1> hs(self);
615 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700616 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700617 CHECK(self->IsExceptionPending());
618 return nullptr; // Failure - Indicate to caller to deliver exception
619 }
620 return h_class.Get();
621}
622
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800623inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
624 uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700625 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
626 return class_linker->ResolveString(string_idx, referrer);
627}
628
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800629inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700630 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700631 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700632 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000633 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700634 self->ClearException();
635 }
636 // Decode locked object and unlock, before popping local references.
637 self->DecodeJObject(locked)->MonitorExit(self);
638 if (UNLIKELY(self->IsExceptionPending())) {
639 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
640 << saved_exception->Dump()
641 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000642 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700643 }
644 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700645 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000646 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700647 }
648}
649
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700650template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800651inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700652 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
653 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
654 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
655 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
656 if (LIKELY(f > kMinIntAsFloat)) {
657 if (LIKELY(f < kMaxIntAsFloat)) {
658 return static_cast<INT_TYPE>(f);
659 } else {
660 return kMaxInt;
661 }
662 } else {
663 return (f != f) ? 0 : kMinInt; // f != f implies NaN
664 }
665}
666
667} // namespace art
668
669#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_