blob: f8e32c41bfc85b777e31a549ae25686481ef0528 [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);
Roland Levillainab880f42016-05-12 16:24:36 +0100146 va_end(args);
147}
Ian Rogers62d6c772013-02-27 08:32:07 -0800148
Ian Rogers87e552d2012-08-31 15:54:48 -0700149// IllegalAccessError
150
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700152 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700153 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700154 << PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000155 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700156}
157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700160 InvokeType type) {
161 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
163 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700164 << " method " << PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000165 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700166}
167
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700169 std::ostringstream msg;
170 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
171 << PrettyDescriptor(referrer) << "'";
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 Chartierc7853442015-03-27 14:35:38 -0700175void ThrowIllegalAccessErrorField(mirror::Class* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700176 std::ostringstream msg;
177 msg << "Field '" << PrettyField(accessed, false) << "' 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 Chartiere401d142015-04-22 13:56:20 -0700182void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700183 std::ostringstream msg;
184 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
185 << PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000186 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700189}
190
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700191void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800192 va_list args;
193 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000194 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800195 va_end(args);
196}
197
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700198// IllegalAccessException
199
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000200void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700201 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700202}
203
Ian Rogers62d6c772013-02-27 08:32:07 -0800204// IllegalArgumentException
205
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000206void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700207 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800208}
209
210
Ian Rogers87e552d2012-08-31 15:54:48 -0700211// IncompatibleClassChangeError
212
213void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700215 std::ostringstream msg;
216 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
217 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000218 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700219 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700221}
222
Alex Light705ad492015-09-21 11:36:30 -0700223void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
224 mirror::Class* target_class,
225 mirror::Object* this_object,
226 ArtMethod* referrer) {
227 // Referrer is calling interface_method on this_object, however, the interface_method isn't
228 // implemented by this_object.
229 CHECK(this_object != nullptr);
230 std::ostringstream msg;
231 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
232 << "' does not implement interface '" << PrettyDescriptor(target_class) << "' in call to '"
233 << PrettyMethod(method) << "'";
234 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
235 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
236 msg.str().c_str());
237}
238
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700242 // Referrer is calling interface_method on this_object, however, the interface_method isn't
243 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700244 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700245 std::ostringstream msg;
246 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
247 << "' does not implement interface '"
248 << PrettyDescriptor(interface_method->GetDeclaringClass())
249 << "' in call to '" << PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000250 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800252 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700253}
254
Mathieu Chartierc7853442015-03-27 14:35:38 -0700255void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700257 std::ostringstream msg;
258 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 << (is_static ? "static" : "instance") << " field" << " rather than a "
260 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800262 msg.str().c_str());
263}
264
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265void ThrowIncompatibleClassChangeError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800266 va_list args;
267 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000268 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 va_end(args);
270}
271
Alex Light9139e002015-10-09 15:59:48 -0700272void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
273 DCHECK(method != nullptr);
274 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
275 /*referrer*/nullptr,
276 StringPrintf("Conflicting default method implementations %s",
277 PrettyMethod(method).c_str()).c_str());
278}
279
280
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281// IOException
282
283void ThrowIOException(const char* fmt, ...) {
284 va_list args;
285 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700286 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700287 va_end(args);
288}
289
Andreas Gampe329d1882014-04-08 10:32:19 -0700290void ThrowWrappedIOException(const char* fmt, ...) {
291 va_list args;
292 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700293 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700294 va_end(args);
295}
296
Ian Rogers62d6c772013-02-27 08:32:07 -0800297// LinkageError
298
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299void ThrowLinkageError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 va_list args;
301 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000302 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 va_end(args);
304}
305
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100306void ThrowWrappedLinkageError(mirror::Class* referrer, const char* fmt, ...) {
307 va_list args;
308 va_start(args, fmt);
309 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
310 va_end(args);
311}
312
Ian Rogers62d6c772013-02-27 08:32:07 -0800313// NegativeArraySizeException
314
315void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700316 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700317 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800318}
319
320void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700321 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800322}
323
324// NoSuchFieldError
325
326void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700327 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700329 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700331 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000332 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700333}
334
Mathieu Chartier4e067782015-05-13 13:13:24 -0700335void ThrowNoSuchFieldException(mirror::Class* c, const StringPiece& name) {
336 std::ostringstream msg;
337 std::string temp;
338 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
339 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
340}
341
Ian Rogers87e552d2012-08-31 15:54:48 -0700342// NoSuchMethodError
343
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700345 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700346 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700347 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700348 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700349 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000350 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700351}
352
Ian Rogers62d6c772013-02-27 08:32:07 -0800353void ThrowNoSuchMethodError(uint32_t method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700354 ArtMethod* method = Thread::Current()->GetCurrentMethod(nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000355 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700356 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700357 std::ostringstream msg;
358 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000359 ThrowException("Ljava/lang/NoSuchMethodError;",
360 method->GetDeclaringClass(), msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800361}
362
363// NullPointerException
364
Mathieu Chartierc7853442015-03-27 14:35:38 -0700365void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 std::ostringstream msg;
367 msg << "Attempt to " << (is_read ? "read from" : "write to")
368 << " field '" << PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700369 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800370}
371
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000372static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200373 const DexFile& dex_file,
374 InvokeType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700375 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 std::ostringstream msg;
377 msg << "Attempt to invoke " << type << " method '"
378 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800380}
381
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000382void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200383 InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000384 mirror::DexCache* dex_cache =
385 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200386 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000387 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200388}
389
Mathieu Chartiere401d142015-04-22 13:56:20 -0700390void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200391 InvokeType type) {
392 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
393 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000394 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200395 dex_file, type);
396}
397
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000398void ThrowNullPointerExceptionFromDexPC() {
399 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000401 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800402 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
403 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800404 switch (instr->Opcode()) {
405 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000406 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200407 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000409 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 break;
411 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000412 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200413 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800414 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000415 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 break;
417 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000418 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200419 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800420 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000421 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800422 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200423 case Instruction::INVOKE_VIRTUAL_QUICK:
424 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
425 // Since we replaced the method index, we ask the verifier to tell us which
426 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000428 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700429 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200430 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000431 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200432 } else {
433 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000434 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200435 }
436 break;
437 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800438 case Instruction::IGET:
439 case Instruction::IGET_WIDE:
440 case Instruction::IGET_OBJECT:
441 case Instruction::IGET_BOOLEAN:
442 case Instruction::IGET_BYTE:
443 case Instruction::IGET_CHAR:
444 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700445 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000446 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
447 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800448 break;
449 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200450 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800451 case Instruction::IGET_BOOLEAN_QUICK:
452 case Instruction::IGET_BYTE_QUICK:
453 case Instruction::IGET_CHAR_QUICK:
454 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200455 case Instruction::IGET_WIDE_QUICK:
456 case Instruction::IGET_OBJECT_QUICK: {
457 // Since we replaced the field index, we ask the verifier to tell us which
458 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700459 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000460 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700461 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200462 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000463 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200464 } else {
465 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000466 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200467 }
468 break;
469 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800470 case Instruction::IPUT:
471 case Instruction::IPUT_WIDE:
472 case Instruction::IPUT_OBJECT:
473 case Instruction::IPUT_BOOLEAN:
474 case Instruction::IPUT_BYTE:
475 case Instruction::IPUT_CHAR:
476 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700477 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000478 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
479 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800480 break;
481 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200482 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700483 case Instruction::IPUT_BOOLEAN_QUICK:
484 case Instruction::IPUT_BYTE_QUICK:
485 case Instruction::IPUT_CHAR_QUICK:
486 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200487 case Instruction::IPUT_WIDE_QUICK:
488 case Instruction::IPUT_OBJECT_QUICK: {
489 // Since we replaced the field index, we ask the verifier to tell us which
490 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700491 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000492 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700493 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200494 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000495 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200496 } else {
497 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000498 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200499 }
500 break;
501 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800502 case Instruction::AGET:
503 case Instruction::AGET_WIDE:
504 case Instruction::AGET_OBJECT:
505 case Instruction::AGET_BOOLEAN:
506 case Instruction::AGET_BYTE:
507 case Instruction::AGET_CHAR:
508 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700509 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800510 "Attempt to read from null array");
511 break;
512 case Instruction::APUT:
513 case Instruction::APUT_WIDE:
514 case Instruction::APUT_OBJECT:
515 case Instruction::APUT_BOOLEAN:
516 case Instruction::APUT_BYTE:
517 case Instruction::APUT_CHAR:
518 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700519 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800520 "Attempt to write to null array");
521 break;
522 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700523 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800524 "Attempt to get length of null array");
525 break;
526 default: {
527 // TODO: We should have covered all the cases where we expect a NPE above, this
528 // message/logging is so we can improve any cases we've missed in the future.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000529 const DexFile* dex_file =
530 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700531 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800532 StringPrintf("Null pointer exception during instruction '%s'",
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000533 instr->DumpString(dex_file).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800534 break;
535 }
536 }
537}
538
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000539void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700540 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800541}
542
543// RuntimeException
544
545void ThrowRuntimeException(const char* fmt, ...) {
546 va_list args;
547 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700548 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800549 va_end(args);
550}
551
Andreas Gampe103992b2016-01-04 15:32:43 -0800552// Stack overflow.
553
554void ThrowStackOverflowError(Thread* self) {
555 if (self->IsHandlingStackOverflow()) {
556 LOG(ERROR) << "Recursive stack overflow.";
557 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
558 }
559
560 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
561 JNIEnvExt* env = self->GetJniEnv();
562 std::string msg("stack size ");
563 msg += PrettySize(self->GetStackSize());
564
565 // Avoid running Java code for exception initialization.
566 // TODO: Checks to make this a bit less brittle.
567
568 std::string error_msg;
569
570 // Allocate an uninitialized object.
571 ScopedLocalRef<jobject> exc(env,
572 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
573 if (exc.get() != nullptr) {
574 // "Initialize".
575 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
576 // Only Throwable has "custom" fields:
577 // String detailMessage.
578 // Throwable cause (= this).
579 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
580 // Object stackState;
581 // StackTraceElement[] stackTrace;
582 // Only Throwable has a non-empty constructor:
583 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
584 // fillInStackTrace();
585
586 // detailMessage.
587 // TODO: Use String::FromModifiedUTF...?
588 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
589 if (s.get() != nullptr) {
590 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
591
592 // cause.
593 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
594
595 // suppressedExceptions.
596 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
597 WellKnownClasses::java_util_Collections,
598 WellKnownClasses::java_util_Collections_EMPTY_LIST));
599 CHECK(emptylist.get() != nullptr);
600 env->SetObjectField(exc.get(),
601 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
602 emptylist.get());
603
604 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
605 // nativeFillInStackTrace.
606 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
607 {
608 ScopedObjectAccessUnchecked soa(env);
609 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
610 }
611 if (stack_state_val.get() != nullptr) {
612 env->SetObjectField(exc.get(),
613 WellKnownClasses::java_lang_Throwable_stackState,
614 stack_state_val.get());
615
616 // stackTrace.
617 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
618 WellKnownClasses::libcore_util_EmptyArray,
619 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
620 env->SetObjectField(exc.get(),
621 WellKnownClasses::java_lang_Throwable_stackTrace,
622 stack_trace_elem.get());
623 } else {
624 error_msg = "Could not create stack trace.";
625 }
626 // Throw the exception.
627 self->SetException(reinterpret_cast<mirror::Throwable*>(self->DecodeJObject(exc.get())));
628 } else {
629 // Could not allocate a string object.
630 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
631 }
632 } else {
633 error_msg = "Could not allocate StackOverflowError object.";
634 }
635
636 if (!error_msg.empty()) {
637 LOG(WARNING) << error_msg;
638 CHECK(self->IsExceptionPending());
639 }
640
641 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
642 self->ResetDefaultStackEnd(); // Return to default stack size.
643
644 // And restore protection if implicit checks are on.
645 if (!explicit_overflow_check) {
646 self->ProtectStack();
647 }
648}
649
Ian Rogers62d6c772013-02-27 08:32:07 -0800650// VerifyError
651
Ian Rogersef7d42f2014-01-06 12:55:46 -0800652void ThrowVerifyError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800653 va_list args;
654 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000655 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700657}
658
659} // namespace art