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