blob: 09ca0aab51fbd0d7a7f2892a457078dda93c5c50 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/abstract_method.h"
27#include "mirror/array.h"
Ian Rogers693ff612013-02-01 10:56:12 -080028#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/throwable.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070030#include "object_utils.h"
31#include "thread.h"
32
Ian Rogersaf6e67a2013-01-16 08:38:37 -080033extern "C" void art_interpreter_invoke_handler();
Ian Rogers57b86d42012-03-27 16:05:41 -070034extern "C" void art_proxy_invoke_handler();
35extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070036
jeffhao41005dd2012-05-09 17:58:52 -070037extern "C" double art_l2d(int64_t l);
38extern "C" float art_l2f(int64_t l);
39extern "C" int64_t art_d2l(double d);
40extern "C" int32_t art_d2i(double d);
41extern "C" int64_t art_f2l(float f);
42extern "C" int32_t art_f2i(float f);
43
Shih-wei Liao2d831012011-09-28 22:06:53 -070044namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045namespace mirror {
Ian Rogers57b86d42012-03-27 16:05:41 -070046class Class;
47class Field;
Ian Rogers57b86d42012-03-27 16:05:41 -070048class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049}
Ian Rogers57b86d42012-03-27 16:05:41 -070050
Ian Rogers57b86d42012-03-27 16:05:41 -070051// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
52// cannot be resolved, throw an error. If it can, use it to create an instance.
53// When verification/compiler hasn't been able to verify access, optionally perform an access
54// check.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
56 Thread* self,
57 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070058 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -070060 Runtime* runtime = Runtime::Current();
61 if (UNLIKELY(klass == NULL)) {
62 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
63 if (klass == NULL) {
64 DCHECK(self->IsExceptionPending());
65 return NULL; // Failure
66 }
67 }
68 if (access_check) {
69 if (UNLIKELY(!klass->IsInstantiable())) {
70 self->ThrowNewException("Ljava/lang/InstantiationError;",
71 PrettyDescriptor(klass).c_str());
72 return NULL; // Failure
73 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -070075 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -070076 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070077 return NULL; // Failure
78 }
79 }
Mathieu Chartier155dfe92012-10-09 14:24:49 -070080 if (!klass->IsInitialized() &&
81 !runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -070082 DCHECK(self->IsExceptionPending());
83 return NULL; // Failure
84 }
Ian Rogers50b35e22012-10-04 10:09:15 -070085 return klass->AllocObject(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070086}
87
88// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
89// it cannot be resolved, throw an error. If it can, use it to create an array.
90// When verification/compiler hasn't been able to verify access, optionally perform an access
91// check.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
93 int32_t component_count,
94 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070096 if (UNLIKELY(component_count < 0)) {
Ian Rogers50b35e22012-10-04 10:09:15 -070097 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070098 return NULL; // Failure
99 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700101 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
102 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
103 if (klass == NULL) { // Error
104 DCHECK(Thread::Current()->IsExceptionPending());
105 return NULL; // Failure
106 }
107 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
108 }
109 if (access_check) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700111 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700112 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700113 return NULL; // Failure
114 }
115 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700117}
118
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
120 int32_t component_count,
121 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700123
Ian Rogers08f753d2012-08-24 14:35:25 -0700124// Type of find field operation for fast and slow case.
125enum FindFieldType {
126 InstanceObjectRead,
127 InstanceObjectWrite,
128 InstancePrimitiveRead,
129 InstancePrimitiveWrite,
130 StaticObjectRead,
131 StaticObjectWrite,
132 StaticPrimitiveRead,
133 StaticPrimitiveWrite,
134};
135
136// Slow field find that can initialize classes and may throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137extern mirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer,
138 Thread* self, 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
Ian Rogers08f753d2012-08-24 14:35:25 -0700141// Fast path field resolution that can't initialize classes or throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142static inline mirror::Field* FindFieldFast(uint32_t field_idx,
143 const mirror::AbstractMethod* referrer,
144 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 mirror::Field* resolved_field =
147 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700148 if (UNLIKELY(resolved_field == NULL)) {
149 return NULL;
150 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700152 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700153 if (UNLIKELY(!fields_class->IsInitializing())) {
154 return NULL;
155 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700156 // Check for incompatible class change.
157 bool is_primitive;
158 bool is_set;
159 bool is_static;
160 switch (type) {
161 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
162 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
163 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
164 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
165 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
166 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
167 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
168 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
169 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
170 is_primitive = true; is_set = true; is_static = true; break;
171 }
172 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
173 // Incompatible class change.
174 return NULL;
175 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700177 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
178 !referring_class->CanAccessMember(fields_class,
179 resolved_field->GetAccessFlags()) ||
180 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700181 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700182 return NULL;
183 }
184 FieldHelper fh(resolved_field);
185 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
186 fh.FieldSize() != expected_size)) {
187 return NULL;
188 }
189 return resolved_field;
190}
191
Ian Rogers08f753d2012-08-24 14:35:25 -0700192// Fast path method resolution that can't throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193static inline mirror::AbstractMethod* FindMethodFast(uint32_t method_idx,
194 mirror::Object* this_object,
195 const mirror::AbstractMethod* referrer,
196 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700198 bool is_direct = type == kStatic || type == kDirect;
199 if (UNLIKELY(this_object == NULL && !is_direct)) {
200 return NULL;
201 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 mirror::AbstractMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700203 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
204 if (UNLIKELY(resolved_method == NULL)) {
205 return NULL;
206 }
207 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700208 // Check for incompatible class change errors and access.
209 bool icce = resolved_method->CheckIncompatibleClassChange(type);
210 if (UNLIKELY(icce)) {
211 return NULL;
212 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
214 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700215 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
216 !referring_class->CanAccessMember(methods_class,
217 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700218 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700219 return NULL;
220 }
221 }
222 if (type == kInterface) { // Most common form of slow path dispatch.
223 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
224 } else if (is_direct) {
225 return resolved_method;
226 } else if (type == kSuper) {
227 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
228 Get(resolved_method->GetMethodIndex());
229 } else {
230 DCHECK(type == kVirtual);
231 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
232 }
233}
234
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235extern mirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
236 mirror::AbstractMethod* referrer,
237 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700239
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240extern mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
241 const mirror::AbstractMethod* referrer, Thread* self,
242 bool can_run_clinit, bool verify_access)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700244
jeffhaod7521322012-11-21 15:38:24 -0800245extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
246
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247static inline mirror::String* ResolveStringFromCode(const mirror::AbstractMethod* referrer,
248 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700250 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
251 return class_linker->ResolveString(string_idx, referrer);
252}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700253
TDYa1273d71d802012-08-15 03:47:03 -0700254static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700256 UNLOCK_FUNCTION(monitor_lock_) {
257 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258 mirror::Throwable* saved_exception = NULL;
TDYa1273d71d802012-08-15 03:47:03 -0700259 if (UNLIKELY(self->IsExceptionPending())) {
260 saved_exception = self->GetException();
261 self->ClearException();
262 }
263 // Decode locked object and unlock, before popping local references.
264 self->DecodeJObject(locked)->MonitorExit(self);
265 if (UNLIKELY(self->IsExceptionPending())) {
266 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
267 << saved_exception->Dump()
268 << "\nEncountered second exception during implicit MonitorExit:\n"
269 << self->GetException()->Dump();
270 }
271 // Restore pending exception.
272 if (saved_exception != NULL) {
273 self->SetException(saved_exception);
274 }
275}
276
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700279 if (o == NULL) {
280 return;
281 }
282 if (o == kInvalidIndirectRefObject) {
283 JniAbortF(NULL, "invalid reference returned from %s",
284 PrettyMethod(self->GetCurrentMethod()).c_str());
285 }
286 // Make sure that the result is an instance of the type this method was expected to return.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 mirror::AbstractMethod* m = self->GetCurrentMethod();
TDYa1273d71d802012-08-15 03:47:03 -0700288 MethodHelper mh(m);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 mirror::Class* return_type = mh.GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700290
291 if (!o->InstanceOf(return_type)) {
292 JniAbortF(NULL, "attempt to return an instance of %s from %s",
293 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
294 }
295}
296
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800297static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800298 for (;;) {
299 if (thread->ReadFlag(kCheckpointRequest)) {
300 thread->RunCheckpointFunction();
301 thread->AtomicClearFlag(kCheckpointRequest);
302 } else if (thread->ReadFlag(kSuspendRequest)) {
303 thread->FullSuspendCheck();
304 } else {
305 break;
306 }
307 }
308}
309
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800310JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
311 jobject rcvr_jobj, jobject interface_method_jobj,
312 std::vector<jvalue>& args)
313 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ;
314
Shih-wei Liao2d831012011-09-28 22:06:53 -0700315} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700316
buzbee54330722011-08-23 16:46:55 -0700317#endif // ART_SRC_RUNTIME_SUPPORT_H_