blob: 5b2c58c55cc8336c1a658aa25538cf4567b57b6f [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
Elliott Hughes0f3c5532012-03-30 14:51:51 -07002 * Copyright (C) 2012 The Android Open Source Project
Shih-wei Liao2d831012011-09-28 22:06:53 -07003 *
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 */
16
17#include "runtime_support.h"
18
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070020#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "gc/card_table-inl.h"
22#include "mirror/abstract_method-inl.h"
23#include "mirror/class-inl.h"
24#include "mirror/field-inl.h"
25#include "mirror/object-inl.h"
26#include "mirror/object_array-inl.h"
27#include "mirror/proxy.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080028#include "reflection.h"
29#include "scoped_thread_state_change.h"
TDYa1275bb86012012-04-11 05:57:28 -070030#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070031#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070032
jeffhao41005dd2012-05-09 17:58:52 -070033double art_l2d(int64_t l) {
Elliott Hughes74847412012-06-20 18:10:21 -070034 return static_cast<double>(l);
jeffhao41005dd2012-05-09 17:58:52 -070035}
36
37float art_l2f(int64_t l) {
Elliott Hughes74847412012-06-20 18:10:21 -070038 return static_cast<float>(l);
jeffhao41005dd2012-05-09 17:58:52 -070039}
Shih-wei Liao2d831012011-09-28 22:06:53 -070040
Ian Rogers776ac1f2012-04-13 23:36:36 -070041/*
42 * Float/double conversion requires clamping to min and max of integer form. If
43 * target doesn't support this normally, use these.
44 */
jeffhao41005dd2012-05-09 17:58:52 -070045int64_t art_d2l(double d) {
Elliott Hughes74847412012-06-20 18:10:21 -070046 static const double kMaxLong = static_cast<double>(static_cast<int64_t>(0x7fffffffffffffffULL));
47 static const double kMinLong = static_cast<double>(static_cast<int64_t>(0x8000000000000000ULL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070048 if (d >= kMaxLong) {
Logan Chien008fa512012-06-22 08:09:57 -070049 return static_cast<int64_t>(0x7fffffffffffffffULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070050 } else if (d <= kMinLong) {
Logan Chien008fa512012-06-22 08:09:57 -070051 return static_cast<int64_t>(0x8000000000000000ULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 } else if (d != d) { // NaN case
53 return 0;
54 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070055 return static_cast<int64_t>(d);
Ian Rogers776ac1f2012-04-13 23:36:36 -070056 }
57}
58
jeffhao41005dd2012-05-09 17:58:52 -070059int64_t art_f2l(float f) {
Elliott Hughes74847412012-06-20 18:10:21 -070060 static const float kMaxLong = static_cast<float>(static_cast<int64_t>(0x7fffffffffffffffULL));
61 static const float kMinLong = static_cast<float>(static_cast<int64_t>(0x8000000000000000ULL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070062 if (f >= kMaxLong) {
Logan Chien008fa512012-06-22 08:09:57 -070063 return static_cast<int64_t>(0x7fffffffffffffffULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070064 } else if (f <= kMinLong) {
Logan Chien008fa512012-06-22 08:09:57 -070065 return static_cast<int64_t>(0x8000000000000000ULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070066 } else if (f != f) { // NaN case
67 return 0;
68 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070069 return static_cast<int64_t>(f);
Ian Rogers776ac1f2012-04-13 23:36:36 -070070 }
71}
72
jeffhao41005dd2012-05-09 17:58:52 -070073int32_t art_d2i(double d) {
Logan Chien008fa512012-06-22 08:09:57 -070074 static const double kMaxInt = static_cast<double>(static_cast<int32_t>(0x7fffffffUL));
75 static const double kMinInt = static_cast<double>(static_cast<int32_t>(0x80000000UL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070076 if (d >= kMaxInt) {
Logan Chien008fa512012-06-22 08:09:57 -070077 return static_cast<int32_t>(0x7fffffffUL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070078 } else if (d <= kMinInt) {
Logan Chien008fa512012-06-22 08:09:57 -070079 return static_cast<int32_t>(0x80000000UL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070080 } else if (d != d) { // NaN case
81 return 0;
82 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070083 return static_cast<int32_t>(d);
Ian Rogers776ac1f2012-04-13 23:36:36 -070084 }
85}
86
jeffhao41005dd2012-05-09 17:58:52 -070087int32_t art_f2i(float f) {
Logan Chien008fa512012-06-22 08:09:57 -070088 static const float kMaxInt = static_cast<float>(static_cast<int32_t>(0x7fffffffUL));
89 static const float kMinInt = static_cast<float>(static_cast<int32_t>(0x80000000UL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070090 if (f >= kMaxInt) {
Logan Chien008fa512012-06-22 08:09:57 -070091 return static_cast<int32_t>(0x7fffffffUL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070092 } else if (f <= kMinInt) {
Logan Chien008fa512012-06-22 08:09:57 -070093 return static_cast<int32_t>(0x80000000UL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070094 } else if (f != f) { // NaN case
95 return 0;
96 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070097 return static_cast<int32_t>(f);
Ian Rogers776ac1f2012-04-13 23:36:36 -070098 }
99}
100
jeffhao41005dd2012-05-09 17:58:52 -0700101namespace art {
102
Ian Rogers57b86d42012-03-27 16:05:41 -0700103// Helper function to allocate array for FILLED_NEW_ARRAY.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
105 int32_t component_count, Thread* self,
106 bool access_check) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700107 if (UNLIKELY(component_count < 0)) {
108 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
109 return NULL; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700110 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700112 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
113 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
114 if (klass == NULL) { // Error
Ian Rogers50b35e22012-10-04 10:09:15 -0700115 DCHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -0700116 return NULL; // Failure
Ian Rogers19846512012-02-24 11:42:47 -0800117 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700118 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700119 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
120 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700121 self->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
122 "Bad filled array request for type %s",
123 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800124 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -0700125 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
126 "Found type %s; filled-new-array not implemented for anything but \'int\'",
127 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800128 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700129 return NULL; // Failure
Ian Rogersad25ac52011-10-04 19:13:33 -0700130 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700131 if (access_check) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700133 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700134 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700135 return NULL; // Failure
136 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800137 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700138 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700140 }
141}
142
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143mirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer,
144 Thread* self, FindFieldType type, size_t expected_size) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700145 bool is_primitive;
146 bool is_set;
147 bool is_static;
148 switch (type) {
149 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
150 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
151 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
152 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
153 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
154 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
155 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
156 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
157 default: is_primitive = true; is_set = true; is_static = true; break;
158 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700159 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 mirror::Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogers57b86d42012-03-27 16:05:41 -0700161 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700162 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
163 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700164 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700165 if (resolved_field->IsStatic() != is_static) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700166 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700167 return NULL;
168 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
170 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700171 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
172 !referring_class->CanAccessMember(fields_class,
173 resolved_field->GetAccessFlags()))) {
174 // The referring class can't access the resolved field, this may occur as a result of a
175 // protected field being made public by a sub-class. Resort to the dex file to determine
176 // the correct class for the access check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700177 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogerse2645d32012-04-11 14:42:42 -0700178 fields_class = class_linker->ResolveType(dex_file,
179 dex_file.GetFieldId(field_idx).class_idx_,
180 referring_class);
181 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700182 ThrowIllegalAccessErrorClass(referring_class, fields_class);
Ian Rogerse2645d32012-04-11 14:42:42 -0700183 return NULL; // failure
184 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
185 resolved_field->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700186 ThrowIllegalAccessErrorField(referring_class, resolved_field);
Ian Rogerse2645d32012-04-11 14:42:42 -0700187 return NULL; // failure
188 }
189 }
190 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700191 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Ian Rogers57b86d42012-03-27 16:05:41 -0700192 return NULL; // failure
193 } else {
194 FieldHelper fh(resolved_field);
195 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
196 fh.FieldSize() != expected_size)) {
197 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
198 "Attempted read of %zd-bit %s on field '%s'",
199 expected_size * (32 / sizeof(int32_t)),
200 is_primitive ? "primitive" : "non-primitive",
201 PrettyField(resolved_field, true).c_str());
202 return NULL; // failure
203 } else if (!is_static) {
204 // instance fields must be being accessed on an initialized class
205 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800206 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700207 // If the class is already initializing, we must be inside <clinit>, or
208 // we'd still be waiting for the lock.
209 if (fields_class->IsInitializing()) {
210 return resolved_field;
Ian Rogers0045a292012-03-31 21:08:41 -0700211 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700212 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800213 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700214 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
215 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800216 }
217 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700218 }
219 }
220}
221
222// Slow path method resolution
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223mirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
224 mirror::AbstractMethod* referrer,
225 Thread* self, bool access_check, InvokeType type) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700226 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
227 bool is_direct = type == kStatic || type == kDirect;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 mirror::AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700229 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700230 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
231 return NULL; // Failure.
jeffhao262e2512012-12-11 09:46:43 -0800232 } else if (UNLIKELY(this_object == NULL && type != kStatic)) {
233 // Maintain interpreter-like semantics where NullPointerException is thrown
234 // after potential NoSuchMethodError from class linker.
235 ThrowNullPointerExceptionForMethodAccess(referrer, method_idx, type);
236 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700237 } else {
238 if (!access_check) {
239 if (is_direct) {
240 return resolved_method;
241 } else if (type == kInterface) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 mirror::AbstractMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700243 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
244 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700245 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
246 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700247 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700248 } else {
249 return interface_method;
250 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800251 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 mirror::ObjectArray<mirror::AbstractMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700253 uint16_t vtable_index = resolved_method->GetMethodIndex();
254 if (type == kSuper) {
255 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
256 } else {
257 vtable = this_object->GetClass()->GetVTable();
258 }
259 // TODO: eliminate bounds check?
260 return vtable->Get(vtable_index);
261 }
262 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700263 // Incompatible class change should have been handled in resolve method.
264 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
Ian Rogers2fc14272012-08-30 10:56:57 -0700265 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
266 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700267 return NULL; // Failure.
268 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
270 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700271 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
272 !referring_class->CanAccessMember(methods_class,
273 resolved_method->GetAccessFlags()))) {
274 // The referring class can't access the resolved method, this may occur as a result of a
275 // protected method being made public by implementing an interface that re-declares the
276 // method public. Resort to the dex file to determine the correct class for the access check
Ian Rogers4445a7e2012-10-05 17:19:13 -0700277 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogers57b86d42012-03-27 16:05:41 -0700278 methods_class = class_linker->ResolveType(dex_file,
279 dex_file.GetMethodId(method_idx).class_idx_,
280 referring_class);
281 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700282 ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
283 referrer, resolved_method, type);
Ian Rogers08f753d2012-08-24 14:35:25 -0700284 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700285 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
286 resolved_method->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700287 ThrowIllegalAccessErrorMethod(referring_class, resolved_method);
Ian Rogers08f753d2012-08-24 14:35:25 -0700288 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700289 }
290 }
291 if (is_direct) {
292 return resolved_method;
293 } else if (type == kInterface) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 mirror::AbstractMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700295 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
296 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700297 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
298 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700299 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700300 } else {
301 return interface_method;
302 }
303 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 mirror::ObjectArray<mirror::AbstractMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700305 uint16_t vtable_index = resolved_method->GetMethodIndex();
306 if (type == kSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 mirror::Class* super_class = referring_class->GetSuperClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700308 if (LIKELY(super_class != NULL)) {
309 vtable = referring_class->GetSuperClass()->GetVTable();
310 } else {
311 vtable = NULL;
312 }
313 } else {
314 vtable = this_object->GetClass()->GetVTable();
315 }
316 if (LIKELY(vtable != NULL &&
317 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
318 return vtable->GetWithoutChecks(vtable_index);
319 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700320 // Behavior to agree with that of the verifier.
321 MethodHelper mh(resolved_method);
322 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
Ian Rogers2fc14272012-08-30 10:56:57 -0700323 mh.GetSignature(), referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700324 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700325 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800326 }
327 }
328 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800329}
330
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, const mirror::AbstractMethod* referrer,
332 Thread* self, bool can_run_clinit, bool verify_access) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700333 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers57b86d42012-03-27 16:05:41 -0700335 if (UNLIKELY(klass == NULL)) {
jeffhao441d9122012-03-21 17:29:10 -0700336 CHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -0700337 return NULL; // Failure - Indicate to caller to deliver exception
jeffhao441d9122012-03-21 17:29:10 -0700338 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700339 // Perform access check if necessary.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700341 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700342 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700343 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogers14b1b242011-10-11 18:54:34 -0700344 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700345 // If we're just implementing const-class, we shouldn't call <clinit>.
346 if (!can_run_clinit) {
347 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700348 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700349 // If we are the <clinit> of this class, just return our storage.
350 //
351 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
352 // running.
353 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
354 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700355 }
Ian Rogers0045a292012-03-31 21:08:41 -0700356 if (!class_linker->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700357 CHECK(self->IsExceptionPending());
358 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700359 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700360 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
361 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700362}
363
jeffhaod7521322012-11-21 15:38:24 -0800364void ThrowStackOverflowError(Thread* self) {
365 CHECK(!self->IsHandlingStackOverflow()) << "Recursive stack overflow.";
366 // Remove extra entry pushed onto second stack during method tracing.
367 if (Runtime::Current()->IsMethodTracingActive()) {
368 InstrumentationMethodUnwindFromCode(self);
369 }
370 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
371 JNIEnvExt* env = self->GetJniEnv();
372 std::string msg("stack size ");
373 msg += PrettySize(self->GetStackSize());
374 // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
375 // would consume more stack.
376 int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
377 msg.c_str(), NULL);
378 if (rc != JNI_OK) {
379 // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
380 // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
381 // instead.
382 LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
383 CHECK(self->IsExceptionPending());
384 }
385 self->ResetDefaultStackEnd(); // Return to default stack size.
386}
387
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800388JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
389 jobject rcvr_jobj, jobject interface_method_jobj,
390 std::vector<jvalue>& args) {
391 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
392
393 // Build argument array possibly triggering GC.
394 soa.Self()->AssertThreadSuspensionIsAllowable();
395 jobjectArray args_jobj = NULL;
396 const JValue zero;
397 if (args.size() > 0) {
398 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
399 if (args_jobj == NULL) {
400 CHECK(soa.Self()->IsExceptionPending());
401 return zero;
402 }
403 for (size_t i = 0; i < args.size(); ++i) {
404 if (shorty[i + 1] == 'L') {
405 jobject val = args.at(i).l;
406 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
407 } else {
408 JValue jv;
409 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800411 if (val == NULL) {
412 CHECK(soa.Self()->IsExceptionPending());
413 return zero;
414 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800415 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800416 }
417 }
418 }
419
420 // Call InvocationHandler.invoke(Object proxy, Method method, Object[] args).
421 jobject inv_hand = soa.Env()->GetObjectField(rcvr_jobj,
422 WellKnownClasses::java_lang_reflect_Proxy_h);
423 jvalue invocation_args[3];
424 invocation_args[0].l = rcvr_jobj;
425 invocation_args[1].l = interface_method_jobj;
426 invocation_args[2].l = args_jobj;
427 jobject result =
428 soa.Env()->CallObjectMethodA(inv_hand,
429 WellKnownClasses::java_lang_reflect_InvocationHandler_invoke,
430 invocation_args);
431
432 // Unbox result and handle error conditions.
433 if (!soa.Self()->IsExceptionPending()) {
434 if (shorty[0] == 'V' || result == NULL) {
435 // Do nothing.
436 return zero;
437 } else {
438 JValue result_unboxed;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439 MethodHelper mh(soa.Decode<mirror::AbstractMethod*>(interface_method_jobj));
440 mirror::Class* result_type = mh.GetReturnType();
441 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800442 bool unboxed_okay = UnboxPrimitiveForResult(result_ref, result_type, result_unboxed);
443 if (!unboxed_okay) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800444 // UnboxPrimitiveForResult creates an IllegalArgumentException. Discard and create a
445 // meaningful ClassCastException.
446 DCHECK(soa.Self()->IsExceptionPending());
447 soa.Self()->ClearException();
448 soa.Self()->ThrowNewException("Ljava/lang/ClassCastException;",
449 StringPrintf("Couldn't convert result of type %s to %s",
450 PrettyTypeOf(result_ref).c_str(),
451 PrettyDescriptor(result_type).c_str()
452 ).c_str());
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800453 }
454 return result_unboxed;
455 }
456 } else {
457 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
458 // a UndeclaredThrowableException.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459 mirror::Throwable* exception = soa.Self()->GetException();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800460 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
462 mirror::SynthesizedProxyClass* proxy_class =
463 down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass());
464 mirror::AbstractMethod* interface_method =
465 soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
466 mirror::AbstractMethod* proxy_method =
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800467 rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
468 int throws_index = -1;
469 size_t num_virt_methods = proxy_class->NumVirtualMethods();
470 for (size_t i = 0; i < num_virt_methods; i++) {
471 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
472 throws_index = i;
473 break;
474 }
475 }
476 CHECK_NE(throws_index, -1);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477 mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
478 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800479 bool declares_exception = false;
480 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800482 declares_exception = declared_exception->IsAssignableFrom(exception_class);
483 }
484 if (!declares_exception) {
485 soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;",
486 NULL);
487 }
488 }
489 return zero;
490 }
491}
492
Shih-wei Liao2d831012011-09-28 22:06:53 -0700493} // namespace art