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