blob: 91710e75288f1f9e2fd3314cf0b3bce9a3cb0044 [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) {
Logan Chien008fa512012-06-22 08:09:57 -070038 return static_cast<int64_t>(0x7fffffffffffffffULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070039 } else if (d <= kMinLong) {
Logan Chien008fa512012-06-22 08:09:57 -070040 return static_cast<int64_t>(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) {
Logan Chien008fa512012-06-22 08:09:57 -070052 return static_cast<int64_t>(0x7fffffffffffffffULL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070053 } else if (f <= kMinLong) {
Logan Chien008fa512012-06-22 08:09:57 -070054 return static_cast<int64_t>(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) {
Logan Chien008fa512012-06-22 08:09:57 -070063 static const double kMaxInt = static_cast<double>(static_cast<int32_t>(0x7fffffffUL));
64 static const double kMinInt = static_cast<double>(static_cast<int32_t>(0x80000000UL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070065 if (d >= kMaxInt) {
Logan Chien008fa512012-06-22 08:09:57 -070066 return static_cast<int32_t>(0x7fffffffUL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070067 } else if (d <= kMinInt) {
Logan Chien008fa512012-06-22 08:09:57 -070068 return static_cast<int32_t>(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) {
Logan Chien008fa512012-06-22 08:09:57 -070077 static const float kMaxInt = static_cast<float>(static_cast<int32_t>(0x7fffffffUL));
78 static const float kMinInt = static_cast<float>(static_cast<int32_t>(0x80000000UL));
Ian Rogers776ac1f2012-04-13 23:36:36 -070079 if (f >= kMaxInt) {
Logan Chien008fa512012-06-22 08:09:57 -070080 return static_cast<int32_t>(0x7fffffffUL);
Ian Rogers776ac1f2012-04-13 23:36:36 -070081 } else if (f <= kMinInt) {
Logan Chien008fa512012-06-22 08:09:57 -070082 return static_cast<int32_t>(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;",
Ian Rogers08f753d2012-08-24 14:35:25 -070096 "Illegal class access: '%s' -> '%s'",
Ian Rogers57b86d42012-03-27 16:05:41 -070097 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;",
Ian Rogers08f753d2012-08-24 14:35:25 -0700108 "Illegal class access ('%s' -> '%s')"
Ian Rogers57b86d42012-03-27 16:05:41 -0700109 "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 Rogers08f753d2012-08-24 14:35:25 -0700117static void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
118 const Method* interface_method,
119 Object* this_object)
120 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
121 std::string interface_method_name(PrettyMethod(interface_method));
122 if (this_object != NULL) {
123 std::string this_class_descriptor(PrettyDescriptor(this_object->GetClass()));
124 std::string interface_class_descriptor(PrettyDescriptor(interface_method->GetDeclaringClass()));
125 self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
126 "Class '%s' does not implement interface '%s' in call to '%s'",
127 this_class_descriptor.c_str(),
128 interface_class_descriptor.c_str(),
129 interface_method_name.c_str());
130 } else {
131 self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
132 "Expected '%s' to be an interface method",
133 interface_method_name.c_str());
134 }
135}
136
137static void ThrowNewIncompatibleClassChangeErrorField(Thread* self, const Field* resolved_field,
138 bool is_static)
139 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700140 self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
Ian Rogers08f753d2012-08-24 14:35:25 -0700141 "Expected '%s' to be a %s field",
142 PrettyField(resolved_field).c_str(),
143 is_static ? "static" : "instance");
144}
145
146void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
147 Method* method) {
148 std::ostringstream msg;
149 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type"
150 << expected_type << " but instead was found to be of type " << found_type;
151 ClassHelper kh(method->GetDeclaringClass());
152 std::string location(kh.GetLocation());
153 if (!location.empty()) {
154 msg << " (accessed from " << location << ")";
155 }
156 Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
157 msg.str().c_str());
158}
159
160void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name,
161 const StringPiece& signature) {
162 ClassHelper kh(c);
163 std::ostringstream msg;
164 msg << "No " << type << " method " << name << signature
165 << " in class " << kh.GetDescriptor() << " or its superclasses";
166 std::string location(kh.GetLocation());
167 if (!location.empty()) {
168 msg << " (accessed from " << location << ")";
169 }
170 Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700171}
172
Ian Rogers57b86d42012-03-27 16:05:41 -0700173void ThrowNewIllegalAccessErrorField(Thread* self,
174 Class* referrer,
175 Field* accessed) {
176 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
177 "Field '%s' is inaccessible to class '%s'",
178 PrettyField(accessed, false).c_str(),
179 PrettyDescriptor(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700180}
181
Ian Rogers57b86d42012-03-27 16:05:41 -0700182void ThrowNewIllegalAccessErrorFinalField(Thread* self,
183 const Method* referrer,
184 Field* accessed) {
185 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
186 "Final field '%s' cannot be written to by method '%s'",
187 PrettyField(accessed, false).c_str(),
188 PrettyMethod(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700189}
190
Ian Rogers57b86d42012-03-27 16:05:41 -0700191void ThrowNewIllegalAccessErrorMethod(Thread* self,
192 Class* referrer,
193 Method* accessed) {
194 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
195 "Method '%s' is inaccessible to class '%s'",
196 PrettyMethod(accessed).c_str(),
197 PrettyDescriptor(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700198}
199
Ian Rogers57b86d42012-03-27 16:05:41 -0700200void ThrowNullPointerExceptionForFieldAccess(Thread* self,
201 Field* field,
202 bool is_read) {
203 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
204 "Attempt to %s field '%s' on a null object reference",
205 is_read ? "read from" : "write to",
206 PrettyField(field, true).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700207}
208
Ian Rogers57b86d42012-03-27 16:05:41 -0700209void ThrowNullPointerExceptionForMethodAccess(Thread* self,
210 Method* caller,
211 uint32_t method_idx,
212 InvokeType type) {
213 const DexFile& dex_file =
214 Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
Ian Rogers57b86d42012-03-27 16:05:41 -0700215 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
216 "Attempt to invoke %s method '%s' on a null object reference",
Elliott Hughes6fcce302012-06-19 16:54:19 -0700217 ToStr<InvokeType>(type).c_str(),
Ian Rogers57b86d42012-03-27 16:05:41 -0700218 PrettyMethod(method_idx, dex_file, true).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700219}
220
TDYa1273f9137d2012-04-08 15:59:19 -0700221void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* throw_method, uint32_t dex_pc) {
222 const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem();
223 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
224 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
225 DecodedInstruction dec_insn(instr);
226 switch (instr->Opcode()) {
227 case Instruction::INVOKE_DIRECT:
228 case Instruction::INVOKE_DIRECT_RANGE:
229 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kDirect);
230 break;
231 case Instruction::INVOKE_VIRTUAL:
232 case Instruction::INVOKE_VIRTUAL_RANGE:
233 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kVirtual);
234 break;
235 case Instruction::IGET:
236 case Instruction::IGET_WIDE:
237 case Instruction::IGET_OBJECT:
238 case Instruction::IGET_BOOLEAN:
239 case Instruction::IGET_BYTE:
240 case Instruction::IGET_CHAR:
241 case Instruction::IGET_SHORT: {
242 Field* field =
243 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
244 ThrowNullPointerExceptionForFieldAccess(self, field, true /* read */);
245 break;
246 }
247 case Instruction::IPUT:
248 case Instruction::IPUT_WIDE:
249 case Instruction::IPUT_OBJECT:
250 case Instruction::IPUT_BOOLEAN:
251 case Instruction::IPUT_BYTE:
252 case Instruction::IPUT_CHAR:
253 case Instruction::IPUT_SHORT: {
254 Field* field =
255 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
256 ThrowNullPointerExceptionForFieldAccess(self, field, false /* write */);
257 break;
258 }
259 case Instruction::AGET:
260 case Instruction::AGET_WIDE:
261 case Instruction::AGET_OBJECT:
262 case Instruction::AGET_BOOLEAN:
263 case Instruction::AGET_BYTE:
264 case Instruction::AGET_CHAR:
265 case Instruction::AGET_SHORT:
266 self->ThrowNewException("Ljava/lang/NullPointerException;",
267 "Attempt to read from null array");
268 break;
269 case Instruction::APUT:
270 case Instruction::APUT_WIDE:
271 case Instruction::APUT_OBJECT:
272 case Instruction::APUT_BOOLEAN:
273 case Instruction::APUT_BYTE:
274 case Instruction::APUT_CHAR:
275 case Instruction::APUT_SHORT:
276 self->ThrowNewException("Ljava/lang/NullPointerException;",
277 "Attempt to write to null array");
278 break;
Elliott Hughes6fcce302012-06-19 16:54:19 -0700279 case Instruction::ARRAY_LENGTH:
280 self->ThrowNewException("Ljava/lang/NullPointerException;",
281 "Attempt to get length of null array");
282 break;
TDYa1273f9137d2012-04-08 15:59:19 -0700283 default: {
284 const DexFile& dex_file = Runtime::Current()->GetClassLinker()
285 ->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache());
286 std::string message("Null pointer exception during instruction '");
287 message += instr->DumpString(&dex_file);
288 message += "'";
289 self->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str());
290 break;
291 }
292 }
293}
294
Ian Rogers57b86d42012-03-27 16:05:41 -0700295std::string FieldNameFromIndex(const Method* method, uint32_t ref,
296 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700297 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700298
299 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
300 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
301
302 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700303 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700304 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700305 if (!access) {
306 return class_name + "." + field_name;
307 }
308
309 std::string result;
310 result += "tried to access field ";
311 result += class_name + "." + field_name;
312 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700314 return result;
315}
316
Ian Rogers57b86d42012-03-27 16:05:41 -0700317std::string MethodNameFromIndex(const Method* method, uint32_t ref,
318 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700320
321 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
322 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
323
324 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700325 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700326 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700327 if (!access) {
328 return class_name + "." + method_name;
329 }
330
331 std::string result;
332 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700333 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700334 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700335 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800336 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700337 return result;
338}
339
TDYa127b92bcab2012-04-08 00:09:51 -0700340static std::string ClassNameFromIndex(const Method* method, uint32_t ref,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 verifier::VerifyErrorRefType ref_type, bool access)
342 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Logan Chien9e5f5c12012-04-10 13:51:45 +0800343 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
344 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
345
346 uint16_t type_idx = 0;
347 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
348 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
349 type_idx = id.class_idx_;
350 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
351 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
352 type_idx = id.class_idx_;
353 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
354 type_idx = ref;
355 } else {
356 CHECK(false) << static_cast<int>(ref_type);
357 }
358
359 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
360 if (!access) {
361 return class_name;
362 }
363
364 std::string result;
365 result += "tried to access class ";
366 result += class_name;
367 result += " from class ";
368 result += PrettyDescriptor(method->GetDeclaringClass());
369 return result;
370}
371
372void ThrowVerificationError(Thread* self, const Method* method,
373 int32_t kind, int32_t ref) {
374 verifier::VerifyErrorRefType ref_type =
375 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
376
377 const char* exception_class = "Ljava/lang/VerifyError;";
378 std::string msg;
379
380 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
381 case verifier::VERIFY_ERROR_NO_CLASS:
382 exception_class = "Ljava/lang/NoClassDefFoundError;";
383 msg = ClassNameFromIndex(method, ref, ref_type, false);
384 break;
385 case verifier::VERIFY_ERROR_NO_FIELD:
386 exception_class = "Ljava/lang/NoSuchFieldError;";
387 msg = FieldNameFromIndex(method, ref, ref_type, false);
388 break;
389 case verifier::VERIFY_ERROR_NO_METHOD:
390 exception_class = "Ljava/lang/NoSuchMethodError;";
391 msg = MethodNameFromIndex(method, ref, ref_type, false);
392 break;
393 case verifier::VERIFY_ERROR_ACCESS_CLASS:
394 exception_class = "Ljava/lang/IllegalAccessError;";
395 msg = ClassNameFromIndex(method, ref, ref_type, true);
396 break;
397 case verifier::VERIFY_ERROR_ACCESS_FIELD:
398 exception_class = "Ljava/lang/IllegalAccessError;";
399 msg = FieldNameFromIndex(method, ref, ref_type, true);
400 break;
401 case verifier::VERIFY_ERROR_ACCESS_METHOD:
402 exception_class = "Ljava/lang/IllegalAccessError;";
403 msg = MethodNameFromIndex(method, ref, ref_type, true);
404 break;
405 case verifier::VERIFY_ERROR_CLASS_CHANGE:
406 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
407 msg = ClassNameFromIndex(method, ref, ref_type, false);
408 break;
409 case verifier::VERIFY_ERROR_INSTANTIATION:
410 exception_class = "Ljava/lang/InstantiationError;";
411 msg = ClassNameFromIndex(method, ref, ref_type, false);
412 break;
413 case verifier::VERIFY_ERROR_BAD_CLASS_SOFT:
414 case verifier::VERIFY_ERROR_BAD_CLASS_HARD:
415 // Generic VerifyError; use default exception, no message.
416 break;
Logan Chien9e5f5c12012-04-10 13:51:45 +0800417 }
418
419 self->ThrowNewException(exception_class, msg.c_str());
420}
421
Ian Rogers57b86d42012-03-27 16:05:41 -0700422// Helper function to allocate array for FILLED_NEW_ARRAY.
423Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
424 Thread* self, bool access_check) {
425 if (UNLIKELY(component_count < 0)) {
426 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
427 return NULL; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700428 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700429 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
430 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
431 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
432 if (klass == NULL) { // Error
433 DCHECK(Thread::Current()->IsExceptionPending());
434 return NULL; // Failure
Ian Rogers19846512012-02-24 11:42:47 -0800435 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700436 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700437 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
438 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
439 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
440 "Bad filled array request for type %s",
441 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800442 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700443 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
444 "Found type %s; filled-new-array not implemented for anything but \'int\'",
445 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800446 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700447 return NULL; // Failure
Ian Rogersad25ac52011-10-04 19:13:33 -0700448 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700449 if (access_check) {
450 Class* referrer = method->GetDeclaringClass();
451 if (UNLIKELY(!referrer->CanAccess(klass))) {
452 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
453 return NULL; // Failure
454 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800455 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700456 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
457 return Array::Alloc(klass, component_count);
458 }
459}
460
Ian Rogers57b86d42012-03-27 16:05:41 -0700461Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700462 FindFieldType type, size_t expected_size) {
463 bool is_primitive;
464 bool is_set;
465 bool is_static;
466 switch (type) {
467 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
468 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
469 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
470 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
471 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
472 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
473 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
474 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
475 default: is_primitive = true; is_set = true; is_static = true; break;
476 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700477 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
478 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
479 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700480 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
481 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700482 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700483 if (resolved_field->IsStatic() != is_static) {
484 ThrowNewIncompatibleClassChangeErrorField(self, resolved_field, is_static);
485 return NULL;
486 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700487 Class* fields_class = resolved_field->GetDeclaringClass();
488 Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700489 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
490 !referring_class->CanAccessMember(fields_class,
491 resolved_field->GetAccessFlags()))) {
492 // The referring class can't access the resolved field, this may occur as a result of a
493 // protected field being made public by a sub-class. Resort to the dex file to determine
494 // the correct class for the access check.
495 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
496 fields_class = class_linker->ResolveType(dex_file,
497 dex_file.GetFieldId(field_idx).class_idx_,
498 referring_class);
499 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
500 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
501 return NULL; // failure
502 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
503 resolved_field->GetAccessFlags()))) {
504 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
505 return NULL; // failure
506 }
507 }
508 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700509 ThrowNewIllegalAccessErrorFinalField(self, referrer, resolved_field);
510 return NULL; // failure
511 } else {
512 FieldHelper fh(resolved_field);
513 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
514 fh.FieldSize() != expected_size)) {
515 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
516 "Attempted read of %zd-bit %s on field '%s'",
517 expected_size * (32 / sizeof(int32_t)),
518 is_primitive ? "primitive" : "non-primitive",
519 PrettyField(resolved_field, true).c_str());
520 return NULL; // failure
521 } else if (!is_static) {
522 // instance fields must be being accessed on an initialized class
523 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800524 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700525 // If the class is already initializing, we must be inside <clinit>, or
526 // we'd still be waiting for the lock.
527 if (fields_class->IsInitializing()) {
528 return resolved_field;
Ian Rogers0045a292012-03-31 21:08:41 -0700529 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700530 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800531 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700532 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
533 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800534 }
535 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700536 }
537 }
538}
539
540// Slow path method resolution
541Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
542 Thread* self, bool access_check, InvokeType type) {
543 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
544 bool is_direct = type == kStatic || type == kDirect;
Ian Rogers08f753d2012-08-24 14:35:25 -0700545 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700546 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700547 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
548 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700549 } else {
550 if (!access_check) {
551 if (is_direct) {
552 return resolved_method;
553 } else if (type == kInterface) {
554 Method* interface_method =
555 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
556 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700557 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self,
Ian Rogers57b86d42012-03-27 16:05:41 -0700558 resolved_method,
559 this_object);
Ian Rogers08f753d2012-08-24 14:35:25 -0700560 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700561 } else {
562 return interface_method;
563 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800564 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700565 ObjectArray<Method>* vtable;
566 uint16_t vtable_index = resolved_method->GetMethodIndex();
567 if (type == kSuper) {
568 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
569 } else {
570 vtable = this_object->GetClass()->GetVTable();
571 }
572 // TODO: eliminate bounds check?
573 return vtable->Get(vtable_index);
574 }
575 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700576 // Incompatible class change should have been handled in resolve method.
577 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
578 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method);
579 return NULL; // Failure.
580 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700581 Class* methods_class = resolved_method->GetDeclaringClass();
582 Class* referring_class = referrer->GetDeclaringClass();
583 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
584 !referring_class->CanAccessMember(methods_class,
585 resolved_method->GetAccessFlags()))) {
586 // The referring class can't access the resolved method, this may occur as a result of a
587 // protected method being made public by implementing an interface that re-declares the
588 // method public. Resort to the dex file to determine the correct class for the access check
589 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
590 methods_class = class_linker->ResolveType(dex_file,
591 dex_file.GetMethodId(method_idx).class_idx_,
592 referring_class);
593 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
594 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
595 referrer, resolved_method, type);
Ian Rogers08f753d2012-08-24 14:35:25 -0700596 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700597 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
598 resolved_method->GetAccessFlags()))) {
599 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
Ian Rogers08f753d2012-08-24 14:35:25 -0700600 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700601 }
602 }
603 if (is_direct) {
604 return resolved_method;
605 } else if (type == kInterface) {
606 Method* interface_method =
607 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
608 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700609 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self,
Ian Rogers57b86d42012-03-27 16:05:41 -0700610 resolved_method,
611 this_object);
Ian Rogers08f753d2012-08-24 14:35:25 -0700612 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700613 } else {
614 return interface_method;
615 }
616 } else {
617 ObjectArray<Method>* vtable;
618 uint16_t vtable_index = resolved_method->GetMethodIndex();
619 if (type == kSuper) {
620 Class* super_class = referring_class->GetSuperClass();
621 if (LIKELY(super_class != NULL)) {
622 vtable = referring_class->GetSuperClass()->GetVTable();
623 } else {
624 vtable = NULL;
625 }
626 } else {
627 vtable = this_object->GetClass()->GetVTable();
628 }
629 if (LIKELY(vtable != NULL &&
630 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
631 return vtable->GetWithoutChecks(vtable_index);
632 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700633 // Behavior to agree with that of the verifier.
634 MethodHelper mh(resolved_method);
635 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
636 mh.GetSignature());
637 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700638 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800639 }
640 }
641 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800642}
643
Ian Rogers57b86d42012-03-27 16:05:41 -0700644Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
645 bool can_run_clinit, bool verify_access) {
646 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
647 Class* klass = class_linker->ResolveType(type_idx, referrer);
648 if (UNLIKELY(klass == NULL)) {
jeffhao441d9122012-03-21 17:29:10 -0700649 CHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -0700650 return NULL; // Failure - Indicate to caller to deliver exception
jeffhao441d9122012-03-21 17:29:10 -0700651 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700652 // Perform access check if necessary.
653 Class* referring_class = referrer->GetDeclaringClass();
654 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
655 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
656 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogers14b1b242011-10-11 18:54:34 -0700657 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700658 // If we're just implementing const-class, we shouldn't call <clinit>.
659 if (!can_run_clinit) {
660 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700661 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700662 // If we are the <clinit> of this class, just return our storage.
663 //
664 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
665 // running.
666 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
667 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700668 }
Ian Rogers0045a292012-03-31 21:08:41 -0700669 if (!class_linker->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700670 CHECK(self->IsExceptionPending());
671 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700672 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700673 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
674 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700675}
676
Shih-wei Liao2d831012011-09-28 22:06:53 -0700677} // namespace art