blob: 1e114bb6dd81e2071134c0fdb8904af5fbb95c87 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
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 "common_throws.h"
18
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020022#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070023#include "invoke_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/abstract_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/object-inl.h"
27#include "mirror/object_array-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070028#include "object_utils.h"
29#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020030#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070031
32#include <sstream>
33
34namespace art {
35
Ian Rogers62d6c772013-02-27 08:32:07 -080036static void AddReferrerLocation(std::ostream& os, const mirror::Class* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -070037 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070038 if (referrer != NULL) {
39 ClassHelper kh(referrer);
40 std::string location(kh.GetLocation());
41 if (!location.empty()) {
42 os << " (declaration of '" << PrettyDescriptor(referrer)
43 << "' appears in " << location << ")";
44 }
45 }
46}
47
Ian Rogers62d6c772013-02-27 08:32:07 -080048static void ThrowException(const ThrowLocation* throw_location, const char* exception_descriptor,
49 const mirror::Class* referrer, const char* fmt, va_list* args = NULL)
50 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070051 std::ostringstream msg;
Ian Rogers62d6c772013-02-27 08:32:07 -080052 if (args != NULL) {
53 std::string vmsg;
54 StringAppendV(&vmsg, fmt, *args);
55 msg << vmsg;
56 } else {
57 msg << fmt;
58 }
59 AddReferrerLocation(msg, referrer);
60 Thread* self = Thread::Current();
61 if (throw_location == NULL) {
62 ThrowLocation computed_throw_location = self->GetCurrentLocationForThrow();
63 self->ThrowNewException(computed_throw_location, exception_descriptor, msg.str().c_str());
64 } else {
65 self->ThrowNewException(*throw_location, exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070066 }
67}
68
Ian Rogers62d6c772013-02-27 08:32:07 -080069// ArithmeticException
70
71void ThrowArithmeticExceptionDivideByZero(Thread* self) {
72 ThrowException(NULL, "Ljava/lang/ArithmeticException;", NULL, "divide by zero");
73}
74
75// ArrayIndexOutOfBoundsException
76
77void ThrowArrayIndexOutOfBoundsException(int index, int length) {
78 ThrowException(NULL, "Ljava/lang/ArrayIndexOutOfBoundsException;", NULL,
79 StringPrintf("length=%d; index=%d", length, index).c_str());
80}
81
82// ArrayStoreException
83
84void ThrowArrayStoreException(const mirror::Class* element_class,
85 const mirror::Class* array_class) {
86 ThrowException(NULL, "Ljava/lang/ArrayStoreException;", NULL,
87 StringPrintf("%s cannot be stored in an array of type %s",
88 PrettyDescriptor(element_class).c_str(),
89 PrettyDescriptor(array_class).c_str()).c_str());
90}
91
92// ClassCastException
93
94void ThrowClassCastException(const mirror::Class* dest_type, const mirror::Class* src_type) {
95 ThrowException(NULL, "Ljava/lang/ClassCastException;", NULL,
96 StringPrintf("%s cannot be cast to %s",
97 PrettyDescriptor(src_type).c_str(),
98 PrettyDescriptor(dest_type).c_str()).c_str());
99}
100
101void ThrowClassCastException(const ThrowLocation* throw_location, const char* msg) {
102 ThrowException(throw_location, "Ljava/lang/ClassCastException;", NULL, msg);
103}
104
105// ClassCircularityError
106
107void ThrowClassCircularityError(mirror::Class* c) {
108 std::ostringstream msg;
109 msg << PrettyDescriptor(c);
110 ThrowException(NULL, "Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
111}
112
113// ClassFormatError
114
115void ThrowClassFormatError(const mirror::Class* referrer, const char* fmt, ...) {
116 va_list args;
117 va_start(args, fmt);
118 ThrowException(NULL, "Ljava/lang/ClassFormatError;", referrer, fmt, &args);
119 va_end(args);}
120
Ian Rogers87e552d2012-08-31 15:54:48 -0700121// IllegalAccessError
122
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700124 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700125 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700126 << PrettyDescriptor(accessed) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800127 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700128}
129
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
131 const mirror::AbstractMethod* caller,
132 const mirror::AbstractMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700133 InvokeType type) {
134 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700135 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
136 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700137 << " method " << PrettyMethod(called).c_str();
Ian Rogers62d6c772013-02-27 08:32:07 -0800138 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700139}
140
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::AbstractMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700142 std::ostringstream msg;
143 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
144 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800145 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700146}
147
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148void ThrowIllegalAccessErrorField(mirror::Class* referrer, mirror::Field* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700149 std::ostringstream msg;
150 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
151 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800152 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700153}
154
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155void ThrowIllegalAccessErrorFinalField(const mirror::AbstractMethod* referrer,
156 mirror::Field* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700157 std::ostringstream msg;
158 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
159 << PrettyMethod(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800160 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer != NULL ? referrer->GetClass() : NULL,
161 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700162}
163
Ian Rogers62d6c772013-02-27 08:32:07 -0800164void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...){
165 va_list args;
166 va_start(args, fmt);
167 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
168 va_end(args);
169}
170
171// IllegalArgumentException
172
173void ThrowIllegalArgumentException(const ThrowLocation* throw_location, const char* msg) {
174 ThrowException(throw_location, "Ljava/lang/IllegalArgumentException;", NULL, msg);
175}
176
177
Ian Rogers87e552d2012-08-31 15:54:48 -0700178// IncompatibleClassChangeError
179
180void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 mirror::AbstractMethod* method,
182 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700183 std::ostringstream msg;
184 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
185 << expected_type << " but instead was found to be of type " << found_type;
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
187 referrer != NULL ? referrer->GetClass() : NULL,
188 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700189}
190
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const mirror::AbstractMethod* interface_method,
192 mirror::Object* this_object,
193 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700194 // Referrer is calling interface_method on this_object, however, the interface_method isn't
195 // implemented by this_object.
196 CHECK(this_object != NULL);
197 std::ostringstream msg;
198 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
199 << "' does not implement interface '"
200 << PrettyDescriptor(interface_method->GetDeclaringClass())
201 << "' in call to '" << PrettyMethod(interface_method) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
203 referrer != NULL ? referrer->GetClass() : NULL,
204 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700205}
206
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207void ThrowIncompatibleClassChangeErrorField(const mirror::Field* resolved_field, bool is_static,
208 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700209 std::ostringstream msg;
210 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700211 << (is_static ? "static" : "instance") << " field" << " rather than a "
212 << (is_static ? "instance" : "static") << " field";
Ian Rogers62d6c772013-02-27 08:32:07 -0800213 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer->GetClass(),
214 msg.str().c_str());
215}
216
217void ThrowIncompatibleClassChangeError(const mirror::Class* referrer, const char* fmt, ...){
218 va_list args;
219 va_start(args, fmt);
220 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
221 va_end(args);
222}
223
224// LinkageError
225
226void ThrowLinkageError(const mirror::Class* referrer, const char* fmt, ...) {
227 va_list args;
228 va_start(args, fmt);
229 ThrowException(NULL, "Ljava/lang/LinkageError;", referrer, fmt, &args);
230 va_end(args);
231}
232
233// NegativeArraySizeException
234
235void ThrowNegativeArraySizeException(int size) {
236 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, StringPrintf("%d", size).c_str());
237}
238
239void ThrowNegativeArraySizeException(const char* msg) {
240 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, msg);
241}
242
243// NoSuchFieldError
244
245void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
246 const StringPiece& type, const StringPiece& name)
247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
248 ClassHelper kh(c);
249 std::ostringstream msg;
250 msg << "No " << scope << "field " << name << " of type " << type
251 << " in class " << kh.GetDescriptor() << " or its superclasses";
252 ThrowException(NULL, "Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700253}
254
255// NoSuchMethodError
256
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 const StringPiece& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700259 std::ostringstream msg;
260 ClassHelper kh(c);
261 msg << "No " << type << " method " << name << signature
262 << " in class " << kh.GetDescriptor() << " or its super classes";
Ian Rogers62d6c772013-02-27 08:32:07 -0800263 ThrowException(NULL, "Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700264}
265
Ian Rogers62d6c772013-02-27 08:32:07 -0800266void ThrowNoSuchMethodError(uint32_t method_idx) {
267 Thread* self = Thread::Current();
268 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
269 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700270 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700271 std::ostringstream msg;
272 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 ThrowException(&throw_location, "Ljava/lang/NoSuchMethodError;",
274 throw_location.GetMethod()->GetDeclaringClass(), msg.str().c_str());
275}
276
277// NullPointerException
278
279void ThrowNullPointerExceptionForFieldAccess(const ThrowLocation& throw_location,
280 mirror::Field* field, bool is_read) {
281 std::ostringstream msg;
282 msg << "Attempt to " << (is_read ? "read from" : "write to")
283 << " field '" << PrettyField(field, true) << "' on a null object reference";
284 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
285}
286
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200287static void ThrowNullPointerExceptionForMethodAccessImpl(const ThrowLocation& throw_location,
288 uint32_t method_idx,
289 const DexFile& dex_file,
290 InvokeType type)
291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 std::ostringstream msg;
293 msg << "Attempt to invoke " << type << " method '"
294 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
295 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
296}
297
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200298void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location, uint32_t method_idx,
299 InvokeType type) {
300 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
301 const DexFile& dex_file = *dex_cache->GetDexFile();
302 ThrowNullPointerExceptionForMethodAccessImpl(throw_location, method_idx,
303 dex_file, type);
304}
305
306void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location,
307 mirror::AbstractMethod* method,
308 InvokeType type) {
309 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
310 const DexFile& dex_file = *dex_cache->GetDexFile();
311 ThrowNullPointerExceptionForMethodAccessImpl(throw_location, method->GetDexMethodIndex(),
312 dex_file, type);
313}
314
Ian Rogers62d6c772013-02-27 08:32:07 -0800315void ThrowNullPointerExceptionFromDexPC(const ThrowLocation& throw_location) {
316 const DexFile::CodeItem* code = MethodHelper(throw_location.GetMethod()).GetCodeItem();
317 uint32_t throw_dex_pc = throw_location.GetDexPc();
318 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
319 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800320 switch (instr->Opcode()) {
321 case Instruction::INVOKE_DIRECT:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200322 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kDirect);
323 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 case Instruction::INVOKE_DIRECT_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200325 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 break;
327 case Instruction::INVOKE_VIRTUAL:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200328 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kVirtual);
329 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 case Instruction::INVOKE_VIRTUAL_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200331 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800332 break;
333 case Instruction::INVOKE_INTERFACE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200334 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kInterface);
335 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 case Instruction::INVOKE_INTERFACE_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200337 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200339 case Instruction::INVOKE_VIRTUAL_QUICK:
340 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
341 // Since we replaced the method index, we ask the verifier to tell us which
342 // method is invoked at this location.
343 mirror::AbstractMethod* method =
344 verifier::MethodVerifier::FindInvokedMethodAtDexPc(throw_location.GetMethod(),
345 throw_location.GetDexPc());
346 if (method != NULL) {
347 // NPE with precise message.
348 ThrowNullPointerExceptionForMethodAccess(throw_location, method, kVirtual);
349 } else {
350 // NPE with imprecise message.
351 ThrowNullPointerException(&throw_location,
352 "Attempt to invoke a virtual method on a null object reference");
353 }
354 break;
355 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 case Instruction::IGET:
357 case Instruction::IGET_WIDE:
358 case Instruction::IGET_OBJECT:
359 case Instruction::IGET_BOOLEAN:
360 case Instruction::IGET_BYTE:
361 case Instruction::IGET_CHAR:
362 case Instruction::IGET_SHORT: {
363 mirror::Field* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200364 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 throw_location.GetMethod(), false);
366 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
367 break;
368 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200369 case Instruction::IGET_QUICK:
370 case Instruction::IGET_WIDE_QUICK:
371 case Instruction::IGET_OBJECT_QUICK: {
372 // Since we replaced the field index, we ask the verifier to tell us which
373 // field is accessed at this location.
374 mirror::Field* field =
375 verifier::MethodVerifier::FindAccessedFieldAtDexPc(throw_location.GetMethod(),
376 throw_location.GetDexPc());
377 if (field != NULL) {
378 // NPE with precise message.
379 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
380 } else {
381 // NPE with imprecise message.
382 ThrowNullPointerException(&throw_location,
383 "Attempt to read from a field on a null object reference");
384 }
385 break;
386 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 case Instruction::IPUT:
388 case Instruction::IPUT_WIDE:
389 case Instruction::IPUT_OBJECT:
390 case Instruction::IPUT_BOOLEAN:
391 case Instruction::IPUT_BYTE:
392 case Instruction::IPUT_CHAR:
393 case Instruction::IPUT_SHORT: {
394 mirror::Field* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200395 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800396 throw_location.GetMethod(), false);
397 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
398 break;
399 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200400 case Instruction::IPUT_QUICK:
401 case Instruction::IPUT_WIDE_QUICK:
402 case Instruction::IPUT_OBJECT_QUICK: {
403 // Since we replaced the field index, we ask the verifier to tell us which
404 // field is accessed at this location.
405 mirror::Field* field =
406 verifier::MethodVerifier::FindAccessedFieldAtDexPc(throw_location.GetMethod(),
407 throw_location.GetDexPc());
408 if (field != NULL) {
409 // NPE with precise message.
410 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
411 } else {
412 // NPE with imprecise message.
413 ThrowNullPointerException(&throw_location,
414 "Attempt to write to a field on a null object reference");
415 }
416 break;
417 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 case Instruction::AGET:
419 case Instruction::AGET_WIDE:
420 case Instruction::AGET_OBJECT:
421 case Instruction::AGET_BOOLEAN:
422 case Instruction::AGET_BYTE:
423 case Instruction::AGET_CHAR:
424 case Instruction::AGET_SHORT:
425 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
426 "Attempt to read from null array");
427 break;
428 case Instruction::APUT:
429 case Instruction::APUT_WIDE:
430 case Instruction::APUT_OBJECT:
431 case Instruction::APUT_BOOLEAN:
432 case Instruction::APUT_BYTE:
433 case Instruction::APUT_CHAR:
434 case Instruction::APUT_SHORT:
435 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
436 "Attempt to write to null array");
437 break;
438 case Instruction::ARRAY_LENGTH:
439 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
440 "Attempt to get length of null array");
441 break;
442 default: {
443 // TODO: We should have covered all the cases where we expect a NPE above, this
444 // message/logging is so we can improve any cases we've missed in the future.
445 const DexFile& dex_file =
446 *throw_location.GetMethod()->GetDeclaringClass()->GetDexCache()->GetDexFile();
447 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
448 StringPrintf("Null pointer exception during instruction '%s'",
449 instr->DumpString(&dex_file).c_str()).c_str());
450 break;
451 }
452 }
453}
454
455void ThrowNullPointerException(const ThrowLocation* throw_location, const char* msg) {
456 ThrowException(throw_location, "Ljava/lang/NullPointerException;", NULL, msg);
457}
458
459// RuntimeException
460
461void ThrowRuntimeException(const char* fmt, ...) {
462 va_list args;
463 va_start(args, fmt);
464 ThrowException(NULL, "Ljava/lang/RuntimeException;", NULL, fmt, &args);
465 va_end(args);
466}
467
468// VerifyError
469
470void ThrowVerifyError(const mirror::Class* referrer, const char* fmt, ...) {
471 va_list args;
472 va_start(args, fmt);
473 ThrowException(NULL, "Ljava/lang/VerifyError;", referrer, fmt, &args);
474 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700475}
476
477} // namespace art