blob: c7eb9576dfeed322c6c3a00c6916d74ca7c7ee99 [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();
Jeff Hao5fa60c32013-04-04 17:57:01 -070034extern "C" void art_portable_proxy_invoke_handler();
Logan Chien8dbb7082013-01-25 20:31:17 +080035extern "C" void art_quick_proxy_invoke_handler();
Ian Rogers57b86d42012-03-27 16:05:41 -070036extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070037
jeffhao41005dd2012-05-09 17:58:52 -070038extern "C" double art_l2d(int64_t l);
39extern "C" float art_l2f(int64_t l);
40extern "C" int64_t art_d2l(double d);
41extern "C" int32_t art_d2i(double d);
42extern "C" int64_t art_f2l(float f);
43extern "C" int32_t art_f2i(float f);
44
Shih-wei Liao2d831012011-09-28 22:06:53 -070045namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046namespace mirror {
Ian Rogers57b86d42012-03-27 16:05:41 -070047class Class;
48class Field;
Ian Rogers57b86d42012-03-27 16:05:41 -070049class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050}
Ian Rogers57b86d42012-03-27 16:05:41 -070051
Ian Rogers57b86d42012-03-27 16:05:41 -070052// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
53// cannot be resolved, throw an error. If it can, use it to create an instance.
54// When verification/compiler hasn't been able to verify access, optionally perform an access
55// check.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
57 Thread* self,
58 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -070061 Runtime* runtime = Runtime::Current();
62 if (UNLIKELY(klass == NULL)) {
63 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
64 if (klass == NULL) {
65 DCHECK(self->IsExceptionPending());
66 return NULL; // Failure
67 }
68 }
69 if (access_check) {
70 if (UNLIKELY(!klass->IsInstantiable())) {
Ian Rogers62d6c772013-02-27 08:32:07 -080071 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
72 self->ThrowNewException(throw_location, "Ljava/lang/InstantiationError;",
Ian Rogers57b86d42012-03-27 16:05:41 -070073 PrettyDescriptor(klass).c_str());
74 return NULL; // Failure
75 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -070077 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -070078 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070079 return NULL; // Failure
80 }
81 }
Mathieu Chartier155dfe92012-10-09 14:24:49 -070082 if (!klass->IsInitialized() &&
83 !runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -070084 DCHECK(self->IsExceptionPending());
85 return NULL; // Failure
86 }
Ian Rogers50b35e22012-10-04 10:09:15 -070087 return klass->AllocObject(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070088}
89
90// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
91// it cannot be resolved, throw an error. If it can, use it to create an array.
92// When verification/compiler hasn't been able to verify access, optionally perform an access
93// check.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
95 int32_t component_count,
96 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070097 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070098 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080099 ThrowNegativeArraySizeException(component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700100 return NULL; // Failure
101 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700103 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
104 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
105 if (klass == NULL) { // Error
106 DCHECK(Thread::Current()->IsExceptionPending());
107 return NULL; // Failure
108 }
109 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
110 }
111 if (access_check) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700113 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700114 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700115 return NULL; // Failure
116 }
117 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700119}
120
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
122 int32_t component_count,
123 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700125
Ian Rogers08f753d2012-08-24 14:35:25 -0700126// Type of find field operation for fast and slow case.
127enum FindFieldType {
128 InstanceObjectRead,
129 InstanceObjectWrite,
130 InstancePrimitiveRead,
131 InstancePrimitiveWrite,
132 StaticObjectRead,
133 StaticObjectWrite,
134 StaticPrimitiveRead,
135 StaticPrimitiveWrite,
136};
137
138// Slow field find that can initialize classes and may throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139extern mirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer,
140 Thread* self, FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700142
Ian Rogers08f753d2012-08-24 14:35:25 -0700143// Fast path field resolution that can't initialize classes or throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144static inline mirror::Field* FindFieldFast(uint32_t field_idx,
145 const mirror::AbstractMethod* referrer,
146 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 mirror::Field* resolved_field =
149 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700150 if (UNLIKELY(resolved_field == NULL)) {
151 return NULL;
152 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700154 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700155 if (UNLIKELY(!fields_class->IsInitializing())) {
156 return NULL;
157 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700158 // Check for incompatible class change.
159 bool is_primitive;
160 bool is_set;
161 bool is_static;
162 switch (type) {
163 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
164 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
165 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
166 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
167 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
168 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
169 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
170 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
171 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
172 is_primitive = true; is_set = true; is_static = true; break;
173 }
174 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
175 // Incompatible class change.
176 return NULL;
177 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700179 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
180 !referring_class->CanAccessMember(fields_class,
181 resolved_field->GetAccessFlags()) ||
182 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700183 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700184 return NULL;
185 }
186 FieldHelper fh(resolved_field);
187 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
188 fh.FieldSize() != expected_size)) {
189 return NULL;
190 }
191 return resolved_field;
192}
193
Ian Rogers08f753d2012-08-24 14:35:25 -0700194// Fast path method resolution that can't throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195static inline mirror::AbstractMethod* FindMethodFast(uint32_t method_idx,
196 mirror::Object* this_object,
197 const mirror::AbstractMethod* referrer,
198 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700200 bool is_direct = type == kStatic || type == kDirect;
201 if (UNLIKELY(this_object == NULL && !is_direct)) {
202 return NULL;
203 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204 mirror::AbstractMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700205 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
206 if (UNLIKELY(resolved_method == NULL)) {
207 return NULL;
208 }
209 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700210 // Check for incompatible class change errors and access.
211 bool icce = resolved_method->CheckIncompatibleClassChange(type);
212 if (UNLIKELY(icce)) {
213 return NULL;
214 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
216 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700217 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
218 !referring_class->CanAccessMember(methods_class,
219 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700220 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700221 return NULL;
222 }
223 }
224 if (type == kInterface) { // Most common form of slow path dispatch.
225 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
226 } else if (is_direct) {
227 return resolved_method;
228 } else if (type == kSuper) {
229 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
230 Get(resolved_method->GetMethodIndex());
231 } else {
232 DCHECK(type == kVirtual);
233 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
234 }
235}
236
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237extern mirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
238 mirror::AbstractMethod* referrer,
239 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700241
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242extern mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
243 const mirror::AbstractMethod* referrer, Thread* self,
244 bool can_run_clinit, bool verify_access)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700246
jeffhaod7521322012-11-21 15:38:24 -0800247extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
248
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249static inline mirror::String* ResolveStringFromCode(const mirror::AbstractMethod* referrer,
250 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700252 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
253 return class_linker->ResolveString(string_idx, referrer);
254}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700255
TDYa1273d71d802012-08-15 03:47:03 -0700256static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700258 UNLOCK_FUNCTION(monitor_lock_) {
259 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 mirror::Throwable* saved_exception = NULL;
Ian Rogers62d6c772013-02-27 08:32:07 -0800261 ThrowLocation saved_throw_location;
TDYa1273d71d802012-08-15 03:47:03 -0700262 if (UNLIKELY(self->IsExceptionPending())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800263 saved_exception = self->GetException(&saved_throw_location);
TDYa1273d71d802012-08-15 03:47:03 -0700264 self->ClearException();
265 }
266 // Decode locked object and unlock, before popping local references.
267 self->DecodeJObject(locked)->MonitorExit(self);
268 if (UNLIKELY(self->IsExceptionPending())) {
269 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
270 << saved_exception->Dump()
271 << "\nEncountered second exception during implicit MonitorExit:\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 << self->GetException(NULL)->Dump();
TDYa1273d71d802012-08-15 03:47:03 -0700273 }
274 // Restore pending exception.
275 if (saved_exception != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 self->SetException(saved_throw_location, saved_exception);
TDYa1273d71d802012-08-15 03:47:03 -0700277 }
278}
279
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700282 if (o == NULL) {
283 return;
284 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 mirror::AbstractMethod* m = self->GetCurrentMethod(NULL);
TDYa1273d71d802012-08-15 03:47:03 -0700286 if (o == kInvalidIndirectRefObject) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800287 JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str());
TDYa1273d71d802012-08-15 03:47:03 -0700288 }
289 // Make sure that the result is an instance of the type this method was expected to return.
Ian Rogers62d6c772013-02-27 08:32:07 -0800290 mirror::Class* return_type = MethodHelper(m).GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700291
292 if (!o->InstanceOf(return_type)) {
293 JniAbortF(NULL, "attempt to return an instance of %s from %s",
294 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
295 }
296}
297
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800298static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800299 for (;;) {
300 if (thread->ReadFlag(kCheckpointRequest)) {
301 thread->RunCheckpointFunction();
302 thread->AtomicClearFlag(kCheckpointRequest);
303 } else if (thread->ReadFlag(kSuspendRequest)) {
304 thread->FullSuspendCheck();
305 } else {
306 break;
307 }
308 }
309}
310
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800311JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
312 jobject rcvr_jobj, jobject interface_method_jobj,
313 std::vector<jvalue>& args)
314 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ;
315
Shih-wei Liao2d831012011-09-28 22:06:53 -0700316} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700317
buzbee54330722011-08-23 16:46:55 -0700318#endif // ART_SRC_RUNTIME_SUPPORT_H_