blob: 9fb686a26ae9f8433784cff11b749385002f1df3 [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
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
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;
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 Rogersb726dcb2012-09-05 08:57:23 -0700134 // 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 Rogers4445a7e2012-10-05 17:19:13 -0700136 const DexFile& dex_file = *throw_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700137 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
148void ThrowIllegalAccessErrorClass(Class* referrer, Class* accessed) {
149 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700151 << PrettyDescriptor(accessed) << "'";
152 AddReferrerLocationFromClass(msg, referrer);
153 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
154}
155
156void ThrowIllegalAccessErrorClassForMethodDispatch(Class* referrer, Class* accessed,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700157 const AbstractMethod* caller,
158 const AbstractMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700159 InvokeType type) {
160 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
162 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700163 << " method " << PrettyMethod(called).c_str();
164 AddReferrerLocation(msg, caller);
165 Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
166}
167
Mathieu Chartier66f19252012-09-18 08:57:04 -0700168void ThrowIllegalAccessErrorMethod(Class* referrer, AbstractMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700169 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
176void 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 Chartier66f19252012-09-18 08:57:04 -0700184void ThrowIllegalAccessErrorFinalField(const AbstractMethod* referrer, Field* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700185 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
194void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700195 AbstractMethod* method, const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700196 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 Chartier66f19252012-09-18 08:57:04 -0700204void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const AbstractMethod* interface_method,
Ian Rogers87e552d2012-08-31 15:54:48 -0700205 Object* this_object,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700206 const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700207 // 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
220void ThrowIncompatibleClassChangeErrorField(const Field* resolved_field, bool is_static,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700221 const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700222 std::ostringstream msg;
223 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700224 << (is_static ? "static" : "instance") << " field" << " rather than a "
225 << (is_static ? "instance" : "static") << " field";
Ian Rogers87e552d2012-08-31 15:54:48 -0700226 AddReferrerLocation(msg, referrer);
227 Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
228 msg.str().c_str());
229}
230
231// NoSuchMethodError
232
233void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700234 const StringPiece& signature, const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700235 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 Chartier66f19252012-09-18 08:57:04 -0700243void ThrowNoSuchMethodError(uint32_t method_idx, const AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700244 DexCache* dex_cache = referrer->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700245 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700246 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