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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 20 | #include "class_linker-inl.h" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 21 | #include "dex_instruction.h" |
| 22 | #include "invoke_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 23 | #include "mirror/abstract_method-inl.h" |
| 24 | #include "mirror/object-inl.h" |
| 25 | #include "mirror/object_array-inl.h" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 26 | #include "object_utils.h" |
| 27 | #include "thread.h" |
| 28 | |
| 29 | #include <sstream> |
| 30 | |
| 31 | namespace art { |
| 32 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 33 | static void AddReferrerLocation(std::ostream& os, const mirror::AbstractMethod* referrer) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 34 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 35 | if (referrer != NULL) { |
| 36 | ClassHelper kh(referrer->GetDeclaringClass()); |
| 37 | std::string location(kh.GetLocation()); |
| 38 | if (!location.empty()) { |
| 39 | os << " (accessed from " << location << ")"; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 44 | static void AddReferrerLocationFromClass(std::ostream& os, mirror::Class* referrer) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 45 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 46 | if (referrer != NULL) { |
| 47 | ClassHelper kh(referrer); |
| 48 | std::string location(kh.GetLocation()); |
| 49 | if (!location.empty()) { |
| 50 | os << " (declaration of '" << PrettyDescriptor(referrer) |
| 51 | << "' appears in " << location << ")"; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // NullPointerException |
| 57 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 58 | void ThrowNullPointerExceptionForFieldAccess(mirror::Field* field, bool is_read) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 59 | std::ostringstream msg; |
| 60 | msg << "Attempt to " << (is_read ? "read from" : "write to") |
| 61 | << " field '" << PrettyField(field, true) << "' on a null object reference"; |
| 62 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", msg.str().c_str()); |
| 63 | } |
| 64 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 65 | void ThrowNullPointerExceptionForMethodAccess(mirror::AbstractMethod* caller, uint32_t method_idx, |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 66 | InvokeType type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 67 | mirror::DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 68 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 69 | std::ostringstream msg; |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 70 | msg << "Attempt to invoke " << type << " method '" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 71 | << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference"; |
| 72 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", msg.str().c_str()); |
| 73 | } |
| 74 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 75 | void ThrowNullPointerExceptionFromDexPC(mirror::AbstractMethod* throw_method, uint32_t dex_pc) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 76 | const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem(); |
| 77 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 78 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
| 79 | DecodedInstruction dec_insn(instr); |
| 80 | switch (instr->Opcode()) { |
| 81 | case Instruction::INVOKE_DIRECT: |
| 82 | case Instruction::INVOKE_DIRECT_RANGE: |
| 83 | ThrowNullPointerExceptionForMethodAccess(throw_method, dec_insn.vB, kDirect); |
| 84 | break; |
| 85 | case Instruction::INVOKE_VIRTUAL: |
| 86 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 87 | ThrowNullPointerExceptionForMethodAccess(throw_method, dec_insn.vB, kVirtual); |
| 88 | break; |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 89 | case Instruction::INVOKE_INTERFACE: |
| 90 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 91 | ThrowNullPointerExceptionForMethodAccess(throw_method, dec_insn.vB, kInterface); |
| 92 | break; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 93 | case Instruction::IGET: |
| 94 | case Instruction::IGET_WIDE: |
| 95 | case Instruction::IGET_OBJECT: |
| 96 | case Instruction::IGET_BOOLEAN: |
| 97 | case Instruction::IGET_BYTE: |
| 98 | case Instruction::IGET_CHAR: |
| 99 | case Instruction::IGET_SHORT: { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 100 | mirror::Field* field = |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 101 | Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false); |
| 102 | ThrowNullPointerExceptionForFieldAccess(field, true /* read */); |
| 103 | break; |
| 104 | } |
| 105 | case Instruction::IPUT: |
| 106 | case Instruction::IPUT_WIDE: |
| 107 | case Instruction::IPUT_OBJECT: |
| 108 | case Instruction::IPUT_BOOLEAN: |
| 109 | case Instruction::IPUT_BYTE: |
| 110 | case Instruction::IPUT_CHAR: |
| 111 | case Instruction::IPUT_SHORT: { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 112 | mirror::Field* field = |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 113 | Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false); |
| 114 | ThrowNullPointerExceptionForFieldAccess(field, false /* write */); |
| 115 | break; |
| 116 | } |
| 117 | case Instruction::AGET: |
| 118 | case Instruction::AGET_WIDE: |
| 119 | case Instruction::AGET_OBJECT: |
| 120 | case Instruction::AGET_BOOLEAN: |
| 121 | case Instruction::AGET_BYTE: |
| 122 | case Instruction::AGET_CHAR: |
| 123 | case Instruction::AGET_SHORT: |
| 124 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", |
| 125 | "Attempt to read from null array"); |
| 126 | break; |
| 127 | case Instruction::APUT: |
| 128 | case Instruction::APUT_WIDE: |
| 129 | case Instruction::APUT_OBJECT: |
| 130 | case Instruction::APUT_BOOLEAN: |
| 131 | case Instruction::APUT_BYTE: |
| 132 | case Instruction::APUT_CHAR: |
| 133 | case Instruction::APUT_SHORT: |
| 134 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", |
| 135 | "Attempt to write to null array"); |
| 136 | break; |
| 137 | case Instruction::ARRAY_LENGTH: |
| 138 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", |
| 139 | "Attempt to get length of null array"); |
| 140 | break; |
| 141 | default: { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 142 | // TODO: We should have covered all the cases where we expect a NPE above, this |
| 143 | // message/logging is so we can improve any cases we've missed in the future. |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 144 | const DexFile& dex_file = *throw_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 145 | std::string message("Null pointer exception during instruction '"); |
| 146 | message += instr->DumpString(&dex_file); |
| 147 | message += "'"; |
| 148 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str()); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // IllegalAccessError |
| 155 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 156 | void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 157 | std::ostringstream msg; |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 158 | msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 159 | << PrettyDescriptor(accessed) << "'"; |
| 160 | AddReferrerLocationFromClass(msg, referrer); |
| 161 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 162 | } |
| 163 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 164 | void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed, |
| 165 | const mirror::AbstractMethod* caller, |
| 166 | const mirror::AbstractMethod* called, |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 167 | InvokeType type) { |
| 168 | std::ostringstream msg; |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 169 | msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '" |
| 170 | << PrettyDescriptor(accessed) << "') in attempt to invoke " << type |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 171 | << " method " << PrettyMethod(called).c_str(); |
| 172 | AddReferrerLocation(msg, caller); |
| 173 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 174 | } |
| 175 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 176 | void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::AbstractMethod* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 177 | std::ostringstream msg; |
| 178 | msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '" |
| 179 | << PrettyDescriptor(referrer) << "'"; |
| 180 | AddReferrerLocationFromClass(msg, referrer); |
| 181 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 182 | } |
| 183 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 184 | void ThrowIllegalAccessErrorField(mirror::Class* referrer, mirror::Field* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 185 | std::ostringstream msg; |
| 186 | msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '" |
| 187 | << PrettyDescriptor(referrer) << "'"; |
| 188 | AddReferrerLocationFromClass(msg, referrer); |
| 189 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 190 | } |
| 191 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 192 | void ThrowIllegalAccessErrorFinalField(const mirror::AbstractMethod* referrer, |
| 193 | mirror::Field* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 194 | std::ostringstream msg; |
| 195 | msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '" |
| 196 | << PrettyMethod(referrer) << "'"; |
| 197 | AddReferrerLocation(msg, referrer); |
| 198 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 199 | } |
| 200 | |
| 201 | // IncompatibleClassChangeError |
| 202 | |
| 203 | void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 204 | mirror::AbstractMethod* method, |
| 205 | const mirror::AbstractMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 206 | std::ostringstream msg; |
| 207 | msg << "The method '" << PrettyMethod(method) << "' was expected to be of type " |
| 208 | << expected_type << " but instead was found to be of type " << found_type; |
| 209 | AddReferrerLocation(msg, referrer); |
| 210 | Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;", |
| 211 | msg.str().c_str()); |
| 212 | } |
| 213 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 214 | void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const mirror::AbstractMethod* interface_method, |
| 215 | mirror::Object* this_object, |
| 216 | const mirror::AbstractMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 217 | // Referrer is calling interface_method on this_object, however, the interface_method isn't |
| 218 | // implemented by this_object. |
| 219 | CHECK(this_object != NULL); |
| 220 | std::ostringstream msg; |
| 221 | msg << "Class '" << PrettyDescriptor(this_object->GetClass()) |
| 222 | << "' does not implement interface '" |
| 223 | << PrettyDescriptor(interface_method->GetDeclaringClass()) |
| 224 | << "' in call to '" << PrettyMethod(interface_method) << "'"; |
| 225 | AddReferrerLocation(msg, referrer); |
| 226 | Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;", |
| 227 | msg.str().c_str()); |
| 228 | } |
| 229 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 230 | void ThrowIncompatibleClassChangeErrorField(const mirror::Field* resolved_field, bool is_static, |
| 231 | const mirror::AbstractMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 232 | std::ostringstream msg; |
| 233 | msg << "Expected '" << PrettyField(resolved_field) << "' to be a " |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 234 | << (is_static ? "static" : "instance") << " field" << " rather than a " |
| 235 | << (is_static ? "instance" : "static") << " field"; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 236 | AddReferrerLocation(msg, referrer); |
| 237 | Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;", |
| 238 | msg.str().c_str()); |
| 239 | } |
| 240 | |
| 241 | // NoSuchMethodError |
| 242 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 243 | void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name, |
| 244 | const StringPiece& signature, const mirror::AbstractMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 245 | std::ostringstream msg; |
| 246 | ClassHelper kh(c); |
| 247 | msg << "No " << type << " method " << name << signature |
| 248 | << " in class " << kh.GetDescriptor() << " or its super classes"; |
| 249 | AddReferrerLocation(msg, referrer); |
| 250 | Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str()); |
| 251 | } |
| 252 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 253 | void ThrowNoSuchMethodError(uint32_t method_idx, const mirror::AbstractMethod* referrer) { |
| 254 | mirror::DexCache* dex_cache = referrer->GetDeclaringClass()->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 255 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 256 | std::ostringstream msg; |
| 257 | msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'"; |
| 258 | AddReferrerLocation(msg, referrer); |
| 259 | Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str()); |
| 260 | } |
| 261 | |
| 262 | } // namespace art |