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