blob: eff50b37105cc953ba2015384275f352e72d1dae [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 }
81 return klass->AllocObject();
82}
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 Rogers87e552d2012-08-31 15:54:48 -070089 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)) {
92 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
93 component_count);
94 return NULL; // Failure
95 }
96 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
97 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
98 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
99 if (klass == NULL) { // Error
100 DCHECK(Thread::Current()->IsExceptionPending());
101 return NULL; // Failure
102 }
103 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
104 }
105 if (access_check) {
106 Class* referrer = method->GetDeclaringClass();
107 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700108 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700109 return NULL; // Failure
110 }
111 }
112 return Array::Alloc(klass, component_count);
113}
114
Mathieu Chartier66f19252012-09-18 08:57:04 -0700115extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, AbstractMethod* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700118
Ian Rogers08f753d2012-08-24 14:35:25 -0700119// Type of find field operation for fast and slow case.
120enum FindFieldType {
121 InstanceObjectRead,
122 InstanceObjectWrite,
123 InstancePrimitiveRead,
124 InstancePrimitiveWrite,
125 StaticObjectRead,
126 StaticObjectWrite,
127 StaticPrimitiveRead,
128 StaticPrimitiveWrite,
129};
130
131// Slow field find that can initialize classes and may throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700132extern Field* FindFieldFromCode(uint32_t field_idx, const AbstractMethod* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700133 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700135
Ian Rogers08f753d2012-08-24 14:35:25 -0700136// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700137static inline Field* FindFieldFast(uint32_t field_idx, const AbstractMethod* referrer,
Ian Rogers08f753d2012-08-24 14:35:25 -0700138 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 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
141 if (UNLIKELY(resolved_field == NULL)) {
142 return NULL;
143 }
144 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700145 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700146 if (UNLIKELY(!fields_class->IsInitializing())) {
147 return NULL;
148 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700149 // Check for incompatible class change.
150 bool is_primitive;
151 bool is_set;
152 bool is_static;
153 switch (type) {
154 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
155 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
156 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
157 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
158 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
159 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
160 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
161 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
162 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
163 is_primitive = true; is_set = true; is_static = true; break;
164 }
165 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
166 // Incompatible class change.
167 return NULL;
168 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700169 Class* referring_class = referrer->GetDeclaringClass();
170 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
171 !referring_class->CanAccessMember(fields_class,
172 resolved_field->GetAccessFlags()) ||
173 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700174 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700175 return NULL;
176 }
177 FieldHelper fh(resolved_field);
178 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
179 fh.FieldSize() != expected_size)) {
180 return NULL;
181 }
182 return resolved_field;
183}
184
Ian Rogers08f753d2012-08-24 14:35:25 -0700185// Fast path method resolution that can't throw exceptions.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700186static inline AbstractMethod* FindMethodFast(uint32_t method_idx, Object* this_object,
187 const AbstractMethod* referrer, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700189 bool is_direct = type == kStatic || type == kDirect;
190 if (UNLIKELY(this_object == NULL && !is_direct)) {
191 return NULL;
192 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700193 AbstractMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700194 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
195 if (UNLIKELY(resolved_method == NULL)) {
196 return NULL;
197 }
198 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700199 // Check for incompatible class change errors and access.
200 bool icce = resolved_method->CheckIncompatibleClassChange(type);
201 if (UNLIKELY(icce)) {
202 return NULL;
203 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700204 Class* methods_class = resolved_method->GetDeclaringClass();
205 Class* referring_class = referrer->GetDeclaringClass();
206 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
207 !referring_class->CanAccessMember(methods_class,
208 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700209 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700210 return NULL;
211 }
212 }
213 if (type == kInterface) { // Most common form of slow path dispatch.
214 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
215 } else if (is_direct) {
216 return resolved_method;
217 } else if (type == kSuper) {
218 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
219 Get(resolved_method->GetMethodIndex());
220 } else {
221 DCHECK(type == kVirtual);
222 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
223 }
224}
225
Mathieu Chartier66f19252012-09-18 08:57:04 -0700226extern AbstractMethod* FindMethodFromCode(uint32_t method_idx, Object* this_object, const AbstractMethod* referrer,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700228 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700229
Mathieu Chartier66f19252012-09-18 08:57:04 -0700230extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const AbstractMethod* referrer, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 bool can_run_clinit, bool verify_access)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700233
Mathieu Chartier66f19252012-09-18 08:57:04 -0700234static inline String* ResolveStringFromCode(const AbstractMethod* referrer, uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700236 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
237 return class_linker->ResolveString(string_idx, referrer);
238}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700239
TDYa1273d71d802012-08-15 03:47:03 -0700240static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700242 UNLOCK_FUNCTION(monitor_lock_) {
243 // Save any pending exception over monitor exit call.
244 Throwable* saved_exception = NULL;
245 if (UNLIKELY(self->IsExceptionPending())) {
246 saved_exception = self->GetException();
247 self->ClearException();
248 }
249 // Decode locked object and unlock, before popping local references.
250 self->DecodeJObject(locked)->MonitorExit(self);
251 if (UNLIKELY(self->IsExceptionPending())) {
252 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
253 << saved_exception->Dump()
254 << "\nEncountered second exception during implicit MonitorExit:\n"
255 << self->GetException()->Dump();
256 }
257 // Restore pending exception.
258 if (saved_exception != NULL) {
259 self->SetException(saved_exception);
260 }
261}
262
263static inline void CheckReferenceResult(Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700264 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700265 if (o == NULL) {
266 return;
267 }
268 if (o == kInvalidIndirectRefObject) {
269 JniAbortF(NULL, "invalid reference returned from %s",
270 PrettyMethod(self->GetCurrentMethod()).c_str());
271 }
272 // Make sure that the result is an instance of the type this method was expected to return.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700273 AbstractMethod* m = self->GetCurrentMethod();
TDYa1273d71d802012-08-15 03:47:03 -0700274 MethodHelper mh(m);
275 Class* return_type = mh.GetReturnType();
276
277 if (!o->InstanceOf(return_type)) {
278 JniAbortF(NULL, "attempt to return an instance of %s from %s",
279 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
280 }
281}
282
Shih-wei Liao2d831012011-09-28 22:06:53 -0700283} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700284
buzbee54330722011-08-23 16:46:55 -0700285#endif // ART_SRC_RUNTIME_SUPPORT_H_