blob: a46f531d93336892892b0c3b68297d49beb51e77 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_throws.h"
18
Ian Rogers22d5e732014-07-15 22:23:51 -070019#include <sstream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
Andreas Gampe103992b2016-01-04 15:32:43 -080022
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.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"
Steven Morelande431e272017-07-18 16:53:49 -070034#include "nativehelper/ScopedLocalRef.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070035#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070036#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020037#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070038
Ian Rogers87e552d2012-08-31 15:54:48 -070039namespace art {
40
Andreas Gampe46ee31b2016-12-14 10:11:49 -080041using android::base::StringAppendV;
42using android::base::StringPrintf;
43
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070044static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070045 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070047 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070048 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070049 os << " (declaration of '" << referrer->PrettyDescriptor()
50 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070051 }
52 }
53}
54
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000055static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070056 ObjPtr<mirror::Class> referrer,
57 const char* fmt,
58 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070060 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070061 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080062 std::string vmsg;
63 StringAppendV(&vmsg, fmt, *args);
64 msg << vmsg;
65 } else {
66 msg << fmt;
67 }
68 AddReferrerLocation(msg, referrer);
69 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000070 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070071}
72
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000073static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070074 ObjPtr<mirror::Class> referrer,
75 const char* fmt,
76 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070078 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070080 std::string vmsg;
81 StringAppendV(&vmsg, fmt, *args);
82 msg << vmsg;
83 } else {
84 msg << fmt;
85 }
86 AddReferrerLocation(msg, referrer);
87 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000088 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070089}
90
Sebastien Hertz56adf602013-07-09 17:27:07 +020091// AbstractMethodError
92
Mathieu Chartiere401d142015-04-22 13:56:20 -070093void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020095 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -070096 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +020097}
98
Alex Light705ad492015-09-21 11:36:30 -070099void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
100 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
101 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700102 dex_file.PrettyMethod(method_idx,
103 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700104}
105
Ian Rogers62d6c772013-02-27 08:32:07 -0800106// ArithmeticException
107
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200108void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800110}
111
112// ArrayIndexOutOfBoundsException
113
114void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 StringPrintf("length=%d; index=%d", length, index).c_str());
117}
118
119// ArrayStoreException
120
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700121void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
122 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700123 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700125 mirror::Class::PrettyDescriptor(element_class).c_str(),
126 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800127}
128
Orion Hodsonc069a302017-01-18 09:23:12 +0000129// BootstrapMethodError
130
131void ThrowBootstrapMethodError(const char* fmt, ...) {
132 va_list args;
133 va_start(args, fmt);
134 ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
135 va_end(args);
136}
137
138void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
139 va_list args;
140 va_start(args, fmt);
141 ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
142 va_end(args);
143}
144
Ian Rogers62d6c772013-02-27 08:32:07 -0800145// ClassCastException
146
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700147void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700148 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800149 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700150 mirror::Class::PrettyDescriptor(src_type).c_str(),
151 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800152}
153
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000154void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800156}
157
158// ClassCircularityError
159
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700160void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800161 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700162 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000163 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800164}
165
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700166void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100167 va_list args;
168 va_start(args, fmt);
169 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
170 va_end(args);
171}
172
Ian Rogers62d6c772013-02-27 08:32:07 -0800173// ClassFormatError
174
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700175void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 va_list args;
177 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000178 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100179 va_end(args);
180}
Ian Rogers62d6c772013-02-27 08:32:07 -0800181
Ian Rogers87e552d2012-08-31 15:54:48 -0700182// IllegalAccessError
183
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700184void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700185 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700186 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
187 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000188 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700189}
190
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700191void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
192 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700193 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700194 InvokeType type) {
195 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700196 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
197 << "' attempting to access '"
198 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
199 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000200 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700201}
202
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700203void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700204 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700205 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
206 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000207 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700208}
209
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700210void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700211 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700212 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
213 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000214 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700215}
216
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700218 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700219 msg << "Final field '" << ArtField::PrettyField(accessed, false)
220 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000221 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800223 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700224}
225
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700226void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800227 va_list args;
228 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000229 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800230 va_end(args);
231}
232
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700233// IllegalAccessException
234
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000235void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700236 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700237}
238
Ian Rogers62d6c772013-02-27 08:32:07 -0800239// IllegalArgumentException
240
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000241void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700242 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800243}
244
245
Ian Rogers87e552d2012-08-31 15:54:48 -0700246// IncompatibleClassChangeError
247
248void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700250 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700251 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700252 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000253 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800255 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700256}
257
Alex Light705ad492015-09-21 11:36:30 -0700258void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700259 ObjPtr<mirror::Class> target_class,
260 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700261 ArtMethod* referrer) {
262 // Referrer is calling interface_method on this_object, however, the interface_method isn't
263 // implemented by this_object.
264 CHECK(this_object != nullptr);
265 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700266 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
267 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
268 << "' in call to '"
269 << ArtMethod::PrettyMethod(method) << "'";
Alex Light705ad492015-09-21 11:36:30 -0700270 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
271 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
272 msg.str().c_str());
273}
274
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700276 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700278 // Referrer is calling interface_method on this_object, however, the interface_method isn't
279 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700280 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700281 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700282 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700283 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700284 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
285 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000286 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700287 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700289}
290
Mathieu Chartierc7853442015-03-27 14:35:38 -0700291void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700293 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700294 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700295 << (is_static ? "static" : "instance") << " field" << " rather than a "
296 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800298 msg.str().c_str());
299}
300
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700301void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 va_list args;
303 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000304 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 va_end(args);
306}
307
Alex Light9139e002015-10-09 15:59:48 -0700308void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
309 DCHECK(method != nullptr);
310 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
311 /*referrer*/nullptr,
312 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700313 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700314}
315
Alex Lightdb01a092017-04-03 15:39:55 -0700316// InternalError
317
318void ThrowInternalError(const char* fmt, ...) {
319 va_list args;
320 va_start(args, fmt);
321 ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
322 va_end(args);
323}
Alex Light9139e002015-10-09 15:59:48 -0700324
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700325// IOException
326
327void ThrowIOException(const char* fmt, ...) {
328 va_list args;
329 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700330 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700331 va_end(args);
332}
333
Andreas Gampe329d1882014-04-08 10:32:19 -0700334void ThrowWrappedIOException(const char* fmt, ...) {
335 va_list args;
336 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700337 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700338 va_end(args);
339}
340
Ian Rogers62d6c772013-02-27 08:32:07 -0800341// LinkageError
342
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700343void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 va_list args;
345 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000346 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800347 va_end(args);
348}
349
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700350void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100351 va_list args;
352 va_start(args, fmt);
353 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
354 va_end(args);
355}
356
Ian Rogers62d6c772013-02-27 08:32:07 -0800357// NegativeArraySizeException
358
359void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700360 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700361 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800362}
363
364void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700365 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800366}
367
368// NoSuchFieldError
369
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700370void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700371 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700373 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700375 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000376 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700377}
378
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700379void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700380 std::ostringstream msg;
381 std::string temp;
382 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
383 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
384}
385
Ian Rogers87e552d2012-08-31 15:54:48 -0700386// NoSuchMethodError
387
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700388void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700389 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700390 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700391 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700392 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700393 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000394 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700395}
396
Ian Rogers62d6c772013-02-27 08:32:07 -0800397// NullPointerException
398
Mathieu Chartierc7853442015-03-27 14:35:38 -0700399void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800400 std::ostringstream msg;
401 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700402 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700403 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800404}
405
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000406static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200407 const DexFile& dex_file,
408 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700409 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 std::ostringstream msg;
411 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700412 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700413 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800414}
415
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000416void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200417 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700418 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000419 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200420 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000421 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200422}
423
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200425 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700426 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200427 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000428 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200429 dex_file, type);
430}
431
Vladimir Marko953437b2016-08-24 08:30:46 +0000432static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
433 DCHECK(kEmitCompilerReadBarrier);
434 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
435 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
436 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
437 monitor_offset += gray_byte_position;
438 }
439 return addr == monitor_offset;
440}
441
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100442static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700443 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100444 if (!CanDoImplicitNullCheckOn(addr)) {
445 return false;
446 }
447
448 switch (instr.Opcode()) {
449 case Instruction::INVOKE_DIRECT:
450 case Instruction::INVOKE_DIRECT_RANGE:
451 case Instruction::INVOKE_VIRTUAL:
452 case Instruction::INVOKE_VIRTUAL_RANGE:
453 case Instruction::INVOKE_INTERFACE:
454 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000455 case Instruction::INVOKE_POLYMORPHIC:
456 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100457 case Instruction::INVOKE_VIRTUAL_QUICK:
458 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
459 // Without inlining, we could just check that the offset is the class offset.
460 // However, when inlining, the compiler can (validly) merge the null check with a field access
461 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
462 // which is the caller.
463 return true;
464 }
465
Vladimir Marko953437b2016-08-24 08:30:46 +0000466 case Instruction::IGET_OBJECT:
467 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
468 return true;
469 }
470 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100471 case Instruction::IGET:
472 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100473 case Instruction::IGET_BOOLEAN:
474 case Instruction::IGET_BYTE:
475 case Instruction::IGET_CHAR:
476 case Instruction::IGET_SHORT:
477 case Instruction::IPUT:
478 case Instruction::IPUT_WIDE:
479 case Instruction::IPUT_OBJECT:
480 case Instruction::IPUT_BOOLEAN:
481 case Instruction::IPUT_BYTE:
482 case Instruction::IPUT_CHAR:
483 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100484 ArtField* field =
485 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000486 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100487 }
488
Vladimir Marko953437b2016-08-24 08:30:46 +0000489 case Instruction::IGET_OBJECT_QUICK:
490 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
491 return true;
492 }
493 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100494 case Instruction::IGET_QUICK:
495 case Instruction::IGET_BOOLEAN_QUICK:
496 case Instruction::IGET_BYTE_QUICK:
497 case Instruction::IGET_CHAR_QUICK:
498 case Instruction::IGET_SHORT_QUICK:
499 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100500 case Instruction::IPUT_QUICK:
501 case Instruction::IPUT_BOOLEAN_QUICK:
502 case Instruction::IPUT_BYTE_QUICK:
503 case Instruction::IPUT_CHAR_QUICK:
504 case Instruction::IPUT_SHORT_QUICK:
505 case Instruction::IPUT_WIDE_QUICK:
506 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000507 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100508 }
509
Vladimir Marko953437b2016-08-24 08:30:46 +0000510 case Instruction::AGET_OBJECT:
511 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
512 return true;
513 }
514 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100515 case Instruction::AGET:
516 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100517 case Instruction::AGET_BOOLEAN:
518 case Instruction::AGET_BYTE:
519 case Instruction::AGET_CHAR:
520 case Instruction::AGET_SHORT:
521 case Instruction::APUT:
522 case Instruction::APUT_WIDE:
523 case Instruction::APUT_OBJECT:
524 case Instruction::APUT_BOOLEAN:
525 case Instruction::APUT_BYTE:
526 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100527 case Instruction::APUT_SHORT:
528 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100529 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100530 // The length access should crash. We currently do not do implicit checks on
531 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000532 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100533 }
534
535 default: {
536 // We have covered all the cases where an NPE could occur.
537 // Note that this must be kept in sync with the compiler, and adding
538 // any new way to do implicit checks in the compiler should also update
539 // this code.
540 return false;
541 }
542 }
543}
544
545void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000546 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700547 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000548 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800549 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
550 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100551 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
552 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
553 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
554 << "0x" << std::hex << addr << std::dec
555 << ", at "
556 << instr->DumpString(dex_file)
557 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700558 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100559 }
560
Ian Rogers62d6c772013-02-27 08:32:07 -0800561 switch (instr->Opcode()) {
562 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000563 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200564 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000566 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800567 break;
568 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000569 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200570 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800571 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000572 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 break;
574 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000575 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200576 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800577 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000578 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800579 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000580 case Instruction::INVOKE_POLYMORPHIC:
581 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_45cc(), kVirtual);
582 break;
583 case Instruction::INVOKE_POLYMORPHIC_RANGE:
584 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_4rcc(), kVirtual);
585 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200586 case Instruction::INVOKE_VIRTUAL_QUICK:
587 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
588 // Since we replaced the method index, we ask the verifier to tell us which
589 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000591 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700592 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200593 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000594 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200595 } else {
596 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000597 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200598 }
599 break;
600 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800601 case Instruction::IGET:
602 case Instruction::IGET_WIDE:
603 case Instruction::IGET_OBJECT:
604 case Instruction::IGET_BOOLEAN:
605 case Instruction::IGET_BYTE:
606 case Instruction::IGET_CHAR:
607 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700608 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000609 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
610 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800611 break;
612 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200613 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800614 case Instruction::IGET_BOOLEAN_QUICK:
615 case Instruction::IGET_BYTE_QUICK:
616 case Instruction::IGET_CHAR_QUICK:
617 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200618 case Instruction::IGET_WIDE_QUICK:
619 case Instruction::IGET_OBJECT_QUICK: {
620 // Since we replaced the field index, we ask the verifier to tell us which
621 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700622 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000623 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700624 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200625 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000626 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200627 } else {
628 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000629 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200630 }
631 break;
632 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800633 case Instruction::IPUT:
634 case Instruction::IPUT_WIDE:
635 case Instruction::IPUT_OBJECT:
636 case Instruction::IPUT_BOOLEAN:
637 case Instruction::IPUT_BYTE:
638 case Instruction::IPUT_CHAR:
639 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700640 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000641 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
642 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800643 break;
644 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200645 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700646 case Instruction::IPUT_BOOLEAN_QUICK:
647 case Instruction::IPUT_BYTE_QUICK:
648 case Instruction::IPUT_CHAR_QUICK:
649 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200650 case Instruction::IPUT_WIDE_QUICK:
651 case Instruction::IPUT_OBJECT_QUICK: {
652 // Since we replaced the field index, we ask the verifier to tell us which
653 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700654 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000655 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700656 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200657 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000658 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200659 } else {
660 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000661 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200662 }
663 break;
664 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 case Instruction::AGET:
666 case Instruction::AGET_WIDE:
667 case Instruction::AGET_OBJECT:
668 case Instruction::AGET_BOOLEAN:
669 case Instruction::AGET_BYTE:
670 case Instruction::AGET_CHAR:
671 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700672 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800673 "Attempt to read from null array");
674 break;
675 case Instruction::APUT:
676 case Instruction::APUT_WIDE:
677 case Instruction::APUT_OBJECT:
678 case Instruction::APUT_BOOLEAN:
679 case Instruction::APUT_BYTE:
680 case Instruction::APUT_CHAR:
681 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700682 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800683 "Attempt to write to null array");
684 break;
685 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700686 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800687 "Attempt to get length of null array");
688 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100689 case Instruction::FILL_ARRAY_DATA: {
690 ThrowException("Ljava/lang/NullPointerException;", nullptr,
691 "Attempt to write to null array");
692 break;
693 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100694 case Instruction::MONITOR_ENTER:
695 case Instruction::MONITOR_EXIT: {
696 ThrowException("Ljava/lang/NullPointerException;", nullptr,
697 "Attempt to do a synchronize operation on a null object");
698 break;
699 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800700 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000701 const DexFile* dex_file =
702 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100703 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
704 << instr->DumpString(dex_file)
705 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700706 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800707 break;
708 }
709 }
710}
711
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000712void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700713 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800714}
715
716// RuntimeException
717
718void ThrowRuntimeException(const char* fmt, ...) {
719 va_list args;
720 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700721 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800722 va_end(args);
723}
724
Leonard Mosescueb842212016-10-06 17:26:36 -0700725// SecurityException
726
727void ThrowSecurityException(const char* fmt, ...) {
728 va_list args;
729 va_start(args, fmt);
730 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
731 va_end(args);
732}
733
Andreas Gampe103992b2016-01-04 15:32:43 -0800734// Stack overflow.
735
736void ThrowStackOverflowError(Thread* self) {
737 if (self->IsHandlingStackOverflow()) {
738 LOG(ERROR) << "Recursive stack overflow.";
739 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
740 }
741
742 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
743 JNIEnvExt* env = self->GetJniEnv();
744 std::string msg("stack size ");
745 msg += PrettySize(self->GetStackSize());
746
747 // Avoid running Java code for exception initialization.
748 // TODO: Checks to make this a bit less brittle.
749
750 std::string error_msg;
751
752 // Allocate an uninitialized object.
753 ScopedLocalRef<jobject> exc(env,
754 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
755 if (exc.get() != nullptr) {
756 // "Initialize".
757 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
758 // Only Throwable has "custom" fields:
759 // String detailMessage.
760 // Throwable cause (= this).
761 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
762 // Object stackState;
763 // StackTraceElement[] stackTrace;
764 // Only Throwable has a non-empty constructor:
765 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
766 // fillInStackTrace();
767
768 // detailMessage.
769 // TODO: Use String::FromModifiedUTF...?
770 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
771 if (s.get() != nullptr) {
772 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
773
774 // cause.
775 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
776
777 // suppressedExceptions.
778 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
779 WellKnownClasses::java_util_Collections,
780 WellKnownClasses::java_util_Collections_EMPTY_LIST));
781 CHECK(emptylist.get() != nullptr);
782 env->SetObjectField(exc.get(),
783 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
784 emptylist.get());
785
786 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
787 // nativeFillInStackTrace.
788 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
789 {
790 ScopedObjectAccessUnchecked soa(env);
791 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
792 }
793 if (stack_state_val.get() != nullptr) {
794 env->SetObjectField(exc.get(),
795 WellKnownClasses::java_lang_Throwable_stackState,
796 stack_state_val.get());
797
798 // stackTrace.
799 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
800 WellKnownClasses::libcore_util_EmptyArray,
801 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
802 env->SetObjectField(exc.get(),
803 WellKnownClasses::java_lang_Throwable_stackTrace,
804 stack_trace_elem.get());
805 } else {
806 error_msg = "Could not create stack trace.";
807 }
808 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700809 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800810 } else {
811 // Could not allocate a string object.
812 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
813 }
814 } else {
815 error_msg = "Could not allocate StackOverflowError object.";
816 }
817
818 if (!error_msg.empty()) {
819 LOG(WARNING) << error_msg;
820 CHECK(self->IsExceptionPending());
821 }
822
823 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
824 self->ResetDefaultStackEnd(); // Return to default stack size.
825
826 // And restore protection if implicit checks are on.
827 if (!explicit_overflow_check) {
828 self->ProtectStack();
829 }
830}
831
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100832// StringIndexOutOfBoundsException
833
834void ThrowStringIndexOutOfBoundsException(int index, int length) {
835 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
836 StringPrintf("length=%d; index=%d", length, index).c_str());
837}
838
Ian Rogers62d6c772013-02-27 08:32:07 -0800839// VerifyError
840
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700841void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800842 va_list args;
843 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000844 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800845 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700846}
847
Narayan Kamath208f8572016-08-03 12:46:58 +0100848// WrongMethodTypeException
849
850void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
851 mirror::MethodType* callsite_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100852 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
853 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000854 StringPrintf("Expected %s but was %s",
855 callee_type->PrettyDescriptor().c_str(),
856 callsite_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100857}
858
Ian Rogers87e552d2012-08-31 15:54:48 -0700859} // namespace art