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