blob: f76da8edaa323eea2a59f81d36d8ff2595a8a0bb [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
38// TODO: Fix no thread safety analysis when GCC can handle template specialization.
39template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070040ALWAYS_INLINE
Ian Rogerse5877a12014-07-16 12:06:35 -070041static inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
42 mirror::ArtMethod* method,
43 Thread* self, bool* slow_path) {
Andreas Gampe58a5af82014-07-31 16:23:49 -070044 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070045 if (UNLIKELY(klass == NULL)) {
46 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
47 *slow_path = true;
48 if (klass == NULL) {
49 DCHECK(self->IsExceptionPending());
50 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -070051 } else {
52 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070053 }
54 }
55 if (kAccessCheck) {
56 if (UNLIKELY(!klass->IsInstantiable())) {
57 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
58 self->ThrowNewException(throw_location, "Ljava/lang/InstantiationError;",
59 PrettyDescriptor(klass).c_str());
60 *slow_path = true;
61 return nullptr; // Failure
62 }
63 mirror::Class* referrer = method->GetDeclaringClass();
64 if (UNLIKELY(!referrer->CanAccess(klass))) {
65 ThrowIllegalAccessErrorClass(referrer, klass);
66 *slow_path = true;
67 return nullptr; // Failure
68 }
69 }
70 if (UNLIKELY(!klass->IsInitialized())) {
71 StackHandleScope<1> hs(self);
72 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
73 // EnsureInitialized (the class initializer) might cause a GC.
74 // may cause us to suspend meaning that another thread may try to
75 // change the allocator while we are stuck in the entrypoints of
76 // an old allocator. Also, the class initialization may fail. To
77 // handle these cases we mark the slow path boolean as true so
78 // that the caller knows to check the allocator type to see if it
79 // has changed and to null-check the return value in case the
80 // initialization fails.
81 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -070082 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070083 DCHECK(self->IsExceptionPending());
84 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -070085 } else {
86 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070087 }
88 return h_klass.Get();
89 }
90 return klass;
91}
92
93// TODO: Fix no thread safety analysis when annotalysis is smarter.
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070094ALWAYS_INLINE
Ian Rogerse5877a12014-07-16 12:06:35 -070095static inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
96 Thread* self,
97 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070098 if (UNLIKELY(!klass->IsInitialized())) {
99 StackHandleScope<1> hs(self);
100 Handle<mirror::Class> h_class(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_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700111 DCHECK(self->IsExceptionPending());
112 return nullptr; // Failure
113 }
114 return h_class.Get();
115 }
116 return klass;
117}
118
119// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
120// cannot be resolved, throw an error. If it can, use it to create an instance.
121// When verification/compiler hasn't been able to verify access, optionally perform an access
122// check.
123// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
124template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700125ALWAYS_INLINE
Ian Rogerse5877a12014-07-16 12:06:35 -0700126static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
127 mirror::ArtMethod* method,
128 Thread* self,
129 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700130 bool slow_path = false;
131 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
132 if (UNLIKELY(slow_path)) {
133 if (klass == nullptr) {
134 return nullptr;
135 }
136 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
137 }
138 DCHECK(klass != nullptr);
139 return klass->Alloc<kInstrumented>(self, allocator_type);
140}
141
142// Given the context of a calling Method and a resolved class, create an instance.
143// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
144template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700145ALWAYS_INLINE
Ian Rogerse5877a12014-07-16 12:06:35 -0700146static inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
147 mirror::ArtMethod* method,
148 Thread* self,
149 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700150 DCHECK(klass != nullptr);
151 bool slow_path = false;
152 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
153 if (UNLIKELY(slow_path)) {
154 if (klass == nullptr) {
155 return nullptr;
156 }
157 gc::Heap* heap = Runtime::Current()->GetHeap();
158 // Pass in false since the object can not be finalizable.
159 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
160 }
161 // Pass in false since the object can not be finalizable.
162 return klass->Alloc<kInstrumented, false>(self, allocator_type);
163}
164
165// Given the context of a calling Method and an initialized class, create an instance.
166// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
167template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700168ALWAYS_INLINE
Ian Rogerse5877a12014-07-16 12:06:35 -0700169static inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
170 mirror::ArtMethod* method,
171 Thread* self,
172 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700173 DCHECK(klass != nullptr);
174 // Pass in false since the object can not be finalizable.
175 return klass->Alloc<kInstrumented, false>(self, allocator_type);
176}
177
178
179// TODO: Fix no thread safety analysis when GCC can handle template specialization.
180template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700181ALWAYS_INLINE
Ian Rogerse5877a12014-07-16 12:06:35 -0700182static inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
183 mirror::ArtMethod* method,
184 int32_t component_count,
185 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700186 if (UNLIKELY(component_count < 0)) {
187 ThrowNegativeArraySizeException(component_count);
188 *slow_path = true;
189 return nullptr; // Failure
190 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700191 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700192 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
193 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
194 *slow_path = true;
195 if (klass == nullptr) { // Error
196 DCHECK(Thread::Current()->IsExceptionPending());
197 return nullptr; // Failure
198 }
199 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
200 }
201 if (kAccessCheck) {
202 mirror::Class* referrer = method->GetDeclaringClass();
203 if (UNLIKELY(!referrer->CanAccess(klass))) {
204 ThrowIllegalAccessErrorClass(referrer, klass);
205 *slow_path = true;
206 return nullptr; // Failure
207 }
208 }
209 return klass;
210}
211
212// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
213// it cannot be resolved, throw an error. If it can, use it to create an array.
214// When verification/compiler hasn't been able to verify access, optionally perform an access
215// check.
216// TODO: Fix no thread safety analysis when GCC can handle template specialization.
217template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700218ALWAYS_INLINE
Ian Rogerse5877a12014-07-16 12:06:35 -0700219static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
220 mirror::ArtMethod* method,
221 int32_t component_count,
222 Thread* self,
223 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700224 bool slow_path = false;
225 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, method, component_count,
226 &slow_path);
227 if (UNLIKELY(slow_path)) {
228 if (klass == nullptr) {
229 return nullptr;
230 }
231 gc::Heap* heap = Runtime::Current()->GetHeap();
232 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700233 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234 heap->GetCurrentAllocator());
235 }
236 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700237 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700238}
239
240template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700241ALWAYS_INLINE
Ian Rogerse5877a12014-07-16 12:06:35 -0700242static inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
243 mirror::ArtMethod* method,
244 int32_t component_count,
245 Thread* self,
246 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 DCHECK(klass != nullptr);
248 if (UNLIKELY(component_count < 0)) {
249 ThrowNegativeArraySizeException(component_count);
250 return nullptr; // Failure
251 }
252 if (kAccessCheck) {
253 mirror::Class* referrer = method->GetDeclaringClass();
254 if (UNLIKELY(!referrer->CanAccess(klass))) {
255 ThrowIllegalAccessErrorClass(referrer, klass);
256 return nullptr; // Failure
257 }
258 }
259 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
260 // suspension.
261 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700262 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700263}
264
265template<FindFieldType type, bool access_check>
266static inline mirror::ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
267 Thread* self, size_t expected_size) {
268 bool is_primitive;
269 bool is_set;
270 bool is_static;
271 switch (type) {
272 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
273 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
274 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
275 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
276 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
277 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
278 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
279 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
280 default: is_primitive = true; is_set = true; is_static = true; break;
281 }
282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
283 mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
284 if (UNLIKELY(resolved_field == nullptr)) {
285 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
286 return nullptr; // Failure.
287 }
288 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
289 if (access_check) {
290 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
291 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
292 return nullptr;
293 }
294 mirror::Class* referring_class = referrer->GetDeclaringClass();
295 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
296 field_idx))) {
297 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
298 return nullptr; // Failure.
299 }
300 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
301 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
302 return nullptr; // Failure.
303 } else {
304 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
305 resolved_field->FieldSize() != expected_size)) {
306 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
307 DCHECK(throw_location.GetMethod() == referrer);
308 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
309 "Attempted read of %zd-bit %s on field '%s'",
310 expected_size * (32 / sizeof(int32_t)),
311 is_primitive ? "primitive" : "non-primitive",
312 PrettyField(resolved_field, true).c_str());
313 return nullptr; // Failure.
314 }
315 }
316 }
317 if (!is_static) {
318 // instance fields must be being accessed on an initialized class
319 return resolved_field;
320 } else {
321 // If the class is initialized we're done.
322 if (LIKELY(fields_class->IsInitialized())) {
323 return resolved_field;
324 } else {
325 StackHandleScope<1> hs(self);
326 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700327 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700328 // Otherwise let's ensure the class is initialized before resolving the field.
329 return resolved_field;
330 }
331 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
332 return nullptr; // Failure.
333 }
334 }
335}
336
337// Explicit template declarations of FindFieldFromCode for all field access types.
338#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
339template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
340mirror::ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
341 mirror::ArtMethod* referrer, \
342 Thread* self, size_t expected_size) \
343
344#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
345 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
346 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
347
348EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
349EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
350EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
351EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
352EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
353EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
354EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
355EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
356
357#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
358#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
359
360template<InvokeType type, bool access_check>
361static inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
362 mirror::Object** this_object,
363 mirror::ArtMethod** referrer, Thread* self) {
364 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
365 mirror::ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer, type);
366 if (resolved_method == nullptr) {
367 StackHandleScope<1> hs(self);
368 mirror::Object* null_this = nullptr;
369 HandleWrapper<mirror::Object> h_this(
370 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
371 resolved_method = class_linker->ResolveMethod(self, method_idx, referrer, type);
372 }
373 if (UNLIKELY(resolved_method == nullptr)) {
374 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
375 return nullptr; // Failure.
376 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
377 // Maintain interpreter-like semantics where NullPointerException is thrown
378 // after potential NoSuchMethodError from class linker.
379 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
380 DCHECK_EQ(*referrer, throw_location.GetMethod());
381 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
382 return nullptr; // Failure.
383 } else if (access_check) {
384 // Incompatible class change should have been handled in resolve method.
385 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
386 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
387 *referrer);
388 return nullptr; // Failure.
389 }
390 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
391 mirror::Class* referring_class = (*referrer)->GetDeclaringClass();
392 bool can_access_resolved_method =
393 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
394 method_idx);
395 if (UNLIKELY(!can_access_resolved_method)) {
396 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
397 return nullptr; // Failure.
398 }
399 }
400 switch (type) {
401 case kStatic:
402 case kDirect:
403 return resolved_method;
404 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700405 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700406 uint16_t vtable_index = resolved_method->GetMethodIndex();
407 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700408 (!klass->HasVTable() ||
409 vtable_index >= static_cast<uint32_t>(klass->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 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700415 DCHECK(klass->HasVTable()) << PrettyClass(klass);
416 return klass->GetVTableEntry(vtable_index);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700417 }
418 case kSuper: {
419 mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass();
420 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700421 if (access_check) {
422 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700423 if (super_class == nullptr || !super_class->HasVTable() ||
424 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700425 // Behavior to agree with that of the verifier.
426 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
427 resolved_method->GetName(), resolved_method->GetSignature());
428 return nullptr; // Failure.
429 }
430 } else {
431 // Super class must exist.
432 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700433 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700434 DCHECK(super_class->HasVTable());
435 return super_class->GetVTableEntry(vtable_index);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700436 }
437 case kInterface: {
438 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
439 mirror::ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(imt_index);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700440 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
441 if (kIsDebugBuild) {
442 mirror::Class* klass = (*this_object)->GetClass();
443 mirror::ArtMethod* method = klass->FindVirtualMethodForInterface(resolved_method);
444 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
445 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
446 PrettyClass(klass);
447 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700448 return imt_method;
449 } else {
450 mirror::ArtMethod* interface_method =
451 (*this_object)->GetClass()->FindVirtualMethodForInterface(resolved_method);
452 if (UNLIKELY(interface_method == nullptr)) {
453 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
454 *this_object, *referrer);
455 return nullptr; // Failure.
456 }
457 return interface_method;
458 }
459 }
460 default:
461 LOG(FATAL) << "Unknown invoke type " << type;
462 return nullptr; // Failure.
463 }
464}
465
466// Explicit template declarations of FindMethodFromCode for all invoke types.
467#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
468 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
469 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
470 mirror::Object** this_object, \
471 mirror::ArtMethod** referrer, \
472 Thread* self)
473#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
474 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
475 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
476
477EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
478EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
479EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
480EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
481EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
482
483#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
484#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
485
486// Fast path field resolution that can't initialize classes or throw exceptions.
487static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
488 mirror::ArtMethod* referrer,
Ian Rogerse5877a12014-07-16 12:06:35 -0700489 FindFieldType type, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700490 mirror::ArtField* resolved_field =
491 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
492 if (UNLIKELY(resolved_field == nullptr)) {
493 return nullptr;
494 }
495 // Check for incompatible class change.
496 bool is_primitive;
497 bool is_set;
498 bool is_static;
499 switch (type) {
500 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
501 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
502 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
503 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
504 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
505 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
506 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
507 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
508 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700509 LOG(FATAL) << "UNREACHABLE";
510 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700511 }
512 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
513 // Incompatible class change.
514 return nullptr;
515 }
516 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
517 if (is_static) {
518 // Check class is initialized else fail so that we can contend to initialize the class with
519 // other threads that may be racing to do this.
520 if (UNLIKELY(!fields_class->IsInitialized())) {
521 return nullptr;
522 }
523 }
524 mirror::Class* referring_class = referrer->GetDeclaringClass();
525 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
526 !referring_class->CanAccessMember(fields_class,
527 resolved_field->GetAccessFlags()) ||
528 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
529 // Illegal access.
530 return nullptr;
531 }
532 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
533 resolved_field->FieldSize() != expected_size)) {
534 return nullptr;
535 }
536 return resolved_field;
537}
538
539// Fast path method resolution that can't throw exceptions.
540static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
541 mirror::Object* this_object,
542 mirror::ArtMethod* referrer,
Ian Rogerse5877a12014-07-16 12:06:35 -0700543 bool access_check, InvokeType type) {
Jeff Hao207a37d2014-10-29 17:24:25 -0700544 if (UNLIKELY(this_object == NULL && type != kStatic)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700545 return NULL;
546 }
547 mirror::ArtMethod* resolved_method =
548 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
549 if (UNLIKELY(resolved_method == NULL)) {
550 return NULL;
551 }
552 if (access_check) {
553 // Check for incompatible class change errors and access.
554 bool icce = resolved_method->CheckIncompatibleClassChange(type);
555 if (UNLIKELY(icce)) {
556 return NULL;
557 }
558 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
559 mirror::Class* referring_class = referrer->GetDeclaringClass();
560 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
561 !referring_class->CanAccessMember(methods_class,
562 resolved_method->GetAccessFlags()))) {
563 // Potential illegal access, may need to refine the method's class.
564 return NULL;
565 }
566 }
567 if (type == kInterface) { // Most common form of slow path dispatch.
568 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao207a37d2014-10-29 17:24:25 -0700569 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700570 return resolved_method;
571 } else if (type == kSuper) {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700572 return referrer->GetDeclaringClass()->GetSuperClass()
573 ->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700574 } else {
575 DCHECK(type == kVirtual);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700576 return this_object->GetClass()->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700577 }
578}
579
580static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
581 mirror::ArtMethod* referrer,
582 Thread* self, bool can_run_clinit,
Ian Rogerse5877a12014-07-16 12:06:35 -0700583 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700584 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
585 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
586 if (UNLIKELY(klass == nullptr)) {
587 CHECK(self->IsExceptionPending());
588 return nullptr; // Failure - Indicate to caller to deliver exception
589 }
590 // Perform access check if necessary.
591 mirror::Class* referring_class = referrer->GetDeclaringClass();
592 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
593 ThrowIllegalAccessErrorClass(referring_class, klass);
594 return nullptr; // Failure - Indicate to caller to deliver exception
595 }
596 // If we're just implementing const-class, we shouldn't call <clinit>.
597 if (!can_run_clinit) {
598 return klass;
599 }
600 // If we are the <clinit> of this class, just return our storage.
601 //
602 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
603 // running.
604 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
605 return klass;
606 }
607 StackHandleScope<1> hs(self);
608 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700609 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700610 CHECK(self->IsExceptionPending());
611 return nullptr; // Failure - Indicate to caller to deliver exception
612 }
613 return h_class.Get();
614}
615
616static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogerse5877a12014-07-16 12:06:35 -0700617 uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700618 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
619 return class_linker->ResolveString(string_idx, referrer);
620}
621
Ian Rogerse5877a12014-07-16 12:06:35 -0700622static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700623 // Save any pending exception over monitor exit call.
624 mirror::Throwable* saved_exception = NULL;
625 ThrowLocation saved_throw_location;
626 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
627 if (UNLIKELY(self->IsExceptionPending())) {
628 saved_exception = self->GetException(&saved_throw_location);
629 self->ClearException();
630 }
631 // Decode locked object and unlock, before popping local references.
632 self->DecodeJObject(locked)->MonitorExit(self);
633 if (UNLIKELY(self->IsExceptionPending())) {
634 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
635 << saved_exception->Dump()
636 << "\nEncountered second exception during implicit MonitorExit:\n"
637 << self->GetException(NULL)->Dump();
638 }
639 // Restore pending exception.
640 if (saved_exception != NULL) {
641 self->SetException(saved_throw_location, saved_exception);
642 self->SetExceptionReportedToInstrumentation(is_exception_reported);
643 }
644}
645
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700646template <typename INT_TYPE, typename FLOAT_TYPE>
647static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
648 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
649 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
650 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
651 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
652 if (LIKELY(f > kMinIntAsFloat)) {
653 if (LIKELY(f < kMaxIntAsFloat)) {
654 return static_cast<INT_TYPE>(f);
655 } else {
656 return kMaxInt;
657 }
658 } else {
659 return (f != f) ? 0 : kMinInt; // f != f implies NaN
660 }
661}
662
663} // namespace art
664
665#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_