blob: 315f274788dbc935d12764b176aba1e4c016decd [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
Andreas Gampe329d1882014-04-08 10:32:19 -070069static void ThrowWrappedException(const ThrowLocation* throw_location,
70 const char* exception_descriptor,
71 mirror::Class* referrer, const char* fmt, va_list* args = NULL)
72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
73 std::ostringstream msg;
74 if (args != NULL) {
75 std::string vmsg;
76 StringAppendV(&vmsg, fmt, *args);
77 msg << vmsg;
78 } else {
79 msg << fmt;
80 }
81 AddReferrerLocation(msg, referrer);
82 Thread* self = Thread::Current();
83 if (throw_location == NULL) {
84 ThrowLocation computed_throw_location = self->GetCurrentLocationForThrow();
85 self->ThrowNewWrappedException(computed_throw_location, exception_descriptor, msg.str().c_str());
86 } else {
87 self->ThrowNewWrappedException(*throw_location, exception_descriptor, msg.str().c_str());
88 }
89}
90
Sebastien Hertz56adf602013-07-09 17:27:07 +020091// AbstractMethodError
92
Ian Rogersef7d42f2014-01-06 12:55:46 -080093void ThrowAbstractMethodError(mirror::ArtMethod* method) {
Sebastien Hertz56adf602013-07-09 17:27:07 +020094 ThrowException(NULL, "Ljava/lang/AbstractMethodError;", NULL,
95 StringPrintf("abstract method \"%s\"",
96 PrettyMethod(method).c_str()).c_str());
97}
98
Ian Rogers62d6c772013-02-27 08:32:07 -080099// ArithmeticException
100
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200101void ThrowArithmeticExceptionDivideByZero() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800102 ThrowException(NULL, "Ljava/lang/ArithmeticException;", NULL, "divide by zero");
103}
104
105// ArrayIndexOutOfBoundsException
106
107void ThrowArrayIndexOutOfBoundsException(int index, int length) {
108 ThrowException(NULL, "Ljava/lang/ArrayIndexOutOfBoundsException;", NULL,
109 StringPrintf("length=%d; index=%d", length, index).c_str());
110}
111
112// ArrayStoreException
113
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114void ThrowArrayStoreException(mirror::Class* element_class, mirror::Class* array_class) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800115 ThrowException(NULL, "Ljava/lang/ArrayStoreException;", NULL,
116 StringPrintf("%s cannot be stored in an array of type %s",
117 PrettyDescriptor(element_class).c_str(),
118 PrettyDescriptor(array_class).c_str()).c_str());
119}
120
121// ClassCastException
122
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123void ThrowClassCastException(mirror::Class* dest_type, mirror::Class* src_type) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 ThrowException(NULL, "Ljava/lang/ClassCastException;", NULL,
125 StringPrintf("%s cannot be cast to %s",
126 PrettyDescriptor(src_type).c_str(),
127 PrettyDescriptor(dest_type).c_str()).c_str());
128}
129
130void ThrowClassCastException(const ThrowLocation* throw_location, const char* msg) {
131 ThrowException(throw_location, "Ljava/lang/ClassCastException;", NULL, msg);
132}
133
134// ClassCircularityError
135
136void ThrowClassCircularityError(mirror::Class* c) {
137 std::ostringstream msg;
138 msg << PrettyDescriptor(c);
139 ThrowException(NULL, "Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
140}
141
142// ClassFormatError
143
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144void ThrowClassFormatError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800145 va_list args;
146 va_start(args, fmt);
147 ThrowException(NULL, "Ljava/lang/ClassFormatError;", referrer, fmt, &args);
148 va_end(args);}
149
Ian Rogers87e552d2012-08-31 15:54:48 -0700150// IllegalAccessError
151
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700153 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700155 << PrettyDescriptor(accessed) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700157}
158
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800160 mirror::ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700161 InvokeType type) {
162 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700163 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
164 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700165 << " method " << PrettyMethod(called).c_str();
Ian Rogers62d6c772013-02-27 08:32:07 -0800166 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700167}
168
Brian Carlstromea46f952013-07-30 01:26:50 -0700169void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700170 std::ostringstream msg;
171 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
172 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800173 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700174}
175
Brian Carlstromea46f952013-07-30 01:26:50 -0700176void ThrowIllegalAccessErrorField(mirror::Class* referrer, mirror::ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700177 std::ostringstream msg;
178 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
179 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800180 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700181}
182
Ian Rogersef7d42f2014-01-06 12:55:46 -0800183void ThrowIllegalAccessErrorFinalField(mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700184 mirror::ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700185 std::ostringstream msg;
186 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
187 << PrettyMethod(referrer) << "'";
Brian Carlstromea46f952013-07-30 01:26:50 -0700188 ThrowException(NULL, "Ljava/lang/IllegalAccessError;",
189 referrer != NULL ? referrer->GetClass() : NULL,
Ian Rogers62d6c772013-02-27 08:32:07 -0800190 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700191}
192
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700193void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800194 va_list args;
195 va_start(args, fmt);
196 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
197 va_end(args);
198}
199
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700200// IllegalAccessException
201
202void ThrowIllegalAccessException(const ThrowLocation* throw_location, const char* msg) {
203 ThrowException(throw_location, "Ljava/lang/IllegalAccessException;", NULL, msg);
204}
205
Ian Rogers62d6c772013-02-27 08:32:07 -0800206// IllegalArgumentException
207
208void ThrowIllegalArgumentException(const ThrowLocation* throw_location, const char* msg) {
209 ThrowException(throw_location, "Ljava/lang/IllegalArgumentException;", NULL, msg);
210}
211
212
Ian Rogers87e552d2012-08-31 15:54:48 -0700213// IncompatibleClassChangeError
214
215void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700216 mirror::ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700218 std::ostringstream msg;
219 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
220 << expected_type << " but instead was found to be of type " << found_type;
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
222 referrer != NULL ? referrer->GetClass() : NULL,
223 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700224}
225
Ian Rogersef7d42f2014-01-06 12:55:46 -0800226void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(mirror::ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800228 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700229 // Referrer is calling interface_method on this_object, however, the interface_method isn't
230 // implemented by this_object.
231 CHECK(this_object != NULL);
232 std::ostringstream msg;
233 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
234 << "' does not implement interface '"
235 << PrettyDescriptor(interface_method->GetDeclaringClass())
236 << "' in call to '" << PrettyMethod(interface_method) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800237 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
238 referrer != NULL ? referrer->GetClass() : NULL,
239 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700240}
241
Ian Rogersef7d42f2014-01-06 12:55:46 -0800242void ThrowIncompatibleClassChangeErrorField(mirror::ArtField* resolved_field, bool is_static,
243 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700244 std::ostringstream msg;
245 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 << (is_static ? "static" : "instance") << " field" << " rather than a "
247 << (is_static ? "instance" : "static") << " field";
Ian Rogers62d6c772013-02-27 08:32:07 -0800248 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer->GetClass(),
249 msg.str().c_str());
250}
251
Ian Rogersef7d42f2014-01-06 12:55:46 -0800252void ThrowIncompatibleClassChangeError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800253 va_list args;
254 va_start(args, fmt);
255 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
256 va_end(args);
257}
258
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700259// IOException
260
261void ThrowIOException(const char* fmt, ...) {
262 va_list args;
263 va_start(args, fmt);
264 ThrowException(NULL, "Ljava/io/IOException;", NULL, fmt, &args);
265 va_end(args);
266}
267
Andreas Gampe329d1882014-04-08 10:32:19 -0700268void ThrowWrappedIOException(const char* fmt, ...) {
269 va_list args;
270 va_start(args, fmt);
271 ThrowWrappedException(NULL, "Ljava/io/IOException;", NULL, fmt, &args);
272 va_end(args);
273}
274
Ian Rogers62d6c772013-02-27 08:32:07 -0800275// LinkageError
276
Ian Rogersef7d42f2014-01-06 12:55:46 -0800277void ThrowLinkageError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 va_list args;
279 va_start(args, fmt);
280 ThrowException(NULL, "Ljava/lang/LinkageError;", referrer, fmt, &args);
281 va_end(args);
282}
283
284// NegativeArraySizeException
285
286void ThrowNegativeArraySizeException(int size) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700287 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL,
288 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800289}
290
291void ThrowNegativeArraySizeException(const char* msg) {
292 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, msg);
293}
294
295// NoSuchFieldError
296
297void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
298 const StringPiece& type, const StringPiece& name)
299 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
300 ClassHelper kh(c);
301 std::ostringstream msg;
302 msg << "No " << scope << "field " << name << " of type " << type
303 << " in class " << kh.GetDescriptor() << " or its superclasses";
304 ThrowException(NULL, "Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700305}
306
307// NoSuchMethodError
308
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800309void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700310 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700311 std::ostringstream msg;
312 ClassHelper kh(c);
313 msg << "No " << type << " method " << name << signature
314 << " in class " << kh.GetDescriptor() << " or its super classes";
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 ThrowException(NULL, "Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700316}
317
Ian Rogers62d6c772013-02-27 08:32:07 -0800318void ThrowNoSuchMethodError(uint32_t method_idx) {
319 Thread* self = Thread::Current();
320 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
321 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700322 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700323 std::ostringstream msg;
324 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800325 ThrowException(&throw_location, "Ljava/lang/NoSuchMethodError;",
326 throw_location.GetMethod()->GetDeclaringClass(), msg.str().c_str());
327}
328
329// NullPointerException
330
331void ThrowNullPointerExceptionForFieldAccess(const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700332 mirror::ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800333 std::ostringstream msg;
334 msg << "Attempt to " << (is_read ? "read from" : "write to")
335 << " field '" << PrettyField(field, true) << "' on a null object reference";
336 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
337}
338
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200339static void ThrowNullPointerExceptionForMethodAccessImpl(const ThrowLocation& throw_location,
340 uint32_t method_idx,
341 const DexFile& dex_file,
342 InvokeType type)
343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 std::ostringstream msg;
345 msg << "Attempt to invoke " << type << " method '"
346 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
347 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
348}
349
Brian Carlstromea46f952013-07-30 01:26:50 -0700350void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location,
351 uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200352 InvokeType type) {
353 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
354 const DexFile& dex_file = *dex_cache->GetDexFile();
355 ThrowNullPointerExceptionForMethodAccessImpl(throw_location, method_idx,
356 dex_file, type);
357}
358
359void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700360 mirror::ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200361 InvokeType type) {
362 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
363 const DexFile& dex_file = *dex_cache->GetDexFile();
364 ThrowNullPointerExceptionForMethodAccessImpl(throw_location, method->GetDexMethodIndex(),
365 dex_file, type);
366}
367
Ian Rogers62d6c772013-02-27 08:32:07 -0800368void ThrowNullPointerExceptionFromDexPC(const ThrowLocation& throw_location) {
369 const DexFile::CodeItem* code = MethodHelper(throw_location.GetMethod()).GetCodeItem();
370 uint32_t throw_dex_pc = throw_location.GetDexPc();
371 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
372 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 switch (instr->Opcode()) {
374 case Instruction::INVOKE_DIRECT:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200375 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kDirect);
376 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 case Instruction::INVOKE_DIRECT_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200378 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 break;
380 case Instruction::INVOKE_VIRTUAL:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200381 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kVirtual);
382 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 case Instruction::INVOKE_VIRTUAL_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200384 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 break;
386 case Instruction::INVOKE_INTERFACE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200387 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kInterface);
388 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 case Instruction::INVOKE_INTERFACE_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200390 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200392 case Instruction::INVOKE_VIRTUAL_QUICK:
393 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
394 // Since we replaced the method index, we ask the verifier to tell us which
395 // method is invoked at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700396 mirror::ArtMethod* method =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200397 verifier::MethodVerifier::FindInvokedMethodAtDexPc(throw_location.GetMethod(),
398 throw_location.GetDexPc());
399 if (method != NULL) {
400 // NPE with precise message.
401 ThrowNullPointerExceptionForMethodAccess(throw_location, method, kVirtual);
402 } else {
403 // NPE with imprecise message.
404 ThrowNullPointerException(&throw_location,
405 "Attempt to invoke a virtual method on a null object reference");
406 }
407 break;
408 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 case Instruction::IGET:
410 case Instruction::IGET_WIDE:
411 case Instruction::IGET_OBJECT:
412 case Instruction::IGET_BOOLEAN:
413 case Instruction::IGET_BYTE:
414 case Instruction::IGET_CHAR:
415 case Instruction::IGET_SHORT: {
Brian Carlstromea46f952013-07-30 01:26:50 -0700416 mirror::ArtField* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200417 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 throw_location.GetMethod(), false);
419 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
420 break;
421 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200422 case Instruction::IGET_QUICK:
423 case Instruction::IGET_WIDE_QUICK:
424 case Instruction::IGET_OBJECT_QUICK: {
425 // Since we replaced the field index, we ask the verifier to tell us which
426 // field is accessed at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700427 mirror::ArtField* field =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200428 verifier::MethodVerifier::FindAccessedFieldAtDexPc(throw_location.GetMethod(),
429 throw_location.GetDexPc());
430 if (field != NULL) {
431 // NPE with precise message.
432 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
433 } else {
434 // NPE with imprecise message.
435 ThrowNullPointerException(&throw_location,
436 "Attempt to read from a field on a null object reference");
437 }
438 break;
439 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800440 case Instruction::IPUT:
441 case Instruction::IPUT_WIDE:
442 case Instruction::IPUT_OBJECT:
443 case Instruction::IPUT_BOOLEAN:
444 case Instruction::IPUT_BYTE:
445 case Instruction::IPUT_CHAR:
446 case Instruction::IPUT_SHORT: {
Brian Carlstromea46f952013-07-30 01:26:50 -0700447 mirror::ArtField* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200448 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800449 throw_location.GetMethod(), false);
450 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
451 break;
452 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200453 case Instruction::IPUT_QUICK:
454 case Instruction::IPUT_WIDE_QUICK:
455 case Instruction::IPUT_OBJECT_QUICK: {
456 // Since we replaced the field index, we ask the verifier to tell us which
457 // field is accessed at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700458 mirror::ArtField* field =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200459 verifier::MethodVerifier::FindAccessedFieldAtDexPc(throw_location.GetMethod(),
460 throw_location.GetDexPc());
461 if (field != NULL) {
462 // NPE with precise message.
463 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
464 } else {
465 // NPE with imprecise message.
466 ThrowNullPointerException(&throw_location,
467 "Attempt to write to a field on a null object reference");
468 }
469 break;
470 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800471 case Instruction::AGET:
472 case Instruction::AGET_WIDE:
473 case Instruction::AGET_OBJECT:
474 case Instruction::AGET_BOOLEAN:
475 case Instruction::AGET_BYTE:
476 case Instruction::AGET_CHAR:
477 case Instruction::AGET_SHORT:
478 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
479 "Attempt to read from null array");
480 break;
481 case Instruction::APUT:
482 case Instruction::APUT_WIDE:
483 case Instruction::APUT_OBJECT:
484 case Instruction::APUT_BOOLEAN:
485 case Instruction::APUT_BYTE:
486 case Instruction::APUT_CHAR:
487 case Instruction::APUT_SHORT:
488 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
489 "Attempt to write to null array");
490 break;
491 case Instruction::ARRAY_LENGTH:
492 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
493 "Attempt to get length of null array");
494 break;
495 default: {
496 // TODO: We should have covered all the cases where we expect a NPE above, this
497 // message/logging is so we can improve any cases we've missed in the future.
498 const DexFile& dex_file =
499 *throw_location.GetMethod()->GetDeclaringClass()->GetDexCache()->GetDexFile();
500 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
501 StringPrintf("Null pointer exception during instruction '%s'",
502 instr->DumpString(&dex_file).c_str()).c_str());
503 break;
504 }
505 }
506}
507
508void ThrowNullPointerException(const ThrowLocation* throw_location, const char* msg) {
509 ThrowException(throw_location, "Ljava/lang/NullPointerException;", NULL, msg);
510}
511
512// RuntimeException
513
514void ThrowRuntimeException(const char* fmt, ...) {
515 va_list args;
516 va_start(args, fmt);
517 ThrowException(NULL, "Ljava/lang/RuntimeException;", NULL, fmt, &args);
518 va_end(args);
519}
520
521// VerifyError
522
Ian Rogersef7d42f2014-01-06 12:55:46 -0800523void ThrowVerifyError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800524 va_list args;
525 va_start(args, fmt);
526 ThrowException(NULL, "Ljava/lang/VerifyError;", referrer, fmt, &args);
527 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700528}
529
530} // namespace art