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