blob: a44f79e193401194beeffbf3b3ffd16ca6c6e095 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_throws.h"
18
Ian Rogers22d5e732014-07-15 22:23:51 -070019#include <sstream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
Andreas Gampe103992b2016-01-04 15:32:43 -080022#include "ScopedLocalRef.h"
23
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020029#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070030#include "invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010032#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070035#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070036#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020037#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070038
Ian Rogers87e552d2012-08-31 15:54:48 -070039namespace art {
40
Andreas Gampe46ee31b2016-12-14 10:11:49 -080041using android::base::StringAppendV;
42using android::base::StringPrintf;
43
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070044static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070045 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070047 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070048 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070049 os << " (declaration of '" << referrer->PrettyDescriptor()
50 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070051 }
52 }
53}
54
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000055static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070056 ObjPtr<mirror::Class> referrer,
57 const char* fmt,
58 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070060 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070061 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080062 std::string vmsg;
63 StringAppendV(&vmsg, fmt, *args);
64 msg << vmsg;
65 } else {
66 msg << fmt;
67 }
68 AddReferrerLocation(msg, referrer);
69 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000070 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070071}
72
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000073static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070074 ObjPtr<mirror::Class> referrer,
75 const char* fmt,
76 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070078 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070080 std::string vmsg;
81 StringAppendV(&vmsg, fmt, *args);
82 msg << vmsg;
83 } else {
84 msg << fmt;
85 }
86 AddReferrerLocation(msg, referrer);
87 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000088 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070089}
90
Sebastien Hertz56adf602013-07-09 17:27:07 +020091// AbstractMethodError
92
Mathieu Chartiere401d142015-04-22 13:56:20 -070093void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020095 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -070096 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +020097}
98
Alex Light705ad492015-09-21 11:36:30 -070099void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
100 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
101 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700102 dex_file.PrettyMethod(method_idx,
103 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700104}
105
Ian Rogers62d6c772013-02-27 08:32:07 -0800106// ArithmeticException
107
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200108void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800110}
111
112// ArrayIndexOutOfBoundsException
113
114void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 StringPrintf("length=%d; index=%d", length, index).c_str());
117}
118
119// ArrayStoreException
120
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700121void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
122 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700123 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800124 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700125 mirror::Class::PrettyDescriptor(element_class).c_str(),
126 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800127}
128
129// ClassCastException
130
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700131void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700132 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700134 mirror::Class::PrettyDescriptor(src_type).c_str(),
135 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800136}
137
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000138void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800140}
141
142// ClassCircularityError
143
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700144void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800145 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700146 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000147 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800148}
149
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700150void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100151 va_list args;
152 va_start(args, fmt);
153 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
154 va_end(args);
155}
156
Ian Rogers62d6c772013-02-27 08:32:07 -0800157// ClassFormatError
158
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700159void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800160 va_list args;
161 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000162 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100163 va_end(args);
164}
Ian Rogers62d6c772013-02-27 08:32:07 -0800165
Ian Rogers87e552d2012-08-31 15:54:48 -0700166// IllegalAccessError
167
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700168void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700169 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700170 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
171 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000172 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700173}
174
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700175void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
176 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700177 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700178 InvokeType type) {
179 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700180 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
181 << "' attempting to access '"
182 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
183 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000184 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700185}
186
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700187void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700188 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700189 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
190 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000191 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700192}
193
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700194void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700195 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700196 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
197 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000198 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700199}
200
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700202 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700203 msg << "Final field '" << ArtField::PrettyField(accessed, false)
204 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000205 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700206 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800207 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700208}
209
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700210void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 va_list args;
212 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000213 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800214 va_end(args);
215}
216
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700217// IllegalAccessException
218
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000219void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700220 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700221}
222
Ian Rogers62d6c772013-02-27 08:32:07 -0800223// IllegalArgumentException
224
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000225void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700226 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800227}
228
229
Ian Rogers87e552d2012-08-31 15:54:48 -0700230// IncompatibleClassChangeError
231
232void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700234 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700235 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700236 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000237 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800239 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700240}
241
Alex Light705ad492015-09-21 11:36:30 -0700242void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700243 ObjPtr<mirror::Class> target_class,
244 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700245 ArtMethod* referrer) {
246 // Referrer is calling interface_method on this_object, however, the interface_method isn't
247 // implemented by this_object.
248 CHECK(this_object != nullptr);
249 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700250 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
251 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
252 << "' in call to '"
253 << ArtMethod::PrettyMethod(method) << "'";
Alex Light705ad492015-09-21 11:36:30 -0700254 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
255 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
256 msg.str().c_str());
257}
258
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700260 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700262 // Referrer is calling interface_method on this_object, however, the interface_method isn't
263 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700264 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700265 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700266 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700267 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700268 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
269 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000270 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700273}
274
Mathieu Chartierc7853442015-03-27 14:35:38 -0700275void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700277 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700278 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 << (is_static ? "static" : "instance") << " field" << " rather than a "
280 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 msg.str().c_str());
283}
284
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700285void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 va_list args;
287 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000288 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 va_end(args);
290}
291
Alex Light9139e002015-10-09 15:59:48 -0700292void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
293 DCHECK(method != nullptr);
294 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
295 /*referrer*/nullptr,
296 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700297 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700298}
299
300
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700301// IOException
302
303void ThrowIOException(const char* fmt, ...) {
304 va_list args;
305 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700306 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700307 va_end(args);
308}
309
Andreas Gampe329d1882014-04-08 10:32:19 -0700310void ThrowWrappedIOException(const char* fmt, ...) {
311 va_list args;
312 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700313 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700314 va_end(args);
315}
316
Ian Rogers62d6c772013-02-27 08:32:07 -0800317// LinkageError
318
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700319void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800320 va_list args;
321 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000322 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 va_end(args);
324}
325
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700326void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100327 va_list args;
328 va_start(args, fmt);
329 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
330 va_end(args);
331}
332
Ian Rogers62d6c772013-02-27 08:32:07 -0800333// NegativeArraySizeException
334
335void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700336 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700337 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800338}
339
340void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700341 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800342}
343
344// NoSuchFieldError
345
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700346void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700347 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700349 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700351 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000352 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700353}
354
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700355void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700356 std::ostringstream msg;
357 std::string temp;
358 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
359 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
360}
361
Ian Rogers87e552d2012-08-31 15:54:48 -0700362// NoSuchMethodError
363
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700364void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700365 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700366 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700367 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700368 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700369 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000370 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700371}
372
Ian Rogers62d6c772013-02-27 08:32:07 -0800373// NullPointerException
374
Mathieu Chartierc7853442015-03-27 14:35:38 -0700375void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 std::ostringstream msg;
377 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700378 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800380}
381
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000382static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200383 const DexFile& dex_file,
384 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700385 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800386 std::ostringstream msg;
387 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700388 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700389 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800390}
391
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000392void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200393 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700394 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000395 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200396 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000397 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398}
399
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200401 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700402 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200403 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000404 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200405 dex_file, type);
406}
407
Vladimir Marko953437b2016-08-24 08:30:46 +0000408static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
409 DCHECK(kEmitCompilerReadBarrier);
410 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
411 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
412 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
413 monitor_offset += gray_byte_position;
414 }
415 return addr == monitor_offset;
416}
417
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100418static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700419 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100420 if (!CanDoImplicitNullCheckOn(addr)) {
421 return false;
422 }
423
424 switch (instr.Opcode()) {
425 case Instruction::INVOKE_DIRECT:
426 case Instruction::INVOKE_DIRECT_RANGE:
427 case Instruction::INVOKE_VIRTUAL:
428 case Instruction::INVOKE_VIRTUAL_RANGE:
429 case Instruction::INVOKE_INTERFACE:
430 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000431 case Instruction::INVOKE_POLYMORPHIC:
432 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100433 case Instruction::INVOKE_VIRTUAL_QUICK:
434 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
435 // Without inlining, we could just check that the offset is the class offset.
436 // However, when inlining, the compiler can (validly) merge the null check with a field access
437 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
438 // which is the caller.
439 return true;
440 }
441
Vladimir Marko953437b2016-08-24 08:30:46 +0000442 case Instruction::IGET_OBJECT:
443 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
444 return true;
445 }
446 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100447 case Instruction::IGET:
448 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100449 case Instruction::IGET_BOOLEAN:
450 case Instruction::IGET_BYTE:
451 case Instruction::IGET_CHAR:
452 case Instruction::IGET_SHORT:
453 case Instruction::IPUT:
454 case Instruction::IPUT_WIDE:
455 case Instruction::IPUT_OBJECT:
456 case Instruction::IPUT_BOOLEAN:
457 case Instruction::IPUT_BYTE:
458 case Instruction::IPUT_CHAR:
459 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100460 ArtField* field =
461 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000462 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100463 }
464
Vladimir Marko953437b2016-08-24 08:30:46 +0000465 case Instruction::IGET_OBJECT_QUICK:
466 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
467 return true;
468 }
469 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100470 case Instruction::IGET_QUICK:
471 case Instruction::IGET_BOOLEAN_QUICK:
472 case Instruction::IGET_BYTE_QUICK:
473 case Instruction::IGET_CHAR_QUICK:
474 case Instruction::IGET_SHORT_QUICK:
475 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100476 case Instruction::IPUT_QUICK:
477 case Instruction::IPUT_BOOLEAN_QUICK:
478 case Instruction::IPUT_BYTE_QUICK:
479 case Instruction::IPUT_CHAR_QUICK:
480 case Instruction::IPUT_SHORT_QUICK:
481 case Instruction::IPUT_WIDE_QUICK:
482 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000483 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100484 }
485
Vladimir Marko953437b2016-08-24 08:30:46 +0000486 case Instruction::AGET_OBJECT:
487 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
488 return true;
489 }
490 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100491 case Instruction::AGET:
492 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100493 case Instruction::AGET_BOOLEAN:
494 case Instruction::AGET_BYTE:
495 case Instruction::AGET_CHAR:
496 case Instruction::AGET_SHORT:
497 case Instruction::APUT:
498 case Instruction::APUT_WIDE:
499 case Instruction::APUT_OBJECT:
500 case Instruction::APUT_BOOLEAN:
501 case Instruction::APUT_BYTE:
502 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100503 case Instruction::APUT_SHORT:
504 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100505 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100506 // The length access should crash. We currently do not do implicit checks on
507 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000508 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100509 }
510
511 default: {
512 // We have covered all the cases where an NPE could occur.
513 // Note that this must be kept in sync with the compiler, and adding
514 // any new way to do implicit checks in the compiler should also update
515 // this code.
516 return false;
517 }
518 }
519}
520
521void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000522 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000524 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800525 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
526 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100527 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
528 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
529 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
530 << "0x" << std::hex << addr << std::dec
531 << ", at "
532 << instr->DumpString(dex_file)
533 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700534 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100535 }
536
Ian Rogers62d6c772013-02-27 08:32:07 -0800537 switch (instr->Opcode()) {
538 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000539 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200540 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800541 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000542 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800543 break;
544 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000545 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200546 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800547 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000548 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800549 break;
550 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000551 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200552 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800553 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000554 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800555 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000556 case Instruction::INVOKE_POLYMORPHIC:
557 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_45cc(), kVirtual);
558 break;
559 case Instruction::INVOKE_POLYMORPHIC_RANGE:
560 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_4rcc(), kVirtual);
561 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200562 case Instruction::INVOKE_VIRTUAL_QUICK:
563 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
564 // Since we replaced the method index, we ask the verifier to tell us which
565 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700566 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000567 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700568 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200569 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000570 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200571 } else {
572 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000573 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200574 }
575 break;
576 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800577 case Instruction::IGET:
578 case Instruction::IGET_WIDE:
579 case Instruction::IGET_OBJECT:
580 case Instruction::IGET_BOOLEAN:
581 case Instruction::IGET_BYTE:
582 case Instruction::IGET_CHAR:
583 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700584 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000585 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
586 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800587 break;
588 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200589 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800590 case Instruction::IGET_BOOLEAN_QUICK:
591 case Instruction::IGET_BYTE_QUICK:
592 case Instruction::IGET_CHAR_QUICK:
593 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200594 case Instruction::IGET_WIDE_QUICK:
595 case Instruction::IGET_OBJECT_QUICK: {
596 // Since we replaced the field index, we ask the verifier to tell us which
597 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700598 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000599 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700600 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200601 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000602 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200603 } else {
604 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000605 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200606 }
607 break;
608 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800609 case Instruction::IPUT:
610 case Instruction::IPUT_WIDE:
611 case Instruction::IPUT_OBJECT:
612 case Instruction::IPUT_BOOLEAN:
613 case Instruction::IPUT_BYTE:
614 case Instruction::IPUT_CHAR:
615 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700616 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000617 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
618 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800619 break;
620 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200621 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700622 case Instruction::IPUT_BOOLEAN_QUICK:
623 case Instruction::IPUT_BYTE_QUICK:
624 case Instruction::IPUT_CHAR_QUICK:
625 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200626 case Instruction::IPUT_WIDE_QUICK:
627 case Instruction::IPUT_OBJECT_QUICK: {
628 // Since we replaced the field index, we ask the verifier to tell us which
629 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700630 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000631 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700632 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200633 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000634 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200635 } else {
636 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000637 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200638 }
639 break;
640 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800641 case Instruction::AGET:
642 case Instruction::AGET_WIDE:
643 case Instruction::AGET_OBJECT:
644 case Instruction::AGET_BOOLEAN:
645 case Instruction::AGET_BYTE:
646 case Instruction::AGET_CHAR:
647 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700648 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800649 "Attempt to read from null array");
650 break;
651 case Instruction::APUT:
652 case Instruction::APUT_WIDE:
653 case Instruction::APUT_OBJECT:
654 case Instruction::APUT_BOOLEAN:
655 case Instruction::APUT_BYTE:
656 case Instruction::APUT_CHAR:
657 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700658 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800659 "Attempt to write to null array");
660 break;
661 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700662 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800663 "Attempt to get length of null array");
664 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100665 case Instruction::FILL_ARRAY_DATA: {
666 ThrowException("Ljava/lang/NullPointerException;", nullptr,
667 "Attempt to write to null array");
668 break;
669 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100670 case Instruction::MONITOR_ENTER:
671 case Instruction::MONITOR_EXIT: {
672 ThrowException("Ljava/lang/NullPointerException;", nullptr,
673 "Attempt to do a synchronize operation on a null object");
674 break;
675 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800676 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000677 const DexFile* dex_file =
678 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100679 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
680 << instr->DumpString(dex_file)
681 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700682 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800683 break;
684 }
685 }
686}
687
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000688void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700689 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800690}
691
692// RuntimeException
693
694void ThrowRuntimeException(const char* fmt, ...) {
695 va_list args;
696 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700697 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800698 va_end(args);
699}
700
Leonard Mosescueb842212016-10-06 17:26:36 -0700701// SecurityException
702
703void ThrowSecurityException(const char* fmt, ...) {
704 va_list args;
705 va_start(args, fmt);
706 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
707 va_end(args);
708}
709
Andreas Gampe103992b2016-01-04 15:32:43 -0800710// Stack overflow.
711
712void ThrowStackOverflowError(Thread* self) {
713 if (self->IsHandlingStackOverflow()) {
714 LOG(ERROR) << "Recursive stack overflow.";
715 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
716 }
717
718 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
719 JNIEnvExt* env = self->GetJniEnv();
720 std::string msg("stack size ");
721 msg += PrettySize(self->GetStackSize());
722
723 // Avoid running Java code for exception initialization.
724 // TODO: Checks to make this a bit less brittle.
725
726 std::string error_msg;
727
728 // Allocate an uninitialized object.
729 ScopedLocalRef<jobject> exc(env,
730 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
731 if (exc.get() != nullptr) {
732 // "Initialize".
733 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
734 // Only Throwable has "custom" fields:
735 // String detailMessage.
736 // Throwable cause (= this).
737 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
738 // Object stackState;
739 // StackTraceElement[] stackTrace;
740 // Only Throwable has a non-empty constructor:
741 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
742 // fillInStackTrace();
743
744 // detailMessage.
745 // TODO: Use String::FromModifiedUTF...?
746 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
747 if (s.get() != nullptr) {
748 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
749
750 // cause.
751 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
752
753 // suppressedExceptions.
754 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
755 WellKnownClasses::java_util_Collections,
756 WellKnownClasses::java_util_Collections_EMPTY_LIST));
757 CHECK(emptylist.get() != nullptr);
758 env->SetObjectField(exc.get(),
759 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
760 emptylist.get());
761
762 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
763 // nativeFillInStackTrace.
764 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
765 {
766 ScopedObjectAccessUnchecked soa(env);
767 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
768 }
769 if (stack_state_val.get() != nullptr) {
770 env->SetObjectField(exc.get(),
771 WellKnownClasses::java_lang_Throwable_stackState,
772 stack_state_val.get());
773
774 // stackTrace.
775 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
776 WellKnownClasses::libcore_util_EmptyArray,
777 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
778 env->SetObjectField(exc.get(),
779 WellKnownClasses::java_lang_Throwable_stackTrace,
780 stack_trace_elem.get());
781 } else {
782 error_msg = "Could not create stack trace.";
783 }
784 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700785 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800786 } else {
787 // Could not allocate a string object.
788 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
789 }
790 } else {
791 error_msg = "Could not allocate StackOverflowError object.";
792 }
793
794 if (!error_msg.empty()) {
795 LOG(WARNING) << error_msg;
796 CHECK(self->IsExceptionPending());
797 }
798
799 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
800 self->ResetDefaultStackEnd(); // Return to default stack size.
801
802 // And restore protection if implicit checks are on.
803 if (!explicit_overflow_check) {
804 self->ProtectStack();
805 }
806}
807
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100808// StringIndexOutOfBoundsException
809
810void ThrowStringIndexOutOfBoundsException(int index, int length) {
811 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
812 StringPrintf("length=%d; index=%d", length, index).c_str());
813}
814
Ian Rogers62d6c772013-02-27 08:32:07 -0800815// VerifyError
816
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700817void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800818 va_list args;
819 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000820 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800821 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700822}
823
Narayan Kamath208f8572016-08-03 12:46:58 +0100824// WrongMethodTypeException
825
826void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
827 mirror::MethodType* callsite_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100828 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
829 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000830 StringPrintf("Expected %s but was %s",
831 callee_type->PrettyDescriptor().c_str(),
832 callsite_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100833}
834
Ian Rogers87e552d2012-08-31 15:54:48 -0700835} // namespace art