blob: 1c8d1740b1133c2edf7ac78e5ced35745b897d19 [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
Ian Rogersaf6e67a2013-01-16 08:38:37 -080031extern "C" void art_interpreter_invoke_handler();
Ian Rogers57b86d42012-03-27 16:05:41 -070032extern "C" void art_proxy_invoke_handler();
33extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070034
jeffhao41005dd2012-05-09 17:58:52 -070035extern "C" double art_l2d(int64_t l);
36extern "C" float art_l2f(int64_t l);
37extern "C" int64_t art_d2l(double d);
38extern "C" int32_t art_d2i(double d);
39extern "C" int64_t art_f2l(float f);
40extern "C" int32_t art_f2i(float f);
41
Shih-wei Liao2d831012011-09-28 22:06:53 -070042namespace art {
43
Ian Rogers57b86d42012-03-27 16:05:41 -070044class Array;
45class Class;
46class Field;
Mathieu Chartier66f19252012-09-18 08:57:04 -070047class AbstractMethod;
Ian Rogers57b86d42012-03-27 16:05:41 -070048class Object;
49
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.
Mathieu Chartier66f19252012-09-18 08:57:04 -070054static inline Object* AllocObjectFromCode(uint32_t type_idx, AbstractMethod* method, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070056 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070057 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
58 Runtime* runtime = Runtime::Current();
59 if (UNLIKELY(klass == NULL)) {
60 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
61 if (klass == NULL) {
62 DCHECK(self->IsExceptionPending());
63 return NULL; // Failure
64 }
65 }
66 if (access_check) {
67 if (UNLIKELY(!klass->IsInstantiable())) {
68 self->ThrowNewException("Ljava/lang/InstantiationError;",
69 PrettyDescriptor(klass).c_str());
70 return NULL; // Failure
71 }
72 Class* referrer = method->GetDeclaringClass();
73 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -070074 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070075 return NULL; // Failure
76 }
77 }
Mathieu Chartier155dfe92012-10-09 14:24:49 -070078 if (!klass->IsInitialized() &&
79 !runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -070080 DCHECK(self->IsExceptionPending());
81 return NULL; // Failure
82 }
Ian Rogers50b35e22012-10-04 10:09:15 -070083 return klass->AllocObject(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070084}
85
86// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
87// it cannot be resolved, throw an error. If it can, use it to create an array.
88// When verification/compiler hasn't been able to verify access, optionally perform an access
89// check.
Mathieu Chartier66f19252012-09-18 08:57:04 -070090static inline Array* AllocArrayFromCode(uint32_t type_idx, AbstractMethod* method, int32_t component_count,
Ian Rogers50b35e22012-10-04 10:09:15 -070091 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070093 if (UNLIKELY(component_count < 0)) {
Ian Rogers50b35e22012-10-04 10:09:15 -070094 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070095 return NULL; // Failure
96 }
97 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
98 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
99 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
100 if (klass == NULL) { // Error
101 DCHECK(Thread::Current()->IsExceptionPending());
102 return NULL; // Failure
103 }
104 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
105 }
106 if (access_check) {
107 Class* referrer = method->GetDeclaringClass();
108 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700109 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700110 return NULL; // Failure
111 }
112 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700113 return Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700114}
115
Mathieu Chartier66f19252012-09-18 08:57:04 -0700116extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, AbstractMethod* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700119
Ian Rogers08f753d2012-08-24 14:35:25 -0700120// Type of find field operation for fast and slow case.
121enum FindFieldType {
122 InstanceObjectRead,
123 InstanceObjectWrite,
124 InstancePrimitiveRead,
125 InstancePrimitiveWrite,
126 StaticObjectRead,
127 StaticObjectWrite,
128 StaticPrimitiveRead,
129 StaticPrimitiveWrite,
130};
131
132// Slow field find that can initialize classes and may throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700133extern Field* FindFieldFromCode(uint32_t field_idx, const AbstractMethod* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700134 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700136
Ian Rogers08f753d2012-08-24 14:35:25 -0700137// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700138static inline Field* FindFieldFast(uint32_t field_idx, const AbstractMethod* referrer,
Ian Rogers08f753d2012-08-24 14:35:25 -0700139 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700141 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
142 if (UNLIKELY(resolved_field == NULL)) {
143 return NULL;
144 }
145 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700146 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700147 if (UNLIKELY(!fields_class->IsInitializing())) {
148 return NULL;
149 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700150 // Check for incompatible class change.
151 bool is_primitive;
152 bool is_set;
153 bool is_static;
154 switch (type) {
155 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
156 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
157 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
158 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
159 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
160 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
161 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
162 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
163 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
164 is_primitive = true; is_set = true; is_static = true; break;
165 }
166 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
167 // Incompatible class change.
168 return NULL;
169 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700170 Class* referring_class = referrer->GetDeclaringClass();
171 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
172 !referring_class->CanAccessMember(fields_class,
173 resolved_field->GetAccessFlags()) ||
174 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700175 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700176 return NULL;
177 }
178 FieldHelper fh(resolved_field);
179 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
180 fh.FieldSize() != expected_size)) {
181 return NULL;
182 }
183 return resolved_field;
184}
185
Ian Rogers08f753d2012-08-24 14:35:25 -0700186// Fast path method resolution that can't throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700187static inline AbstractMethod* FindMethodFast(uint32_t method_idx, Object* this_object,
188 const AbstractMethod* referrer, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700190 bool is_direct = type == kStatic || type == kDirect;
191 if (UNLIKELY(this_object == NULL && !is_direct)) {
192 return NULL;
193 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700194 AbstractMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700195 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
196 if (UNLIKELY(resolved_method == NULL)) {
197 return NULL;
198 }
199 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700200 // Check for incompatible class change errors and access.
201 bool icce = resolved_method->CheckIncompatibleClassChange(type);
202 if (UNLIKELY(icce)) {
203 return NULL;
204 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700205 Class* methods_class = resolved_method->GetDeclaringClass();
206 Class* referring_class = referrer->GetDeclaringClass();
207 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
208 !referring_class->CanAccessMember(methods_class,
209 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700210 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700211 return NULL;
212 }
213 }
214 if (type == kInterface) { // Most common form of slow path dispatch.
215 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
216 } else if (is_direct) {
217 return resolved_method;
218 } else if (type == kSuper) {
219 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
220 Get(resolved_method->GetMethodIndex());
221 } else {
222 DCHECK(type == kVirtual);
223 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
224 }
225}
226
jeffhao262e2512012-12-11 09:46:43 -0800227extern AbstractMethod* FindMethodFromCode(uint32_t method_idx, Object* this_object, AbstractMethod* referrer,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700230
Mathieu Chartier66f19252012-09-18 08:57:04 -0700231extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const AbstractMethod* referrer, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 bool can_run_clinit, bool verify_access)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700234
jeffhaod7521322012-11-21 15:38:24 -0800235extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
236
Mathieu Chartier66f19252012-09-18 08:57:04 -0700237static inline String* ResolveStringFromCode(const AbstractMethod* referrer, uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700239 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
240 return class_linker->ResolveString(string_idx, referrer);
241}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700242
TDYa1273d71d802012-08-15 03:47:03 -0700243static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700245 UNLOCK_FUNCTION(monitor_lock_) {
246 // Save any pending exception over monitor exit call.
247 Throwable* saved_exception = NULL;
248 if (UNLIKELY(self->IsExceptionPending())) {
249 saved_exception = self->GetException();
250 self->ClearException();
251 }
252 // Decode locked object and unlock, before popping local references.
253 self->DecodeJObject(locked)->MonitorExit(self);
254 if (UNLIKELY(self->IsExceptionPending())) {
255 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
256 << saved_exception->Dump()
257 << "\nEncountered second exception during implicit MonitorExit:\n"
258 << self->GetException()->Dump();
259 }
260 // Restore pending exception.
261 if (saved_exception != NULL) {
262 self->SetException(saved_exception);
263 }
264}
265
266static inline void CheckReferenceResult(Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700268 if (o == NULL) {
269 return;
270 }
271 if (o == kInvalidIndirectRefObject) {
272 JniAbortF(NULL, "invalid reference returned from %s",
273 PrettyMethod(self->GetCurrentMethod()).c_str());
274 }
275 // Make sure that the result is an instance of the type this method was expected to return.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700276 AbstractMethod* m = self->GetCurrentMethod();
TDYa1273d71d802012-08-15 03:47:03 -0700277 MethodHelper mh(m);
278 Class* return_type = mh.GetReturnType();
279
280 if (!o->InstanceOf(return_type)) {
281 JniAbortF(NULL, "attempt to return an instance of %s from %s",
282 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
283 }
284}
285
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800286static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800287 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
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800299JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
300 jobject rcvr_jobj, jobject interface_method_jobj,
301 std::vector<jvalue>& args)
302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ;
303
Shih-wei Liao2d831012011-09-28 22:06:53 -0700304} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700305
buzbee54330722011-08-23 16:46:55 -0700306#endif // ART_SRC_RUNTIME_SUPPORT_H_