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