blob: 16406c46219671809c9fb42b8cc0636f7938ebcf [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,
Ian Rogers2fc14272012-08-30 10:56:57 -0700147 Method* method, const Method* referrer) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700148 std::ostringstream msg;
jeffhaoc0228b82012-08-29 18:15:05 -0700149 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
Ian Rogers08f753d2012-08-24 14:35:25 -0700150 << expected_type << " but instead was found to be of type " << found_type;
jeffhaoe4f0b2a2012-08-30 11:18:57 -0700151 if (referrer != NULL) {
152 ClassHelper kh(referrer->GetDeclaringClass());
153 std::string location(kh.GetLocation());
154 if (!location.empty()) {
155 msg << " (accessed from " << location << ")";
156 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700157 }
158 Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
159 msg.str().c_str());
160}
161
162void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name,
Ian Rogers2fc14272012-08-30 10:56:57 -0700163 const StringPiece& signature, const Method* referrer) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700164 ClassHelper kh(c);
165 std::ostringstream msg;
166 msg << "No " << type << " method " << name << signature
167 << " in class " << kh.GetDescriptor() << " or its superclasses";
jeffhaoe4f0b2a2012-08-30 11:18:57 -0700168 if (referrer != NULL) {
169 kh.ChangeClass(referrer->GetDeclaringClass());
170 std::string location(kh.GetLocation());
171 if (!location.empty()) {
172 msg << " (accessed from " << location << ")";
173 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700174 }
175 Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700176}
177
Ian Rogers57b86d42012-03-27 16:05:41 -0700178void ThrowNewIllegalAccessErrorField(Thread* self,
179 Class* referrer,
180 Field* accessed) {
181 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
182 "Field '%s' is inaccessible to class '%s'",
183 PrettyField(accessed, false).c_str(),
184 PrettyDescriptor(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700185}
186
Ian Rogers57b86d42012-03-27 16:05:41 -0700187void ThrowNewIllegalAccessErrorFinalField(Thread* self,
188 const Method* referrer,
189 Field* accessed) {
190 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
191 "Final field '%s' cannot be written to by method '%s'",
192 PrettyField(accessed, false).c_str(),
193 PrettyMethod(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700194}
195
Ian Rogers57b86d42012-03-27 16:05:41 -0700196void ThrowNewIllegalAccessErrorMethod(Thread* self,
197 Class* referrer,
198 Method* accessed) {
199 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
200 "Method '%s' is inaccessible to class '%s'",
201 PrettyMethod(accessed).c_str(),
202 PrettyDescriptor(referrer).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700203}
204
Ian Rogers57b86d42012-03-27 16:05:41 -0700205void ThrowNullPointerExceptionForFieldAccess(Thread* self,
206 Field* field,
207 bool is_read) {
208 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
209 "Attempt to %s field '%s' on a null object reference",
210 is_read ? "read from" : "write to",
211 PrettyField(field, true).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700212}
213
Ian Rogers57b86d42012-03-27 16:05:41 -0700214void ThrowNullPointerExceptionForMethodAccess(Thread* self,
215 Method* caller,
216 uint32_t method_idx,
217 InvokeType type) {
218 const DexFile& dex_file =
219 Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
Ian Rogers57b86d42012-03-27 16:05:41 -0700220 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
221 "Attempt to invoke %s method '%s' on a null object reference",
Elliott Hughes6fcce302012-06-19 16:54:19 -0700222 ToStr<InvokeType>(type).c_str(),
Ian Rogers57b86d42012-03-27 16:05:41 -0700223 PrettyMethod(method_idx, dex_file, true).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700224}
225
TDYa1273f9137d2012-04-08 15:59:19 -0700226void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* throw_method, uint32_t dex_pc) {
227 const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem();
228 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
229 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
230 DecodedInstruction dec_insn(instr);
231 switch (instr->Opcode()) {
232 case Instruction::INVOKE_DIRECT:
233 case Instruction::INVOKE_DIRECT_RANGE:
234 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kDirect);
235 break;
236 case Instruction::INVOKE_VIRTUAL:
237 case Instruction::INVOKE_VIRTUAL_RANGE:
238 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kVirtual);
239 break;
240 case Instruction::IGET:
241 case Instruction::IGET_WIDE:
242 case Instruction::IGET_OBJECT:
243 case Instruction::IGET_BOOLEAN:
244 case Instruction::IGET_BYTE:
245 case Instruction::IGET_CHAR:
246 case Instruction::IGET_SHORT: {
247 Field* field =
248 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
249 ThrowNullPointerExceptionForFieldAccess(self, field, true /* read */);
250 break;
251 }
252 case Instruction::IPUT:
253 case Instruction::IPUT_WIDE:
254 case Instruction::IPUT_OBJECT:
255 case Instruction::IPUT_BOOLEAN:
256 case Instruction::IPUT_BYTE:
257 case Instruction::IPUT_CHAR:
258 case Instruction::IPUT_SHORT: {
259 Field* field =
260 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
261 ThrowNullPointerExceptionForFieldAccess(self, field, false /* write */);
262 break;
263 }
264 case Instruction::AGET:
265 case Instruction::AGET_WIDE:
266 case Instruction::AGET_OBJECT:
267 case Instruction::AGET_BOOLEAN:
268 case Instruction::AGET_BYTE:
269 case Instruction::AGET_CHAR:
270 case Instruction::AGET_SHORT:
271 self->ThrowNewException("Ljava/lang/NullPointerException;",
272 "Attempt to read from null array");
273 break;
274 case Instruction::APUT:
275 case Instruction::APUT_WIDE:
276 case Instruction::APUT_OBJECT:
277 case Instruction::APUT_BOOLEAN:
278 case Instruction::APUT_BYTE:
279 case Instruction::APUT_CHAR:
280 case Instruction::APUT_SHORT:
281 self->ThrowNewException("Ljava/lang/NullPointerException;",
282 "Attempt to write to null array");
283 break;
Elliott Hughes6fcce302012-06-19 16:54:19 -0700284 case Instruction::ARRAY_LENGTH:
285 self->ThrowNewException("Ljava/lang/NullPointerException;",
286 "Attempt to get length of null array");
287 break;
TDYa1273f9137d2012-04-08 15:59:19 -0700288 default: {
289 const DexFile& dex_file = Runtime::Current()->GetClassLinker()
290 ->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache());
291 std::string message("Null pointer exception during instruction '");
292 message += instr->DumpString(&dex_file);
293 message += "'";
294 self->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str());
295 break;
296 }
297 }
298}
299
Ian Rogers57b86d42012-03-27 16:05:41 -0700300std::string FieldNameFromIndex(const Method* method, uint32_t ref,
301 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700302 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700303
304 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
305 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
306
307 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700308 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700309 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700310 if (!access) {
311 return class_name + "." + field_name;
312 }
313
314 std::string result;
315 result += "tried to access field ";
316 result += class_name + "." + field_name;
317 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800318 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700319 return result;
320}
321
Ian Rogers57b86d42012-03-27 16:05:41 -0700322std::string MethodNameFromIndex(const Method* method, uint32_t ref,
323 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700324 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700325
326 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
327 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
328
329 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700330 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700331 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700332 if (!access) {
333 return class_name + "." + method_name;
334 }
335
336 std::string result;
337 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700338 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700339 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700340 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800341 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700342 return result;
343}
344
Ian Rogers57b86d42012-03-27 16:05:41 -0700345// Helper function to allocate array for FILLED_NEW_ARRAY.
346Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
347 Thread* self, bool access_check) {
348 if (UNLIKELY(component_count < 0)) {
349 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
350 return NULL; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700351 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700352 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
353 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
354 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
355 if (klass == NULL) { // Error
356 DCHECK(Thread::Current()->IsExceptionPending());
357 return NULL; // Failure
Ian Rogers19846512012-02-24 11:42:47 -0800358 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700359 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700360 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
361 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
362 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
363 "Bad filled array request for type %s",
364 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800365 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700366 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
367 "Found type %s; filled-new-array not implemented for anything but \'int\'",
368 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -0800369 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700370 return NULL; // Failure
Ian Rogersad25ac52011-10-04 19:13:33 -0700371 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700372 if (access_check) {
373 Class* referrer = method->GetDeclaringClass();
374 if (UNLIKELY(!referrer->CanAccess(klass))) {
375 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
376 return NULL; // Failure
377 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800378 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700379 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
380 return Array::Alloc(klass, component_count);
381 }
382}
383
Ian Rogers57b86d42012-03-27 16:05:41 -0700384Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
Ian Rogers08f753d2012-08-24 14:35:25 -0700385 FindFieldType type, size_t expected_size) {
386 bool is_primitive;
387 bool is_set;
388 bool is_static;
389 switch (type) {
390 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
391 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
392 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
393 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
394 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
395 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
396 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
397 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
398 default: is_primitive = true; is_set = true; is_static = true; break;
399 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700400 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
401 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
402 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700403 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
404 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700405 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700406 if (resolved_field->IsStatic() != is_static) {
407 ThrowNewIncompatibleClassChangeErrorField(self, resolved_field, is_static);
408 return NULL;
409 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700410 Class* fields_class = resolved_field->GetDeclaringClass();
411 Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700412 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
413 !referring_class->CanAccessMember(fields_class,
414 resolved_field->GetAccessFlags()))) {
415 // The referring class can't access the resolved field, this may occur as a result of a
416 // protected field being made public by a sub-class. Resort to the dex file to determine
417 // the correct class for the access check.
418 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
419 fields_class = class_linker->ResolveType(dex_file,
420 dex_file.GetFieldId(field_idx).class_idx_,
421 referring_class);
422 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
423 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
424 return NULL; // failure
425 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
426 resolved_field->GetAccessFlags()))) {
427 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
428 return NULL; // failure
429 }
430 }
431 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700432 ThrowNewIllegalAccessErrorFinalField(self, referrer, resolved_field);
433 return NULL; // failure
434 } else {
435 FieldHelper fh(resolved_field);
436 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
437 fh.FieldSize() != expected_size)) {
438 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
439 "Attempted read of %zd-bit %s on field '%s'",
440 expected_size * (32 / sizeof(int32_t)),
441 is_primitive ? "primitive" : "non-primitive",
442 PrettyField(resolved_field, true).c_str());
443 return NULL; // failure
444 } else if (!is_static) {
445 // instance fields must be being accessed on an initialized class
446 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800447 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700448 // If the class is already initializing, we must be inside <clinit>, or
449 // we'd still be waiting for the lock.
450 if (fields_class->IsInitializing()) {
451 return resolved_field;
Ian Rogers0045a292012-03-31 21:08:41 -0700452 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700453 return resolved_field;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800454 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700455 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
456 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800457 }
458 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700459 }
460 }
461}
462
463// Slow path method resolution
464Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
465 Thread* self, bool access_check, InvokeType type) {
466 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
467 bool is_direct = type == kStatic || type == kDirect;
Ian Rogers08f753d2012-08-24 14:35:25 -0700468 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700469 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700470 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
471 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700472 } else {
473 if (!access_check) {
474 if (is_direct) {
475 return resolved_method;
476 } else if (type == kInterface) {
477 Method* interface_method =
478 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
479 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700480 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self,
Ian Rogers57b86d42012-03-27 16:05:41 -0700481 resolved_method,
482 this_object);
Ian Rogers08f753d2012-08-24 14:35:25 -0700483 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700484 } else {
485 return interface_method;
486 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800487 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -0700488 ObjectArray<Method>* vtable;
489 uint16_t vtable_index = resolved_method->GetMethodIndex();
490 if (type == kSuper) {
491 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
492 } else {
493 vtable = this_object->GetClass()->GetVTable();
494 }
495 // TODO: eliminate bounds check?
496 return vtable->Get(vtable_index);
497 }
498 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700499 // Incompatible class change should have been handled in resolve method.
500 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
Ian Rogers2fc14272012-08-30 10:56:57 -0700501 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
502 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700503 return NULL; // Failure.
504 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700505 Class* methods_class = resolved_method->GetDeclaringClass();
506 Class* referring_class = referrer->GetDeclaringClass();
507 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
508 !referring_class->CanAccessMember(methods_class,
509 resolved_method->GetAccessFlags()))) {
510 // The referring class can't access the resolved method, this may occur as a result of a
511 // protected method being made public by implementing an interface that re-declares the
512 // method public. Resort to the dex file to determine the correct class for the access check
513 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
514 methods_class = class_linker->ResolveType(dex_file,
515 dex_file.GetMethodId(method_idx).class_idx_,
516 referring_class);
517 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
518 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
519 referrer, resolved_method, type);
Ian Rogers08f753d2012-08-24 14:35:25 -0700520 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700521 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
522 resolved_method->GetAccessFlags()))) {
523 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
Ian Rogers08f753d2012-08-24 14:35:25 -0700524 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700525 }
526 }
527 if (is_direct) {
528 return resolved_method;
529 } else if (type == kInterface) {
530 Method* interface_method =
531 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
532 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700533 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self,
Ian Rogers57b86d42012-03-27 16:05:41 -0700534 resolved_method,
535 this_object);
Ian Rogers08f753d2012-08-24 14:35:25 -0700536 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700537 } else {
538 return interface_method;
539 }
540 } else {
541 ObjectArray<Method>* vtable;
542 uint16_t vtable_index = resolved_method->GetMethodIndex();
543 if (type == kSuper) {
544 Class* super_class = referring_class->GetSuperClass();
545 if (LIKELY(super_class != NULL)) {
546 vtable = referring_class->GetSuperClass()->GetVTable();
547 } else {
548 vtable = NULL;
549 }
550 } else {
551 vtable = this_object->GetClass()->GetVTable();
552 }
553 if (LIKELY(vtable != NULL &&
554 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
555 return vtable->GetWithoutChecks(vtable_index);
556 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700557 // Behavior to agree with that of the verifier.
558 MethodHelper mh(resolved_method);
559 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
Ian Rogers2fc14272012-08-30 10:56:57 -0700560 mh.GetSignature(), referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700561 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700562 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800563 }
564 }
565 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800566}
567
Ian Rogers57b86d42012-03-27 16:05:41 -0700568Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
569 bool can_run_clinit, bool verify_access) {
570 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
571 Class* klass = class_linker->ResolveType(type_idx, referrer);
572 if (UNLIKELY(klass == NULL)) {
jeffhao441d9122012-03-21 17:29:10 -0700573 CHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -0700574 return NULL; // Failure - Indicate to caller to deliver exception
jeffhao441d9122012-03-21 17:29:10 -0700575 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700576 // Perform access check if necessary.
577 Class* referring_class = referrer->GetDeclaringClass();
578 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
579 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
580 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogers14b1b242011-10-11 18:54:34 -0700581 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700582 // If we're just implementing const-class, we shouldn't call <clinit>.
583 if (!can_run_clinit) {
584 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700585 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700586 // If we are the <clinit> of this class, just return our storage.
587 //
588 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
589 // running.
590 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
591 return klass;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700592 }
Ian Rogers0045a292012-03-31 21:08:41 -0700593 if (!class_linker->EnsureInitialized(klass, true, true)) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700594 CHECK(self->IsExceptionPending());
595 return NULL; // Failure - Indicate to caller to deliver exception
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700596 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700597 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
598 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700599}
600
Shih-wei Liao2d831012011-09-28 22:06:53 -0700601} // namespace art