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 | |
| 29 | static void AddReferrerLocation(std::ostream& os, const Method* 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 | |
| 61 | void ThrowNullPointerExceptionForMethodAccess(Method* caller, uint32_t method_idx, |
| 62 | InvokeType type) { |
| 63 | DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache(); |
| 64 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
| 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 | |
| 71 | void ThrowNullPointerExceptionFromDexPC(Method* throw_method, uint32_t dex_pc) { |
| 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 | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 136 | const DexFile& dex_file = Runtime::Current()->GetClassLinker() |
| 137 | ->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache()); |
| 138 | std::string message("Null pointer exception during instruction '"); |
| 139 | message += instr->DumpString(&dex_file); |
| 140 | message += "'"; |
| 141 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str()); |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // IllegalAccessError |
| 148 | |
| 149 | void ThrowIllegalAccessErrorClass(Class* referrer, Class* accessed) { |
| 150 | std::ostringstream msg; |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame^] | 151 | msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 152 | << PrettyDescriptor(accessed) << "'"; |
| 153 | AddReferrerLocationFromClass(msg, referrer); |
| 154 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 155 | } |
| 156 | |
| 157 | void ThrowIllegalAccessErrorClassForMethodDispatch(Class* referrer, Class* accessed, |
| 158 | const Method* caller, |
| 159 | const Method* called, |
| 160 | InvokeType type) { |
| 161 | std::ostringstream msg; |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame^] | 162 | msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '" |
| 163 | << PrettyDescriptor(accessed) << "') in attempt to invoke " << type |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 164 | << " method " << PrettyMethod(called).c_str(); |
| 165 | AddReferrerLocation(msg, caller); |
| 166 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 167 | } |
| 168 | |
| 169 | void ThrowIllegalAccessErrorMethod(Class* referrer, Method* accessed) { |
| 170 | std::ostringstream msg; |
| 171 | msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '" |
| 172 | << PrettyDescriptor(referrer) << "'"; |
| 173 | AddReferrerLocationFromClass(msg, referrer); |
| 174 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 175 | } |
| 176 | |
| 177 | void ThrowIllegalAccessErrorField(Class* referrer, Field* accessed) { |
| 178 | std::ostringstream msg; |
| 179 | msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '" |
| 180 | << PrettyDescriptor(referrer) << "'"; |
| 181 | AddReferrerLocationFromClass(msg, referrer); |
| 182 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 183 | } |
| 184 | |
| 185 | void ThrowIllegalAccessErrorFinalField(const Method* referrer, Field* accessed) { |
| 186 | std::ostringstream msg; |
| 187 | msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '" |
| 188 | << PrettyMethod(referrer) << "'"; |
| 189 | AddReferrerLocation(msg, referrer); |
| 190 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str()); |
| 191 | } |
| 192 | |
| 193 | // IncompatibleClassChangeError |
| 194 | |
| 195 | void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type, |
| 196 | Method* method, const Method* referrer) { |
| 197 | std::ostringstream msg; |
| 198 | msg << "The method '" << PrettyMethod(method) << "' was expected to be of type " |
| 199 | << expected_type << " but instead was found to be of type " << found_type; |
| 200 | AddReferrerLocation(msg, referrer); |
| 201 | Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;", |
| 202 | msg.str().c_str()); |
| 203 | } |
| 204 | |
| 205 | void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const Method* interface_method, |
| 206 | Object* this_object, |
| 207 | const Method* referrer) { |
| 208 | // Referrer is calling interface_method on this_object, however, the interface_method isn't |
| 209 | // implemented by this_object. |
| 210 | CHECK(this_object != NULL); |
| 211 | std::ostringstream msg; |
| 212 | msg << "Class '" << PrettyDescriptor(this_object->GetClass()) |
| 213 | << "' does not implement interface '" |
| 214 | << PrettyDescriptor(interface_method->GetDeclaringClass()) |
| 215 | << "' in call to '" << PrettyMethod(interface_method) << "'"; |
| 216 | AddReferrerLocation(msg, referrer); |
| 217 | Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;", |
| 218 | msg.str().c_str()); |
| 219 | } |
| 220 | |
| 221 | void ThrowIncompatibleClassChangeErrorField(const Field* resolved_field, bool is_static, |
| 222 | const Method* referrer) { |
| 223 | std::ostringstream msg; |
| 224 | msg << "Expected '" << PrettyField(resolved_field) << "' to be a " |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame^] | 225 | << (is_static ? "static" : "instance") << " field" << " rather than a " |
| 226 | << (is_static ? "instance" : "static") << " field"; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 227 | AddReferrerLocation(msg, referrer); |
| 228 | Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;", |
| 229 | msg.str().c_str()); |
| 230 | } |
| 231 | |
| 232 | // NoSuchMethodError |
| 233 | |
| 234 | void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name, |
| 235 | const StringPiece& signature, const Method* referrer) { |
| 236 | std::ostringstream msg; |
| 237 | ClassHelper kh(c); |
| 238 | msg << "No " << type << " method " << name << signature |
| 239 | << " in class " << kh.GetDescriptor() << " or its super classes"; |
| 240 | AddReferrerLocation(msg, referrer); |
| 241 | Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str()); |
| 242 | } |
| 243 | |
| 244 | void ThrowNoSuchMethodError(uint32_t method_idx, const Method* referrer) { |
| 245 | DexCache* dex_cache = referrer->GetDeclaringClass()->GetDexCache(); |
| 246 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
| 247 | std::ostringstream msg; |
| 248 | msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'"; |
| 249 | AddReferrerLocation(msg, referrer); |
| 250 | Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str()); |
| 251 | } |
| 252 | |
| 253 | } // namespace art |