blob: 0aa33c644ed8f6a31017dd64890b7fc6662209a5 [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()) {
45 os << " (declaration of '" << PrettyDescriptor(referrer)
46 << "' appears in " << location << ")";
47 }
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\"",
92 PrettyMethod(method).c_str()).c_str());
93}
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\"",
98 PrettyMethod(method_idx,
99 dex_file,
100 /* with_signature */ true).c_str()).c_str());
101}
102
Ian Rogers62d6c772013-02-27 08:32:07 -0800103// ArithmeticException
104
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200105void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800107}
108
109// ArrayIndexOutOfBoundsException
110
111void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800113 StringPrintf("length=%d; index=%d", length, index).c_str());
114}
115
116// ArrayStoreException
117
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700118void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
119 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700120 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800121 StringPrintf("%s cannot be stored in an array of type %s",
122 PrettyDescriptor(element_class).c_str(),
123 PrettyDescriptor(array_class).c_str()).c_str());
124}
125
126// ClassCastException
127
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700128void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800130 StringPrintf("%s cannot be cast to %s",
131 PrettyDescriptor(src_type).c_str(),
132 PrettyDescriptor(dest_type).c_str()).c_str());
133}
134
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000135void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700136 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800137}
138
139// ClassCircularityError
140
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700141void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800142 std::ostringstream msg;
143 msg << PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000144 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800145}
146
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700147void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100148 va_list args;
149 va_start(args, fmt);
150 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
151 va_end(args);
152}
153
Ian Rogers62d6c772013-02-27 08:32:07 -0800154// ClassFormatError
155
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700156void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800157 va_list args;
158 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000159 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100160 va_end(args);
161}
Ian Rogers62d6c772013-02-27 08:32:07 -0800162
Ian Rogers87e552d2012-08-31 15:54:48 -0700163// IllegalAccessError
164
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700165void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700166 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700167 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700168 << PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000169 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700170}
171
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700172void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
173 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700175 InvokeType type) {
176 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
178 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700179 << " method " << 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;
185 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
186 << 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;
192 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
193 << 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;
199 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
200 << 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;
231 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
232 << 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;
246 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
247 << "' does not implement interface '" << PrettyDescriptor(target_class) << "' in call to '"
248 << PrettyMethod(method) << "'";
249 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
250 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
251 msg.str().c_str());
252}
253
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700255 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700257 // Referrer is calling interface_method on this_object, however, the interface_method isn't
258 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700259 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700260 std::ostringstream msg;
261 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
262 << "' does not implement interface '"
263 << PrettyDescriptor(interface_method->GetDeclaringClass())
264 << "' in call to '" << PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000265 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800267 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700268}
269
Mathieu Chartierc7853442015-03-27 14:35:38 -0700270void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700272 std::ostringstream msg;
273 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 << (is_static ? "static" : "instance") << " field" << " rather than a "
275 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 msg.str().c_str());
278}
279
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700280void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800281 va_list args;
282 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000283 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800284 va_end(args);
285}
286
Alex Light9139e002015-10-09 15:59:48 -0700287void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
288 DCHECK(method != nullptr);
289 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
290 /*referrer*/nullptr,
291 StringPrintf("Conflicting default method implementations %s",
292 PrettyMethod(method).c_str()).c_str());
293}
294
295
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700296// IOException
297
298void ThrowIOException(const char* fmt, ...) {
299 va_list args;
300 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700301 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700302 va_end(args);
303}
304
Andreas Gampe329d1882014-04-08 10:32:19 -0700305void ThrowWrappedIOException(const char* fmt, ...) {
306 va_list args;
307 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700308 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700309 va_end(args);
310}
311
Ian Rogers62d6c772013-02-27 08:32:07 -0800312// LinkageError
313
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700314void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 va_list args;
316 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000317 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 va_end(args);
319}
320
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700321void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100322 va_list args;
323 va_start(args, fmt);
324 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
325 va_end(args);
326}
327
Ian Rogers62d6c772013-02-27 08:32:07 -0800328// NegativeArraySizeException
329
330void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700331 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700332 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800333}
334
335void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700336 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800337}
338
339// NoSuchFieldError
340
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700341void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700342 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800343 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700344 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800345 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700346 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000347 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700348}
349
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700350void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700351 std::ostringstream msg;
352 std::string temp;
353 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
354 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
355}
356
Ian Rogers87e552d2012-08-31 15:54:48 -0700357// NoSuchMethodError
358
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700359void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700360 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700361 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700362 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700363 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700364 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000365 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700366}
367
Ian Rogers62d6c772013-02-27 08:32:07 -0800368// NullPointerException
369
Mathieu Chartierc7853442015-03-27 14:35:38 -0700370void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 std::ostringstream msg;
372 msg << "Attempt to " << (is_read ? "read from" : "write to")
373 << " field '" << PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700374 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800375}
376
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000377static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200378 const DexFile& dex_file,
379 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700380 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 std::ostringstream msg;
382 msg << "Attempt to invoke " << type << " method '"
383 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700384 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800385}
386
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000387void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200388 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700389 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000390 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200391 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000392 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200393}
394
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200396 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700397 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000399 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200400 dex_file, type);
401}
402
Vladimir Marko953437b2016-08-24 08:30:46 +0000403static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
404 DCHECK(kEmitCompilerReadBarrier);
405 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
406 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
407 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
408 monitor_offset += gray_byte_position;
409 }
410 return addr == monitor_offset;
411}
412
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100413static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700414 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100415 if (!CanDoImplicitNullCheckOn(addr)) {
416 return false;
417 }
418
419 switch (instr.Opcode()) {
420 case Instruction::INVOKE_DIRECT:
421 case Instruction::INVOKE_DIRECT_RANGE:
422 case Instruction::INVOKE_VIRTUAL:
423 case Instruction::INVOKE_VIRTUAL_RANGE:
424 case Instruction::INVOKE_INTERFACE:
425 case Instruction::INVOKE_INTERFACE_RANGE:
426 case Instruction::INVOKE_VIRTUAL_QUICK:
427 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
428 // Without inlining, we could just check that the offset is the class offset.
429 // However, when inlining, the compiler can (validly) merge the null check with a field access
430 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
431 // which is the caller.
432 return true;
433 }
434
Vladimir Marko953437b2016-08-24 08:30:46 +0000435 case Instruction::IGET_OBJECT:
436 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
437 return true;
438 }
439 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100440 case Instruction::IGET:
441 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100442 case Instruction::IGET_BOOLEAN:
443 case Instruction::IGET_BYTE:
444 case Instruction::IGET_CHAR:
445 case Instruction::IGET_SHORT:
446 case Instruction::IPUT:
447 case Instruction::IPUT_WIDE:
448 case Instruction::IPUT_OBJECT:
449 case Instruction::IPUT_BOOLEAN:
450 case Instruction::IPUT_BYTE:
451 case Instruction::IPUT_CHAR:
452 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100453 ArtField* field =
454 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000455 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100456 }
457
Vladimir Marko953437b2016-08-24 08:30:46 +0000458 case Instruction::IGET_OBJECT_QUICK:
459 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
460 return true;
461 }
462 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100463 case Instruction::IGET_QUICK:
464 case Instruction::IGET_BOOLEAN_QUICK:
465 case Instruction::IGET_BYTE_QUICK:
466 case Instruction::IGET_CHAR_QUICK:
467 case Instruction::IGET_SHORT_QUICK:
468 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100469 case Instruction::IPUT_QUICK:
470 case Instruction::IPUT_BOOLEAN_QUICK:
471 case Instruction::IPUT_BYTE_QUICK:
472 case Instruction::IPUT_CHAR_QUICK:
473 case Instruction::IPUT_SHORT_QUICK:
474 case Instruction::IPUT_WIDE_QUICK:
475 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000476 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100477 }
478
Vladimir Marko953437b2016-08-24 08:30:46 +0000479 case Instruction::AGET_OBJECT:
480 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
481 return true;
482 }
483 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100484 case Instruction::AGET:
485 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100486 case Instruction::AGET_BOOLEAN:
487 case Instruction::AGET_BYTE:
488 case Instruction::AGET_CHAR:
489 case Instruction::AGET_SHORT:
490 case Instruction::APUT:
491 case Instruction::APUT_WIDE:
492 case Instruction::APUT_OBJECT:
493 case Instruction::APUT_BOOLEAN:
494 case Instruction::APUT_BYTE:
495 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100496 case Instruction::APUT_SHORT:
497 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100498 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100499 // The length access should crash. We currently do not do implicit checks on
500 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000501 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100502 }
503
504 default: {
505 // We have covered all the cases where an NPE could occur.
506 // Note that this must be kept in sync with the compiler, and adding
507 // any new way to do implicit checks in the compiler should also update
508 // this code.
509 return false;
510 }
511 }
512}
513
514void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000515 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000517 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800518 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
519 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100520 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
521 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
522 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
523 << "0x" << std::hex << addr << std::dec
524 << ", at "
525 << instr->DumpString(dex_file)
526 << " in "
527 << PrettyMethod(method);
528 }
529
Ian Rogers62d6c772013-02-27 08:32:07 -0800530 switch (instr->Opcode()) {
531 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000532 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200533 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800534 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000535 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800536 break;
537 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000538 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200539 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800540 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000541 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800542 break;
543 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000544 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200545 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800546 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000547 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800548 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200549 case Instruction::INVOKE_VIRTUAL_QUICK:
550 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
551 // Since we replaced the method index, we ask the verifier to tell us which
552 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000554 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700555 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200556 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000557 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200558 } else {
559 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000560 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200561 }
562 break;
563 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800564 case Instruction::IGET:
565 case Instruction::IGET_WIDE:
566 case Instruction::IGET_OBJECT:
567 case Instruction::IGET_BOOLEAN:
568 case Instruction::IGET_BYTE:
569 case Instruction::IGET_CHAR:
570 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700571 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000572 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
573 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800574 break;
575 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200576 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800577 case Instruction::IGET_BOOLEAN_QUICK:
578 case Instruction::IGET_BYTE_QUICK:
579 case Instruction::IGET_CHAR_QUICK:
580 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200581 case Instruction::IGET_WIDE_QUICK:
582 case Instruction::IGET_OBJECT_QUICK: {
583 // Since we replaced the field index, we ask the verifier to tell us which
584 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700585 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000586 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700587 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200588 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000589 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200590 } else {
591 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000592 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200593 }
594 break;
595 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800596 case Instruction::IPUT:
597 case Instruction::IPUT_WIDE:
598 case Instruction::IPUT_OBJECT:
599 case Instruction::IPUT_BOOLEAN:
600 case Instruction::IPUT_BYTE:
601 case Instruction::IPUT_CHAR:
602 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700603 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000604 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
605 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800606 break;
607 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200608 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700609 case Instruction::IPUT_BOOLEAN_QUICK:
610 case Instruction::IPUT_BYTE_QUICK:
611 case Instruction::IPUT_CHAR_QUICK:
612 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200613 case Instruction::IPUT_WIDE_QUICK:
614 case Instruction::IPUT_OBJECT_QUICK: {
615 // Since we replaced the field index, we ask the verifier to tell us which
616 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700617 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000618 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700619 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200620 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000621 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200622 } else {
623 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000624 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200625 }
626 break;
627 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 case Instruction::AGET:
629 case Instruction::AGET_WIDE:
630 case Instruction::AGET_OBJECT:
631 case Instruction::AGET_BOOLEAN:
632 case Instruction::AGET_BYTE:
633 case Instruction::AGET_CHAR:
634 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700635 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800636 "Attempt to read from null array");
637 break;
638 case Instruction::APUT:
639 case Instruction::APUT_WIDE:
640 case Instruction::APUT_OBJECT:
641 case Instruction::APUT_BOOLEAN:
642 case Instruction::APUT_BYTE:
643 case Instruction::APUT_CHAR:
644 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700645 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800646 "Attempt to write to null array");
647 break;
648 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700649 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800650 "Attempt to get length of null array");
651 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100652 case Instruction::FILL_ARRAY_DATA: {
653 ThrowException("Ljava/lang/NullPointerException;", nullptr,
654 "Attempt to write to null array");
655 break;
656 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100657 case Instruction::MONITOR_ENTER:
658 case Instruction::MONITOR_EXIT: {
659 ThrowException("Ljava/lang/NullPointerException;", nullptr,
660 "Attempt to do a synchronize operation on a null object");
661 break;
662 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800663 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000664 const DexFile* dex_file =
665 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100666 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
667 << instr->DumpString(dex_file)
668 << " in "
669 << PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -0800670 break;
671 }
672 }
673}
674
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000675void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700676 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800677}
678
679// RuntimeException
680
681void ThrowRuntimeException(const char* fmt, ...) {
682 va_list args;
683 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700684 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800685 va_end(args);
686}
687
Andreas Gampe103992b2016-01-04 15:32:43 -0800688// Stack overflow.
689
690void ThrowStackOverflowError(Thread* self) {
691 if (self->IsHandlingStackOverflow()) {
692 LOG(ERROR) << "Recursive stack overflow.";
693 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
694 }
695
696 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
697 JNIEnvExt* env = self->GetJniEnv();
698 std::string msg("stack size ");
699 msg += PrettySize(self->GetStackSize());
700
701 // Avoid running Java code for exception initialization.
702 // TODO: Checks to make this a bit less brittle.
703
704 std::string error_msg;
705
706 // Allocate an uninitialized object.
707 ScopedLocalRef<jobject> exc(env,
708 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
709 if (exc.get() != nullptr) {
710 // "Initialize".
711 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
712 // Only Throwable has "custom" fields:
713 // String detailMessage.
714 // Throwable cause (= this).
715 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
716 // Object stackState;
717 // StackTraceElement[] stackTrace;
718 // Only Throwable has a non-empty constructor:
719 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
720 // fillInStackTrace();
721
722 // detailMessage.
723 // TODO: Use String::FromModifiedUTF...?
724 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
725 if (s.get() != nullptr) {
726 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
727
728 // cause.
729 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
730
731 // suppressedExceptions.
732 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
733 WellKnownClasses::java_util_Collections,
734 WellKnownClasses::java_util_Collections_EMPTY_LIST));
735 CHECK(emptylist.get() != nullptr);
736 env->SetObjectField(exc.get(),
737 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
738 emptylist.get());
739
740 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
741 // nativeFillInStackTrace.
742 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
743 {
744 ScopedObjectAccessUnchecked soa(env);
745 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
746 }
747 if (stack_state_val.get() != nullptr) {
748 env->SetObjectField(exc.get(),
749 WellKnownClasses::java_lang_Throwable_stackState,
750 stack_state_val.get());
751
752 // stackTrace.
753 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
754 WellKnownClasses::libcore_util_EmptyArray,
755 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
756 env->SetObjectField(exc.get(),
757 WellKnownClasses::java_lang_Throwable_stackTrace,
758 stack_trace_elem.get());
759 } else {
760 error_msg = "Could not create stack trace.";
761 }
762 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700763 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800764 } else {
765 // Could not allocate a string object.
766 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
767 }
768 } else {
769 error_msg = "Could not allocate StackOverflowError object.";
770 }
771
772 if (!error_msg.empty()) {
773 LOG(WARNING) << error_msg;
774 CHECK(self->IsExceptionPending());
775 }
776
777 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
778 self->ResetDefaultStackEnd(); // Return to default stack size.
779
780 // And restore protection if implicit checks are on.
781 if (!explicit_overflow_check) {
782 self->ProtectStack();
783 }
784}
785
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100786// StringIndexOutOfBoundsException
787
788void ThrowStringIndexOutOfBoundsException(int index, int length) {
789 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
790 StringPrintf("length=%d; index=%d", length, index).c_str());
791}
792
Ian Rogers62d6c772013-02-27 08:32:07 -0800793// VerifyError
794
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700795void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800796 va_list args;
797 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000798 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800799 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700800}
801
Narayan Kamath208f8572016-08-03 12:46:58 +0100802// WrongMethodTypeException
803
804void ThrowWrongMethodTypeException(mirror::MethodType* callee_type,
805 mirror::MethodType* callsite_type) {
806 // TODO(narayan): Should we provide more detail here ? The RI doesn't bother.
807 UNUSED(callee_type);
808 UNUSED(callsite_type);
809
810 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
811 nullptr,
812 "Invalid method type for signature polymorphic call");
813}
814
Ian Rogers87e552d2012-08-31 15:54:48 -0700815} // namespace art