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