blob: 1baa8f78a94bc3ca0fa0aafaddc69c81aff63a32 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object-inl.h"
32#include "mirror/object_array-inl.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070033#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070034#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020035#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070036
Ian Rogers87e552d2012-08-31 15:54:48 -070037namespace art {
38
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070039static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070040 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070041 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070042 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070043 if (!location.empty()) {
44 os << " (declaration of '" << PrettyDescriptor(referrer)
45 << "' appears in " << location << ")";
46 }
47 }
48}
49
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000050static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070051 ObjPtr<mirror::Class> referrer,
52 const char* fmt,
53 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070054 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070055 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070056 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080057 std::string vmsg;
58 StringAppendV(&vmsg, fmt, *args);
59 msg << vmsg;
60 } else {
61 msg << fmt;
62 }
63 AddReferrerLocation(msg, referrer);
64 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000065 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070066}
67
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000068static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070069 ObjPtr<mirror::Class> referrer,
70 const char* fmt,
71 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070073 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070074 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070075 std::string vmsg;
76 StringAppendV(&vmsg, fmt, *args);
77 msg << vmsg;
78 } else {
79 msg << fmt;
80 }
81 AddReferrerLocation(msg, referrer);
82 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000083 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070084}
85
Sebastien Hertz56adf602013-07-09 17:27:07 +020086// AbstractMethodError
87
Mathieu Chartiere401d142015-04-22 13:56:20 -070088void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020090 StringPrintf("abstract method \"%s\"",
91 PrettyMethod(method).c_str()).c_str());
92}
93
Alex Light705ad492015-09-21 11:36:30 -070094void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
95 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
96 StringPrintf("abstract method \"%s\"",
97 PrettyMethod(method_idx,
98 dex_file,
99 /* with_signature */ true).c_str()).c_str());
100}
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",
121 PrettyDescriptor(element_class).c_str(),
122 PrettyDescriptor(array_class).c_str()).c_str());
123}
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",
130 PrettyDescriptor(src_type).c_str(),
131 PrettyDescriptor(dest_type).c_str()).c_str());
132}
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;
142 msg << 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;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700166 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700167 << 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;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700176 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
177 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700178 << " method " << PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000179 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700180}
181
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700182void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700183 std::ostringstream msg;
184 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
185 << PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000186 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700187}
188
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700189void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700190 std::ostringstream msg;
191 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
192 << PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000193 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700194}
195
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700197 std::ostringstream msg;
198 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
199 << PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000200 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700203}
204
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700205void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800206 va_list args;
207 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000208 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 va_end(args);
210}
211
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700212// IllegalAccessException
213
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000214void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700215 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700216}
217
Ian Rogers62d6c772013-02-27 08:32:07 -0800218// IllegalArgumentException
219
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000220void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700221 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800222}
223
224
Ian Rogers87e552d2012-08-31 15:54:48 -0700225// IncompatibleClassChangeError
226
227void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700229 std::ostringstream msg;
230 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
231 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000232 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800234 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700235}
236
Alex Light705ad492015-09-21 11:36:30 -0700237void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700238 ObjPtr<mirror::Class> target_class,
239 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700240 ArtMethod* referrer) {
241 // Referrer is calling interface_method on this_object, however, the interface_method isn't
242 // implemented by this_object.
243 CHECK(this_object != nullptr);
244 std::ostringstream msg;
245 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
246 << "' does not implement interface '" << PrettyDescriptor(target_class) << "' in call to '"
247 << PrettyMethod(method) << "'";
248 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
249 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
250 msg.str().c_str());
251}
252
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700254 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700256 // Referrer is calling interface_method on this_object, however, the interface_method isn't
257 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700258 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700259 std::ostringstream msg;
260 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
261 << "' does not implement interface '"
262 << PrettyDescriptor(interface_method->GetDeclaringClass())
263 << "' in call to '" << PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000264 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800266 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700267}
268
Mathieu Chartierc7853442015-03-27 14:35:38 -0700269void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700271 std::ostringstream msg;
272 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273 << (is_static ? "static" : "instance") << " field" << " rather than a "
274 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 msg.str().c_str());
277}
278
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700279void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 va_list args;
281 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000282 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800283 va_end(args);
284}
285
Alex Light9139e002015-10-09 15:59:48 -0700286void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
287 DCHECK(method != nullptr);
288 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
289 /*referrer*/nullptr,
290 StringPrintf("Conflicting default method implementations %s",
291 PrettyMethod(method).c_str()).c_str());
292}
293
294
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700295// IOException
296
297void ThrowIOException(const char* fmt, ...) {
298 va_list args;
299 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700300 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700301 va_end(args);
302}
303
Andreas Gampe329d1882014-04-08 10:32:19 -0700304void ThrowWrappedIOException(const char* fmt, ...) {
305 va_list args;
306 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700307 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700308 va_end(args);
309}
310
Ian Rogers62d6c772013-02-27 08:32:07 -0800311// LinkageError
312
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700313void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 va_list args;
315 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000316 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 va_end(args);
318}
319
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700320void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100321 va_list args;
322 va_start(args, fmt);
323 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
324 va_end(args);
325}
326
Ian Rogers62d6c772013-02-27 08:32:07 -0800327// NegativeArraySizeException
328
329void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700330 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700331 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800332}
333
334void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700335 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800336}
337
338// NoSuchFieldError
339
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700340void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700341 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700343 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700345 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000346 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700347}
348
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700349void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700350 std::ostringstream msg;
351 std::string temp;
352 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
353 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
354}
355
Ian Rogers87e552d2012-08-31 15:54:48 -0700356// NoSuchMethodError
357
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700358void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700359 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700360 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700361 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700362 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700363 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000364 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700365}
366
Ian Rogers62d6c772013-02-27 08:32:07 -0800367// NullPointerException
368
Mathieu Chartierc7853442015-03-27 14:35:38 -0700369void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 std::ostringstream msg;
371 msg << "Attempt to " << (is_read ? "read from" : "write to")
372 << " field '" << PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700373 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800374}
375
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000376static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200377 const DexFile& dex_file,
378 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700379 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 std::ostringstream msg;
381 msg << "Attempt to invoke " << type << " method '"
382 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700383 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800384}
385
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000386void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200387 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700388 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000389 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200390 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000391 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200392}
393
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200395 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700396 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200397 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000398 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200399 dex_file, type);
400}
401
Vladimir Marko953437b2016-08-24 08:30:46 +0000402static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
403 DCHECK(kEmitCompilerReadBarrier);
404 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
405 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
406 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
407 monitor_offset += gray_byte_position;
408 }
409 return addr == monitor_offset;
410}
411
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100412static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700413 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100414 if (!CanDoImplicitNullCheckOn(addr)) {
415 return false;
416 }
417
418 switch (instr.Opcode()) {
419 case Instruction::INVOKE_DIRECT:
420 case Instruction::INVOKE_DIRECT_RANGE:
421 case Instruction::INVOKE_VIRTUAL:
422 case Instruction::INVOKE_VIRTUAL_RANGE:
423 case Instruction::INVOKE_INTERFACE:
424 case Instruction::INVOKE_INTERFACE_RANGE:
425 case Instruction::INVOKE_VIRTUAL_QUICK:
426 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
427 // Without inlining, we could just check that the offset is the class offset.
428 // However, when inlining, the compiler can (validly) merge the null check with a field access
429 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
430 // which is the caller.
431 return true;
432 }
433
Vladimir Marko953437b2016-08-24 08:30:46 +0000434 case Instruction::IGET_OBJECT:
435 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
436 return true;
437 }
438 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100439 case Instruction::IGET:
440 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100441 case Instruction::IGET_BOOLEAN:
442 case Instruction::IGET_BYTE:
443 case Instruction::IGET_CHAR:
444 case Instruction::IGET_SHORT:
445 case Instruction::IPUT:
446 case Instruction::IPUT_WIDE:
447 case Instruction::IPUT_OBJECT:
448 case Instruction::IPUT_BOOLEAN:
449 case Instruction::IPUT_BYTE:
450 case Instruction::IPUT_CHAR:
451 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100452 ArtField* field =
453 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000454 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100455 }
456
Vladimir Marko953437b2016-08-24 08:30:46 +0000457 case Instruction::IGET_OBJECT_QUICK:
458 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
459 return true;
460 }
461 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100462 case Instruction::IGET_QUICK:
463 case Instruction::IGET_BOOLEAN_QUICK:
464 case Instruction::IGET_BYTE_QUICK:
465 case Instruction::IGET_CHAR_QUICK:
466 case Instruction::IGET_SHORT_QUICK:
467 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100468 case Instruction::IPUT_QUICK:
469 case Instruction::IPUT_BOOLEAN_QUICK:
470 case Instruction::IPUT_BYTE_QUICK:
471 case Instruction::IPUT_CHAR_QUICK:
472 case Instruction::IPUT_SHORT_QUICK:
473 case Instruction::IPUT_WIDE_QUICK:
474 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000475 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100476 }
477
Vladimir Marko953437b2016-08-24 08:30:46 +0000478 case Instruction::AGET_OBJECT:
479 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
480 return true;
481 }
482 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100483 case Instruction::AGET:
484 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100485 case Instruction::AGET_BOOLEAN:
486 case Instruction::AGET_BYTE:
487 case Instruction::AGET_CHAR:
488 case Instruction::AGET_SHORT:
489 case Instruction::APUT:
490 case Instruction::APUT_WIDE:
491 case Instruction::APUT_OBJECT:
492 case Instruction::APUT_BOOLEAN:
493 case Instruction::APUT_BYTE:
494 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100495 case Instruction::APUT_SHORT:
496 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100497 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100498 // The length access should crash. We currently do not do implicit checks on
499 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000500 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100501 }
502
503 default: {
504 // We have covered all the cases where an NPE could occur.
505 // Note that this must be kept in sync with the compiler, and adding
506 // any new way to do implicit checks in the compiler should also update
507 // this code.
508 return false;
509 }
510 }
511}
512
513void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000514 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700515 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000516 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800517 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
518 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100519 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
520 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
521 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
522 << "0x" << std::hex << addr << std::dec
523 << ", at "
524 << instr->DumpString(dex_file)
525 << " in "
526 << PrettyMethod(method);
527 }
528
Ian Rogers62d6c772013-02-27 08:32:07 -0800529 switch (instr->Opcode()) {
530 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000531 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200532 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800533 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000534 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800535 break;
536 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000537 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200538 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800539 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000540 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800541 break;
542 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000543 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200544 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800545 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000546 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800547 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200548 case Instruction::INVOKE_VIRTUAL_QUICK:
549 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
550 // Since we replaced the method index, we ask the verifier to tell us which
551 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700552 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000553 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700554 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200555 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000556 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200557 } else {
558 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000559 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200560 }
561 break;
562 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800563 case Instruction::IGET:
564 case Instruction::IGET_WIDE:
565 case Instruction::IGET_OBJECT:
566 case Instruction::IGET_BOOLEAN:
567 case Instruction::IGET_BYTE:
568 case Instruction::IGET_CHAR:
569 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700570 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000571 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
572 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 break;
574 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200575 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800576 case Instruction::IGET_BOOLEAN_QUICK:
577 case Instruction::IGET_BYTE_QUICK:
578 case Instruction::IGET_CHAR_QUICK:
579 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200580 case Instruction::IGET_WIDE_QUICK:
581 case Instruction::IGET_OBJECT_QUICK: {
582 // Since we replaced the field index, we ask the verifier to tell us which
583 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700584 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000585 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700586 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200587 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000588 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200589 } else {
590 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000591 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200592 }
593 break;
594 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800595 case Instruction::IPUT:
596 case Instruction::IPUT_WIDE:
597 case Instruction::IPUT_OBJECT:
598 case Instruction::IPUT_BOOLEAN:
599 case Instruction::IPUT_BYTE:
600 case Instruction::IPUT_CHAR:
601 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700602 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000603 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
604 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800605 break;
606 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200607 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700608 case Instruction::IPUT_BOOLEAN_QUICK:
609 case Instruction::IPUT_BYTE_QUICK:
610 case Instruction::IPUT_CHAR_QUICK:
611 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200612 case Instruction::IPUT_WIDE_QUICK:
613 case Instruction::IPUT_OBJECT_QUICK: {
614 // Since we replaced the field index, we ask the verifier to tell us which
615 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700616 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000617 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700618 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200619 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000620 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200621 } else {
622 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000623 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200624 }
625 break;
626 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800627 case Instruction::AGET:
628 case Instruction::AGET_WIDE:
629 case Instruction::AGET_OBJECT:
630 case Instruction::AGET_BOOLEAN:
631 case Instruction::AGET_BYTE:
632 case Instruction::AGET_CHAR:
633 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700634 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800635 "Attempt to read from null array");
636 break;
637 case Instruction::APUT:
638 case Instruction::APUT_WIDE:
639 case Instruction::APUT_OBJECT:
640 case Instruction::APUT_BOOLEAN:
641 case Instruction::APUT_BYTE:
642 case Instruction::APUT_CHAR:
643 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700644 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800645 "Attempt to write to null array");
646 break;
647 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700648 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800649 "Attempt to get length of null array");
650 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100651 case Instruction::FILL_ARRAY_DATA: {
652 ThrowException("Ljava/lang/NullPointerException;", nullptr,
653 "Attempt to write to null array");
654 break;
655 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100656 case Instruction::MONITOR_ENTER:
657 case Instruction::MONITOR_EXIT: {
658 ThrowException("Ljava/lang/NullPointerException;", nullptr,
659 "Attempt to do a synchronize operation on a null object");
660 break;
661 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800662 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000663 const DexFile* dex_file =
664 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100665 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
666 << instr->DumpString(dex_file)
667 << " in "
668 << PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -0800669 break;
670 }
671 }
672}
673
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000674void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700675 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800676}
677
678// RuntimeException
679
680void ThrowRuntimeException(const char* fmt, ...) {
681 va_list args;
682 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700683 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800684 va_end(args);
685}
686
Andreas Gampe103992b2016-01-04 15:32:43 -0800687// Stack overflow.
688
689void ThrowStackOverflowError(Thread* self) {
690 if (self->IsHandlingStackOverflow()) {
691 LOG(ERROR) << "Recursive stack overflow.";
692 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
693 }
694
695 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
696 JNIEnvExt* env = self->GetJniEnv();
697 std::string msg("stack size ");
698 msg += PrettySize(self->GetStackSize());
699
700 // Avoid running Java code for exception initialization.
701 // TODO: Checks to make this a bit less brittle.
702
703 std::string error_msg;
704
705 // Allocate an uninitialized object.
706 ScopedLocalRef<jobject> exc(env,
707 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
708 if (exc.get() != nullptr) {
709 // "Initialize".
710 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
711 // Only Throwable has "custom" fields:
712 // String detailMessage.
713 // Throwable cause (= this).
714 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
715 // Object stackState;
716 // StackTraceElement[] stackTrace;
717 // Only Throwable has a non-empty constructor:
718 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
719 // fillInStackTrace();
720
721 // detailMessage.
722 // TODO: Use String::FromModifiedUTF...?
723 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
724 if (s.get() != nullptr) {
725 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
726
727 // cause.
728 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
729
730 // suppressedExceptions.
731 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
732 WellKnownClasses::java_util_Collections,
733 WellKnownClasses::java_util_Collections_EMPTY_LIST));
734 CHECK(emptylist.get() != nullptr);
735 env->SetObjectField(exc.get(),
736 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
737 emptylist.get());
738
739 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
740 // nativeFillInStackTrace.
741 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
742 {
743 ScopedObjectAccessUnchecked soa(env);
744 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
745 }
746 if (stack_state_val.get() != nullptr) {
747 env->SetObjectField(exc.get(),
748 WellKnownClasses::java_lang_Throwable_stackState,
749 stack_state_val.get());
750
751 // stackTrace.
752 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
753 WellKnownClasses::libcore_util_EmptyArray,
754 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
755 env->SetObjectField(exc.get(),
756 WellKnownClasses::java_lang_Throwable_stackTrace,
757 stack_trace_elem.get());
758 } else {
759 error_msg = "Could not create stack trace.";
760 }
761 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700762 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800763 } else {
764 // Could not allocate a string object.
765 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
766 }
767 } else {
768 error_msg = "Could not allocate StackOverflowError object.";
769 }
770
771 if (!error_msg.empty()) {
772 LOG(WARNING) << error_msg;
773 CHECK(self->IsExceptionPending());
774 }
775
776 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
777 self->ResetDefaultStackEnd(); // Return to default stack size.
778
779 // And restore protection if implicit checks are on.
780 if (!explicit_overflow_check) {
781 self->ProtectStack();
782 }
783}
784
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100785// StringIndexOutOfBoundsException
786
787void ThrowStringIndexOutOfBoundsException(int index, int length) {
788 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
789 StringPrintf("length=%d; index=%d", length, index).c_str());
790}
791
Ian Rogers62d6c772013-02-27 08:32:07 -0800792// VerifyError
793
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700794void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800795 va_list args;
796 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000797 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800798 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700799}
800
801} // namespace art