blob: 9b55ca50b211f485f3390c202669ebaf49f62a01 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_throws.h"
18
Ian Rogers22d5e732014-07-15 22:23:51 -070019#include <sstream>
20
Andreas Gampe57943812017-12-06 21:39:13 -080021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
Andreas Gampe103992b2016-01-04 15:32:43 -080023
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020028#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070029#include "invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010031#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/object-inl.h"
33#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070034#include "nativehelper/scoped_local_ref.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070035#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070036#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020037#include "verifier/method_verifier.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070038#include "well_known_classes.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070039
Ian Rogers87e552d2012-08-31 15:54:48 -070040namespace art {
41
Andreas Gampe46ee31b2016-12-14 10:11:49 -080042using android::base::StringAppendV;
43using android::base::StringPrintf;
44
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070045static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070046 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070048 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070049 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070050 os << " (declaration of '" << referrer->PrettyDescriptor()
51 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070052 }
53 }
54}
55
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000056static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070057 ObjPtr<mirror::Class> referrer,
58 const char* fmt,
59 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070061 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070062 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080063 std::string vmsg;
64 StringAppendV(&vmsg, fmt, *args);
65 msg << vmsg;
66 } else {
67 msg << fmt;
68 }
69 AddReferrerLocation(msg, referrer);
70 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000071 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070072}
73
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000074static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070075 ObjPtr<mirror::Class> referrer,
76 const char* fmt,
77 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070079 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070080 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070081 std::string vmsg;
82 StringAppendV(&vmsg, fmt, *args);
83 msg << vmsg;
84 } else {
85 msg << fmt;
86 }
87 AddReferrerLocation(msg, referrer);
88 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000089 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070090}
91
Sebastien Hertz56adf602013-07-09 17:27:07 +020092// AbstractMethodError
93
Mathieu Chartiere401d142015-04-22 13:56:20 -070094void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070095 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020096 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -070097 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +020098}
99
Alex Light705ad492015-09-21 11:36:30 -0700100void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
101 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
102 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700103 dex_file.PrettyMethod(method_idx,
104 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700105}
106
Ian Rogers62d6c772013-02-27 08:32:07 -0800107// ArithmeticException
108
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200109void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700110 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800111}
112
113// ArrayIndexOutOfBoundsException
114
115void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800117 StringPrintf("length=%d; index=%d", length, index).c_str());
118}
119
120// ArrayStoreException
121
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700122void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
123 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700124 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700126 mirror::Class::PrettyDescriptor(element_class).c_str(),
127 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800128}
129
Orion Hodsonc069a302017-01-18 09:23:12 +0000130// BootstrapMethodError
131
132void ThrowBootstrapMethodError(const char* fmt, ...) {
133 va_list args;
134 va_start(args, fmt);
135 ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
136 va_end(args);
137}
138
139void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
140 va_list args;
141 va_start(args, fmt);
142 ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
143 va_end(args);
144}
145
Ian Rogers62d6c772013-02-27 08:32:07 -0800146// ClassCastException
147
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700148void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700149 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800150 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700151 mirror::Class::PrettyDescriptor(src_type).c_str(),
152 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800153}
154
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000155void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800157}
158
159// ClassCircularityError
160
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700161void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800162 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700163 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000164 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800165}
166
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700167void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100168 va_list args;
169 va_start(args, fmt);
170 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
171 va_end(args);
172}
173
Ian Rogers62d6c772013-02-27 08:32:07 -0800174// ClassFormatError
175
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700176void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 va_list args;
178 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000179 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100180 va_end(args);
181}
Ian Rogers62d6c772013-02-27 08:32:07 -0800182
Ian Rogers87e552d2012-08-31 15:54:48 -0700183// IllegalAccessError
184
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700185void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700186 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700187 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
188 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000189 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700190}
191
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700192void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
193 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700195 InvokeType type) {
196 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700197 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
198 << "' attempting to access '"
199 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
200 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000201 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700202}
203
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700204void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700205 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700206 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
207 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000208 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700209}
210
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700211void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700212 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700213 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
214 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000215 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700216}
217
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700219 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700220 msg << "Final field '" << ArtField::PrettyField(accessed, false)
221 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000222 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800224 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700225}
226
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700227void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 va_list args;
229 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000230 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 va_end(args);
232}
233
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700234// IllegalAccessException
235
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000236void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700238}
239
Ian Rogers62d6c772013-02-27 08:32:07 -0800240// IllegalArgumentException
241
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000242void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700243 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800244}
245
246
Ian Rogers87e552d2012-08-31 15:54:48 -0700247// IncompatibleClassChangeError
248
249void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700251 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700252 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700253 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000254 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800256 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700257}
258
Alex Light705ad492015-09-21 11:36:30 -0700259void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700260 ObjPtr<mirror::Class> target_class,
261 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700262 ArtMethod* referrer) {
263 // Referrer is calling interface_method on this_object, however, the interface_method isn't
264 // implemented by this_object.
265 CHECK(this_object != nullptr);
266 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700267 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
268 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
269 << "' in call to '"
270 << ArtMethod::PrettyMethod(method) << "'";
Alex Light705ad492015-09-21 11:36:30 -0700271 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
272 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
273 msg.str().c_str());
274}
275
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700277 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700279 // Referrer is calling interface_method on this_object, however, the interface_method isn't
280 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700281 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700282 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700283 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700284 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700285 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
286 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000287 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700290}
291
Mathieu Chartierc7853442015-03-27 14:35:38 -0700292void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700294 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700295 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 << (is_static ? "static" : "instance") << " field" << " rather than a "
297 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 msg.str().c_str());
300}
301
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700302void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 va_list args;
304 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000305 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 va_end(args);
307}
308
Alex Light9139e002015-10-09 15:59:48 -0700309void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
310 DCHECK(method != nullptr);
311 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
312 /*referrer*/nullptr,
313 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700314 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700315}
316
Alex Lightdb01a092017-04-03 15:39:55 -0700317// InternalError
318
319void ThrowInternalError(const char* fmt, ...) {
320 va_list args;
321 va_start(args, fmt);
322 ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
323 va_end(args);
324}
Alex Light9139e002015-10-09 15:59:48 -0700325
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700326// IOException
327
328void ThrowIOException(const char* fmt, ...) {
329 va_list args;
330 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700331 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332 va_end(args);
333}
334
Andreas Gampe329d1882014-04-08 10:32:19 -0700335void ThrowWrappedIOException(const char* fmt, ...) {
336 va_list args;
337 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700338 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700339 va_end(args);
340}
341
Ian Rogers62d6c772013-02-27 08:32:07 -0800342// LinkageError
343
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700344void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800345 va_list args;
346 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000347 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 va_end(args);
349}
350
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700351void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100352 va_list args;
353 va_start(args, fmt);
354 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
355 va_end(args);
356}
357
Ian Rogers62d6c772013-02-27 08:32:07 -0800358// NegativeArraySizeException
359
360void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700361 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700362 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800363}
364
365void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700366 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800367}
368
369// NoSuchFieldError
370
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700371void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700372 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700374 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700376 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000377 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700378}
379
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700380void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700381 std::ostringstream msg;
382 std::string temp;
383 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
384 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
385}
386
Ian Rogers87e552d2012-08-31 15:54:48 -0700387// NoSuchMethodError
388
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700389void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700390 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700391 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700392 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700393 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700394 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000395 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700396}
397
Ian Rogers62d6c772013-02-27 08:32:07 -0800398// NullPointerException
399
Mathieu Chartierc7853442015-03-27 14:35:38 -0700400void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800401 std::ostringstream msg;
402 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700403 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700404 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800405}
406
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000407static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200408 const DexFile& dex_file,
409 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700410 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 std::ostringstream msg;
412 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700413 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700414 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800415}
416
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000417void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200418 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700419 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000420 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200421 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000422 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200423}
424
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200426 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700427 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200428 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000429 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200430 dex_file, type);
431}
432
Vladimir Marko953437b2016-08-24 08:30:46 +0000433static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
434 DCHECK(kEmitCompilerReadBarrier);
435 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
Vladimir Marko33bff252017-11-01 14:35:42 +0000436 if (kUseBakerReadBarrier &&
437 (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64)) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000438 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
439 monitor_offset += gray_byte_position;
440 }
441 return addr == monitor_offset;
442}
443
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100444static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700445 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100446 if (!CanDoImplicitNullCheckOn(addr)) {
447 return false;
448 }
449
450 switch (instr.Opcode()) {
451 case Instruction::INVOKE_DIRECT:
452 case Instruction::INVOKE_DIRECT_RANGE:
453 case Instruction::INVOKE_VIRTUAL:
454 case Instruction::INVOKE_VIRTUAL_RANGE:
455 case Instruction::INVOKE_INTERFACE:
456 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000457 case Instruction::INVOKE_POLYMORPHIC:
458 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100459 case Instruction::INVOKE_VIRTUAL_QUICK:
460 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
461 // Without inlining, we could just check that the offset is the class offset.
462 // However, when inlining, the compiler can (validly) merge the null check with a field access
463 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
464 // which is the caller.
465 return true;
466 }
467
Vladimir Marko953437b2016-08-24 08:30:46 +0000468 case Instruction::IGET_OBJECT:
469 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
470 return true;
471 }
472 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100473 case Instruction::IGET:
474 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100475 case Instruction::IGET_BOOLEAN:
476 case Instruction::IGET_BYTE:
477 case Instruction::IGET_CHAR:
478 case Instruction::IGET_SHORT:
479 case Instruction::IPUT:
480 case Instruction::IPUT_WIDE:
481 case Instruction::IPUT_OBJECT:
482 case Instruction::IPUT_BOOLEAN:
483 case Instruction::IPUT_BYTE:
484 case Instruction::IPUT_CHAR:
485 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100486 ArtField* field =
487 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000488 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100489 }
490
Vladimir Marko953437b2016-08-24 08:30:46 +0000491 case Instruction::IGET_OBJECT_QUICK:
492 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
493 return true;
494 }
495 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100496 case Instruction::IGET_QUICK:
497 case Instruction::IGET_BOOLEAN_QUICK:
498 case Instruction::IGET_BYTE_QUICK:
499 case Instruction::IGET_CHAR_QUICK:
500 case Instruction::IGET_SHORT_QUICK:
501 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100502 case Instruction::IPUT_QUICK:
503 case Instruction::IPUT_BOOLEAN_QUICK:
504 case Instruction::IPUT_BYTE_QUICK:
505 case Instruction::IPUT_CHAR_QUICK:
506 case Instruction::IPUT_SHORT_QUICK:
507 case Instruction::IPUT_WIDE_QUICK:
508 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000509 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100510 }
511
Vladimir Marko953437b2016-08-24 08:30:46 +0000512 case Instruction::AGET_OBJECT:
513 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
514 return true;
515 }
516 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100517 case Instruction::AGET:
518 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100519 case Instruction::AGET_BOOLEAN:
520 case Instruction::AGET_BYTE:
521 case Instruction::AGET_CHAR:
522 case Instruction::AGET_SHORT:
523 case Instruction::APUT:
524 case Instruction::APUT_WIDE:
525 case Instruction::APUT_OBJECT:
526 case Instruction::APUT_BOOLEAN:
527 case Instruction::APUT_BYTE:
528 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100529 case Instruction::APUT_SHORT:
530 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100531 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100532 // The length access should crash. We currently do not do implicit checks on
533 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000534 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100535 }
536
537 default: {
538 // We have covered all the cases where an NPE could occur.
539 // Note that this must be kept in sync with the compiler, and adding
540 // any new way to do implicit checks in the compiler should also update
541 // this code.
542 return false;
543 }
544 }
545}
546
547void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000548 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000550 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800551 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
552 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100553 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
554 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
555 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
556 << "0x" << std::hex << addr << std::dec
557 << ", at "
558 << instr->DumpString(dex_file)
559 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700560 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100561 }
562
Ian Rogers62d6c772013-02-27 08:32:07 -0800563 switch (instr->Opcode()) {
564 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000565 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200566 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800567 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000568 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800569 break;
570 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000571 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200572 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000574 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800575 break;
576 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000577 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200578 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800579 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000580 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800581 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000582 case Instruction::INVOKE_POLYMORPHIC:
583 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_45cc(), kVirtual);
584 break;
585 case Instruction::INVOKE_POLYMORPHIC_RANGE:
586 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_4rcc(), kVirtual);
587 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200588 case Instruction::INVOKE_VIRTUAL_QUICK:
589 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
590 // Since we replaced the method index, we ask the verifier to tell us which
591 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000593 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700594 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200595 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000596 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200597 } else {
598 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000599 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200600 }
601 break;
602 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800603 case Instruction::IGET:
604 case Instruction::IGET_WIDE:
605 case Instruction::IGET_OBJECT:
606 case Instruction::IGET_BOOLEAN:
607 case Instruction::IGET_BYTE:
608 case Instruction::IGET_CHAR:
609 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700610 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000611 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
612 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800613 break;
614 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200615 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800616 case Instruction::IGET_BOOLEAN_QUICK:
617 case Instruction::IGET_BYTE_QUICK:
618 case Instruction::IGET_CHAR_QUICK:
619 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200620 case Instruction::IGET_WIDE_QUICK:
621 case Instruction::IGET_OBJECT_QUICK: {
622 // Since we replaced the field index, we ask the verifier to tell us which
623 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700624 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000625 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700626 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200627 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000628 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200629 } else {
630 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000631 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200632 }
633 break;
634 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800635 case Instruction::IPUT:
636 case Instruction::IPUT_WIDE:
637 case Instruction::IPUT_OBJECT:
638 case Instruction::IPUT_BOOLEAN:
639 case Instruction::IPUT_BYTE:
640 case Instruction::IPUT_CHAR:
641 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700642 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000643 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
644 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800645 break;
646 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200647 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700648 case Instruction::IPUT_BOOLEAN_QUICK:
649 case Instruction::IPUT_BYTE_QUICK:
650 case Instruction::IPUT_CHAR_QUICK:
651 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200652 case Instruction::IPUT_WIDE_QUICK:
653 case Instruction::IPUT_OBJECT_QUICK: {
654 // Since we replaced the field index, we ask the verifier to tell us which
655 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700656 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000657 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700658 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200659 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000660 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200661 } else {
662 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000663 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200664 }
665 break;
666 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800667 case Instruction::AGET:
668 case Instruction::AGET_WIDE:
669 case Instruction::AGET_OBJECT:
670 case Instruction::AGET_BOOLEAN:
671 case Instruction::AGET_BYTE:
672 case Instruction::AGET_CHAR:
673 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700674 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800675 "Attempt to read from null array");
676 break;
677 case Instruction::APUT:
678 case Instruction::APUT_WIDE:
679 case Instruction::APUT_OBJECT:
680 case Instruction::APUT_BOOLEAN:
681 case Instruction::APUT_BYTE:
682 case Instruction::APUT_CHAR:
683 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700684 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800685 "Attempt to write to null array");
686 break;
687 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700688 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 "Attempt to get length of null array");
690 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100691 case Instruction::FILL_ARRAY_DATA: {
692 ThrowException("Ljava/lang/NullPointerException;", nullptr,
693 "Attempt to write to null array");
694 break;
695 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100696 case Instruction::MONITOR_ENTER:
697 case Instruction::MONITOR_EXIT: {
698 ThrowException("Ljava/lang/NullPointerException;", nullptr,
699 "Attempt to do a synchronize operation on a null object");
700 break;
701 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800702 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000703 const DexFile* dex_file =
704 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100705 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
706 << instr->DumpString(dex_file)
707 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700708 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800709 break;
710 }
711 }
712}
713
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000714void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700715 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800716}
717
718// RuntimeException
719
720void ThrowRuntimeException(const char* fmt, ...) {
721 va_list args;
722 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700723 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800724 va_end(args);
725}
726
Leonard Mosescueb842212016-10-06 17:26:36 -0700727// SecurityException
728
729void ThrowSecurityException(const char* fmt, ...) {
730 va_list args;
731 va_start(args, fmt);
732 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
733 va_end(args);
734}
735
Andreas Gampe103992b2016-01-04 15:32:43 -0800736// Stack overflow.
737
738void ThrowStackOverflowError(Thread* self) {
739 if (self->IsHandlingStackOverflow()) {
740 LOG(ERROR) << "Recursive stack overflow.";
741 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
742 }
743
744 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
745 JNIEnvExt* env = self->GetJniEnv();
746 std::string msg("stack size ");
747 msg += PrettySize(self->GetStackSize());
748
749 // Avoid running Java code for exception initialization.
750 // TODO: Checks to make this a bit less brittle.
751
752 std::string error_msg;
753
754 // Allocate an uninitialized object.
755 ScopedLocalRef<jobject> exc(env,
756 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
757 if (exc.get() != nullptr) {
758 // "Initialize".
759 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
760 // Only Throwable has "custom" fields:
761 // String detailMessage.
762 // Throwable cause (= this).
763 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
764 // Object stackState;
765 // StackTraceElement[] stackTrace;
766 // Only Throwable has a non-empty constructor:
767 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
768 // fillInStackTrace();
769
770 // detailMessage.
771 // TODO: Use String::FromModifiedUTF...?
772 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
773 if (s.get() != nullptr) {
774 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
775
776 // cause.
777 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
778
779 // suppressedExceptions.
780 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
781 WellKnownClasses::java_util_Collections,
782 WellKnownClasses::java_util_Collections_EMPTY_LIST));
783 CHECK(emptylist.get() != nullptr);
784 env->SetObjectField(exc.get(),
785 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
786 emptylist.get());
787
788 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
789 // nativeFillInStackTrace.
790 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
791 {
792 ScopedObjectAccessUnchecked soa(env);
793 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
794 }
795 if (stack_state_val.get() != nullptr) {
796 env->SetObjectField(exc.get(),
797 WellKnownClasses::java_lang_Throwable_stackState,
798 stack_state_val.get());
799
800 // stackTrace.
801 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
802 WellKnownClasses::libcore_util_EmptyArray,
803 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
804 env->SetObjectField(exc.get(),
805 WellKnownClasses::java_lang_Throwable_stackTrace,
806 stack_trace_elem.get());
807 } else {
808 error_msg = "Could not create stack trace.";
809 }
810 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700811 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800812 } else {
813 // Could not allocate a string object.
814 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
815 }
816 } else {
817 error_msg = "Could not allocate StackOverflowError object.";
818 }
819
820 if (!error_msg.empty()) {
821 LOG(WARNING) << error_msg;
822 CHECK(self->IsExceptionPending());
823 }
824
825 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
826 self->ResetDefaultStackEnd(); // Return to default stack size.
827
828 // And restore protection if implicit checks are on.
829 if (!explicit_overflow_check) {
830 self->ProtectStack();
831 }
832}
833
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100834// StringIndexOutOfBoundsException
835
836void ThrowStringIndexOutOfBoundsException(int index, int length) {
837 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
838 StringPrintf("length=%d; index=%d", length, index).c_str());
839}
840
Ian Rogers62d6c772013-02-27 08:32:07 -0800841// VerifyError
842
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700843void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800844 va_list args;
845 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000846 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800847 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700848}
849
Narayan Kamath208f8572016-08-03 12:46:58 +0100850// WrongMethodTypeException
851
852void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
853 mirror::MethodType* callsite_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100854 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
855 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000856 StringPrintf("Expected %s but was %s",
857 callee_type->PrettyDescriptor().c_str(),
858 callsite_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100859}
860
Ian Rogers87e552d2012-08-31 15:54:48 -0700861} // namespace art