blob: 37be5f72e66f11613eda3d6352cedab671c09c9f [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
TDYa1275bb86012012-04-11 05:57:28 -070019#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070020#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070021
jeffhao41005dd2012-05-09 17:58:52 -070022double art_l2d(int64_t l) {
Elliott Hughes74847412012-06-20 18:10:21 -070023 return static_cast<double>(l);
jeffhao41005dd2012-05-09 17:58:52 -070024}
25
26float art_l2f(int64_t l) {
Elliott Hughes74847412012-06-20 18:10:21 -070027 return static_cast<float>(l);
jeffhao41005dd2012-05-09 17:58:52 -070028}
Shih-wei Liao2d831012011-09-28 22:06:53 -070029
Ian Rogers776ac1f2012-04-13 23:36:36 -070030/*
31 * Float/double conversion requires clamping to min and max of integer form. If
32 * target doesn't support this normally, use these.
33 */
jeffhao41005dd2012-05-09 17:58:52 -070034int64_t art_d2l(double d) {
Elliott Hughes74847412012-06-20 18:10:21 -070035 static const double kMaxLong = static_cast<double>(static_cast<int64_t>(0x7fffffffffffffffULL));
36 static const double kMinLong = static_cast<double>(static_cast<int64_t>(0x8000000000000000ULL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070037 if (d >= kMaxLong) {
Elliott Hughes74847412012-06-20 18:10:21 -070038 return 0x7fffffffffffffffULL;
Ian Rogers776ac1f2012-04-13 23:36:36 -070039 } else if (d <= kMinLong) {
Elliott Hughes74847412012-06-20 18:10:21 -070040 return 0x8000000000000000ULL;
Ian Rogers776ac1f2012-04-13 23:36:36 -070041 } else if (d != d) { // NaN case
42 return 0;
43 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070044 return static_cast<int64_t>(d);
Ian Rogers776ac1f2012-04-13 23:36:36 -070045 }
46}
47
jeffhao41005dd2012-05-09 17:58:52 -070048int64_t art_f2l(float f) {
Elliott Hughes74847412012-06-20 18:10:21 -070049 static const float kMaxLong = static_cast<float>(static_cast<int64_t>(0x7fffffffffffffffULL));
50 static const float kMinLong = static_cast<float>(static_cast<int64_t>(0x8000000000000000ULL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070051 if (f >= kMaxLong) {
Elliott Hughes74847412012-06-20 18:10:21 -070052 return 0x7fffffffffffffffULL;
Ian Rogers776ac1f2012-04-13 23:36:36 -070053 } else if (f <= kMinLong) {
Elliott Hughes74847412012-06-20 18:10:21 -070054 return 0x8000000000000000ULL;
Ian Rogers776ac1f2012-04-13 23:36:36 -070055 } else if (f != f) { // NaN case
56 return 0;
57 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070058 return static_cast<int64_t>(f);
Ian Rogers776ac1f2012-04-13 23:36:36 -070059 }
60}
61
jeffhao41005dd2012-05-09 17:58:52 -070062int32_t art_d2i(double d) {
Elliott Hughes74847412012-06-20 18:10:21 -070063 static const double kMaxInt = static_cast<double>(0x7fffffffUL);
64 static const double kMinInt = static_cast<double>(0x80000000UL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070065 if (d >= kMaxInt) {
Elliott Hughes74847412012-06-20 18:10:21 -070066 return 0x7fffffffUL;
Ian Rogers776ac1f2012-04-13 23:36:36 -070067 } else if (d <= kMinInt) {
Elliott Hughes74847412012-06-20 18:10:21 -070068 return 0x80000000UL;
Ian Rogers776ac1f2012-04-13 23:36:36 -070069 } else if (d != d) { // NaN case
70 return 0;
71 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070072 return static_cast<int32_t>(d);
Ian Rogers776ac1f2012-04-13 23:36:36 -070073 }
74}
75
jeffhao41005dd2012-05-09 17:58:52 -070076int32_t art_f2i(float f) {
Elliott Hughes74847412012-06-20 18:10:21 -070077 static const float kMaxInt = static_cast<float>(0x7fffffffUL);
78 static const float kMinInt = static_cast<float>(0x80000000UL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070079 if (f >= kMaxInt) {
Elliott Hughes74847412012-06-20 18:10:21 -070080 return 0x7fffffffUL;
Ian Rogers776ac1f2012-04-13 23:36:36 -070081 } else if (f <= kMinInt) {
Elliott Hughes74847412012-06-20 18:10:21 -070082 return 0x80000000UL;
Ian Rogers776ac1f2012-04-13 23:36:36 -070083 } else if (f != f) { // NaN case
84 return 0;
85 } else {
Elliott Hughes74847412012-06-20 18:10:21 -070086 return static_cast<int32_t>(f);
Ian Rogers776ac1f2012-04-13 23:36:36 -070087 }
88}
89
jeffhao41005dd2012-05-09 17:58:52 -070090namespace art {
91
Ian Rogers57b86d42012-03-27 16:05:41 -070092void ThrowNewIllegalAccessErrorClass(Thread* self,
93 Class* referrer,
94 Class* accessed) {
95 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
96 "illegal class access: '%s' -> '%s'",
97 PrettyDescriptor(referrer).c_str(),
98 PrettyDescriptor(accessed).c_str());
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -080099}
100
Ian Rogers57b86d42012-03-27 16:05:41 -0700101void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self,
102 Class* referrer,
103 Class* accessed,
104 const Method* caller,
105 const Method* called,
106 InvokeType type) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700107 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
108 "illegal class access ('%s' -> '%s')"
109 "in attempt to invoke %s method '%s' from '%s'",
110 PrettyDescriptor(referrer).c_str(),
111 PrettyDescriptor(accessed).c_str(),
Elliott Hughes6fcce302012-06-19 16:54:19 -0700112 ToStr<InvokeType>(type).c_str(),
Ian Rogers57b86d42012-03-27 16:05:41 -0700113 PrettyMethod(called).c_str(),
114 PrettyMethod(caller).c_str());
buzbee44b412b2012-02-04 08:50:53 -0800115}
116
Ian Rogers57b86d42012-03-27 16:05:41 -0700117void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
118 const Method* referrer,
119 const Method* interface_method,
120 Object* this_object) {
121 self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
122 "class '%s' does not implement interface '%s' in call to '%s' from '%s'",
123 PrettyDescriptor(this_object->GetClass()).c_str(),
124 PrettyDescriptor(interface_method->GetDeclaringClass()).c_str(),
125 PrettyMethod(interface_method).c_str(), PrettyMethod(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700126}
127
Ian Rogers57b86d42012-03-27 16:05:41 -0700128void ThrowNewIllegalAccessErrorField(Thread* self,
129 Class* referrer,
130 Field* accessed) {
131 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
132 "Field '%s' is inaccessible to class '%s'",
133 PrettyField(accessed, false).c_str(),
134 PrettyDescriptor(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700135}
136
Ian Rogers57b86d42012-03-27 16:05:41 -0700137void ThrowNewIllegalAccessErrorFinalField(Thread* self,
138 const Method* referrer,
139 Field* accessed) {
140 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
141 "Final field '%s' cannot be written to by method '%s'",
142 PrettyField(accessed, false).c_str(),
143 PrettyMethod(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700144}
145
Ian Rogers57b86d42012-03-27 16:05:41 -0700146void ThrowNewIllegalAccessErrorMethod(Thread* self,
147 Class* referrer,
148 Method* accessed) {
149 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
150 "Method '%s' is inaccessible to class '%s'",
151 PrettyMethod(accessed).c_str(),
152 PrettyDescriptor(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700153}
154
Ian Rogers57b86d42012-03-27 16:05:41 -0700155void ThrowNullPointerExceptionForFieldAccess(Thread* self,
156 Field* field,
157 bool is_read) {
158 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
159 "Attempt to %s field '%s' on a null object reference",
160 is_read ? "read from" : "write to",
161 PrettyField(field, true).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700162}
163
Ian Rogers57b86d42012-03-27 16:05:41 -0700164void ThrowNullPointerExceptionForMethodAccess(Thread* self,
165 Method* caller,
166 uint32_t method_idx,
167 InvokeType type) {
168 const DexFile& dex_file =
169 Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
Ian Rogers57b86d42012-03-27 16:05:41 -0700170 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
171 "Attempt to invoke %s method '%s' on a null object reference",
Elliott Hughes6fcce302012-06-19 16:54:19 -0700172 ToStr<InvokeType>(type).c_str(),
Ian Rogers57b86d42012-03-27 16:05:41 -0700173 PrettyMethod(method_idx, dex_file, true).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700174}
175
TDYa1273f9137d2012-04-08 15:59:19 -0700176void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* throw_method, uint32_t dex_pc) {
177 const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem();
178 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
179 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
180 DecodedInstruction dec_insn(instr);
181 switch (instr->Opcode()) {
182 case Instruction::INVOKE_DIRECT:
183 case Instruction::INVOKE_DIRECT_RANGE:
184 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kDirect);
185 break;
186 case Instruction::INVOKE_VIRTUAL:
187 case Instruction::INVOKE_VIRTUAL_RANGE:
188 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kVirtual);
189 break;
190 case Instruction::IGET:
191 case Instruction::IGET_WIDE:
192 case Instruction::IGET_OBJECT:
193 case Instruction::IGET_BOOLEAN:
194 case Instruction::IGET_BYTE:
195 case Instruction::IGET_CHAR:
196 case Instruction::IGET_SHORT: {
197 Field* field =
198 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
199 ThrowNullPointerExceptionForFieldAccess(self, field, true /* read */);
200 break;
201 }
202 case Instruction::IPUT:
203 case Instruction::IPUT_WIDE:
204 case Instruction::IPUT_OBJECT:
205 case Instruction::IPUT_BOOLEAN:
206 case Instruction::IPUT_BYTE:
207 case Instruction::IPUT_CHAR:
208 case Instruction::IPUT_SHORT: {
209 Field* field =
210 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
211 ThrowNullPointerExceptionForFieldAccess(self, field, false /* write */);
212 break;
213 }
214 case Instruction::AGET:
215 case Instruction::AGET_WIDE:
216 case Instruction::AGET_OBJECT:
217 case Instruction::AGET_BOOLEAN:
218 case Instruction::AGET_BYTE:
219 case Instruction::AGET_CHAR:
220 case Instruction::AGET_SHORT:
221 self->ThrowNewException("Ljava/lang/NullPointerException;",
222 "Attempt to read from null array");
223 break;
224 case Instruction::APUT:
225 case Instruction::APUT_WIDE:
226 case Instruction::APUT_OBJECT:
227 case Instruction::APUT_BOOLEAN:
228 case Instruction::APUT_BYTE:
229 case Instruction::APUT_CHAR:
230 case Instruction::APUT_SHORT:
231 self->ThrowNewException("Ljava/lang/NullPointerException;",
232 "Attempt to write to null array");
233 break;
Elliott Hughes6fcce302012-06-19 16:54:19 -0700234 case Instruction::ARRAY_LENGTH:
235 self->ThrowNewException("Ljava/lang/NullPointerException;",
236 "Attempt to get length of null array");
237 break;
TDYa1273f9137d2012-04-08 15:59:19 -0700238 default: {
239 const DexFile& dex_file = Runtime::Current()->GetClassLinker()
240 ->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache());
241 std::string message("Null pointer exception during instruction '");
242 message += instr->DumpString(&dex_file);
243 message += "'";
244 self->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str());
245 break;
246 }
247 }
248}
249
Ian Rogers57b86d42012-03-27 16:05:41 -0700250std::string FieldNameFromIndex(const Method* method, uint32_t ref,
251 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700252 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700253
254 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
255 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
256
257 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700258 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700259 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700260 if (!access) {
261 return class_name + "." + field_name;
262 }
263
264 std::string result;
265 result += "tried to access field ";
266 result += class_name + "." + field_name;
267 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700269 return result;
270}
271
Ian Rogers57b86d42012-03-27 16:05:41 -0700272std::string MethodNameFromIndex(const Method* method, uint32_t ref,
273 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700274 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700275
276 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
277 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
278
279 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700280 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700281 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700282 if (!access) {
283 return class_name + "." + method_name;
284 }
285
286 std::string result;
287 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700288 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700289 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700290 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700292 return result;
293}
294
TDYa127b92bcab2012-04-08 00:09:51 -0700295static std::string ClassNameFromIndex(const Method* method, uint32_t ref,
296 verifier::VerifyErrorRefType ref_type, bool access) {
Logan Chien9e5f5c12012-04-10 13:51:45 +0800297 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
298 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
299
300 uint16_t type_idx = 0;
301 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
302 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
303 type_idx = id.class_idx_;
304 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
305 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
306 type_idx = id.class_idx_;
307 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
308 type_idx = ref;
309 } else {
310 CHECK(false) << static_cast<int>(ref_type);
311 }
312
313 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
314 if (!access) {
315 return class_name;
316 }
317
318 std::string result;
319 result += "tried to access class ";
320 result += class_name;
321 result += " from class ";
322 result += PrettyDescriptor(method->GetDeclaringClass());
323 return result;
324}
325
326void ThrowVerificationError(Thread* self, const Method* method,
327 int32_t kind, int32_t ref) {
328 verifier::VerifyErrorRefType ref_type =
329 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
330
331 const char* exception_class = "Ljava/lang/VerifyError;";
332 std::string msg;
333
334 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
335 case verifier::VERIFY_ERROR_NO_CLASS:
336 exception_class = "Ljava/lang/NoClassDefFoundError;";
337 msg = ClassNameFromIndex(method, ref, ref_type, false);
338 break;
339 case verifier::VERIFY_ERROR_NO_FIELD:
340 exception_class = "Ljava/lang/NoSuchFieldError;";
341 msg = FieldNameFromIndex(method, ref, ref_type, false);
342 break;
343 case verifier::VERIFY_ERROR_NO_METHOD:
344 exception_class = "Ljava/lang/NoSuchMethodError;";
345 msg = MethodNameFromIndex(method, ref, ref_type, false);
346 break;
347 case verifier::VERIFY_ERROR_ACCESS_CLASS:
348 exception_class = "Ljava/lang/IllegalAccessError;";
349 msg = ClassNameFromIndex(method, ref, ref_type, true);
350 break;
351 case verifier::VERIFY_ERROR_ACCESS_FIELD:
352 exception_class = "Ljava/lang/IllegalAccessError;";
353 msg = FieldNameFromIndex(method, ref, ref_type, true);
354 break;
355 case verifier::VERIFY_ERROR_ACCESS_METHOD:
356 exception_class = "Ljava/lang/IllegalAccessError;";
357 msg = MethodNameFromIndex(method, ref, ref_type, true);
358 break;
359 case verifier::VERIFY_ERROR_CLASS_CHANGE:
360 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
361 msg = ClassNameFromIndex(method, ref, ref_type, false);
362 break;
363 case verifier::VERIFY_ERROR_INSTANTIATION:
364 exception_class = "Ljava/lang/InstantiationError;";
365 msg = ClassNameFromIndex(method, ref, ref_type, false);
366 break;
367 case verifier::VERIFY_ERROR_BAD_CLASS_SOFT:
368 case verifier::VERIFY_ERROR_BAD_CLASS_HARD:
369 // Generic VerifyError; use default exception, no message.
370 break;
Logan Chien9e5f5c12012-04-10 13:51:45 +0800371 }
372
373 self->ThrowNewException(exception_class, msg.c_str());
374}
375
Ian Rogers57b86d42012-03-27 16:05:41 -0700376// Helper function to allocate array for FILLED_NEW_ARRAY.
377Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
378 Thread* self, bool access_check) {
379 if (UNLIKELY(component_count < 0)) {
380 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
381 return NULL; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700382 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700383 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
384 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
385 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
386 if (klass == NULL) { // Error
387 DCHECK(Thread::Current()->IsExceptionPending());
388 return NULL; // Failure
Ian Rogers19846512012-02-24 11:42:47 -0800389 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700390 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700391 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
392 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
393 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
394 "Bad filled array request for type %s",
395 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800396 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700397 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
398 "Found type %s; filled-new-array not implemented for anything but \'int\'",
399 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800400 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700401 return NULL; // Failure
Ian Rogersad25ac52011-10-04 19:13:33 -0700402 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700403 if (access_check) {
404 Class* referrer = method->GetDeclaringClass();
405 if (UNLIKELY(!referrer->CanAccess(klass))) {
406 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
407 return NULL; // Failure
408 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800409 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700410 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
411 return Array::Alloc(klass, component_count);
412 }
413}
414
415// Slow path field resolution and declaring class initialization
416Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
417 bool is_static, bool is_primitive, bool is_set, size_t expected_size) {
418 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
419 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
420 if (UNLIKELY(resolved_field == NULL)) {
421 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
422 return NULL; // failure
423 } else {
424 Class* fields_class = resolved_field->GetDeclaringClass();
425 Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700426 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
427 !referring_class->CanAccessMember(fields_class,
428 resolved_field->GetAccessFlags()))) {
429 // The referring class can't access the resolved field, this may occur as a result of a
430 // protected field being made public by a sub-class. Resort to the dex file to determine
431 // the correct class for the access check.
432 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
433 fields_class = class_linker->ResolveType(dex_file,
434 dex_file.GetFieldId(field_idx).class_idx_,
435 referring_class);
436 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
437 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
438 return NULL; // failure
439 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
440 resolved_field->GetAccessFlags()))) {
441 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
442 return NULL; // failure
443 }
444 }
445 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700446 ThrowNewIllegalAccessErrorFinalField(self, referrer, resolved_field);
447 return NULL; // failure
448 } else {
449 FieldHelper fh(resolved_field);
450 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
451 fh.FieldSize() != expected_size)) {
452 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
453 "Attempted read of %zd-bit %s on field '%s'",
454 expected_size * (32 / sizeof(int32_t)),
455 is_primitive ? "primitive" : "non-primitive",
456 PrettyField(resolved_field, true).c_str());
457 return NULL; // failure
458 } else if (!is_static) {
459 // instance fields must be being accessed on an initialized class
460 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800461 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700462 // If the class is already initializing, we must be inside <clinit>, or
463 // we'd still be waiting for the lock.
464 if (fields_class->IsInitializing()) {
465 return resolved_field;
Ian Rogers0045a292012-03-31 21:08:41 -0700466 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700467 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800468 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700469 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
470 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800471 }
472 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700473 }
474 }
475}
476
477// Slow path method resolution
478Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
479 Thread* self, bool access_check, InvokeType type) {
480 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
481 bool is_direct = type == kStatic || type == kDirect;
482 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
483 if (UNLIKELY(resolved_method == NULL)) {
484 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
485 return NULL; // failure
486 } else {
487 if (!access_check) {
488 if (is_direct) {
489 return resolved_method;
490 } else if (type == kInterface) {
491 Method* interface_method =
492 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
493 if (UNLIKELY(interface_method == NULL)) {
494 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
495 resolved_method,
496 this_object);
497 return NULL; // failure
498 } else {
499 return interface_method;
500 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800501 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700502 ObjectArray<Method>* vtable;
503 uint16_t vtable_index = resolved_method->GetMethodIndex();
504 if (type == kSuper) {
505 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
506 } else {
507 vtable = this_object->GetClass()->GetVTable();
508 }
509 // TODO: eliminate bounds check?
510 return vtable->Get(vtable_index);
511 }
512 } else {
513 Class* methods_class = resolved_method->GetDeclaringClass();
514 Class* referring_class = referrer->GetDeclaringClass();
515 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
516 !referring_class->CanAccessMember(methods_class,
517 resolved_method->GetAccessFlags()))) {
518 // The referring class can't access the resolved method, this may occur as a result of a
519 // protected method being made public by implementing an interface that re-declares the
520 // method public. Resort to the dex file to determine the correct class for the access check
521 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
522 methods_class = class_linker->ResolveType(dex_file,
523 dex_file.GetMethodId(method_idx).class_idx_,
524 referring_class);
525 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
526 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
527 referrer, resolved_method, type);
528 return NULL; // failure
529 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
530 resolved_method->GetAccessFlags()))) {
531 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
532 return NULL; // failure
533 }
534 }
535 if (is_direct) {
536 return resolved_method;
537 } else if (type == kInterface) {
538 Method* interface_method =
539 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
540 if (UNLIKELY(interface_method == NULL)) {
541 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
542 resolved_method,
543 this_object);
544 return NULL; // failure
545 } else {
546 return interface_method;
547 }
548 } else {
549 ObjectArray<Method>* vtable;
550 uint16_t vtable_index = resolved_method->GetMethodIndex();
551 if (type == kSuper) {
552 Class* super_class = referring_class->GetSuperClass();
553 if (LIKELY(super_class != NULL)) {
554 vtable = referring_class->GetSuperClass()->GetVTable();
555 } else {
556 vtable = NULL;
557 }
558 } else {
559 vtable = this_object->GetClass()->GetVTable();
560 }
561 if (LIKELY(vtable != NULL &&
562 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
563 return vtable->GetWithoutChecks(vtable_index);
564 } else {
565 // Behavior to agree with that of the verifier
566 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
567 "attempt to invoke %s method '%s' from '%s'"
568 " using incorrect form of method dispatch",
569 (type == kSuper ? "super class" : "virtual"),
570 PrettyMethod(resolved_method).c_str(),
571 PrettyMethod(referrer).c_str());
572 return NULL; // failure
573 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800574 }
575 }
576 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800577}
578
Ian Rogers57b86d42012-03-27 16:05:41 -0700579Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
580 bool can_run_clinit, bool verify_access) {
581 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
582 Class* klass = class_linker->ResolveType(type_idx, referrer);
583 if (UNLIKELY(klass == NULL)) {
jeffhao441d9122012-03-21 17:29:10 -0700584 CHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -0700585 return NULL; // Failure - Indicate to caller to deliver exception
jeffhao441d9122012-03-21 17:29:10 -0700586 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700587 // Perform access check if necessary.
588 Class* referring_class = referrer->GetDeclaringClass();
589 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
590 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
591 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogers14b1b242011-10-11 18:54:34 -0700592 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700593 // If we're just implementing const-class, we shouldn't call <clinit>.
594 if (!can_run_clinit) {
595 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700596 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700597 // If we are the <clinit> of this class, just return our storage.
598 //
599 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
600 // running.
601 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
602 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700603 }
Ian Rogers0045a292012-03-31 21:08:41 -0700604 if (!class_linker->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700605 CHECK(self->IsExceptionPending());
606 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700607 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700608 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
609 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700610}
611
Shih-wei Liao2d831012011-09-28 22:06:53 -0700612} // namespace art