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