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