Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 19 | #include <sstream> |
| 20 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | #include <android-base/stringprintf.h> |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 23 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 24 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Santiago Aboy Solanes | 14e8d64 | 2022-02-04 14:15:01 +0000 | [diff] [blame] | 26 | #include "art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "class_linker-inl.h" |
Vladimir Marko | f5c537e | 2018-04-09 18:33:55 +0100 | [diff] [blame] | 28 | #include "debug_print.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 29 | #include "dex/dex_file-inl.h" |
| 30 | #include "dex/dex_instruction-inl.h" |
David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame] | 31 | #include "dex/invoke_type.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 32 | #include "mirror/class-inl.h" |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 33 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "mirror/object-inl.h" |
| 35 | #include "mirror/object_array-inl.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 36 | #include "nativehelper/scoped_local_ref.h" |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 37 | #include "obj_ptr-inl.h" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 38 | #include "thread.h" |
Andreas Gampe | a7c83ac | 2017-09-11 08:14:23 -0700 | [diff] [blame] | 39 | #include "well_known_classes.h" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 40 | |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 41 | namespace art { |
| 42 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 43 | using android::base::StringAppendV; |
| 44 | using android::base::StringPrintf; |
| 45 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 46 | static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 47 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 48 | if (referrer != nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 49 | std::string location(referrer->GetLocation()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 50 | if (!location.empty()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 51 | os << " (declaration of '" << referrer->PrettyDescriptor() |
| 52 | << "' appears in " << location << ")"; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 57 | static void ThrowException(const char* exception_descriptor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 58 | Thread* self = Thread::Current(); |
| 59 | self->ThrowNewException(exception_descriptor, nullptr); |
| 60 | } |
| 61 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 62 | static void ThrowException(const char* exception_descriptor, |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 63 | ObjPtr<mirror::Class> referrer, |
| 64 | const char* fmt, |
| 65 | va_list* args = nullptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 66 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 67 | std::ostringstream msg; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 68 | if (args != nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 69 | std::string vmsg; |
| 70 | StringAppendV(&vmsg, fmt, *args); |
| 71 | msg << vmsg; |
| 72 | } else { |
| 73 | msg << fmt; |
| 74 | } |
| 75 | AddReferrerLocation(msg, referrer); |
| 76 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 77 | self->ThrowNewException(exception_descriptor, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 80 | static void ThrowWrappedException(const char* exception_descriptor, |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 81 | ObjPtr<mirror::Class> referrer, |
| 82 | const char* fmt, |
| 83 | va_list* args = nullptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 84 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 85 | std::ostringstream msg; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 86 | if (args != nullptr) { |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 87 | std::string vmsg; |
| 88 | StringAppendV(&vmsg, fmt, *args); |
| 89 | msg << vmsg; |
| 90 | } else { |
| 91 | msg << fmt; |
| 92 | } |
| 93 | AddReferrerLocation(msg, referrer); |
| 94 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 95 | self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str()); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Sebastien Hertz | 56adf60 | 2013-07-09 17:27:07 +0200 | [diff] [blame] | 98 | // AbstractMethodError |
| 99 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 100 | void ThrowAbstractMethodError(ArtMethod* method) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 101 | ThrowException("Ljava/lang/AbstractMethodError;", nullptr, |
Sebastien Hertz | 56adf60 | 2013-07-09 17:27:07 +0200 | [diff] [blame] | 102 | StringPrintf("abstract method \"%s\"", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 103 | ArtMethod::PrettyMethod(method).c_str()).c_str()); |
Sebastien Hertz | 56adf60 | 2013-07-09 17:27:07 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 106 | void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 107 | ThrowException("Ljava/lang/AbstractMethodError;", /* referrer= */ nullptr, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 108 | StringPrintf("abstract method \"%s\"", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 109 | dex_file.PrettyMethod(method_idx, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 110 | /* with_signature= */ true).c_str()).c_str()); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 113 | // ArithmeticException |
| 114 | |
Sebastien Hertz | 0a3b863 | 2013-06-26 11:16:01 +0200 | [diff] [blame] | 115 | void ThrowArithmeticExceptionDivideByZero() { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 116 | ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero"); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // ArrayIndexOutOfBoundsException |
| 120 | |
| 121 | void ThrowArrayIndexOutOfBoundsException(int index, int length) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 122 | ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 123 | StringPrintf("length=%d; index=%d", length, index).c_str()); |
| 124 | } |
| 125 | |
| 126 | // ArrayStoreException |
| 127 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 128 | void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class, |
| 129 | ObjPtr<mirror::Class> array_class) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 130 | ThrowException("Ljava/lang/ArrayStoreException;", nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 131 | StringPrintf("%s cannot be stored in an array of type %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 132 | mirror::Class::PrettyDescriptor(element_class).c_str(), |
| 133 | mirror::Class::PrettyDescriptor(array_class).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 136 | // BootstrapMethodError |
| 137 | |
| 138 | void ThrowBootstrapMethodError(const char* fmt, ...) { |
| 139 | va_list args; |
| 140 | va_start(args, fmt); |
| 141 | ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args); |
| 142 | va_end(args); |
| 143 | } |
| 144 | |
| 145 | void ThrowWrappedBootstrapMethodError(const char* fmt, ...) { |
| 146 | va_list args; |
| 147 | va_start(args, fmt); |
| 148 | ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args); |
| 149 | va_end(args); |
| 150 | } |
| 151 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 152 | // ClassCastException |
| 153 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 154 | void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) { |
Vladimir Marko | f5c537e | 2018-04-09 18:33:55 +0100 | [diff] [blame] | 155 | DumpB77342775DebugData(dest_type, src_type); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 156 | ThrowException("Ljava/lang/ClassCastException;", nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 157 | StringPrintf("%s cannot be cast to %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 158 | mirror::Class::PrettyDescriptor(src_type).c_str(), |
| 159 | mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 162 | void ThrowClassCastException(const char* msg) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 163 | ThrowException("Ljava/lang/ClassCastException;", nullptr, msg); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // ClassCircularityError |
| 167 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 168 | void ThrowClassCircularityError(ObjPtr<mirror::Class> c) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 169 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 170 | msg << mirror::Class::PrettyDescriptor(c); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 171 | ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 174 | void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) { |
Roland Levillain | 989ab3b | 2016-05-18 15:52:54 +0100 | [diff] [blame] | 175 | va_list args; |
| 176 | va_start(args, fmt); |
| 177 | ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args); |
| 178 | va_end(args); |
| 179 | } |
| 180 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 181 | // ClassFormatError |
| 182 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 183 | void ThrowClassFormatError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 184 | va_list args; |
| 185 | va_start(args, fmt); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 186 | ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args); |
Roland Levillain | ab880f4 | 2016-05-12 16:24:36 +0100 | [diff] [blame] | 187 | va_end(args); |
| 188 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 189 | |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 190 | // IllegalAccessError |
| 191 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 192 | void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 193 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 194 | msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer) |
| 195 | << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'"; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 196 | ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 199 | void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer, |
| 200 | ObjPtr<mirror::Class> accessed, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 201 | ArtMethod* called, |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 202 | InvokeType type) { |
| 203 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 204 | msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer) |
| 205 | << "' attempting to access '" |
| 206 | << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type |
| 207 | << " method " << ArtMethod::PrettyMethod(called).c_str(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 208 | ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 211 | void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 212 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 213 | msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '" |
| 214 | << mirror::Class::PrettyDescriptor(referrer) << "'"; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 215 | ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 218 | void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 219 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 220 | msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '" |
| 221 | << mirror::Class::PrettyDescriptor(referrer) << "'"; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 222 | ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 225 | void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 226 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 227 | msg << "Final field '" << ArtField::PrettyField(accessed, false) |
| 228 | << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'"; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 229 | ThrowException("Ljava/lang/IllegalAccessError;", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 230 | referrer != nullptr ? referrer->GetDeclaringClass() : nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 231 | msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 234 | void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 235 | va_list args; |
| 236 | va_start(args, fmt); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 237 | ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 238 | va_end(args); |
| 239 | } |
| 240 | |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 241 | // IllegalAccessException |
| 242 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 243 | void ThrowIllegalAccessException(const char* msg) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 244 | ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg); |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 247 | // IllegalArgumentException |
| 248 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 249 | void ThrowIllegalArgumentException(const char* msg) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 250 | ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 253 | // IllegalStateException |
| 254 | |
| 255 | void ThrowIllegalStateException(const char* msg) { |
| 256 | ThrowException("Ljava/lang/IllegalStateException;", nullptr, msg); |
| 257 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 258 | |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 259 | // IncompatibleClassChangeError |
| 260 | |
Nicolas Geoffray | bdf7dc0 | 2022-01-19 10:34:57 +0000 | [diff] [blame] | 261 | void ThrowIncompatibleClassChangeError(InvokeType expected_type, |
| 262 | InvokeType found_type, |
| 263 | ArtMethod* method, |
| 264 | ArtMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 265 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 266 | msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type " |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 267 | << expected_type << " but instead was found to be of type " << found_type; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 268 | ThrowException("Ljava/lang/IncompatibleClassChangeError;", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 269 | referrer != nullptr ? referrer->GetDeclaringClass() : nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 270 | msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 273 | void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method, |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 274 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 275 | ArtMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 276 | // Referrer is calling interface_method on this_object, however, the interface_method isn't |
| 277 | // implemented by this_object. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 278 | CHECK(this_object != nullptr); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 279 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 280 | msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass()) |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 281 | << "' does not implement interface '" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 282 | << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass()) |
| 283 | << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'"; |
Vladimir Marko | f5c537e | 2018-04-09 18:33:55 +0100 | [diff] [blame] | 284 | DumpB77342775DebugData(interface_method->GetDeclaringClass(), this_object->GetClass()); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 285 | ThrowException("Ljava/lang/IncompatibleClassChangeError;", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 286 | referrer != nullptr ? referrer->GetDeclaringClass() : nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 287 | msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Nicolas Geoffray | bdf7dc0 | 2022-01-19 10:34:57 +0000 | [diff] [blame] | 290 | void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, |
| 291 | bool is_static, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 292 | ArtMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 293 | std::ostringstream msg; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 294 | msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a " |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 295 | << (is_static ? "static" : "instance") << " field" << " rather than a " |
| 296 | << (is_static ? "instance" : "static") << " field"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 297 | ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 298 | msg.str().c_str()); |
| 299 | } |
| 300 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 301 | void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 302 | va_list args; |
| 303 | va_start(args, fmt); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 304 | ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 305 | va_end(args); |
| 306 | } |
| 307 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 308 | void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) { |
| 309 | DCHECK(method != nullptr); |
| 310 | ThrowException("Ljava/lang/IncompatibleClassChangeError;", |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 311 | /*referrer=*/nullptr, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 312 | StringPrintf("Conflicting default method implementations %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 313 | ArtMethod::PrettyMethod(method).c_str()).c_str()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 316 | // IndexOutOfBoundsException |
| 317 | |
| 318 | void ThrowIndexOutOfBoundsException(int index, int length) { |
| 319 | ThrowException("Ljava/lang/IndexOutOfBoundsException;", nullptr, |
| 320 | StringPrintf("length=%d; index=%d", length, index).c_str()); |
| 321 | } |
| 322 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 323 | // InternalError |
| 324 | |
| 325 | void ThrowInternalError(const char* fmt, ...) { |
| 326 | va_list args; |
| 327 | va_start(args, fmt); |
| 328 | ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args); |
| 329 | va_end(args); |
| 330 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 331 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 332 | // IOException |
| 333 | |
| 334 | void ThrowIOException(const char* fmt, ...) { |
| 335 | va_list args; |
| 336 | va_start(args, fmt); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 337 | ThrowException("Ljava/io/IOException;", nullptr, fmt, &args); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 338 | va_end(args); |
| 339 | } |
| 340 | |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 341 | void ThrowWrappedIOException(const char* fmt, ...) { |
| 342 | va_list args; |
| 343 | va_start(args, fmt); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 344 | ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 345 | va_end(args); |
| 346 | } |
| 347 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 348 | // LinkageError |
| 349 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 350 | void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 351 | va_list args; |
| 352 | va_start(args, fmt); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 353 | ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 354 | va_end(args); |
| 355 | } |
| 356 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 357 | void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 358 | va_list args; |
| 359 | va_start(args, fmt); |
| 360 | ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args); |
| 361 | va_end(args); |
| 362 | } |
| 363 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 364 | // NegativeArraySizeException |
| 365 | |
| 366 | void ThrowNegativeArraySizeException(int size) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 367 | ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 368 | StringPrintf("%d", size).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void ThrowNegativeArraySizeException(const char* msg) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 372 | ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // NoSuchFieldError |
| 376 | |
Vladimir Marko | 7210108 | 2019-02-05 16:16:30 +0000 | [diff] [blame] | 377 | void ThrowNoSuchFieldError(std::string_view scope, |
| 378 | ObjPtr<mirror::Class> c, |
| 379 | std::string_view type, |
| 380 | std::string_view name) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 381 | std::ostringstream msg; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 382 | std::string temp; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 383 | msg << "No " << scope << "field " << name << " of type " << type |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 384 | << " in class " << c->GetDescriptor(&temp) << " or its superclasses"; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 385 | ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Vladimir Marko | 7210108 | 2019-02-05 16:16:30 +0000 | [diff] [blame] | 388 | void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, std::string_view name) { |
Mathieu Chartier | 4e06778 | 2015-05-13 13:13:24 -0700 | [diff] [blame] | 389 | std::ostringstream msg; |
| 390 | std::string temp; |
| 391 | msg << "No field " << name << " in class " << c->GetDescriptor(&temp); |
| 392 | ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str()); |
| 393 | } |
| 394 | |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 395 | // NoSuchMethodError |
| 396 | |
Vladimir Marko | 7210108 | 2019-02-05 16:16:30 +0000 | [diff] [blame] | 397 | void ThrowNoSuchMethodError(InvokeType type, |
| 398 | ObjPtr<mirror::Class> c, |
| 399 | std::string_view name, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 400 | const Signature& signature) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 401 | std::ostringstream msg; |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 402 | std::string temp; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 403 | msg << "No " << type << " method " << name << signature |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 404 | << " in class " << c->GetDescriptor(&temp) << " or its super classes"; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 405 | ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 408 | // NullPointerException |
| 409 | |
Santiago Aboy Solanes | 14e8d64 | 2022-02-04 14:15:01 +0000 | [diff] [blame] | 410 | void ThrowNullPointerExceptionForFieldAccess(ArtField* field, ArtMethod* method, bool is_read) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 411 | std::ostringstream msg; |
Santiago Aboy Solanes | 14e8d64 | 2022-02-04 14:15:01 +0000 | [diff] [blame] | 412 | msg << "Attempt to " << (is_read ? "read from" : "write to") << " field '" |
| 413 | << ArtField::PrettyField(field) << "' on a null object reference in method '" |
| 414 | << ArtMethod::PrettyMethod(method) << "'"; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 415 | ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 418 | static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx, |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 419 | const DexFile& dex_file, |
| 420 | InvokeType type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 421 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 422 | std::ostringstream msg; |
| 423 | msg << "Attempt to invoke " << type << " method '" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 424 | << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference"; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 425 | ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Vladimir Marko | 7e09737 | 2018-11-28 16:40:59 +0000 | [diff] [blame] | 428 | void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx, InvokeType type) { |
| 429 | const DexFile& dex_file = *Thread::Current()->GetCurrentMethod(nullptr)->GetDexFile(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 430 | ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Vladimir Marko | 7e09737 | 2018-11-28 16:40:59 +0000 | [diff] [blame] | 433 | void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method, InvokeType type) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 434 | ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(), |
Vladimir Marko | 7e09737 | 2018-11-28 16:40:59 +0000 | [diff] [blame] | 435 | *method->GetDexFile(), |
| 436 | type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 439 | static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) { |
| 440 | DCHECK(kEmitCompilerReadBarrier); |
| 441 | uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value(); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 442 | if (kUseBakerReadBarrier && |
| 443 | (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64)) { |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 444 | constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte; |
| 445 | monitor_offset += gray_byte_position; |
| 446 | } |
| 447 | return addr == monitor_offset; |
| 448 | } |
| 449 | |
Nicolas Geoffray | 1344914 | 2017-12-07 22:26:24 +0000 | [diff] [blame] | 450 | static bool IsValidImplicitCheck(uintptr_t addr, const Instruction& instr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 451 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 452 | if (!CanDoImplicitNullCheckOn(addr)) { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | switch (instr.Opcode()) { |
| 457 | case Instruction::INVOKE_DIRECT: |
| 458 | case Instruction::INVOKE_DIRECT_RANGE: |
| 459 | case Instruction::INVOKE_VIRTUAL: |
| 460 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 461 | case Instruction::INVOKE_INTERFACE: |
| 462 | case Instruction::INVOKE_INTERFACE_RANGE: |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 463 | case Instruction::INVOKE_POLYMORPHIC: |
| 464 | case Instruction::INVOKE_POLYMORPHIC_RANGE: |
Nicolas Geoffray | 786e1fe | 2020-01-13 15:17:07 +0000 | [diff] [blame] | 465 | case Instruction::INVOKE_SUPER: |
Nicolas Geoffray | 4924ea9 | 2021-03-23 08:25:31 +0000 | [diff] [blame] | 466 | case Instruction::INVOKE_SUPER_RANGE: { |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 467 | // Without inlining, we could just check that the offset is the class offset. |
| 468 | // However, when inlining, the compiler can (validly) merge the null check with a field access |
| 469 | // on the same object. Note that the stack map at the NPE will reflect the invoke's location, |
| 470 | // which is the caller. |
| 471 | return true; |
| 472 | } |
| 473 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 474 | case Instruction::IGET_OBJECT: |
| 475 | if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) { |
| 476 | return true; |
| 477 | } |
| 478 | FALLTHROUGH_INTENDED; |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 479 | case Instruction::IGET: |
| 480 | case Instruction::IGET_WIDE: |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 481 | case Instruction::IGET_BOOLEAN: |
| 482 | case Instruction::IGET_BYTE: |
| 483 | case Instruction::IGET_CHAR: |
| 484 | case Instruction::IGET_SHORT: |
| 485 | case Instruction::IPUT: |
| 486 | case Instruction::IPUT_WIDE: |
| 487 | case Instruction::IPUT_OBJECT: |
| 488 | case Instruction::IPUT_BOOLEAN: |
| 489 | case Instruction::IPUT_BYTE: |
| 490 | case Instruction::IPUT_CHAR: |
| 491 | case Instruction::IPUT_SHORT: { |
Nicolas Geoffray | 1344914 | 2017-12-07 22:26:24 +0000 | [diff] [blame] | 492 | // We might be doing an implicit null check with an offset that doesn't correspond |
| 493 | // to the instruction, for example with two field accesses and the first one being |
| 494 | // eliminated or re-ordered. |
| 495 | return true; |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 498 | case Instruction::AGET_OBJECT: |
| 499 | if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) { |
| 500 | return true; |
| 501 | } |
| 502 | FALLTHROUGH_INTENDED; |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 503 | case Instruction::AGET: |
| 504 | case Instruction::AGET_WIDE: |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 505 | case Instruction::AGET_BOOLEAN: |
| 506 | case Instruction::AGET_BYTE: |
| 507 | case Instruction::AGET_CHAR: |
| 508 | case Instruction::AGET_SHORT: |
| 509 | case Instruction::APUT: |
| 510 | case Instruction::APUT_WIDE: |
| 511 | case Instruction::APUT_OBJECT: |
| 512 | case Instruction::APUT_BOOLEAN: |
| 513 | case Instruction::APUT_BYTE: |
| 514 | case Instruction::APUT_CHAR: |
Nicolas Geoffray | 350cc99 | 2016-06-29 21:45:10 +0100 | [diff] [blame] | 515 | case Instruction::APUT_SHORT: |
| 516 | case Instruction::FILL_ARRAY_DATA: |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 517 | case Instruction::ARRAY_LENGTH: { |
Nicolas Geoffray | 350cc99 | 2016-06-29 21:45:10 +0100 | [diff] [blame] | 518 | // The length access should crash. We currently do not do implicit checks on |
| 519 | // the array access itself. |
Vladimir Marko | 953437b | 2016-08-24 08:30:46 +0000 | [diff] [blame] | 520 | return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value()); |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | default: { |
| 524 | // We have covered all the cases where an NPE could occur. |
| 525 | // Note that this must be kept in sync with the compiler, and adding |
| 526 | // any new way to do implicit checks in the compiler should also update |
| 527 | // this code. |
| 528 | return false; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 534 | uint32_t throw_dex_pc; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 535 | ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc); |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 536 | CodeItemInstructionAccessor accessor(method->DexInstructions()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 537 | CHECK_LT(throw_dex_pc, accessor.InsnsSizeInCodeUnits()); |
| 538 | const Instruction& instr = accessor.InstructionAt(throw_dex_pc); |
| 539 | if (check_address && !IsValidImplicitCheck(addr, instr)) { |
Vladimir Marko | 7e09737 | 2018-11-28 16:40:59 +0000 | [diff] [blame] | 540 | const DexFile* dex_file = method->GetDexFile(); |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 541 | LOG(FATAL) << "Invalid address for an implicit NullPointerException check: " |
| 542 | << "0x" << std::hex << addr << std::dec |
| 543 | << ", at " |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 544 | << instr.DumpString(dex_file) |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 545 | << " in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 546 | << method->PrettyMethod(); |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 547 | } |
| 548 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 549 | switch (instr.Opcode()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 550 | case Instruction::INVOKE_DIRECT: |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 551 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kDirect); |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 552 | break; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 553 | case Instruction::INVOKE_DIRECT_RANGE: |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 554 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kDirect); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 555 | break; |
| 556 | case Instruction::INVOKE_VIRTUAL: |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 557 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kVirtual); |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 558 | break; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 559 | case Instruction::INVOKE_VIRTUAL_RANGE: |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 560 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kVirtual); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 561 | break; |
Nicolas Geoffray | 786e1fe | 2020-01-13 15:17:07 +0000 | [diff] [blame] | 562 | case Instruction::INVOKE_SUPER: |
| 563 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kSuper); |
| 564 | break; |
| 565 | case Instruction::INVOKE_SUPER_RANGE: |
| 566 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kSuper); |
| 567 | break; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 568 | case Instruction::INVOKE_INTERFACE: |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 569 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kInterface); |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 570 | break; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 571 | case Instruction::INVOKE_INTERFACE_RANGE: |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 572 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kInterface); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 573 | break; |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 574 | case Instruction::INVOKE_POLYMORPHIC: |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 575 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_45cc(), kVirtual); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 576 | break; |
| 577 | case Instruction::INVOKE_POLYMORPHIC_RANGE: |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 578 | ThrowNullPointerExceptionForMethodAccess(instr.VRegB_4rcc(), kVirtual); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 579 | break; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 580 | case Instruction::IGET: |
| 581 | case Instruction::IGET_WIDE: |
| 582 | case Instruction::IGET_OBJECT: |
| 583 | case Instruction::IGET_BOOLEAN: |
| 584 | case Instruction::IGET_BYTE: |
| 585 | case Instruction::IGET_CHAR: |
| 586 | case Instruction::IGET_SHORT: { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 587 | ArtField* field = |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 588 | Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false); |
Andreas Gampe | 7130744 | 2018-02-06 13:38:03 -0800 | [diff] [blame] | 589 | Thread::Current()->ClearException(); // Resolution may fail, ignore. |
Santiago Aboy Solanes | 14e8d64 | 2022-02-04 14:15:01 +0000 | [diff] [blame] | 590 | ThrowNullPointerExceptionForFieldAccess(field, method, /* is_read= */ true); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | case Instruction::IPUT: |
| 594 | case Instruction::IPUT_WIDE: |
| 595 | case Instruction::IPUT_OBJECT: |
| 596 | case Instruction::IPUT_BOOLEAN: |
| 597 | case Instruction::IPUT_BYTE: |
| 598 | case Instruction::IPUT_CHAR: |
| 599 | case Instruction::IPUT_SHORT: { |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 600 | ArtField* field = Runtime::Current()->GetClassLinker()->ResolveField( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 601 | instr.VRegC_22c(), method, /* is_static= */ false); |
Andreas Gampe | 7130744 | 2018-02-06 13:38:03 -0800 | [diff] [blame] | 602 | Thread::Current()->ClearException(); // Resolution may fail, ignore. |
Santiago Aboy Solanes | 14e8d64 | 2022-02-04 14:15:01 +0000 | [diff] [blame] | 603 | ThrowNullPointerExceptionForFieldAccess(field, method, /* is_read= */ false); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 604 | break; |
| 605 | } |
| 606 | case Instruction::AGET: |
| 607 | case Instruction::AGET_WIDE: |
| 608 | case Instruction::AGET_OBJECT: |
| 609 | case Instruction::AGET_BOOLEAN: |
| 610 | case Instruction::AGET_BYTE: |
| 611 | case Instruction::AGET_CHAR: |
| 612 | case Instruction::AGET_SHORT: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 613 | ThrowException("Ljava/lang/NullPointerException;", nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 614 | "Attempt to read from null array"); |
| 615 | break; |
| 616 | case Instruction::APUT: |
| 617 | case Instruction::APUT_WIDE: |
| 618 | case Instruction::APUT_OBJECT: |
| 619 | case Instruction::APUT_BOOLEAN: |
| 620 | case Instruction::APUT_BYTE: |
| 621 | case Instruction::APUT_CHAR: |
| 622 | case Instruction::APUT_SHORT: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 623 | ThrowException("Ljava/lang/NullPointerException;", nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 624 | "Attempt to write to null array"); |
| 625 | break; |
| 626 | case Instruction::ARRAY_LENGTH: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 627 | ThrowException("Ljava/lang/NullPointerException;", nullptr, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 628 | "Attempt to get length of null array"); |
| 629 | break; |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 630 | case Instruction::FILL_ARRAY_DATA: { |
| 631 | ThrowException("Ljava/lang/NullPointerException;", nullptr, |
| 632 | "Attempt to write to null array"); |
| 633 | break; |
| 634 | } |
Nicolas Geoffray | 7f0ae73 | 2016-06-29 14:54:35 +0100 | [diff] [blame] | 635 | case Instruction::MONITOR_ENTER: |
| 636 | case Instruction::MONITOR_EXIT: { |
| 637 | ThrowException("Ljava/lang/NullPointerException;", nullptr, |
| 638 | "Attempt to do a synchronize operation on a null object"); |
| 639 | break; |
| 640 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 641 | default: { |
Vladimir Marko | 7e09737 | 2018-11-28 16:40:59 +0000 | [diff] [blame] | 642 | const DexFile* dex_file = method->GetDexFile(); |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 643 | LOG(FATAL) << "NullPointerException at an unexpected instruction: " |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 644 | << instr.DumpString(dex_file) |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 645 | << " in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 646 | << method->PrettyMethod(); |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 647 | UNREACHABLE(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 652 | void ThrowNullPointerException(const char* msg) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 653 | ThrowException("Ljava/lang/NullPointerException;", nullptr, msg); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Nicolas Geoffray | 635967b | 2019-08-07 10:15:39 +0100 | [diff] [blame] | 656 | void ThrowNullPointerException() { |
| 657 | ThrowException("Ljava/lang/NullPointerException;"); |
| 658 | } |
| 659 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 660 | // ReadOnlyBufferException |
| 661 | |
| 662 | void ThrowReadOnlyBufferException() { |
| 663 | Thread::Current()->ThrowNewException("Ljava/nio/ReadOnlyBufferException;", nullptr); |
| 664 | } |
| 665 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 666 | // RuntimeException |
| 667 | |
| 668 | void ThrowRuntimeException(const char* fmt, ...) { |
| 669 | va_list args; |
| 670 | va_start(args, fmt); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 671 | ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 672 | va_end(args); |
| 673 | } |
| 674 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 675 | // SecurityException |
| 676 | |
| 677 | void ThrowSecurityException(const char* fmt, ...) { |
| 678 | va_list args; |
| 679 | va_start(args, fmt); |
| 680 | ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args); |
| 681 | va_end(args); |
| 682 | } |
| 683 | |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 684 | // Stack overflow. |
| 685 | |
| 686 | void ThrowStackOverflowError(Thread* self) { |
| 687 | if (self->IsHandlingStackOverflow()) { |
| 688 | LOG(ERROR) << "Recursive stack overflow."; |
| 689 | // We don't fail here because SetStackEndForStackOverflow will print better diagnostics. |
| 690 | } |
| 691 | |
| 692 | self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute. |
| 693 | JNIEnvExt* env = self->GetJniEnv(); |
| 694 | std::string msg("stack size "); |
| 695 | msg += PrettySize(self->GetStackSize()); |
| 696 | |
| 697 | // Avoid running Java code for exception initialization. |
| 698 | // TODO: Checks to make this a bit less brittle. |
Andreas Gampe | 5ad2e06 | 2018-10-01 15:35:47 +0000 | [diff] [blame] | 699 | // |
| 700 | // Note: this lambda ensures that the destruction of the ScopedLocalRefs will run in the extended |
| 701 | // stack, which is important for modes with larger stack sizes (e.g., ASAN). Using a lambda |
| 702 | // instead of a block simplifies the control flow. |
| 703 | auto create_and_throw = [&]() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 704 | // Allocate an uninitialized object. |
| 705 | ScopedLocalRef<jobject> exc(env, |
| 706 | env->AllocObject(WellKnownClasses::java_lang_StackOverflowError)); |
| 707 | if (exc == nullptr) { |
| 708 | LOG(WARNING) << "Could not allocate StackOverflowError object."; |
| 709 | return; |
| 710 | } |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 711 | |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 712 | // "Initialize". |
| 713 | // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object. |
| 714 | // Only Throwable has "custom" fields: |
| 715 | // String detailMessage. |
| 716 | // Throwable cause (= this). |
| 717 | // List<Throwable> suppressedExceptions (= Collections.emptyList()). |
| 718 | // Object stackState; |
| 719 | // StackTraceElement[] stackTrace; |
| 720 | // Only Throwable has a non-empty constructor: |
Neil Fuller | c0f02d4 | 2018-06-11 09:49:01 +0000 | [diff] [blame] | 721 | // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT; |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 722 | // fillInStackTrace(); |
| 723 | |
| 724 | // detailMessage. |
| 725 | // TODO: Use String::FromModifiedUTF...? |
| 726 | ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str())); |
Andreas Gampe | 5ad2e06 | 2018-10-01 15:35:47 +0000 | [diff] [blame] | 727 | if (s == nullptr) { |
| 728 | LOG(WARNING) << "Could not throw new StackOverflowError because JNI NewStringUTF failed."; |
| 729 | return; |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 730 | } |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 731 | |
Andreas Gampe | 5ad2e06 | 2018-10-01 15:35:47 +0000 | [diff] [blame] | 732 | env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get()); |
| 733 | |
| 734 | // cause. |
| 735 | env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get()); |
| 736 | |
| 737 | // suppressedExceptions. |
| 738 | ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField( |
| 739 | WellKnownClasses::java_util_Collections, |
| 740 | WellKnownClasses::java_util_Collections_EMPTY_LIST)); |
| 741 | CHECK(emptylist != nullptr); |
| 742 | env->SetObjectField(exc.get(), |
| 743 | WellKnownClasses::java_lang_Throwable_suppressedExceptions, |
| 744 | emptylist.get()); |
| 745 | |
| 746 | // stackState is set as result of fillInStackTrace. fillInStackTrace calls |
| 747 | // nativeFillInStackTrace. |
| 748 | ScopedLocalRef<jobject> stack_state_val(env, nullptr); |
| 749 | { |
| 750 | ScopedObjectAccessUnchecked soa(env); // TODO: Is this necessary? |
Vladimir Marko | d34b73b | 2020-05-05 10:07:59 +0100 | [diff] [blame] | 751 | stack_state_val.reset(soa.Self()->CreateInternalStackTrace(soa)); |
Andreas Gampe | 5ad2e06 | 2018-10-01 15:35:47 +0000 | [diff] [blame] | 752 | } |
| 753 | if (stack_state_val != nullptr) { |
| 754 | env->SetObjectField(exc.get(), |
| 755 | WellKnownClasses::java_lang_Throwable_stackState, |
| 756 | stack_state_val.get()); |
| 757 | |
| 758 | // stackTrace. |
| 759 | ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField( |
| 760 | WellKnownClasses::libcore_util_EmptyArray, |
| 761 | WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT)); |
| 762 | env->SetObjectField(exc.get(), |
| 763 | WellKnownClasses::java_lang_Throwable_stackTrace, |
| 764 | stack_trace_elem.get()); |
| 765 | } else { |
| 766 | LOG(WARNING) << "Could not create stack trace."; |
| 767 | // Note: we'll create an exception without stack state, which is valid. |
| 768 | } |
| 769 | |
| 770 | // Throw the exception. |
| 771 | self->SetException(self->DecodeJObject(exc.get())->AsThrowable()); |
| 772 | }; |
| 773 | create_and_throw(); |
| 774 | CHECK(self->IsExceptionPending()); |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 775 | |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 776 | self->ResetDefaultStackEnd(); // Return to default stack size. |
| 777 | |
| 778 | // And restore protection if implicit checks are on. |
Nicolas Geoffray | 541ca32 | 2022-01-20 09:03:04 +0000 | [diff] [blame] | 779 | if (Runtime::Current()->GetImplicitStackOverflowChecks()) { |
Andreas Gampe | 103992b | 2016-01-04 15:32:43 -0800 | [diff] [blame] | 780 | self->ProtectStack(); |
| 781 | } |
| 782 | } |
| 783 | |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 784 | // StringIndexOutOfBoundsException |
| 785 | |
| 786 | void ThrowStringIndexOutOfBoundsException(int index, int length) { |
| 787 | ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr, |
| 788 | StringPrintf("length=%d; index=%d", length, index).c_str()); |
| 789 | } |
| 790 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 791 | // UnsupportedOperationException |
| 792 | |
| 793 | void ThrowUnsupportedOperationException() { |
| 794 | ThrowException("Ljava/lang/UnsupportedOperationException;"); |
| 795 | } |
| 796 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 797 | // VerifyError |
| 798 | |
Mathieu Chartier | 6b3d12b | 2016-10-13 13:59:58 -0700 | [diff] [blame] | 799 | void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 800 | va_list args; |
| 801 | va_start(args, fmt); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 802 | ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 803 | va_end(args); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 806 | // WrongMethodTypeException |
| 807 | |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 808 | void ThrowWrongMethodTypeException(ObjPtr<mirror::MethodType> expected_type, |
| 809 | ObjPtr<mirror::MethodType> actual_type) { |
Orion Hodson | 3f38346 | 2018-05-17 14:03:39 +0100 | [diff] [blame] | 810 | ThrowWrongMethodTypeException(expected_type->PrettyDescriptor(), actual_type->PrettyDescriptor()); |
| 811 | } |
| 812 | |
| 813 | void ThrowWrongMethodTypeException(const std::string& expected_descriptor, |
| 814 | const std::string& actual_descriptor) { |
| 815 | std::ostringstream msg; |
| 816 | msg << "Expected " << expected_descriptor << " but was " << actual_descriptor; |
| 817 | ThrowException("Ljava/lang/invoke/WrongMethodTypeException;", nullptr, msg.str().c_str()); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 820 | } // namespace art |