blob: adeedb743975817468ad8454353eedc14ee6cf3d [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 Rogers87e552d2012-08-31 15:54:48 -070021#include "common_throws.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070022#include "dex_file.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070023#include "indirect_reference_table.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024#include "invoke_type.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070025#include "jni_internal.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -070026#include "object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070027#include "object_utils.h"
28#include "thread.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070029#include "verifier/method_verifier.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070030
31extern "C" void art_proxy_invoke_handler();
32extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070033
jeffhao41005dd2012-05-09 17:58:52 -070034extern "C" double art_l2d(int64_t l);
35extern "C" float art_l2f(int64_t l);
36extern "C" int64_t art_d2l(double d);
37extern "C" int32_t art_d2i(double d);
38extern "C" int64_t art_f2l(float f);
39extern "C" int32_t art_f2i(float f);
40
Shih-wei Liao2d831012011-09-28 22:06:53 -070041namespace art {
42
Ian Rogers57b86d42012-03-27 16:05:41 -070043class Array;
44class Class;
45class Field;
Mathieu Chartier66f19252012-09-18 08:57:04 -070046class AbstractMethod;
Ian Rogers57b86d42012-03-27 16:05:41 -070047class Object;
48
Ian Rogers57b86d42012-03-27 16:05:41 -070049// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
50// cannot be resolved, throw an error. If it can, use it to create an instance.
51// When verification/compiler hasn't been able to verify access, optionally perform an access
52// check.
Mathieu Chartier66f19252012-09-18 08:57:04 -070053static inline Object* AllocObjectFromCode(uint32_t type_idx, AbstractMethod* method, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070055 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070056 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
57 Runtime* runtime = Runtime::Current();
58 if (UNLIKELY(klass == NULL)) {
59 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
60 if (klass == NULL) {
61 DCHECK(self->IsExceptionPending());
62 return NULL; // Failure
63 }
64 }
65 if (access_check) {
66 if (UNLIKELY(!klass->IsInstantiable())) {
67 self->ThrowNewException("Ljava/lang/InstantiationError;",
68 PrettyDescriptor(klass).c_str());
69 return NULL; // Failure
70 }
71 Class* referrer = method->GetDeclaringClass();
72 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -070073 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070074 return NULL; // Failure
75 }
76 }
Mathieu Chartier155dfe92012-10-09 14:24:49 -070077 if (!klass->IsInitialized() &&
78 !runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -070079 DCHECK(self->IsExceptionPending());
80 return NULL; // Failure
81 }
Ian Rogers50b35e22012-10-04 10:09:15 -070082 return klass->AllocObject(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070083}
84
85// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
86// it cannot be resolved, throw an error. If it can, use it to create an array.
87// When verification/compiler hasn't been able to verify access, optionally perform an access
88// check.
Mathieu Chartier66f19252012-09-18 08:57:04 -070089static inline Array* AllocArrayFromCode(uint32_t type_idx, AbstractMethod* method, int32_t component_count,
Ian Rogers50b35e22012-10-04 10:09:15 -070090 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070091 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070092 if (UNLIKELY(component_count < 0)) {
Ian Rogers50b35e22012-10-04 10:09:15 -070093 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070094 return NULL; // Failure
95 }
96 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
97 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
98 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
99 if (klass == NULL) { // Error
100 DCHECK(Thread::Current()->IsExceptionPending());
101 return NULL; // Failure
102 }
103 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
104 }
105 if (access_check) {
106 Class* referrer = method->GetDeclaringClass();
107 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700108 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700109 return NULL; // Failure
110 }
111 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700112 return Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700113}
114
Mathieu Chartier66f19252012-09-18 08:57:04 -0700115extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, AbstractMethod* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700118
Ian Rogers08f753d2012-08-24 14:35:25 -0700119// Type of find field operation for fast and slow case.
120enum FindFieldType {
121 InstanceObjectRead,
122 InstanceObjectWrite,
123 InstancePrimitiveRead,
124 InstancePrimitiveWrite,
125 StaticObjectRead,
126 StaticObjectWrite,
127 StaticPrimitiveRead,
128 StaticPrimitiveWrite,
129};
130
131// Slow field find that can initialize classes and may throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700132extern Field* FindFieldFromCode(uint32_t field_idx, const AbstractMethod* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700133 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700135
Ian Rogers08f753d2012-08-24 14:35:25 -0700136// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700137static inline Field* FindFieldFast(uint32_t field_idx, const AbstractMethod* referrer,
Ian Rogers08f753d2012-08-24 14:35:25 -0700138 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700140 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
141 if (UNLIKELY(resolved_field == NULL)) {
142 return NULL;
143 }
144 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700145 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700146 if (UNLIKELY(!fields_class->IsInitializing())) {
147 return NULL;
148 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700149 // Check for incompatible class change.
150 bool is_primitive;
151 bool is_set;
152 bool is_static;
153 switch (type) {
154 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
155 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
156 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
157 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
158 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
159 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
160 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
161 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
162 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
163 is_primitive = true; is_set = true; is_static = true; break;
164 }
165 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
166 // Incompatible class change.
167 return NULL;
168 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700169 Class* referring_class = referrer->GetDeclaringClass();
170 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
171 !referring_class->CanAccessMember(fields_class,
172 resolved_field->GetAccessFlags()) ||
173 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700174 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700175 return NULL;
176 }
177 FieldHelper fh(resolved_field);
178 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
179 fh.FieldSize() != expected_size)) {
180 return NULL;
181 }
182 return resolved_field;
183}
184
Ian Rogers08f753d2012-08-24 14:35:25 -0700185// Fast path method resolution that can't throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700186static inline AbstractMethod* FindMethodFast(uint32_t method_idx, Object* this_object,
187 const AbstractMethod* referrer, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700189 bool is_direct = type == kStatic || type == kDirect;
190 if (UNLIKELY(this_object == NULL && !is_direct)) {
191 return NULL;
192 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700193 AbstractMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700194 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
195 if (UNLIKELY(resolved_method == NULL)) {
196 return NULL;
197 }
198 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700199 // Check for incompatible class change errors and access.
200 bool icce = resolved_method->CheckIncompatibleClassChange(type);
201 if (UNLIKELY(icce)) {
202 return NULL;
203 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700204 Class* methods_class = resolved_method->GetDeclaringClass();
205 Class* referring_class = referrer->GetDeclaringClass();
206 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
207 !referring_class->CanAccessMember(methods_class,
208 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700209 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700210 return NULL;
211 }
212 }
213 if (type == kInterface) { // Most common form of slow path dispatch.
214 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
215 } else if (is_direct) {
216 return resolved_method;
217 } else if (type == kSuper) {
218 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
219 Get(resolved_method->GetMethodIndex());
220 } else {
221 DCHECK(type == kVirtual);
222 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
223 }
224}
225
jeffhao262e2512012-12-11 09:46:43 -0800226extern AbstractMethod* FindMethodFromCode(uint32_t method_idx, Object* this_object, AbstractMethod* referrer,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700228 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700229
Mathieu Chartier66f19252012-09-18 08:57:04 -0700230extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const AbstractMethod* referrer, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 bool can_run_clinit, bool verify_access)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700233
jeffhaod7521322012-11-21 15:38:24 -0800234extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
235
Mathieu Chartier66f19252012-09-18 08:57:04 -0700236static inline String* ResolveStringFromCode(const AbstractMethod* referrer, uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700238 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
239 return class_linker->ResolveString(string_idx, referrer);
240}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700241
TDYa1273d71d802012-08-15 03:47:03 -0700242static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700244 UNLOCK_FUNCTION(monitor_lock_) {
245 // Save any pending exception over monitor exit call.
246 Throwable* saved_exception = NULL;
247 if (UNLIKELY(self->IsExceptionPending())) {
248 saved_exception = self->GetException();
249 self->ClearException();
250 }
251 // Decode locked object and unlock, before popping local references.
252 self->DecodeJObject(locked)->MonitorExit(self);
253 if (UNLIKELY(self->IsExceptionPending())) {
254 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
255 << saved_exception->Dump()
256 << "\nEncountered second exception during implicit MonitorExit:\n"
257 << self->GetException()->Dump();
258 }
259 // Restore pending exception.
260 if (saved_exception != NULL) {
261 self->SetException(saved_exception);
262 }
263}
264
265static inline void CheckReferenceResult(Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700267 if (o == NULL) {
268 return;
269 }
270 if (o == kInvalidIndirectRefObject) {
271 JniAbortF(NULL, "invalid reference returned from %s",
272 PrettyMethod(self->GetCurrentMethod()).c_str());
273 }
274 // Make sure that the result is an instance of the type this method was expected to return.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700275 AbstractMethod* m = self->GetCurrentMethod();
TDYa1273d71d802012-08-15 03:47:03 -0700276 MethodHelper mh(m);
277 Class* return_type = mh.GetReturnType();
278
279 if (!o->InstanceOf(return_type)) {
280 JniAbortF(NULL, "attempt to return an instance of %s from %s",
281 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
282 }
283}
284
jeffhao373c52f2012-11-20 16:11:52 -0800285static inline void CheckSuspend(Thread* thread)
286 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
287 for (;;) {
288 if (thread->ReadFlag(kCheckpointRequest)) {
289 thread->RunCheckpointFunction();
290 thread->AtomicClearFlag(kCheckpointRequest);
291 } else if (thread->ReadFlag(kSuspendRequest)) {
292 thread->FullSuspendCheck();
293 } else {
294 break;
295 }
296 }
297}
298
Shih-wei Liao2d831012011-09-28 22:06:53 -0700299} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700300
buzbee54330722011-08-23 16:46:55 -0700301#endif // ART_SRC_RUNTIME_SUPPORT_H_