1 /* src/vmcore/loader.c - class loader functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
36 #include "mm/memory.h"
38 #include "native/llni.h"
40 #include "threads/lock-common.h"
42 #include "toolbox/logging.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/global.h"
47 #include "vm/primitive.h"
48 #include "vm/stringlocal.h"
51 #include "vm/jit_interface.h"
53 #if defined(ENABLE_JAVASE)
54 # include "vmcore/annotation.h"
55 # include "vmcore/stackmap.h"
58 #include "vmcore/classcache.h"
59 #include "vmcore/field.h"
60 #include "vmcore/linker.h"
61 #include "vmcore/loader.h"
62 #include "vmcore/method.h"
63 #include "vmcore/options.h"
64 #include "vmcore/rt-timing.h"
66 #if defined(ENABLE_STATISTICS)
67 # include "vmcore/statistics.h"
70 #include "vmcore/suck.h"
72 #if defined(ENABLE_ZLIB)
73 # include "vmcore/zip.h"
76 #if defined(ENABLE_JVMTI)
77 # include "native/jvmti/cacaodbg.h"
81 /* loader_init *****************************************************************
83 Initializes all lists and loads all classes required for the system
86 *******************************************************************************/
88 bool loader_init(void)
90 #if defined(ENABLE_THREADS)
91 list_classpath_entry *lce;
93 /* Initialize the monitor pointer for zip/jar file locking. */
95 for (lce = list_first(list_classpath_entries); lce != NULL;
96 lce = list_next(list_classpath_entries, lce))
97 if (lce->type == CLASSPATH_ARCHIVE)
98 LOCK_INIT_OBJECT_LOCK(lce);
101 /* load some important classes */
103 if (!(class_java_lang_Object = load_class_bootstrap(utf_java_lang_Object)))
106 if (!(class_java_lang_String = load_class_bootstrap(utf_java_lang_String)))
109 #if defined(ENABLE_JAVASE)
110 if (!(class_java_lang_Cloneable =
111 load_class_bootstrap(utf_java_lang_Cloneable)))
114 if (!(class_java_io_Serializable =
115 load_class_bootstrap(utf_java_io_Serializable)))
119 /* load classes for wrapping primitive types */
121 #if defined(ENABLE_JAVASE)
122 if (!(class_java_lang_Void = load_class_bootstrap(utf_java_lang_Void)))
126 if (!(class_java_lang_Boolean =
127 load_class_bootstrap(utf_java_lang_Boolean)))
130 if (!(class_java_lang_Byte = load_class_bootstrap(utf_java_lang_Byte)))
133 if (!(class_java_lang_Character =
134 load_class_bootstrap(utf_java_lang_Character)))
137 if (!(class_java_lang_Short = load_class_bootstrap(utf_java_lang_Short)))
140 if (!(class_java_lang_Integer =
141 load_class_bootstrap(utf_java_lang_Integer)))
144 if (!(class_java_lang_Long = load_class_bootstrap(utf_java_lang_Long)))
147 if (!(class_java_lang_Float = load_class_bootstrap(utf_java_lang_Float)))
150 if (!(class_java_lang_Double = load_class_bootstrap(utf_java_lang_Double)))
154 /* load some other important classes */
156 if (!(class_java_lang_Class = load_class_bootstrap(utf_java_lang_Class)))
159 #if defined(ENABLE_JAVASE)
160 if (!(class_java_lang_ClassLoader =
161 load_class_bootstrap(utf_java_lang_ClassLoader)))
164 if (!(class_java_lang_SecurityManager =
165 load_class_bootstrap(utf_java_lang_SecurityManager)))
169 if (!(class_java_lang_System = load_class_bootstrap(utf_java_lang_System)))
172 if (!(class_java_lang_Thread =
173 load_class_bootstrap(utf_new_char("java/lang/Thread"))))
176 #if defined(ENABLE_JAVASE)
177 if (!(class_java_lang_ThreadGroup =
178 load_class_bootstrap(utf_java_lang_ThreadGroup)))
182 #if defined(WITH_CLASSPATH_GNU)
183 if (!(class_java_lang_VMSystem =
184 load_class_bootstrap(utf_new_char("java/lang/VMSystem"))))
188 if (!(class_java_lang_VMThread =
189 load_class_bootstrap(utf_new_char("java/lang/VMThread"))))
194 /* some classes which may be used more often */
196 #if defined(ENABLE_JAVASE)
197 if (!(class_java_lang_StackTraceElement =
198 load_class_bootstrap(utf_java_lang_StackTraceElement)))
201 if (!(class_java_lang_reflect_Constructor =
202 load_class_bootstrap(utf_java_lang_reflect_Constructor)))
205 if (!(class_java_lang_reflect_Field =
206 load_class_bootstrap(utf_java_lang_reflect_Field)))
209 if (!(class_java_lang_reflect_Method =
210 load_class_bootstrap(utf_java_lang_reflect_Method)))
213 if (!(class_java_security_PrivilegedAction =
214 load_class_bootstrap(utf_new_char("java/security/PrivilegedAction"))))
217 if (!(class_java_util_Vector = load_class_bootstrap(utf_java_util_Vector)))
220 # if defined(WITH_CLASSPATH_SUN)
221 if (!(class_sun_reflect_MagicAccessorImpl =
222 load_class_bootstrap(utf_new_char("sun/reflect/MagicAccessorImpl"))))
226 if (!(arrayclass_java_lang_Object =
227 load_class_bootstrap(utf_new_char("[Ljava/lang/Object;"))))
230 #if defined(ENABLE_ANNOTATIONS)
231 /* needed by annotation support */
232 if (!(class_sun_reflect_ConstantPool =
233 load_class_bootstrap(utf_sun_reflect_ConstantPool)))
236 #if defined(WITH_CLASSPATH_GNU)
237 /* needed by GNU Classpaths annotation support */
238 if (!(class_sun_reflect_annotation_AnnotationParser =
239 load_class_bootstrap(utf_sun_reflect_annotation_AnnotationParser)))
250 /* loader_load_all_classes *****************************************************
252 Loads all classes specified in the BOOTCLASSPATH.
254 *******************************************************************************/
256 void loader_load_all_classes(void)
258 list_classpath_entry *lce;
259 #if defined(ENABLE_ZLIB)
262 hashtable_zipfile_entry *htzfe;
266 for (lce = list_first(list_classpath_entries); lce != NULL;
267 lce = list_next(list_classpath_entries, lce)) {
268 #if defined(ENABLE_ZLIB)
269 if (lce->type == CLASSPATH_ARCHIVE) {
270 /* get the classes hashtable */
274 for (slot = 0; slot < ht->size; slot++) {
275 htzfe = (hashtable_zipfile_entry *) ht->ptr[slot];
277 for (; htzfe; htzfe = htzfe->hashlink) {
280 /* skip all entries in META-INF and .properties,
283 if (!strncmp(u->text, "META-INF", strlen("META-INF")) ||
284 strstr(u->text, ".properties") ||
285 strstr(u->text, ".png"))
288 /* load class from bootstrap classloader */
290 if (!load_class_bootstrap(u)) {
291 fprintf(stderr, "Error loading: ");
292 utf_fprint_printable_ascii_classname(stderr, u);
293 fprintf(stderr, "\n");
296 /* print out exception and cause */
298 exceptions_print_current_exception();
306 #if defined(ENABLE_ZLIB)
313 /* loader_skip_attribute_body **************************************************
315 Skips an attribute the attribute_name_index has already been read.
318 u2 attribute_name_index;
320 u1 info[attribute_length];
323 *******************************************************************************/
325 bool loader_skip_attribute_body(classbuffer *cb)
329 if (!suck_check_classbuffer_size(cb, 4))
332 attribute_length = suck_u4(cb);
334 if (!suck_check_classbuffer_size(cb, attribute_length))
337 suck_skip_nbytes(cb, attribute_length);
343 /* load_constantpool ***********************************************************
345 Loads the constantpool of a class, the entries are transformed into
346 a simpler format by resolving references (a detailed overview of
347 the compact structures can be found in global.h).
349 *******************************************************************************/
351 static bool load_constantpool(classbuffer *cb, descriptor_pool *descpool)
354 /* The following structures are used to save information which cannot be
355 processed during the first pass. After the complete constantpool has
356 been traversed the references can be resolved.
357 (only in specific order) */
359 /* CONSTANT_Class entries */
360 typedef struct forward_class {
361 struct forward_class *next;
366 /* CONSTANT_String */
367 typedef struct forward_string {
368 struct forward_string *next;
373 /* CONSTANT_NameAndType */
374 typedef struct forward_nameandtype {
375 struct forward_nameandtype *next;
379 } forward_nameandtype;
381 /* CONSTANT_Fieldref, CONSTANT_Methodref or CONSTANT_InterfaceMethodref */
382 typedef struct forward_fieldmethint {
383 struct forward_fieldmethint *next;
387 u2 nameandtype_index;
388 } forward_fieldmethint;
394 forward_class *forward_classes = NULL;
395 forward_string *forward_strings = NULL;
396 forward_nameandtype *forward_nameandtypes = NULL;
397 forward_fieldmethint *forward_fieldmethints = NULL;
401 forward_nameandtype *nfn;
402 forward_fieldmethint *nff;
410 /* number of entries in the constant_pool table plus one */
411 if (!suck_check_classbuffer_size(cb, 2))
414 cpcount = c->cpcount = suck_u2(cb);
416 /* allocate memory */
417 cptags = c->cptags = MNEW(u1, cpcount);
418 cpinfos = c->cpinfos = MNEW(voidptr, cpcount);
421 exceptions_throw_classformaterror(c, "Illegal constant pool size");
425 #if defined(ENABLE_STATISTICS)
427 count_const_pool_len += (sizeof(u1) + sizeof(voidptr)) * cpcount;
430 /* initialize constantpool */
431 for (idx = 0; idx < cpcount; idx++) {
432 cptags[idx] = CONSTANT_UNUSED;
437 /******* first pass *******/
438 /* entries which cannot be resolved now are written into
439 temporary structures and traversed again later */
442 while (idx < cpcount) {
445 /* get constant type */
446 if (!suck_check_classbuffer_size(cb, 1))
453 nfc = DNEW(forward_class);
455 nfc->next = forward_classes;
456 forward_classes = nfc;
458 nfc->thisindex = idx;
459 /* reference to CONSTANT_NameAndType */
460 if (!suck_check_classbuffer_size(cb, 2))
463 nfc->name_index = suck_u2(cb);
468 case CONSTANT_String:
469 nfs = DNEW(forward_string);
471 nfs->next = forward_strings;
472 forward_strings = nfs;
474 nfs->thisindex = idx;
476 /* reference to CONSTANT_Utf8_info with string characters */
477 if (!suck_check_classbuffer_size(cb, 2))
480 nfs->string_index = suck_u2(cb);
485 case CONSTANT_NameAndType:
486 nfn = DNEW(forward_nameandtype);
488 nfn->next = forward_nameandtypes;
489 forward_nameandtypes = nfn;
491 nfn->thisindex = idx;
493 if (!suck_check_classbuffer_size(cb, 2 + 2))
496 /* reference to CONSTANT_Utf8_info containing simple name */
497 nfn->name_index = suck_u2(cb);
499 /* reference to CONSTANT_Utf8_info containing field or method
501 nfn->sig_index = suck_u2(cb);
506 case CONSTANT_Fieldref:
507 case CONSTANT_Methodref:
508 case CONSTANT_InterfaceMethodref:
509 nff = DNEW(forward_fieldmethint);
511 nff->next = forward_fieldmethints;
512 forward_fieldmethints = nff;
514 nff->thisindex = idx;
518 if (!suck_check_classbuffer_size(cb, 2 + 2))
521 /* class or interface type that contains the declaration of the
523 nff->class_index = suck_u2(cb);
525 /* name and descriptor of the field or method */
526 nff->nameandtype_index = suck_u2(cb);
531 case CONSTANT_Integer: {
532 constant_integer *ci = NEW(constant_integer);
534 #if defined(ENABLE_STATISTICS)
536 count_const_pool_len += sizeof(constant_integer);
539 if (!suck_check_classbuffer_size(cb, 4))
542 ci->value = suck_s4(cb);
543 cptags[idx] = CONSTANT_Integer;
550 case CONSTANT_Float: {
551 constant_float *cf = NEW(constant_float);
553 #if defined(ENABLE_STATISTICS)
555 count_const_pool_len += sizeof(constant_float);
558 if (!suck_check_classbuffer_size(cb, 4))
561 cf->value = suck_float(cb);
562 cptags[idx] = CONSTANT_Float;
569 case CONSTANT_Long: {
570 constant_long *cl = NEW(constant_long);
572 #if defined(ENABLE_STATISTICS)
574 count_const_pool_len += sizeof(constant_long);
577 if (!suck_check_classbuffer_size(cb, 8))
580 cl->value = suck_s8(cb);
581 cptags[idx] = CONSTANT_Long;
585 exceptions_throw_classformaterror(c, "Invalid constant pool entry");
591 case CONSTANT_Double: {
592 constant_double *cd = NEW(constant_double);
594 #if defined(ENABLE_STATISTICS)
596 count_const_pool_len += sizeof(constant_double);
599 if (!suck_check_classbuffer_size(cb, 8))
602 cd->value = suck_double(cb);
603 cptags[idx] = CONSTANT_Double;
607 exceptions_throw_classformaterror(c, "Invalid constant pool entry");
613 case CONSTANT_Utf8: {
616 /* number of bytes in the bytes array (not string-length) */
617 if (!suck_check_classbuffer_size(cb, 2))
620 length = suck_u2(cb);
621 cptags[idx] = CONSTANT_Utf8;
623 /* validate the string */
624 if (!suck_check_classbuffer_size(cb, length))
627 #ifdef ENABLE_VERIFIER
629 !is_valid_utf((char *) cb->pos, (char *) (cb->pos + length)))
631 exceptions_throw_classformaterror(c, "Invalid UTF-8 string");
634 #endif /* ENABLE_VERIFIER */
635 /* insert utf-string into the utf-symboltable */
636 cpinfos[idx] = utf_new((char *) cb->pos, length);
638 /* skip bytes of the string (buffer size check above) */
639 suck_skip_nbytes(cb, length);
645 exceptions_throw_classformaterror(c, "Illegal constant pool type");
651 /* resolve entries in temporary structures */
653 while (forward_classes) {
655 class_getconstant(c, forward_classes->name_index, CONSTANT_Utf8);
659 #ifdef ENABLE_VERIFIER
660 if (opt_verify && !is_valid_name_utf(name)) {
661 exceptions_throw_classformaterror(c, "Class reference with invalid name");
664 #endif /* ENABLE_VERIFIER */
666 /* add all class references to the descriptor_pool */
668 if (!descriptor_pool_add_class(descpool, name))
671 cptags[forward_classes->thisindex] = CONSTANT_Class;
673 /* the classref is created later */
674 cpinfos[forward_classes->thisindex] = name;
676 nfc = forward_classes;
677 forward_classes = forward_classes->next;
680 while (forward_strings) {
682 class_getconstant(c, forward_strings->string_index, CONSTANT_Utf8);
686 /* resolve utf-string */
687 cptags[forward_strings->thisindex] = CONSTANT_String;
688 cpinfos[forward_strings->thisindex] = text;
690 nfs = forward_strings;
691 forward_strings = forward_strings->next;
694 while (forward_nameandtypes) {
695 constant_nameandtype *cn = NEW(constant_nameandtype);
697 #if defined(ENABLE_STATISTICS)
699 count_const_pool_len += sizeof(constant_nameandtype);
702 /* resolve simple name and descriptor */
703 cn->name = class_getconstant(c,
704 forward_nameandtypes->name_index,
709 cn->descriptor = class_getconstant(c,
710 forward_nameandtypes->sig_index,
715 #ifdef ENABLE_VERIFIER
718 if (!is_valid_name_utf(cn->name)) {
719 exceptions_throw_classformaterror(c,
720 "Illegal Field name \"%s\"",
726 /* disallow referencing <clinit> among others */
727 if (cn->name->text[0] == '<' && cn->name != utf_init) {
728 exceptions_throw_classformaterror(c, "Illegal reference to special method");
732 #endif /* ENABLE_VERIFIER */
734 cptags[forward_nameandtypes->thisindex] = CONSTANT_NameAndType;
735 cpinfos[forward_nameandtypes->thisindex] = cn;
737 nfn = forward_nameandtypes;
738 forward_nameandtypes = forward_nameandtypes->next;
741 while (forward_fieldmethints) {
742 constant_nameandtype *nat;
743 constant_FMIref *fmi = NEW(constant_FMIref);
745 #if defined(ENABLE_STATISTICS)
747 count_const_pool_len += sizeof(constant_FMIref);
749 /* resolve simple name and descriptor */
751 nat = class_getconstant(c,
752 forward_fieldmethints->nameandtype_index,
753 CONSTANT_NameAndType);
757 /* add all descriptors in {Field,Method}ref to the descriptor_pool */
759 if (!descriptor_pool_add(descpool, nat->descriptor, NULL))
762 /* the classref is created later */
764 fmi->p.index = forward_fieldmethints->class_index;
765 fmi->name = nat->name;
766 fmi->descriptor = nat->descriptor;
768 cptags[forward_fieldmethints->thisindex] = forward_fieldmethints->tag;
769 cpinfos[forward_fieldmethints->thisindex] = fmi;
771 nff = forward_fieldmethints;
772 forward_fieldmethints = forward_fieldmethints->next;
775 /* everything was ok */
781 /* loader_load_attribute_signature *********************************************
783 Signature_attribute {
784 u2 attribute_name_index;
789 *******************************************************************************/
791 #if defined(ENABLE_JAVASE)
792 bool loader_load_attribute_signature(classbuffer *cb, utf **signature)
802 /* check remaining bytecode */
804 if (!suck_check_classbuffer_size(cb, 4 + 2))
807 /* check attribute length */
809 attribute_length = suck_u4(cb);
811 if (attribute_length != 2) {
812 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
816 if (*signature != NULL) {
817 exceptions_throw_classformaterror(c, "Multiple Signature attributes");
823 signature_index = suck_u2(cb);
825 if (!(*signature = class_getconstant(c, signature_index, CONSTANT_Utf8)))
830 #endif /* defined(ENABLE_JAVASE) */
833 /* load_class_from_sysloader ***************************************************
835 Load the class with the given name using the system class loader
838 name.............the classname
842 NULL if an exception has been thrown
844 *******************************************************************************/
846 classinfo *load_class_from_sysloader(utf *name)
852 assert(class_java_lang_Object);
853 assert(class_java_lang_ClassLoader);
854 assert(class_java_lang_ClassLoader->state & CLASS_LINKED);
856 m = class_resolveclassmethod(class_java_lang_ClassLoader,
857 utf_getSystemClassLoader,
858 utf_void__java_lang_ClassLoader,
859 class_java_lang_Object,
865 cl = vm_call_method(m, NULL);
870 c = load_class_from_classloader(name, cl);
876 /* load_class_from_classloader *************************************************
878 Load the class with the given name using the given user-defined class loader.
881 name.............the classname
882 cl...............user-defined class loader
886 NULL if an exception has been thrown
888 *******************************************************************************/
890 classinfo *load_class_from_classloader(utf *name, classloader *cl)
895 java_handle_t *string;
896 #if defined(ENABLE_RT_TIMING)
897 struct timespec time_start, time_lookup, time_prepare, time_java,
901 RT_TIMING_GET_TIME(time_start);
905 /* lookup if this class has already been loaded */
907 c = classcache_lookup(cl, name);
909 RT_TIMING_GET_TIME(time_lookup);
910 RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_CL_LOOKUP);
915 /* if other class loader than bootstrap, call it */
923 namelen = name->blength;
925 /* handle array classes */
926 if (text[0] == '[') {
932 /* check for cases like `[L;' or `[L[I;' or `[Ljava.lang.Object' */
933 if (namelen < 4 || text[2] == '[' || text[namelen - 1] != ';') {
934 exceptions_throw_classnotfoundexception(name);
938 u = utf_new(text + 2, namelen - 3);
940 if (!(comp = load_class_from_classloader(u, cl)))
943 /* create the array class */
945 c = class_array_of(comp, false);
947 tmpc = classcache_store(cl, c, true);
950 /* exception, free the loaded class */
951 c->state &= ~CLASS_LOADING;
958 /* load the component class */
960 u = utf_new(text + 1, namelen - 1);
962 if (!(comp = load_class_from_classloader(u, cl)))
965 /* create the array class */
967 c = class_array_of(comp, false);
969 tmpc = classcache_store(cl, c, true);
972 /* exception, free the loaded class */
973 c->state &= ~CLASS_LOADING;
980 /* primitive array classes are loaded by the bootstrap loader */
982 c = load_class_bootstrap(name);
988 assert(class_java_lang_Object);
990 lc = class_resolveclassmethod(cl->vftbl->class,
992 utf_java_lang_String__java_lang_Class,
993 class_java_lang_Object,
997 return false; /* exception */
999 /* move return value into `o' and cast it afterwards to a classinfo* */
1001 string = javastring_new_slash_to_dot(name);
1003 RT_TIMING_GET_TIME(time_prepare);
1005 o = vm_call_method(lc, cl, string);
1007 RT_TIMING_GET_TIME(time_java);
1009 c = LLNI_classinfo_unwrap(o);
1012 /* Store this class in the loaded class cache. If another
1013 class with the same (initloader,name) pair has been
1014 stored earlier it will be returned by classcache_store
1015 In this case classcache_store may not free the class
1016 because it has already been exposed to Java code which
1017 may have kept references to that class. */
1019 tmpc = classcache_store(cl, c, false);
1022 /* exception, free the loaded class */
1023 c->state &= ~CLASS_LOADING;
1030 RT_TIMING_GET_TIME(time_cache);
1032 RT_TIMING_TIME_DIFF(time_lookup , time_prepare, RT_TIMING_LOAD_CL_PREPARE);
1033 RT_TIMING_TIME_DIFF(time_prepare, time_java , RT_TIMING_LOAD_CL_JAVA);
1034 RT_TIMING_TIME_DIFF(time_java , time_cache , RT_TIMING_LOAD_CL_CACHE);
1036 /* SUN compatible -verbose:class output */
1038 if (opt_verboseclass && (c != NULL) && (c->classloader == cl)) {
1040 utf_display_printable_ascii_classname(name);
1044 #if defined(ENABLE_JVMTI)
1045 /* fire Class Load JVMTI event */
1046 if (jvmti) jvmti_ClassLoadPrepare(false, c);
1053 c = load_class_bootstrap(name);
1059 /* load_class_bootstrap ********************************************************
1061 Load the class with the given name using the bootstrap class loader.
1064 name.............the classname
1067 loaded classinfo, or
1068 NULL if an exception has been thrown
1071 load_class_bootstrap is synchronized. It can be treated as an
1074 *******************************************************************************/
1076 classinfo *load_class_bootstrap(utf *name)
1081 #if defined(ENABLE_RT_TIMING)
1082 struct timespec time_start, time_lookup, time_array, time_suck,
1083 time_load, time_cache;
1086 RT_TIMING_GET_TIME(time_start);
1092 /* lookup if this class has already been loaded */
1094 r = classcache_lookup(NULL, name);
1097 RT_TIMING_GET_TIME(time_lookup);
1098 RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_BOOT_LOOKUP);
1103 RT_TIMING_GET_TIME(time_lookup);
1104 RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_BOOT_LOOKUP);
1106 /* create the classinfo */
1108 c = class_create_classinfo(name);
1110 /* handle array classes */
1112 if (name->text[0] == '[') {
1113 c = load_newly_created_array(c, NULL);
1118 assert(c->state & CLASS_LOADED);
1120 RT_TIMING_GET_TIME(time_array);
1121 RT_TIMING_TIME_DIFF(time_start,time_array,RT_TIMING_LOAD_BOOT_ARRAY);
1126 #if defined(ENABLE_STATISTICS)
1129 if (opt_getcompilingtime)
1130 compilingtime_stop();
1132 if (opt_getloadingtime)
1133 loadingtime_start();
1136 /* load classdata, throw exception on error */
1141 /* this normally means, the classpath was not set properly */
1143 if (name == utf_java_lang_Object)
1144 vm_abort("java/lang/NoClassDefFoundError: java/lang/Object");
1146 exceptions_throw_classnotfoundexception(name);
1151 RT_TIMING_GET_TIME(time_suck);
1153 /* load the class from the buffer */
1155 r = load_class_from_classbuffer(cb);
1157 RT_TIMING_GET_TIME(time_load);
1160 /* the class could not be loaded, free the classinfo struct */
1165 /* Store this class in the loaded class cache this step also
1166 checks the loading constraints. If the class has been loaded
1167 before, the earlier loaded class is returned. */
1169 classinfo *res = classcache_store(NULL, c, true);
1179 RT_TIMING_GET_TIME(time_cache);
1181 /* SUN compatible -verbose:class output */
1183 if (opt_verboseclass && r) {
1185 utf_display_printable_ascii_classname(name);
1186 printf(" from %s]\n", cb->path);
1193 #if defined(ENABLE_STATISTICS)
1196 if (opt_getloadingtime)
1199 if (opt_getcompilingtime)
1200 compilingtime_start();
1203 RT_TIMING_TIME_DIFF(time_lookup, time_suck , RT_TIMING_LOAD_BOOT_SUCK);
1204 RT_TIMING_TIME_DIFF(time_suck , time_load , RT_TIMING_LOAD_BOOT_LOAD);
1205 RT_TIMING_TIME_DIFF(time_load , time_cache, RT_TIMING_LOAD_BOOT_CACHE);
1206 RT_TIMING_TIME_DIFF(time_lookup, time_cache, RT_TIMING_LOAD_BOOT_TOTAL);
1212 /* load_class_from_classbuffer *************************************************
1214 Loads everything interesting about a class from the class file. The
1215 'classinfo' structure must have been allocated previously.
1217 The super class and the interfaces implemented by this class need
1218 not be loaded. The link is set later by the function 'class_link'.
1221 This function is NOT synchronized!
1223 *******************************************************************************/
1225 classinfo *load_class_from_classbuffer(classbuffer *cb)
1233 descriptor_pool *descpool;
1234 #if defined(ENABLE_STATISTICS)
1238 #if defined(ENABLE_RT_TIMING)
1239 struct timespec time_start, time_checks, time_ndpool, time_cpool,
1240 time_setup, time_fields, time_methods, time_classrefs,
1241 time_descs, time_setrefs, time_parsefds, time_parsemds,
1242 time_parsecpool, time_verify, time_attrs;
1245 RT_TIMING_GET_TIME(time_start);
1247 /* get the classbuffer's class */
1251 /* the class is already loaded */
1253 if (c->state & CLASS_LOADED)
1256 #if defined(ENABLE_STATISTICS)
1258 count_class_loads++;
1261 #if !defined(NDEBUG)
1262 /* output for debugging purposes */
1265 log_message_class("Loading class: ", c);
1268 /* mark start of dump memory area */
1270 dumpsize = dump_size();
1272 /* class is currently loading */
1274 c->state |= CLASS_LOADING;
1276 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
1277 goto return_exception;
1279 /* check signature */
1281 if (suck_u4(cb) != MAGIC) {
1282 exceptions_throw_classformaterror(c, "Bad magic number");
1284 goto return_exception;
1292 if (!(ma < MAJOR_VERSION || (ma == MAJOR_VERSION && mi <= MINOR_VERSION))) {
1293 exceptions_throw_unsupportedclassversionerror(c, ma, mi);
1294 goto return_exception;
1297 RT_TIMING_GET_TIME(time_checks);
1299 /* create a new descriptor pool */
1301 descpool = descriptor_pool_new(c);
1303 RT_TIMING_GET_TIME(time_ndpool);
1305 /* load the constant pool */
1307 if (!load_constantpool(cb, descpool))
1308 goto return_exception;
1310 RT_TIMING_GET_TIME(time_cpool);
1314 if (!suck_check_classbuffer_size(cb, 2))
1315 goto return_exception;
1317 /* We OR the flags here, as we set already some flags in
1318 class_create_classinfo. */
1320 c->flags |= suck_u2(cb);
1322 /* check ACC flags consistency */
1324 if (c->flags & ACC_INTERFACE) {
1325 if (!(c->flags & ACC_ABSTRACT)) {
1326 /* We work around this because interfaces in JDK 1.1 are
1327 * not declared abstract. */
1329 c->flags |= ACC_ABSTRACT;
1332 if (c->flags & ACC_FINAL) {
1333 exceptions_throw_classformaterror(c,
1334 "Illegal class modifiers: 0x%X",
1336 goto return_exception;
1339 if (c->flags & ACC_SUPER) {
1340 c->flags &= ~ACC_SUPER; /* kjc seems to set this on interfaces */
1344 if ((c->flags & (ACC_ABSTRACT | ACC_FINAL)) == (ACC_ABSTRACT | ACC_FINAL)) {
1345 exceptions_throw_classformaterror(c,
1346 "Illegal class modifiers: 0x%X",
1348 goto return_exception;
1351 if (!suck_check_classbuffer_size(cb, 2 + 2))
1352 goto return_exception;
1358 if (!(name = (utf *) class_getconstant(c, i, CONSTANT_Class)))
1359 goto return_exception;
1361 if (c->name == utf_not_named_yet) {
1362 /* we finally have a name for this class */
1364 class_set_packagename(c);
1366 else if (name != c->name) {
1367 exceptions_throw_noclassdeffounderror_wrong_name(c, name);
1368 goto return_exception;
1371 /* retrieve superclass */
1373 c->super.any = NULL;
1375 if ((i = suck_u2(cb))) {
1376 if (!(supername = (utf *) class_getconstant(c, i, CONSTANT_Class)))
1377 goto return_exception;
1379 /* java.lang.Object may not have a super class. */
1381 if (c->name == utf_java_lang_Object) {
1382 exceptions_throw_classformaterror(NULL, "java.lang.Object with superclass");
1383 goto return_exception;
1386 /* Interfaces must have java.lang.Object as super class. */
1388 if ((c->flags & ACC_INTERFACE) && (supername != utf_java_lang_Object)) {
1389 exceptions_throw_classformaterror(c, "Interfaces must have java.lang.Object as superclass");
1390 goto return_exception;
1396 /* This is only allowed for java.lang.Object. */
1398 if (c->name != utf_java_lang_Object) {
1399 exceptions_throw_classformaterror(c, "Bad superclass index");
1400 goto return_exception;
1404 /* retrieve interfaces */
1406 if (!suck_check_classbuffer_size(cb, 2))
1407 goto return_exception;
1409 c->interfacescount = suck_u2(cb);
1411 if (!suck_check_classbuffer_size(cb, 2 * c->interfacescount))
1412 goto return_exception;
1414 c->interfaces = MNEW(classref_or_classinfo, c->interfacescount);
1415 for (i = 0; i < c->interfacescount; i++) {
1416 /* the classrefs are created later */
1417 if (!(c->interfaces[i].any = (utf *) class_getconstant(c, suck_u2(cb), CONSTANT_Class)))
1418 goto return_exception;
1421 RT_TIMING_GET_TIME(time_setup);
1425 if (!suck_check_classbuffer_size(cb, 2))
1426 goto return_exception;
1428 c->fieldscount = suck_u2(cb);
1429 c->fields = MNEW(fieldinfo, c->fieldscount);
1431 MZERO(c->fields, fieldinfo, c->fieldscount);
1433 for (i = 0; i < c->fieldscount; i++) {
1434 if (!field_load(cb, &(c->fields[i]), descpool))
1435 goto return_exception;
1438 RT_TIMING_GET_TIME(time_fields);
1442 if (!suck_check_classbuffer_size(cb, 2))
1443 goto return_exception;
1445 c->methodscount = suck_u2(cb);
1446 c->methods = MNEW(methodinfo, c->methodscount);
1448 MZERO(c->methods, methodinfo, c->methodscount);
1450 for (i = 0; i < c->methodscount; i++) {
1451 if (!method_load(cb, &(c->methods[i]), descpool))
1452 goto return_exception;
1455 RT_TIMING_GET_TIME(time_methods);
1457 /* create the class reference table */
1460 descriptor_pool_create_classrefs(descpool, &(c->classrefcount));
1462 RT_TIMING_GET_TIME(time_classrefs);
1464 /* allocate space for the parsed descriptors */
1466 descriptor_pool_alloc_parsed_descriptors(descpool);
1468 descriptor_pool_get_parsed_descriptors(descpool, &(c->parseddescsize));
1470 #if defined(ENABLE_STATISTICS)
1472 descriptor_pool_get_sizes(descpool, &classrefsize, &descsize);
1473 count_classref_len += classrefsize;
1474 count_parsed_desc_len += descsize;
1478 RT_TIMING_GET_TIME(time_descs);
1480 /* put the classrefs in the constant pool */
1481 for (i = 0; i < c->cpcount; i++) {
1482 if (c->cptags[i] == CONSTANT_Class) {
1483 utf *name = (utf *) c->cpinfos[i];
1484 c->cpinfos[i] = descriptor_pool_lookup_classref(descpool, name);
1488 /* set the super class reference */
1491 c->super.ref = descriptor_pool_lookup_classref(descpool, supername);
1493 goto return_exception;
1496 /* set the super interfaces references */
1498 for (i = 0; i < c->interfacescount; i++) {
1499 c->interfaces[i].ref =
1500 descriptor_pool_lookup_classref(descpool,
1501 (utf *) c->interfaces[i].any);
1502 if (!c->interfaces[i].ref)
1503 goto return_exception;
1506 RT_TIMING_GET_TIME(time_setrefs);
1508 /* parse field descriptors */
1510 for (i = 0; i < c->fieldscount; i++) {
1511 c->fields[i].parseddesc =
1512 descriptor_pool_parse_field_descriptor(descpool,
1513 c->fields[i].descriptor);
1514 if (!c->fields[i].parseddesc)
1515 goto return_exception;
1518 RT_TIMING_GET_TIME(time_parsefds);
1520 /* parse method descriptors */
1522 for (i = 0; i < c->methodscount; i++) {
1523 methodinfo *m = &c->methods[i];
1525 descriptor_pool_parse_method_descriptor(descpool, m->descriptor,
1526 m->flags, class_get_self_classref(m->class));
1528 goto return_exception;
1530 for (j = 0; j < m->rawexceptiontablelength; j++) {
1531 if (!m->rawexceptiontable[j].catchtype.any)
1533 if ((m->rawexceptiontable[j].catchtype.ref =
1534 descriptor_pool_lookup_classref(descpool,
1535 (utf *) m->rawexceptiontable[j].catchtype.any)) == NULL)
1536 goto return_exception;
1539 for (j = 0; j < m->thrownexceptionscount; j++) {
1540 if (!m->thrownexceptions[j].any)
1542 if ((m->thrownexceptions[j].ref = descriptor_pool_lookup_classref(descpool,
1543 (utf *) m->thrownexceptions[j].any)) == NULL)
1544 goto return_exception;
1548 RT_TIMING_GET_TIME(time_parsemds);
1550 /* parse the loaded descriptors */
1552 for (i = 0; i < c->cpcount; i++) {
1553 constant_FMIref *fmi;
1556 switch (c->cptags[i]) {
1557 case CONSTANT_Fieldref:
1558 fmi = (constant_FMIref *) c->cpinfos[i];
1559 fmi->parseddesc.fd =
1560 descriptor_pool_parse_field_descriptor(descpool,
1562 if (!fmi->parseddesc.fd)
1563 goto return_exception;
1564 index = fmi->p.index;
1566 (constant_classref *) class_getconstant(c, index,
1568 if (!fmi->p.classref)
1569 goto return_exception;
1571 case CONSTANT_Methodref:
1572 case CONSTANT_InterfaceMethodref:
1573 fmi = (constant_FMIref *) c->cpinfos[i];
1574 index = fmi->p.index;
1576 (constant_classref *) class_getconstant(c, index,
1578 if (!fmi->p.classref)
1579 goto return_exception;
1580 fmi->parseddesc.md =
1581 descriptor_pool_parse_method_descriptor(descpool,
1585 if (!fmi->parseddesc.md)
1586 goto return_exception;
1591 RT_TIMING_GET_TIME(time_parsecpool);
1593 #ifdef ENABLE_VERIFIER
1594 /* Check if all fields and methods can be uniquely
1595 * identified by (name,descriptor). */
1598 /* We use a hash table here to avoid making the
1599 * average case quadratic in # of methods, fields.
1601 static int shift = 0;
1603 u2 *next; /* for chaining colliding hash entries */
1609 /* Allocate hashtable */
1610 len = c->methodscount;
1611 if (len < c->fieldscount) len = c->fieldscount;
1613 hashtab = MNEW(u2,(hashlen + len));
1614 next = hashtab + hashlen;
1616 /* Determine bitshift (to get good hash values) */
1626 memset(hashtab, 0, sizeof(u2) * (hashlen + len));
1628 for (i = 0; i < c->fieldscount; ++i) {
1629 fieldinfo *fi = c->fields + i;
1631 /* It's ok if we lose bits here */
1632 index = ((((size_t) fi->name) +
1633 ((size_t) fi->descriptor)) >> shift) % hashlen;
1635 if ((old = hashtab[index])) {
1639 if (c->fields[old].name == fi->name &&
1640 c->fields[old].descriptor == fi->descriptor) {
1641 exceptions_throw_classformaterror(c, "Repetitive field name/signature");
1642 goto return_exception;
1644 } while ((old = next[old]));
1646 hashtab[index] = i + 1;
1650 memset(hashtab, 0, sizeof(u2) * (hashlen + hashlen/5));
1652 for (i = 0; i < c->methodscount; ++i) {
1653 methodinfo *mi = c->methods + i;
1655 /* It's ok if we lose bits here */
1656 index = ((((size_t) mi->name) +
1657 ((size_t) mi->descriptor)) >> shift) % hashlen;
1661 for (dbg=0;dbg<hashlen+hashlen/5;++dbg){
1662 printf("Hash[%d]:%d\n",dbg,hashtab[dbg]);
1666 if ((old = hashtab[index])) {
1670 if (c->methods[old].name == mi->name &&
1671 c->methods[old].descriptor == mi->descriptor) {
1672 exceptions_throw_classformaterror(c, "Repetitive method name/signature");
1673 goto return_exception;
1675 } while ((old = next[old]));
1677 hashtab[index] = i + 1;
1680 MFREE(hashtab, u2, (hashlen + len));
1682 #endif /* ENABLE_VERIFIER */
1684 RT_TIMING_GET_TIME(time_verify);
1686 #if defined(ENABLE_STATISTICS)
1688 size_classinfo += sizeof(classinfo*) * c->interfacescount;
1689 size_fieldinfo += sizeof(fieldinfo) * c->fieldscount;
1690 size_methodinfo += sizeof(methodinfo) * c->methodscount;
1694 /* load attribute structures */
1696 if (!class_load_attributes(cb))
1697 goto return_exception;
1699 /* Pre Java 1.5 version don't check this. This implementation is like
1700 Java 1.5 do it: for class file version 45.3 we don't check it, older
1701 versions are checked.
1704 if (((ma == 45) && (mi > 3)) || (ma > 45)) {
1705 /* check if all data has been read */
1706 s4 classdata_left = ((cb->data + cb->size) - cb->pos);
1708 if (classdata_left > 0) {
1709 exceptions_throw_classformaterror(c, "Extra bytes at the end of class file");
1710 goto return_exception;
1714 RT_TIMING_GET_TIME(time_attrs);
1716 /* release dump area */
1718 dump_release(dumpsize);
1720 /* revert loading state and class is loaded */
1722 c->state = (c->state & ~CLASS_LOADING) | CLASS_LOADED;
1724 #if defined(ENABLE_JVMTI)
1725 /* fire Class Prepare JVMTI event */
1728 jvmti_ClassLoadPrepare(true, c);
1731 #if !defined(NDEBUG)
1733 log_message_class("Loading done class: ", c);
1736 RT_TIMING_TIME_DIFF(time_start , time_checks , RT_TIMING_LOAD_CHECKS);
1737 RT_TIMING_TIME_DIFF(time_checks , time_ndpool , RT_TIMING_LOAD_NDPOOL);
1738 RT_TIMING_TIME_DIFF(time_ndpool , time_cpool , RT_TIMING_LOAD_CPOOL);
1739 RT_TIMING_TIME_DIFF(time_cpool , time_setup , RT_TIMING_LOAD_SETUP);
1740 RT_TIMING_TIME_DIFF(time_setup , time_fields , RT_TIMING_LOAD_FIELDS);
1741 RT_TIMING_TIME_DIFF(time_fields , time_methods , RT_TIMING_LOAD_METHODS);
1742 RT_TIMING_TIME_DIFF(time_methods , time_classrefs , RT_TIMING_LOAD_CLASSREFS);
1743 RT_TIMING_TIME_DIFF(time_classrefs , time_descs , RT_TIMING_LOAD_DESCS);
1744 RT_TIMING_TIME_DIFF(time_descs , time_setrefs , RT_TIMING_LOAD_SETREFS);
1745 RT_TIMING_TIME_DIFF(time_setrefs , time_parsefds , RT_TIMING_LOAD_PARSEFDS);
1746 RT_TIMING_TIME_DIFF(time_parsefds , time_parsemds , RT_TIMING_LOAD_PARSEMDS);
1747 RT_TIMING_TIME_DIFF(time_parsemds , time_parsecpool, RT_TIMING_LOAD_PARSECP);
1748 RT_TIMING_TIME_DIFF(time_parsecpool, time_verify , RT_TIMING_LOAD_VERIFY);
1749 RT_TIMING_TIME_DIFF(time_verify , time_attrs , RT_TIMING_LOAD_ATTRS);
1750 RT_TIMING_TIME_DIFF(time_start , time_attrs , RT_TIMING_LOAD_TOTAL);
1755 /* release dump area */
1757 dump_release(dumpsize);
1759 /* an exception has been thrown */
1765 /* load_newly_created_array ****************************************************
1767 Load a newly created array class.
1770 c....................the array class C has been loaded
1771 other classinfo......the array class was found in the class cache,
1773 NULL.................an exception has been thrown
1776 This is an internal function. Do not use it unless you know exactly
1779 Use one of the load_class_... functions for general array class loading.
1781 *******************************************************************************/
1783 classinfo *load_newly_created_array(classinfo *c, classloader *loader)
1785 classinfo *comp = NULL;
1787 methoddesc *clonedesc;
1788 constant_classref *classrefs;
1793 text = c->name->text;
1794 namelen = c->name->blength;
1796 /* Check array class name */
1798 if ((namelen < 2) || (text[0] != '[')) {
1799 exceptions_throw_classnotfoundexception(c->name);
1803 /* Check the element type */
1807 /* c is an array of arrays. We have to create the component class. */
1809 u = utf_new(text + 1, namelen - 1);
1811 comp = load_class_from_classloader(u, loader);
1816 assert(comp->state & CLASS_LOADED);
1818 /* the array's flags are that of the component class */
1819 c->flags = (comp->flags & ~ACC_INTERFACE) | ACC_FINAL | ACC_ABSTRACT;
1820 c->classloader = comp->classloader;
1824 /* c is an array of objects. */
1826 /* check for cases like `[L;' or `[L[I;' or `[Ljava.lang.Object' */
1827 if ((namelen < 4) || (text[2] == '[') || (text[namelen - 1] != ';')) {
1828 exceptions_throw_classnotfoundexception(c->name);
1832 u = utf_new(text + 2, namelen - 3);
1834 if (!(comp = load_class_from_classloader(u, loader)))
1837 assert(comp->state & CLASS_LOADED);
1839 /* the array's flags are that of the component class */
1840 c->flags = (comp->flags & ~ACC_INTERFACE) | ACC_FINAL | ACC_ABSTRACT;
1841 c->classloader = comp->classloader;
1845 /* c is an array of a primitive type */
1847 /* check for cases like `[II' and whether the character is a
1848 valid primitive type */
1850 if ((namelen > 2) || (primitive_class_get_by_char(text[1]) == NULL)) {
1851 exceptions_throw_classnotfoundexception(c->name);
1855 /* the accessibility of the array class is public (VM Spec 5.3.3) */
1856 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
1857 c->classloader = NULL;
1860 assert(class_java_lang_Object);
1861 #if defined(ENABLE_JAVASE)
1862 assert(class_java_lang_Cloneable);
1863 assert(class_java_io_Serializable);
1866 /* setup the array class */
1868 c->super.cls = class_java_lang_Object;
1870 #if defined(ENABLE_JAVASE)
1872 c->interfacescount = 2;
1873 c->interfaces = MNEW(classref_or_classinfo, 2);
1874 c->interfaces[0].cls = class_java_lang_Cloneable;
1875 c->interfaces[1].cls = class_java_io_Serializable;
1877 #elif defined(ENABLE_JAVAME_CLDC1_1)
1879 c->interfacescount = 0;
1880 c->interfaces = NULL;
1883 # error unknow Java configuration
1886 c->methodscount = 1;
1887 c->methods = MNEW(methodinfo, c->methodscount);
1888 MZERO(c->methods, methodinfo, c->methodscount);
1890 classrefs = MNEW(constant_classref, 2);
1891 CLASSREF_INIT(classrefs[0], c, c->name);
1892 CLASSREF_INIT(classrefs[1], c, utf_java_lang_Object);
1894 /* create descriptor for clone method */
1895 /* we need one paramslot which is reserved for the 'this' parameter */
1896 clonedesc = NEW(methoddesc);
1897 clonedesc->returntype.type = TYPE_ADR;
1898 clonedesc->returntype.classref = classrefs + 1;
1899 clonedesc->returntype.arraydim = 0;
1900 /* initialize params to "empty", add real params below in
1901 descriptor_params_from_paramtypes */
1902 clonedesc->paramcount = 0;
1903 clonedesc->paramslots = 0;
1904 clonedesc->paramtypes[0].classref = classrefs + 0;
1905 clonedesc->params = NULL;
1907 /* create methodinfo */
1910 MSET(clone, 0, methodinfo, 1);
1912 #if defined(ENABLE_THREADS)
1913 lock_init_object_lock(&clone->header);
1916 /* ATTENTION: if you delete the ACC_NATIVE below, set
1917 clone->maxlocals=1 (interpreter related) */
1919 clone->flags = ACC_PUBLIC | ACC_NATIVE;
1920 clone->name = utf_clone;
1921 clone->descriptor = utf_void__java_lang_Object;
1922 clone->parseddesc = clonedesc;
1925 /* parse the descriptor to get the register allocation */
1927 if (!descriptor_params_from_paramtypes(clonedesc, clone->flags))
1930 clone->code = codegen_generate_stub_native(clone, BUILTIN_clone);
1932 /* XXX: field: length? */
1934 /* array classes are not loaded from class files */
1936 c->state |= CLASS_LOADED;
1937 c->parseddescs = (u1 *) clonedesc;
1938 c->parseddescsize = sizeof(methodinfo);
1939 c->classrefs = classrefs;
1940 c->classrefcount = 1;
1942 /* insert class into the loaded class cache */
1943 /* XXX free classinfo if NULL returned? */
1945 return classcache_store(loader, c, true);
1949 /* loader_close ****************************************************************
1951 Frees all resources.
1953 *******************************************************************************/
1955 void loader_close(void)
1962 * These are local overrides for various environment variables in Emacs.
1963 * Please do not remove this and leave it at the end of the file, where
1964 * Emacs will automagically detect them.
1965 * ---------------------------------------------------------------------
1968 * indent-tabs-mode: t
1972 * vim:noexpandtab:sw=4:ts=4: