blob: 9f0dbbbdd09cd032895b415e4d36fd92d414ab66 [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 Gampe103992b2016-01-04 15:32:43 -080021#include "ScopedLocalRef.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020028#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070029#include "invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010031#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/object-inl.h"
33#include "mirror/object_array-inl.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070034#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070035#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020036#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070037
Ian Rogers87e552d2012-08-31 15:54:48 -070038namespace art {
39
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070040static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070041 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070042 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070043 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070044 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070045 os << " (declaration of '" << referrer->PrettyDescriptor()
46 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070047 }
48 }
49}
50
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000051static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070052 ObjPtr<mirror::Class> referrer,
53 const char* fmt,
54 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070055 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070056 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070057 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080058 std::string vmsg;
59 StringAppendV(&vmsg, fmt, *args);
60 msg << vmsg;
61 } else {
62 msg << fmt;
63 }
64 AddReferrerLocation(msg, referrer);
65 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000066 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070067}
68
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000069static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070070 ObjPtr<mirror::Class> referrer,
71 const char* fmt,
72 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070074 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070076 std::string vmsg;
77 StringAppendV(&vmsg, fmt, *args);
78 msg << vmsg;
79 } else {
80 msg << fmt;
81 }
82 AddReferrerLocation(msg, referrer);
83 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000084 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070085}
86
Sebastien Hertz56adf602013-07-09 17:27:07 +020087// AbstractMethodError
88
Mathieu Chartiere401d142015-04-22 13:56:20 -070089void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020091 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -070092 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +020093}
94
Alex Light705ad492015-09-21 11:36:30 -070095void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
96 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
97 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -070098 dex_file.PrettyMethod(method_idx,
99 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700100}
101
Ian Rogers62d6c772013-02-27 08:32:07 -0800102// ArithmeticException
103
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200104void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700105 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800106}
107
108// ArrayIndexOutOfBoundsException
109
110void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700111 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 StringPrintf("length=%d; index=%d", length, index).c_str());
113}
114
115// ArrayStoreException
116
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700117void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
118 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700119 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800120 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700121 mirror::Class::PrettyDescriptor(element_class).c_str(),
122 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800123}
124
125// ClassCastException
126
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700127void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800129 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700130 mirror::Class::PrettyDescriptor(src_type).c_str(),
131 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800132}
133
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000134void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700135 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800136}
137
138// ClassCircularityError
139
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700140void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800141 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700142 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000143 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800144}
145
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700146void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100147 va_list args;
148 va_start(args, fmt);
149 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
150 va_end(args);
151}
152
Ian Rogers62d6c772013-02-27 08:32:07 -0800153// ClassFormatError
154
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700155void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 va_list args;
157 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000158 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100159 va_end(args);
160}
Ian Rogers62d6c772013-02-27 08:32:07 -0800161
Ian Rogers87e552d2012-08-31 15:54:48 -0700162// IllegalAccessError
163
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700164void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700165 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700166 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
167 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000168 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700169}
170
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700171void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
172 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700173 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700174 InvokeType type) {
175 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700176 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
177 << "' attempting to access '"
178 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
179 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000180 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700181}
182
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700183void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700184 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700185 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
186 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000187 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700188}
189
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700190void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700191 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700192 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
193 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000194 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700195}
196
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700198 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700199 msg << "Final field '" << ArtField::PrettyField(accessed, false)
200 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000201 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700204}
205
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700206void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800207 va_list args;
208 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000209 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800210 va_end(args);
211}
212
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700213// IllegalAccessException
214
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000215void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700216 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700217}
218
Ian Rogers62d6c772013-02-27 08:32:07 -0800219// IllegalArgumentException
220
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000221void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700222 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800223}
224
225
Ian Rogers87e552d2012-08-31 15:54:48 -0700226// IncompatibleClassChangeError
227
228void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700230 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700231 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700232 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000233 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800235 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700236}
237
Alex Light705ad492015-09-21 11:36:30 -0700238void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700239 ObjPtr<mirror::Class> target_class,
240 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700241 ArtMethod* referrer) {
242 // Referrer is calling interface_method on this_object, however, the interface_method isn't
243 // implemented by this_object.
244 CHECK(this_object != nullptr);
245 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700246 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
247 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
248 << "' in call to '"
249 << ArtMethod::PrettyMethod(method) << "'";
Alex Light705ad492015-09-21 11:36:30 -0700250 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
251 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
252 msg.str().c_str());
253}
254
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700256 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700258 // Referrer is calling interface_method on this_object, however, the interface_method isn't
259 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700260 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700261 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700262 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700263 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700264 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
265 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000266 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700269}
270
Mathieu Chartierc7853442015-03-27 14:35:38 -0700271void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700273 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700274 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 << (is_static ? "static" : "instance") << " field" << " rather than a "
276 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 msg.str().c_str());
279}
280
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700281void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 va_list args;
283 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000284 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 va_end(args);
286}
287
Alex Light9139e002015-10-09 15:59:48 -0700288void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
289 DCHECK(method != nullptr);
290 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
291 /*referrer*/nullptr,
292 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700293 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700294}
295
296
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700297// IOException
298
299void ThrowIOException(const char* fmt, ...) {
300 va_list args;
301 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700302 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700303 va_end(args);
304}
305
Andreas Gampe329d1882014-04-08 10:32:19 -0700306void ThrowWrappedIOException(const char* fmt, ...) {
307 va_list args;
308 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700309 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700310 va_end(args);
311}
312
Ian Rogers62d6c772013-02-27 08:32:07 -0800313// LinkageError
314
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700315void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800316 va_list args;
317 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000318 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 va_end(args);
320}
321
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700322void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100323 va_list args;
324 va_start(args, fmt);
325 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
326 va_end(args);
327}
328
Ian Rogers62d6c772013-02-27 08:32:07 -0800329// NegativeArraySizeException
330
331void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700332 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700333 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800334}
335
336void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700337 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800338}
339
340// NoSuchFieldError
341
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700342void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700343 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700345 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700347 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000348 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700349}
350
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700351void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700352 std::ostringstream msg;
353 std::string temp;
354 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
355 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
356}
357
Ian Rogers87e552d2012-08-31 15:54:48 -0700358// NoSuchMethodError
359
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700360void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700361 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700362 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700363 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700364 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700365 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000366 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700367}
368
Ian Rogers62d6c772013-02-27 08:32:07 -0800369// NullPointerException
370
Mathieu Chartierc7853442015-03-27 14:35:38 -0700371void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 std::ostringstream msg;
373 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700374 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700375 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800376}
377
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000378static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200379 const DexFile& dex_file,
380 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700381 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800382 std::ostringstream msg;
383 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700384 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700385 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800386}
387
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000388void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200389 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700390 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000391 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200392 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000393 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200394}
395
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200397 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700398 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200399 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000400 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200401 dex_file, type);
402}
403
Vladimir Marko953437b2016-08-24 08:30:46 +0000404static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
405 DCHECK(kEmitCompilerReadBarrier);
406 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
407 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
408 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
409 monitor_offset += gray_byte_position;
410 }
411 return addr == monitor_offset;
412}
413
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100414static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700415 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100416 if (!CanDoImplicitNullCheckOn(addr)) {
417 return false;
418 }
419
420 switch (instr.Opcode()) {
421 case Instruction::INVOKE_DIRECT:
422 case Instruction::INVOKE_DIRECT_RANGE:
423 case Instruction::INVOKE_VIRTUAL:
424 case Instruction::INVOKE_VIRTUAL_RANGE:
425 case Instruction::INVOKE_INTERFACE:
426 case Instruction::INVOKE_INTERFACE_RANGE:
427 case Instruction::INVOKE_VIRTUAL_QUICK:
428 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
429 // Without inlining, we could just check that the offset is the class offset.
430 // However, when inlining, the compiler can (validly) merge the null check with a field access
431 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
432 // which is the caller.
433 return true;
434 }
435
Vladimir Marko953437b2016-08-24 08:30:46 +0000436 case Instruction::IGET_OBJECT:
437 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
438 return true;
439 }
440 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100441 case Instruction::IGET:
442 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100443 case Instruction::IGET_BOOLEAN:
444 case Instruction::IGET_BYTE:
445 case Instruction::IGET_CHAR:
446 case Instruction::IGET_SHORT:
447 case Instruction::IPUT:
448 case Instruction::IPUT_WIDE:
449 case Instruction::IPUT_OBJECT:
450 case Instruction::IPUT_BOOLEAN:
451 case Instruction::IPUT_BYTE:
452 case Instruction::IPUT_CHAR:
453 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100454 ArtField* field =
455 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000456 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100457 }
458
Vladimir Marko953437b2016-08-24 08:30:46 +0000459 case Instruction::IGET_OBJECT_QUICK:
460 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
461 return true;
462 }
463 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100464 case Instruction::IGET_QUICK:
465 case Instruction::IGET_BOOLEAN_QUICK:
466 case Instruction::IGET_BYTE_QUICK:
467 case Instruction::IGET_CHAR_QUICK:
468 case Instruction::IGET_SHORT_QUICK:
469 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100470 case Instruction::IPUT_QUICK:
471 case Instruction::IPUT_BOOLEAN_QUICK:
472 case Instruction::IPUT_BYTE_QUICK:
473 case Instruction::IPUT_CHAR_QUICK:
474 case Instruction::IPUT_SHORT_QUICK:
475 case Instruction::IPUT_WIDE_QUICK:
476 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000477 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100478 }
479
Vladimir Marko953437b2016-08-24 08:30:46 +0000480 case Instruction::AGET_OBJECT:
481 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
482 return true;
483 }
484 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100485 case Instruction::AGET:
486 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100487 case Instruction::AGET_BOOLEAN:
488 case Instruction::AGET_BYTE:
489 case Instruction::AGET_CHAR:
490 case Instruction::AGET_SHORT:
491 case Instruction::APUT:
492 case Instruction::APUT_WIDE:
493 case Instruction::APUT_OBJECT:
494 case Instruction::APUT_BOOLEAN:
495 case Instruction::APUT_BYTE:
496 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100497 case Instruction::APUT_SHORT:
498 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100499 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100500 // The length access should crash. We currently do not do implicit checks on
501 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000502 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100503 }
504
505 default: {
506 // We have covered all the cases where an NPE could occur.
507 // Note that this must be kept in sync with the compiler, and adding
508 // any new way to do implicit checks in the compiler should also update
509 // this code.
510 return false;
511 }
512 }
513}
514
515void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000516 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000518 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800519 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
520 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100521 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
522 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
523 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
524 << "0x" << std::hex << addr << std::dec
525 << ", at "
526 << instr->DumpString(dex_file)
527 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700528 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100529 }
530
Ian Rogers62d6c772013-02-27 08:32:07 -0800531 switch (instr->Opcode()) {
532 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000533 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200534 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800535 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000536 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800537 break;
538 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000539 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200540 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800541 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000542 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800543 break;
544 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000545 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200546 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800547 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000548 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800549 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200550 case Instruction::INVOKE_VIRTUAL_QUICK:
551 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
552 // Since we replaced the method index, we ask the verifier to tell us which
553 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700554 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000555 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700556 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200557 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000558 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200559 } else {
560 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000561 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200562 }
563 break;
564 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 case Instruction::IGET:
566 case Instruction::IGET_WIDE:
567 case Instruction::IGET_OBJECT:
568 case Instruction::IGET_BOOLEAN:
569 case Instruction::IGET_BYTE:
570 case Instruction::IGET_CHAR:
571 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700572 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000573 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
574 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800575 break;
576 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200577 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800578 case Instruction::IGET_BOOLEAN_QUICK:
579 case Instruction::IGET_BYTE_QUICK:
580 case Instruction::IGET_CHAR_QUICK:
581 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200582 case Instruction::IGET_WIDE_QUICK:
583 case Instruction::IGET_OBJECT_QUICK: {
584 // Since we replaced the field index, we ask the verifier to tell us which
585 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700586 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000587 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700588 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200589 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000590 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200591 } else {
592 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000593 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200594 }
595 break;
596 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800597 case Instruction::IPUT:
598 case Instruction::IPUT_WIDE:
599 case Instruction::IPUT_OBJECT:
600 case Instruction::IPUT_BOOLEAN:
601 case Instruction::IPUT_BYTE:
602 case Instruction::IPUT_CHAR:
603 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700604 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000605 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
606 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800607 break;
608 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200609 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700610 case Instruction::IPUT_BOOLEAN_QUICK:
611 case Instruction::IPUT_BYTE_QUICK:
612 case Instruction::IPUT_CHAR_QUICK:
613 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200614 case Instruction::IPUT_WIDE_QUICK:
615 case Instruction::IPUT_OBJECT_QUICK: {
616 // Since we replaced the field index, we ask the verifier to tell us which
617 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700618 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000619 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700620 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200621 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000622 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200623 } else {
624 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000625 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200626 }
627 break;
628 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800629 case Instruction::AGET:
630 case Instruction::AGET_WIDE:
631 case Instruction::AGET_OBJECT:
632 case Instruction::AGET_BOOLEAN:
633 case Instruction::AGET_BYTE:
634 case Instruction::AGET_CHAR:
635 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700636 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800637 "Attempt to read from null array");
638 break;
639 case Instruction::APUT:
640 case Instruction::APUT_WIDE:
641 case Instruction::APUT_OBJECT:
642 case Instruction::APUT_BOOLEAN:
643 case Instruction::APUT_BYTE:
644 case Instruction::APUT_CHAR:
645 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700646 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800647 "Attempt to write to null array");
648 break;
649 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700650 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800651 "Attempt to get length of null array");
652 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100653 case Instruction::FILL_ARRAY_DATA: {
654 ThrowException("Ljava/lang/NullPointerException;", nullptr,
655 "Attempt to write to null array");
656 break;
657 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100658 case Instruction::MONITOR_ENTER:
659 case Instruction::MONITOR_EXIT: {
660 ThrowException("Ljava/lang/NullPointerException;", nullptr,
661 "Attempt to do a synchronize operation on a null object");
662 break;
663 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800664 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000665 const DexFile* dex_file =
666 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100667 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
668 << instr->DumpString(dex_file)
669 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700670 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800671 break;
672 }
673 }
674}
675
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000676void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700677 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800678}
679
680// RuntimeException
681
682void ThrowRuntimeException(const char* fmt, ...) {
683 va_list args;
684 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700685 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800686 va_end(args);
687}
688
Leonard Mosescueb842212016-10-06 17:26:36 -0700689// SecurityException
690
691void ThrowSecurityException(const char* fmt, ...) {
692 va_list args;
693 va_start(args, fmt);
694 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
695 va_end(args);
696}
697
Andreas Gampe103992b2016-01-04 15:32:43 -0800698// Stack overflow.
699
700void ThrowStackOverflowError(Thread* self) {
701 if (self->IsHandlingStackOverflow()) {
702 LOG(ERROR) << "Recursive stack overflow.";
703 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
704 }
705
706 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
707 JNIEnvExt* env = self->GetJniEnv();
708 std::string msg("stack size ");
709 msg += PrettySize(self->GetStackSize());
710
711 // Avoid running Java code for exception initialization.
712 // TODO: Checks to make this a bit less brittle.
713
714 std::string error_msg;
715
716 // Allocate an uninitialized object.
717 ScopedLocalRef<jobject> exc(env,
718 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
719 if (exc.get() != nullptr) {
720 // "Initialize".
721 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
722 // Only Throwable has "custom" fields:
723 // String detailMessage.
724 // Throwable cause (= this).
725 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
726 // Object stackState;
727 // StackTraceElement[] stackTrace;
728 // Only Throwable has a non-empty constructor:
729 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
730 // fillInStackTrace();
731
732 // detailMessage.
733 // TODO: Use String::FromModifiedUTF...?
734 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
735 if (s.get() != nullptr) {
736 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
737
738 // cause.
739 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
740
741 // suppressedExceptions.
742 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
743 WellKnownClasses::java_util_Collections,
744 WellKnownClasses::java_util_Collections_EMPTY_LIST));
745 CHECK(emptylist.get() != nullptr);
746 env->SetObjectField(exc.get(),
747 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
748 emptylist.get());
749
750 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
751 // nativeFillInStackTrace.
752 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
753 {
754 ScopedObjectAccessUnchecked soa(env);
755 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
756 }
757 if (stack_state_val.get() != nullptr) {
758 env->SetObjectField(exc.get(),
759 WellKnownClasses::java_lang_Throwable_stackState,
760 stack_state_val.get());
761
762 // stackTrace.
763 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
764 WellKnownClasses::libcore_util_EmptyArray,
765 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
766 env->SetObjectField(exc.get(),
767 WellKnownClasses::java_lang_Throwable_stackTrace,
768 stack_trace_elem.get());
769 } else {
770 error_msg = "Could not create stack trace.";
771 }
772 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700773 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800774 } else {
775 // Could not allocate a string object.
776 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
777 }
778 } else {
779 error_msg = "Could not allocate StackOverflowError object.";
780 }
781
782 if (!error_msg.empty()) {
783 LOG(WARNING) << error_msg;
784 CHECK(self->IsExceptionPending());
785 }
786
787 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
788 self->ResetDefaultStackEnd(); // Return to default stack size.
789
790 // And restore protection if implicit checks are on.
791 if (!explicit_overflow_check) {
792 self->ProtectStack();
793 }
794}
795
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100796// StringIndexOutOfBoundsException
797
798void ThrowStringIndexOutOfBoundsException(int index, int length) {
799 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
800 StringPrintf("length=%d; index=%d", length, index).c_str());
801}
802
Ian Rogers62d6c772013-02-27 08:32:07 -0800803// VerifyError
804
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700805void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800806 va_list args;
807 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000808 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800809 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700810}
811
Narayan Kamath208f8572016-08-03 12:46:58 +0100812// WrongMethodTypeException
813
814void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
815 mirror::MethodType* callsite_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100816 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
817 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000818 StringPrintf("Expected %s but was %s",
819 callee_type->PrettyDescriptor().c_str(),
820 callsite_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100821}
822
Ian Rogers87e552d2012-08-31 15:54:48 -0700823} // namespace art