blob: 16f0d2ec6e1d2a0b4e26f5e7e25eee128082ad16 [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"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070023#include "invoke_type.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -070024#include "object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070025#include "object_utils.h"
26#include "thread.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027#include "verifier/method_verifier.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070028
29extern "C" void art_proxy_invoke_handler();
30extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070031
jeffhao41005dd2012-05-09 17:58:52 -070032extern "C" double art_l2d(int64_t l);
33extern "C" float art_l2f(int64_t l);
34extern "C" int64_t art_d2l(double d);
35extern "C" int32_t art_d2i(double d);
36extern "C" int64_t art_f2l(float f);
37extern "C" int32_t art_f2i(float f);
38
Shih-wei Liao2d831012011-09-28 22:06:53 -070039namespace art {
40
Ian Rogers57b86d42012-03-27 16:05:41 -070041class Array;
42class Class;
43class Field;
44class Method;
45class Object;
46
Ian Rogers57b86d42012-03-27 16:05:41 -070047// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
48// cannot be resolved, throw an error. If it can, use it to create an instance.
49// When verification/compiler hasn't been able to verify access, optionally perform an access
50// check.
51static inline Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070053 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070054 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
55 Runtime* runtime = Runtime::Current();
56 if (UNLIKELY(klass == NULL)) {
57 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
58 if (klass == NULL) {
59 DCHECK(self->IsExceptionPending());
60 return NULL; // Failure
61 }
62 }
63 if (access_check) {
64 if (UNLIKELY(!klass->IsInstantiable())) {
65 self->ThrowNewException("Ljava/lang/InstantiationError;",
66 PrettyDescriptor(klass).c_str());
67 return NULL; // Failure
68 }
69 Class* referrer = method->GetDeclaringClass();
70 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -070071 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070072 return NULL; // Failure
73 }
74 }
Ian Rogers0045a292012-03-31 21:08:41 -070075 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -070076 DCHECK(self->IsExceptionPending());
77 return NULL; // Failure
78 }
79 return klass->AllocObject();
80}
81
82// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
83// it cannot be resolved, throw an error. If it can, use it to create an array.
84// When verification/compiler hasn't been able to verify access, optionally perform an access
85// check.
86static inline Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers87e552d2012-08-31 15:54:48 -070087 bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070089 if (UNLIKELY(component_count < 0)) {
90 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
91 component_count);
92 return NULL; // Failure
93 }
94 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
95 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
96 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
97 if (klass == NULL) { // Error
98 DCHECK(Thread::Current()->IsExceptionPending());
99 return NULL; // Failure
100 }
101 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
102 }
103 if (access_check) {
104 Class* referrer = method->GetDeclaringClass();
105 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700106 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700107 return NULL; // Failure
108 }
109 }
110 return Array::Alloc(klass, component_count);
111}
112
Ian Rogersce9eca62011-10-07 17:11:03 -0700113extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 Thread* self, bool access_check)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700116
Ian Rogers08f753d2012-08-24 14:35:25 -0700117// Type of find field operation for fast and slow case.
118enum FindFieldType {
119 InstanceObjectRead,
120 InstanceObjectWrite,
121 InstancePrimitiveRead,
122 InstancePrimitiveWrite,
123 StaticObjectRead,
124 StaticObjectWrite,
125 StaticPrimitiveRead,
126 StaticPrimitiveWrite,
127};
128
129// Slow field find that can initialize classes and may throw exceptions.
Ian Rogers1bddec32012-02-04 12:27:34 -0800130extern Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700131 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700133
Ian Rogers08f753d2012-08-24 14:35:25 -0700134// Fast path field resolution that can't initialize classes or throw exceptions.
135static inline Field* FindFieldFast(uint32_t field_idx, const Method* referrer,
136 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700138 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
139 if (UNLIKELY(resolved_field == NULL)) {
140 return NULL;
141 }
142 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700143 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700144 if (UNLIKELY(!fields_class->IsInitializing())) {
145 return NULL;
146 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700147 // Check for incompatible class change.
148 bool is_primitive;
149 bool is_set;
150 bool is_static;
151 switch (type) {
152 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
153 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
154 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
155 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
156 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
157 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
158 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
159 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
160 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
161 is_primitive = true; is_set = true; is_static = true; break;
162 }
163 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
164 // Incompatible class change.
165 return NULL;
166 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700167 Class* referring_class = referrer->GetDeclaringClass();
168 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
169 !referring_class->CanAccessMember(fields_class,
170 resolved_field->GetAccessFlags()) ||
171 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700172 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700173 return NULL;
174 }
175 FieldHelper fh(resolved_field);
176 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
177 fh.FieldSize() != expected_size)) {
178 return NULL;
179 }
180 return resolved_field;
181}
182
Ian Rogers08f753d2012-08-24 14:35:25 -0700183// Fast path method resolution that can't throw exceptions.
184static inline Method* FindMethodFast(uint32_t method_idx, Object* this_object,
185 const Method* referrer, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700187 bool is_direct = type == kStatic || type == kDirect;
188 if (UNLIKELY(this_object == NULL && !is_direct)) {
189 return NULL;
190 }
191 Method* resolved_method =
192 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
193 if (UNLIKELY(resolved_method == NULL)) {
194 return NULL;
195 }
196 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700197 // Check for incompatible class change errors and access.
198 bool icce = resolved_method->CheckIncompatibleClassChange(type);
199 if (UNLIKELY(icce)) {
200 return NULL;
201 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700202 Class* methods_class = resolved_method->GetDeclaringClass();
203 Class* referring_class = referrer->GetDeclaringClass();
204 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
205 !referring_class->CanAccessMember(methods_class,
206 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700207 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700208 return NULL;
209 }
210 }
211 if (type == kInterface) { // Most common form of slow path dispatch.
212 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
213 } else if (is_direct) {
214 return resolved_method;
215 } else if (type == kSuper) {
216 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
217 Get(resolved_method->GetMethodIndex());
218 } else {
219 DCHECK(type == kVirtual);
220 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
221 }
222}
223
224extern Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 Thread* self, bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700227
Elliott Hughesf3778f62012-01-26 14:14:35 -0800228extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 bool can_run_clinit, bool verify_access)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700231
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232static inline String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700234 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
235 return class_linker->ResolveString(string_idx, referrer);
236}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700237
TDYa1273d71d802012-08-15 03:47:03 -0700238static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700240 UNLOCK_FUNCTION(monitor_lock_) {
241 // Save any pending exception over monitor exit call.
242 Throwable* saved_exception = NULL;
243 if (UNLIKELY(self->IsExceptionPending())) {
244 saved_exception = self->GetException();
245 self->ClearException();
246 }
247 // Decode locked object and unlock, before popping local references.
248 self->DecodeJObject(locked)->MonitorExit(self);
249 if (UNLIKELY(self->IsExceptionPending())) {
250 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
251 << saved_exception->Dump()
252 << "\nEncountered second exception during implicit MonitorExit:\n"
253 << self->GetException()->Dump();
254 }
255 // Restore pending exception.
256 if (saved_exception != NULL) {
257 self->SetException(saved_exception);
258 }
259}
260
261static inline void CheckReferenceResult(Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700263 if (o == NULL) {
264 return;
265 }
266 if (o == kInvalidIndirectRefObject) {
267 JniAbortF(NULL, "invalid reference returned from %s",
268 PrettyMethod(self->GetCurrentMethod()).c_str());
269 }
270 // Make sure that the result is an instance of the type this method was expected to return.
271 Method* m = self->GetCurrentMethod();
272 MethodHelper mh(m);
273 Class* return_type = mh.GetReturnType();
274
275 if (!o->InstanceOf(return_type)) {
276 JniAbortF(NULL, "attempt to return an instance of %s from %s",
277 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
278 }
279}
280
Shih-wei Liao2d831012011-09-28 22:06:53 -0700281} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700282
buzbee54330722011-08-23 16:46:55 -0700283#endif // ART_SRC_RUNTIME_SUPPORT_H_