1 /* src/vm/exceptions.c - exception related functions
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Christian Thalinger
29 Changes: Edwin Steiner
31 $Id: exceptions.c 3547 2005-11-03 20:38:59Z twisti $
42 #include "mm/memory.h"
43 #include "native/native.h"
44 #include "native/include/java_lang_String.h"
45 #include "native/include/java_lang_Throwable.h"
46 #include "toolbox/logging.h"
47 #include "toolbox/util.h"
49 #include "vm/exceptions.h"
50 #include "vm/global.h"
51 #include "vm/loader.h"
52 #include "vm/options.h"
53 #include "vm/stringlocal.h"
54 #include "vm/tables.h"
55 #include "vm/jit/asmpart.h"
56 #include "vm/jit/jit.h"
59 /* for raising exceptions from native methods *********************************/
61 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
62 java_objectheader *_no_threads_exceptionptr = NULL;
66 /* init_system_exceptions ******************************************************
68 Load and link exceptions used in the system.
70 *******************************************************************************/
72 bool exceptions_init(void)
74 /* java/lang/Throwable */
76 if (!(class_java_lang_Throwable =
77 load_class_bootstrap(utf_java_lang_Throwable)) ||
78 !link_class(class_java_lang_Throwable))
82 /* java/lang/VMThrowable */
84 if (!(class_java_lang_VMThrowable =
85 load_class_bootstrap(utf_java_lang_VMThrowable)) ||
86 !link_class(class_java_lang_VMThrowable))
92 if (!(class_java_lang_Error = load_class_bootstrap(utf_java_lang_Error)) ||
93 !link_class(class_java_lang_Error))
96 /* java/lang/NoClassDefFoundError */
98 if (!(class_java_lang_NoClassDefFoundError =
99 load_class_bootstrap(utf_java_lang_NoClassDefFoundError)) ||
100 !link_class(class_java_lang_NoClassDefFoundError))
103 /* java/lang/OutOfMemoryError */
105 if (!(class_java_lang_OutOfMemoryError =
106 load_class_bootstrap(utf_java_lang_OutOfMemoryError)) ||
107 !link_class(class_java_lang_OutOfMemoryError))
111 /* java/lang/Exception */
113 if (!(class_java_lang_Exception =
114 load_class_bootstrap(utf_java_lang_Exception)) ||
115 !link_class(class_java_lang_Exception))
118 /* java/lang/ClassNotFoundException */
120 if (!(class_java_lang_ClassNotFoundException =
121 load_class_bootstrap(utf_java_lang_ClassNotFoundException)) ||
122 !link_class(class_java_lang_ClassNotFoundException))
125 /* java/lang/IllegalArgumentException */
127 if (!(class_java_lang_IllegalArgumentException =
128 load_class_bootstrap(utf_java_lang_IllegalArgumentException)) ||
129 !link_class(class_java_lang_IllegalArgumentException))
132 /* java/lang/IllegalMonitorStateException */
134 if (!(class_java_lang_IllegalMonitorStateException =
135 load_class_bootstrap(utf_java_lang_IllegalMonitorStateException)) ||
136 !link_class(class_java_lang_IllegalMonitorStateException))
139 /* java/lang/NullPointerException */
141 if (!(class_java_lang_NullPointerException =
142 load_class_bootstrap(utf_java_lang_NullPointerException)) ||
143 !link_class(class_java_lang_NullPointerException))
151 static void throw_exception_exit_intern(bool doexit)
153 java_objectheader *xptr;
157 xptr = *exceptionptr;
160 /* clear exception, because we are calling jit code again */
161 *exceptionptr = NULL;
163 c = xptr->vftbl->class;
165 pss = class_resolveclassmethod(c,
168 class_java_lang_Object,
171 /* print the stacktrace */
173 asm_calljavafunction(pss, xptr, NULL, NULL, NULL);
175 /* This normally means, we are EXTREMLY out of memory or have a */
176 /* serious problem while printStackTrace. But may be another */
177 /* exception, so print it. */
180 java_lang_Throwable *t;
182 t = (java_lang_Throwable *) *exceptionptr;
184 fprintf(stderr, "Exception while printStackTrace(): ");
185 utf_fprint_classname(stderr, t->header.vftbl->class->name);
187 if (t->detailMessage) {
190 buf = javastring_tochar((java_objectheader *) t->detailMessage);
191 fprintf(stderr, ": %s", buf);
192 MFREE(buf, char, strlen(buf));
195 fprintf(stderr, "\n");
199 utf_fprint_classname(stderr, c->name);
200 fprintf(stderr, ": printStackTrace()V not found!\n");
213 void throw_exception(void)
215 throw_exception_exit_intern(false);
219 void throw_exception_exit(void)
221 throw_exception_exit_intern(true);
225 void throw_main_exception(void)
227 fprintf(stderr, "Exception in thread \"main\" ");
230 throw_exception_exit_intern(false);
234 void throw_main_exception_exit(void)
236 fprintf(stderr, "Exception in thread \"main\" ");
239 throw_exception_exit_intern(true);
243 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
250 len = strlen(exception);
251 tmp = MNEW(char, len + 1);
252 strncpy(tmp, exception, len);
255 /* convert to classname */
257 for (i = len - 1; i >= 0; i--)
258 if (tmp[i] == '/') tmp[i] = '.';
260 fprintf(stderr, "Exception in thread \"main\" %s", tmp);
262 MFREE(tmp, char, len);
264 if (strlen(message) > 0) {
265 fprintf(stderr, ": ");
267 va_start(ap, message);
268 vfprintf(stderr, message, ap);
272 fprintf(stderr, "\n");
281 /* new_exception ***************************************************************
283 Creates an exception object with the given name and initalizes it.
285 *******************************************************************************/
287 java_objectheader *new_exception(const char *classname)
289 java_objectheader *o;
292 if (!(c = load_class_bootstrap(utf_new_char(classname))))
293 return *exceptionptr;
295 o = native_new_and_init(c);
298 return *exceptionptr;
304 /* new_exception_message *******************************************************
306 Creates an exception object with the given name and initalizes it
307 with the given char message.
309 *******************************************************************************/
311 java_objectheader *new_exception_message(const char *classname,
314 java_objectheader *o;
317 if (!(c = load_class_bootstrap(utf_new_char(classname))))
318 return *exceptionptr;
320 o = native_new_and_init_string(c, javastring_new_char(message));
323 return *exceptionptr;
329 /* new_exception_throwable *****************************************************
331 Creates an exception object with the given name and initalizes it
332 with the given java/lang/Throwable exception.
334 *******************************************************************************/
336 java_objectheader *new_exception_throwable(const char *classname,
337 java_lang_Throwable *throwable)
339 java_objectheader *o;
342 if (!(c = load_class_bootstrap(utf_new_char(classname))))
343 return *exceptionptr;
345 o = native_new_and_init_throwable(c, throwable);
348 return *exceptionptr;
354 /* new_exception_utfmessage ****************************************************
356 Creates an exception object with the given name and initalizes it
357 with the given utf message.
359 *******************************************************************************/
361 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
363 java_objectheader *o;
366 if (!(c = load_class_bootstrap(utf_new_char(classname))))
367 return *exceptionptr;
369 o = native_new_and_init_string(c, javastring_new(message));
372 return *exceptionptr;
378 /* new_exception_javastring ****************************************************
380 Creates an exception object with the given name and initalizes it
381 with the given java/lang/String message.
383 *******************************************************************************/
385 java_objectheader *new_exception_javastring(const char *classname,
386 java_lang_String *message)
388 java_objectheader *o;
391 if (!(c = load_class_bootstrap(utf_new_char(classname))))
392 return *exceptionptr;
394 o = native_new_and_init_string(c, message);
397 return *exceptionptr;
403 /* new_exception_int ***********************************************************
405 Creates an exception object with the given name and initalizes it
406 with the given int value.
408 *******************************************************************************/
410 java_objectheader *new_exception_int(const char *classname, s4 i)
412 java_objectheader *o;
415 if (!(c = load_class_bootstrap(utf_new_char(classname))))
416 return *exceptionptr;
418 o = native_new_and_init_int(c, i);
421 return *exceptionptr;
427 /* new_classformaterror ********************************************************
429 generates a java.lang.ClassFormatError for the classloader
431 *******************************************************************************/
433 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
435 java_objectheader *o;
440 /* calculate message length */
445 msglen += utf_strlen(c->name) + strlen(" (");
447 va_start(ap, message);
448 msglen += get_variable_message_length(message, ap);
452 msglen += strlen(")");
454 msglen += strlen("0");
456 /* allocate a buffer */
458 msg = MNEW(char, msglen);
460 /* print message into allocated buffer */
463 utf_sprint_classname(msg, c->name);
467 va_start(ap, message);
468 vsprintf(msg + strlen(msg), message, ap);
474 o = new_exception_message(string_java_lang_ClassFormatError, msg);
476 MFREE(msg, char, msglen);
482 /* new_classnotfoundexception **************************************************
484 Generates a java.lang.ClassNotFoundException for the classloader.
486 *******************************************************************************/
488 java_objectheader *new_classnotfoundexception(utf *name)
490 java_objectheader *o;
492 o = native_new_and_init_string(class_java_lang_ClassNotFoundException,
493 javastring_new(name));
496 return *exceptionptr;
502 /* new_noclassdeffounderror ****************************************************
504 Generates a java.lang.NoClassDefFoundError
506 *******************************************************************************/
508 java_objectheader *new_noclassdeffounderror(utf *name)
510 java_objectheader *o;
512 o = native_new_and_init_string(class_java_lang_NoClassDefFoundError,
513 javastring_new(name));
516 return *exceptionptr;
522 /* classnotfoundexception_to_noclassdeffounderror ******************************
524 Check the *exceptionptr for a ClassNotFoundException. If it is one,
525 convert it to a NoClassDefFoundError.
527 *******************************************************************************/
529 void classnotfoundexception_to_noclassdeffounderror(void)
531 java_objectheader *xptr;
532 java_objectheader *cause;
536 cause = *exceptionptr;
538 /* convert ClassNotFoundException's to NoClassDefFoundError's */
540 if (builtin_instanceof(cause, class_java_lang_ClassNotFoundException)) {
541 /* clear exception, because we are calling jit code again */
543 *exceptionptr = NULL;
545 /* create new error */
548 new_exception_javastring(string_java_lang_NoClassDefFoundError,
549 ((java_lang_Throwable *) cause)->detailMessage);
551 /* we had an exception while creating the error */
556 /* set new exception */
558 *exceptionptr = xptr;
563 /* new_internalerror ***********************************************************
565 Generates a java.lang.InternalError for the VM.
567 *******************************************************************************/
569 java_objectheader *new_internalerror(const char *message, ...)
571 java_objectheader *o;
576 /* calculate exception message length */
578 va_start(ap, message);
579 msglen = get_variable_message_length(message, ap);
582 /* allocate memory */
584 msg = MNEW(char, msglen);
586 /* generate message */
588 va_start(ap, message);
589 vsprintf(msg, message, ap);
592 /* create exception object */
594 o = new_exception_message(string_java_lang_InternalError, msg);
598 MFREE(msg, char, msglen);
604 /* new_unsupportedclassversionerror ********************************************
606 generates a java.lang.UnsupportedClassVersionError for the classloader
608 *******************************************************************************/
610 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
612 java_objectheader *o;
617 /* calculate exception message length */
619 msglen = utf_strlen(c->name) + strlen(" (") + strlen(")") + strlen("0");
621 va_start(ap, message);
622 msglen += get_variable_message_length(message, ap);
625 /* allocate memory */
627 msg = MNEW(char, msglen);
629 /* generate message */
631 utf_sprint_classname(msg, c->name);
634 va_start(ap, message);
635 vsprintf(msg + strlen(msg), message, ap);
640 /* create exception object */
642 o = new_exception_message(string_java_lang_UnsupportedClassVersionError,
647 MFREE(msg, char, msglen);
653 /* new_verifyerror *************************************************************
655 generates a java.lang.VerifyError for the jit compiler
657 *******************************************************************************/
659 java_objectheader *new_verifyerror(methodinfo *m, const char *message, ...)
661 java_objectheader *o;
666 useinlining = false; /* at least until sure inlining works with exceptions*/
668 /* calculate exception message length */
670 msglen = strlen("(class: ") + utf_strlen(m->class->name) +
671 strlen(", method: ") + utf_strlen(m->name) +
672 strlen(" signature: ") + utf_strlen(m->descriptor) +
673 strlen(") ") + strlen("0");
675 va_start(ap, message);
676 msglen += get_variable_message_length(message, ap);
679 /* allocate memory */
681 msg = MNEW(char, msglen);
683 /* generate message */
685 strcpy(msg, "(class: ");
686 utf_strcat(msg, m->class->name);
687 strcat(msg, ", method: ");
688 utf_strcat(msg, m->name);
689 strcat(msg, " signature: ");
690 utf_strcat(msg, m->descriptor);
693 va_start(ap, message);
694 vsprintf(msg + strlen(msg), message, ap);
697 /* create exception object */
699 o = new_exception_message(string_java_lang_VerifyError, msg);
703 MFREE(msg, char, msglen);
709 /* new_arithmeticexception *****************************************************
711 Generates a java.lang.ArithmeticException for the jit compiler.
713 *******************************************************************************/
715 java_objectheader *new_arithmeticexception(void)
717 java_objectheader *e;
719 e = new_exception_message(string_java_lang_ArithmeticException,
720 string_java_lang_ArithmeticException_message);
723 return *exceptionptr;
729 /* new_arrayindexoutofboundsexception ******************************************
731 Generates a java.lang.ArrayIndexOutOfBoundsException for the JIT
734 *******************************************************************************/
736 java_objectheader *new_arrayindexoutofboundsexception(s4 index)
738 java_objectheader *e;
742 /* convert the index into a String, like Sun does */
744 m = class_resolveclassmethod(class_java_lang_String,
745 utf_new_char("valueOf"),
746 utf_new_char("(I)Ljava/lang/String;"),
747 class_java_lang_Object,
751 return *exceptionptr;
753 s = (java_lang_String *) asm_calljavafunction(m,
754 (void *) (ptrint) index,
760 return *exceptionptr;
762 e = new_exception_javastring(string_java_lang_ArrayIndexOutOfBoundsException,
766 return *exceptionptr;
772 /* new_arraystoreexception *****************************************************
774 generates a java.lang.ArrayStoreException for the jit compiler
776 *******************************************************************************/
778 java_objectheader *new_arraystoreexception(void)
780 java_objectheader *e;
782 e = new_exception(string_java_lang_ArrayStoreException);
783 /* e = native_new_and_init(class_java_lang_ArrayStoreException); */
786 return *exceptionptr;
792 /* new_classcastexception ******************************************************
794 generates a java.lang.ClassCastException for the jit compiler
796 *******************************************************************************/
798 java_objectheader *new_classcastexception(void)
800 java_objectheader *e;
802 e = new_exception(string_java_lang_ClassCastException);
805 return *exceptionptr;
811 /* new_illegalargumentexception ************************************************
813 Generates a java.lang.IllegalArgumentException for the VM system.
815 *******************************************************************************/
817 java_objectheader *new_illegalargumentexception(void)
819 java_objectheader *e;
821 if (!(e = native_new_and_init(class_java_lang_IllegalArgumentException)))
822 return *exceptionptr;
828 /* new_illegalmonitorstateexception ********************************************
830 Generates a java.lang.IllegalMonitorStateException for the VM
833 *******************************************************************************/
835 java_objectheader *new_illegalmonitorstateexception(void)
837 java_objectheader *e;
839 if (!(e = native_new_and_init(class_java_lang_IllegalMonitorStateException)))
840 return *exceptionptr;
846 /* new_negativearraysizeexception **********************************************
848 generates a java.lang.NegativeArraySizeException for the jit compiler
850 *******************************************************************************/
852 java_objectheader *new_negativearraysizeexception(void)
854 java_objectheader *e;
856 e = new_exception(string_java_lang_NegativeArraySizeException);
859 return *exceptionptr;
865 /* new_nullpointerexception ****************************************************
867 generates a java.lang.NullPointerException for the jit compiler
869 *******************************************************************************/
871 java_objectheader *new_nullpointerexception(void)
873 java_objectheader *e;
875 e = native_new_and_init(class_java_lang_NullPointerException);
878 return *exceptionptr;
885 * These are local overrides for various environment variables in Emacs.
886 * Please do not remove this and leave it at the end of the file, where
887 * Emacs will automagically detect them.
888 * ---------------------------------------------------------------------
891 * indent-tabs-mode: t