blob: 909fb41e59e61dfd1841a0c9bbb80a5ff3a46172 [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001// Copyright 2012 Google Inc. All Rights Reserved.
buzbee54330722011-08-23 16:46:55 -07002
3#ifndef ART_SRC_RUNTIME_SUPPORT_H_
4#define ART_SRC_RUNTIME_SUPPORT_H_
5
Shih-wei Liao2d831012011-09-28 22:06:53 -07006#include "class_linker.h"
Ian Rogers57b86d42012-03-27 16:05:41 -07007#include "dex_file.h"
8#include "dex_verifier.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -07009#include "object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070010#include "object_utils.h"
11#include "thread.h"
12
13extern "C" void art_proxy_invoke_handler();
14extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070015
16namespace art {
17
Ian Rogers57b86d42012-03-27 16:05:41 -070018class Array;
19class Class;
20class Field;
21class Method;
22class Object;
23
24// Helpers to give consistent descriptive exception messages
25void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed);
26void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer,
27 Class* accessed,
28 const Method* caller,
29 const Method* called,
30 InvokeType type);
31void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
32 const Method* referrer,
33 const Method* interface_method,
34 Object* this_object);
35void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed);
36void ThrowNewIllegalAccessErrorFinalField(Thread* self, const Method* referrer, Field* accessed);
37
38void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed);
39void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read);
40void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller, uint32_t method_idx,
41 InvokeType type);
42
43std::string FieldNameFromIndex(const Method* method, uint32_t ref,
44 verifier::VerifyErrorRefType ref_type, bool access);
45std::string MethodNameFromIndex(const Method* method, uint32_t ref,
46 verifier::VerifyErrorRefType ref_type, bool access);
47
48// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
49// cannot be resolved, throw an error. If it can, use it to create an instance.
50// When verification/compiler hasn't been able to verify access, optionally perform an access
51// check.
52static inline Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
53 bool access_check) {
54 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
55 Runtime* runtime = Runtime::Current();
56 if (UNLIKELY(klass == NULL)) {
57 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
58 if (klass == NULL) {
59 DCHECK(self->IsExceptionPending());
60 return NULL; // Failure
61 }
62 }
63 if (access_check) {
64 if (UNLIKELY(!klass->IsInstantiable())) {
65 self->ThrowNewException("Ljava/lang/InstantiationError;",
66 PrettyDescriptor(klass).c_str());
67 return NULL; // Failure
68 }
69 Class* referrer = method->GetDeclaringClass();
70 if (UNLIKELY(!referrer->CanAccess(klass))) {
71 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
72 return NULL; // Failure
73 }
74 }
75 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
76 DCHECK(self->IsExceptionPending());
77 return NULL; // Failure
78 }
79 return klass->AllocObject();
80}
81
82// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
83// it cannot be resolved, throw an error. If it can, use it to create an array.
84// When verification/compiler hasn't been able to verify access, optionally perform an access
85// check.
86static inline Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
87 Thread* self, bool access_check) {
88 if (UNLIKELY(component_count < 0)) {
89 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
90 component_count);
91 return NULL; // Failure
92 }
93 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
94 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
95 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
96 if (klass == NULL) { // Error
97 DCHECK(Thread::Current()->IsExceptionPending());
98 return NULL; // Failure
99 }
100 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
101 }
102 if (access_check) {
103 Class* referrer = method->GetDeclaringClass();
104 if (UNLIKELY(!referrer->CanAccess(klass))) {
105 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
106 return NULL; // Failure
107 }
108 }
109 return Array::Alloc(klass, component_count);
110}
111
Ian Rogersce9eca62011-10-07 17:11:03 -0700112extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800113 Thread* self, bool access_check);
Ian Rogers57b86d42012-03-27 16:05:41 -0700114
Ian Rogers1bddec32012-02-04 12:27:34 -0800115extern Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
Ian Rogers57b86d42012-03-27 16:05:41 -0700116 bool is_static, bool is_primitive, bool is_set,
117 size_t expected_size);
118
119// Fast path field resolution that can't throw exceptions
120static inline Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
121 size_t expected_size, bool is_set) {
122 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
123 if (UNLIKELY(resolved_field == NULL)) {
124 return NULL;
125 }
126 Class* fields_class = resolved_field->GetDeclaringClass();
127 // Check class is initiliazed or initializing
128 if (UNLIKELY(!fields_class->IsInitializing())) {
129 return NULL;
130 }
131 Class* referring_class = referrer->GetDeclaringClass();
132 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
133 !referring_class->CanAccessMember(fields_class,
134 resolved_field->GetAccessFlags()) ||
135 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
136 // illegal access
137 return NULL;
138 }
139 FieldHelper fh(resolved_field);
140 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
141 fh.FieldSize() != expected_size)) {
142 return NULL;
143 }
144 return resolved_field;
145}
146
147// Fast path method resolution that can't throw exceptions
148static inline Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
149 bool access_check, InvokeType type) {
150 bool is_direct = type == kStatic || type == kDirect;
151 if (UNLIKELY(this_object == NULL && !is_direct)) {
152 return NULL;
153 }
154 Method* resolved_method =
155 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
156 if (UNLIKELY(resolved_method == NULL)) {
157 return NULL;
158 }
159 if (access_check) {
160 Class* methods_class = resolved_method->GetDeclaringClass();
161 Class* referring_class = referrer->GetDeclaringClass();
162 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
163 !referring_class->CanAccessMember(methods_class,
164 resolved_method->GetAccessFlags()))) {
165 // potential illegal access
166 return NULL;
167 }
168 }
169 if (type == kInterface) { // Most common form of slow path dispatch.
170 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
171 } else if (is_direct) {
172 return resolved_method;
173 } else if (type == kSuper) {
174 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
175 Get(resolved_method->GetMethodIndex());
176 } else {
177 DCHECK(type == kVirtual);
178 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
179 }
180}
181
182extern Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
183 Thread* self, bool access_check, InvokeType type);
184
Elliott Hughesf3778f62012-01-26 14:14:35 -0800185extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
186 bool can_run_clinit, bool verify_access);
Ian Rogers57b86d42012-03-27 16:05:41 -0700187
188static inline String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
189 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
190 return class_linker->ResolveString(string_idx, referrer);
191}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700192
193} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700194
buzbee54330722011-08-23 16:46:55 -0700195#endif // ART_SRC_RUNTIME_SUPPORT_H_