blob: 64b7ecdabe8737193dc7610ac102329ff65075f1 [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"
25#include "indirect_reference_table.h"
26#include "invoke_type.h"
27#include "jni_internal.h"
28#include "mirror/art_method.h"
29#include "mirror/array.h"
30#include "mirror/class-inl.h"
31#include "mirror/object-inl.h"
32#include "mirror/throwable.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033#include "handle_scope-inl.h"
34#include "thread.h"
35
36namespace art {
37
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070039ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -080040inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
41 mirror::ArtMethod* method,
42 Thread* self, bool* slow_path) {
Andreas Gampe58a5af82014-07-31 16:23:49 -070043 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070044 if (UNLIKELY(klass == nullptr)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070045 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
46 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070048 DCHECK(self->IsExceptionPending());
49 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -070050 } else {
51 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070052 }
53 }
54 if (kAccessCheck) {
55 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000056 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070057 *slow_path = true;
58 return nullptr; // Failure
59 }
60 mirror::Class* referrer = method->GetDeclaringClass();
61 if (UNLIKELY(!referrer->CanAccess(klass))) {
62 ThrowIllegalAccessErrorClass(referrer, klass);
63 *slow_path = true;
64 return nullptr; // Failure
65 }
66 }
67 if (UNLIKELY(!klass->IsInitialized())) {
68 StackHandleScope<1> hs(self);
69 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
70 // EnsureInitialized (the class initializer) might cause a GC.
71 // may cause us to suspend meaning that another thread may try to
72 // change the allocator while we are stuck in the entrypoints of
73 // an old allocator. Also, the class initialization may fail. To
74 // handle these cases we mark the slow path boolean as true so
75 // that the caller knows to check the allocator type to see if it
76 // has changed and to null-check the return value in case the
77 // initialization fails.
78 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -070079 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070080 DCHECK(self->IsExceptionPending());
81 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -070082 } else {
83 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070084 }
85 return h_klass.Get();
86 }
87 return klass;
88}
89
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070090ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -080091inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
92 Thread* self,
93 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070094 if (UNLIKELY(!klass->IsInitialized())) {
95 StackHandleScope<1> hs(self);
96 Handle<mirror::Class> h_class(hs.NewHandle(klass));
97 // EnsureInitialized (the class initializer) might cause a GC.
98 // may cause us to suspend meaning that another thread may try to
99 // change the allocator while we are stuck in the entrypoints of
100 // an old allocator. Also, the class initialization may fail. To
101 // handle these cases we mark the slow path boolean as true so
102 // that the caller knows to check the allocator type to see if it
103 // has changed and to null-check the return value in case the
104 // initialization fails.
105 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700106 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700107 DCHECK(self->IsExceptionPending());
108 return nullptr; // Failure
109 }
110 return h_class.Get();
111 }
112 return klass;
113}
114
115// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
116// cannot be resolved, throw an error. If it can, use it to create an instance.
117// When verification/compiler hasn't been able to verify access, optionally perform an access
118// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700119template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700120ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800121inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
122 mirror::ArtMethod* method,
123 Thread* self,
124 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700125 bool slow_path = false;
126 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
127 if (UNLIKELY(slow_path)) {
128 if (klass == nullptr) {
129 return nullptr;
130 }
131 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
132 }
133 DCHECK(klass != nullptr);
134 return klass->Alloc<kInstrumented>(self, allocator_type);
135}
136
137// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700138template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700139ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800140inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
141 Thread* self,
142 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700143 DCHECK(klass != nullptr);
144 bool slow_path = false;
145 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
146 if (UNLIKELY(slow_path)) {
147 if (klass == nullptr) {
148 return nullptr;
149 }
150 gc::Heap* heap = Runtime::Current()->GetHeap();
151 // Pass in false since the object can not be finalizable.
152 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
153 }
154 // Pass in false since the object can not be finalizable.
155 return klass->Alloc<kInstrumented, false>(self, allocator_type);
156}
157
158// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700159template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700160ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800161inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
162 Thread* self,
163 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700164 DCHECK(klass != nullptr);
165 // Pass in false since the object can not be finalizable.
166 return klass->Alloc<kInstrumented, false>(self, allocator_type);
167}
168
169
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700170template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700171ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800172inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800173 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800174 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800175 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700176 if (UNLIKELY(component_count < 0)) {
177 ThrowNegativeArraySizeException(component_count);
178 *slow_path = true;
179 return nullptr; // Failure
180 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700181 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700182 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
183 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
184 *slow_path = true;
185 if (klass == nullptr) { // Error
186 DCHECK(Thread::Current()->IsExceptionPending());
187 return nullptr; // Failure
188 }
189 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
190 }
191 if (kAccessCheck) {
192 mirror::Class* referrer = method->GetDeclaringClass();
193 if (UNLIKELY(!referrer->CanAccess(klass))) {
194 ThrowIllegalAccessErrorClass(referrer, klass);
195 *slow_path = true;
196 return nullptr; // Failure
197 }
198 }
199 return klass;
200}
201
202// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
203// it cannot be resolved, throw an error. If it can, use it to create an array.
204// When verification/compiler hasn't been able to verify access, optionally perform an access
205// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700206template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700207ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800208inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800209 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800210 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800211 Thread* self,
212 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700213 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800214 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700215 &slow_path);
216 if (UNLIKELY(slow_path)) {
217 if (klass == nullptr) {
218 return nullptr;
219 }
220 gc::Heap* heap = Runtime::Current()->GetHeap();
221 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700222 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223 heap->GetCurrentAllocator());
224 }
225 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700226 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700227}
228
229template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700230ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800231inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800232 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800233 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800234 Thread* self,
235 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700236 DCHECK(klass != nullptr);
237 if (UNLIKELY(component_count < 0)) {
238 ThrowNegativeArraySizeException(component_count);
239 return nullptr; // Failure
240 }
241 if (kAccessCheck) {
242 mirror::Class* referrer = method->GetDeclaringClass();
243 if (UNLIKELY(!referrer->CanAccess(klass))) {
244 ThrowIllegalAccessErrorClass(referrer, klass);
245 return nullptr; // Failure
246 }
247 }
248 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
249 // suspension.
250 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700251 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252}
253
254template<FindFieldType type, bool access_check>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700255inline ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800256 Thread* self, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700257 bool is_primitive;
258 bool is_set;
259 bool is_static;
260 switch (type) {
261 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
262 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
263 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
264 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
265 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
266 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
267 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
268 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
269 default: is_primitive = true; is_set = true; is_static = true; break;
270 }
271 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700272 ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700273 if (UNLIKELY(resolved_field == nullptr)) {
274 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
275 return nullptr; // Failure.
276 }
277 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
278 if (access_check) {
279 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
280 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
281 return nullptr;
282 }
283 mirror::Class* referring_class = referrer->GetDeclaringClass();
284 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
285 field_idx))) {
286 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
287 return nullptr; // Failure.
288 }
289 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
290 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
291 return nullptr; // Failure.
292 } else {
293 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
294 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000295 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700296 "Attempted read of %zd-bit %s on field '%s'",
297 expected_size * (32 / sizeof(int32_t)),
298 is_primitive ? "primitive" : "non-primitive",
299 PrettyField(resolved_field, true).c_str());
300 return nullptr; // Failure.
301 }
302 }
303 }
304 if (!is_static) {
305 // instance fields must be being accessed on an initialized class
306 return resolved_field;
307 } else {
308 // If the class is initialized we're done.
309 if (LIKELY(fields_class->IsInitialized())) {
310 return resolved_field;
311 } else {
312 StackHandleScope<1> hs(self);
313 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700314 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700315 // Otherwise let's ensure the class is initialized before resolving the field.
316 return resolved_field;
317 }
318 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
319 return nullptr; // Failure.
320 }
321 }
322}
323
324// Explicit template declarations of FindFieldFromCode for all field access types.
325#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
326template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700327ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700328 mirror::ArtMethod* referrer, \
329 Thread* self, size_t expected_size) \
330
331#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
332 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
333 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
334
335EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
336EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
337EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
338EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
339EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
340EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
341EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
342EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
343
344#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
345#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
346
347template<InvokeType type, bool access_check>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800348inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
349 mirror::Object** this_object,
350 mirror::ArtMethod** referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700351 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700352 mirror::ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700353 if (resolved_method == nullptr) {
354 StackHandleScope<1> hs(self);
355 mirror::Object* null_this = nullptr;
356 HandleWrapper<mirror::Object> h_this(
357 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
358 resolved_method = class_linker->ResolveMethod(self, method_idx, referrer, type);
359 }
360 if (UNLIKELY(resolved_method == nullptr)) {
361 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
362 return nullptr; // Failure.
363 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
364 // Maintain interpreter-like semantics where NullPointerException is thrown
365 // after potential NoSuchMethodError from class linker.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000366 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700367 return nullptr; // Failure.
368 } else if (access_check) {
369 // Incompatible class change should have been handled in resolve method.
370 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
371 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
372 *referrer);
373 return nullptr; // Failure.
374 }
375 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
376 mirror::Class* referring_class = (*referrer)->GetDeclaringClass();
377 bool can_access_resolved_method =
378 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
379 method_idx);
380 if (UNLIKELY(!can_access_resolved_method)) {
381 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
382 return nullptr; // Failure.
383 }
384 }
385 switch (type) {
386 case kStatic:
387 case kDirect:
388 return resolved_method;
389 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700390 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700391 uint16_t vtable_index = resolved_method->GetMethodIndex();
392 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700393 (!klass->HasVTable() ||
394 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700395 // Behavior to agree with that of the verifier.
396 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
397 resolved_method->GetName(), resolved_method->GetSignature());
398 return nullptr; // Failure.
399 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700400 DCHECK(klass->HasVTable()) << PrettyClass(klass);
401 return klass->GetVTableEntry(vtable_index);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700402 }
403 case kSuper: {
404 mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass();
405 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700406 if (access_check) {
407 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700408 if (super_class == nullptr || !super_class->HasVTable() ||
409 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700410 // Behavior to agree with that of the verifier.
411 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
412 resolved_method->GetName(), resolved_method->GetSignature());
413 return nullptr; // Failure.
414 }
415 } else {
416 // Super class must exist.
417 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700418 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700419 DCHECK(super_class->HasVTable());
420 return super_class->GetVTableEntry(vtable_index);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700421 }
422 case kInterface: {
423 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
424 mirror::ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(imt_index);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700425 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
426 if (kIsDebugBuild) {
427 mirror::Class* klass = (*this_object)->GetClass();
428 mirror::ArtMethod* method = klass->FindVirtualMethodForInterface(resolved_method);
429 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
430 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
431 PrettyClass(klass);
432 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700433 return imt_method;
434 } else {
435 mirror::ArtMethod* interface_method =
436 (*this_object)->GetClass()->FindVirtualMethodForInterface(resolved_method);
437 if (UNLIKELY(interface_method == nullptr)) {
438 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
439 *this_object, *referrer);
440 return nullptr; // Failure.
441 }
442 return interface_method;
443 }
444 }
445 default:
446 LOG(FATAL) << "Unknown invoke type " << type;
447 return nullptr; // Failure.
448 }
449}
450
451// Explicit template declarations of FindMethodFromCode for all invoke types.
452#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
453 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
454 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
455 mirror::Object** this_object, \
456 mirror::ArtMethod** referrer, \
457 Thread* self)
458#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
459 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
460 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
461
462EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
463EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
464EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
465EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
466EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
467
468#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
469#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
470
471// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700472inline ArtField* FindFieldFast(uint32_t field_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800473 mirror::ArtMethod* referrer,
474 FindFieldType type, size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700475 ArtField* resolved_field =
476 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700477 if (UNLIKELY(resolved_field == nullptr)) {
478 return nullptr;
479 }
480 // Check for incompatible class change.
481 bool is_primitive;
482 bool is_set;
483 bool is_static;
484 switch (type) {
485 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
486 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
487 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
488 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
489 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
490 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
491 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
492 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
493 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700494 LOG(FATAL) << "UNREACHABLE";
495 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700496 }
497 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
498 // Incompatible class change.
499 return nullptr;
500 }
501 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
502 if (is_static) {
503 // Check class is initialized else fail so that we can contend to initialize the class with
504 // other threads that may be racing to do this.
505 if (UNLIKELY(!fields_class->IsInitialized())) {
506 return nullptr;
507 }
508 }
509 mirror::Class* referring_class = referrer->GetDeclaringClass();
510 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
511 !referring_class->CanAccessMember(fields_class,
512 resolved_field->GetAccessFlags()) ||
513 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
514 // Illegal access.
515 return nullptr;
516 }
517 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
518 resolved_field->FieldSize() != expected_size)) {
519 return nullptr;
520 }
521 return resolved_field;
522}
523
524// Fast path method resolution that can't throw exceptions.
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800525inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
526 mirror::Object* this_object,
527 mirror::ArtMethod* referrer,
528 bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700529 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
530 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700531 }
532 mirror::ArtMethod* resolved_method =
533 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700534 if (UNLIKELY(resolved_method == nullptr)) {
535 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700536 }
537 if (access_check) {
538 // Check for incompatible class change errors and access.
539 bool icce = resolved_method->CheckIncompatibleClassChange(type);
540 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700541 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700542 }
543 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
544 mirror::Class* referring_class = referrer->GetDeclaringClass();
545 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
546 !referring_class->CanAccessMember(methods_class,
547 resolved_method->GetAccessFlags()))) {
548 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700549 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700550 }
551 }
552 if (type == kInterface) { // Most common form of slow path dispatch.
553 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao207a37d2014-10-29 17:24:25 -0700554 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700555 return resolved_method;
556 } else if (type == kSuper) {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700557 return referrer->GetDeclaringClass()->GetSuperClass()
558 ->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700559 } else {
560 DCHECK(type == kVirtual);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700561 return this_object->GetClass()->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700562 }
563}
564
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800565inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700566 mirror::ArtMethod* referrer,
567 Thread* self, bool can_run_clinit,
Ian Rogerse5877a12014-07-16 12:06:35 -0700568 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700569 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
570 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
571 if (UNLIKELY(klass == nullptr)) {
572 CHECK(self->IsExceptionPending());
573 return nullptr; // Failure - Indicate to caller to deliver exception
574 }
575 // Perform access check if necessary.
576 mirror::Class* referring_class = referrer->GetDeclaringClass();
577 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
578 ThrowIllegalAccessErrorClass(referring_class, klass);
579 return nullptr; // Failure - Indicate to caller to deliver exception
580 }
581 // If we're just implementing const-class, we shouldn't call <clinit>.
582 if (!can_run_clinit) {
583 return klass;
584 }
585 // If we are the <clinit> of this class, just return our storage.
586 //
587 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
588 // running.
589 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
590 return klass;
591 }
592 StackHandleScope<1> hs(self);
593 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700594 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700595 CHECK(self->IsExceptionPending());
596 return nullptr; // Failure - Indicate to caller to deliver exception
597 }
598 return h_class.Get();
599}
600
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800601inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
602 uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700603 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
604 return class_linker->ResolveString(string_idx, referrer);
605}
606
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800607inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700608 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700609 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700610 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000611 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700612 self->ClearException();
613 }
614 // Decode locked object and unlock, before popping local references.
615 self->DecodeJObject(locked)->MonitorExit(self);
616 if (UNLIKELY(self->IsExceptionPending())) {
617 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
618 << saved_exception->Dump()
619 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000620 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700621 }
622 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700623 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000624 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700625 }
626}
627
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700628template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800629inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700630 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
631 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
632 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
633 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
634 if (LIKELY(f > kMinIntAsFloat)) {
635 if (LIKELY(f < kMaxIntAsFloat)) {
636 return static_cast<INT_TYPE>(f);
637 } else {
638 return kMaxInt;
639 }
640 } else {
641 return (f != f) ? 0 : kMinInt; // f != f implies NaN
642 }
643}
644
645} // namespace art
646
647#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_