blob: 734d544bd2b0816032af1d4eb9979514f9c21858 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
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 Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "class_linker-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070021#include "dex_instruction.h"
22#include "invoke_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/abstract_method-inl.h"
24#include "mirror/object-inl.h"
25#include "mirror/object_array-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070026#include "object_utils.h"
27#include "thread.h"
28
29#include <sstream>
30
31namespace art {
32
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033static void AddReferrerLocation(std::ostream& os, const mirror::AbstractMethod* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -070034 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070035 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 Rogers2dd0e2c2013-01-24 12:42:14 -080044static void AddReferrerLocationFromClass(std::ostream& os, mirror::Class* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -070045 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070046 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 Rogers2dd0e2c2013-01-24 12:42:14 -080058void ThrowNullPointerExceptionForFieldAccess(mirror::Field* field, bool is_read) {
Ian Rogers87e552d2012-08-31 15:54:48 -070059 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 Rogers2dd0e2c2013-01-24 12:42:14 -080065void ThrowNullPointerExceptionForMethodAccess(mirror::AbstractMethod* caller, uint32_t method_idx,
Ian Rogers87e552d2012-08-31 15:54:48 -070066 InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 mirror::DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -070068 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -070069 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -070070 msg << "Attempt to invoke " << type << " method '"
Ian Rogers87e552d2012-08-31 15:54:48 -070071 << 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 Rogers2dd0e2c2013-01-24 12:42:14 -080075void ThrowNullPointerExceptionFromDexPC(mirror::AbstractMethod* throw_method, uint32_t dex_pc) {
Ian Rogers87e552d2012-08-31 15:54:48 -070076 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 Rogers137e88f2012-10-08 17:46:47 -070089 case Instruction::INVOKE_INTERFACE:
90 case Instruction::INVOKE_INTERFACE_RANGE:
91 ThrowNullPointerExceptionForMethodAccess(throw_method, dec_insn.vB, kInterface);
92 break;
Ian Rogers87e552d2012-08-31 15:54:48 -070093 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800100 mirror::Field* field =
Ian Rogers87e552d2012-08-31 15:54:48 -0700101 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800112 mirror::Field* field =
Ian Rogers87e552d2012-08-31 15:54:48 -0700113 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 Rogersb726dcb2012-09-05 08:57:23 -0700142 // 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 Rogers4445a7e2012-10-05 17:19:13 -0700144 const DexFile& dex_file = *throw_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700145 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800156void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700157 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700158 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700159 << PrettyDescriptor(accessed) << "'";
160 AddReferrerLocationFromClass(msg, referrer);
161 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
162}
163
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
165 const mirror::AbstractMethod* caller,
166 const mirror::AbstractMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700167 InvokeType type) {
168 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700169 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
170 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700171 << " method " << PrettyMethod(called).c_str();
172 AddReferrerLocation(msg, caller);
173 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
174}
175
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::AbstractMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700177 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800184void ThrowIllegalAccessErrorField(mirror::Class* referrer, mirror::Field* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700185 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800192void ThrowIllegalAccessErrorFinalField(const mirror::AbstractMethod* referrer,
193 mirror::Field* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700194 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
203void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204 mirror::AbstractMethod* method,
205 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700206 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800214void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const mirror::AbstractMethod* interface_method,
215 mirror::Object* this_object,
216 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700217 // 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800230void ThrowIncompatibleClassChangeErrorField(const mirror::Field* resolved_field, bool is_static,
231 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700232 std::ostringstream msg;
233 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 << (is_static ? "static" : "instance") << " field" << " rather than a "
235 << (is_static ? "instance" : "static") << " field";
Ian Rogers87e552d2012-08-31 15:54:48 -0700236 AddReferrerLocation(msg, referrer);
237 Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
238 msg.str().c_str());
239}
240
241// NoSuchMethodError
242
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
244 const StringPiece& signature, const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700245 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800253void ThrowNoSuchMethodError(uint32_t method_idx, const mirror::AbstractMethod* referrer) {
254 mirror::DexCache* dex_cache = referrer->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700255 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700256 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