blob: f7ff15fbfd45ca8a940119e6873aa1c61d248214 [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 Gampe57943812017-12-06 21:39:13 -080021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
Andreas Gampe103992b2016-01-04 15:32:43 -080023
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "class_linker-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file-inl.h"
28#include "dex/dex_instruction-inl.h"
David Sehr8c0961f2018-01-23 16:11:38 -080029#include "dex/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
Orion Hodson928033d2018-02-07 05:30:54 +000056static void ThrowException(const char* exception_descriptor) REQUIRES_SHARED(Locks::mutator_lock_) {
57 Thread* self = Thread::Current();
58 self->ThrowNewException(exception_descriptor, nullptr);
59}
60
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000061static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070062 ObjPtr<mirror::Class> referrer,
63 const char* fmt,
64 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070066 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080068 std::string vmsg;
69 StringAppendV(&vmsg, fmt, *args);
70 msg << vmsg;
71 } else {
72 msg << fmt;
73 }
74 AddReferrerLocation(msg, referrer);
75 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000076 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070077}
78
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000079static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070080 ObjPtr<mirror::Class> referrer,
81 const char* fmt,
82 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070084 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070085 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070086 std::string vmsg;
87 StringAppendV(&vmsg, fmt, *args);
88 msg << vmsg;
89 } else {
90 msg << fmt;
91 }
92 AddReferrerLocation(msg, referrer);
93 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000094 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070095}
96
Sebastien Hertz56adf602013-07-09 17:27:07 +020097// AbstractMethodError
98
Mathieu Chartiere401d142015-04-22 13:56:20 -070099void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700100 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +0200101 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700102 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +0200103}
104
Alex Light705ad492015-09-21 11:36:30 -0700105void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
106 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
107 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700108 dex_file.PrettyMethod(method_idx,
109 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700110}
111
Ian Rogers62d6c772013-02-27 08:32:07 -0800112// ArithmeticException
113
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200114void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800116}
117
118// ArrayIndexOutOfBoundsException
119
120void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800122 StringPrintf("length=%d; index=%d", length, index).c_str());
123}
124
125// ArrayStoreException
126
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700127void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
128 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800130 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700131 mirror::Class::PrettyDescriptor(element_class).c_str(),
132 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800133}
134
Orion Hodsonc069a302017-01-18 09:23:12 +0000135// BootstrapMethodError
136
137void ThrowBootstrapMethodError(const char* fmt, ...) {
138 va_list args;
139 va_start(args, fmt);
140 ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
141 va_end(args);
142}
143
144void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
145 va_list args;
146 va_start(args, fmt);
147 ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
148 va_end(args);
149}
150
Ian Rogers62d6c772013-02-27 08:32:07 -0800151// ClassCastException
152
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700153void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700154 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700156 mirror::Class::PrettyDescriptor(src_type).c_str(),
157 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800158}
159
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000160void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700161 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800162}
163
164// ClassCircularityError
165
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700166void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800167 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700168 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000169 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800170}
171
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700172void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100173 va_list args;
174 va_start(args, fmt);
175 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
176 va_end(args);
177}
178
Ian Rogers62d6c772013-02-27 08:32:07 -0800179// ClassFormatError
180
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700181void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800182 va_list args;
183 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000184 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100185 va_end(args);
186}
Ian Rogers62d6c772013-02-27 08:32:07 -0800187
Ian Rogers87e552d2012-08-31 15:54:48 -0700188// IllegalAccessError
189
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700190void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700191 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700192 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
193 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000194 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700195}
196
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700197void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
198 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700200 InvokeType type) {
201 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700202 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
203 << "' attempting to access '"
204 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
205 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000206 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700207}
208
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700209void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700210 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700211 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
212 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000213 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700214}
215
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700216void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700217 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700218 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
219 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000220 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700221}
222
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700224 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700225 msg << "Final field '" << ArtField::PrettyField(accessed, false)
226 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000227 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800229 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700230}
231
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700232void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 va_list args;
234 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000235 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 va_end(args);
237}
238
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700239// IllegalAccessException
240
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000241void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700242 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700243}
244
Ian Rogers62d6c772013-02-27 08:32:07 -0800245// IllegalArgumentException
246
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000247void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700248 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800249}
250
Orion Hodson928033d2018-02-07 05:30:54 +0000251// IllegalStateException
252
253void ThrowIllegalStateException(const char* msg) {
254 ThrowException("Ljava/lang/IllegalStateException;", nullptr, msg);
255}
Ian Rogers62d6c772013-02-27 08:32:07 -0800256
Ian Rogers87e552d2012-08-31 15:54:48 -0700257// IncompatibleClassChangeError
258
259void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700261 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700262 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700263 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000264 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800266 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700267}
268
Alex Light705ad492015-09-21 11:36:30 -0700269void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700270 ObjPtr<mirror::Class> target_class,
271 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700272 ArtMethod* referrer) {
273 // Referrer is calling interface_method on this_object, however, the interface_method isn't
274 // implemented by this_object.
275 CHECK(this_object != nullptr);
276 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700277 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
278 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
279 << "' in call to '"
280 << ArtMethod::PrettyMethod(method) << "'";
Alex Light705ad492015-09-21 11:36:30 -0700281 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
282 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
283 msg.str().c_str());
284}
285
Mathieu Chartiere401d142015-04-22 13:56:20 -0700286void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700287 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700289 // Referrer is calling interface_method on this_object, however, the interface_method isn't
290 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700291 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700292 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700293 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700294 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700295 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
296 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000297 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700300}
301
Mathieu Chartierc7853442015-03-27 14:35:38 -0700302void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700304 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700305 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700306 << (is_static ? "static" : "instance") << " field" << " rather than a "
307 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 msg.str().c_str());
310}
311
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700312void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800313 va_list args;
314 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000315 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800316 va_end(args);
317}
318
Alex Light9139e002015-10-09 15:59:48 -0700319void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
320 DCHECK(method != nullptr);
321 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
322 /*referrer*/nullptr,
323 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700324 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700325}
326
Orion Hodson928033d2018-02-07 05:30:54 +0000327// IndexOutOfBoundsException
328
329void ThrowIndexOutOfBoundsException(int index, int length) {
330 ThrowException("Ljava/lang/IndexOutOfBoundsException;", nullptr,
331 StringPrintf("length=%d; index=%d", length, index).c_str());
332}
333
Alex Lightdb01a092017-04-03 15:39:55 -0700334// InternalError
335
336void ThrowInternalError(const char* fmt, ...) {
337 va_list args;
338 va_start(args, fmt);
339 ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
340 va_end(args);
341}
Alex Light9139e002015-10-09 15:59:48 -0700342
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700343// IOException
344
345void ThrowIOException(const char* fmt, ...) {
346 va_list args;
347 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700348 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700349 va_end(args);
350}
351
Andreas Gampe329d1882014-04-08 10:32:19 -0700352void ThrowWrappedIOException(const char* fmt, ...) {
353 va_list args;
354 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700355 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700356 va_end(args);
357}
358
Ian Rogers62d6c772013-02-27 08:32:07 -0800359// LinkageError
360
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700361void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 va_list args;
363 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000364 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 va_end(args);
366}
367
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700368void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100369 va_list args;
370 va_start(args, fmt);
371 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
372 va_end(args);
373}
374
Ian Rogers62d6c772013-02-27 08:32:07 -0800375// NegativeArraySizeException
376
377void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700378 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700379 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800380}
381
382void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700383 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800384}
385
386// NoSuchFieldError
387
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700388void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700389 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800390 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700391 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700393 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000394 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700395}
396
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700397void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700398 std::ostringstream msg;
399 std::string temp;
400 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
401 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
402}
403
Ian Rogers87e552d2012-08-31 15:54:48 -0700404// NoSuchMethodError
405
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700406void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700407 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700408 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700409 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700410 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700411 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000412 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700413}
414
Ian Rogers62d6c772013-02-27 08:32:07 -0800415// NullPointerException
416
Mathieu Chartierc7853442015-03-27 14:35:38 -0700417void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 std::ostringstream msg;
419 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700420 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700421 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800422}
423
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000424static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200425 const DexFile& dex_file,
426 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700427 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800428 std::ostringstream msg;
429 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700430 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700431 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800432}
433
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000434void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200435 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700436 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000437 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200438 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000439 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200440}
441
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200443 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700444 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200445 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000446 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200447 dex_file, type);
448}
449
Vladimir Marko953437b2016-08-24 08:30:46 +0000450static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
451 DCHECK(kEmitCompilerReadBarrier);
452 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
Vladimir Marko33bff252017-11-01 14:35:42 +0000453 if (kUseBakerReadBarrier &&
454 (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64)) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000455 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
456 monitor_offset += gray_byte_position;
457 }
458 return addr == monitor_offset;
459}
460
Nicolas Geoffray13449142017-12-07 22:26:24 +0000461static bool IsValidImplicitCheck(uintptr_t addr, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700462 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100463 if (!CanDoImplicitNullCheckOn(addr)) {
464 return false;
465 }
466
467 switch (instr.Opcode()) {
468 case Instruction::INVOKE_DIRECT:
469 case Instruction::INVOKE_DIRECT_RANGE:
470 case Instruction::INVOKE_VIRTUAL:
471 case Instruction::INVOKE_VIRTUAL_RANGE:
472 case Instruction::INVOKE_INTERFACE:
473 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000474 case Instruction::INVOKE_POLYMORPHIC:
475 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100476 case Instruction::INVOKE_VIRTUAL_QUICK:
477 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
478 // Without inlining, we could just check that the offset is the class offset.
479 // However, when inlining, the compiler can (validly) merge the null check with a field access
480 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
481 // which is the caller.
482 return true;
483 }
484
Vladimir Marko953437b2016-08-24 08:30:46 +0000485 case Instruction::IGET_OBJECT:
486 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
487 return true;
488 }
489 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100490 case Instruction::IGET:
491 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100492 case Instruction::IGET_BOOLEAN:
493 case Instruction::IGET_BYTE:
494 case Instruction::IGET_CHAR:
495 case Instruction::IGET_SHORT:
496 case Instruction::IPUT:
497 case Instruction::IPUT_WIDE:
498 case Instruction::IPUT_OBJECT:
499 case Instruction::IPUT_BOOLEAN:
500 case Instruction::IPUT_BYTE:
501 case Instruction::IPUT_CHAR:
502 case Instruction::IPUT_SHORT: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000503 // We might be doing an implicit null check with an offset that doesn't correspond
504 // to the instruction, for example with two field accesses and the first one being
505 // eliminated or re-ordered.
506 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100507 }
508
Vladimir Marko953437b2016-08-24 08:30:46 +0000509 case Instruction::IGET_OBJECT_QUICK:
510 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
511 return true;
512 }
513 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100514 case Instruction::IGET_QUICK:
515 case Instruction::IGET_BOOLEAN_QUICK:
516 case Instruction::IGET_BYTE_QUICK:
517 case Instruction::IGET_CHAR_QUICK:
518 case Instruction::IGET_SHORT_QUICK:
519 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100520 case Instruction::IPUT_QUICK:
521 case Instruction::IPUT_BOOLEAN_QUICK:
522 case Instruction::IPUT_BYTE_QUICK:
523 case Instruction::IPUT_CHAR_QUICK:
524 case Instruction::IPUT_SHORT_QUICK:
525 case Instruction::IPUT_WIDE_QUICK:
526 case Instruction::IPUT_OBJECT_QUICK: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000527 // We might be doing an implicit null check with an offset that doesn't correspond
528 // to the instruction, for example with two field accesses and the first one being
529 // eliminated or re-ordered.
530 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100531 }
532
Vladimir Marko953437b2016-08-24 08:30:46 +0000533 case Instruction::AGET_OBJECT:
534 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
535 return true;
536 }
537 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100538 case Instruction::AGET:
539 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100540 case Instruction::AGET_BOOLEAN:
541 case Instruction::AGET_BYTE:
542 case Instruction::AGET_CHAR:
543 case Instruction::AGET_SHORT:
544 case Instruction::APUT:
545 case Instruction::APUT_WIDE:
546 case Instruction::APUT_OBJECT:
547 case Instruction::APUT_BOOLEAN:
548 case Instruction::APUT_BYTE:
549 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100550 case Instruction::APUT_SHORT:
551 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100552 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100553 // The length access should crash. We currently do not do implicit checks on
554 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000555 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100556 }
557
558 default: {
559 // We have covered all the cases where an NPE could occur.
560 // Note that this must be kept in sync with the compiler, and adding
561 // any new way to do implicit checks in the compiler should also update
562 // this code.
563 return false;
564 }
565 }
566}
567
568void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000569 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700570 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
David Sehr0225f8e2018-01-31 08:52:24 +0000571 CodeItemInstructionAccessor accessor(method->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800572 CHECK_LT(throw_dex_pc, accessor.InsnsSizeInCodeUnits());
573 const Instruction& instr = accessor.InstructionAt(throw_dex_pc);
574 if (check_address && !IsValidImplicitCheck(addr, instr)) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100575 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
576 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
577 << "0x" << std::hex << addr << std::dec
578 << ", at "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800579 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100580 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700581 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100582 }
583
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800584 switch (instr.Opcode()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800585 case Instruction::INVOKE_DIRECT:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800586 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200587 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800588 case Instruction::INVOKE_DIRECT_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800589 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800590 break;
591 case Instruction::INVOKE_VIRTUAL:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800592 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200593 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800594 case Instruction::INVOKE_VIRTUAL_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800595 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800596 break;
597 case Instruction::INVOKE_INTERFACE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800598 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200599 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800600 case Instruction::INVOKE_INTERFACE_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800601 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800602 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000603 case Instruction::INVOKE_POLYMORPHIC:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800604 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_45cc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000605 break;
606 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800607 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_4rcc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000608 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200609 case Instruction::INVOKE_VIRTUAL_QUICK:
610 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
611 // Since we replaced the method index, we ask the verifier to tell us which
612 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700613 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000614 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700615 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200616 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000617 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200618 } else {
619 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000620 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200621 }
622 break;
623 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800624 case Instruction::IGET:
625 case Instruction::IGET_WIDE:
626 case Instruction::IGET_OBJECT:
627 case Instruction::IGET_BOOLEAN:
628 case Instruction::IGET_BYTE:
629 case Instruction::IGET_CHAR:
630 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700631 ArtField* field =
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800632 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000633 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 break;
635 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200636 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800637 case Instruction::IGET_BOOLEAN_QUICK:
638 case Instruction::IGET_BYTE_QUICK:
639 case Instruction::IGET_CHAR_QUICK:
640 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200641 case Instruction::IGET_WIDE_QUICK:
642 case Instruction::IGET_OBJECT_QUICK: {
643 // Since we replaced the field index, we ask the verifier to tell us which
644 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700645 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000646 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700647 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200648 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000649 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200650 } else {
651 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000652 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200653 }
654 break;
655 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 case Instruction::IPUT:
657 case Instruction::IPUT_WIDE:
658 case Instruction::IPUT_OBJECT:
659 case Instruction::IPUT_BOOLEAN:
660 case Instruction::IPUT_BYTE:
661 case Instruction::IPUT_CHAR:
662 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700663 ArtField* field =
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800664 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000665 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800666 break;
667 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200668 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700669 case Instruction::IPUT_BOOLEAN_QUICK:
670 case Instruction::IPUT_BYTE_QUICK:
671 case Instruction::IPUT_CHAR_QUICK:
672 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200673 case Instruction::IPUT_WIDE_QUICK:
674 case Instruction::IPUT_OBJECT_QUICK: {
675 // Since we replaced the field index, we ask the verifier to tell us which
676 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700677 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000678 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700679 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200680 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000681 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200682 } else {
683 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000684 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200685 }
686 break;
687 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 case Instruction::AGET:
689 case Instruction::AGET_WIDE:
690 case Instruction::AGET_OBJECT:
691 case Instruction::AGET_BOOLEAN:
692 case Instruction::AGET_BYTE:
693 case Instruction::AGET_CHAR:
694 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700695 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800696 "Attempt to read from null array");
697 break;
698 case Instruction::APUT:
699 case Instruction::APUT_WIDE:
700 case Instruction::APUT_OBJECT:
701 case Instruction::APUT_BOOLEAN:
702 case Instruction::APUT_BYTE:
703 case Instruction::APUT_CHAR:
704 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700705 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800706 "Attempt to write to null array");
707 break;
708 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700709 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800710 "Attempt to get length of null array");
711 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100712 case Instruction::FILL_ARRAY_DATA: {
713 ThrowException("Ljava/lang/NullPointerException;", nullptr,
714 "Attempt to write to null array");
715 break;
716 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100717 case Instruction::MONITOR_ENTER:
718 case Instruction::MONITOR_EXIT: {
719 ThrowException("Ljava/lang/NullPointerException;", nullptr,
720 "Attempt to do a synchronize operation on a null object");
721 break;
722 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800723 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000724 const DexFile* dex_file =
725 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100726 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800727 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100728 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700729 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800730 break;
731 }
732 }
733}
734
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000735void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700736 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800737}
738
Orion Hodson928033d2018-02-07 05:30:54 +0000739// ReadOnlyBufferException
740
741void ThrowReadOnlyBufferException() {
742 Thread::Current()->ThrowNewException("Ljava/nio/ReadOnlyBufferException;", nullptr);
743}
744
Ian Rogers62d6c772013-02-27 08:32:07 -0800745// RuntimeException
746
747void ThrowRuntimeException(const char* fmt, ...) {
748 va_list args;
749 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700750 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800751 va_end(args);
752}
753
Leonard Mosescueb842212016-10-06 17:26:36 -0700754// SecurityException
755
756void ThrowSecurityException(const char* fmt, ...) {
757 va_list args;
758 va_start(args, fmt);
759 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
760 va_end(args);
761}
762
Andreas Gampe103992b2016-01-04 15:32:43 -0800763// Stack overflow.
764
765void ThrowStackOverflowError(Thread* self) {
766 if (self->IsHandlingStackOverflow()) {
767 LOG(ERROR) << "Recursive stack overflow.";
768 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
769 }
770
771 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
772 JNIEnvExt* env = self->GetJniEnv();
773 std::string msg("stack size ");
774 msg += PrettySize(self->GetStackSize());
775
776 // Avoid running Java code for exception initialization.
777 // TODO: Checks to make this a bit less brittle.
778
779 std::string error_msg;
780
781 // Allocate an uninitialized object.
782 ScopedLocalRef<jobject> exc(env,
783 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
784 if (exc.get() != nullptr) {
785 // "Initialize".
786 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
787 // Only Throwable has "custom" fields:
788 // String detailMessage.
789 // Throwable cause (= this).
790 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
791 // Object stackState;
792 // StackTraceElement[] stackTrace;
793 // Only Throwable has a non-empty constructor:
794 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
795 // fillInStackTrace();
796
797 // detailMessage.
798 // TODO: Use String::FromModifiedUTF...?
799 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
800 if (s.get() != nullptr) {
801 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
802
803 // cause.
804 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
805
806 // suppressedExceptions.
807 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
808 WellKnownClasses::java_util_Collections,
809 WellKnownClasses::java_util_Collections_EMPTY_LIST));
810 CHECK(emptylist.get() != nullptr);
811 env->SetObjectField(exc.get(),
812 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
813 emptylist.get());
814
815 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
816 // nativeFillInStackTrace.
817 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
818 {
819 ScopedObjectAccessUnchecked soa(env);
820 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
821 }
822 if (stack_state_val.get() != nullptr) {
823 env->SetObjectField(exc.get(),
824 WellKnownClasses::java_lang_Throwable_stackState,
825 stack_state_val.get());
826
827 // stackTrace.
828 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
829 WellKnownClasses::libcore_util_EmptyArray,
830 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
831 env->SetObjectField(exc.get(),
832 WellKnownClasses::java_lang_Throwable_stackTrace,
833 stack_trace_elem.get());
834 } else {
835 error_msg = "Could not create stack trace.";
836 }
837 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700838 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800839 } else {
840 // Could not allocate a string object.
841 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
842 }
843 } else {
844 error_msg = "Could not allocate StackOverflowError object.";
845 }
846
847 if (!error_msg.empty()) {
848 LOG(WARNING) << error_msg;
849 CHECK(self->IsExceptionPending());
850 }
851
852 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
853 self->ResetDefaultStackEnd(); // Return to default stack size.
854
855 // And restore protection if implicit checks are on.
856 if (!explicit_overflow_check) {
857 self->ProtectStack();
858 }
859}
860
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100861// StringIndexOutOfBoundsException
862
863void ThrowStringIndexOutOfBoundsException(int index, int length) {
864 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
865 StringPrintf("length=%d; index=%d", length, index).c_str());
866}
867
Orion Hodson928033d2018-02-07 05:30:54 +0000868// UnsupportedOperationException
869
870void ThrowUnsupportedOperationException() {
871 ThrowException("Ljava/lang/UnsupportedOperationException;");
872}
873
Ian Rogers62d6c772013-02-27 08:32:07 -0800874// VerifyError
875
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700876void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800877 va_list args;
878 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000879 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800880 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700881}
882
Narayan Kamath208f8572016-08-03 12:46:58 +0100883// WrongMethodTypeException
884
Orion Hodson928033d2018-02-07 05:30:54 +0000885void ThrowWrongMethodTypeException(mirror::MethodType* expected_type,
886 mirror::MethodType* actual_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100887 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
888 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000889 StringPrintf("Expected %s but was %s",
Orion Hodson928033d2018-02-07 05:30:54 +0000890 expected_type->PrettyDescriptor().c_str(),
891 actual_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100892}
893
Ian Rogers87e552d2012-08-31 15:54:48 -0700894} // namespace art