blob: b601f8c5d9c6b9f765c7c2a337f36e202f6384ac [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 Rogers62d6c772013-02-27 08:32:07 -0800104mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 int32_t component_count, Thread* self,
106 bool access_check) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700107 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 ThrowNegativeArraySizeException(component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700109 return NULL; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700110 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800111 mirror::Class* klass = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700112 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Ian Rogers62d6c772013-02-27 08:32:07 -0800113 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, referrer);
Ian Rogers57b86d42012-03-27 16:05:41 -0700114 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 Rogers62d6c772013-02-27 08:32:07 -0800121 ThrowRuntimeException("Bad filled array request for type %s",
122 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800123 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
125 DCHECK(throw_location.GetMethod() == referrer);
126 self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
Ian Rogers50b35e22012-10-04 10:09:15 -0700127 "Found type %s; filled-new-array not implemented for anything but \'int\'",
128 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800129 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700130 return NULL; // Failure
Ian Rogersad25ac52011-10-04 19:13:33 -0700131 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700132 if (access_check) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 mirror::Class* referrer_klass = referrer->GetDeclaringClass();
134 if (UNLIKELY(!referrer_klass->CanAccess(klass))) {
135 ThrowIllegalAccessErrorClass(referrer_klass, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700136 return NULL; // Failure
137 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800138 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700139 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700141 }
142}
143
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144mirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer,
145 Thread* self, FindFieldType type, size_t expected_size) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700146 bool is_primitive;
147 bool is_set;
148 bool is_static;
149 switch (type) {
150 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
151 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
152 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
153 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
154 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
155 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
156 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
157 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
158 default: is_primitive = true; is_set = true; is_static = true; break;
159 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700160 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 mirror::Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogers57b86d42012-03-27 16:05:41 -0700162 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700163 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
164 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700165 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700166 if (resolved_field->IsStatic() != is_static) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700167 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700168 return NULL;
169 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
171 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700172 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
173 !referring_class->CanAccessMember(fields_class,
174 resolved_field->GetAccessFlags()))) {
175 // The referring class can't access the resolved field, this may occur as a result of a
176 // protected field being made public by a sub-class. Resort to the dex file to determine
177 // the correct class for the access check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700178 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogerse2645d32012-04-11 14:42:42 -0700179 fields_class = class_linker->ResolveType(dex_file,
180 dex_file.GetFieldId(field_idx).class_idx_,
181 referring_class);
182 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700183 ThrowIllegalAccessErrorClass(referring_class, fields_class);
Ian Rogerse2645d32012-04-11 14:42:42 -0700184 return NULL; // failure
185 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
186 resolved_field->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700187 ThrowIllegalAccessErrorField(referring_class, resolved_field);
Ian Rogerse2645d32012-04-11 14:42:42 -0700188 return NULL; // failure
189 }
190 }
191 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700192 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Ian Rogers57b86d42012-03-27 16:05:41 -0700193 return NULL; // failure
194 } else {
195 FieldHelper fh(resolved_field);
196 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
197 fh.FieldSize() != expected_size)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
199 DCHECK(throw_location.GetMethod() == referrer);
200 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Ian Rogers57b86d42012-03-27 16:05:41 -0700201 "Attempted read of %zd-bit %s on field '%s'",
202 expected_size * (32 / sizeof(int32_t)),
203 is_primitive ? "primitive" : "non-primitive",
204 PrettyField(resolved_field, true).c_str());
205 return NULL; // failure
206 } else if (!is_static) {
207 // instance fields must be being accessed on an initialized class
208 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800209 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700210 // If the class is already initializing, we must be inside <clinit>, or
211 // we'd still be waiting for the lock.
212 if (fields_class->IsInitializing()) {
213 return resolved_field;
Ian Rogers0045a292012-03-31 21:08:41 -0700214 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700215 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800216 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700217 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
218 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800219 }
220 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700221 }
222 }
223}
224
225// Slow path method resolution
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226mirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
227 mirror::AbstractMethod* referrer,
228 Thread* self, bool access_check, InvokeType type) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700229 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
230 bool is_direct = type == kStatic || type == kDirect;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 mirror::AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700232 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700233 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
234 return NULL; // Failure.
jeffhao262e2512012-12-11 09:46:43 -0800235 } else if (UNLIKELY(this_object == NULL && type != kStatic)) {
236 // Maintain interpreter-like semantics where NullPointerException is thrown
237 // after potential NoSuchMethodError from class linker.
Ian Rogers62d6c772013-02-27 08:32:07 -0800238 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
239 DCHECK(referrer == throw_location.GetMethod());
240 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
jeffhao262e2512012-12-11 09:46:43 -0800241 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700242 } else {
243 if (!access_check) {
244 if (is_direct) {
245 return resolved_method;
246 } else if (type == kInterface) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 mirror::AbstractMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700248 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
249 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700250 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
251 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700252 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700253 } else {
254 return interface_method;
255 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800256 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 mirror::ObjectArray<mirror::AbstractMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700258 uint16_t vtable_index = resolved_method->GetMethodIndex();
259 if (type == kSuper) {
260 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
261 } else {
262 vtable = this_object->GetClass()->GetVTable();
263 }
264 // TODO: eliminate bounds check?
265 return vtable->Get(vtable_index);
266 }
267 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700268 // Incompatible class change should have been handled in resolve method.
269 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
Ian Rogers2fc14272012-08-30 10:56:57 -0700270 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
271 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700272 return NULL; // Failure.
273 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
275 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700276 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
277 !referring_class->CanAccessMember(methods_class,
278 resolved_method->GetAccessFlags()))) {
279 // The referring class can't access the resolved method, this may occur as a result of a
280 // protected method being made public by implementing an interface that re-declares the
281 // method public. Resort to the dex file to determine the correct class for the access check
Ian Rogers4445a7e2012-10-05 17:19:13 -0700282 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogers57b86d42012-03-27 16:05:41 -0700283 methods_class = class_linker->ResolveType(dex_file,
284 dex_file.GetMethodId(method_idx).class_idx_,
285 referring_class);
286 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700287 ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
288 referrer, resolved_method, type);
Ian Rogers08f753d2012-08-24 14:35:25 -0700289 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700290 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
291 resolved_method->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700292 ThrowIllegalAccessErrorMethod(referring_class, resolved_method);
Ian Rogers08f753d2012-08-24 14:35:25 -0700293 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700294 }
295 }
296 if (is_direct) {
297 return resolved_method;
298 } else if (type == kInterface) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 mirror::AbstractMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700300 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
301 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700302 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
303 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700304 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700305 } else {
306 return interface_method;
307 }
308 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800309 mirror::ObjectArray<mirror::AbstractMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700310 uint16_t vtable_index = resolved_method->GetMethodIndex();
311 if (type == kSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 mirror::Class* super_class = referring_class->GetSuperClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700313 if (LIKELY(super_class != NULL)) {
314 vtable = referring_class->GetSuperClass()->GetVTable();
315 } else {
316 vtable = NULL;
317 }
318 } else {
319 vtable = this_object->GetClass()->GetVTable();
320 }
321 if (LIKELY(vtable != NULL &&
322 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
323 return vtable->GetWithoutChecks(vtable_index);
324 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700325 // Behavior to agree with that of the verifier.
326 MethodHelper mh(resolved_method);
327 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 mh.GetSignature());
Ian Rogers08f753d2012-08-24 14:35:25 -0700329 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700330 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800331 }
332 }
333 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800334}
335
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, const mirror::AbstractMethod* referrer,
337 Thread* self, bool can_run_clinit, bool verify_access) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700338 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers57b86d42012-03-27 16:05:41 -0700340 if (UNLIKELY(klass == NULL)) {
jeffhao441d9122012-03-21 17:29:10 -0700341 CHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -0700342 return NULL; // Failure - Indicate to caller to deliver exception
jeffhao441d9122012-03-21 17:29:10 -0700343 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700344 // Perform access check if necessary.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700346 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700347 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700348 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogers14b1b242011-10-11 18:54:34 -0700349 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700350 // If we're just implementing const-class, we shouldn't call <clinit>.
351 if (!can_run_clinit) {
352 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700353 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700354 // If we are the <clinit> of this class, just return our storage.
355 //
356 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
357 // running.
358 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
359 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700360 }
Ian Rogers0045a292012-03-31 21:08:41 -0700361 if (!class_linker->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700362 CHECK(self->IsExceptionPending());
363 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700364 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700365 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
366 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700367}
368
jeffhaod7521322012-11-21 15:38:24 -0800369void ThrowStackOverflowError(Thread* self) {
370 CHECK(!self->IsHandlingStackOverflow()) << "Recursive stack overflow.";
Ian Rogers62d6c772013-02-27 08:32:07 -0800371
372 if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
373 // Remove extra entry pushed onto second stack during method tracing.
374 Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false);
jeffhaod7521322012-11-21 15:38:24 -0800375 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800376
jeffhaod7521322012-11-21 15:38:24 -0800377 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
378 JNIEnvExt* env = self->GetJniEnv();
379 std::string msg("stack size ");
380 msg += PrettySize(self->GetStackSize());
381 // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
382 // would consume more stack.
383 int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
384 msg.c_str(), NULL);
385 if (rc != JNI_OK) {
386 // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
387 // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
388 // instead.
389 LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
390 CHECK(self->IsExceptionPending());
391 }
392 self->ResetDefaultStackEnd(); // Return to default stack size.
393}
394
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800395JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
396 jobject rcvr_jobj, jobject interface_method_jobj,
397 std::vector<jvalue>& args) {
398 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
399
400 // Build argument array possibly triggering GC.
401 soa.Self()->AssertThreadSuspensionIsAllowable();
402 jobjectArray args_jobj = NULL;
403 const JValue zero;
404 if (args.size() > 0) {
405 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
406 if (args_jobj == NULL) {
407 CHECK(soa.Self()->IsExceptionPending());
408 return zero;
409 }
410 for (size_t i = 0; i < args.size(); ++i) {
411 if (shorty[i + 1] == 'L') {
412 jobject val = args.at(i).l;
413 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
414 } else {
415 JValue jv;
416 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800418 if (val == NULL) {
419 CHECK(soa.Self()->IsExceptionPending());
420 return zero;
421 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800423 }
424 }
425 }
426
427 // Call InvocationHandler.invoke(Object proxy, Method method, Object[] args).
428 jobject inv_hand = soa.Env()->GetObjectField(rcvr_jobj,
429 WellKnownClasses::java_lang_reflect_Proxy_h);
430 jvalue invocation_args[3];
431 invocation_args[0].l = rcvr_jobj;
432 invocation_args[1].l = interface_method_jobj;
433 invocation_args[2].l = args_jobj;
434 jobject result =
435 soa.Env()->CallObjectMethodA(inv_hand,
436 WellKnownClasses::java_lang_reflect_InvocationHandler_invoke,
437 invocation_args);
438
439 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800440 if (LIKELY(!soa.Self()->IsExceptionPending())) {
441 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800442 // Do nothing.
443 return zero;
444 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800446 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
447 mirror::AbstractMethod* interface_method =
448 soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
449 mirror::Class* result_type = MethodHelper(interface_method).GetReturnType();
450 mirror::AbstractMethod* proxy_method;
451 if (interface_method->GetDeclaringClass()->IsInterface()) {
452 proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
453 } else {
454 // Proxy dispatch to a method defined in Object.
455 DCHECK(interface_method->GetDeclaringClass()->IsObjectClass());
456 proxy_method = interface_method;
457 }
458 ThrowLocation throw_location(rcvr, proxy_method, -1);
459 JValue result_unboxed;
460 if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800461 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800462 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800463 }
464 return result_unboxed;
465 }
466 } else {
467 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
468 // a UndeclaredThrowableException.
Ian Rogers62d6c772013-02-27 08:32:07 -0800469 mirror::Throwable* exception = soa.Self()->GetException(NULL);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800470 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
472 mirror::SynthesizedProxyClass* proxy_class =
473 down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass());
474 mirror::AbstractMethod* interface_method =
475 soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
476 mirror::AbstractMethod* proxy_method =
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800477 rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
478 int throws_index = -1;
479 size_t num_virt_methods = proxy_class->NumVirtualMethods();
480 for (size_t i = 0; i < num_virt_methods; i++) {
481 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
482 throws_index = i;
483 break;
484 }
485 }
486 CHECK_NE(throws_index, -1);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487 mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
488 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800489 bool declares_exception = false;
490 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800492 declares_exception = declared_exception->IsAssignableFrom(exception_class);
493 }
494 if (!declares_exception) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800495 ThrowLocation throw_location(rcvr, proxy_method, -1);
496 soa.Self()->ThrowNewWrappedException(throw_location,
497 "Ljava/lang/reflect/UndeclaredThrowableException;",
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800498 NULL);
499 }
500 }
501 return zero;
502 }
503}
504
Shih-wei Liao2d831012011-09-28 22:06:53 -0700505} // namespace art