blob: c30272e1142d75cc25fb1eb03741e1f652c2f897 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_throws.h"
18
Ian Rogers22d5e732014-07-15 22:23:51 -070019#include <sstream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
Andreas Gampe103992b2016-01-04 15:32:43 -080022#include "ScopedLocalRef.h"
23
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"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020029#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070030#include "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"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070035#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070036#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020037#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070038
Ian Rogers87e552d2012-08-31 15:54:48 -070039namespace art {
40
Andreas Gampe46ee31b2016-12-14 10:11:49 -080041using android::base::StringAppendV;
42using android::base::StringPrintf;
43
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070044static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070045 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070047 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070048 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070049 os << " (declaration of '" << referrer->PrettyDescriptor()
50 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070051 }
52 }
53}
54
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000055static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070056 ObjPtr<mirror::Class> referrer,
57 const char* fmt,
58 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070060 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070061 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080062 std::string vmsg;
63 StringAppendV(&vmsg, fmt, *args);
64 msg << vmsg;
65 } else {
66 msg << fmt;
67 }
68 AddReferrerLocation(msg, referrer);
69 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000070 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070071}
72
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000073static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070074 ObjPtr<mirror::Class> referrer,
75 const char* fmt,
76 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070078 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070080 std::string vmsg;
81 StringAppendV(&vmsg, fmt, *args);
82 msg << vmsg;
83 } else {
84 msg << fmt;
85 }
86 AddReferrerLocation(msg, referrer);
87 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000088 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070089}
90
Sebastien Hertz56adf602013-07-09 17:27:07 +020091// AbstractMethodError
92
Mathieu Chartiere401d142015-04-22 13:56:20 -070093void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020095 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -070096 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +020097}
98
Alex Light705ad492015-09-21 11:36:30 -070099void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
100 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
101 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700102 dex_file.PrettyMethod(method_idx,
103 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700104}
105
Ian Rogers62d6c772013-02-27 08:32:07 -0800106// ArithmeticException
107
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200108void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800110}
111
112// ArrayIndexOutOfBoundsException
113
114void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 StringPrintf("length=%d; index=%d", length, index).c_str());
117}
118
119// ArrayStoreException
120
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700121void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
122 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700123 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700125 mirror::Class::PrettyDescriptor(element_class).c_str(),
126 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800127}
128
129// ClassCastException
130
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700131void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700132 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700134 mirror::Class::PrettyDescriptor(src_type).c_str(),
135 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800136}
137
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000138void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800140}
141
142// ClassCircularityError
143
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700144void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800145 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700146 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000147 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800148}
149
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700150void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100151 va_list args;
152 va_start(args, fmt);
153 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
154 va_end(args);
155}
156
Ian Rogers62d6c772013-02-27 08:32:07 -0800157// ClassFormatError
158
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700159void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800160 va_list args;
161 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000162 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100163 va_end(args);
164}
Ian Rogers62d6c772013-02-27 08:32:07 -0800165
Ian Rogers87e552d2012-08-31 15:54:48 -0700166// IllegalAccessError
167
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700168void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700169 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700170 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
171 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000172 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700173}
174
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700175void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
176 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700177 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700178 InvokeType type) {
179 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700180 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
181 << "' attempting to access '"
182 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
183 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000184 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700185}
186
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700187void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700188 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700189 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
190 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000191 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700192}
193
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700194void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700195 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700196 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
197 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000198 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700199}
200
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700202 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700203 msg << "Final field '" << ArtField::PrettyField(accessed, false)
204 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000205 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700206 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800207 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700208}
209
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700210void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 va_list args;
212 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000213 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800214 va_end(args);
215}
216
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700217// IllegalAccessException
218
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000219void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700220 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700221}
222
Ian Rogers62d6c772013-02-27 08:32:07 -0800223// IllegalArgumentException
224
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000225void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700226 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800227}
228
229
Ian Rogers87e552d2012-08-31 15:54:48 -0700230// IncompatibleClassChangeError
231
232void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700234 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700235 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700236 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000237 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800239 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700240}
241
Alex Light705ad492015-09-21 11:36:30 -0700242void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700243 ObjPtr<mirror::Class> target_class,
244 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700245 ArtMethod* referrer) {
246 // Referrer is calling interface_method on this_object, however, the interface_method isn't
247 // implemented by this_object.
248 CHECK(this_object != nullptr);
249 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700250 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
251 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
252 << "' in call to '"
253 << ArtMethod::PrettyMethod(method) << "'";
Alex Light705ad492015-09-21 11:36:30 -0700254 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
255 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
256 msg.str().c_str());
257}
258
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700260 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700262 // Referrer is calling interface_method on this_object, however, the interface_method isn't
263 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700264 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700265 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700266 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700267 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700268 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
269 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000270 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700273}
274
Mathieu Chartierc7853442015-03-27 14:35:38 -0700275void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700277 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700278 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 << (is_static ? "static" : "instance") << " field" << " rather than a "
280 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 msg.str().c_str());
283}
284
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700285void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 va_list args;
287 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000288 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 va_end(args);
290}
291
Alex Light9139e002015-10-09 15:59:48 -0700292void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
293 DCHECK(method != nullptr);
294 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
295 /*referrer*/nullptr,
296 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700297 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700298}
299
300
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700301// IOException
302
303void ThrowIOException(const char* fmt, ...) {
304 va_list args;
305 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700306 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700307 va_end(args);
308}
309
Andreas Gampe329d1882014-04-08 10:32:19 -0700310void ThrowWrappedIOException(const char* fmt, ...) {
311 va_list args;
312 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700313 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700314 va_end(args);
315}
316
Ian Rogers62d6c772013-02-27 08:32:07 -0800317// LinkageError
318
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700319void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800320 va_list args;
321 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000322 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 va_end(args);
324}
325
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700326void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100327 va_list args;
328 va_start(args, fmt);
329 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
330 va_end(args);
331}
332
Ian Rogers62d6c772013-02-27 08:32:07 -0800333// NegativeArraySizeException
334
335void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700336 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700337 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800338}
339
340void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700341 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800342}
343
344// NoSuchFieldError
345
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700346void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700347 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700349 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700351 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000352 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700353}
354
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700355void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700356 std::ostringstream msg;
357 std::string temp;
358 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
359 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
360}
361
Ian Rogers87e552d2012-08-31 15:54:48 -0700362// NoSuchMethodError
363
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700364void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700365 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700366 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700367 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700368 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700369 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000370 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700371}
372
Ian Rogers62d6c772013-02-27 08:32:07 -0800373// NullPointerException
374
Mathieu Chartierc7853442015-03-27 14:35:38 -0700375void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 std::ostringstream msg;
377 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700378 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800380}
381
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000382static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200383 const DexFile& dex_file,
384 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700385 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800386 std::ostringstream msg;
387 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700388 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700389 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800390}
391
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000392void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200393 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700394 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000395 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200396 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000397 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398}
399
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200401 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700402 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200403 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000404 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200405 dex_file, type);
406}
407
Vladimir Marko953437b2016-08-24 08:30:46 +0000408static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
409 DCHECK(kEmitCompilerReadBarrier);
410 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
411 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
412 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
413 monitor_offset += gray_byte_position;
414 }
415 return addr == monitor_offset;
416}
417
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100418static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700419 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100420 if (!CanDoImplicitNullCheckOn(addr)) {
421 return false;
422 }
423
424 switch (instr.Opcode()) {
425 case Instruction::INVOKE_DIRECT:
426 case Instruction::INVOKE_DIRECT_RANGE:
427 case Instruction::INVOKE_VIRTUAL:
428 case Instruction::INVOKE_VIRTUAL_RANGE:
429 case Instruction::INVOKE_INTERFACE:
430 case Instruction::INVOKE_INTERFACE_RANGE:
431 case Instruction::INVOKE_VIRTUAL_QUICK:
432 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
433 // Without inlining, we could just check that the offset is the class offset.
434 // However, when inlining, the compiler can (validly) merge the null check with a field access
435 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
436 // which is the caller.
437 return true;
438 }
439
Vladimir Marko953437b2016-08-24 08:30:46 +0000440 case Instruction::IGET_OBJECT:
441 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
442 return true;
443 }
444 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100445 case Instruction::IGET:
446 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100447 case Instruction::IGET_BOOLEAN:
448 case Instruction::IGET_BYTE:
449 case Instruction::IGET_CHAR:
450 case Instruction::IGET_SHORT:
451 case Instruction::IPUT:
452 case Instruction::IPUT_WIDE:
453 case Instruction::IPUT_OBJECT:
454 case Instruction::IPUT_BOOLEAN:
455 case Instruction::IPUT_BYTE:
456 case Instruction::IPUT_CHAR:
457 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100458 ArtField* field =
459 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000460 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100461 }
462
Vladimir Marko953437b2016-08-24 08:30:46 +0000463 case Instruction::IGET_OBJECT_QUICK:
464 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
465 return true;
466 }
467 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100468 case Instruction::IGET_QUICK:
469 case Instruction::IGET_BOOLEAN_QUICK:
470 case Instruction::IGET_BYTE_QUICK:
471 case Instruction::IGET_CHAR_QUICK:
472 case Instruction::IGET_SHORT_QUICK:
473 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100474 case Instruction::IPUT_QUICK:
475 case Instruction::IPUT_BOOLEAN_QUICK:
476 case Instruction::IPUT_BYTE_QUICK:
477 case Instruction::IPUT_CHAR_QUICK:
478 case Instruction::IPUT_SHORT_QUICK:
479 case Instruction::IPUT_WIDE_QUICK:
480 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000481 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100482 }
483
Vladimir Marko953437b2016-08-24 08:30:46 +0000484 case Instruction::AGET_OBJECT:
485 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
486 return true;
487 }
488 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100489 case Instruction::AGET:
490 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100491 case Instruction::AGET_BOOLEAN:
492 case Instruction::AGET_BYTE:
493 case Instruction::AGET_CHAR:
494 case Instruction::AGET_SHORT:
495 case Instruction::APUT:
496 case Instruction::APUT_WIDE:
497 case Instruction::APUT_OBJECT:
498 case Instruction::APUT_BOOLEAN:
499 case Instruction::APUT_BYTE:
500 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100501 case Instruction::APUT_SHORT:
502 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100503 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100504 // The length access should crash. We currently do not do implicit checks on
505 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000506 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100507 }
508
509 default: {
510 // We have covered all the cases where an NPE could occur.
511 // Note that this must be kept in sync with the compiler, and adding
512 // any new way to do implicit checks in the compiler should also update
513 // this code.
514 return false;
515 }
516 }
517}
518
519void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000520 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700521 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000522 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800523 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
524 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100525 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
526 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
527 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
528 << "0x" << std::hex << addr << std::dec
529 << ", at "
530 << instr->DumpString(dex_file)
531 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700532 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100533 }
534
Ian Rogers62d6c772013-02-27 08:32:07 -0800535 switch (instr->Opcode()) {
536 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000537 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200538 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800539 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000540 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800541 break;
542 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000543 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200544 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800545 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000546 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800547 break;
548 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000549 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200550 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800551 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000552 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800553 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200554 case Instruction::INVOKE_VIRTUAL_QUICK:
555 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
556 // Since we replaced the method index, we ask the verifier to tell us which
557 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700558 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000559 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700560 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200561 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000562 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200563 } else {
564 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000565 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200566 }
567 break;
568 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800569 case Instruction::IGET:
570 case Instruction::IGET_WIDE:
571 case Instruction::IGET_OBJECT:
572 case Instruction::IGET_BOOLEAN:
573 case Instruction::IGET_BYTE:
574 case Instruction::IGET_CHAR:
575 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700576 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000577 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
578 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800579 break;
580 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200581 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800582 case Instruction::IGET_BOOLEAN_QUICK:
583 case Instruction::IGET_BYTE_QUICK:
584 case Instruction::IGET_CHAR_QUICK:
585 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200586 case Instruction::IGET_WIDE_QUICK:
587 case Instruction::IGET_OBJECT_QUICK: {
588 // Since we replaced the field index, we ask the verifier to tell us which
589 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700590 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000591 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700592 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200593 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000594 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200595 } else {
596 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000597 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200598 }
599 break;
600 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800601 case Instruction::IPUT:
602 case Instruction::IPUT_WIDE:
603 case Instruction::IPUT_OBJECT:
604 case Instruction::IPUT_BOOLEAN:
605 case Instruction::IPUT_BYTE:
606 case Instruction::IPUT_CHAR:
607 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700608 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000609 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
610 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800611 break;
612 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200613 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700614 case Instruction::IPUT_BOOLEAN_QUICK:
615 case Instruction::IPUT_BYTE_QUICK:
616 case Instruction::IPUT_CHAR_QUICK:
617 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200618 case Instruction::IPUT_WIDE_QUICK:
619 case Instruction::IPUT_OBJECT_QUICK: {
620 // Since we replaced the field index, we ask the verifier to tell us which
621 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700622 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000623 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700624 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200625 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000626 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200627 } else {
628 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000629 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200630 }
631 break;
632 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800633 case Instruction::AGET:
634 case Instruction::AGET_WIDE:
635 case Instruction::AGET_OBJECT:
636 case Instruction::AGET_BOOLEAN:
637 case Instruction::AGET_BYTE:
638 case Instruction::AGET_CHAR:
639 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700640 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800641 "Attempt to read from null array");
642 break;
643 case Instruction::APUT:
644 case Instruction::APUT_WIDE:
645 case Instruction::APUT_OBJECT:
646 case Instruction::APUT_BOOLEAN:
647 case Instruction::APUT_BYTE:
648 case Instruction::APUT_CHAR:
649 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700650 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800651 "Attempt to write to null array");
652 break;
653 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700654 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800655 "Attempt to get length of null array");
656 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100657 case Instruction::FILL_ARRAY_DATA: {
658 ThrowException("Ljava/lang/NullPointerException;", nullptr,
659 "Attempt to write to null array");
660 break;
661 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100662 case Instruction::MONITOR_ENTER:
663 case Instruction::MONITOR_EXIT: {
664 ThrowException("Ljava/lang/NullPointerException;", nullptr,
665 "Attempt to do a synchronize operation on a null object");
666 break;
667 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000669 const DexFile* dex_file =
670 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100671 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
672 << instr->DumpString(dex_file)
673 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700674 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800675 break;
676 }
677 }
678}
679
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000680void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700681 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800682}
683
684// RuntimeException
685
686void ThrowRuntimeException(const char* fmt, ...) {
687 va_list args;
688 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700689 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800690 va_end(args);
691}
692
Leonard Mosescueb842212016-10-06 17:26:36 -0700693// SecurityException
694
695void ThrowSecurityException(const char* fmt, ...) {
696 va_list args;
697 va_start(args, fmt);
698 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
699 va_end(args);
700}
701
Andreas Gampe103992b2016-01-04 15:32:43 -0800702// Stack overflow.
703
704void ThrowStackOverflowError(Thread* self) {
705 if (self->IsHandlingStackOverflow()) {
706 LOG(ERROR) << "Recursive stack overflow.";
707 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
708 }
709
710 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
711 JNIEnvExt* env = self->GetJniEnv();
712 std::string msg("stack size ");
713 msg += PrettySize(self->GetStackSize());
714
715 // Avoid running Java code for exception initialization.
716 // TODO: Checks to make this a bit less brittle.
717
718 std::string error_msg;
719
720 // Allocate an uninitialized object.
721 ScopedLocalRef<jobject> exc(env,
722 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
723 if (exc.get() != nullptr) {
724 // "Initialize".
725 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
726 // Only Throwable has "custom" fields:
727 // String detailMessage.
728 // Throwable cause (= this).
729 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
730 // Object stackState;
731 // StackTraceElement[] stackTrace;
732 // Only Throwable has a non-empty constructor:
733 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
734 // fillInStackTrace();
735
736 // detailMessage.
737 // TODO: Use String::FromModifiedUTF...?
738 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
739 if (s.get() != nullptr) {
740 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
741
742 // cause.
743 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
744
745 // suppressedExceptions.
746 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
747 WellKnownClasses::java_util_Collections,
748 WellKnownClasses::java_util_Collections_EMPTY_LIST));
749 CHECK(emptylist.get() != nullptr);
750 env->SetObjectField(exc.get(),
751 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
752 emptylist.get());
753
754 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
755 // nativeFillInStackTrace.
756 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
757 {
758 ScopedObjectAccessUnchecked soa(env);
759 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
760 }
761 if (stack_state_val.get() != nullptr) {
762 env->SetObjectField(exc.get(),
763 WellKnownClasses::java_lang_Throwable_stackState,
764 stack_state_val.get());
765
766 // stackTrace.
767 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
768 WellKnownClasses::libcore_util_EmptyArray,
769 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
770 env->SetObjectField(exc.get(),
771 WellKnownClasses::java_lang_Throwable_stackTrace,
772 stack_trace_elem.get());
773 } else {
774 error_msg = "Could not create stack trace.";
775 }
776 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700777 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800778 } else {
779 // Could not allocate a string object.
780 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
781 }
782 } else {
783 error_msg = "Could not allocate StackOverflowError object.";
784 }
785
786 if (!error_msg.empty()) {
787 LOG(WARNING) << error_msg;
788 CHECK(self->IsExceptionPending());
789 }
790
791 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
792 self->ResetDefaultStackEnd(); // Return to default stack size.
793
794 // And restore protection if implicit checks are on.
795 if (!explicit_overflow_check) {
796 self->ProtectStack();
797 }
798}
799
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100800// StringIndexOutOfBoundsException
801
802void ThrowStringIndexOutOfBoundsException(int index, int length) {
803 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
804 StringPrintf("length=%d; index=%d", length, index).c_str());
805}
806
Ian Rogers62d6c772013-02-27 08:32:07 -0800807// VerifyError
808
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700809void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800810 va_list args;
811 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000812 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800813 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700814}
815
Narayan Kamath208f8572016-08-03 12:46:58 +0100816// WrongMethodTypeException
817
818void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
819 mirror::MethodType* callsite_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100820 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
821 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000822 StringPrintf("Expected %s but was %s",
823 callee_type->PrettyDescriptor().c_str(),
824 callsite_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100825}
826
Ian Rogers87e552d2012-08-31 15:54:48 -0700827} // namespace art