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