1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common_throws.h"
#include "dex_instruction.h"
#include "invoke_type.h"
#include "logging.h"
#include "object_utils.h"
#include "thread.h"
#include <sstream>
namespace art {
static void AddReferrerLocation(std::ostream& os, const AbstractMethod* referrer)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
if (referrer != NULL) {
ClassHelper kh(referrer->GetDeclaringClass());
std::string location(kh.GetLocation());
if (!location.empty()) {
os << " (accessed from " << location << ")";
}
}
}
static void AddReferrerLocationFromClass(std::ostream& os, Class* referrer)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
if (referrer != NULL) {
ClassHelper kh(referrer);
std::string location(kh.GetLocation());
if (!location.empty()) {
os << " (declaration of '" << PrettyDescriptor(referrer)
<< "' appears in " << location << ")";
}
}
}
// NullPointerException
void ThrowNullPointerExceptionForFieldAccess(Field* field, bool is_read) {
std::ostringstream msg;
msg << "Attempt to " << (is_read ? "read from" : "write to")
<< " field '" << PrettyField(field, true) << "' on a null object reference";
Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", msg.str().c_str());
}
void ThrowNullPointerExceptionForMethodAccess(AbstractMethod* caller, uint32_t method_idx,
InvokeType type) {
DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
const DexFile& dex_file = *dex_cache->GetDexFile();
std::ostringstream msg;
msg << "Attempt to invoke " << type << " method '"
<< PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", msg.str().c_str());
}
void ThrowNullPointerExceptionFromDexPC(AbstractMethod* throw_method, uint32_t dex_pc) {
const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem();
CHECK_LT(dex_pc, code->insns_size_in_code_units_);
const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
DecodedInstruction dec_insn(instr);
switch (instr->Opcode()) {
case Instruction::INVOKE_DIRECT:
case Instruction::INVOKE_DIRECT_RANGE:
ThrowNullPointerExceptionForMethodAccess(throw_method, dec_insn.vB, kDirect);
break;
case Instruction::INVOKE_VIRTUAL:
case Instruction::INVOKE_VIRTUAL_RANGE:
ThrowNullPointerExceptionForMethodAccess(throw_method, dec_insn.vB, kVirtual);
break;
case Instruction::INVOKE_INTERFACE:
case Instruction::INVOKE_INTERFACE_RANGE:
ThrowNullPointerExceptionForMethodAccess(throw_method, dec_insn.vB, kInterface);
break;
case Instruction::IGET:
case Instruction::IGET_WIDE:
case Instruction::IGET_OBJECT:
case Instruction::IGET_BOOLEAN:
case Instruction::IGET_BYTE:
case Instruction::IGET_CHAR:
case Instruction::IGET_SHORT: {
Field* field =
Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
break;
}
case Instruction::IPUT:
case Instruction::IPUT_WIDE:
case Instruction::IPUT_OBJECT:
case Instruction::IPUT_BOOLEAN:
case Instruction::IPUT_BYTE:
case Instruction::IPUT_CHAR:
case Instruction::IPUT_SHORT: {
Field* field =
Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
break;
}
case Instruction::AGET:
case Instruction::AGET_WIDE:
case Instruction::AGET_OBJECT:
case Instruction::AGET_BOOLEAN:
case Instruction::AGET_BYTE:
case Instruction::AGET_CHAR:
case Instruction::AGET_SHORT:
Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
"Attempt to read from null array");
break;
case Instruction::APUT:
case Instruction::APUT_WIDE:
case Instruction::APUT_OBJECT:
case Instruction::APUT_BOOLEAN:
case Instruction::APUT_BYTE:
case Instruction::APUT_CHAR:
case Instruction::APUT_SHORT:
Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
"Attempt to write to null array");
break;
case Instruction::ARRAY_LENGTH:
Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;",
"Attempt to get length of null array");
break;
default: {
// TODO: We should have covered all the cases where we expect a NPE above, this
// message/logging is so we can improve any cases we've missed in the future.
const DexFile& dex_file = *throw_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
std::string message("Null pointer exception during instruction '");
message += instr->DumpString(&dex_file);
message += "'";
Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str());
break;
}
}
}
// IllegalAccessError
void ThrowIllegalAccessErrorClass(Class* referrer, Class* accessed) {
std::ostringstream msg;
msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
<< PrettyDescriptor(accessed) << "'";
AddReferrerLocationFromClass(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
}
void ThrowIllegalAccessErrorClassForMethodDispatch(Class* referrer, Class* accessed,
const AbstractMethod* caller,
const AbstractMethod* called,
InvokeType type) {
std::ostringstream msg;
msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
<< PrettyDescriptor(accessed) << "') in attempt to invoke " << type
<< " method " << PrettyMethod(called).c_str();
AddReferrerLocation(msg, caller);
Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
}
void ThrowIllegalAccessErrorMethod(Class* referrer, AbstractMethod* accessed) {
std::ostringstream msg;
msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
<< PrettyDescriptor(referrer) << "'";
AddReferrerLocationFromClass(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
}
void ThrowIllegalAccessErrorField(Class* referrer, Field* accessed) {
std::ostringstream msg;
msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
<< PrettyDescriptor(referrer) << "'";
AddReferrerLocationFromClass(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
}
void ThrowIllegalAccessErrorFinalField(const AbstractMethod* referrer, Field* accessed) {
std::ostringstream msg;
msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
<< PrettyMethod(referrer) << "'";
AddReferrerLocation(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessError;", msg.str().c_str());
}
// IncompatibleClassChangeError
void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
AbstractMethod* method, const AbstractMethod* referrer) {
std::ostringstream msg;
msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
<< expected_type << " but instead was found to be of type " << found_type;
AddReferrerLocation(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
msg.str().c_str());
}
void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const AbstractMethod* interface_method,
Object* this_object,
const AbstractMethod* referrer) {
// Referrer is calling interface_method on this_object, however, the interface_method isn't
// implemented by this_object.
CHECK(this_object != NULL);
std::ostringstream msg;
msg << "Class '" << PrettyDescriptor(this_object->GetClass())
<< "' does not implement interface '"
<< PrettyDescriptor(interface_method->GetDeclaringClass())
<< "' in call to '" << PrettyMethod(interface_method) << "'";
AddReferrerLocation(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
msg.str().c_str());
}
void ThrowIncompatibleClassChangeErrorField(const Field* resolved_field, bool is_static,
const AbstractMethod* referrer) {
std::ostringstream msg;
msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
<< (is_static ? "static" : "instance") << " field" << " rather than a "
<< (is_static ? "instance" : "static") << " field";
AddReferrerLocation(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;",
msg.str().c_str());
}
// NoSuchMethodError
void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name,
const StringPiece& signature, const AbstractMethod* referrer) {
std::ostringstream msg;
ClassHelper kh(c);
msg << "No " << type << " method " << name << signature
<< " in class " << kh.GetDescriptor() << " or its super classes";
AddReferrerLocation(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str());
}
void ThrowNoSuchMethodError(uint32_t method_idx, const AbstractMethod* referrer) {
DexCache* dex_cache = referrer->GetDeclaringClass()->GetDexCache();
const DexFile& dex_file = *dex_cache->GetDexFile();
std::ostringstream msg;
msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
AddReferrerLocation(msg, referrer);
Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str());
}
} // namespace art
|