blob: dc3627a0b2e1121b93972d5fb19e3d00bae127af [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"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020022#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070023#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]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 switch (instr->Opcode()) {
302 case Instruction::INVOKE_DIRECT:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200303 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kDirect);
304 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 case Instruction::INVOKE_DIRECT_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200306 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800307 break;
308 case Instruction::INVOKE_VIRTUAL:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200309 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kVirtual);
310 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 case Instruction::INVOKE_VIRTUAL_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200312 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800313 break;
314 case Instruction::INVOKE_INTERFACE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200315 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kInterface);
316 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 case Instruction::INVOKE_INTERFACE_RANGE:
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200318 ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 break;
320 case Instruction::IGET:
321 case Instruction::IGET_WIDE:
322 case Instruction::IGET_OBJECT:
323 case Instruction::IGET_BOOLEAN:
324 case Instruction::IGET_BYTE:
325 case Instruction::IGET_CHAR:
326 case Instruction::IGET_SHORT: {
327 mirror::Field* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200328 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 throw_location.GetMethod(), false);
330 ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */);
331 break;
332 }
333 case Instruction::IPUT:
334 case Instruction::IPUT_WIDE:
335 case Instruction::IPUT_OBJECT:
336 case Instruction::IPUT_BOOLEAN:
337 case Instruction::IPUT_BYTE:
338 case Instruction::IPUT_CHAR:
339 case Instruction::IPUT_SHORT: {
340 mirror::Field* field =
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200341 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 throw_location.GetMethod(), false);
343 ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */);
344 break;
345 }
346 case Instruction::AGET:
347 case Instruction::AGET_WIDE:
348 case Instruction::AGET_OBJECT:
349 case Instruction::AGET_BOOLEAN:
350 case Instruction::AGET_BYTE:
351 case Instruction::AGET_CHAR:
352 case Instruction::AGET_SHORT:
353 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
354 "Attempt to read from null array");
355 break;
356 case Instruction::APUT:
357 case Instruction::APUT_WIDE:
358 case Instruction::APUT_OBJECT:
359 case Instruction::APUT_BOOLEAN:
360 case Instruction::APUT_BYTE:
361 case Instruction::APUT_CHAR:
362 case Instruction::APUT_SHORT:
363 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
364 "Attempt to write to null array");
365 break;
366 case Instruction::ARRAY_LENGTH:
367 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
368 "Attempt to get length of null array");
369 break;
370 default: {
371 // TODO: We should have covered all the cases where we expect a NPE above, this
372 // message/logging is so we can improve any cases we've missed in the future.
373 const DexFile& dex_file =
374 *throw_location.GetMethod()->GetDeclaringClass()->GetDexCache()->GetDexFile();
375 ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL,
376 StringPrintf("Null pointer exception during instruction '%s'",
377 instr->DumpString(&dex_file).c_str()).c_str());
378 break;
379 }
380 }
381}
382
383void ThrowNullPointerException(const ThrowLocation* throw_location, const char* msg) {
384 ThrowException(throw_location, "Ljava/lang/NullPointerException;", NULL, msg);
385}
386
387// RuntimeException
388
389void ThrowRuntimeException(const char* fmt, ...) {
390 va_list args;
391 va_start(args, fmt);
392 ThrowException(NULL, "Ljava/lang/RuntimeException;", NULL, fmt, &args);
393 va_end(args);
394}
395
396// VerifyError
397
398void ThrowVerifyError(const mirror::Class* referrer, const char* fmt, ...) {
399 va_list args;
400 va_start(args, fmt);
401 ThrowException(NULL, "Ljava/lang/VerifyError;", referrer, fmt, &args);
402 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700403}
404
405} // namespace art