blob: 1e77753e87536bba1c4f3964d95a4db840ad2e14 [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
Ian Rogers22d5e732014-07-15 22:23:51 -070019#include <sstream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
Andreas Gampe103992b2016-01-04 15:32:43 -080022
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020028#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070029#include "invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010031#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/object-inl.h"
33#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070034#include "nativehelper/scoped_local_ref.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070035#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070036#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020037#include "verifier/method_verifier.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070038#include "well_known_classes.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070039
Ian Rogers87e552d2012-08-31 15:54:48 -070040namespace art {
41
Andreas Gampe46ee31b2016-12-14 10:11:49 -080042using android::base::StringAppendV;
43using android::base::StringPrintf;
44
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070045static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070046 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070048 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070049 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070050 os << " (declaration of '" << referrer->PrettyDescriptor()
51 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070052 }
53 }
54}
55
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000056static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070057 ObjPtr<mirror::Class> referrer,
58 const char* fmt,
59 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070061 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070062 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080063 std::string vmsg;
64 StringAppendV(&vmsg, fmt, *args);
65 msg << vmsg;
66 } else {
67 msg << fmt;
68 }
69 AddReferrerLocation(msg, referrer);
70 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000071 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070072}
73
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000074static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070075 ObjPtr<mirror::Class> referrer,
76 const char* fmt,
77 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070079 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070080 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070081 std::string vmsg;
82 StringAppendV(&vmsg, fmt, *args);
83 msg << vmsg;
84 } else {
85 msg << fmt;
86 }
87 AddReferrerLocation(msg, referrer);
88 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000089 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070090}
91
Sebastien Hertz56adf602013-07-09 17:27:07 +020092// AbstractMethodError
93
Mathieu Chartiere401d142015-04-22 13:56:20 -070094void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070095 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020096 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -070097 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +020098}
99
Alex Light705ad492015-09-21 11:36:30 -0700100void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
101 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
102 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700103 dex_file.PrettyMethod(method_idx,
104 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700105}
106
Ian Rogers62d6c772013-02-27 08:32:07 -0800107// ArithmeticException
108
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200109void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700110 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800111}
112
113// ArrayIndexOutOfBoundsException
114
115void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800117 StringPrintf("length=%d; index=%d", length, index).c_str());
118}
119
120// ArrayStoreException
121
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700122void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
123 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700124 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700126 mirror::Class::PrettyDescriptor(element_class).c_str(),
127 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800128}
129
Orion Hodsonc069a302017-01-18 09:23:12 +0000130// BootstrapMethodError
131
132void ThrowBootstrapMethodError(const char* fmt, ...) {
133 va_list args;
134 va_start(args, fmt);
135 ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
136 va_end(args);
137}
138
139void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
140 va_list args;
141 va_start(args, fmt);
142 ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
143 va_end(args);
144}
145
Ian Rogers62d6c772013-02-27 08:32:07 -0800146// ClassCastException
147
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700148void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700149 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800150 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700151 mirror::Class::PrettyDescriptor(src_type).c_str(),
152 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800153}
154
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000155void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800157}
158
159// ClassCircularityError
160
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700161void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800162 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700163 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000164 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800165}
166
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700167void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100168 va_list args;
169 va_start(args, fmt);
170 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
171 va_end(args);
172}
173
Ian Rogers62d6c772013-02-27 08:32:07 -0800174// ClassFormatError
175
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700176void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 va_list args;
178 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000179 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100180 va_end(args);
181}
Ian Rogers62d6c772013-02-27 08:32:07 -0800182
Ian Rogers87e552d2012-08-31 15:54:48 -0700183// IllegalAccessError
184
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700185void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700186 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700187 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
188 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000189 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700190}
191
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700192void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
193 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700195 InvokeType type) {
196 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700197 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
198 << "' attempting to access '"
199 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
200 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000201 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700202}
203
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700204void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700205 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700206 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
207 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000208 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700209}
210
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700211void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700212 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700213 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
214 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000215 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700216}
217
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700219 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700220 msg << "Final field '" << ArtField::PrettyField(accessed, false)
221 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000222 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800224 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700225}
226
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700227void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 va_list args;
229 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000230 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 va_end(args);
232}
233
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700234// IllegalAccessException
235
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000236void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700238}
239
Ian Rogers62d6c772013-02-27 08:32:07 -0800240// IllegalArgumentException
241
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000242void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700243 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800244}
245
246
Ian Rogers87e552d2012-08-31 15:54:48 -0700247// IncompatibleClassChangeError
248
249void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700251 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700252 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700253 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000254 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800256 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700257}
258
Alex Light705ad492015-09-21 11:36:30 -0700259void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700260 ObjPtr<mirror::Class> target_class,
261 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700262 ArtMethod* referrer) {
263 // Referrer is calling interface_method on this_object, however, the interface_method isn't
264 // implemented by this_object.
265 CHECK(this_object != nullptr);
266 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700267 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
268 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
269 << "' in call to '"
270 << ArtMethod::PrettyMethod(method) << "'";
Alex Light705ad492015-09-21 11:36:30 -0700271 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
272 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
273 msg.str().c_str());
274}
275
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700277 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700279 // Referrer is calling interface_method on this_object, however, the interface_method isn't
280 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700281 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700282 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700283 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700284 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700285 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
286 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000287 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700290}
291
Mathieu Chartierc7853442015-03-27 14:35:38 -0700292void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700294 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700295 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 << (is_static ? "static" : "instance") << " field" << " rather than a "
297 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 msg.str().c_str());
300}
301
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700302void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 va_list args;
304 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000305 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 va_end(args);
307}
308
Alex Light9139e002015-10-09 15:59:48 -0700309void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
310 DCHECK(method != nullptr);
311 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
312 /*referrer*/nullptr,
313 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700314 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700315}
316
Alex Lightdb01a092017-04-03 15:39:55 -0700317// InternalError
318
319void ThrowInternalError(const char* fmt, ...) {
320 va_list args;
321 va_start(args, fmt);
322 ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
323 va_end(args);
324}
Alex Light9139e002015-10-09 15:59:48 -0700325
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700326// IOException
327
328void ThrowIOException(const char* fmt, ...) {
329 va_list args;
330 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700331 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332 va_end(args);
333}
334
Andreas Gampe329d1882014-04-08 10:32:19 -0700335void ThrowWrappedIOException(const char* fmt, ...) {
336 va_list args;
337 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700338 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700339 va_end(args);
340}
341
Ian Rogers62d6c772013-02-27 08:32:07 -0800342// LinkageError
343
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700344void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800345 va_list args;
346 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000347 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 va_end(args);
349}
350
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700351void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100352 va_list args;
353 va_start(args, fmt);
354 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
355 va_end(args);
356}
357
Ian Rogers62d6c772013-02-27 08:32:07 -0800358// NegativeArraySizeException
359
360void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700361 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700362 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800363}
364
365void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700366 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800367}
368
369// NoSuchFieldError
370
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700371void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700372 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700374 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700376 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000377 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700378}
379
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700380void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700381 std::ostringstream msg;
382 std::string temp;
383 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
384 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
385}
386
Ian Rogers87e552d2012-08-31 15:54:48 -0700387// NoSuchMethodError
388
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700389void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700390 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700391 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700392 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700393 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700394 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000395 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700396}
397
Ian Rogers62d6c772013-02-27 08:32:07 -0800398// NullPointerException
399
Mathieu Chartierc7853442015-03-27 14:35:38 -0700400void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800401 std::ostringstream msg;
402 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700403 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700404 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800405}
406
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000407static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200408 const DexFile& dex_file,
409 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700410 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 std::ostringstream msg;
412 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700413 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700414 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800415}
416
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000417void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200418 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700419 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000420 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200421 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000422 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200423}
424
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200426 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700427 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200428 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000429 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200430 dex_file, type);
431}
432
Vladimir Marko953437b2016-08-24 08:30:46 +0000433static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
434 DCHECK(kEmitCompilerReadBarrier);
435 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
436 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
437 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
438 monitor_offset += gray_byte_position;
439 }
440 return addr == monitor_offset;
441}
442
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100443static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700444 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100445 if (!CanDoImplicitNullCheckOn(addr)) {
446 return false;
447 }
448
449 switch (instr.Opcode()) {
450 case Instruction::INVOKE_DIRECT:
451 case Instruction::INVOKE_DIRECT_RANGE:
452 case Instruction::INVOKE_VIRTUAL:
453 case Instruction::INVOKE_VIRTUAL_RANGE:
454 case Instruction::INVOKE_INTERFACE:
455 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000456 case Instruction::INVOKE_POLYMORPHIC:
457 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100458 case Instruction::INVOKE_VIRTUAL_QUICK:
459 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
460 // Without inlining, we could just check that the offset is the class offset.
461 // However, when inlining, the compiler can (validly) merge the null check with a field access
462 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
463 // which is the caller.
464 return true;
465 }
466
Vladimir Marko953437b2016-08-24 08:30:46 +0000467 case Instruction::IGET_OBJECT:
468 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
469 return true;
470 }
471 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100472 case Instruction::IGET:
473 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100474 case Instruction::IGET_BOOLEAN:
475 case Instruction::IGET_BYTE:
476 case Instruction::IGET_CHAR:
477 case Instruction::IGET_SHORT:
478 case Instruction::IPUT:
479 case Instruction::IPUT_WIDE:
480 case Instruction::IPUT_OBJECT:
481 case Instruction::IPUT_BOOLEAN:
482 case Instruction::IPUT_BYTE:
483 case Instruction::IPUT_CHAR:
484 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100485 ArtField* field =
486 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000487 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100488 }
489
Vladimir Marko953437b2016-08-24 08:30:46 +0000490 case Instruction::IGET_OBJECT_QUICK:
491 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
492 return true;
493 }
494 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100495 case Instruction::IGET_QUICK:
496 case Instruction::IGET_BOOLEAN_QUICK:
497 case Instruction::IGET_BYTE_QUICK:
498 case Instruction::IGET_CHAR_QUICK:
499 case Instruction::IGET_SHORT_QUICK:
500 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100501 case Instruction::IPUT_QUICK:
502 case Instruction::IPUT_BOOLEAN_QUICK:
503 case Instruction::IPUT_BYTE_QUICK:
504 case Instruction::IPUT_CHAR_QUICK:
505 case Instruction::IPUT_SHORT_QUICK:
506 case Instruction::IPUT_WIDE_QUICK:
507 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000508 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100509 }
510
Vladimir Marko953437b2016-08-24 08:30:46 +0000511 case Instruction::AGET_OBJECT:
512 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
513 return true;
514 }
515 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100516 case Instruction::AGET:
517 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100518 case Instruction::AGET_BOOLEAN:
519 case Instruction::AGET_BYTE:
520 case Instruction::AGET_CHAR:
521 case Instruction::AGET_SHORT:
522 case Instruction::APUT:
523 case Instruction::APUT_WIDE:
524 case Instruction::APUT_OBJECT:
525 case Instruction::APUT_BOOLEAN:
526 case Instruction::APUT_BYTE:
527 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100528 case Instruction::APUT_SHORT:
529 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100530 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100531 // The length access should crash. We currently do not do implicit checks on
532 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000533 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100534 }
535
536 default: {
537 // We have covered all the cases where an NPE could occur.
538 // Note that this must be kept in sync with the compiler, and adding
539 // any new way to do implicit checks in the compiler should also update
540 // this code.
541 return false;
542 }
543 }
544}
545
546void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000547 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700548 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000549 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800550 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
551 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100552 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
553 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
554 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
555 << "0x" << std::hex << addr << std::dec
556 << ", at "
557 << instr->DumpString(dex_file)
558 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700559 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100560 }
561
Ian Rogers62d6c772013-02-27 08:32:07 -0800562 switch (instr->Opcode()) {
563 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000564 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200565 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800566 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000567 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800568 break;
569 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000570 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200571 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800572 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000573 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800574 break;
575 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000576 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200577 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800578 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000579 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800580 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000581 case Instruction::INVOKE_POLYMORPHIC:
582 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_45cc(), kVirtual);
583 break;
584 case Instruction::INVOKE_POLYMORPHIC_RANGE:
585 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_4rcc(), kVirtual);
586 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200587 case Instruction::INVOKE_VIRTUAL_QUICK:
588 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
589 // Since we replaced the method index, we ask the verifier to tell us which
590 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700591 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000592 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700593 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200594 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000595 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200596 } else {
597 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000598 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200599 }
600 break;
601 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800602 case Instruction::IGET:
603 case Instruction::IGET_WIDE:
604 case Instruction::IGET_OBJECT:
605 case Instruction::IGET_BOOLEAN:
606 case Instruction::IGET_BYTE:
607 case Instruction::IGET_CHAR:
608 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700609 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000610 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
611 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800612 break;
613 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200614 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800615 case Instruction::IGET_BOOLEAN_QUICK:
616 case Instruction::IGET_BYTE_QUICK:
617 case Instruction::IGET_CHAR_QUICK:
618 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200619 case Instruction::IGET_WIDE_QUICK:
620 case Instruction::IGET_OBJECT_QUICK: {
621 // Since we replaced the field index, we ask the verifier to tell us which
622 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700623 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000624 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700625 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200626 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000627 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200628 } else {
629 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000630 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200631 }
632 break;
633 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 case Instruction::IPUT:
635 case Instruction::IPUT_WIDE:
636 case Instruction::IPUT_OBJECT:
637 case Instruction::IPUT_BOOLEAN:
638 case Instruction::IPUT_BYTE:
639 case Instruction::IPUT_CHAR:
640 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700641 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000642 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
643 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800644 break;
645 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200646 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700647 case Instruction::IPUT_BOOLEAN_QUICK:
648 case Instruction::IPUT_BYTE_QUICK:
649 case Instruction::IPUT_CHAR_QUICK:
650 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200651 case Instruction::IPUT_WIDE_QUICK:
652 case Instruction::IPUT_OBJECT_QUICK: {
653 // Since we replaced the field index, we ask the verifier to tell us which
654 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700655 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000656 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700657 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200658 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000659 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200660 } else {
661 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000662 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200663 }
664 break;
665 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800666 case Instruction::AGET:
667 case Instruction::AGET_WIDE:
668 case Instruction::AGET_OBJECT:
669 case Instruction::AGET_BOOLEAN:
670 case Instruction::AGET_BYTE:
671 case Instruction::AGET_CHAR:
672 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700673 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800674 "Attempt to read from null array");
675 break;
676 case Instruction::APUT:
677 case Instruction::APUT_WIDE:
678 case Instruction::APUT_OBJECT:
679 case Instruction::APUT_BOOLEAN:
680 case Instruction::APUT_BYTE:
681 case Instruction::APUT_CHAR:
682 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700683 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800684 "Attempt to write to null array");
685 break;
686 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700687 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 "Attempt to get length of null array");
689 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100690 case Instruction::FILL_ARRAY_DATA: {
691 ThrowException("Ljava/lang/NullPointerException;", nullptr,
692 "Attempt to write to null array");
693 break;
694 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100695 case Instruction::MONITOR_ENTER:
696 case Instruction::MONITOR_EXIT: {
697 ThrowException("Ljava/lang/NullPointerException;", nullptr,
698 "Attempt to do a synchronize operation on a null object");
699 break;
700 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800701 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000702 const DexFile* dex_file =
703 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100704 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
705 << instr->DumpString(dex_file)
706 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700707 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800708 break;
709 }
710 }
711}
712
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000713void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700714 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800715}
716
717// RuntimeException
718
719void ThrowRuntimeException(const char* fmt, ...) {
720 va_list args;
721 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700722 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800723 va_end(args);
724}
725
Leonard Mosescueb842212016-10-06 17:26:36 -0700726// SecurityException
727
728void ThrowSecurityException(const char* fmt, ...) {
729 va_list args;
730 va_start(args, fmt);
731 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
732 va_end(args);
733}
734
Andreas Gampe103992b2016-01-04 15:32:43 -0800735// Stack overflow.
736
737void ThrowStackOverflowError(Thread* self) {
738 if (self->IsHandlingStackOverflow()) {
739 LOG(ERROR) << "Recursive stack overflow.";
740 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
741 }
742
743 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
744 JNIEnvExt* env = self->GetJniEnv();
745 std::string msg("stack size ");
746 msg += PrettySize(self->GetStackSize());
747
748 // Avoid running Java code for exception initialization.
749 // TODO: Checks to make this a bit less brittle.
750
751 std::string error_msg;
752
753 // Allocate an uninitialized object.
754 ScopedLocalRef<jobject> exc(env,
755 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
756 if (exc.get() != nullptr) {
757 // "Initialize".
758 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
759 // Only Throwable has "custom" fields:
760 // String detailMessage.
761 // Throwable cause (= this).
762 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
763 // Object stackState;
764 // StackTraceElement[] stackTrace;
765 // Only Throwable has a non-empty constructor:
766 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
767 // fillInStackTrace();
768
769 // detailMessage.
770 // TODO: Use String::FromModifiedUTF...?
771 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
772 if (s.get() != nullptr) {
773 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
774
775 // cause.
776 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
777
778 // suppressedExceptions.
779 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
780 WellKnownClasses::java_util_Collections,
781 WellKnownClasses::java_util_Collections_EMPTY_LIST));
782 CHECK(emptylist.get() != nullptr);
783 env->SetObjectField(exc.get(),
784 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
785 emptylist.get());
786
787 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
788 // nativeFillInStackTrace.
789 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
790 {
791 ScopedObjectAccessUnchecked soa(env);
792 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
793 }
794 if (stack_state_val.get() != nullptr) {
795 env->SetObjectField(exc.get(),
796 WellKnownClasses::java_lang_Throwable_stackState,
797 stack_state_val.get());
798
799 // stackTrace.
800 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
801 WellKnownClasses::libcore_util_EmptyArray,
802 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
803 env->SetObjectField(exc.get(),
804 WellKnownClasses::java_lang_Throwable_stackTrace,
805 stack_trace_elem.get());
806 } else {
807 error_msg = "Could not create stack trace.";
808 }
809 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700810 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800811 } else {
812 // Could not allocate a string object.
813 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
814 }
815 } else {
816 error_msg = "Could not allocate StackOverflowError object.";
817 }
818
819 if (!error_msg.empty()) {
820 LOG(WARNING) << error_msg;
821 CHECK(self->IsExceptionPending());
822 }
823
824 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
825 self->ResetDefaultStackEnd(); // Return to default stack size.
826
827 // And restore protection if implicit checks are on.
828 if (!explicit_overflow_check) {
829 self->ProtectStack();
830 }
831}
832
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100833// StringIndexOutOfBoundsException
834
835void ThrowStringIndexOutOfBoundsException(int index, int length) {
836 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
837 StringPrintf("length=%d; index=%d", length, index).c_str());
838}
839
Ian Rogers62d6c772013-02-27 08:32:07 -0800840// VerifyError
841
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700842void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800843 va_list args;
844 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000845 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800846 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700847}
848
Narayan Kamath208f8572016-08-03 12:46:58 +0100849// WrongMethodTypeException
850
851void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
852 mirror::MethodType* callsite_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100853 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
854 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000855 StringPrintf("Expected %s but was %s",
856 callee_type->PrettyDescriptor().c_str(),
857 callsite_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100858}
859
Ian Rogers87e552d2012-08-31 15:54:48 -0700860} // namespace art