blob: ccc5d835c90dea920bd8452036d3374e5bbc267f [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);
440 if (!imt_method->IsImtConflictMethod()) {
441 return imt_method;
442 } else {
443 mirror::ArtMethod* interface_method =
444 (*this_object)->GetClass()->FindVirtualMethodForInterface(resolved_method);
445 if (UNLIKELY(interface_method == nullptr)) {
446 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
447 *this_object, *referrer);
448 return nullptr; // Failure.
449 }
450 return interface_method;
451 }
452 }
453 default:
454 LOG(FATAL) << "Unknown invoke type " << type;
455 return nullptr; // Failure.
456 }
457}
458
459// Explicit template declarations of FindMethodFromCode for all invoke types.
460#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
461 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
462 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
463 mirror::Object** this_object, \
464 mirror::ArtMethod** referrer, \
465 Thread* self)
466#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
467 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
468 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
469
470EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
471EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
472EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
473EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
474EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
475
476#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
477#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
478
479// Fast path field resolution that can't initialize classes or throw exceptions.
480static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
481 mirror::ArtMethod* referrer,
Ian Rogerse5877a12014-07-16 12:06:35 -0700482 FindFieldType type, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700483 mirror::ArtField* resolved_field =
484 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
485 if (UNLIKELY(resolved_field == nullptr)) {
486 return nullptr;
487 }
488 // Check for incompatible class change.
489 bool is_primitive;
490 bool is_set;
491 bool is_static;
492 switch (type) {
493 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
494 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
495 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
496 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
497 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
498 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
499 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
500 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
501 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700502 LOG(FATAL) << "UNREACHABLE";
503 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700504 }
505 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
506 // Incompatible class change.
507 return nullptr;
508 }
509 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
510 if (is_static) {
511 // Check class is initialized else fail so that we can contend to initialize the class with
512 // other threads that may be racing to do this.
513 if (UNLIKELY(!fields_class->IsInitialized())) {
514 return nullptr;
515 }
516 }
517 mirror::Class* referring_class = referrer->GetDeclaringClass();
518 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
519 !referring_class->CanAccessMember(fields_class,
520 resolved_field->GetAccessFlags()) ||
521 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
522 // Illegal access.
523 return nullptr;
524 }
525 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
526 resolved_field->FieldSize() != expected_size)) {
527 return nullptr;
528 }
529 return resolved_field;
530}
531
532// Fast path method resolution that can't throw exceptions.
533static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
534 mirror::Object* this_object,
535 mirror::ArtMethod* referrer,
Ian Rogerse5877a12014-07-16 12:06:35 -0700536 bool access_check, InvokeType type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700537 bool is_direct = type == kStatic || type == kDirect;
538 if (UNLIKELY(this_object == NULL && !is_direct)) {
539 return NULL;
540 }
541 mirror::ArtMethod* resolved_method =
542 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
543 if (UNLIKELY(resolved_method == NULL)) {
544 return NULL;
545 }
546 if (access_check) {
547 // Check for incompatible class change errors and access.
548 bool icce = resolved_method->CheckIncompatibleClassChange(type);
549 if (UNLIKELY(icce)) {
550 return NULL;
551 }
552 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
553 mirror::Class* referring_class = referrer->GetDeclaringClass();
554 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
555 !referring_class->CanAccessMember(methods_class,
556 resolved_method->GetAccessFlags()))) {
557 // Potential illegal access, may need to refine the method's class.
558 return NULL;
559 }
560 }
561 if (type == kInterface) { // Most common form of slow path dispatch.
562 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
563 } else if (is_direct) {
564 return resolved_method;
565 } else if (type == kSuper) {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700566 return referrer->GetDeclaringClass()->GetSuperClass()
567 ->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700568 } else {
569 DCHECK(type == kVirtual);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700570 return this_object->GetClass()->GetVTableEntry(resolved_method->GetMethodIndex());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700571 }
572}
573
574static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
575 mirror::ArtMethod* referrer,
576 Thread* self, bool can_run_clinit,
Ian Rogerse5877a12014-07-16 12:06:35 -0700577 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700578 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
579 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
580 if (UNLIKELY(klass == nullptr)) {
581 CHECK(self->IsExceptionPending());
582 return nullptr; // Failure - Indicate to caller to deliver exception
583 }
584 // Perform access check if necessary.
585 mirror::Class* referring_class = referrer->GetDeclaringClass();
586 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
587 ThrowIllegalAccessErrorClass(referring_class, klass);
588 return nullptr; // Failure - Indicate to caller to deliver exception
589 }
590 // If we're just implementing const-class, we shouldn't call <clinit>.
591 if (!can_run_clinit) {
592 return klass;
593 }
594 // If we are the <clinit> of this class, just return our storage.
595 //
596 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
597 // running.
598 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
599 return klass;
600 }
601 StackHandleScope<1> hs(self);
602 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700603 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700604 CHECK(self->IsExceptionPending());
605 return nullptr; // Failure - Indicate to caller to deliver exception
606 }
607 return h_class.Get();
608}
609
610static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogerse5877a12014-07-16 12:06:35 -0700611 uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700612 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
613 return class_linker->ResolveString(string_idx, referrer);
614}
615
Ian Rogerse5877a12014-07-16 12:06:35 -0700616static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700617 // Save any pending exception over monitor exit call.
618 mirror::Throwable* saved_exception = NULL;
619 ThrowLocation saved_throw_location;
620 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
621 if (UNLIKELY(self->IsExceptionPending())) {
622 saved_exception = self->GetException(&saved_throw_location);
623 self->ClearException();
624 }
625 // Decode locked object and unlock, before popping local references.
626 self->DecodeJObject(locked)->MonitorExit(self);
627 if (UNLIKELY(self->IsExceptionPending())) {
628 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
629 << saved_exception->Dump()
630 << "\nEncountered second exception during implicit MonitorExit:\n"
631 << self->GetException(NULL)->Dump();
632 }
633 // Restore pending exception.
634 if (saved_exception != NULL) {
635 self->SetException(saved_throw_location, saved_exception);
636 self->SetExceptionReportedToInstrumentation(is_exception_reported);
637 }
638}
639
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700640template <typename INT_TYPE, typename FLOAT_TYPE>
641static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
642 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
643 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
644 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
645 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
646 if (LIKELY(f > kMinIntAsFloat)) {
647 if (LIKELY(f < kMaxIntAsFloat)) {
648 return static_cast<INT_TYPE>(f);
649 } else {
650 return kMaxInt;
651 }
652 } else {
653 return (f != f) ? 0 : kMinInt; // f != f implies NaN
654 }
655}
656
657} // namespace art
658
659#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_