blob: 0d24e488ad980a82644eb648002152e90ed0a4dc [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 }
Ian Rogers0045a292012-03-31 21:08:41 -070077 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -070078 DCHECK(self->IsExceptionPending());
79 return NULL; // Failure
80 }
Ian Rogers50b35e22012-10-04 10:09:15 -070081 return klass->AllocObject(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070082}
83
84// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
85// it cannot be resolved, throw an error. If it can, use it to create an array.
86// When verification/compiler hasn't been able to verify access, optionally perform an access
87// check.
Mathieu Chartier66f19252012-09-18 08:57:04 -070088static inline Array* AllocArrayFromCode(uint32_t type_idx, AbstractMethod* method, int32_t component_count,
Ian Rogers50b35e22012-10-04 10:09:15 -070089 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070090 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070091 if (UNLIKELY(component_count < 0)) {
Ian Rogers50b35e22012-10-04 10:09:15 -070092 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070093 return NULL; // Failure
94 }
95 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
96 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
97 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
98 if (klass == NULL) { // Error
99 DCHECK(Thread::Current()->IsExceptionPending());
100 return NULL; // Failure
101 }
102 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
103 }
104 if (access_check) {
105 Class* referrer = method->GetDeclaringClass();
106 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700107 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700108 return NULL; // Failure
109 }
110 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700111 return Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700112}
113
Mathieu Chartier66f19252012-09-18 08:57:04 -0700114extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, AbstractMethod* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700117
Ian Rogers08f753d2012-08-24 14:35:25 -0700118// Type of find field operation for fast and slow case.
119enum FindFieldType {
120 InstanceObjectRead,
121 InstanceObjectWrite,
122 InstancePrimitiveRead,
123 InstancePrimitiveWrite,
124 StaticObjectRead,
125 StaticObjectWrite,
126 StaticPrimitiveRead,
127 StaticPrimitiveWrite,
128};
129
130// Slow field find that can initialize classes and may throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700131extern Field* FindFieldFromCode(uint32_t field_idx, const AbstractMethod* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700132 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700134
Ian Rogers08f753d2012-08-24 14:35:25 -0700135// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700136static inline Field* FindFieldFast(uint32_t field_idx, const AbstractMethod* referrer,
Ian Rogers08f753d2012-08-24 14:35:25 -0700137 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 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
140 if (UNLIKELY(resolved_field == NULL)) {
141 return NULL;
142 }
143 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700144 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700145 if (UNLIKELY(!fields_class->IsInitializing())) {
146 return NULL;
147 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700148 // Check for incompatible class change.
149 bool is_primitive;
150 bool is_set;
151 bool is_static;
152 switch (type) {
153 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
154 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
155 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
156 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
157 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
158 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
159 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
160 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
161 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
162 is_primitive = true; is_set = true; is_static = true; break;
163 }
164 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
165 // Incompatible class change.
166 return NULL;
167 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700168 Class* referring_class = referrer->GetDeclaringClass();
169 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
170 !referring_class->CanAccessMember(fields_class,
171 resolved_field->GetAccessFlags()) ||
172 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700173 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700174 return NULL;
175 }
176 FieldHelper fh(resolved_field);
177 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
178 fh.FieldSize() != expected_size)) {
179 return NULL;
180 }
181 return resolved_field;
182}
183
Ian Rogers08f753d2012-08-24 14:35:25 -0700184// Fast path method resolution that can't throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700185static inline AbstractMethod* FindMethodFast(uint32_t method_idx, Object* this_object,
186 const AbstractMethod* referrer, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700188 bool is_direct = type == kStatic || type == kDirect;
189 if (UNLIKELY(this_object == NULL && !is_direct)) {
190 return NULL;
191 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700192 AbstractMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700193 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
194 if (UNLIKELY(resolved_method == NULL)) {
195 return NULL;
196 }
197 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700198 // Check for incompatible class change errors and access.
199 bool icce = resolved_method->CheckIncompatibleClassChange(type);
200 if (UNLIKELY(icce)) {
201 return NULL;
202 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700203 Class* methods_class = resolved_method->GetDeclaringClass();
204 Class* referring_class = referrer->GetDeclaringClass();
205 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
206 !referring_class->CanAccessMember(methods_class,
207 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700208 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700209 return NULL;
210 }
211 }
212 if (type == kInterface) { // Most common form of slow path dispatch.
213 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
214 } else if (is_direct) {
215 return resolved_method;
216 } else if (type == kSuper) {
217 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
218 Get(resolved_method->GetMethodIndex());
219 } else {
220 DCHECK(type == kVirtual);
221 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
222 }
223}
224
Mathieu Chartier66f19252012-09-18 08:57:04 -0700225extern AbstractMethod* FindMethodFromCode(uint32_t method_idx, Object* this_object, const AbstractMethod* referrer,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700228
Mathieu Chartier66f19252012-09-18 08:57:04 -0700229extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const AbstractMethod* referrer, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 bool can_run_clinit, bool verify_access)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700232
Mathieu Chartier66f19252012-09-18 08:57:04 -0700233static inline String* ResolveStringFromCode(const AbstractMethod* referrer, uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700235 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
236 return class_linker->ResolveString(string_idx, referrer);
237}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700238
TDYa1273d71d802012-08-15 03:47:03 -0700239static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700241 UNLOCK_FUNCTION(monitor_lock_) {
242 // Save any pending exception over monitor exit call.
243 Throwable* saved_exception = NULL;
244 if (UNLIKELY(self->IsExceptionPending())) {
245 saved_exception = self->GetException();
246 self->ClearException();
247 }
248 // Decode locked object and unlock, before popping local references.
249 self->DecodeJObject(locked)->MonitorExit(self);
250 if (UNLIKELY(self->IsExceptionPending())) {
251 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
252 << saved_exception->Dump()
253 << "\nEncountered second exception during implicit MonitorExit:\n"
254 << self->GetException()->Dump();
255 }
256 // Restore pending exception.
257 if (saved_exception != NULL) {
258 self->SetException(saved_exception);
259 }
260}
261
262static inline void CheckReferenceResult(Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700264 if (o == NULL) {
265 return;
266 }
267 if (o == kInvalidIndirectRefObject) {
268 JniAbortF(NULL, "invalid reference returned from %s",
269 PrettyMethod(self->GetCurrentMethod()).c_str());
270 }
271 // Make sure that the result is an instance of the type this method was expected to return.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700272 AbstractMethod* m = self->GetCurrentMethod();
TDYa1273d71d802012-08-15 03:47:03 -0700273 MethodHelper mh(m);
274 Class* return_type = mh.GetReturnType();
275
276 if (!o->InstanceOf(return_type)) {
277 JniAbortF(NULL, "attempt to return an instance of %s from %s",
278 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
279 }
280}
281
Shih-wei Liao2d831012011-09-28 22:06:53 -0700282} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700283
buzbee54330722011-08-23 16:46:55 -0700284#endif // ART_SRC_RUNTIME_SUPPORT_H_