blob: 7fa8cf932604086b058e82b69a96803597ec246e [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"
Ian Rogers87e552d2012-08-31 15:54:48 -070033#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020034#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070035
Ian Rogers87e552d2012-08-31 15:54:48 -070036namespace art {
37
Ian Rogersef7d42f2014-01-06 12:55:46 -080038static void AddReferrerLocation(std::ostream& os, mirror::Class* referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070039 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070040 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070041 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070042 if (!location.empty()) {
43 os << " (declaration of '" << PrettyDescriptor(referrer)
44 << "' appears in " << location << ")";
45 }
46 }
47}
48
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000049static void ThrowException(const char* exception_descriptor,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070050 mirror::Class* referrer, const char* fmt, va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070052 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070053 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080054 std::string vmsg;
55 StringAppendV(&vmsg, fmt, *args);
56 msg << vmsg;
57 } else {
58 msg << fmt;
59 }
60 AddReferrerLocation(msg, referrer);
61 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070063}
64
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000065static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 mirror::Class* referrer, const char* fmt, va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070068 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070070 std::string vmsg;
71 StringAppendV(&vmsg, fmt, *args);
72 msg << vmsg;
73 } else {
74 msg << fmt;
75 }
76 AddReferrerLocation(msg, referrer);
77 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000078 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070079}
80
Sebastien Hertz56adf602013-07-09 17:27:07 +020081// AbstractMethodError
82
Mathieu Chartiere401d142015-04-22 13:56:20 -070083void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020085 StringPrintf("abstract method \"%s\"",
86 PrettyMethod(method).c_str()).c_str());
87}
88
Alex Light705ad492015-09-21 11:36:30 -070089void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
90 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
91 StringPrintf("abstract method \"%s\"",
92 PrettyMethod(method_idx,
93 dex_file,
94 /* with_signature */ true).c_str()).c_str());
95}
96
Ian Rogers62d6c772013-02-27 08:32:07 -080097// ArithmeticException
98
Sebastien Hertz0a3b8632013-06-26 11:16:01 +020099void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700100 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800101}
102
103// ArrayIndexOutOfBoundsException
104
105void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800107 StringPrintf("length=%d; index=%d", length, index).c_str());
108}
109
110// ArrayStoreException
111
Ian Rogersef7d42f2014-01-06 12:55:46 -0800112void ThrowArrayStoreException(mirror::Class* element_class, mirror::Class* array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800114 StringPrintf("%s cannot be stored in an array of type %s",
115 PrettyDescriptor(element_class).c_str(),
116 PrettyDescriptor(array_class).c_str()).c_str());
117}
118
119// ClassCastException
120
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121void ThrowClassCastException(mirror::Class* dest_type, mirror::Class* src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700122 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800123 StringPrintf("%s cannot be cast to %s",
124 PrettyDescriptor(src_type).c_str(),
125 PrettyDescriptor(dest_type).c_str()).c_str());
126}
127
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000128void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800130}
131
132// ClassCircularityError
133
134void ThrowClassCircularityError(mirror::Class* c) {
135 std::ostringstream msg;
136 msg << PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000137 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800138}
139
Roland Levillain989ab3b2016-05-18 15:52:54 +0100140void ThrowClassCircularityError(mirror::Class* c, const char* fmt, ...) {
141 va_list args;
142 va_start(args, fmt);
143 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
144 va_end(args);
145}
146
Ian Rogers62d6c772013-02-27 08:32:07 -0800147// ClassFormatError
148
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149void ThrowClassFormatError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800150 va_list args;
151 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000152 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100153 va_end(args);
154}
Ian Rogers62d6c772013-02-27 08:32:07 -0800155
Ian Rogers87e552d2012-08-31 15:54:48 -0700156// IllegalAccessError
157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700159 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700160 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700161 << PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000162 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700163}
164
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700167 InvokeType type) {
168 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700169 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
170 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700171 << " method " << PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000172 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700173}
174
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700176 std::ostringstream msg;
177 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
178 << PrettyDescriptor(referrer) << "'";
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 Chartierc7853442015-03-27 14:35:38 -0700182void ThrowIllegalAccessErrorField(mirror::Class* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700183 std::ostringstream msg;
184 msg << "Field '" << PrettyField(accessed, false) << "' 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 Chartiere401d142015-04-22 13:56:20 -0700189void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700190 std::ostringstream msg;
191 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
192 << PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000193 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800195 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700196}
197
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700198void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 va_list args;
200 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000201 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 va_end(args);
203}
204
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700205// IllegalAccessException
206
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000207void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700208 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700209}
210
Ian Rogers62d6c772013-02-27 08:32:07 -0800211// IllegalArgumentException
212
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000213void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700214 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800215}
216
217
Ian Rogers87e552d2012-08-31 15:54:48 -0700218// IncompatibleClassChangeError
219
220void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700221 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700222 std::ostringstream msg;
223 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
224 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000225 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700226 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800227 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700228}
229
Alex Light705ad492015-09-21 11:36:30 -0700230void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
231 mirror::Class* target_class,
232 mirror::Object* this_object,
233 ArtMethod* referrer) {
234 // Referrer is calling interface_method on this_object, however, the interface_method isn't
235 // implemented by this_object.
236 CHECK(this_object != nullptr);
237 std::ostringstream msg;
238 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
239 << "' does not implement interface '" << PrettyDescriptor(target_class) << "' in call to '"
240 << PrettyMethod(method) << "'";
241 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
242 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
243 msg.str().c_str());
244}
245
Mathieu Chartiere401d142015-04-22 13:56:20 -0700246void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700248 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700249 // Referrer is calling interface_method on this_object, however, the interface_method isn't
250 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700251 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700252 std::ostringstream msg;
253 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
254 << "' does not implement interface '"
255 << PrettyDescriptor(interface_method->GetDeclaringClass())
256 << "' in call to '" << PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000257 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700260}
261
Mathieu Chartierc7853442015-03-27 14:35:38 -0700262void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700264 std::ostringstream msg;
265 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 << (is_static ? "static" : "instance") << " field" << " rather than a "
267 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 msg.str().c_str());
270}
271
Ian Rogersef7d42f2014-01-06 12:55:46 -0800272void ThrowIncompatibleClassChangeError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 va_list args;
274 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000275 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 va_end(args);
277}
278
Alex Light9139e002015-10-09 15:59:48 -0700279void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
280 DCHECK(method != nullptr);
281 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
282 /*referrer*/nullptr,
283 StringPrintf("Conflicting default method implementations %s",
284 PrettyMethod(method).c_str()).c_str());
285}
286
287
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700288// IOException
289
290void ThrowIOException(const char* fmt, ...) {
291 va_list args;
292 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700293 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700294 va_end(args);
295}
296
Andreas Gampe329d1882014-04-08 10:32:19 -0700297void ThrowWrappedIOException(const char* fmt, ...) {
298 va_list args;
299 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700300 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700301 va_end(args);
302}
303
Ian Rogers62d6c772013-02-27 08:32:07 -0800304// LinkageError
305
Ian Rogersef7d42f2014-01-06 12:55:46 -0800306void ThrowLinkageError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800307 va_list args;
308 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000309 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 va_end(args);
311}
312
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100313void ThrowWrappedLinkageError(mirror::Class* referrer, const char* fmt, ...) {
314 va_list args;
315 va_start(args, fmt);
316 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
317 va_end(args);
318}
319
Ian Rogers62d6c772013-02-27 08:32:07 -0800320// NegativeArraySizeException
321
322void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700323 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700324 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800325}
326
327void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700328 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800329}
330
331// NoSuchFieldError
332
333void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700334 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700336 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700338 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000339 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700340}
341
Mathieu Chartier4e067782015-05-13 13:13:24 -0700342void ThrowNoSuchFieldException(mirror::Class* c, const StringPiece& name) {
343 std::ostringstream msg;
344 std::string temp;
345 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
346 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
347}
348
Ian Rogers87e552d2012-08-31 15:54:48 -0700349// NoSuchMethodError
350
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700352 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700353 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700354 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700355 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700356 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000357 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700358}
359
Ian Rogers62d6c772013-02-27 08:32:07 -0800360// NullPointerException
361
Mathieu Chartierc7853442015-03-27 14:35:38 -0700362void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 std::ostringstream msg;
364 msg << "Attempt to " << (is_read ? "read from" : "write to")
365 << " field '" << PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700366 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800367}
368
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000369static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200370 const DexFile& dex_file,
371 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700372 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 std::ostringstream msg;
374 msg << "Attempt to invoke " << type << " method '"
375 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700376 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800377}
378
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000379void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200380 InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000381 mirror::DexCache* dex_cache =
382 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200383 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000384 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200385}
386
Mathieu Chartiere401d142015-04-22 13:56:20 -0700387void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200388 InvokeType type) {
389 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
390 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000391 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200392 dex_file, type);
393}
394
Vladimir Marko953437b2016-08-24 08:30:46 +0000395static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
396 DCHECK(kEmitCompilerReadBarrier);
397 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
398 if (kUseBakerReadBarrier && (kRuntimeISA == kX86 || kRuntimeISA == kX86_64)) {
399 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
400 monitor_offset += gray_byte_position;
401 }
402 return addr == monitor_offset;
403}
404
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100405static bool IsValidImplicitCheck(uintptr_t addr, ArtMethod* method, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700406 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100407 if (!CanDoImplicitNullCheckOn(addr)) {
408 return false;
409 }
410
411 switch (instr.Opcode()) {
412 case Instruction::INVOKE_DIRECT:
413 case Instruction::INVOKE_DIRECT_RANGE:
414 case Instruction::INVOKE_VIRTUAL:
415 case Instruction::INVOKE_VIRTUAL_RANGE:
416 case Instruction::INVOKE_INTERFACE:
417 case Instruction::INVOKE_INTERFACE_RANGE:
418 case Instruction::INVOKE_VIRTUAL_QUICK:
419 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
420 // Without inlining, we could just check that the offset is the class offset.
421 // However, when inlining, the compiler can (validly) merge the null check with a field access
422 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
423 // which is the caller.
424 return true;
425 }
426
Vladimir Marko953437b2016-08-24 08:30:46 +0000427 case Instruction::IGET_OBJECT:
428 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
429 return true;
430 }
431 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100432 case Instruction::IGET:
433 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100434 case Instruction::IGET_BOOLEAN:
435 case Instruction::IGET_BYTE:
436 case Instruction::IGET_CHAR:
437 case Instruction::IGET_SHORT:
438 case Instruction::IPUT:
439 case Instruction::IPUT_WIDE:
440 case Instruction::IPUT_OBJECT:
441 case Instruction::IPUT_BOOLEAN:
442 case Instruction::IPUT_BYTE:
443 case Instruction::IPUT_CHAR:
444 case Instruction::IPUT_SHORT: {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100445 ArtField* field =
446 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Vladimir Marko953437b2016-08-24 08:30:46 +0000447 return (addr == 0) || (addr == field->GetOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100448 }
449
Vladimir Marko953437b2016-08-24 08:30:46 +0000450 case Instruction::IGET_OBJECT_QUICK:
451 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
452 return true;
453 }
454 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100455 case Instruction::IGET_QUICK:
456 case Instruction::IGET_BOOLEAN_QUICK:
457 case Instruction::IGET_BYTE_QUICK:
458 case Instruction::IGET_CHAR_QUICK:
459 case Instruction::IGET_SHORT_QUICK:
460 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100461 case Instruction::IPUT_QUICK:
462 case Instruction::IPUT_BOOLEAN_QUICK:
463 case Instruction::IPUT_BYTE_QUICK:
464 case Instruction::IPUT_CHAR_QUICK:
465 case Instruction::IPUT_SHORT_QUICK:
466 case Instruction::IPUT_WIDE_QUICK:
467 case Instruction::IPUT_OBJECT_QUICK: {
Vladimir Marko953437b2016-08-24 08:30:46 +0000468 return (addr == 0u) || (addr == instr.VRegC_22c());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100469 }
470
Vladimir Marko953437b2016-08-24 08:30:46 +0000471 case Instruction::AGET_OBJECT:
472 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
473 return true;
474 }
475 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100476 case Instruction::AGET:
477 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100478 case Instruction::AGET_BOOLEAN:
479 case Instruction::AGET_BYTE:
480 case Instruction::AGET_CHAR:
481 case Instruction::AGET_SHORT:
482 case Instruction::APUT:
483 case Instruction::APUT_WIDE:
484 case Instruction::APUT_OBJECT:
485 case Instruction::APUT_BOOLEAN:
486 case Instruction::APUT_BYTE:
487 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100488 case Instruction::APUT_SHORT:
489 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100490 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100491 // The length access should crash. We currently do not do implicit checks on
492 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000493 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100494 }
495
496 default: {
497 // We have covered all the cases where an NPE could occur.
498 // Note that this must be kept in sync with the compiler, and adding
499 // any new way to do implicit checks in the compiler should also update
500 // this code.
501 return false;
502 }
503 }
504}
505
506void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000507 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700508 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000509 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800510 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
511 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100512 if (check_address && !IsValidImplicitCheck(addr, method, *instr)) {
513 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
514 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
515 << "0x" << std::hex << addr << std::dec
516 << ", at "
517 << instr->DumpString(dex_file)
518 << " in "
519 << PrettyMethod(method);
520 }
521
Ian Rogers62d6c772013-02-27 08:32:07 -0800522 switch (instr->Opcode()) {
523 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000524 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200525 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800526 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000527 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800528 break;
529 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000530 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200531 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800532 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000533 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800534 break;
535 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000536 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200537 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800538 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000539 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800540 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200541 case Instruction::INVOKE_VIRTUAL_QUICK:
542 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
543 // Since we replaced the method index, we ask the verifier to tell us which
544 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700545 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000546 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700547 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200548 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000549 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200550 } else {
551 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000552 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200553 }
554 break;
555 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800556 case Instruction::IGET:
557 case Instruction::IGET_WIDE:
558 case Instruction::IGET_OBJECT:
559 case Instruction::IGET_BOOLEAN:
560 case Instruction::IGET_BYTE:
561 case Instruction::IGET_CHAR:
562 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700563 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000564 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
565 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800566 break;
567 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200568 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800569 case Instruction::IGET_BOOLEAN_QUICK:
570 case Instruction::IGET_BYTE_QUICK:
571 case Instruction::IGET_CHAR_QUICK:
572 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200573 case Instruction::IGET_WIDE_QUICK:
574 case Instruction::IGET_OBJECT_QUICK: {
575 // Since we replaced the field index, we ask the verifier to tell us which
576 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700577 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000578 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700579 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200580 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000581 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200582 } else {
583 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000584 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200585 }
586 break;
587 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800588 case Instruction::IPUT:
589 case Instruction::IPUT_WIDE:
590 case Instruction::IPUT_OBJECT:
591 case Instruction::IPUT_BOOLEAN:
592 case Instruction::IPUT_BYTE:
593 case Instruction::IPUT_CHAR:
594 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700595 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000596 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
597 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800598 break;
599 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200600 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700601 case Instruction::IPUT_BOOLEAN_QUICK:
602 case Instruction::IPUT_BYTE_QUICK:
603 case Instruction::IPUT_CHAR_QUICK:
604 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200605 case Instruction::IPUT_WIDE_QUICK:
606 case Instruction::IPUT_OBJECT_QUICK: {
607 // Since we replaced the field index, we ask the verifier to tell us which
608 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700609 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000610 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700611 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200612 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000613 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200614 } else {
615 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000616 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200617 }
618 break;
619 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800620 case Instruction::AGET:
621 case Instruction::AGET_WIDE:
622 case Instruction::AGET_OBJECT:
623 case Instruction::AGET_BOOLEAN:
624 case Instruction::AGET_BYTE:
625 case Instruction::AGET_CHAR:
626 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700627 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 "Attempt to read from null array");
629 break;
630 case Instruction::APUT:
631 case Instruction::APUT_WIDE:
632 case Instruction::APUT_OBJECT:
633 case Instruction::APUT_BOOLEAN:
634 case Instruction::APUT_BYTE:
635 case Instruction::APUT_CHAR:
636 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700637 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800638 "Attempt to write to null array");
639 break;
640 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700641 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800642 "Attempt to get length of null array");
643 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100644 case Instruction::FILL_ARRAY_DATA: {
645 ThrowException("Ljava/lang/NullPointerException;", nullptr,
646 "Attempt to write to null array");
647 break;
648 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100649 case Instruction::MONITOR_ENTER:
650 case Instruction::MONITOR_EXIT: {
651 ThrowException("Ljava/lang/NullPointerException;", nullptr,
652 "Attempt to do a synchronize operation on a null object");
653 break;
654 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800655 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000656 const DexFile* dex_file =
657 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100658 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
659 << instr->DumpString(dex_file)
660 << " in "
661 << PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -0800662 break;
663 }
664 }
665}
666
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000667void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700668 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800669}
670
671// RuntimeException
672
673void ThrowRuntimeException(const char* fmt, ...) {
674 va_list args;
675 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700676 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800677 va_end(args);
678}
679
Andreas Gampe103992b2016-01-04 15:32:43 -0800680// Stack overflow.
681
682void ThrowStackOverflowError(Thread* self) {
683 if (self->IsHandlingStackOverflow()) {
684 LOG(ERROR) << "Recursive stack overflow.";
685 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
686 }
687
688 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
689 JNIEnvExt* env = self->GetJniEnv();
690 std::string msg("stack size ");
691 msg += PrettySize(self->GetStackSize());
692
693 // Avoid running Java code for exception initialization.
694 // TODO: Checks to make this a bit less brittle.
695
696 std::string error_msg;
697
698 // Allocate an uninitialized object.
699 ScopedLocalRef<jobject> exc(env,
700 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
701 if (exc.get() != nullptr) {
702 // "Initialize".
703 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
704 // Only Throwable has "custom" fields:
705 // String detailMessage.
706 // Throwable cause (= this).
707 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
708 // Object stackState;
709 // StackTraceElement[] stackTrace;
710 // Only Throwable has a non-empty constructor:
711 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
712 // fillInStackTrace();
713
714 // detailMessage.
715 // TODO: Use String::FromModifiedUTF...?
716 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
717 if (s.get() != nullptr) {
718 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
719
720 // cause.
721 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
722
723 // suppressedExceptions.
724 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
725 WellKnownClasses::java_util_Collections,
726 WellKnownClasses::java_util_Collections_EMPTY_LIST));
727 CHECK(emptylist.get() != nullptr);
728 env->SetObjectField(exc.get(),
729 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
730 emptylist.get());
731
732 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
733 // nativeFillInStackTrace.
734 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
735 {
736 ScopedObjectAccessUnchecked soa(env);
737 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
738 }
739 if (stack_state_val.get() != nullptr) {
740 env->SetObjectField(exc.get(),
741 WellKnownClasses::java_lang_Throwable_stackState,
742 stack_state_val.get());
743
744 // stackTrace.
745 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
746 WellKnownClasses::libcore_util_EmptyArray,
747 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
748 env->SetObjectField(exc.get(),
749 WellKnownClasses::java_lang_Throwable_stackTrace,
750 stack_trace_elem.get());
751 } else {
752 error_msg = "Could not create stack trace.";
753 }
754 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700755 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800756 } else {
757 // Could not allocate a string object.
758 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
759 }
760 } else {
761 error_msg = "Could not allocate StackOverflowError object.";
762 }
763
764 if (!error_msg.empty()) {
765 LOG(WARNING) << error_msg;
766 CHECK(self->IsExceptionPending());
767 }
768
769 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
770 self->ResetDefaultStackEnd(); // Return to default stack size.
771
772 // And restore protection if implicit checks are on.
773 if (!explicit_overflow_check) {
774 self->ProtectStack();
775 }
776}
777
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100778// StringIndexOutOfBoundsException
779
780void ThrowStringIndexOutOfBoundsException(int index, int length) {
781 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
782 StringPrintf("length=%d; index=%d", length, index).c_str());
783}
784
Ian Rogers62d6c772013-02-27 08:32:07 -0800785// VerifyError
786
Ian Rogersef7d42f2014-01-06 12:55:46 -0800787void ThrowVerifyError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800788 va_list args;
789 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000790 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800791 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700792}
793
794} // namespace art