blob: 84a19cf491a13bf010a9ec1f77d02b398859a541 [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"
20#include "gc/card_table-inl.h"
21#include "mirror/abstract_method-inl.h"
22#include "mirror/class-inl.h"
23#include "mirror/field-inl.h"
24#include "mirror/object-inl.h"
25#include "mirror/object_array-inl.h"
26#include "mirror/proxy.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080027#include "reflection.h"
28#include "scoped_thread_state_change.h"
TDYa1275bb86012012-04-11 05:57:28 -070029#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070030#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070031
jeffhao41005dd2012-05-09 17:58:52 -070032double art_l2d(int64_t l) {
Elliott Hughes74847412012-06-20 18:10:21 -070033 return static_cast<double>(l);
jeffhao41005dd2012-05-09 17:58:52 -070034}
35
36float art_l2f(int64_t l) {
Elliott Hughes74847412012-06-20 18:10:21 -070037 return static_cast<float>(l);
jeffhao41005dd2012-05-09 17:58:52 -070038}
Shih-wei Liao2d831012011-09-28 22:06:53 -070039
Ian Rogers776ac1f2012-04-13 23:36:36 -070040/*
41 * Float/double conversion requires clamping to min and max of integer form. If
42 * target doesn't support this normally, use these.
43 */
jeffhao41005dd2012-05-09 17:58:52 -070044int64_t art_d2l(double d) {
Elliott Hughes74847412012-06-20 18:10:21 -070045 static const double kMaxLong = static_cast<double>(static_cast<int64_t>(0x7fffffffffffffffULL));
46 static const double kMinLong = static_cast<double>(static_cast<int64_t>(0x8000000000000000ULL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070047 if (d >= kMaxLong) {
Logan Chien008fa512012-06-22 08:09:57 -070048 return static_cast<int64_t>(0x7fffffffffffffffULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070049 } else if (d <= kMinLong) {
Logan Chien008fa512012-06-22 08:09:57 -070050 return static_cast<int64_t>(0x8000000000000000ULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070051 } else if (d != d) { // NaN case
52 return 0;
53 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070054 return static_cast<int64_t>(d);
Ian Rogers776ac1f2012-04-13 23:36:36 -070055 }
56}
57
jeffhao41005dd2012-05-09 17:58:52 -070058int64_t art_f2l(float f) {
Elliott Hughes74847412012-06-20 18:10:21 -070059 static const float kMaxLong = static_cast<float>(static_cast<int64_t>(0x7fffffffffffffffULL));
60 static const float kMinLong = static_cast<float>(static_cast<int64_t>(0x8000000000000000ULL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070061 if (f >= kMaxLong) {
Logan Chien008fa512012-06-22 08:09:57 -070062 return static_cast<int64_t>(0x7fffffffffffffffULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070063 } else if (f <= kMinLong) {
Logan Chien008fa512012-06-22 08:09:57 -070064 return static_cast<int64_t>(0x8000000000000000ULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070065 } else if (f != f) { // NaN case
66 return 0;
67 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070068 return static_cast<int64_t>(f);
Ian Rogers776ac1f2012-04-13 23:36:36 -070069 }
70}
71
jeffhao41005dd2012-05-09 17:58:52 -070072int32_t art_d2i(double d) {
Logan Chien008fa512012-06-22 08:09:57 -070073 static const double kMaxInt = static_cast<double>(static_cast<int32_t>(0x7fffffffUL));
74 static const double kMinInt = static_cast<double>(static_cast<int32_t>(0x80000000UL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070075 if (d >= kMaxInt) {
Logan Chien008fa512012-06-22 08:09:57 -070076 return static_cast<int32_t>(0x7fffffffUL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070077 } else if (d <= kMinInt) {
Logan Chien008fa512012-06-22 08:09:57 -070078 return static_cast<int32_t>(0x80000000UL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070079 } else if (d != d) { // NaN case
80 return 0;
81 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070082 return static_cast<int32_t>(d);
Ian Rogers776ac1f2012-04-13 23:36:36 -070083 }
84}
85
jeffhao41005dd2012-05-09 17:58:52 -070086int32_t art_f2i(float f) {
Logan Chien008fa512012-06-22 08:09:57 -070087 static const float kMaxInt = static_cast<float>(static_cast<int32_t>(0x7fffffffUL));
88 static const float kMinInt = static_cast<float>(static_cast<int32_t>(0x80000000UL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070089 if (f >= kMaxInt) {
Logan Chien008fa512012-06-22 08:09:57 -070090 return static_cast<int32_t>(0x7fffffffUL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070091 } else if (f <= kMinInt) {
Logan Chien008fa512012-06-22 08:09:57 -070092 return static_cast<int32_t>(0x80000000UL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070093 } else if (f != f) { // NaN case
94 return 0;
95 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070096 return static_cast<int32_t>(f);
Ian Rogers776ac1f2012-04-13 23:36:36 -070097 }
98}
99
jeffhao41005dd2012-05-09 17:58:52 -0700100namespace art {
101
Ian Rogers57b86d42012-03-27 16:05:41 -0700102// Helper function to allocate array for FILLED_NEW_ARRAY.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method,
104 int32_t component_count, Thread* self,
105 bool access_check) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700106 if (UNLIKELY(component_count < 0)) {
107 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
108 return NULL; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700109 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700111 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
112 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
113 if (klass == NULL) { // Error
Ian Rogers50b35e22012-10-04 10:09:15 -0700114 DCHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -0700115 return NULL; // Failure
Ian Rogers19846512012-02-24 11:42:47 -0800116 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700117 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700118 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
119 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700120 self->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
121 "Bad filled array request for type %s",
122 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800123 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -0700124 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
125 "Found type %s; filled-new-array not implemented for anything but \'int\'",
126 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800127 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700128 return NULL; // Failure
Ian Rogersad25ac52011-10-04 19:13:33 -0700129 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700130 if (access_check) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700132 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700133 ThrowIllegalAccessErrorClass(referrer, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700134 return NULL; // Failure
135 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800136 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700137 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -0700139 }
140}
141
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142mirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer,
143 Thread* self, FindFieldType type, size_t expected_size) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700144 bool is_primitive;
145 bool is_set;
146 bool is_static;
147 switch (type) {
148 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
149 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
150 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
151 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
152 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
153 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
154 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
155 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
156 default: is_primitive = true; is_set = true; is_static = true; break;
157 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700158 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 mirror::Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogers57b86d42012-03-27 16:05:41 -0700160 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700161 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
162 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700163 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700164 if (resolved_field->IsStatic() != is_static) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700165 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700166 return NULL;
167 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
169 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700170 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
171 !referring_class->CanAccessMember(fields_class,
172 resolved_field->GetAccessFlags()))) {
173 // The referring class can't access the resolved field, this may occur as a result of a
174 // protected field being made public by a sub-class. Resort to the dex file to determine
175 // the correct class for the access check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700176 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogerse2645d32012-04-11 14:42:42 -0700177 fields_class = class_linker->ResolveType(dex_file,
178 dex_file.GetFieldId(field_idx).class_idx_,
179 referring_class);
180 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700181 ThrowIllegalAccessErrorClass(referring_class, fields_class);
Ian Rogerse2645d32012-04-11 14:42:42 -0700182 return NULL; // failure
183 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
184 resolved_field->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700185 ThrowIllegalAccessErrorField(referring_class, resolved_field);
Ian Rogerse2645d32012-04-11 14:42:42 -0700186 return NULL; // failure
187 }
188 }
189 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700190 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Ian Rogers57b86d42012-03-27 16:05:41 -0700191 return NULL; // failure
192 } else {
193 FieldHelper fh(resolved_field);
194 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
195 fh.FieldSize() != expected_size)) {
196 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
197 "Attempted read of %zd-bit %s on field '%s'",
198 expected_size * (32 / sizeof(int32_t)),
199 is_primitive ? "primitive" : "non-primitive",
200 PrettyField(resolved_field, true).c_str());
201 return NULL; // failure
202 } else if (!is_static) {
203 // instance fields must be being accessed on an initialized class
204 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800205 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700206 // If the class is already initializing, we must be inside <clinit>, or
207 // we'd still be waiting for the lock.
208 if (fields_class->IsInitializing()) {
209 return resolved_field;
Ian Rogers0045a292012-03-31 21:08:41 -0700210 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700211 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800212 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700213 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
214 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800215 }
216 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700217 }
218 }
219}
220
221// Slow path method resolution
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222mirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
223 mirror::AbstractMethod* referrer,
224 Thread* self, bool access_check, InvokeType type) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700225 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
226 bool is_direct = type == kStatic || type == kDirect;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 mirror::AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700228 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700229 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
230 return NULL; // Failure.
jeffhao262e2512012-12-11 09:46:43 -0800231 } else if (UNLIKELY(this_object == NULL && type != kStatic)) {
232 // Maintain interpreter-like semantics where NullPointerException is thrown
233 // after potential NoSuchMethodError from class linker.
234 ThrowNullPointerExceptionForMethodAccess(referrer, method_idx, type);
235 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700236 } else {
237 if (!access_check) {
238 if (is_direct) {
239 return resolved_method;
240 } else if (type == kInterface) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 mirror::AbstractMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700242 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
243 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700244 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
245 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700246 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700247 } else {
248 return interface_method;
249 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800250 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 mirror::ObjectArray<mirror::AbstractMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700252 uint16_t vtable_index = resolved_method->GetMethodIndex();
253 if (type == kSuper) {
254 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
255 } else {
256 vtable = this_object->GetClass()->GetVTable();
257 }
258 // TODO: eliminate bounds check?
259 return vtable->Get(vtable_index);
260 }
261 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700262 // Incompatible class change should have been handled in resolve method.
263 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
Ian Rogers2fc14272012-08-30 10:56:57 -0700264 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
265 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700266 return NULL; // Failure.
267 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
269 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700270 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
271 !referring_class->CanAccessMember(methods_class,
272 resolved_method->GetAccessFlags()))) {
273 // The referring class can't access the resolved method, this may occur as a result of a
274 // protected method being made public by implementing an interface that re-declares the
275 // method public. Resort to the dex file to determine the correct class for the access check
Ian Rogers4445a7e2012-10-05 17:19:13 -0700276 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogers57b86d42012-03-27 16:05:41 -0700277 methods_class = class_linker->ResolveType(dex_file,
278 dex_file.GetMethodId(method_idx).class_idx_,
279 referring_class);
280 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700281 ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
282 referrer, resolved_method, type);
Ian Rogers08f753d2012-08-24 14:35:25 -0700283 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700284 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
285 resolved_method->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700286 ThrowIllegalAccessErrorMethod(referring_class, resolved_method);
Ian Rogers08f753d2012-08-24 14:35:25 -0700287 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700288 }
289 }
290 if (is_direct) {
291 return resolved_method;
292 } else if (type == kInterface) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293 mirror::AbstractMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700294 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
295 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700296 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
297 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700298 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700299 } else {
300 return interface_method;
301 }
302 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 mirror::ObjectArray<mirror::AbstractMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700304 uint16_t vtable_index = resolved_method->GetMethodIndex();
305 if (type == kSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 mirror::Class* super_class = referring_class->GetSuperClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700307 if (LIKELY(super_class != NULL)) {
308 vtable = referring_class->GetSuperClass()->GetVTable();
309 } else {
310 vtable = NULL;
311 }
312 } else {
313 vtable = this_object->GetClass()->GetVTable();
314 }
315 if (LIKELY(vtable != NULL &&
316 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
317 return vtable->GetWithoutChecks(vtable_index);
318 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700319 // Behavior to agree with that of the verifier.
320 MethodHelper mh(resolved_method);
321 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
Ian Rogers2fc14272012-08-30 10:56:57 -0700322 mh.GetSignature(), referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700323 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700324 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800325 }
326 }
327 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800328}
329
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, const mirror::AbstractMethod* referrer,
331 Thread* self, bool can_run_clinit, bool verify_access) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700332 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers57b86d42012-03-27 16:05:41 -0700334 if (UNLIKELY(klass == NULL)) {
jeffhao441d9122012-03-21 17:29:10 -0700335 CHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -0700336 return NULL; // Failure - Indicate to caller to deliver exception
jeffhao441d9122012-03-21 17:29:10 -0700337 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700338 // Perform access check if necessary.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700340 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700341 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700342 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogers14b1b242011-10-11 18:54:34 -0700343 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700344 // If we're just implementing const-class, we shouldn't call <clinit>.
345 if (!can_run_clinit) {
346 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700347 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700348 // If we are the <clinit> of this class, just return our storage.
349 //
350 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
351 // running.
352 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
353 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700354 }
Ian Rogers0045a292012-03-31 21:08:41 -0700355 if (!class_linker->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700356 CHECK(self->IsExceptionPending());
357 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700358 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700359 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
360 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700361}
362
jeffhaod7521322012-11-21 15:38:24 -0800363void ThrowStackOverflowError(Thread* self) {
364 CHECK(!self->IsHandlingStackOverflow()) << "Recursive stack overflow.";
365 // Remove extra entry pushed onto second stack during method tracing.
366 if (Runtime::Current()->IsMethodTracingActive()) {
367 InstrumentationMethodUnwindFromCode(self);
368 }
369 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
370 JNIEnvExt* env = self->GetJniEnv();
371 std::string msg("stack size ");
372 msg += PrettySize(self->GetStackSize());
373 // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
374 // would consume more stack.
375 int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
376 msg.c_str(), NULL);
377 if (rc != JNI_OK) {
378 // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
379 // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
380 // instead.
381 LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
382 CHECK(self->IsExceptionPending());
383 }
384 self->ResetDefaultStackEnd(); // Return to default stack size.
385}
386
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800387JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
388 jobject rcvr_jobj, jobject interface_method_jobj,
389 std::vector<jvalue>& args) {
390 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
391
392 // Build argument array possibly triggering GC.
393 soa.Self()->AssertThreadSuspensionIsAllowable();
394 jobjectArray args_jobj = NULL;
395 const JValue zero;
396 if (args.size() > 0) {
397 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
398 if (args_jobj == NULL) {
399 CHECK(soa.Self()->IsExceptionPending());
400 return zero;
401 }
402 for (size_t i = 0; i < args.size(); ++i) {
403 if (shorty[i + 1] == 'L') {
404 jobject val = args.at(i).l;
405 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
406 } else {
407 JValue jv;
408 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800410 if (val == NULL) {
411 CHECK(soa.Self()->IsExceptionPending());
412 return zero;
413 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800415 }
416 }
417 }
418
419 // Call InvocationHandler.invoke(Object proxy, Method method, Object[] args).
420 jobject inv_hand = soa.Env()->GetObjectField(rcvr_jobj,
421 WellKnownClasses::java_lang_reflect_Proxy_h);
422 jvalue invocation_args[3];
423 invocation_args[0].l = rcvr_jobj;
424 invocation_args[1].l = interface_method_jobj;
425 invocation_args[2].l = args_jobj;
426 jobject result =
427 soa.Env()->CallObjectMethodA(inv_hand,
428 WellKnownClasses::java_lang_reflect_InvocationHandler_invoke,
429 invocation_args);
430
431 // Unbox result and handle error conditions.
432 if (!soa.Self()->IsExceptionPending()) {
433 if (shorty[0] == 'V' || result == NULL) {
434 // Do nothing.
435 return zero;
436 } else {
437 JValue result_unboxed;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438 MethodHelper mh(soa.Decode<mirror::AbstractMethod*>(interface_method_jobj));
439 mirror::Class* result_type = mh.GetReturnType();
440 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800441 bool unboxed_okay = UnboxPrimitiveForResult(result_ref, result_type, result_unboxed);
442 if (!unboxed_okay) {
443 soa.Self()->ThrowNewWrappedException("Ljava/lang/ClassCastException;",
444 StringPrintf("Couldn't convert result of type %s to %s",
445 PrettyTypeOf(result_ref).c_str(),
446 PrettyDescriptor(result_type).c_str()
447 ).c_str());
448 }
449 return result_unboxed;
450 }
451 } else {
452 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
453 // a UndeclaredThrowableException.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454 mirror::Throwable* exception = soa.Self()->GetException();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800455 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
457 mirror::SynthesizedProxyClass* proxy_class =
458 down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass());
459 mirror::AbstractMethod* interface_method =
460 soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
461 mirror::AbstractMethod* proxy_method =
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800462 rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
463 int throws_index = -1;
464 size_t num_virt_methods = proxy_class->NumVirtualMethods();
465 for (size_t i = 0; i < num_virt_methods; i++) {
466 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
467 throws_index = i;
468 break;
469 }
470 }
471 CHECK_NE(throws_index, -1);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472 mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
473 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800474 bool declares_exception = false;
475 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800477 declares_exception = declared_exception->IsAssignableFrom(exception_class);
478 }
479 if (!declares_exception) {
480 soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;",
481 NULL);
482 }
483 }
484 return zero;
485 }
486}
487
Shih-wei Liao2d831012011-09-28 22:06:53 -0700488} // namespace art