blob: cefc4abdf90f342b206895d5ce585e9b426f8962 [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 Rogers87e552d2012-08-31 15:54:48 -070020#include "dex_instruction.h"
21#include "invoke_type.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070022#include "object_utils.h"
23#include "thread.h"
24
25#include <sstream>
26
27namespace art {
28
Mathieu Chartier66f19252012-09-18 08:57:04 -070029static void AddReferrerLocation(std::ostream& os, const AbstractMethod* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -070030 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070031 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
40static void AddReferrerLocationFromClass(std::ostream& os, Class* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -070041 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070042 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
54void 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 Chartier66f19252012-09-18 08:57:04 -070061void ThrowNullPointerExceptionForMethodAccess(AbstractMethod* caller, uint32_t method_idx,
Ian Rogers87e552d2012-08-31 15:54:48 -070062 InvokeType type) {
63 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -070064 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -070065 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -070066 msg << "Attempt to invoke " << type << " method '"
Ian Rogers87e552d2012-08-31 15:54:48 -070067 << 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 Chartier66f19252012-09-18 08:57:04 -070071void ThrowNullPointerExceptionFromDexPC(AbstractMethod* throw_method, uint32_t dex_pc) {
Ian Rogers87e552d2012-08-31 15:54:48 -070072 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;
Ian Rogers137e88f2012-10-08 17:46:47 -070085 case Instruction::INVOKE_INTERFACE:
86 case Instruction::INVOKE_INTERFACE_RANGE:
87 ThrowNullPointerExceptionForMethodAccess(throw_method, dec_insn.vB, kInterface);
88 break;
Ian Rogers87e552d2012-08-31 15:54:48 -070089 case Instruction::IGET:
90 case Instruction::IGET_WIDE:
91 case Instruction::IGET_OBJECT:
92 case Instruction::IGET_BOOLEAN:
93 case Instruction::IGET_BYTE:
94 case Instruction::IGET_CHAR:
95 case Instruction::IGET_SHORT: {
96 Field* field =
97 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
98 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
99 break;
100 }
101 case Instruction::IPUT:
102 case Instruction::IPUT_WIDE:
103 case Instruction::IPUT_OBJECT:
104 case Instruction::IPUT_BOOLEAN:
105 case Instruction::IPUT_BYTE:
106 case Instruction::IPUT_CHAR:
107 case Instruction::IPUT_SHORT: {
108 Field* field =
109 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
110 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
111 break;
112 }
113 case Instruction::AGET:
114 case Instruction::AGET_WIDE:
115 case Instruction::AGET_OBJECT:
116 case Instruction::AGET_BOOLEAN:
117 case Instruction::AGET_BYTE:
118 case Instruction::AGET_CHAR:
119 case Instruction::AGET_SHORT:
120 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
121 "Attempt to read from null array");
122 break;
123 case Instruction::APUT:
124 case Instruction::APUT_WIDE:
125 case Instruction::APUT_OBJECT:
126 case Instruction::APUT_BOOLEAN:
127 case Instruction::APUT_BYTE:
128 case Instruction::APUT_CHAR:
129 case Instruction::APUT_SHORT:
130 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
131 "Attempt to write to null array");
132 break;
133 case Instruction::ARRAY_LENGTH:
134 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
135 "Attempt to get length of null array");
136 break;
137 default: {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700138 // TODO: We should have covered all the cases where we expect a NPE above, this
139 // message/logging is so we can improve any cases we've missed in the future.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700140 const DexFile& dex_file = *throw_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700141 std::string message("Null pointer exception during instruction '");
142 message += instr->DumpString(&dex_file);
143 message += "'";
144 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str());
145 break;
146 }
147 }
148}
149
150// IllegalAccessError
151
152void ThrowIllegalAccessErrorClass(Class* referrer, Class* accessed) {
153 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700155 << PrettyDescriptor(accessed) << "'";
156 AddReferrerLocationFromClass(msg, referrer);
157 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
158}
159
160void ThrowIllegalAccessErrorClassForMethodDispatch(Class* referrer, Class* accessed,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700161 const AbstractMethod* caller,
162 const AbstractMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700163 InvokeType type) {
164 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
166 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700167 << " method " << PrettyMethod(called).c_str();
168 AddReferrerLocation(msg, caller);
169 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
170}
171
Mathieu Chartier66f19252012-09-18 08:57:04 -0700172void ThrowIllegalAccessErrorMethod(Class* referrer, AbstractMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700173 std::ostringstream msg;
174 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
175 << PrettyDescriptor(referrer) << "'";
176 AddReferrerLocationFromClass(msg, referrer);
177 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
178}
179
180void ThrowIllegalAccessErrorField(Class* referrer, Field* accessed) {
181 std::ostringstream msg;
182 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
183 << PrettyDescriptor(referrer) << "'";
184 AddReferrerLocationFromClass(msg, referrer);
185 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
186}
187
Mathieu Chartier66f19252012-09-18 08:57:04 -0700188void ThrowIllegalAccessErrorFinalField(const AbstractMethod* referrer, Field* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700189 std::ostringstream msg;
190 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
191 << PrettyMethod(referrer) << "'";
192 AddReferrerLocation(msg, referrer);
193 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
194}
195
196// IncompatibleClassChangeError
197
198void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700199 AbstractMethod* method, const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700200 std::ostringstream msg;
201 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
202 << expected_type << " but instead was found to be of type " << found_type;
203 AddReferrerLocation(msg, referrer);
204 Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
205 msg.str().c_str());
206}
207
Mathieu Chartier66f19252012-09-18 08:57:04 -0700208void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const AbstractMethod* interface_method,
Ian Rogers87e552d2012-08-31 15:54:48 -0700209 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700210 const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700211 // Referrer is calling interface_method on this_object, however, the interface_method isn't
212 // implemented by this_object.
213 CHECK(this_object != NULL);
214 std::ostringstream msg;
215 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
216 << "' does not implement interface '"
217 << PrettyDescriptor(interface_method->GetDeclaringClass())
218 << "' in call to '" << PrettyMethod(interface_method) << "'";
219 AddReferrerLocation(msg, referrer);
220 Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
221 msg.str().c_str());
222}
223
224void ThrowIncompatibleClassChangeErrorField(const Field* resolved_field, bool is_static,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700225 const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700226 std::ostringstream msg;
227 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700228 << (is_static ? "static" : "instance") << " field" << " rather than a "
229 << (is_static ? "instance" : "static") << " field";
Ian Rogers87e552d2012-08-31 15:54:48 -0700230 AddReferrerLocation(msg, referrer);
231 Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
232 msg.str().c_str());
233}
234
235// NoSuchMethodError
236
237void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700238 const StringPiece& signature, const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700239 std::ostringstream msg;
240 ClassHelper kh(c);
241 msg << "No " << type << " method " << name << signature
242 << " in class " << kh.GetDescriptor() << " or its super classes";
243 AddReferrerLocation(msg, referrer);
244 Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str());
245}
246
Mathieu Chartier66f19252012-09-18 08:57:04 -0700247void ThrowNoSuchMethodError(uint32_t method_idx, const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700248 DexCache* dex_cache = referrer->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700249 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700250 std::ostringstream msg;
251 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
252 AddReferrerLocation(msg, referrer);
253 Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str());
254}
255
256} // namespace art