blob: 17a0a8a714fa425285885095a39b8a10664b827f [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"
Santiago Aboy Solanes14e8d642022-02-04 14:15:01 +000026#include "art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class_linker-inl.h"
Vladimir Markof5c537e2018-04-09 18:33:55 +010028#include "debug_print.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/dex_file-inl.h"
30#include "dex/dex_instruction-inl.h"
David Sehr8c0961f2018-01-23 16:11:38 -080031#include "dex/invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010033#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/object-inl.h"
35#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070036#include "nativehelper/scoped_local_ref.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070037#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070038#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070039#include "well_known_classes.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070040
Ian Rogers87e552d2012-08-31 15:54:48 -070041namespace art {
42
Andreas Gampe46ee31b2016-12-14 10:11:49 -080043using android::base::StringAppendV;
44using android::base::StringPrintf;
45
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070046static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070047 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070048 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070049 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070050 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070051 os << " (declaration of '" << referrer->PrettyDescriptor()
52 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070053 }
54 }
55}
56
Orion Hodson928033d2018-02-07 05:30:54 +000057static void ThrowException(const char* exception_descriptor) REQUIRES_SHARED(Locks::mutator_lock_) {
58 Thread* self = Thread::Current();
59 self->ThrowNewException(exception_descriptor, nullptr);
60}
61
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070063 ObjPtr<mirror::Class> referrer,
64 const char* fmt,
65 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070067 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080069 std::string vmsg;
70 StringAppendV(&vmsg, fmt, *args);
71 msg << vmsg;
72 } else {
73 msg << fmt;
74 }
75 AddReferrerLocation(msg, referrer);
76 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000077 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070078}
79
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000080static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070081 ObjPtr<mirror::Class> referrer,
82 const char* fmt,
83 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070085 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070086 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070087 std::string vmsg;
88 StringAppendV(&vmsg, fmt, *args);
89 msg << vmsg;
90 } else {
91 msg << fmt;
92 }
93 AddReferrerLocation(msg, referrer);
94 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000095 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070096}
97
Sebastien Hertz56adf602013-07-09 17:27:07 +020098// AbstractMethodError
99
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700101 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +0200102 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700103 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +0200104}
105
Alex Light705ad492015-09-21 11:36:30 -0700106void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700107 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer= */ nullptr,
Alex Light705ad492015-09-21 11:36:30 -0700108 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700109 dex_file.PrettyMethod(method_idx,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700110 /* with_signature= */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700111}
112
Ian Rogers62d6c772013-02-27 08:32:07 -0800113// ArithmeticException
114
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200115void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800117}
118
119// ArrayIndexOutOfBoundsException
120
121void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700122 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800123 StringPrintf("length=%d; index=%d", length, index).c_str());
124}
125
126// ArrayStoreException
127
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700128void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
129 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800131 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700132 mirror::Class::PrettyDescriptor(element_class).c_str(),
133 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800134}
135
Orion Hodsonc069a302017-01-18 09:23:12 +0000136// BootstrapMethodError
137
138void ThrowBootstrapMethodError(const char* fmt, ...) {
139 va_list args;
140 va_start(args, fmt);
141 ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
142 va_end(args);
143}
144
145void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
146 va_list args;
147 va_start(args, fmt);
148 ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
149 va_end(args);
150}
151
Ian Rogers62d6c772013-02-27 08:32:07 -0800152// ClassCastException
153
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700154void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Vladimir Markof5c537e2018-04-09 18:33:55 +0100155 DumpB77342775DebugData(dest_type, src_type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800157 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700158 mirror::Class::PrettyDescriptor(src_type).c_str(),
159 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800160}
161
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000162void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700163 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800164}
165
166// ClassCircularityError
167
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700168void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800169 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700170 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000171 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800172}
173
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700174void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100175 va_list args;
176 va_start(args, fmt);
177 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
178 va_end(args);
179}
180
Ian Rogers62d6c772013-02-27 08:32:07 -0800181// ClassFormatError
182
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700183void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800184 va_list args;
185 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000186 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100187 va_end(args);
188}
Ian Rogers62d6c772013-02-27 08:32:07 -0800189
Ian Rogers87e552d2012-08-31 15:54:48 -0700190// IllegalAccessError
191
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700192void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700193 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700194 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
195 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000196 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700197}
198
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700199void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
200 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700202 InvokeType type) {
203 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700204 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
205 << "' attempting to access '"
206 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
207 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000208 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700209}
210
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700211void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700212 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700213 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
214 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000215 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700216}
217
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700218void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700219 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700220 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
221 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000222 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700223}
224
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700226 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700227 msg << "Final field '" << ArtField::PrettyField(accessed, false)
228 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000229 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700230 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700232}
233
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700234void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800235 va_list args;
236 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000237 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800238 va_end(args);
239}
240
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700241// IllegalAccessException
242
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000243void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700244 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700245}
246
Ian Rogers62d6c772013-02-27 08:32:07 -0800247// IllegalArgumentException
248
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000249void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700250 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800251}
252
Orion Hodson928033d2018-02-07 05:30:54 +0000253// IllegalStateException
254
255void ThrowIllegalStateException(const char* msg) {
256 ThrowException("Ljava/lang/IllegalStateException;", nullptr, msg);
257}
Ian Rogers62d6c772013-02-27 08:32:07 -0800258
Ian Rogers87e552d2012-08-31 15:54:48 -0700259// IncompatibleClassChangeError
260
Nicolas Geoffraybdf7dc02022-01-19 10:34:57 +0000261void ThrowIncompatibleClassChangeError(InvokeType expected_type,
262 InvokeType found_type,
263 ArtMethod* method,
264 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700265 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700266 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700267 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000268 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700271}
272
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700274 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700276 // Referrer is calling interface_method on this_object, however, the interface_method isn't
277 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700278 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700279 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700280 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700281 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700282 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
283 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Vladimir Markof5c537e2018-04-09 18:33:55 +0100284 DumpB77342775DebugData(interface_method->GetDeclaringClass(), this_object->GetClass());
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000285 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700286 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800287 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700288}
289
Nicolas Geoffraybdf7dc02022-01-19 10:34:57 +0000290void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field,
291 bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700293 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700294 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700295 << (is_static ? "static" : "instance") << " field" << " rather than a "
296 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800298 msg.str().c_str());
299}
300
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700301void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 va_list args;
303 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000304 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 va_end(args);
306}
307
Alex Light9139e002015-10-09 15:59:48 -0700308void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
309 DCHECK(method != nullptr);
310 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700311 /*referrer=*/nullptr,
Alex Light9139e002015-10-09 15:59:48 -0700312 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700313 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700314}
315
Orion Hodson928033d2018-02-07 05:30:54 +0000316// IndexOutOfBoundsException
317
318void ThrowIndexOutOfBoundsException(int index, int length) {
319 ThrowException("Ljava/lang/IndexOutOfBoundsException;", nullptr,
320 StringPrintf("length=%d; index=%d", length, index).c_str());
321}
322
Alex Lightdb01a092017-04-03 15:39:55 -0700323// InternalError
324
325void ThrowInternalError(const char* fmt, ...) {
326 va_list args;
327 va_start(args, fmt);
328 ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
329 va_end(args);
330}
Alex Light9139e002015-10-09 15:59:48 -0700331
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332// IOException
333
334void ThrowIOException(const char* fmt, ...) {
335 va_list args;
336 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700337 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700338 va_end(args);
339}
340
Andreas Gampe329d1882014-04-08 10:32:19 -0700341void ThrowWrappedIOException(const char* fmt, ...) {
342 va_list args;
343 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700344 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700345 va_end(args);
346}
347
Ian Rogers62d6c772013-02-27 08:32:07 -0800348// LinkageError
349
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700350void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 va_list args;
352 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000353 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 va_end(args);
355}
356
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700357void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100358 va_list args;
359 va_start(args, fmt);
360 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
361 va_end(args);
362}
363
Ian Rogers62d6c772013-02-27 08:32:07 -0800364// NegativeArraySizeException
365
366void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700367 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700368 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800369}
370
371void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700372 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800373}
374
375// NoSuchFieldError
376
Vladimir Marko72101082019-02-05 16:16:30 +0000377void ThrowNoSuchFieldError(std::string_view scope,
378 ObjPtr<mirror::Class> c,
379 std::string_view type,
380 std::string_view name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700382 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700384 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000385 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700386}
387
Vladimir Marko72101082019-02-05 16:16:30 +0000388void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, std::string_view name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700389 std::ostringstream msg;
390 std::string temp;
391 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
392 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
393}
394
Ian Rogers87e552d2012-08-31 15:54:48 -0700395// NoSuchMethodError
396
Vladimir Marko72101082019-02-05 16:16:30 +0000397void ThrowNoSuchMethodError(InvokeType type,
398 ObjPtr<mirror::Class> c,
399 std::string_view name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700400 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700401 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700402 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700403 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700404 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000405 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700406}
407
Ian Rogers62d6c772013-02-27 08:32:07 -0800408// NullPointerException
409
Santiago Aboy Solanes14e8d642022-02-04 14:15:01 +0000410void ThrowNullPointerExceptionForFieldAccess(ArtField* field, ArtMethod* method, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 std::ostringstream msg;
Santiago Aboy Solanes14e8d642022-02-04 14:15:01 +0000412 msg << "Attempt to " << (is_read ? "read from" : "write to") << " field '"
413 << ArtField::PrettyField(field) << "' on a null object reference in method '"
414 << ArtMethod::PrettyMethod(method) << "'";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700415 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800416}
417
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000418static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200419 const DexFile& dex_file,
420 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700421 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800422 std::ostringstream msg;
423 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700424 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700425 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800426}
427
Vladimir Marko7e097372018-11-28 16:40:59 +0000428void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx, InvokeType type) {
429 const DexFile& dex_file = *Thread::Current()->GetCurrentMethod(nullptr)->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000430 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200431}
432
Vladimir Marko7e097372018-11-28 16:40:59 +0000433void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method, InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000434 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Vladimir Marko7e097372018-11-28 16:40:59 +0000435 *method->GetDexFile(),
436 type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200437}
438
Vladimir Marko953437b2016-08-24 08:30:46 +0000439static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
440 DCHECK(kEmitCompilerReadBarrier);
441 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
Vladimir Marko33bff252017-11-01 14:35:42 +0000442 if (kUseBakerReadBarrier &&
443 (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64)) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000444 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
445 monitor_offset += gray_byte_position;
446 }
447 return addr == monitor_offset;
448}
449
Nicolas Geoffray13449142017-12-07 22:26:24 +0000450static bool IsValidImplicitCheck(uintptr_t addr, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700451 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100452 if (!CanDoImplicitNullCheckOn(addr)) {
453 return false;
454 }
455
456 switch (instr.Opcode()) {
457 case Instruction::INVOKE_DIRECT:
458 case Instruction::INVOKE_DIRECT_RANGE:
459 case Instruction::INVOKE_VIRTUAL:
460 case Instruction::INVOKE_VIRTUAL_RANGE:
461 case Instruction::INVOKE_INTERFACE:
462 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000463 case Instruction::INVOKE_POLYMORPHIC:
464 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffray786e1fe2020-01-13 15:17:07 +0000465 case Instruction::INVOKE_SUPER:
Nicolas Geoffray4924ea92021-03-23 08:25:31 +0000466 case Instruction::INVOKE_SUPER_RANGE: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100467 // Without inlining, we could just check that the offset is the class offset.
468 // However, when inlining, the compiler can (validly) merge the null check with a field access
469 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
470 // which is the caller.
471 return true;
472 }
473
Vladimir Marko953437b2016-08-24 08:30:46 +0000474 case Instruction::IGET_OBJECT:
475 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
476 return true;
477 }
478 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100479 case Instruction::IGET:
480 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100481 case Instruction::IGET_BOOLEAN:
482 case Instruction::IGET_BYTE:
483 case Instruction::IGET_CHAR:
484 case Instruction::IGET_SHORT:
485 case Instruction::IPUT:
486 case Instruction::IPUT_WIDE:
487 case Instruction::IPUT_OBJECT:
488 case Instruction::IPUT_BOOLEAN:
489 case Instruction::IPUT_BYTE:
490 case Instruction::IPUT_CHAR:
491 case Instruction::IPUT_SHORT: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000492 // We might be doing an implicit null check with an offset that doesn't correspond
493 // to the instruction, for example with two field accesses and the first one being
494 // eliminated or re-ordered.
495 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100496 }
497
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 case Instruction::AGET_OBJECT:
499 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
500 return true;
501 }
502 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100503 case Instruction::AGET:
504 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100505 case Instruction::AGET_BOOLEAN:
506 case Instruction::AGET_BYTE:
507 case Instruction::AGET_CHAR:
508 case Instruction::AGET_SHORT:
509 case Instruction::APUT:
510 case Instruction::APUT_WIDE:
511 case Instruction::APUT_OBJECT:
512 case Instruction::APUT_BOOLEAN:
513 case Instruction::APUT_BYTE:
514 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100515 case Instruction::APUT_SHORT:
516 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100517 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100518 // The length access should crash. We currently do not do implicit checks on
519 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000520 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100521 }
522
523 default: {
524 // We have covered all the cases where an NPE could occur.
525 // Note that this must be kept in sync with the compiler, and adding
526 // any new way to do implicit checks in the compiler should also update
527 // this code.
528 return false;
529 }
530 }
531}
532
533void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000534 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700535 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
David Sehr0225f8e2018-01-31 08:52:24 +0000536 CodeItemInstructionAccessor accessor(method->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800537 CHECK_LT(throw_dex_pc, accessor.InsnsSizeInCodeUnits());
538 const Instruction& instr = accessor.InstructionAt(throw_dex_pc);
539 if (check_address && !IsValidImplicitCheck(addr, instr)) {
Vladimir Marko7e097372018-11-28 16:40:59 +0000540 const DexFile* dex_file = method->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100541 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
542 << "0x" << std::hex << addr << std::dec
543 << ", at "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800544 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100545 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700546 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100547 }
548
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800549 switch (instr.Opcode()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800550 case Instruction::INVOKE_DIRECT:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800551 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200552 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800553 case Instruction::INVOKE_DIRECT_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800554 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800555 break;
556 case Instruction::INVOKE_VIRTUAL:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800557 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200558 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800559 case Instruction::INVOKE_VIRTUAL_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800560 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800561 break;
Nicolas Geoffray786e1fe2020-01-13 15:17:07 +0000562 case Instruction::INVOKE_SUPER:
563 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kSuper);
564 break;
565 case Instruction::INVOKE_SUPER_RANGE:
566 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kSuper);
567 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800568 case Instruction::INVOKE_INTERFACE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800569 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200570 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800571 case Instruction::INVOKE_INTERFACE_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800572 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000574 case Instruction::INVOKE_POLYMORPHIC:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800575 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_45cc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000576 break;
577 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800578 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_4rcc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000579 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800580 case Instruction::IGET:
581 case Instruction::IGET_WIDE:
582 case Instruction::IGET_OBJECT:
583 case Instruction::IGET_BOOLEAN:
584 case Instruction::IGET_BYTE:
585 case Instruction::IGET_CHAR:
586 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700587 ArtField* field =
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800588 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Andreas Gampe71307442018-02-06 13:38:03 -0800589 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Santiago Aboy Solanes14e8d642022-02-04 14:15:01 +0000590 ThrowNullPointerExceptionForFieldAccess(field, method, /* is_read= */ true);
Ian Rogers62d6c772013-02-27 08:32:07 -0800591 break;
592 }
593 case Instruction::IPUT:
594 case Instruction::IPUT_WIDE:
595 case Instruction::IPUT_OBJECT:
596 case Instruction::IPUT_BOOLEAN:
597 case Instruction::IPUT_BYTE:
598 case Instruction::IPUT_CHAR:
599 case Instruction::IPUT_SHORT: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000600 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveField(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700601 instr.VRegC_22c(), method, /* is_static= */ false);
Andreas Gampe71307442018-02-06 13:38:03 -0800602 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Santiago Aboy Solanes14e8d642022-02-04 14:15:01 +0000603 ThrowNullPointerExceptionForFieldAccess(field, method, /* is_read= */ false);
Ian Rogers62d6c772013-02-27 08:32:07 -0800604 break;
605 }
606 case Instruction::AGET:
607 case Instruction::AGET_WIDE:
608 case Instruction::AGET_OBJECT:
609 case Instruction::AGET_BOOLEAN:
610 case Instruction::AGET_BYTE:
611 case Instruction::AGET_CHAR:
612 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700613 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800614 "Attempt to read from null array");
615 break;
616 case Instruction::APUT:
617 case Instruction::APUT_WIDE:
618 case Instruction::APUT_OBJECT:
619 case Instruction::APUT_BOOLEAN:
620 case Instruction::APUT_BYTE:
621 case Instruction::APUT_CHAR:
622 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700623 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800624 "Attempt to write to null array");
625 break;
626 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700627 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 "Attempt to get length of null array");
629 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100630 case Instruction::FILL_ARRAY_DATA: {
631 ThrowException("Ljava/lang/NullPointerException;", nullptr,
632 "Attempt to write to null array");
633 break;
634 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100635 case Instruction::MONITOR_ENTER:
636 case Instruction::MONITOR_EXIT: {
637 ThrowException("Ljava/lang/NullPointerException;", nullptr,
638 "Attempt to do a synchronize operation on a null object");
639 break;
640 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800641 default: {
Vladimir Marko7e097372018-11-28 16:40:59 +0000642 const DexFile* dex_file = method->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100643 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800644 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100645 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700646 << method->PrettyMethod();
Elliott Hughesc1896c92018-11-29 11:33:18 -0800647 UNREACHABLE();
Ian Rogers62d6c772013-02-27 08:32:07 -0800648 }
649 }
650}
651
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000652void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700653 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800654}
655
Nicolas Geoffray635967b2019-08-07 10:15:39 +0100656void ThrowNullPointerException() {
657 ThrowException("Ljava/lang/NullPointerException;");
658}
659
Orion Hodson928033d2018-02-07 05:30:54 +0000660// ReadOnlyBufferException
661
662void ThrowReadOnlyBufferException() {
663 Thread::Current()->ThrowNewException("Ljava/nio/ReadOnlyBufferException;", nullptr);
664}
665
Ian Rogers62d6c772013-02-27 08:32:07 -0800666// RuntimeException
667
668void ThrowRuntimeException(const char* fmt, ...) {
669 va_list args;
670 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700671 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 va_end(args);
673}
674
Leonard Mosescueb842212016-10-06 17:26:36 -0700675// SecurityException
676
677void ThrowSecurityException(const char* fmt, ...) {
678 va_list args;
679 va_start(args, fmt);
680 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
681 va_end(args);
682}
683
Andreas Gampe103992b2016-01-04 15:32:43 -0800684// Stack overflow.
685
686void ThrowStackOverflowError(Thread* self) {
687 if (self->IsHandlingStackOverflow()) {
688 LOG(ERROR) << "Recursive stack overflow.";
689 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
690 }
691
692 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
693 JNIEnvExt* env = self->GetJniEnv();
694 std::string msg("stack size ");
695 msg += PrettySize(self->GetStackSize());
696
697 // Avoid running Java code for exception initialization.
698 // TODO: Checks to make this a bit less brittle.
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000699 //
700 // Note: this lambda ensures that the destruction of the ScopedLocalRefs will run in the extended
701 // stack, which is important for modes with larger stack sizes (e.g., ASAN). Using a lambda
702 // instead of a block simplifies the control flow.
703 auto create_and_throw = [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
704 // Allocate an uninitialized object.
705 ScopedLocalRef<jobject> exc(env,
706 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
707 if (exc == nullptr) {
708 LOG(WARNING) << "Could not allocate StackOverflowError object.";
709 return;
710 }
Andreas Gampe103992b2016-01-04 15:32:43 -0800711
Andreas Gampe103992b2016-01-04 15:32:43 -0800712 // "Initialize".
713 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
714 // Only Throwable has "custom" fields:
715 // String detailMessage.
716 // Throwable cause (= this).
717 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
718 // Object stackState;
719 // StackTraceElement[] stackTrace;
720 // Only Throwable has a non-empty constructor:
Neil Fullerc0f02d42018-06-11 09:49:01 +0000721 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
Andreas Gampe103992b2016-01-04 15:32:43 -0800722 // fillInStackTrace();
723
724 // detailMessage.
725 // TODO: Use String::FromModifiedUTF...?
726 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000727 if (s == nullptr) {
728 LOG(WARNING) << "Could not throw new StackOverflowError because JNI NewStringUTF failed.";
729 return;
Andreas Gampe103992b2016-01-04 15:32:43 -0800730 }
Andreas Gampe103992b2016-01-04 15:32:43 -0800731
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000732 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
733
734 // cause.
735 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
736
737 // suppressedExceptions.
738 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
739 WellKnownClasses::java_util_Collections,
740 WellKnownClasses::java_util_Collections_EMPTY_LIST));
741 CHECK(emptylist != nullptr);
742 env->SetObjectField(exc.get(),
743 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
744 emptylist.get());
745
746 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
747 // nativeFillInStackTrace.
748 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
749 {
750 ScopedObjectAccessUnchecked soa(env); // TODO: Is this necessary?
Vladimir Markod34b73b2020-05-05 10:07:59 +0100751 stack_state_val.reset(soa.Self()->CreateInternalStackTrace(soa));
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000752 }
753 if (stack_state_val != nullptr) {
754 env->SetObjectField(exc.get(),
755 WellKnownClasses::java_lang_Throwable_stackState,
756 stack_state_val.get());
757
758 // stackTrace.
759 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
760 WellKnownClasses::libcore_util_EmptyArray,
761 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
762 env->SetObjectField(exc.get(),
763 WellKnownClasses::java_lang_Throwable_stackTrace,
764 stack_trace_elem.get());
765 } else {
766 LOG(WARNING) << "Could not create stack trace.";
767 // Note: we'll create an exception without stack state, which is valid.
768 }
769
770 // Throw the exception.
771 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
772 };
773 create_and_throw();
774 CHECK(self->IsExceptionPending());
Andreas Gampe103992b2016-01-04 15:32:43 -0800775
Andreas Gampe103992b2016-01-04 15:32:43 -0800776 self->ResetDefaultStackEnd(); // Return to default stack size.
777
778 // And restore protection if implicit checks are on.
Nicolas Geoffray541ca322022-01-20 09:03:04 +0000779 if (Runtime::Current()->GetImplicitStackOverflowChecks()) {
Andreas Gampe103992b2016-01-04 15:32:43 -0800780 self->ProtectStack();
781 }
782}
783
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100784// StringIndexOutOfBoundsException
785
786void ThrowStringIndexOutOfBoundsException(int index, int length) {
787 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
788 StringPrintf("length=%d; index=%d", length, index).c_str());
789}
790
Orion Hodson928033d2018-02-07 05:30:54 +0000791// UnsupportedOperationException
792
793void ThrowUnsupportedOperationException() {
794 ThrowException("Ljava/lang/UnsupportedOperationException;");
795}
796
Ian Rogers62d6c772013-02-27 08:32:07 -0800797// VerifyError
798
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700799void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800800 va_list args;
801 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000802 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800803 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700804}
805
Narayan Kamath208f8572016-08-03 12:46:58 +0100806// WrongMethodTypeException
807
Orion Hodsona5dca522018-02-27 12:42:11 +0000808void ThrowWrongMethodTypeException(ObjPtr<mirror::MethodType> expected_type,
809 ObjPtr<mirror::MethodType> actual_type) {
Orion Hodson3f383462018-05-17 14:03:39 +0100810 ThrowWrongMethodTypeException(expected_type->PrettyDescriptor(), actual_type->PrettyDescriptor());
811}
812
813void ThrowWrongMethodTypeException(const std::string& expected_descriptor,
814 const std::string& actual_descriptor) {
815 std::ostringstream msg;
816 msg << "Expected " << expected_descriptor << " but was " << actual_descriptor;
817 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;", nullptr, msg.str().c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100818}
819
Ian Rogers87e552d2012-08-31 15:54:48 -0700820} // namespace art