blob: 2956ea7315f06cb3b2164cad826549443a3ba257 [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
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed)
48 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070049void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer,
50 Class* accessed,
51 const Method* caller,
52 const Method* called,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 InvokeType type)
54 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070055void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
56 const Method* referrer,
57 const Method* interface_method,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 Object* this_object)
59 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers08f753d2012-08-24 14:35:25 -070060void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Ian Rogers2fc14272012-08-30 10:56:57 -070061 Method* method, const Method* referrer)
Ian Rogers08f753d2012-08-24 14:35:25 -070062 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
63void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name,
Ian Rogers2fc14272012-08-30 10:56:57 -070064 const StringPiece& signature, const Method* referrer)
Ian Rogers08f753d2012-08-24 14:35:25 -070065 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed)
67 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
68void ThrowNewIllegalAccessErrorFinalField(Thread* self, const Method* referrer, Field* accessed)
69 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed)
71 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
72void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read)
73 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070074void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller, uint32_t method_idx,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 InvokeType type)
76 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
77void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* caller, uint32_t dex_pc)
78 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070079
80std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 verifier::VerifyErrorRefType ref_type, bool access)
82 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070083std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084 verifier::VerifyErrorRefType ref_type, bool access)
85 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070086
87// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
88// cannot be resolved, throw an error. If it can, use it to create an instance.
89// When verification/compiler hasn't been able to verify access, optionally perform an access
90// check.
91static inline Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 bool access_check)
93 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070094 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
95 Runtime* runtime = Runtime::Current();
96 if (UNLIKELY(klass == NULL)) {
97 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
98 if (klass == NULL) {
99 DCHECK(self->IsExceptionPending());
100 return NULL; // Failure
101 }
102 }
103 if (access_check) {
104 if (UNLIKELY(!klass->IsInstantiable())) {
105 self->ThrowNewException("Ljava/lang/InstantiationError;",
106 PrettyDescriptor(klass).c_str());
107 return NULL; // Failure
108 }
109 Class* referrer = method->GetDeclaringClass();
110 if (UNLIKELY(!referrer->CanAccess(klass))) {
111 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
112 return NULL; // Failure
113 }
114 }
Ian Rogers0045a292012-03-31 21:08:41 -0700115 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700116 DCHECK(self->IsExceptionPending());
117 return NULL; // Failure
118 }
119 return klass->AllocObject();
120}
121
122// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
123// it cannot be resolved, throw an error. If it can, use it to create an array.
124// When verification/compiler hasn't been able to verify access, optionally perform an access
125// check.
126static inline Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 Thread* self, bool access_check)
128 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700129 if (UNLIKELY(component_count < 0)) {
130 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
131 component_count);
132 return NULL; // Failure
133 }
134 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
135 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
136 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
137 if (klass == NULL) { // Error
138 DCHECK(Thread::Current()->IsExceptionPending());
139 return NULL; // Failure
140 }
141 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
142 }
143 if (access_check) {
144 Class* referrer = method->GetDeclaringClass();
145 if (UNLIKELY(!referrer->CanAccess(klass))) {
146 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
147 return NULL; // Failure
148 }
149 }
150 return Array::Alloc(klass, component_count);
151}
152
Ian Rogersce9eca62011-10-07 17:11:03 -0700153extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154 Thread* self, bool access_check)
155 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700156
Ian Rogers08f753d2012-08-24 14:35:25 -0700157// Type of find field operation for fast and slow case.
158enum FindFieldType {
159 InstanceObjectRead,
160 InstanceObjectWrite,
161 InstancePrimitiveRead,
162 InstancePrimitiveWrite,
163 StaticObjectRead,
164 StaticObjectWrite,
165 StaticPrimitiveRead,
166 StaticPrimitiveWrite,
167};
168
169// Slow field find that can initialize classes and may throw exceptions.
Ian Rogers1bddec32012-02-04 12:27:34 -0800170extern Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700171 FindFieldType type, size_t expected_size)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700173
Ian Rogers08f753d2012-08-24 14:35:25 -0700174// Fast path field resolution that can't initialize classes or throw exceptions.
175static inline Field* FindFieldFast(uint32_t field_idx, const Method* referrer,
176 FindFieldType type, size_t expected_size)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700178 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
179 if (UNLIKELY(resolved_field == NULL)) {
180 return NULL;
181 }
182 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700183 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700184 if (UNLIKELY(!fields_class->IsInitializing())) {
185 return NULL;
186 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700187 // Check for incompatible class change.
188 bool is_primitive;
189 bool is_set;
190 bool is_static;
191 switch (type) {
192 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
193 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
194 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
195 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
196 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
197 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
198 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
199 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
200 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
201 is_primitive = true; is_set = true; is_static = true; break;
202 }
203 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
204 // Incompatible class change.
205 return NULL;
206 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700207 Class* referring_class = referrer->GetDeclaringClass();
208 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
209 !referring_class->CanAccessMember(fields_class,
210 resolved_field->GetAccessFlags()) ||
211 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700212 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700213 return NULL;
214 }
215 FieldHelper fh(resolved_field);
216 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
217 fh.FieldSize() != expected_size)) {
218 return NULL;
219 }
220 return resolved_field;
221}
222
Ian Rogers08f753d2012-08-24 14:35:25 -0700223// Fast path method resolution that can't throw exceptions.
224static inline Method* FindMethodFast(uint32_t method_idx, Object* this_object,
225 const Method* referrer, bool access_check, InvokeType type)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700227 bool is_direct = type == kStatic || type == kDirect;
228 if (UNLIKELY(this_object == NULL && !is_direct)) {
229 return NULL;
230 }
231 Method* resolved_method =
232 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
233 if (UNLIKELY(resolved_method == NULL)) {
234 return NULL;
235 }
236 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700237 // Check for incompatible class change errors and access.
238 bool icce = resolved_method->CheckIncompatibleClassChange(type);
239 if (UNLIKELY(icce)) {
240 return NULL;
241 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700242 Class* methods_class = resolved_method->GetDeclaringClass();
243 Class* referring_class = referrer->GetDeclaringClass();
244 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
245 !referring_class->CanAccessMember(methods_class,
246 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700247 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700248 return NULL;
249 }
250 }
251 if (type == kInterface) { // Most common form of slow path dispatch.
252 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
253 } else if (is_direct) {
254 return resolved_method;
255 } else if (type == kSuper) {
256 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
257 Get(resolved_method->GetMethodIndex());
258 } else {
259 DCHECK(type == kVirtual);
260 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
261 }
262}
263
264extern Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 Thread* self, bool access_check, InvokeType type)
266 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700267
Elliott Hughesf3778f62012-01-26 14:14:35 -0800268extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 bool can_run_clinit, bool verify_access)
270 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272static inline String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx)
273 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700274 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
275 return class_linker->ResolveString(string_idx, referrer);
276}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700277
TDYa1273d71d802012-08-15 03:47:03 -0700278static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
279 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
280 UNLOCK_FUNCTION(monitor_lock_) {
281 // Save any pending exception over monitor exit call.
282 Throwable* saved_exception = NULL;
283 if (UNLIKELY(self->IsExceptionPending())) {
284 saved_exception = self->GetException();
285 self->ClearException();
286 }
287 // Decode locked object and unlock, before popping local references.
288 self->DecodeJObject(locked)->MonitorExit(self);
289 if (UNLIKELY(self->IsExceptionPending())) {
290 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
291 << saved_exception->Dump()
292 << "\nEncountered second exception during implicit MonitorExit:\n"
293 << self->GetException()->Dump();
294 }
295 // Restore pending exception.
296 if (saved_exception != NULL) {
297 self->SetException(saved_exception);
298 }
299}
300
301static inline void CheckReferenceResult(Object* o, Thread* self)
302 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
303 if (o == NULL) {
304 return;
305 }
306 if (o == kInvalidIndirectRefObject) {
307 JniAbortF(NULL, "invalid reference returned from %s",
308 PrettyMethod(self->GetCurrentMethod()).c_str());
309 }
310 // Make sure that the result is an instance of the type this method was expected to return.
311 Method* m = self->GetCurrentMethod();
312 MethodHelper mh(m);
313 Class* return_type = mh.GetReturnType();
314
315 if (!o->InstanceOf(return_type)) {
316 JniAbortF(NULL, "attempt to return an instance of %s from %s",
317 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
318 }
319}
320
Shih-wei Liao2d831012011-09-28 22:06:53 -0700321} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700322
buzbee54330722011-08-23 16:46:55 -0700323#endif // ART_SRC_RUNTIME_SUPPORT_H_