blob: 260202bef1eff66ae2fbb4902221a25d4116e864 [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 Rogers57b86d42012-03-27 16:05:41 -070021#include "dex_file.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070022#include "invoke_type.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -070023#include "object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070024#include "object_utils.h"
25#include "thread.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070026#include "verifier/method_verifier.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070027
28extern "C" void art_proxy_invoke_handler();
29extern "C" void art_work_around_app_jni_bugs();
Shih-wei Liao2d831012011-09-28 22:06:53 -070030
jeffhao41005dd2012-05-09 17:58:52 -070031extern "C" double art_l2d(int64_t l);
32extern "C" float art_l2f(int64_t l);
33extern "C" int64_t art_d2l(double d);
34extern "C" int32_t art_d2i(double d);
35extern "C" int64_t art_f2l(float f);
36extern "C" int32_t art_f2i(float f);
37
Shih-wei Liao2d831012011-09-28 22:06:53 -070038namespace art {
39
Ian Rogers57b86d42012-03-27 16:05:41 -070040class Array;
41class Class;
42class Field;
43class Method;
44class Object;
45
46// Helpers to give consistent descriptive exception messages
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed)
48 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070049void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer,
50 Class* accessed,
51 const Method* caller,
52 const Method* called,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 InvokeType type)
54 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070055void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
56 const Method* referrer,
57 const Method* interface_method,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 Object* this_object)
59 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers08f753d2012-08-24 14:35:25 -070060void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
61 Method* method)
62 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
63void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name,
64 const StringPiece& signature)
65 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed)
67 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
68void ThrowNewIllegalAccessErrorFinalField(Thread* self, const Method* referrer, Field* accessed)
69 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed)
71 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
72void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read)
73 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070074void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller, uint32_t method_idx,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 InvokeType type)
76 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
77void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* caller, uint32_t dex_pc)
78 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
79void ThrowVerificationError(Thread* self, const Method* method, int32_t kind, int32_t ref)
80 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070081
82std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 verifier::VerifyErrorRefType ref_type, bool access)
84 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070085std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 verifier::VerifyErrorRefType ref_type, bool access)
87 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070088
89// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
90// cannot be resolved, throw an error. If it can, use it to create an instance.
91// When verification/compiler hasn't been able to verify access, optionally perform an access
92// check.
93static inline Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 bool access_check)
95 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070096 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
97 Runtime* runtime = Runtime::Current();
98 if (UNLIKELY(klass == NULL)) {
99 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
100 if (klass == NULL) {
101 DCHECK(self->IsExceptionPending());
102 return NULL; // Failure
103 }
104 }
105 if (access_check) {
106 if (UNLIKELY(!klass->IsInstantiable())) {
107 self->ThrowNewException("Ljava/lang/InstantiationError;",
108 PrettyDescriptor(klass).c_str());
109 return NULL; // Failure
110 }
111 Class* referrer = method->GetDeclaringClass();
112 if (UNLIKELY(!referrer->CanAccess(klass))) {
113 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
114 return NULL; // Failure
115 }
116 }
Ian Rogers0045a292012-03-31 21:08:41 -0700117 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700118 DCHECK(self->IsExceptionPending());
119 return NULL; // Failure
120 }
121 return klass->AllocObject();
122}
123
124// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
125// it cannot be resolved, throw an error. If it can, use it to create an array.
126// When verification/compiler hasn't been able to verify access, optionally perform an access
127// check.
128static inline Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 Thread* self, bool access_check)
130 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700131 if (UNLIKELY(component_count < 0)) {
132 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
133 component_count);
134 return NULL; // Failure
135 }
136 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
137 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
138 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
139 if (klass == NULL) { // Error
140 DCHECK(Thread::Current()->IsExceptionPending());
141 return NULL; // Failure
142 }
143 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
144 }
145 if (access_check) {
146 Class* referrer = method->GetDeclaringClass();
147 if (UNLIKELY(!referrer->CanAccess(klass))) {
148 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
149 return NULL; // Failure
150 }
151 }
152 return Array::Alloc(klass, component_count);
153}
154
Ian Rogersce9eca62011-10-07 17:11:03 -0700155extern Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700156 Thread* self, bool access_check)
157 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700158
Ian Rogers08f753d2012-08-24 14:35:25 -0700159// Type of find field operation for fast and slow case.
160enum FindFieldType {
161 InstanceObjectRead,
162 InstanceObjectWrite,
163 InstancePrimitiveRead,
164 InstancePrimitiveWrite,
165 StaticObjectRead,
166 StaticObjectWrite,
167 StaticPrimitiveRead,
168 StaticPrimitiveWrite,
169};
170
171// Slow field find that can initialize classes and may throw exceptions.
Ian Rogers1bddec32012-02-04 12:27:34 -0800172extern Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700173 FindFieldType type, size_t expected_size)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700175
Ian Rogers08f753d2012-08-24 14:35:25 -0700176// Fast path field resolution that can't initialize classes or throw exceptions.
177static inline Field* FindFieldFast(uint32_t field_idx, const Method* referrer,
178 FindFieldType type, size_t expected_size)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700180 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
181 if (UNLIKELY(resolved_field == NULL)) {
182 return NULL;
183 }
184 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700185 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700186 if (UNLIKELY(!fields_class->IsInitializing())) {
187 return NULL;
188 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700189 // Check for incompatible class change.
190 bool is_primitive;
191 bool is_set;
192 bool is_static;
193 switch (type) {
194 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
195 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
196 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
197 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
198 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
199 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
200 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
201 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
202 default: LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
203 is_primitive = true; is_set = true; is_static = true; break;
204 }
205 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
206 // Incompatible class change.
207 return NULL;
208 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700209 Class* referring_class = referrer->GetDeclaringClass();
210 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
211 !referring_class->CanAccessMember(fields_class,
212 resolved_field->GetAccessFlags()) ||
213 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700214 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700215 return NULL;
216 }
217 FieldHelper fh(resolved_field);
218 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
219 fh.FieldSize() != expected_size)) {
220 return NULL;
221 }
222 return resolved_field;
223}
224
Ian Rogers08f753d2012-08-24 14:35:25 -0700225// Fast path method resolution that can't throw exceptions.
226static inline Method* FindMethodFast(uint32_t method_idx, Object* this_object,
227 const Method* referrer, bool access_check, InvokeType type)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700229 bool is_direct = type == kStatic || type == kDirect;
230 if (UNLIKELY(this_object == NULL && !is_direct)) {
231 return NULL;
232 }
233 Method* resolved_method =
234 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
235 if (UNLIKELY(resolved_method == NULL)) {
236 return NULL;
237 }
238 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700239 // Check for incompatible class change errors and access.
240 bool icce = resolved_method->CheckIncompatibleClassChange(type);
241 if (UNLIKELY(icce)) {
242 return NULL;
243 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700244 Class* methods_class = resolved_method->GetDeclaringClass();
245 Class* referring_class = referrer->GetDeclaringClass();
246 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
247 !referring_class->CanAccessMember(methods_class,
248 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700249 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700250 return NULL;
251 }
252 }
253 if (type == kInterface) { // Most common form of slow path dispatch.
254 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
255 } else if (is_direct) {
256 return resolved_method;
257 } else if (type == kSuper) {
258 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
259 Get(resolved_method->GetMethodIndex());
260 } else {
261 DCHECK(type == kVirtual);
262 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
263 }
264}
265
266extern Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 Thread* self, bool access_check, InvokeType type)
268 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700269
Elliott Hughesf3778f62012-01-26 14:14:35 -0800270extern Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 bool can_run_clinit, bool verify_access)
272 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700273
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274static inline String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx)
275 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700276 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
277 return class_linker->ResolveString(string_idx, referrer);
278}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700279
TDYa1273d71d802012-08-15 03:47:03 -0700280static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
281 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
282 UNLOCK_FUNCTION(monitor_lock_) {
283 // Save any pending exception over monitor exit call.
284 Throwable* saved_exception = NULL;
285 if (UNLIKELY(self->IsExceptionPending())) {
286 saved_exception = self->GetException();
287 self->ClearException();
288 }
289 // Decode locked object and unlock, before popping local references.
290 self->DecodeJObject(locked)->MonitorExit(self);
291 if (UNLIKELY(self->IsExceptionPending())) {
292 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
293 << saved_exception->Dump()
294 << "\nEncountered second exception during implicit MonitorExit:\n"
295 << self->GetException()->Dump();
296 }
297 // Restore pending exception.
298 if (saved_exception != NULL) {
299 self->SetException(saved_exception);
300 }
301}
302
303static inline void CheckReferenceResult(Object* o, Thread* self)
304 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
305 if (o == NULL) {
306 return;
307 }
308 if (o == kInvalidIndirectRefObject) {
309 JniAbortF(NULL, "invalid reference returned from %s",
310 PrettyMethod(self->GetCurrentMethod()).c_str());
311 }
312 // Make sure that the result is an instance of the type this method was expected to return.
313 Method* m = self->GetCurrentMethod();
314 MethodHelper mh(m);
315 Class* return_type = mh.GetReturnType();
316
317 if (!o->InstanceOf(return_type)) {
318 JniAbortF(NULL, "attempt to return an instance of %s from %s",
319 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
320 }
321}
322
Shih-wei Liao2d831012011-09-28 22:06:53 -0700323} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700324
buzbee54330722011-08-23 16:46:55 -0700325#endif // ART_SRC_RUNTIME_SUPPORT_H_