blob: 4f4bed0169aa8a0e1a5e8b0a593928eb1f233364 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_throws.h"
18
Ian Rogers22d5e732014-07-15 22:23:51 -070019#include <sstream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
Andreas Gampe103992b2016-01-04 15:32:43 -080022#include "ScopedLocalRef.h"
23
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020029#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070030#include "invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010032#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070035#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070036#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020037#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070038
Ian Rogers87e552d2012-08-31 15:54:48 -070039namespace art {
40
Andreas Gampe46ee31b2016-12-14 10:11:49 -080041using android::base::StringAppendV;
42using android::base::StringPrintf;
43
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070044static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070045 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070047 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070048 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070049 os << " (declaration of '" << referrer->PrettyDescriptor()
50 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070051 }
52 }
53}
54
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000055static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070056 ObjPtr<mirror::Class> referrer,
57 const char* fmt,
58 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070060 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070061 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080062 std::string vmsg;
63 StringAppendV(&vmsg, fmt, *args);
64 msg << vmsg;
65 } else {
66 msg << fmt;
67 }
68 AddReferrerLocation(msg, referrer);
69 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000070 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070071}
72
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000073static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070074 ObjPtr<mirror::Class> referrer,
75 const char* fmt,
76 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070078 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070080 std::string vmsg;
81 StringAppendV(&vmsg, fmt, *args);
82 msg << vmsg;
83 } else {
84 msg << fmt;
85 }
86 AddReferrerLocation(msg, referrer);
87 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000088 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070089}
90
Sebastien Hertz56adf602013-07-09 17:27:07 +020091// AbstractMethodError
92
Mathieu Chartiere401d142015-04-22 13:56:20 -070093void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020095 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -070096 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +020097}
98
Alex Light705ad492015-09-21 11:36:30 -070099void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
100 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
101 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700102 dex_file.PrettyMethod(method_idx,
103 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700104}
105
Ian Rogers62d6c772013-02-27 08:32:07 -0800106// ArithmeticException
107
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200108void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800110}
111
112// ArrayIndexOutOfBoundsException
113
114void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 StringPrintf("length=%d; index=%d", length, index).c_str());
117}
118
119// ArrayStoreException
120
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700121void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
122 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700123 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700125 mirror::Class::PrettyDescriptor(element_class).c_str(),
126 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800127}
128
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
316
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700317// IOException
318
319void ThrowIOException(const char* fmt, ...) {
320 va_list args;
321 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700322 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700323 va_end(args);
324}
325
Andreas Gampe329d1882014-04-08 10:32:19 -0700326void ThrowWrappedIOException(const char* fmt, ...) {
327 va_list args;
328 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700329 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700330 va_end(args);
331}
332
Ian Rogers62d6c772013-02-27 08:32:07 -0800333// LinkageError
334
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700335void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 va_list args;
337 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000338 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800339 va_end(args);
340}
341
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700342void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100343 va_list args;
344 va_start(args, fmt);
345 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
346 va_end(args);
347}
348
Ian Rogers62d6c772013-02-27 08:32:07 -0800349// NegativeArraySizeException
350
351void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700352 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700353 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800354}
355
356void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700357 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800358}
359
360// NoSuchFieldError
361
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700362void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700363 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800364 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700365 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700367 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000368 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700369}
370
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700371void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700372 std::ostringstream msg;
373 std::string temp;
374 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
375 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
376}
377
Ian Rogers87e552d2012-08-31 15:54:48 -0700378// NoSuchMethodError
379
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700380void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700381 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700382 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700383 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700384 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700385 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000386 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700387}
388
Ian Rogers62d6c772013-02-27 08:32:07 -0800389// NullPointerException
390
Mathieu Chartierc7853442015-03-27 14:35:38 -0700391void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 std::ostringstream msg;
393 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700394 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700395 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800396}
397
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000398static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200399 const DexFile& dex_file,
400 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700401 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800402 std::ostringstream msg;
403 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700404 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700405 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800406}
407
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000408void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200409 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700410 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000411 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200412 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000413 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200414}
415
Mathieu Chartiere401d142015-04-22 13:56:20 -0700416void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200417 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700418 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200419 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000420 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200421 dex_file, type);
422}
423
Vladimir Marko953437b2016-08-24 08:30:46 +0000424static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
425 DCHECK(kEmitCompilerReadBarrier);
426 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
427 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
428 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
429 monitor_offset += gray_byte_position;
430 }
431 return addr == monitor_offset;
432}
433
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100434static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700435 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100436 if (!CanDoImplicitNullCheckOn(addr)) {
437 return false;
438 }
439
440 switch (instr.Opcode()) {
441 case Instruction::INVOKE_DIRECT:
442 case Instruction::INVOKE_DIRECT_RANGE:
443 case Instruction::INVOKE_VIRTUAL:
444 case Instruction::INVOKE_VIRTUAL_RANGE:
445 case Instruction::INVOKE_INTERFACE:
446 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000447 case Instruction::INVOKE_POLYMORPHIC:
448 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100449 case Instruction::INVOKE_VIRTUAL_QUICK:
450 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
451 // Without inlining, we could just check that the offset is the class offset.
452 // However, when inlining, the compiler can (validly) merge the null check with a field access
453 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
454 // which is the caller.
455 return true;
456 }
457
Vladimir Marko953437b2016-08-24 08:30:46 +0000458 case Instruction::IGET_OBJECT:
459 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
460 return true;
461 }
462 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100463 case Instruction::IGET:
464 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100465 case Instruction::IGET_BOOLEAN:
466 case Instruction::IGET_BYTE:
467 case Instruction::IGET_CHAR:
468 case Instruction::IGET_SHORT:
469 case Instruction::IPUT:
470 case Instruction::IPUT_WIDE:
471 case Instruction::IPUT_OBJECT:
472 case Instruction::IPUT_BOOLEAN:
473 case Instruction::IPUT_BYTE:
474 case Instruction::IPUT_CHAR:
475 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100476 ArtField* field =
477 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000478 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100479 }
480
Vladimir Marko953437b2016-08-24 08:30:46 +0000481 case Instruction::IGET_OBJECT_QUICK:
482 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
483 return true;
484 }
485 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100486 case Instruction::IGET_QUICK:
487 case Instruction::IGET_BOOLEAN_QUICK:
488 case Instruction::IGET_BYTE_QUICK:
489 case Instruction::IGET_CHAR_QUICK:
490 case Instruction::IGET_SHORT_QUICK:
491 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100492 case Instruction::IPUT_QUICK:
493 case Instruction::IPUT_BOOLEAN_QUICK:
494 case Instruction::IPUT_BYTE_QUICK:
495 case Instruction::IPUT_CHAR_QUICK:
496 case Instruction::IPUT_SHORT_QUICK:
497 case Instruction::IPUT_WIDE_QUICK:
498 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000499 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100500 }
501
Vladimir Marko953437b2016-08-24 08:30:46 +0000502 case Instruction::AGET_OBJECT:
503 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
504 return true;
505 }
506 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100507 case Instruction::AGET:
508 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100509 case Instruction::AGET_BOOLEAN:
510 case Instruction::AGET_BYTE:
511 case Instruction::AGET_CHAR:
512 case Instruction::AGET_SHORT:
513 case Instruction::APUT:
514 case Instruction::APUT_WIDE:
515 case Instruction::APUT_OBJECT:
516 case Instruction::APUT_BOOLEAN:
517 case Instruction::APUT_BYTE:
518 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100519 case Instruction::APUT_SHORT:
520 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100521 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100522 // The length access should crash. We currently do not do implicit checks on
523 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000524 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100525 }
526
527 default: {
528 // We have covered all the cases where an NPE could occur.
529 // Note that this must be kept in sync with the compiler, and adding
530 // any new way to do implicit checks in the compiler should also update
531 // this code.
532 return false;
533 }
534 }
535}
536
537void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000538 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000540 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800541 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
542 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100543 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
544 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
545 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
546 << "0x" << std::hex << addr << std::dec
547 << ", at "
548 << instr->DumpString(dex_file)
549 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700550 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100551 }
552
Ian Rogers62d6c772013-02-27 08:32:07 -0800553 switch (instr->Opcode()) {
554 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000555 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200556 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800557 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000558 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800559 break;
560 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000561 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200562 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800563 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000564 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 break;
566 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000567 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200568 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800569 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000570 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800571 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000572 case Instruction::INVOKE_POLYMORPHIC:
573 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_45cc(), kVirtual);
574 break;
575 case Instruction::INVOKE_POLYMORPHIC_RANGE:
576 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_4rcc(), kVirtual);
577 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200578 case Instruction::INVOKE_VIRTUAL_QUICK:
579 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
580 // Since we replaced the method index, we ask the verifier to tell us which
581 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700582 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000583 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700584 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200585 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000586 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200587 } else {
588 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000589 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200590 }
591 break;
592 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800593 case Instruction::IGET:
594 case Instruction::IGET_WIDE:
595 case Instruction::IGET_OBJECT:
596 case Instruction::IGET_BOOLEAN:
597 case Instruction::IGET_BYTE:
598 case Instruction::IGET_CHAR:
599 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700600 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000601 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
602 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800603 break;
604 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200605 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800606 case Instruction::IGET_BOOLEAN_QUICK:
607 case Instruction::IGET_BYTE_QUICK:
608 case Instruction::IGET_CHAR_QUICK:
609 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200610 case Instruction::IGET_WIDE_QUICK:
611 case Instruction::IGET_OBJECT_QUICK: {
612 // Since we replaced the field index, we ask the verifier to tell us which
613 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700614 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000615 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700616 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200617 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000618 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200619 } else {
620 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000621 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200622 }
623 break;
624 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800625 case Instruction::IPUT:
626 case Instruction::IPUT_WIDE:
627 case Instruction::IPUT_OBJECT:
628 case Instruction::IPUT_BOOLEAN:
629 case Instruction::IPUT_BYTE:
630 case Instruction::IPUT_CHAR:
631 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700632 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000633 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
634 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800635 break;
636 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200637 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700638 case Instruction::IPUT_BOOLEAN_QUICK:
639 case Instruction::IPUT_BYTE_QUICK:
640 case Instruction::IPUT_CHAR_QUICK:
641 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200642 case Instruction::IPUT_WIDE_QUICK:
643 case Instruction::IPUT_OBJECT_QUICK: {
644 // Since we replaced the field index, we ask the verifier to tell us which
645 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700646 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000647 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700648 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200649 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000650 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200651 } else {
652 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000653 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200654 }
655 break;
656 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800657 case Instruction::AGET:
658 case Instruction::AGET_WIDE:
659 case Instruction::AGET_OBJECT:
660 case Instruction::AGET_BOOLEAN:
661 case Instruction::AGET_BYTE:
662 case Instruction::AGET_CHAR:
663 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700664 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 "Attempt to read from null array");
666 break;
667 case Instruction::APUT:
668 case Instruction::APUT_WIDE:
669 case Instruction::APUT_OBJECT:
670 case Instruction::APUT_BOOLEAN:
671 case Instruction::APUT_BYTE:
672 case Instruction::APUT_CHAR:
673 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700674 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800675 "Attempt to write to null array");
676 break;
677 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700678 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800679 "Attempt to get length of null array");
680 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100681 case Instruction::FILL_ARRAY_DATA: {
682 ThrowException("Ljava/lang/NullPointerException;", nullptr,
683 "Attempt to write to null array");
684 break;
685 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100686 case Instruction::MONITOR_ENTER:
687 case Instruction::MONITOR_EXIT: {
688 ThrowException("Ljava/lang/NullPointerException;", nullptr,
689 "Attempt to do a synchronize operation on a null object");
690 break;
691 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800692 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000693 const DexFile* dex_file =
694 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100695 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
696 << instr->DumpString(dex_file)
697 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700698 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800699 break;
700 }
701 }
702}
703
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000704void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700705 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800706}
707
708// RuntimeException
709
710void ThrowRuntimeException(const char* fmt, ...) {
711 va_list args;
712 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700713 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800714 va_end(args);
715}
716
Leonard Mosescueb842212016-10-06 17:26:36 -0700717// SecurityException
718
719void ThrowSecurityException(const char* fmt, ...) {
720 va_list args;
721 va_start(args, fmt);
722 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
723 va_end(args);
724}
725
Andreas Gampe103992b2016-01-04 15:32:43 -0800726// Stack overflow.
727
728void ThrowStackOverflowError(Thread* self) {
729 if (self->IsHandlingStackOverflow()) {
730 LOG(ERROR) << "Recursive stack overflow.";
731 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
732 }
733
734 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
735 JNIEnvExt* env = self->GetJniEnv();
736 std::string msg("stack size ");
737 msg += PrettySize(self->GetStackSize());
738
739 // Avoid running Java code for exception initialization.
740 // TODO: Checks to make this a bit less brittle.
741
742 std::string error_msg;
743
744 // Allocate an uninitialized object.
745 ScopedLocalRef<jobject> exc(env,
746 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
747 if (exc.get() != nullptr) {
748 // "Initialize".
749 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
750 // Only Throwable has "custom" fields:
751 // String detailMessage.
752 // Throwable cause (= this).
753 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
754 // Object stackState;
755 // StackTraceElement[] stackTrace;
756 // Only Throwable has a non-empty constructor:
757 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
758 // fillInStackTrace();
759
760 // detailMessage.
761 // TODO: Use String::FromModifiedUTF...?
762 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
763 if (s.get() != nullptr) {
764 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
765
766 // cause.
767 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
768
769 // suppressedExceptions.
770 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
771 WellKnownClasses::java_util_Collections,
772 WellKnownClasses::java_util_Collections_EMPTY_LIST));
773 CHECK(emptylist.get() != nullptr);
774 env->SetObjectField(exc.get(),
775 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
776 emptylist.get());
777
778 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
779 // nativeFillInStackTrace.
780 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
781 {
782 ScopedObjectAccessUnchecked soa(env);
783 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
784 }
785 if (stack_state_val.get() != nullptr) {
786 env->SetObjectField(exc.get(),
787 WellKnownClasses::java_lang_Throwable_stackState,
788 stack_state_val.get());
789
790 // stackTrace.
791 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
792 WellKnownClasses::libcore_util_EmptyArray,
793 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
794 env->SetObjectField(exc.get(),
795 WellKnownClasses::java_lang_Throwable_stackTrace,
796 stack_trace_elem.get());
797 } else {
798 error_msg = "Could not create stack trace.";
799 }
800 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700801 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800802 } else {
803 // Could not allocate a string object.
804 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
805 }
806 } else {
807 error_msg = "Could not allocate StackOverflowError object.";
808 }
809
810 if (!error_msg.empty()) {
811 LOG(WARNING) << error_msg;
812 CHECK(self->IsExceptionPending());
813 }
814
815 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
816 self->ResetDefaultStackEnd(); // Return to default stack size.
817
818 // And restore protection if implicit checks are on.
819 if (!explicit_overflow_check) {
820 self->ProtectStack();
821 }
822}
823
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100824// StringIndexOutOfBoundsException
825
826void ThrowStringIndexOutOfBoundsException(int index, int length) {
827 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
828 StringPrintf("length=%d; index=%d", length, index).c_str());
829}
830
Ian Rogers62d6c772013-02-27 08:32:07 -0800831// VerifyError
832
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700833void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800834 va_list args;
835 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000836 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800837 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700838}
839
Narayan Kamath208f8572016-08-03 12:46:58 +0100840// WrongMethodTypeException
841
842void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
843 mirror::MethodType* callsite_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100844 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
845 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000846 StringPrintf("Expected %s but was %s",
847 callee_type->PrettyDescriptor().c_str(),
848 callsite_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100849}
850
Ian Rogers87e552d2012-08-31 15:54:48 -0700851} // namespace art