blob: 0419dab0f883bf411160a9be50f18c8319094e35 [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 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
Sebastien Hertz56adf602013-07-09 17:27:07 +020069// AbstractMethodError
70
Brian Carlstromea46f952013-07-30 01:26:50 -070071void ThrowAbstractMethodError(const 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
92void ThrowArrayStoreException(const mirror::Class* element_class,
93 const mirror::Class* array_class) {
94 ThrowException(NULL, "Ljava/lang/ArrayStoreException;", NULL,
95 StringPrintf("%s cannot be stored in an array of type %s",
96 PrettyDescriptor(element_class).c_str(),
97 PrettyDescriptor(array_class).c_str()).c_str());
98}
99
100// ClassCastException
101
102void ThrowClassCastException(const mirror::Class* dest_type, const mirror::Class* src_type) {
103 ThrowException(NULL, "Ljava/lang/ClassCastException;", NULL,
104 StringPrintf("%s cannot be cast to %s",
105 PrettyDescriptor(src_type).c_str(),
106 PrettyDescriptor(dest_type).c_str()).c_str());
107}
108
109void ThrowClassCastException(const ThrowLocation* throw_location, const char* msg) {
110 ThrowException(throw_location, "Ljava/lang/ClassCastException;", NULL, msg);
111}
112
113// ClassCircularityError
114
115void ThrowClassCircularityError(mirror::Class* c) {
116 std::ostringstream msg;
117 msg << PrettyDescriptor(c);
118 ThrowException(NULL, "Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
119}
120
121// ClassFormatError
122
123void ThrowClassFormatError(const mirror::Class* referrer, const char* fmt, ...) {
124 va_list args;
125 va_start(args, fmt);
126 ThrowException(NULL, "Ljava/lang/ClassFormatError;", referrer, fmt, &args);
127 va_end(args);}
128
Ian Rogers87e552d2012-08-31 15:54:48 -0700129// IllegalAccessError
130
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700132 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700134 << PrettyDescriptor(accessed) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800135 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700136}
137
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
Brian Carlstromea46f952013-07-30 01:26:50 -0700139 const mirror::ArtMethod* caller,
140 const mirror::ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700141 InvokeType type) {
142 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
144 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700145 << " method " << PrettyMethod(called).c_str();
Ian Rogers62d6c772013-02-27 08:32:07 -0800146 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700147}
148
Brian Carlstromea46f952013-07-30 01:26:50 -0700149void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700150 std::ostringstream msg;
151 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
152 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800153 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700154}
155
Brian Carlstromea46f952013-07-30 01:26:50 -0700156void ThrowIllegalAccessErrorField(mirror::Class* referrer, mirror::ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700157 std::ostringstream msg;
158 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
159 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800160 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700161}
162
Brian Carlstromea46f952013-07-30 01:26:50 -0700163void ThrowIllegalAccessErrorFinalField(const mirror::ArtMethod* referrer,
164 mirror::ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700165 std::ostringstream msg;
166 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
167 << PrettyMethod(referrer) << "'";
Brian Carlstromea46f952013-07-30 01:26:50 -0700168 ThrowException(NULL, "Ljava/lang/IllegalAccessError;",
169 referrer != NULL ? referrer->GetClass() : NULL,
Ian Rogers62d6c772013-02-27 08:32:07 -0800170 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700171}
172
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700173void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 va_list args;
175 va_start(args, fmt);
176 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
177 va_end(args);
178}
179
180// IllegalArgumentException
181
182void ThrowIllegalArgumentException(const ThrowLocation* throw_location, const char* msg) {
183 ThrowException(throw_location, "Ljava/lang/IllegalArgumentException;", NULL, msg);
184}
185
186
Ian Rogers87e552d2012-08-31 15:54:48 -0700187// IncompatibleClassChangeError
188
189void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700190 mirror::ArtMethod* method,
191 const mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700192 std::ostringstream msg;
193 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
194 << expected_type << " but instead was found to be of type " << found_type;
Ian Rogers62d6c772013-02-27 08:32:07 -0800195 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
196 referrer != NULL ? referrer->GetClass() : NULL,
197 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700198}
199
Brian Carlstromea46f952013-07-30 01:26:50 -0700200void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const mirror::ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700202 const mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700203 // Referrer is calling interface_method on this_object, however, the interface_method isn't
204 // implemented by this_object.
205 CHECK(this_object != NULL);
206 std::ostringstream msg;
207 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
208 << "' does not implement interface '"
209 << PrettyDescriptor(interface_method->GetDeclaringClass())
210 << "' in call to '" << PrettyMethod(interface_method) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
212 referrer != NULL ? referrer->GetClass() : NULL,
213 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700214}
215
Brian Carlstromea46f952013-07-30 01:26:50 -0700216void ThrowIncompatibleClassChangeErrorField(const mirror::ArtField* resolved_field, bool is_static,
217 const mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700218 std::ostringstream msg;
219 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700220 << (is_static ? "static" : "instance") << " field" << " rather than a "
221 << (is_static ? "instance" : "static") << " field";
Ian Rogers62d6c772013-02-27 08:32:07 -0800222 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer->GetClass(),
223 msg.str().c_str());
224}
225
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700226void ThrowIncompatibleClassChangeError(const mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800227 va_list args;
228 va_start(args, fmt);
229 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
230 va_end(args);
231}
232
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700233// IOException
234
235void ThrowIOException(const char* fmt, ...) {
236 va_list args;
237 va_start(args, fmt);
238 ThrowException(NULL, "Ljava/io/IOException;", NULL, fmt, &args);
239 va_end(args);
240}
241
Ian Rogers62d6c772013-02-27 08:32:07 -0800242// LinkageError
243
244void ThrowLinkageError(const mirror::Class* referrer, const char* fmt, ...) {
245 va_list args;
246 va_start(args, fmt);
247 ThrowException(NULL, "Ljava/lang/LinkageError;", referrer, fmt, &args);
248 va_end(args);
249}
250
251// NegativeArraySizeException
252
253void ThrowNegativeArraySizeException(int size) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700254 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL,
255 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800256}
257
258void ThrowNegativeArraySizeException(const char* msg) {
259 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, msg);
260}
261
262// NoSuchFieldError
263
264void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
265 const StringPiece& type, const StringPiece& name)
266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
267 ClassHelper kh(c);
268 std::ostringstream msg;
269 msg << "No " << scope << "field " << name << " of type " << type
270 << " in class " << kh.GetDescriptor() << " or its superclasses";
271 ThrowException(NULL, "Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700272}
273
274// NoSuchMethodError
275
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700277 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700278 std::ostringstream msg;
279 ClassHelper kh(c);
280 msg << "No " << type << " method " << name << signature
281 << " in class " << kh.GetDescriptor() << " or its super classes";
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 ThrowException(NULL, "Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700283}
284
Ian Rogers62d6c772013-02-27 08:32:07 -0800285void ThrowNoSuchMethodError(uint32_t method_idx) {
286 Thread* self = Thread::Current();
287 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
288 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700289 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700290 std::ostringstream msg;
291 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 ThrowException(&throw_location, "Ljava/lang/NoSuchMethodError;",
293 throw_location.GetMethod()->GetDeclaringClass(), msg.str().c_str());
294}
295
296// NullPointerException
297
298void ThrowNullPointerExceptionForFieldAccess(const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700299 mirror::ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 std::ostringstream msg;
301 msg << "Attempt to " << (is_read ? "read from" : "write to")
302 << " field '" << PrettyField(field, true) << "' on a null object reference";
303 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
304}
305
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200306static void ThrowNullPointerExceptionForMethodAccessImpl(const ThrowLocation& throw_location,
307 uint32_t method_idx,
308 const DexFile& dex_file,
309 InvokeType type)
310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 std::ostringstream msg;
312 msg << "Attempt to invoke " << type << " method '"
313 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
314 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
315}
316
Brian Carlstromea46f952013-07-30 01:26:50 -0700317void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location,
318 uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200319 InvokeType type) {
320 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
321 const DexFile& dex_file = *dex_cache->GetDexFile();
322 ThrowNullPointerExceptionForMethodAccessImpl(throw_location, method_idx,
323 dex_file, type);
324}
325
326void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700327 mirror::ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200328 InvokeType type) {
329 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
330 const DexFile& dex_file = *dex_cache->GetDexFile();
331 ThrowNullPointerExceptionForMethodAccessImpl(throw_location, method->GetDexMethodIndex(),
332 dex_file, type);
333}
334
Ian Rogers62d6c772013-02-27 08:32:07 -0800335void ThrowNullPointerExceptionFromDexPC(const ThrowLocation& throw_location) {
336 const DexFile::CodeItem* code = MethodHelper(throw_location.GetMethod()).GetCodeItem();
337 uint32_t throw_dex_pc = throw_location.GetDexPc();
338 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
339 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 switch (instr->Opcode()) {
341 case Instruction::INVOKE_DIRECT:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200342 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kDirect);
343 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 case Instruction::INVOKE_DIRECT_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200345 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 break;
347 case Instruction::INVOKE_VIRTUAL:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200348 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kVirtual);
349 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 case Instruction::INVOKE_VIRTUAL_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200351 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 break;
353 case Instruction::INVOKE_INTERFACE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200354 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kInterface);
355 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 case Instruction::INVOKE_INTERFACE_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200357 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800358 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200359 case Instruction::INVOKE_VIRTUAL_QUICK:
360 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
361 // Since we replaced the method index, we ask the verifier to tell us which
362 // method is invoked at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700363 mirror::ArtMethod* method =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200364 verifier::MethodVerifier::FindInvokedMethodAtDexPc(throw_location.GetMethod(),
365 throw_location.GetDexPc());
366 if (method != NULL) {
367 // NPE with precise message.
368 ThrowNullPointerExceptionForMethodAccess(throw_location, method, kVirtual);
369 } else {
370 // NPE with imprecise message.
371 ThrowNullPointerException(&throw_location,
372 "Attempt to invoke a virtual method on a null object reference");
373 }
374 break;
375 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 case Instruction::IGET:
377 case Instruction::IGET_WIDE:
378 case Instruction::IGET_OBJECT:
379 case Instruction::IGET_BOOLEAN:
380 case Instruction::IGET_BYTE:
381 case Instruction::IGET_CHAR:
382 case Instruction::IGET_SHORT: {
Brian Carlstromea46f952013-07-30 01:26:50 -0700383 mirror::ArtField* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200384 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 throw_location.GetMethod(), false);
386 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
387 break;
388 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200389 case Instruction::IGET_QUICK:
390 case Instruction::IGET_WIDE_QUICK:
391 case Instruction::IGET_OBJECT_QUICK: {
392 // Since we replaced the field index, we ask the verifier to tell us which
393 // field is accessed at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700394 mirror::ArtField* field =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200395 verifier::MethodVerifier::FindAccessedFieldAtDexPc(throw_location.GetMethod(),
396 throw_location.GetDexPc());
397 if (field != NULL) {
398 // NPE with precise message.
399 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
400 } else {
401 // NPE with imprecise message.
402 ThrowNullPointerException(&throw_location,
403 "Attempt to read from a field on a null object reference");
404 }
405 break;
406 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 case Instruction::IPUT:
408 case Instruction::IPUT_WIDE:
409 case Instruction::IPUT_OBJECT:
410 case Instruction::IPUT_BOOLEAN:
411 case Instruction::IPUT_BYTE:
412 case Instruction::IPUT_CHAR:
413 case Instruction::IPUT_SHORT: {
Brian Carlstromea46f952013-07-30 01:26:50 -0700414 mirror::ArtField* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200415 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 throw_location.GetMethod(), false);
417 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
418 break;
419 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200420 case Instruction::IPUT_QUICK:
421 case Instruction::IPUT_WIDE_QUICK:
422 case Instruction::IPUT_OBJECT_QUICK: {
423 // Since we replaced the field index, we ask the verifier to tell us which
424 // field is accessed at this location.
Brian Carlstromea46f952013-07-30 01:26:50 -0700425 mirror::ArtField* field =
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200426 verifier::MethodVerifier::FindAccessedFieldAtDexPc(throw_location.GetMethod(),
427 throw_location.GetDexPc());
428 if (field != NULL) {
429 // NPE with precise message.
430 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
431 } else {
432 // NPE with imprecise message.
433 ThrowNullPointerException(&throw_location,
434 "Attempt to write to a field on a null object reference");
435 }
436 break;
437 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800438 case Instruction::AGET:
439 case Instruction::AGET_WIDE:
440 case Instruction::AGET_OBJECT:
441 case Instruction::AGET_BOOLEAN:
442 case Instruction::AGET_BYTE:
443 case Instruction::AGET_CHAR:
444 case Instruction::AGET_SHORT:
445 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
446 "Attempt to read from null array");
447 break;
448 case Instruction::APUT:
449 case Instruction::APUT_WIDE:
450 case Instruction::APUT_OBJECT:
451 case Instruction::APUT_BOOLEAN:
452 case Instruction::APUT_BYTE:
453 case Instruction::APUT_CHAR:
454 case Instruction::APUT_SHORT:
455 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
456 "Attempt to write to null array");
457 break;
458 case Instruction::ARRAY_LENGTH:
459 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
460 "Attempt to get length of null array");
461 break;
462 default: {
463 // TODO: We should have covered all the cases where we expect a NPE above, this
464 // message/logging is so we can improve any cases we've missed in the future.
465 const DexFile& dex_file =
466 *throw_location.GetMethod()->GetDeclaringClass()->GetDexCache()->GetDexFile();
467 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
468 StringPrintf("Null pointer exception during instruction '%s'",
469 instr->DumpString(&dex_file).c_str()).c_str());
470 break;
471 }
472 }
473}
474
475void ThrowNullPointerException(const ThrowLocation* throw_location, const char* msg) {
476 ThrowException(throw_location, "Ljava/lang/NullPointerException;", NULL, msg);
477}
478
479// RuntimeException
480
481void ThrowRuntimeException(const char* fmt, ...) {
482 va_list args;
483 va_start(args, fmt);
484 ThrowException(NULL, "Ljava/lang/RuntimeException;", NULL, fmt, &args);
485 va_end(args);
486}
487
488// VerifyError
489
490void ThrowVerifyError(const mirror::Class* referrer, const char* fmt, ...) {
491 va_list args;
492 va_start(args, fmt);
493 ThrowException(NULL, "Ljava/lang/VerifyError;", referrer, fmt, &args);
494 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700495}
496
497} // namespace art