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