blob: 499bf2845150f5ad41520d0baa4bfffb1a072f80 [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"
Vladimir Markof5c537e2018-04-09 18:33:55 +010027#include "debug_print.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file-inl.h"
29#include "dex/dex_instruction-inl.h"
David Sehr8c0961f2018-01-23 16:11:38 -080030#include "dex/invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010032#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070035#include "nativehelper/scoped_local_ref.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070036#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070037#include "thread.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000038#include "vdex_file.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020039#include "verifier/method_verifier.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070040#include "well_known_classes.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070041
Ian Rogers87e552d2012-08-31 15:54:48 -070042namespace art {
43
Andreas Gampe46ee31b2016-12-14 10:11:49 -080044using android::base::StringAppendV;
45using android::base::StringPrintf;
46
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070047static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070048 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070050 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070051 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070052 os << " (declaration of '" << referrer->PrettyDescriptor()
53 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070054 }
55 }
56}
57
Orion Hodson928033d2018-02-07 05:30:54 +000058static void ThrowException(const char* exception_descriptor) REQUIRES_SHARED(Locks::mutator_lock_) {
59 Thread* self = Thread::Current();
60 self->ThrowNewException(exception_descriptor, nullptr);
61}
62
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000063static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070064 ObjPtr<mirror::Class> referrer,
65 const char* fmt,
66 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070068 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080070 std::string vmsg;
71 StringAppendV(&vmsg, fmt, *args);
72 msg << vmsg;
73 } else {
74 msg << fmt;
75 }
76 AddReferrerLocation(msg, referrer);
77 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000078 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070079}
80
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000081static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070082 ObjPtr<mirror::Class> referrer,
83 const char* fmt,
84 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070086 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070087 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070088 std::string vmsg;
89 StringAppendV(&vmsg, fmt, *args);
90 msg << vmsg;
91 } else {
92 msg << fmt;
93 }
94 AddReferrerLocation(msg, referrer);
95 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000096 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070097}
98
Sebastien Hertz56adf602013-07-09 17:27:07 +020099// AbstractMethodError
100
Mathieu Chartiere401d142015-04-22 13:56:20 -0700101void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700102 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +0200103 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700104 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +0200105}
106
Alex Light705ad492015-09-21 11:36:30 -0700107void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700108 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer= */ nullptr,
Alex Light705ad492015-09-21 11:36:30 -0700109 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700110 dex_file.PrettyMethod(method_idx,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700111 /* with_signature= */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700112}
113
Ian Rogers62d6c772013-02-27 08:32:07 -0800114// ArithmeticException
115
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200116void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700117 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800118}
119
120// ArrayIndexOutOfBoundsException
121
122void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700123 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 StringPrintf("length=%d; index=%d", length, index).c_str());
125}
126
127// ArrayStoreException
128
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700129void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
130 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700131 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800132 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700133 mirror::Class::PrettyDescriptor(element_class).c_str(),
134 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800135}
136
Orion Hodsonc069a302017-01-18 09:23:12 +0000137// BootstrapMethodError
138
139void ThrowBootstrapMethodError(const char* fmt, ...) {
140 va_list args;
141 va_start(args, fmt);
142 ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
143 va_end(args);
144}
145
146void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
147 va_list args;
148 va_start(args, fmt);
149 ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
150 va_end(args);
151}
152
Ian Rogers62d6c772013-02-27 08:32:07 -0800153// ClassCastException
154
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700155void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Vladimir Markof5c537e2018-04-09 18:33:55 +0100156 DumpB77342775DebugData(dest_type, src_type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700157 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700159 mirror::Class::PrettyDescriptor(src_type).c_str(),
160 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800161}
162
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000163void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700164 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800165}
166
167// ClassCircularityError
168
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700169void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800170 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700171 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000172 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800173}
174
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700175void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100176 va_list args;
177 va_start(args, fmt);
178 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
179 va_end(args);
180}
181
Ian Rogers62d6c772013-02-27 08:32:07 -0800182// ClassFormatError
183
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700184void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 va_list args;
186 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000187 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100188 va_end(args);
189}
Ian Rogers62d6c772013-02-27 08:32:07 -0800190
Ian Rogers87e552d2012-08-31 15:54:48 -0700191// IllegalAccessError
192
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700193void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700194 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700195 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
196 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000197 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700198}
199
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700200void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
201 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700203 InvokeType type) {
204 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700205 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
206 << "' attempting to access '"
207 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
208 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000209 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700210}
211
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700212void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700213 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700214 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
215 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000216 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700217}
218
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700219void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700220 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700221 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
222 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000223 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700224}
225
Mathieu Chartiere401d142015-04-22 13:56:20 -0700226void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700227 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700228 msg << "Final field '" << ArtField::PrettyField(accessed, false)
229 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000230 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800232 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700233}
234
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700235void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 va_list args;
237 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000238 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800239 va_end(args);
240}
241
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700242// IllegalAccessException
243
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000244void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700245 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700246}
247
Ian Rogers62d6c772013-02-27 08:32:07 -0800248// IllegalArgumentException
249
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000250void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700251 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800252}
253
Orion Hodson928033d2018-02-07 05:30:54 +0000254// IllegalStateException
255
256void ThrowIllegalStateException(const char* msg) {
257 ThrowException("Ljava/lang/IllegalStateException;", nullptr, msg);
258}
Ian Rogers62d6c772013-02-27 08:32:07 -0800259
Ian Rogers87e552d2012-08-31 15:54:48 -0700260// IncompatibleClassChangeError
261
262void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700264 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700265 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700266 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000267 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700270}
271
Alex Light705ad492015-09-21 11:36:30 -0700272void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700273 ObjPtr<mirror::Class> target_class,
274 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700275 ArtMethod* referrer) {
276 // Referrer is calling interface_method on this_object, however, the interface_method isn't
277 // implemented by this_object.
278 CHECK(this_object != nullptr);
279 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700280 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
281 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
282 << "' in call to '"
283 << ArtMethod::PrettyMethod(method) << "'";
Vladimir Markof5c537e2018-04-09 18:33:55 +0100284 DumpB77342775DebugData(target_class, this_object->GetClass());
Alex Light705ad492015-09-21 11:36:30 -0700285 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
286 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
287 msg.str().c_str());
288}
289
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700291 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700293 // Referrer is calling interface_method on this_object, however, the interface_method isn't
294 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700295 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700296 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700297 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700298 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700299 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
300 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Vladimir Markof5c537e2018-04-09 18:33:55 +0100301 DumpB77342775DebugData(interface_method->GetDeclaringClass(), this_object->GetClass());
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000302 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800304 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700305}
306
Mathieu Chartierc7853442015-03-27 14:35:38 -0700307void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700309 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700310 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700311 << (is_static ? "static" : "instance") << " field" << " rather than a "
312 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 msg.str().c_str());
315}
316
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700317void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 va_list args;
319 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000320 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 va_end(args);
322}
323
Alex Light9139e002015-10-09 15:59:48 -0700324void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
325 DCHECK(method != nullptr);
326 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700327 /*referrer=*/nullptr,
Alex Light9139e002015-10-09 15:59:48 -0700328 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700329 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700330}
331
Orion Hodson928033d2018-02-07 05:30:54 +0000332// IndexOutOfBoundsException
333
334void ThrowIndexOutOfBoundsException(int index, int length) {
335 ThrowException("Ljava/lang/IndexOutOfBoundsException;", nullptr,
336 StringPrintf("length=%d; index=%d", length, index).c_str());
337}
338
Alex Lightdb01a092017-04-03 15:39:55 -0700339// InternalError
340
341void ThrowInternalError(const char* fmt, ...) {
342 va_list args;
343 va_start(args, fmt);
344 ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
345 va_end(args);
346}
Alex Light9139e002015-10-09 15:59:48 -0700347
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700348// IOException
349
350void ThrowIOException(const char* fmt, ...) {
351 va_list args;
352 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700353 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700354 va_end(args);
355}
356
Andreas Gampe329d1882014-04-08 10:32:19 -0700357void ThrowWrappedIOException(const char* fmt, ...) {
358 va_list args;
359 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700360 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700361 va_end(args);
362}
363
Ian Rogers62d6c772013-02-27 08:32:07 -0800364// LinkageError
365
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700366void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 va_list args;
368 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000369 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 va_end(args);
371}
372
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700373void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100374 va_list args;
375 va_start(args, fmt);
376 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
377 va_end(args);
378}
379
Ian Rogers62d6c772013-02-27 08:32:07 -0800380// NegativeArraySizeException
381
382void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700383 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700384 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800385}
386
387void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700388 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800389}
390
391// NoSuchFieldError
392
Vladimir Marko72101082019-02-05 16:16:30 +0000393void ThrowNoSuchFieldError(std::string_view scope,
394 ObjPtr<mirror::Class> c,
395 std::string_view type,
396 std::string_view name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700398 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700400 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000401 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700402}
403
Vladimir Marko72101082019-02-05 16:16:30 +0000404void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, std::string_view name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700405 std::ostringstream msg;
406 std::string temp;
407 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
408 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
409}
410
Ian Rogers87e552d2012-08-31 15:54:48 -0700411// NoSuchMethodError
412
Vladimir Marko72101082019-02-05 16:16:30 +0000413void ThrowNoSuchMethodError(InvokeType type,
414 ObjPtr<mirror::Class> c,
415 std::string_view name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700416 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700417 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700418 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700419 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700420 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000421 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700422}
423
Ian Rogers62d6c772013-02-27 08:32:07 -0800424// NullPointerException
425
Mathieu Chartierc7853442015-03-27 14:35:38 -0700426void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800427 std::ostringstream msg;
428 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700429 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700430 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800431}
432
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000433static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200434 const DexFile& dex_file,
435 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700436 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800437 std::ostringstream msg;
438 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700439 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700440 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800441}
442
Vladimir Marko7e097372018-11-28 16:40:59 +0000443void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx, InvokeType type) {
444 const DexFile& dex_file = *Thread::Current()->GetCurrentMethod(nullptr)->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000445 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200446}
447
Vladimir Marko7e097372018-11-28 16:40:59 +0000448void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method, InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000449 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Vladimir Marko7e097372018-11-28 16:40:59 +0000450 *method->GetDexFile(),
451 type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200452}
453
Vladimir Marko953437b2016-08-24 08:30:46 +0000454static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
455 DCHECK(kEmitCompilerReadBarrier);
456 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
Vladimir Marko33bff252017-11-01 14:35:42 +0000457 if (kUseBakerReadBarrier &&
458 (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64)) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000459 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
460 monitor_offset += gray_byte_position;
461 }
462 return addr == monitor_offset;
463}
464
Nicolas Geoffray13449142017-12-07 22:26:24 +0000465static bool IsValidImplicitCheck(uintptr_t addr, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700466 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100467 if (!CanDoImplicitNullCheckOn(addr)) {
468 return false;
469 }
470
471 switch (instr.Opcode()) {
472 case Instruction::INVOKE_DIRECT:
473 case Instruction::INVOKE_DIRECT_RANGE:
474 case Instruction::INVOKE_VIRTUAL:
475 case Instruction::INVOKE_VIRTUAL_RANGE:
476 case Instruction::INVOKE_INTERFACE:
477 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000478 case Instruction::INVOKE_POLYMORPHIC:
479 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100480 case Instruction::INVOKE_VIRTUAL_QUICK:
481 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
482 // Without inlining, we could just check that the offset is the class offset.
483 // However, when inlining, the compiler can (validly) merge the null check with a field access
484 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
485 // which is the caller.
486 return true;
487 }
488
Vladimir Marko953437b2016-08-24 08:30:46 +0000489 case Instruction::IGET_OBJECT:
490 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
491 return true;
492 }
493 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100494 case Instruction::IGET:
495 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100496 case Instruction::IGET_BOOLEAN:
497 case Instruction::IGET_BYTE:
498 case Instruction::IGET_CHAR:
499 case Instruction::IGET_SHORT:
500 case Instruction::IPUT:
501 case Instruction::IPUT_WIDE:
502 case Instruction::IPUT_OBJECT:
503 case Instruction::IPUT_BOOLEAN:
504 case Instruction::IPUT_BYTE:
505 case Instruction::IPUT_CHAR:
506 case Instruction::IPUT_SHORT: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000507 // We might be doing an implicit null check with an offset that doesn't correspond
508 // to the instruction, for example with two field accesses and the first one being
509 // eliminated or re-ordered.
510 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100511 }
512
Vladimir Marko953437b2016-08-24 08:30:46 +0000513 case Instruction::IGET_OBJECT_QUICK:
514 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
515 return true;
516 }
517 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100518 case Instruction::IGET_QUICK:
519 case Instruction::IGET_BOOLEAN_QUICK:
520 case Instruction::IGET_BYTE_QUICK:
521 case Instruction::IGET_CHAR_QUICK:
522 case Instruction::IGET_SHORT_QUICK:
523 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100524 case Instruction::IPUT_QUICK:
525 case Instruction::IPUT_BOOLEAN_QUICK:
526 case Instruction::IPUT_BYTE_QUICK:
527 case Instruction::IPUT_CHAR_QUICK:
528 case Instruction::IPUT_SHORT_QUICK:
529 case Instruction::IPUT_WIDE_QUICK:
530 case Instruction::IPUT_OBJECT_QUICK: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000531 // We might be doing an implicit null check with an offset that doesn't correspond
532 // to the instruction, for example with two field accesses and the first one being
533 // eliminated or re-ordered.
534 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100535 }
536
Vladimir Marko953437b2016-08-24 08:30:46 +0000537 case Instruction::AGET_OBJECT:
538 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
539 return true;
540 }
541 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100542 case Instruction::AGET:
543 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100544 case Instruction::AGET_BOOLEAN:
545 case Instruction::AGET_BYTE:
546 case Instruction::AGET_CHAR:
547 case Instruction::AGET_SHORT:
548 case Instruction::APUT:
549 case Instruction::APUT_WIDE:
550 case Instruction::APUT_OBJECT:
551 case Instruction::APUT_BOOLEAN:
552 case Instruction::APUT_BYTE:
553 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100554 case Instruction::APUT_SHORT:
555 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100556 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100557 // The length access should crash. We currently do not do implicit checks on
558 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000559 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100560 }
561
562 default: {
563 // We have covered all the cases where an NPE could occur.
564 // Note that this must be kept in sync with the compiler, and adding
565 // any new way to do implicit checks in the compiler should also update
566 // this code.
567 return false;
568 }
569 }
570}
571
572void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000573 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700574 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
David Sehr0225f8e2018-01-31 08:52:24 +0000575 CodeItemInstructionAccessor accessor(method->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800576 CHECK_LT(throw_dex_pc, accessor.InsnsSizeInCodeUnits());
577 const Instruction& instr = accessor.InstructionAt(throw_dex_pc);
578 if (check_address && !IsValidImplicitCheck(addr, instr)) {
Vladimir Marko7e097372018-11-28 16:40:59 +0000579 const DexFile* dex_file = method->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100580 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
581 << "0x" << std::hex << addr << std::dec
582 << ", at "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800583 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100584 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700585 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100586 }
587
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800588 switch (instr.Opcode()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800589 case Instruction::INVOKE_DIRECT:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800590 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200591 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800592 case Instruction::INVOKE_DIRECT_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800593 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800594 break;
595 case Instruction::INVOKE_VIRTUAL:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800596 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200597 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800598 case Instruction::INVOKE_VIRTUAL_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800599 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800600 break;
601 case Instruction::INVOKE_INTERFACE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800602 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200603 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800604 case Instruction::INVOKE_INTERFACE_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800605 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800606 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000607 case Instruction::INVOKE_POLYMORPHIC:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800608 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_45cc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000609 break;
610 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800611 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_4rcc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000612 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200613 case Instruction::INVOKE_VIRTUAL_QUICK:
614 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000615 uint16_t method_idx = method->GetIndexFromQuickening(throw_dex_pc);
616 if (method_idx != DexFile::kDexNoIndex16) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200617 // NPE with precise message.
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000618 ThrowNullPointerExceptionForMethodAccess(method_idx, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200619 } else {
620 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000621 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200622 }
623 break;
624 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800625 case Instruction::IGET:
626 case Instruction::IGET_WIDE:
627 case Instruction::IGET_OBJECT:
628 case Instruction::IGET_BOOLEAN:
629 case Instruction::IGET_BYTE:
630 case Instruction::IGET_CHAR:
631 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700632 ArtField* field =
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800633 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Andreas Gampe71307442018-02-06 13:38:03 -0800634 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700635 ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ true);
Ian Rogers62d6c772013-02-27 08:32:07 -0800636 break;
637 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200638 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800639 case Instruction::IGET_BOOLEAN_QUICK:
640 case Instruction::IGET_BYTE_QUICK:
641 case Instruction::IGET_CHAR_QUICK:
642 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200643 case Instruction::IGET_WIDE_QUICK:
644 case Instruction::IGET_OBJECT_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000645 uint16_t field_idx = method->GetIndexFromQuickening(throw_dex_pc);
646 ArtField* field = nullptr;
647 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
648 field = Runtime::Current()->GetClassLinker()->ResolveField(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700649 field_idx, method, /* is_static= */ false);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000650 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700651 ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200652 break;
653 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800654 case Instruction::IPUT:
655 case Instruction::IPUT_WIDE:
656 case Instruction::IPUT_OBJECT:
657 case Instruction::IPUT_BOOLEAN:
658 case Instruction::IPUT_BYTE:
659 case Instruction::IPUT_CHAR:
660 case Instruction::IPUT_SHORT: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000661 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveField(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700662 instr.VRegC_22c(), method, /* is_static= */ false);
Andreas Gampe71307442018-02-06 13:38:03 -0800663 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700664 ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ false);
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 break;
666 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200667 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700668 case Instruction::IPUT_BOOLEAN_QUICK:
669 case Instruction::IPUT_BYTE_QUICK:
670 case Instruction::IPUT_CHAR_QUICK:
671 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200672 case Instruction::IPUT_WIDE_QUICK:
673 case Instruction::IPUT_OBJECT_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000674 uint16_t field_idx = method->GetIndexFromQuickening(throw_dex_pc);
675 ArtField* field = nullptr;
676 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
677 field = Runtime::Current()->GetClassLinker()->ResolveField(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700678 field_idx, method, /* is_static= */ false);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000679 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700680 ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200681 break;
682 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800683 case Instruction::AGET:
684 case Instruction::AGET_WIDE:
685 case Instruction::AGET_OBJECT:
686 case Instruction::AGET_BOOLEAN:
687 case Instruction::AGET_BYTE:
688 case Instruction::AGET_CHAR:
689 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700690 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800691 "Attempt to read from null array");
692 break;
693 case Instruction::APUT:
694 case Instruction::APUT_WIDE:
695 case Instruction::APUT_OBJECT:
696 case Instruction::APUT_BOOLEAN:
697 case Instruction::APUT_BYTE:
698 case Instruction::APUT_CHAR:
699 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700700 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800701 "Attempt to write to null array");
702 break;
703 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700704 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800705 "Attempt to get length of null array");
706 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100707 case Instruction::FILL_ARRAY_DATA: {
708 ThrowException("Ljava/lang/NullPointerException;", nullptr,
709 "Attempt to write to null array");
710 break;
711 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100712 case Instruction::MONITOR_ENTER:
713 case Instruction::MONITOR_EXIT: {
714 ThrowException("Ljava/lang/NullPointerException;", nullptr,
715 "Attempt to do a synchronize operation on a null object");
716 break;
717 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800718 default: {
Vladimir Marko7e097372018-11-28 16:40:59 +0000719 const DexFile* dex_file = method->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100720 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800721 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100722 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700723 << method->PrettyMethod();
Elliott Hughesc1896c92018-11-29 11:33:18 -0800724 UNREACHABLE();
Ian Rogers62d6c772013-02-27 08:32:07 -0800725 }
726 }
727}
728
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000729void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700730 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800731}
732
Orion Hodson928033d2018-02-07 05:30:54 +0000733// ReadOnlyBufferException
734
735void ThrowReadOnlyBufferException() {
736 Thread::Current()->ThrowNewException("Ljava/nio/ReadOnlyBufferException;", nullptr);
737}
738
Ian Rogers62d6c772013-02-27 08:32:07 -0800739// RuntimeException
740
741void ThrowRuntimeException(const char* fmt, ...) {
742 va_list args;
743 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700744 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800745 va_end(args);
746}
747
Leonard Mosescueb842212016-10-06 17:26:36 -0700748// SecurityException
749
750void ThrowSecurityException(const char* fmt, ...) {
751 va_list args;
752 va_start(args, fmt);
753 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
754 va_end(args);
755}
756
Andreas Gampe103992b2016-01-04 15:32:43 -0800757// Stack overflow.
758
759void ThrowStackOverflowError(Thread* self) {
760 if (self->IsHandlingStackOverflow()) {
761 LOG(ERROR) << "Recursive stack overflow.";
762 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
763 }
764
765 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
766 JNIEnvExt* env = self->GetJniEnv();
767 std::string msg("stack size ");
768 msg += PrettySize(self->GetStackSize());
769
770 // Avoid running Java code for exception initialization.
771 // TODO: Checks to make this a bit less brittle.
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000772 //
773 // Note: this lambda ensures that the destruction of the ScopedLocalRefs will run in the extended
774 // stack, which is important for modes with larger stack sizes (e.g., ASAN). Using a lambda
775 // instead of a block simplifies the control flow.
776 auto create_and_throw = [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
777 // Allocate an uninitialized object.
778 ScopedLocalRef<jobject> exc(env,
779 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
780 if (exc == nullptr) {
781 LOG(WARNING) << "Could not allocate StackOverflowError object.";
782 return;
783 }
Andreas Gampe103992b2016-01-04 15:32:43 -0800784
Andreas Gampe103992b2016-01-04 15:32:43 -0800785 // "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:
Neil Fullerc0f02d42018-06-11 09:49:01 +0000794 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
Andreas Gampe103992b2016-01-04 15:32:43 -0800795 // fillInStackTrace();
796
797 // detailMessage.
798 // TODO: Use String::FromModifiedUTF...?
799 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000800 if (s == nullptr) {
801 LOG(WARNING) << "Could not throw new StackOverflowError because JNI NewStringUTF failed.";
802 return;
Andreas Gampe103992b2016-01-04 15:32:43 -0800803 }
Andreas Gampe103992b2016-01-04 15:32:43 -0800804
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000805 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
806
807 // cause.
808 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
809
810 // suppressedExceptions.
811 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
812 WellKnownClasses::java_util_Collections,
813 WellKnownClasses::java_util_Collections_EMPTY_LIST));
814 CHECK(emptylist != nullptr);
815 env->SetObjectField(exc.get(),
816 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
817 emptylist.get());
818
819 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
820 // nativeFillInStackTrace.
821 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
822 {
823 ScopedObjectAccessUnchecked soa(env); // TODO: Is this necessary?
824 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
825 }
826 if (stack_state_val != nullptr) {
827 env->SetObjectField(exc.get(),
828 WellKnownClasses::java_lang_Throwable_stackState,
829 stack_state_val.get());
830
831 // stackTrace.
832 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
833 WellKnownClasses::libcore_util_EmptyArray,
834 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
835 env->SetObjectField(exc.get(),
836 WellKnownClasses::java_lang_Throwable_stackTrace,
837 stack_trace_elem.get());
838 } else {
839 LOG(WARNING) << "Could not create stack trace.";
840 // Note: we'll create an exception without stack state, which is valid.
841 }
842
843 // Throw the exception.
844 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
845 };
846 create_and_throw();
847 CHECK(self->IsExceptionPending());
Andreas Gampe103992b2016-01-04 15:32:43 -0800848
849 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
850 self->ResetDefaultStackEnd(); // Return to default stack size.
851
852 // And restore protection if implicit checks are on.
853 if (!explicit_overflow_check) {
854 self->ProtectStack();
855 }
856}
857
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100858// StringIndexOutOfBoundsException
859
860void ThrowStringIndexOutOfBoundsException(int index, int length) {
861 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
862 StringPrintf("length=%d; index=%d", length, index).c_str());
863}
864
Orion Hodson928033d2018-02-07 05:30:54 +0000865// UnsupportedOperationException
866
867void ThrowUnsupportedOperationException() {
868 ThrowException("Ljava/lang/UnsupportedOperationException;");
869}
870
Ian Rogers62d6c772013-02-27 08:32:07 -0800871// VerifyError
872
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700873void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800874 va_list args;
875 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000876 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800877 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700878}
879
Narayan Kamath208f8572016-08-03 12:46:58 +0100880// WrongMethodTypeException
881
Orion Hodsona5dca522018-02-27 12:42:11 +0000882void ThrowWrongMethodTypeException(ObjPtr<mirror::MethodType> expected_type,
883 ObjPtr<mirror::MethodType> actual_type) {
Orion Hodson3f383462018-05-17 14:03:39 +0100884 ThrowWrongMethodTypeException(expected_type->PrettyDescriptor(), actual_type->PrettyDescriptor());
885}
886
887void ThrowWrongMethodTypeException(const std::string& expected_descriptor,
888 const std::string& actual_descriptor) {
889 std::ostringstream msg;
890 msg << "Expected " << expected_descriptor << " but was " << actual_descriptor;
891 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;", nullptr, msg.str().c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100892}
893
Ian Rogers87e552d2012-08-31 15:54:48 -0700894} // namespace art