blob: b4208fe0547002b11f6e6ff9b17a033a54343679 [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)
Mathieu Chartier90443472015-07-16 20:32:27 -070039 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -070051 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -070067 SHARED_REQUIRES(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
140// ClassFormatError
141
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142void ThrowClassFormatError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800143 va_list args;
144 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000145 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800146 va_end(args);}
147
Ian Rogers87e552d2012-08-31 15:54:48 -0700148// IllegalAccessError
149
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700151 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700153 << PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000154 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700155}
156
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700159 InvokeType type) {
160 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
162 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700163 << " method " << PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000164 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700165}
166
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700168 std::ostringstream msg;
169 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
170 << PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000171 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700172}
173
Mathieu Chartierc7853442015-03-27 14:35:38 -0700174void ThrowIllegalAccessErrorField(mirror::Class* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700175 std::ostringstream msg;
176 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
177 << PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000178 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700179}
180
Mathieu Chartiere401d142015-04-22 13:56:20 -0700181void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700182 std::ostringstream msg;
183 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
184 << PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000185 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800187 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700188}
189
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700190void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 va_list args;
192 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000193 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800194 va_end(args);
195}
196
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700197// IllegalAccessException
198
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000199void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700200 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700201}
202
Ian Rogers62d6c772013-02-27 08:32:07 -0800203// IllegalArgumentException
204
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000205void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700206 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800207}
208
209
Ian Rogers87e552d2012-08-31 15:54:48 -0700210// IncompatibleClassChangeError
211
212void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700214 std::ostringstream msg;
215 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
216 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000217 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700220}
221
Alex Light705ad492015-09-21 11:36:30 -0700222void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
223 mirror::Class* target_class,
224 mirror::Object* this_object,
225 ArtMethod* referrer) {
226 // Referrer is calling interface_method on this_object, however, the interface_method isn't
227 // implemented by this_object.
228 CHECK(this_object != nullptr);
229 std::ostringstream msg;
230 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
231 << "' does not implement interface '" << PrettyDescriptor(target_class) << "' in call to '"
232 << PrettyMethod(method) << "'";
233 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
234 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
235 msg.str().c_str());
236}
237
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700240 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700241 // Referrer is calling interface_method on this_object, however, the interface_method isn't
242 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700243 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700244 std::ostringstream msg;
245 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
246 << "' does not implement interface '"
247 << PrettyDescriptor(interface_method->GetDeclaringClass())
248 << "' in call to '" << PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000249 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700252}
253
Mathieu Chartierc7853442015-03-27 14:35:38 -0700254void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700256 std::ostringstream msg;
257 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 << (is_static ? "static" : "instance") << " field" << " rather than a "
259 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800261 msg.str().c_str());
262}
263
Ian Rogersef7d42f2014-01-06 12:55:46 -0800264void ThrowIncompatibleClassChangeError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 va_list args;
266 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000267 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 va_end(args);
269}
270
Alex Light9139e002015-10-09 15:59:48 -0700271void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
272 DCHECK(method != nullptr);
273 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
274 /*referrer*/nullptr,
275 StringPrintf("Conflicting default method implementations %s",
276 PrettyMethod(method).c_str()).c_str());
277}
278
279
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700280// IOException
281
282void ThrowIOException(const char* fmt, ...) {
283 va_list args;
284 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700285 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700286 va_end(args);
287}
288
Andreas Gampe329d1882014-04-08 10:32:19 -0700289void ThrowWrappedIOException(const char* fmt, ...) {
290 va_list args;
291 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700292 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700293 va_end(args);
294}
295
Ian Rogers62d6c772013-02-27 08:32:07 -0800296// LinkageError
297
Ian Rogersef7d42f2014-01-06 12:55:46 -0800298void ThrowLinkageError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 va_list args;
300 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000301 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 va_end(args);
303}
304
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100305void ThrowWrappedLinkageError(mirror::Class* referrer, const char* fmt, ...) {
306 va_list args;
307 va_start(args, fmt);
308 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
309 va_end(args);
310}
311
Ian Rogers62d6c772013-02-27 08:32:07 -0800312// NegativeArraySizeException
313
314void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700315 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700316 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800317}
318
319void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700320 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800321}
322
323// NoSuchFieldError
324
325void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700326 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800327 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700328 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700330 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000331 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700332}
333
Mathieu Chartier4e067782015-05-13 13:13:24 -0700334void ThrowNoSuchFieldException(mirror::Class* c, const StringPiece& name) {
335 std::ostringstream msg;
336 std::string temp;
337 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
338 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
339}
340
Ian Rogers87e552d2012-08-31 15:54:48 -0700341// NoSuchMethodError
342
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700344 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700345 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700346 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700347 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700348 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000349 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700350}
351
Ian Rogers62d6c772013-02-27 08:32:07 -0800352void ThrowNoSuchMethodError(uint32_t method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353 ArtMethod* method = Thread::Current()->GetCurrentMethod(nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000354 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700355 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700356 std::ostringstream msg;
357 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000358 ThrowException("Ljava/lang/NoSuchMethodError;",
359 method->GetDeclaringClass(), msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800360}
361
362// NullPointerException
363
Mathieu Chartierc7853442015-03-27 14:35:38 -0700364void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 std::ostringstream msg;
366 msg << "Attempt to " << (is_read ? "read from" : "write to")
367 << " field '" << PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700368 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800369}
370
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000371static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200372 const DexFile& dex_file,
373 InvokeType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700374 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 std::ostringstream msg;
376 msg << "Attempt to invoke " << type << " method '"
377 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700378 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800379}
380
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000381void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200382 InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000383 mirror::DexCache* dex_cache =
384 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200385 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000386 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200387}
388
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200390 InvokeType type) {
391 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
392 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000393 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200394 dex_file, type);
395}
396
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000397void ThrowNullPointerExceptionFromDexPC() {
398 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000400 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800401 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
402 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 switch (instr->Opcode()) {
404 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000405 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200406 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000408 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 break;
410 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000411 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200412 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800413 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000414 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800415 break;
416 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000417 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200418 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800419 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000420 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800421 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200422 case Instruction::INVOKE_VIRTUAL_QUICK:
423 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
424 // Since we replaced the method index, we ask the verifier to tell us which
425 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000427 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700428 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200429 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000430 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200431 } else {
432 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000433 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200434 }
435 break;
436 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800437 case Instruction::IGET:
438 case Instruction::IGET_WIDE:
439 case Instruction::IGET_OBJECT:
440 case Instruction::IGET_BOOLEAN:
441 case Instruction::IGET_BYTE:
442 case Instruction::IGET_CHAR:
443 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700444 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000445 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
446 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800447 break;
448 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200449 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800450 case Instruction::IGET_BOOLEAN_QUICK:
451 case Instruction::IGET_BYTE_QUICK:
452 case Instruction::IGET_CHAR_QUICK:
453 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200454 case Instruction::IGET_WIDE_QUICK:
455 case Instruction::IGET_OBJECT_QUICK: {
456 // Since we replaced the field index, we ask the verifier to tell us which
457 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700458 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000459 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700460 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200461 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000462 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200463 } else {
464 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000465 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200466 }
467 break;
468 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800469 case Instruction::IPUT:
470 case Instruction::IPUT_WIDE:
471 case Instruction::IPUT_OBJECT:
472 case Instruction::IPUT_BOOLEAN:
473 case Instruction::IPUT_BYTE:
474 case Instruction::IPUT_CHAR:
475 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700476 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000477 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
478 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800479 break;
480 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200481 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700482 case Instruction::IPUT_BOOLEAN_QUICK:
483 case Instruction::IPUT_BYTE_QUICK:
484 case Instruction::IPUT_CHAR_QUICK:
485 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200486 case Instruction::IPUT_WIDE_QUICK:
487 case Instruction::IPUT_OBJECT_QUICK: {
488 // Since we replaced the field index, we ask the verifier to tell us which
489 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700490 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000491 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700492 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200493 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000494 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200495 } else {
496 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000497 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200498 }
499 break;
500 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800501 case Instruction::AGET:
502 case Instruction::AGET_WIDE:
503 case Instruction::AGET_OBJECT:
504 case Instruction::AGET_BOOLEAN:
505 case Instruction::AGET_BYTE:
506 case Instruction::AGET_CHAR:
507 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700508 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800509 "Attempt to read from null array");
510 break;
511 case Instruction::APUT:
512 case Instruction::APUT_WIDE:
513 case Instruction::APUT_OBJECT:
514 case Instruction::APUT_BOOLEAN:
515 case Instruction::APUT_BYTE:
516 case Instruction::APUT_CHAR:
517 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700518 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800519 "Attempt to write to null array");
520 break;
521 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700522 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800523 "Attempt to get length of null array");
524 break;
525 default: {
526 // TODO: We should have covered all the cases where we expect a NPE above, this
527 // message/logging is so we can improve any cases we've missed in the future.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000528 const DexFile* dex_file =
529 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700530 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800531 StringPrintf("Null pointer exception during instruction '%s'",
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000532 instr->DumpString(dex_file).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800533 break;
534 }
535 }
536}
537
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000538void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700539 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800540}
541
542// RuntimeException
543
544void ThrowRuntimeException(const char* fmt, ...) {
545 va_list args;
546 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700547 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800548 va_end(args);
549}
550
Andreas Gampe103992b2016-01-04 15:32:43 -0800551// Stack overflow.
552
553void ThrowStackOverflowError(Thread* self) {
554 if (self->IsHandlingStackOverflow()) {
555 LOG(ERROR) << "Recursive stack overflow.";
556 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
557 }
558
559 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
560 JNIEnvExt* env = self->GetJniEnv();
561 std::string msg("stack size ");
562 msg += PrettySize(self->GetStackSize());
563
564 // Avoid running Java code for exception initialization.
565 // TODO: Checks to make this a bit less brittle.
566
567 std::string error_msg;
568
569 // Allocate an uninitialized object.
570 ScopedLocalRef<jobject> exc(env,
571 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
572 if (exc.get() != nullptr) {
573 // "Initialize".
574 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
575 // Only Throwable has "custom" fields:
576 // String detailMessage.
577 // Throwable cause (= this).
578 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
579 // Object stackState;
580 // StackTraceElement[] stackTrace;
581 // Only Throwable has a non-empty constructor:
582 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
583 // fillInStackTrace();
584
585 // detailMessage.
586 // TODO: Use String::FromModifiedUTF...?
587 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
588 if (s.get() != nullptr) {
589 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
590
591 // cause.
592 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
593
594 // suppressedExceptions.
595 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
596 WellKnownClasses::java_util_Collections,
597 WellKnownClasses::java_util_Collections_EMPTY_LIST));
598 CHECK(emptylist.get() != nullptr);
599 env->SetObjectField(exc.get(),
600 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
601 emptylist.get());
602
603 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
604 // nativeFillInStackTrace.
605 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
606 {
607 ScopedObjectAccessUnchecked soa(env);
608 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
609 }
610 if (stack_state_val.get() != nullptr) {
611 env->SetObjectField(exc.get(),
612 WellKnownClasses::java_lang_Throwable_stackState,
613 stack_state_val.get());
614
615 // stackTrace.
616 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
617 WellKnownClasses::libcore_util_EmptyArray,
618 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
619 env->SetObjectField(exc.get(),
620 WellKnownClasses::java_lang_Throwable_stackTrace,
621 stack_trace_elem.get());
622 } else {
623 error_msg = "Could not create stack trace.";
624 }
625 // Throw the exception.
626 self->SetException(reinterpret_cast<mirror::Throwable*>(self->DecodeJObject(exc.get())));
627 } else {
628 // Could not allocate a string object.
629 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
630 }
631 } else {
632 error_msg = "Could not allocate StackOverflowError object.";
633 }
634
635 if (!error_msg.empty()) {
636 LOG(WARNING) << error_msg;
637 CHECK(self->IsExceptionPending());
638 }
639
640 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
641 self->ResetDefaultStackEnd(); // Return to default stack size.
642
643 // And restore protection if implicit checks are on.
644 if (!explicit_overflow_check) {
645 self->ProtectStack();
646 }
647}
648
Ian Rogers62d6c772013-02-27 08:32:07 -0800649// VerifyError
650
Ian Rogersef7d42f2014-01-06 12:55:46 -0800651void ThrowVerifyError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800652 va_list args;
653 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000654 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800655 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700656}
657
658} // namespace art