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