blob: 0bb9da274b4cbd8e5d4beb8ef05969eab459ae7a [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 Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070022#include "dex_instruction.h"
23#include "invoke_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/abstract_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/object-inl.h"
27#include "mirror/object_array-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070028#include "object_utils.h"
29#include "thread.h"
30
31#include <sstream>
32
33namespace art {
34
Ian Rogers62d6c772013-02-27 08:32:07 -080035static void AddReferrerLocation(std::ostream& os, const mirror::Class* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -070036 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070037 if (referrer != NULL) {
38 ClassHelper kh(referrer);
39 std::string location(kh.GetLocation());
40 if (!location.empty()) {
41 os << " (declaration of '" << PrettyDescriptor(referrer)
42 << "' appears in " << location << ")";
43 }
44 }
45}
46
Ian Rogers62d6c772013-02-27 08:32:07 -080047static void ThrowException(const ThrowLocation* throw_location, const char* exception_descriptor,
48 const mirror::Class* referrer, const char* fmt, va_list* args = NULL)
49 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070050 std::ostringstream msg;
Ian Rogers62d6c772013-02-27 08:32:07 -080051 if (args != NULL) {
52 std::string vmsg;
53 StringAppendV(&vmsg, fmt, *args);
54 msg << vmsg;
55 } else {
56 msg << fmt;
57 }
58 AddReferrerLocation(msg, referrer);
59 Thread* self = Thread::Current();
60 if (throw_location == NULL) {
61 ThrowLocation computed_throw_location = self->GetCurrentLocationForThrow();
62 self->ThrowNewException(computed_throw_location, exception_descriptor, msg.str().c_str());
63 } else {
64 self->ThrowNewException(*throw_location, exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070065 }
66}
67
Ian Rogers62d6c772013-02-27 08:32:07 -080068// ArithmeticException
69
70void ThrowArithmeticExceptionDivideByZero(Thread* self) {
71 ThrowException(NULL, "Ljava/lang/ArithmeticException;", NULL, "divide by zero");
72}
73
74// ArrayIndexOutOfBoundsException
75
76void ThrowArrayIndexOutOfBoundsException(int index, int length) {
77 ThrowException(NULL, "Ljava/lang/ArrayIndexOutOfBoundsException;", NULL,
78 StringPrintf("length=%d; index=%d", length, index).c_str());
79}
80
81// ArrayStoreException
82
83void ThrowArrayStoreException(const mirror::Class* element_class,
84 const mirror::Class* array_class) {
85 ThrowException(NULL, "Ljava/lang/ArrayStoreException;", NULL,
86 StringPrintf("%s cannot be stored in an array of type %s",
87 PrettyDescriptor(element_class).c_str(),
88 PrettyDescriptor(array_class).c_str()).c_str());
89}
90
91// ClassCastException
92
93void ThrowClassCastException(const mirror::Class* dest_type, const mirror::Class* src_type) {
94 ThrowException(NULL, "Ljava/lang/ClassCastException;", NULL,
95 StringPrintf("%s cannot be cast to %s",
96 PrettyDescriptor(src_type).c_str(),
97 PrettyDescriptor(dest_type).c_str()).c_str());
98}
99
100void ThrowClassCastException(const ThrowLocation* throw_location, const char* msg) {
101 ThrowException(throw_location, "Ljava/lang/ClassCastException;", NULL, msg);
102}
103
104// ClassCircularityError
105
106void ThrowClassCircularityError(mirror::Class* c) {
107 std::ostringstream msg;
108 msg << PrettyDescriptor(c);
109 ThrowException(NULL, "Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
110}
111
112// ClassFormatError
113
114void ThrowClassFormatError(const mirror::Class* referrer, const char* fmt, ...) {
115 va_list args;
116 va_start(args, fmt);
117 ThrowException(NULL, "Ljava/lang/ClassFormatError;", referrer, fmt, &args);
118 va_end(args);}
119
Ian Rogers87e552d2012-08-31 15:54:48 -0700120// IllegalAccessError
121
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700123 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700125 << PrettyDescriptor(accessed) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800126 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700127}
128
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
130 const mirror::AbstractMethod* caller,
131 const mirror::AbstractMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700132 InvokeType type) {
133 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
135 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700136 << " method " << PrettyMethod(called).c_str();
Ian Rogers62d6c772013-02-27 08:32:07 -0800137 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700138}
139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::AbstractMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700141 std::ostringstream msg;
142 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
143 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800144 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700145}
146
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147void ThrowIllegalAccessErrorField(mirror::Class* referrer, mirror::Field* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700148 std::ostringstream msg;
149 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
150 << PrettyDescriptor(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800151 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700152}
153
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154void ThrowIllegalAccessErrorFinalField(const mirror::AbstractMethod* referrer,
155 mirror::Field* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700156 std::ostringstream msg;
157 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
158 << PrettyMethod(referrer) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800159 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer != NULL ? referrer->GetClass() : NULL,
160 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700161}
162
Ian Rogers62d6c772013-02-27 08:32:07 -0800163void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...){
164 va_list args;
165 va_start(args, fmt);
166 ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
167 va_end(args);
168}
169
170// IllegalArgumentException
171
172void ThrowIllegalArgumentException(const ThrowLocation* throw_location, const char* msg) {
173 ThrowException(throw_location, "Ljava/lang/IllegalArgumentException;", NULL, msg);
174}
175
176
Ian Rogers87e552d2012-08-31 15:54:48 -0700177// IncompatibleClassChangeError
178
179void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 mirror::AbstractMethod* method,
181 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700182 std::ostringstream msg;
183 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
184 << expected_type << " but instead was found to be of type " << found_type;
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
186 referrer != NULL ? referrer->GetClass() : NULL,
187 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700188}
189
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const mirror::AbstractMethod* interface_method,
191 mirror::Object* this_object,
192 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700193 // Referrer is calling interface_method on this_object, however, the interface_method isn't
194 // implemented by this_object.
195 CHECK(this_object != NULL);
196 std::ostringstream msg;
197 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
198 << "' does not implement interface '"
199 << PrettyDescriptor(interface_method->GetDeclaringClass())
200 << "' in call to '" << PrettyMethod(interface_method) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800201 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;",
202 referrer != NULL ? referrer->GetClass() : NULL,
203 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700204}
205
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206void ThrowIncompatibleClassChangeErrorField(const mirror::Field* resolved_field, bool is_static,
207 const mirror::AbstractMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700208 std::ostringstream msg;
209 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700210 << (is_static ? "static" : "instance") << " field" << " rather than a "
211 << (is_static ? "instance" : "static") << " field";
Ian Rogers62d6c772013-02-27 08:32:07 -0800212 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer->GetClass(),
213 msg.str().c_str());
214}
215
216void ThrowIncompatibleClassChangeError(const mirror::Class* referrer, const char* fmt, ...){
217 va_list args;
218 va_start(args, fmt);
219 ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
220 va_end(args);
221}
222
223// LinkageError
224
225void ThrowLinkageError(const mirror::Class* referrer, const char* fmt, ...) {
226 va_list args;
227 va_start(args, fmt);
228 ThrowException(NULL, "Ljava/lang/LinkageError;", referrer, fmt, &args);
229 va_end(args);
230}
231
232// NegativeArraySizeException
233
234void ThrowNegativeArraySizeException(int size) {
235 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, StringPrintf("%d", size).c_str());
236}
237
238void ThrowNegativeArraySizeException(const char* msg) {
239 ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, msg);
240}
241
242// NoSuchFieldError
243
244void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
245 const StringPiece& type, const StringPiece& name)
246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
247 ClassHelper kh(c);
248 std::ostringstream msg;
249 msg << "No " << scope << "field " << name << " of type " << type
250 << " in class " << kh.GetDescriptor() << " or its superclasses";
251 ThrowException(NULL, "Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700252}
253
254// NoSuchMethodError
255
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogers62d6c772013-02-27 08:32:07 -0800257 const StringPiece& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700258 std::ostringstream msg;
259 ClassHelper kh(c);
260 msg << "No " << type << " method " << name << signature
261 << " in class " << kh.GetDescriptor() << " or its super classes";
Ian Rogers62d6c772013-02-27 08:32:07 -0800262 ThrowException(NULL, "Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700263}
264
Ian Rogers62d6c772013-02-27 08:32:07 -0800265void ThrowNoSuchMethodError(uint32_t method_idx) {
266 Thread* self = Thread::Current();
267 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
268 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700269 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700270 std::ostringstream msg;
271 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 ThrowException(&throw_location, "Ljava/lang/NoSuchMethodError;",
273 throw_location.GetMethod()->GetDeclaringClass(), msg.str().c_str());
274}
275
276// NullPointerException
277
278void ThrowNullPointerExceptionForFieldAccess(const ThrowLocation& throw_location,
279 mirror::Field* field, bool is_read) {
280 std::ostringstream msg;
281 msg << "Attempt to " << (is_read ? "read from" : "write to")
282 << " field '" << PrettyField(field, true) << "' on a null object reference";
283 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
284}
285
286void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location, uint32_t method_idx,
287 InvokeType type) {
288 mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache();
289 const DexFile& dex_file = *dex_cache->GetDexFile();
290 std::ostringstream msg;
291 msg << "Attempt to invoke " << type << " method '"
292 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
293 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str());
294}
295
296void ThrowNullPointerExceptionFromDexPC(const ThrowLocation& throw_location) {
297 const DexFile::CodeItem* code = MethodHelper(throw_location.GetMethod()).GetCodeItem();
298 uint32_t throw_dex_pc = throw_location.GetDexPc();
299 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
300 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
301 DecodedInstruction dec_insn(instr);
302 switch (instr->Opcode()) {
303 case Instruction::INVOKE_DIRECT:
304 case Instruction::INVOKE_DIRECT_RANGE:
305 ThrowNullPointerExceptionForMethodAccess(throw_location, dec_insn.vB, kDirect);
306 break;
307 case Instruction::INVOKE_VIRTUAL:
308 case Instruction::INVOKE_VIRTUAL_RANGE:
309 ThrowNullPointerExceptionForMethodAccess(throw_location, dec_insn.vB, kVirtual);
310 break;
311 case Instruction::INVOKE_INTERFACE:
312 case Instruction::INVOKE_INTERFACE_RANGE:
313 ThrowNullPointerExceptionForMethodAccess(throw_location, dec_insn.vB, kInterface);
314 break;
315 case Instruction::IGET:
316 case Instruction::IGET_WIDE:
317 case Instruction::IGET_OBJECT:
318 case Instruction::IGET_BOOLEAN:
319 case Instruction::IGET_BYTE:
320 case Instruction::IGET_CHAR:
321 case Instruction::IGET_SHORT: {
322 mirror::Field* field =
323 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC,
324 throw_location.GetMethod(), false);
325 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
326 break;
327 }
328 case Instruction::IPUT:
329 case Instruction::IPUT_WIDE:
330 case Instruction::IPUT_OBJECT:
331 case Instruction::IPUT_BOOLEAN:
332 case Instruction::IPUT_BYTE:
333 case Instruction::IPUT_CHAR:
334 case Instruction::IPUT_SHORT: {
335 mirror::Field* field =
336 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC,
337 throw_location.GetMethod(), false);
338 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
339 break;
340 }
341 case Instruction::AGET:
342 case Instruction::AGET_WIDE:
343 case Instruction::AGET_OBJECT:
344 case Instruction::AGET_BOOLEAN:
345 case Instruction::AGET_BYTE:
346 case Instruction::AGET_CHAR:
347 case Instruction::AGET_SHORT:
348 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
349 "Attempt to read from null array");
350 break;
351 case Instruction::APUT:
352 case Instruction::APUT_WIDE:
353 case Instruction::APUT_OBJECT:
354 case Instruction::APUT_BOOLEAN:
355 case Instruction::APUT_BYTE:
356 case Instruction::APUT_CHAR:
357 case Instruction::APUT_SHORT:
358 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
359 "Attempt to write to null array");
360 break;
361 case Instruction::ARRAY_LENGTH:
362 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
363 "Attempt to get length of null array");
364 break;
365 default: {
366 // TODO: We should have covered all the cases where we expect a NPE above, this
367 // message/logging is so we can improve any cases we've missed in the future.
368 const DexFile& dex_file =
369 *throw_location.GetMethod()->GetDeclaringClass()->GetDexCache()->GetDexFile();
370 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
371 StringPrintf("Null pointer exception during instruction '%s'",
372 instr->DumpString(&dex_file).c_str()).c_str());
373 break;
374 }
375 }
376}
377
378void ThrowNullPointerException(const ThrowLocation* throw_location, const char* msg) {
379 ThrowException(throw_location, "Ljava/lang/NullPointerException;", NULL, msg);
380}
381
382// RuntimeException
383
384void ThrowRuntimeException(const char* fmt, ...) {
385 va_list args;
386 va_start(args, fmt);
387 ThrowException(NULL, "Ljava/lang/RuntimeException;", NULL, fmt, &args);
388 va_end(args);
389}
390
391// VerifyError
392
393void ThrowVerifyError(const mirror::Class* referrer, const char* fmt, ...) {
394 va_list args;
395 va_start(args, fmt);
396 ThrowException(NULL, "Ljava/lang/VerifyError;", referrer, fmt, &args);
397 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700398}
399
400} // namespace art