blob: 50b6735bfee9c2043078af9308249b9193f11ff0 [file] [log] [blame]
Elliott Hughes0f3c5532012-03-30 14:51:51 -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 */
buzbee54330722011-08-23 16:46:55 -070016
17#ifndef ART_SRC_RUNTIME_SUPPORT_H_
18#define ART_SRC_RUNTIME_SUPPORT_H_
19
Shih-wei Liao2d831012011-09-28 22:06:53 -070020#include "class_linker.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070021#include "dex_file.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070022#include "invoke_type.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -070023#include "object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070024#include "object_utils.h"
25#include "thread.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070026#include "verifier/method_verifier.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070027
28extern "C" void art_proxy_invoke_handler();
29extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070030
jeffhao41005dd2012-05-09 17:58:52 -070031extern "C" double art_l2d(int64_t l);
32extern "C" float art_l2f(int64_t l);
33extern "C" int64_t art_d2l(double d);
34extern "C" int32_t art_d2i(double d);
35extern "C" int64_t art_f2l(float f);
36extern "C" int32_t art_f2i(float f);
37
Shih-wei Liao2d831012011-09-28 22:06:53 -070038namespace art {
39
Ian Rogers57b86d42012-03-27 16:05:41 -070040class Array;
41class Class;
42class Field;
43class Method;
44class Object;
45
46// Helpers to give consistent descriptive exception messages
47void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed);
48void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer,
49 Class* accessed,
50 const Method* caller,
51 const Method* called,
52 InvokeType type);
53void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
54 const Method* referrer,
55 const Method* interface_method,
56 Object* this_object);
57void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed);
58void ThrowNewIllegalAccessErrorFinalField(Thread* self, const Method* referrer, Field* accessed);
59
60void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed);
61void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read);
62void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller, uint32_t method_idx,
63 InvokeType type);
TDYa1273f9137d2012-04-08 15:59:19 -070064void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* caller, uint32_t dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +080065void ThrowVerificationError(Thread* self, const Method* method, int32_t kind, int32_t ref);
Ian Rogers57b86d42012-03-27 16:05:41 -070066
67std::string FieldNameFromIndex(const Method* method, uint32_t ref,
68 verifier::VerifyErrorRefType ref_type, bool access);
69std::string MethodNameFromIndex(const Method* method, uint32_t ref,
70 verifier::VerifyErrorRefType ref_type, bool access);
71
72// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
73// cannot be resolved, throw an error. If it can, use it to create an instance.
74// When verification/compiler hasn't been able to verify access, optionally perform an access
75// check.
76static inline Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
77 bool access_check) {
78 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
79 Runtime* runtime = Runtime::Current();
80 if (UNLIKELY(klass == NULL)) {
81 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
82 if (klass == NULL) {
83 DCHECK(self->IsExceptionPending());
84 return NULL; // Failure
85 }
86 }
87 if (access_check) {
88 if (UNLIKELY(!klass->IsInstantiable())) {
89 self->ThrowNewException("Ljava/lang/InstantiationError;",
90 PrettyDescriptor(klass).c_str());
91 return NULL; // Failure
92 }
93 Class* referrer = method->GetDeclaringClass();
94 if (UNLIKELY(!referrer->CanAccess(klass))) {
95 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
96 return NULL; // Failure
97 }
98 }
Ian Rogers0045a292012-03-31 21:08:41 -070099 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700100 DCHECK(self->IsExceptionPending());
101 return NULL; // Failure
102 }
103 return klass->AllocObject();
104}
105
106// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
107// it cannot be resolved, throw an error. If it can, use it to create an array.
108// When verification/compiler hasn't been able to verify access, optionally perform an access
109// check.
110static inline Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
111 Thread* self, bool access_check) {
112 if (UNLIKELY(component_count < 0)) {
113 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
114 component_count);
115 return NULL; // Failure
116 }
117 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
118 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
119 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
120 if (klass == NULL) { // Error
121 DCHECK(Thread::Current()->IsExceptionPending());
122 return NULL; // Failure
123 }
124 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
125 }
126 if (access_check) {
127 Class* referrer = method->GetDeclaringClass();
128 if (UNLIKELY(!referrer->CanAccess(klass))) {
129 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
130 return NULL; // Failure
131 }
132 }
133 return Array::Alloc(klass, component_count);
134}
135
Ian Rogersce9eca62011-10-07 17:11:03 -0700136extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800137 Thread* self, bool access_check);
Ian Rogers57b86d42012-03-27 16:05:41 -0700138
Ian Rogers1bddec32012-02-04 12:27:34 -0800139extern Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
Ian Rogers57b86d42012-03-27 16:05:41 -0700140 bool is_static, bool is_primitive, bool is_set,
141 size_t expected_size);
142
143// Fast path field resolution that can't throw exceptions
144static inline Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
145 size_t expected_size, bool is_set) {
146 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
147 if (UNLIKELY(resolved_field == NULL)) {
148 return NULL;
149 }
150 Class* fields_class = resolved_field->GetDeclaringClass();
151 // Check class is initiliazed or initializing
152 if (UNLIKELY(!fields_class->IsInitializing())) {
153 return NULL;
154 }
155 Class* referring_class = referrer->GetDeclaringClass();
156 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
157 !referring_class->CanAccessMember(fields_class,
158 resolved_field->GetAccessFlags()) ||
159 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
160 // illegal access
161 return NULL;
162 }
163 FieldHelper fh(resolved_field);
164 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
165 fh.FieldSize() != expected_size)) {
166 return NULL;
167 }
168 return resolved_field;
169}
170
171// Fast path method resolution that can't throw exceptions
172static inline Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
173 bool access_check, InvokeType type) {
174 bool is_direct = type == kStatic || type == kDirect;
175 if (UNLIKELY(this_object == NULL && !is_direct)) {
176 return NULL;
177 }
178 Method* resolved_method =
179 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
180 if (UNLIKELY(resolved_method == NULL)) {
181 return NULL;
182 }
183 if (access_check) {
184 Class* methods_class = resolved_method->GetDeclaringClass();
185 Class* referring_class = referrer->GetDeclaringClass();
186 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
187 !referring_class->CanAccessMember(methods_class,
188 resolved_method->GetAccessFlags()))) {
189 // potential illegal access
190 return NULL;
191 }
192 }
193 if (type == kInterface) { // Most common form of slow path dispatch.
194 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
195 } else if (is_direct) {
196 return resolved_method;
197 } else if (type == kSuper) {
198 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
199 Get(resolved_method->GetMethodIndex());
200 } else {
201 DCHECK(type == kVirtual);
202 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
203 }
204}
205
206extern Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
207 Thread* self, bool access_check, InvokeType type);
208
Elliott Hughesf3778f62012-01-26 14:14:35 -0800209extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
210 bool can_run_clinit, bool verify_access);
Ian Rogers57b86d42012-03-27 16:05:41 -0700211
212static inline String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
213 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
214 return class_linker->ResolveString(string_idx, referrer);
215}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700216
TDYa1275bb86012012-04-11 05:57:28 -0700217extern void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception);
218
Shih-wei Liao2d831012011-09-28 22:06:53 -0700219} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700220
buzbee54330722011-08-23 16:46:55 -0700221#endif // ART_SRC_RUNTIME_SUPPORT_H_