blob: a504237044825826255efa2d5e6fdc04751e9f9c [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"
28#include "mirror/throwable.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070029#include "object_utils.h"
30#include "thread.h"
31
Ian Rogersaf6e67a2013-01-16 08:38:37 -080032extern "C" void art_interpreter_invoke_handler();
Ian Rogers57b86d42012-03-27 16:05:41 -070033extern "C" void art_proxy_invoke_handler();
34extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070035
jeffhao41005dd2012-05-09 17:58:52 -070036extern "C" double art_l2d(int64_t l);
37extern "C" float art_l2f(int64_t l);
38extern "C" int64_t art_d2l(double d);
39extern "C" int32_t art_d2i(double d);
40extern "C" int64_t art_f2l(float f);
41extern "C" int32_t art_f2i(float f);
42
Shih-wei Liao2d831012011-09-28 22:06:53 -070043namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044namespace mirror {
Ian Rogers57b86d42012-03-27 16:05:41 -070045class Class;
46class Field;
Ian Rogers57b86d42012-03-27 16:05:41 -070047class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048}
Ian Rogers57b86d42012-03-27 16:05:41 -070049
Ian Rogers57b86d42012-03-27 16:05:41 -070050// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
51// cannot be resolved, throw an error. If it can, use it to create an instance.
52// When verification/compiler hasn't been able to verify access, optionally perform an access
53// check.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
55 Thread* self,
56 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070057 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -070059 Runtime* runtime = Runtime::Current();
60 if (UNLIKELY(klass == NULL)) {
61 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
62 if (klass == NULL) {
63 DCHECK(self->IsExceptionPending());
64 return NULL; // Failure
65 }
66 }
67 if (access_check) {
68 if (UNLIKELY(!klass->IsInstantiable())) {
69 self->ThrowNewException("Ljava/lang/InstantiationError;",
70 PrettyDescriptor(klass).c_str());
71 return NULL; // Failure
72 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -070074 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -070075 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070076 return NULL; // Failure
77 }
78 }
Mathieu Chartier155dfe92012-10-09 14:24:49 -070079 if (!klass->IsInitialized() &&
80 !runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -070081 DCHECK(self->IsExceptionPending());
82 return NULL; // Failure
83 }
Ian Rogers50b35e22012-10-04 10:09:15 -070084 return klass->AllocObject(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070085}
86
87// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
88// it cannot be resolved, throw an error. If it can, use it to create an array.
89// When verification/compiler hasn't been able to verify access, optionally perform an access
90// check.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
92 int32_t component_count,
93 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070094 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070095 if (UNLIKELY(component_count < 0)) {
Ian Rogers50b35e22012-10-04 10:09:15 -070096 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070097 return NULL; // Failure
98 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700100 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
101 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
102 if (klass == NULL) { // Error
103 DCHECK(Thread::Current()->IsExceptionPending());
104 return NULL; // Failure
105 }
106 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
107 }
108 if (access_check) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700110 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700111 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700112 return NULL; // Failure
113 }
114 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700116}
117
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
119 int32_t component_count,
120 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700122
Ian Rogers08f753d2012-08-24 14:35:25 -0700123// Type of find field operation for fast and slow case.
124enum FindFieldType {
125 InstanceObjectRead,
126 InstanceObjectWrite,
127 InstancePrimitiveRead,
128 InstancePrimitiveWrite,
129 StaticObjectRead,
130 StaticObjectWrite,
131 StaticPrimitiveRead,
132 StaticPrimitiveWrite,
133};
134
135// Slow field find that can initialize classes and may throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136extern mirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer,
137 Thread* self, FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700139
Ian Rogers08f753d2012-08-24 14:35:25 -0700140// Fast path field resolution that can't initialize classes or throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141static inline mirror::Field* FindFieldFast(uint32_t field_idx,
142 const mirror::AbstractMethod* referrer,
143 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700144 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 mirror::Field* resolved_field =
146 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700147 if (UNLIKELY(resolved_field == NULL)) {
148 return NULL;
149 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700151 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700152 if (UNLIKELY(!fields_class->IsInitializing())) {
153 return NULL;
154 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700155 // Check for incompatible class change.
156 bool is_primitive;
157 bool is_set;
158 bool is_static;
159 switch (type) {
160 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
161 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
162 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
163 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
164 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
165 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
166 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
167 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
168 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
169 is_primitive = true; is_set = true; is_static = true; break;
170 }
171 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
172 // Incompatible class change.
173 return NULL;
174 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700176 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
177 !referring_class->CanAccessMember(fields_class,
178 resolved_field->GetAccessFlags()) ||
179 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700180 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700181 return NULL;
182 }
183 FieldHelper fh(resolved_field);
184 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
185 fh.FieldSize() != expected_size)) {
186 return NULL;
187 }
188 return resolved_field;
189}
190
Ian Rogers08f753d2012-08-24 14:35:25 -0700191// Fast path method resolution that can't throw exceptions.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192static inline mirror::AbstractMethod* FindMethodFast(uint32_t method_idx,
193 mirror::Object* this_object,
194 const mirror::AbstractMethod* referrer,
195 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700197 bool is_direct = type == kStatic || type == kDirect;
198 if (UNLIKELY(this_object == NULL && !is_direct)) {
199 return NULL;
200 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 mirror::AbstractMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700202 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
203 if (UNLIKELY(resolved_method == NULL)) {
204 return NULL;
205 }
206 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700207 // Check for incompatible class change errors and access.
208 bool icce = resolved_method->CheckIncompatibleClassChange(type);
209 if (UNLIKELY(icce)) {
210 return NULL;
211 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
213 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700214 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
215 !referring_class->CanAccessMember(methods_class,
216 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700217 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700218 return NULL;
219 }
220 }
221 if (type == kInterface) { // Most common form of slow path dispatch.
222 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
223 } else if (is_direct) {
224 return resolved_method;
225 } else if (type == kSuper) {
226 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
227 Get(resolved_method->GetMethodIndex());
228 } else {
229 DCHECK(type == kVirtual);
230 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
231 }
232}
233
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234extern mirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
235 mirror::AbstractMethod* referrer,
236 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700238
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239extern mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
240 const mirror::AbstractMethod* referrer, Thread* self,
241 bool can_run_clinit, bool verify_access)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700243
jeffhaod7521322012-11-21 15:38:24 -0800244extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
245
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246static inline mirror::String* ResolveStringFromCode(const mirror::AbstractMethod* referrer,
247 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700249 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
250 return class_linker->ResolveString(string_idx, referrer);
251}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700252
TDYa1273d71d802012-08-15 03:47:03 -0700253static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700255 UNLOCK_FUNCTION(monitor_lock_) {
256 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 mirror::Throwable* saved_exception = NULL;
TDYa1273d71d802012-08-15 03:47:03 -0700258 if (UNLIKELY(self->IsExceptionPending())) {
259 saved_exception = self->GetException();
260 self->ClearException();
261 }
262 // Decode locked object and unlock, before popping local references.
263 self->DecodeJObject(locked)->MonitorExit(self);
264 if (UNLIKELY(self->IsExceptionPending())) {
265 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
266 << saved_exception->Dump()
267 << "\nEncountered second exception during implicit MonitorExit:\n"
268 << self->GetException()->Dump();
269 }
270 // Restore pending exception.
271 if (saved_exception != NULL) {
272 self->SetException(saved_exception);
273 }
274}
275
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700277 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700278 if (o == NULL) {
279 return;
280 }
281 if (o == kInvalidIndirectRefObject) {
282 JniAbortF(NULL, "invalid reference returned from %s",
283 PrettyMethod(self->GetCurrentMethod()).c_str());
284 }
285 // Make sure that the result is an instance of the type this method was expected to return.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 mirror::AbstractMethod* m = self->GetCurrentMethod();
TDYa1273d71d802012-08-15 03:47:03 -0700287 MethodHelper mh(m);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288 mirror::Class* return_type = mh.GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700289
290 if (!o->InstanceOf(return_type)) {
291 JniAbortF(NULL, "attempt to return an instance of %s from %s",
292 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
293 }
294}
295
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800296static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800297 for (;;) {
298 if (thread->ReadFlag(kCheckpointRequest)) {
299 thread->RunCheckpointFunction();
300 thread->AtomicClearFlag(kCheckpointRequest);
301 } else if (thread->ReadFlag(kSuspendRequest)) {
302 thread->FullSuspendCheck();
303 } else {
304 break;
305 }
306 }
307}
308
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800309JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
310 jobject rcvr_jobj, jobject interface_method_jobj,
311 std::vector<jvalue>& args)
312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ;
313
Shih-wei Liao2d831012011-09-28 22:06:53 -0700314} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700315
buzbee54330722011-08-23 16:46:55 -0700316#endif // ART_SRC_RUNTIME_SUPPORT_H_