blob: 4b6d82b35b8905e3eca8e2447eb0a1afc224cdf5 [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"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_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 Rogersef7d42f2014-01-06 12:55:46 -080036static void AddReferrerLocation(std::ostream& os, 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,
Ian Rogersef7d42f2014-01-06 12:55:46 -080049 mirror::Class* referrer, const char* fmt, va_list* args = NULL)
Ian Rogers62d6c772013-02-27 08:32:07 -080050 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
Sebastien Hertz56adf602013-07-09 17:27:07 +020069// AbstractMethodError
70
Ian Rogersef7d42f2014-01-06 12:55:46 -080071void ThrowAbstractMethodError(mirror::ArtMethod* method) {
Sebastien Hertz56adf602013-07-09 17:27:07 +020072 ThrowException(NULL, "Ljava/lang/AbstractMethodError;", NULL,
73 StringPrintf("abstract method \"%s\"",
74 PrettyMethod(method).c_str()).c_str());
75}
76
Ian Rogers62d6c772013-02-27 08:32:07 -080077// ArithmeticException
78
Sebastien Hertz0a3b8632013-06-26 11:16:01 +020079void ThrowArithmeticExceptionDivideByZero() {
Ian Rogers62d6c772013-02-27 08:32:07 -080080 ThrowException(NULL, "Ljava/lang/ArithmeticException;", NULL, "divide by zero");
81}
82
83// ArrayIndexOutOfBoundsException
84
85void ThrowArrayIndexOutOfBoundsException(int index, int length) {
86 ThrowException(NULL, "Ljava/lang/ArrayIndexOutOfBoundsException;", NULL,
87 StringPrintf("length=%d; index=%d", length, index).c_str());
88}
89
90// ArrayStoreException
91
Ian Rogersef7d42f2014-01-06 12:55:46 -080092void ThrowArrayStoreException(mirror::Class* element_class, mirror::Class* array_class) {
Ian Rogers62d6c772013-02-27 08:32:07 -080093 ThrowException(NULL, "Ljava/lang/ArrayStoreException;", NULL,
94 StringPrintf("%s cannot be stored in an array of type %s",
95 PrettyDescriptor(element_class).c_str(),
96 PrettyDescriptor(array_class).c_str()).c_str());
97}
98
99// ClassCastException
100
Ian Rogersef7d42f2014-01-06 12:55:46 -0800101void ThrowClassCastException(mirror::Class* dest_type, mirror::Class* src_type) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800102 ThrowException(NULL, "Ljava/lang/ClassCastException;", NULL,
103 StringPrintf("%s cannot be cast to %s",
104 PrettyDescriptor(src_type).c_str(),
105 PrettyDescriptor(dest_type).c_str()).c_str());
106}
107
108void ThrowClassCastException(const ThrowLocation* throw_location, const char* msg) {
109 ThrowException(throw_location, "Ljava/lang/ClassCastException;", NULL, msg);
110}
111
112// ClassCircularityError
113
114void ThrowClassCircularityError(mirror::Class* c) {
115 std::ostringstream msg;
116 msg << PrettyDescriptor(c);
117 ThrowException(NULL, "Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
118}
119
120// ClassFormatError
121
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122void ThrowClassFormatError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800123 va_list args;
124 va_start(args, fmt);
125 ThrowException(NULL, "Ljava/lang/ClassFormatError;", referrer, fmt, &args);
126 va_end(args);}
127
Ian Rogers87e552d2012-08-31 15:54:48 -0700128// IllegalAccessError
129
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700131 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700133 << PrettyDescriptor(accessed) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800134 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700135}
136
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 mirror::ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700139 InvokeType type) {
140 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700141 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
142 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700143 << " method " << PrettyMethod(called).c_str();
Ian Rogers62d6c772013-02-27 08:32:07 -0800144 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700145}
146
Brian Carlstromea46f952013-07-30 01:26:50 -0700147void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700148 std::ostringstream msg;
149 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
150 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800151 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700152}
153
Brian Carlstromea46f952013-07-30 01:26:50 -0700154void ThrowIllegalAccessErrorField(mirror::Class* referrer, mirror::ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700155 std::ostringstream msg;
156 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
157 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700159}
160
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161void ThrowIllegalAccessErrorFinalField(mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700162 mirror::ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700163 std::ostringstream msg;
164 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
165 << PrettyMethod(referrer) << "'";
Brian Carlstromea46f952013-07-30 01:26:50 -0700166 ThrowException(NULL, "Ljava/lang/IllegalAccessError;",
167 referrer != NULL ? referrer->GetClass() : NULL,
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700169}
170
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700171void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800172 va_list args;
173 va_start(args, fmt);
174 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
175 va_end(args);
176}
177
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700178// IllegalAccessException
179
180void ThrowIllegalAccessException(const ThrowLocation* throw_location, const char* msg) {
181 ThrowException(throw_location, "Ljava/lang/IllegalAccessException;", NULL, msg);
182}
183
Ian Rogers62d6c772013-02-27 08:32:07 -0800184// IllegalArgumentException
185
186void ThrowIllegalArgumentException(const ThrowLocation* throw_location, const char* msg) {
187 ThrowException(throw_location, "Ljava/lang/IllegalArgumentException;", NULL, msg);
188}
189
190
Ian Rogers87e552d2012-08-31 15:54:48 -0700191// IncompatibleClassChangeError
192
193void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700194 mirror::ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800195 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700196 std::ostringstream msg;
197 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
198 << expected_type << " but instead was found to be of type " << found_type;
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
200 referrer != NULL ? referrer->GetClass() : NULL,
201 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700202}
203
Ian Rogersef7d42f2014-01-06 12:55:46 -0800204void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(mirror::ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800206 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700207 // Referrer is calling interface_method on this_object, however, the interface_method isn't
208 // implemented by this_object.
209 CHECK(this_object != NULL);
210 std::ostringstream msg;
211 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
212 << "' does not implement interface '"
213 << PrettyDescriptor(interface_method->GetDeclaringClass())
214 << "' in call to '" << PrettyMethod(interface_method) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
216 referrer != NULL ? referrer->GetClass() : NULL,
217 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700218}
219
Ian Rogersef7d42f2014-01-06 12:55:46 -0800220void ThrowIncompatibleClassChangeErrorField(mirror::ArtField* resolved_field, bool is_static,
221 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700222 std::ostringstream msg;
223 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700224 << (is_static ? "static" : "instance") << " field" << " rather than a "
225 << (is_static ? "instance" : "static") << " field";
Ian Rogers62d6c772013-02-27 08:32:07 -0800226 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer->GetClass(),
227 msg.str().c_str());
228}
229
Ian Rogersef7d42f2014-01-06 12:55:46 -0800230void ThrowIncompatibleClassChangeError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 va_list args;
232 va_start(args, fmt);
233 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
234 va_end(args);
235}
236
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700237// IOException
238
239void ThrowIOException(const char* fmt, ...) {
240 va_list args;
241 va_start(args, fmt);
242 ThrowException(NULL, "Ljava/io/IOException;", NULL, fmt, &args);
243 va_end(args);
244}
245
Ian Rogers62d6c772013-02-27 08:32:07 -0800246// LinkageError
247
Ian Rogersef7d42f2014-01-06 12:55:46 -0800248void ThrowLinkageError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800249 va_list args;
250 va_start(args, fmt);
251 ThrowException(NULL, "Ljava/lang/LinkageError;", referrer, fmt, &args);
252 va_end(args);
253}
254
255// NegativeArraySizeException
256
257void ThrowNegativeArraySizeException(int size) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700258 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL,
259 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800260}
261
262void ThrowNegativeArraySizeException(const char* msg) {
263 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, msg);
264}
265
266// NoSuchFieldError
267
268void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
269 const StringPiece& type, const StringPiece& name)
270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
271 ClassHelper kh(c);
272 std::ostringstream msg;
273 msg << "No " << scope << "field " << name << " of type " << type
274 << " in class " << kh.GetDescriptor() << " or its superclasses";
275 ThrowException(NULL, "Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700276}
277
278// NoSuchMethodError
279
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700281 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700282 std::ostringstream msg;
283 ClassHelper kh(c);
284 msg << "No " << type << " method " << name << signature
285 << " in class " << kh.GetDescriptor() << " or its super classes";
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 ThrowException(NULL, "Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700287}
288
Ian Rogers62d6c772013-02-27 08:32:07 -0800289void ThrowNoSuchMethodError(uint32_t method_idx) {
290 Thread* self = Thread::Current();
291 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
292 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700293 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700294 std::ostringstream msg;
295 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 ThrowException(&throw_location, "Ljava/lang/NoSuchMethodError;",
297 throw_location.GetMethod()->GetDeclaringClass(), msg.str().c_str());
298}
299
300// NullPointerException
301
302void ThrowNullPointerExceptionForFieldAccess(const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700303 mirror::ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800304 std::ostringstream msg;
305 msg << "Attempt to " << (is_read ? "read from" : "write to")
306 << " field '" << PrettyField(field, true) << "' on a null object reference";
307 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
308}
309
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200310static void ThrowNullPointerExceptionForMethodAccessImpl(const ThrowLocation& throw_location,
311 uint32_t method_idx,
312 const DexFile& dex_file,
313 InvokeType type)
314 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 std::ostringstream msg;
316 msg << "Attempt to invoke " << type << " method '"
317 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
318 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
319}
320
Brian Carlstromea46f952013-07-30 01:26:50 -0700321void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location,
322 uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200323 InvokeType type) {
324 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
325 const DexFile& dex_file = *dex_cache->GetDexFile();
326 ThrowNullPointerExceptionForMethodAccessImpl(throw_location, method_idx,
327 dex_file, type);
328}
329
330void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700331 mirror::ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200332 InvokeType type) {
333 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
334 const DexFile& dex_file = *dex_cache->GetDexFile();
335 ThrowNullPointerExceptionForMethodAccessImpl(throw_location, method->GetDexMethodIndex(),
336 dex_file, type);
337}
338
Ian Rogers62d6c772013-02-27 08:32:07 -0800339void ThrowNullPointerExceptionFromDexPC(const ThrowLocation& throw_location) {
340 const DexFile::CodeItem* code = MethodHelper(throw_location.GetMethod()).GetCodeItem();
341 uint32_t throw_dex_pc = throw_location.GetDexPc();
342 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
343 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 switch (instr->Opcode()) {
345 case Instruction::INVOKE_DIRECT:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200346 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kDirect);
347 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 case Instruction::INVOKE_DIRECT_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200349 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 break;
351 case Instruction::INVOKE_VIRTUAL:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200352 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kVirtual);
353 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 case Instruction::INVOKE_VIRTUAL_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200355 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 break;
357 case Instruction::INVOKE_INTERFACE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200358 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kInterface);
359 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800360 case Instruction::INVOKE_INTERFACE_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200361 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200363 case Instruction::INVOKE_VIRTUAL_QUICK:
364 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
365 // Since we replaced the method index, we ask the verifier to tell us which
366 // method is invoked at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700367 mirror::ArtMethod* method =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200368 verifier::MethodVerifier::FindInvokedMethodAtDexPc(throw_location.GetMethod(),
369 throw_location.GetDexPc());
370 if (method != NULL) {
371 // NPE with precise message.
372 ThrowNullPointerExceptionForMethodAccess(throw_location, method, kVirtual);
373 } else {
374 // NPE with imprecise message.
375 ThrowNullPointerException(&throw_location,
376 "Attempt to invoke a virtual method on a null object reference");
377 }
378 break;
379 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 case Instruction::IGET:
381 case Instruction::IGET_WIDE:
382 case Instruction::IGET_OBJECT:
383 case Instruction::IGET_BOOLEAN:
384 case Instruction::IGET_BYTE:
385 case Instruction::IGET_CHAR:
386 case Instruction::IGET_SHORT: {
Brian Carlstromea46f952013-07-30 01:26:50 -0700387 mirror::ArtField* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200388 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 throw_location.GetMethod(), false);
390 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
391 break;
392 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200393 case Instruction::IGET_QUICK:
394 case Instruction::IGET_WIDE_QUICK:
395 case Instruction::IGET_OBJECT_QUICK: {
396 // Since we replaced the field index, we ask the verifier to tell us which
397 // field is accessed at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700398 mirror::ArtField* field =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200399 verifier::MethodVerifier::FindAccessedFieldAtDexPc(throw_location.GetMethod(),
400 throw_location.GetDexPc());
401 if (field != NULL) {
402 // NPE with precise message.
403 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
404 } else {
405 // NPE with imprecise message.
406 ThrowNullPointerException(&throw_location,
407 "Attempt to read from a field on a null object reference");
408 }
409 break;
410 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 case Instruction::IPUT:
412 case Instruction::IPUT_WIDE:
413 case Instruction::IPUT_OBJECT:
414 case Instruction::IPUT_BOOLEAN:
415 case Instruction::IPUT_BYTE:
416 case Instruction::IPUT_CHAR:
417 case Instruction::IPUT_SHORT: {
Brian Carlstromea46f952013-07-30 01:26:50 -0700418 mirror::ArtField* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200419 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800420 throw_location.GetMethod(), false);
421 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
422 break;
423 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200424 case Instruction::IPUT_QUICK:
425 case Instruction::IPUT_WIDE_QUICK:
426 case Instruction::IPUT_OBJECT_QUICK: {
427 // Since we replaced the field index, we ask the verifier to tell us which
428 // field is accessed at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700429 mirror::ArtField* field =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200430 verifier::MethodVerifier::FindAccessedFieldAtDexPc(throw_location.GetMethod(),
431 throw_location.GetDexPc());
432 if (field != NULL) {
433 // NPE with precise message.
434 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
435 } else {
436 // NPE with imprecise message.
437 ThrowNullPointerException(&throw_location,
438 "Attempt to write to a field on a null object reference");
439 }
440 break;
441 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800442 case Instruction::AGET:
443 case Instruction::AGET_WIDE:
444 case Instruction::AGET_OBJECT:
445 case Instruction::AGET_BOOLEAN:
446 case Instruction::AGET_BYTE:
447 case Instruction::AGET_CHAR:
448 case Instruction::AGET_SHORT:
449 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
450 "Attempt to read from null array");
451 break;
452 case Instruction::APUT:
453 case Instruction::APUT_WIDE:
454 case Instruction::APUT_OBJECT:
455 case Instruction::APUT_BOOLEAN:
456 case Instruction::APUT_BYTE:
457 case Instruction::APUT_CHAR:
458 case Instruction::APUT_SHORT:
459 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
460 "Attempt to write to null array");
461 break;
462 case Instruction::ARRAY_LENGTH:
463 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
464 "Attempt to get length of null array");
465 break;
466 default: {
467 // TODO: We should have covered all the cases where we expect a NPE above, this
468 // message/logging is so we can improve any cases we've missed in the future.
469 const DexFile& dex_file =
470 *throw_location.GetMethod()->GetDeclaringClass()->GetDexCache()->GetDexFile();
471 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
472 StringPrintf("Null pointer exception during instruction '%s'",
473 instr->DumpString(&dex_file).c_str()).c_str());
474 break;
475 }
476 }
477}
478
479void ThrowNullPointerException(const ThrowLocation* throw_location, const char* msg) {
480 ThrowException(throw_location, "Ljava/lang/NullPointerException;", NULL, msg);
481}
482
483// RuntimeException
484
485void ThrowRuntimeException(const char* fmt, ...) {
486 va_list args;
487 va_start(args, fmt);
488 ThrowException(NULL, "Ljava/lang/RuntimeException;", NULL, fmt, &args);
489 va_end(args);
490}
491
492// VerifyError
493
Ian Rogersef7d42f2014-01-06 12:55:46 -0800494void ThrowVerifyError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800495 va_list args;
496 va_start(args, fmt);
497 ThrowException(NULL, "Ljava/lang/VerifyError;", referrer, fmt, &args);
498 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700499}
500
501} // namespace art