blob: 92d86519dc9ad1d6105f9eb0c9efcf3f0b464233 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_throws.h"
18
Ian Rogers22d5e732014-07-15 22:23:51 -070019#include <sstream>
20
Andreas Gampe57943812017-12-06 21:39:13 -080021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
Andreas Gampe103992b2016-01-04 15:32:43 -080023
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "class_linker-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file-inl.h"
28#include "dex/dex_instruction-inl.h"
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 Geoffray13449142017-12-07 22:26:24 +0000444static bool IsValidImplicitCheck(uintptr_t addr, 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 Geoffray13449142017-12-07 22:26:24 +0000486 // We might be doing an implicit null check with an offset that doesn't correspond
487 // to the instruction, for example with two field accesses and the first one being
488 // eliminated or re-ordered.
489 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100490 }
491
Vladimir Marko953437b2016-08-24 08:30:46 +0000492 case Instruction::IGET_OBJECT_QUICK:
493 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
494 return true;
495 }
496 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100497 case Instruction::IGET_QUICK:
498 case Instruction::IGET_BOOLEAN_QUICK:
499 case Instruction::IGET_BYTE_QUICK:
500 case Instruction::IGET_CHAR_QUICK:
501 case Instruction::IGET_SHORT_QUICK:
502 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100503 case Instruction::IPUT_QUICK:
504 case Instruction::IPUT_BOOLEAN_QUICK:
505 case Instruction::IPUT_BYTE_QUICK:
506 case Instruction::IPUT_CHAR_QUICK:
507 case Instruction::IPUT_SHORT_QUICK:
508 case Instruction::IPUT_WIDE_QUICK:
509 case Instruction::IPUT_OBJECT_QUICK: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000510 // We might be doing an implicit null check with an offset that doesn't correspond
511 // to the instruction, for example with two field accesses and the first one being
512 // eliminated or re-ordered.
513 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100514 }
515
Vladimir Marko953437b2016-08-24 08:30:46 +0000516 case Instruction::AGET_OBJECT:
517 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
518 return true;
519 }
520 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100521 case Instruction::AGET:
522 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100523 case Instruction::AGET_BOOLEAN:
524 case Instruction::AGET_BYTE:
525 case Instruction::AGET_CHAR:
526 case Instruction::AGET_SHORT:
527 case Instruction::APUT:
528 case Instruction::APUT_WIDE:
529 case Instruction::APUT_OBJECT:
530 case Instruction::APUT_BOOLEAN:
531 case Instruction::APUT_BYTE:
532 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100533 case Instruction::APUT_SHORT:
534 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100535 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100536 // The length access should crash. We currently do not do implicit checks on
537 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000538 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100539 }
540
541 default: {
542 // We have covered all the cases where an NPE could occur.
543 // Note that this must be kept in sync with the compiler, and adding
544 // any new way to do implicit checks in the compiler should also update
545 // this code.
546 return false;
547 }
548 }
549}
550
551void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000552 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800554 CodeItemInstructionAccessor accessor(method);
555 CHECK_LT(throw_dex_pc, accessor.InsnsSizeInCodeUnits());
556 const Instruction& instr = accessor.InstructionAt(throw_dex_pc);
557 if (check_address && !IsValidImplicitCheck(addr, instr)) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100558 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
559 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
560 << "0x" << std::hex << addr << std::dec
561 << ", at "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800562 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100563 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700564 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100565 }
566
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800567 switch (instr.Opcode()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800568 case Instruction::INVOKE_DIRECT:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800569 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200570 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800571 case Instruction::INVOKE_DIRECT_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800572 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 break;
574 case Instruction::INVOKE_VIRTUAL:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800575 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200576 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800577 case Instruction::INVOKE_VIRTUAL_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800578 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800579 break;
580 case Instruction::INVOKE_INTERFACE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800581 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200582 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800583 case Instruction::INVOKE_INTERFACE_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800584 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800585 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000586 case Instruction::INVOKE_POLYMORPHIC:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800587 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_45cc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000588 break;
589 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800590 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_4rcc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000591 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200592 case Instruction::INVOKE_VIRTUAL_QUICK:
593 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
594 // Since we replaced the method index, we ask the verifier to tell us which
595 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700596 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000597 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700598 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200599 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000600 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200601 } else {
602 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000603 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200604 }
605 break;
606 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800607 case Instruction::IGET:
608 case Instruction::IGET_WIDE:
609 case Instruction::IGET_OBJECT:
610 case Instruction::IGET_BOOLEAN:
611 case Instruction::IGET_BYTE:
612 case Instruction::IGET_CHAR:
613 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700614 ArtField* field =
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800615 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000616 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800617 break;
618 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200619 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800620 case Instruction::IGET_BOOLEAN_QUICK:
621 case Instruction::IGET_BYTE_QUICK:
622 case Instruction::IGET_CHAR_QUICK:
623 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200624 case Instruction::IGET_WIDE_QUICK:
625 case Instruction::IGET_OBJECT_QUICK: {
626 // Since we replaced the field index, we ask the verifier to tell us which
627 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700628 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000629 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700630 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200631 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000632 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200633 } else {
634 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000635 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200636 }
637 break;
638 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 case Instruction::IPUT:
640 case Instruction::IPUT_WIDE:
641 case Instruction::IPUT_OBJECT:
642 case Instruction::IPUT_BOOLEAN:
643 case Instruction::IPUT_BYTE:
644 case Instruction::IPUT_CHAR:
645 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700646 ArtField* field =
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800647 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000648 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800649 break;
650 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200651 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700652 case Instruction::IPUT_BOOLEAN_QUICK:
653 case Instruction::IPUT_BYTE_QUICK:
654 case Instruction::IPUT_CHAR_QUICK:
655 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200656 case Instruction::IPUT_WIDE_QUICK:
657 case Instruction::IPUT_OBJECT_QUICK: {
658 // Since we replaced the field index, we ask the verifier to tell us which
659 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700660 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000661 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700662 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200663 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000664 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200665 } else {
666 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000667 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200668 }
669 break;
670 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800671 case Instruction::AGET:
672 case Instruction::AGET_WIDE:
673 case Instruction::AGET_OBJECT:
674 case Instruction::AGET_BOOLEAN:
675 case Instruction::AGET_BYTE:
676 case Instruction::AGET_CHAR:
677 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700678 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800679 "Attempt to read from null array");
680 break;
681 case Instruction::APUT:
682 case Instruction::APUT_WIDE:
683 case Instruction::APUT_OBJECT:
684 case Instruction::APUT_BOOLEAN:
685 case Instruction::APUT_BYTE:
686 case Instruction::APUT_CHAR:
687 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700688 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 "Attempt to write to null array");
690 break;
691 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700692 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800693 "Attempt to get length of null array");
694 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100695 case Instruction::FILL_ARRAY_DATA: {
696 ThrowException("Ljava/lang/NullPointerException;", nullptr,
697 "Attempt to write to null array");
698 break;
699 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100700 case Instruction::MONITOR_ENTER:
701 case Instruction::MONITOR_EXIT: {
702 ThrowException("Ljava/lang/NullPointerException;", nullptr,
703 "Attempt to do a synchronize operation on a null object");
704 break;
705 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800706 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000707 const DexFile* dex_file =
708 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100709 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800710 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100711 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700712 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800713 break;
714 }
715 }
716}
717
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000718void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700719 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800720}
721
722// RuntimeException
723
724void ThrowRuntimeException(const char* fmt, ...) {
725 va_list args;
726 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700727 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800728 va_end(args);
729}
730
Leonard Mosescueb842212016-10-06 17:26:36 -0700731// SecurityException
732
733void ThrowSecurityException(const char* fmt, ...) {
734 va_list args;
735 va_start(args, fmt);
736 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
737 va_end(args);
738}
739
Andreas Gampe103992b2016-01-04 15:32:43 -0800740// Stack overflow.
741
742void ThrowStackOverflowError(Thread* self) {
743 if (self->IsHandlingStackOverflow()) {
744 LOG(ERROR) << "Recursive stack overflow.";
745 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
746 }
747
748 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
749 JNIEnvExt* env = self->GetJniEnv();
750 std::string msg("stack size ");
751 msg += PrettySize(self->GetStackSize());
752
753 // Avoid running Java code for exception initialization.
754 // TODO: Checks to make this a bit less brittle.
755
756 std::string error_msg;
757
758 // Allocate an uninitialized object.
759 ScopedLocalRef<jobject> exc(env,
760 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
761 if (exc.get() != nullptr) {
762 // "Initialize".
763 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
764 // Only Throwable has "custom" fields:
765 // String detailMessage.
766 // Throwable cause (= this).
767 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
768 // Object stackState;
769 // StackTraceElement[] stackTrace;
770 // Only Throwable has a non-empty constructor:
771 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
772 // fillInStackTrace();
773
774 // detailMessage.
775 // TODO: Use String::FromModifiedUTF...?
776 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
777 if (s.get() != nullptr) {
778 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
779
780 // cause.
781 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
782
783 // suppressedExceptions.
784 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
785 WellKnownClasses::java_util_Collections,
786 WellKnownClasses::java_util_Collections_EMPTY_LIST));
787 CHECK(emptylist.get() != nullptr);
788 env->SetObjectField(exc.get(),
789 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
790 emptylist.get());
791
792 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
793 // nativeFillInStackTrace.
794 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
795 {
796 ScopedObjectAccessUnchecked soa(env);
797 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
798 }
799 if (stack_state_val.get() != nullptr) {
800 env->SetObjectField(exc.get(),
801 WellKnownClasses::java_lang_Throwable_stackState,
802 stack_state_val.get());
803
804 // stackTrace.
805 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
806 WellKnownClasses::libcore_util_EmptyArray,
807 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
808 env->SetObjectField(exc.get(),
809 WellKnownClasses::java_lang_Throwable_stackTrace,
810 stack_trace_elem.get());
811 } else {
812 error_msg = "Could not create stack trace.";
813 }
814 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700815 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800816 } else {
817 // Could not allocate a string object.
818 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
819 }
820 } else {
821 error_msg = "Could not allocate StackOverflowError object.";
822 }
823
824 if (!error_msg.empty()) {
825 LOG(WARNING) << error_msg;
826 CHECK(self->IsExceptionPending());
827 }
828
829 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
830 self->ResetDefaultStackEnd(); // Return to default stack size.
831
832 // And restore protection if implicit checks are on.
833 if (!explicit_overflow_check) {
834 self->ProtectStack();
835 }
836}
837
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100838// StringIndexOutOfBoundsException
839
840void ThrowStringIndexOutOfBoundsException(int index, int length) {
841 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
842 StringPrintf("length=%d; index=%d", length, index).c_str());
843}
844
Ian Rogers62d6c772013-02-27 08:32:07 -0800845// VerifyError
846
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700847void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800848 va_list args;
849 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000850 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800851 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700852}
853
Narayan Kamath208f8572016-08-03 12:46:58 +0100854// WrongMethodTypeException
855
856void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
857 mirror::MethodType* callsite_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100858 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
859 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000860 StringPrintf("Expected %s but was %s",
861 callee_type->PrettyDescriptor().c_str(),
862 callsite_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100863}
864
Ian Rogers87e552d2012-08-31 15:54:48 -0700865} // namespace art