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
25 $Id: loader.c 8341 2007-08-17 21:32:01Z michi $
38 #include "mm/memory.h"
40 #include "native/llni.h"
42 #include "threads/lock-common.h"
44 #include "toolbox/logging.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/primitive.h"
50 #include "vm/stringlocal.h"
53 #include "vm/jit_interface.h"
55 #if defined(ENABLE_JAVASE)
56 # include "vmcore/annotation.h"
57 # include "vmcore/stackmap.h"
60 #include "vmcore/classcache.h"
61 #include "vmcore/field.h"
62 #include "vmcore/linker.h"
63 #include "vmcore/loader.h"
64 #include "vmcore/method.h"
65 #include "vmcore/options.h"
66 #include "vmcore/rt-timing.h"
68 #if defined(ENABLE_STATISTICS)
69 # include "vmcore/statistics.h"
72 #include "vmcore/suck.h"
74 #if defined(ENABLE_ZLIB)
75 # include "vmcore/zip.h"
78 #if defined(ENABLE_JVMTI)
79 # include "native/jvmti/cacaodbg.h"
83 /* loader_init *****************************************************************
85 Initializes all lists and loads all classes required for the system
88 *******************************************************************************/
90 bool loader_init(void)
92 #if defined(ENABLE_THREADS)
93 list_classpath_entry *lce;
95 /* Initialize the monitor pointer for zip/jar file locking. */
97 for (lce = list_first(list_classpath_entries); lce != NULL;
98 lce = list_next(list_classpath_entries, lce))
99 if (lce->type == CLASSPATH_ARCHIVE)
100 LOCK_INIT_OBJECT_LOCK(lce);
103 /* load some important classes */
105 if (!(class_java_lang_Object = load_class_bootstrap(utf_java_lang_Object)))
108 if (!(class_java_lang_String = load_class_bootstrap(utf_java_lang_String)))
111 #if defined(ENABLE_JAVASE)
112 if (!(class_java_lang_Cloneable =
113 load_class_bootstrap(utf_java_lang_Cloneable)))
116 if (!(class_java_io_Serializable =
117 load_class_bootstrap(utf_java_io_Serializable)))
121 /* load classes for wrapping primitive types */
123 #if defined(ENABLE_JAVASE)
124 if (!(class_java_lang_Void = load_class_bootstrap(utf_java_lang_Void)))
128 if (!(class_java_lang_Boolean =
129 load_class_bootstrap(utf_java_lang_Boolean)))
132 if (!(class_java_lang_Byte = load_class_bootstrap(utf_java_lang_Byte)))
135 if (!(class_java_lang_Character =
136 load_class_bootstrap(utf_java_lang_Character)))
139 if (!(class_java_lang_Short = load_class_bootstrap(utf_java_lang_Short)))
142 if (!(class_java_lang_Integer =
143 load_class_bootstrap(utf_java_lang_Integer)))
146 if (!(class_java_lang_Long = load_class_bootstrap(utf_java_lang_Long)))
149 if (!(class_java_lang_Float = load_class_bootstrap(utf_java_lang_Float)))
152 if (!(class_java_lang_Double = load_class_bootstrap(utf_java_lang_Double)))
156 /* load some other important classes */
158 if (!(class_java_lang_Class = load_class_bootstrap(utf_java_lang_Class)))
161 #if defined(ENABLE_JAVASE)
162 if (!(class_java_lang_ClassLoader =
163 load_class_bootstrap(utf_java_lang_ClassLoader)))
166 if (!(class_java_lang_SecurityManager =
167 load_class_bootstrap(utf_java_lang_SecurityManager)))
171 if (!(class_java_lang_System = load_class_bootstrap(utf_java_lang_System)))
174 if (!(class_java_lang_Thread =
175 load_class_bootstrap(utf_new_char("java/lang/Thread"))))
178 #if defined(ENABLE_JAVASE)
179 if (!(class_java_lang_ThreadGroup =
180 load_class_bootstrap(utf_java_lang_ThreadGroup)))
184 #if defined(WITH_CLASSPATH_GNU)
185 if (!(class_java_lang_VMSystem =
186 load_class_bootstrap(utf_new_char("java/lang/VMSystem"))))
190 if (!(class_java_lang_VMThread =
191 load_class_bootstrap(utf_new_char("java/lang/VMThread"))))
196 /* some classes which may be used more often */
198 #if defined(ENABLE_JAVASE)
199 if (!(class_java_lang_StackTraceElement =
200 load_class_bootstrap(utf_java_lang_StackTraceElement)))
203 if (!(class_java_lang_reflect_Constructor =
204 load_class_bootstrap(utf_java_lang_reflect_Constructor)))
207 if (!(class_java_lang_reflect_Field =
208 load_class_bootstrap(utf_java_lang_reflect_Field)))
211 if (!(class_java_lang_reflect_Method =
212 load_class_bootstrap(utf_java_lang_reflect_Method)))
215 if (!(class_java_security_PrivilegedAction =
216 load_class_bootstrap(utf_new_char("java/security/PrivilegedAction"))))
219 if (!(class_java_util_Vector = load_class_bootstrap(utf_java_util_Vector)))
222 # if defined(WITH_CLASSPATH_SUN)
223 if (!(class_sun_reflect_MagicAccessorImpl =
224 load_class_bootstrap(utf_new_char("sun/reflect/MagicAccessorImpl"))))
228 if (!(arrayclass_java_lang_Object =
229 load_class_bootstrap(utf_new_char("[Ljava/lang/Object;"))))
232 #if defined(ENABLE_ANNOTATIONS)
233 /* needed by annotation support */
234 if (!(class_sun_reflect_ConstantPool =
235 load_class_bootstrap(utf_sun_reflect_ConstantPool)))
238 #if defined(WITH_CLASSPATH_GNU)
239 /* needed by GNU Classpaths annotation support */
240 if (!(class_sun_reflect_annotation_AnnotationParser =
241 load_class_bootstrap(utf_sun_reflect_annotation_AnnotationParser)))
252 /* loader_load_all_classes *****************************************************
254 Loads all classes specified in the BOOTCLASSPATH.
256 *******************************************************************************/
258 void loader_load_all_classes(void)
260 list_classpath_entry *lce;
261 #if defined(ENABLE_ZLIB)
264 hashtable_zipfile_entry *htzfe;
268 for (lce = list_first(list_classpath_entries); lce != NULL;
269 lce = list_next(list_classpath_entries, lce)) {
270 #if defined(ENABLE_ZLIB)
271 if (lce->type == CLASSPATH_ARCHIVE) {
272 /* get the classes hashtable */
276 for (slot = 0; slot < ht->size; slot++) {
277 htzfe = (hashtable_zipfile_entry *) ht->ptr[slot];
279 for (; htzfe; htzfe = htzfe->hashlink) {
282 /* skip all entries in META-INF and .properties,
285 if (!strncmp(u->text, "META-INF", strlen("META-INF")) ||
286 strstr(u->text, ".properties") ||
287 strstr(u->text, ".png"))
290 /* load class from bootstrap classloader */
292 if (!load_class_bootstrap(u)) {
293 fprintf(stderr, "Error loading: ");
294 utf_fprint_printable_ascii_classname(stderr, u);
295 fprintf(stderr, "\n");
298 /* print out exception and cause */
300 exceptions_print_current_exception();
308 #if defined(ENABLE_ZLIB)
315 /* loader_skip_attribute_body **************************************************
317 Skips an attribute the attribute_name_index has already been read.
320 u2 attribute_name_index;
322 u1 info[attribute_length];
325 *******************************************************************************/
327 bool loader_skip_attribute_body(classbuffer *cb)
331 if (!suck_check_classbuffer_size(cb, 4))
334 attribute_length = suck_u4(cb);
336 if (!suck_check_classbuffer_size(cb, attribute_length))
339 suck_skip_nbytes(cb, attribute_length);
345 /* load_constantpool ***********************************************************
347 Loads the constantpool of a class, the entries are transformed into
348 a simpler format by resolving references (a detailed overview of
349 the compact structures can be found in global.h).
351 *******************************************************************************/
353 static bool load_constantpool(classbuffer *cb, descriptor_pool *descpool)
356 /* The following structures are used to save information which cannot be
357 processed during the first pass. After the complete constantpool has
358 been traversed the references can be resolved.
359 (only in specific order) */
361 /* CONSTANT_Class entries */
362 typedef struct forward_class {
363 struct forward_class *next;
368 /* CONSTANT_String */
369 typedef struct forward_string {
370 struct forward_string *next;
375 /* CONSTANT_NameAndType */
376 typedef struct forward_nameandtype {
377 struct forward_nameandtype *next;
381 } forward_nameandtype;
383 /* CONSTANT_Fieldref, CONSTANT_Methodref or CONSTANT_InterfaceMethodref */
384 typedef struct forward_fieldmethint {
385 struct forward_fieldmethint *next;
389 u2 nameandtype_index;
390 } forward_fieldmethint;
396 forward_class *forward_classes = NULL;
397 forward_string *forward_strings = NULL;
398 forward_nameandtype *forward_nameandtypes = NULL;
399 forward_fieldmethint *forward_fieldmethints = NULL;
403 forward_nameandtype *nfn;
404 forward_fieldmethint *nff;
412 /* number of entries in the constant_pool table plus one */
413 if (!suck_check_classbuffer_size(cb, 2))
416 cpcount = c->cpcount = suck_u2(cb);
418 /* allocate memory */
419 cptags = c->cptags = MNEW(u1, cpcount);
420 cpinfos = c->cpinfos = MNEW(voidptr, cpcount);
423 exceptions_throw_classformaterror(c, "Illegal constant pool size");
427 #if defined(ENABLE_STATISTICS)
429 count_const_pool_len += (sizeof(u1) + sizeof(voidptr)) * cpcount;
432 /* initialize constantpool */
433 for (idx = 0; idx < cpcount; idx++) {
434 cptags[idx] = CONSTANT_UNUSED;
439 /******* first pass *******/
440 /* entries which cannot be resolved now are written into
441 temporary structures and traversed again later */
444 while (idx < cpcount) {
447 /* get constant type */
448 if (!suck_check_classbuffer_size(cb, 1))
455 nfc = DNEW(forward_class);
457 nfc->next = forward_classes;
458 forward_classes = nfc;
460 nfc->thisindex = idx;
461 /* reference to CONSTANT_NameAndType */
462 if (!suck_check_classbuffer_size(cb, 2))
465 nfc->name_index = suck_u2(cb);
470 case CONSTANT_String:
471 nfs = DNEW(forward_string);
473 nfs->next = forward_strings;
474 forward_strings = nfs;
476 nfs->thisindex = idx;
478 /* reference to CONSTANT_Utf8_info with string characters */
479 if (!suck_check_classbuffer_size(cb, 2))
482 nfs->string_index = suck_u2(cb);
487 case CONSTANT_NameAndType:
488 nfn = DNEW(forward_nameandtype);
490 nfn->next = forward_nameandtypes;
491 forward_nameandtypes = nfn;
493 nfn->thisindex = idx;
495 if (!suck_check_classbuffer_size(cb, 2 + 2))
498 /* reference to CONSTANT_Utf8_info containing simple name */
499 nfn->name_index = suck_u2(cb);
501 /* reference to CONSTANT_Utf8_info containing field or method
503 nfn->sig_index = suck_u2(cb);
508 case CONSTANT_Fieldref:
509 case CONSTANT_Methodref:
510 case CONSTANT_InterfaceMethodref:
511 nff = DNEW(forward_fieldmethint);
513 nff->next = forward_fieldmethints;
514 forward_fieldmethints = nff;
516 nff->thisindex = idx;
520 if (!suck_check_classbuffer_size(cb, 2 + 2))
523 /* class or interface type that contains the declaration of the
525 nff->class_index = suck_u2(cb);
527 /* name and descriptor of the field or method */
528 nff->nameandtype_index = suck_u2(cb);
533 case CONSTANT_Integer: {
534 constant_integer *ci = NEW(constant_integer);
536 #if defined(ENABLE_STATISTICS)
538 count_const_pool_len += sizeof(constant_integer);
541 if (!suck_check_classbuffer_size(cb, 4))
544 ci->value = suck_s4(cb);
545 cptags[idx] = CONSTANT_Integer;
552 case CONSTANT_Float: {
553 constant_float *cf = NEW(constant_float);
555 #if defined(ENABLE_STATISTICS)
557 count_const_pool_len += sizeof(constant_float);
560 if (!suck_check_classbuffer_size(cb, 4))
563 cf->value = suck_float(cb);
564 cptags[idx] = CONSTANT_Float;
571 case CONSTANT_Long: {
572 constant_long *cl = NEW(constant_long);
574 #if defined(ENABLE_STATISTICS)
576 count_const_pool_len += sizeof(constant_long);
579 if (!suck_check_classbuffer_size(cb, 8))
582 cl->value = suck_s8(cb);
583 cptags[idx] = CONSTANT_Long;
587 exceptions_throw_classformaterror(c, "Invalid constant pool entry");
593 case CONSTANT_Double: {
594 constant_double *cd = NEW(constant_double);
596 #if defined(ENABLE_STATISTICS)
598 count_const_pool_len += sizeof(constant_double);
601 if (!suck_check_classbuffer_size(cb, 8))
604 cd->value = suck_double(cb);
605 cptags[idx] = CONSTANT_Double;
609 exceptions_throw_classformaterror(c, "Invalid constant pool entry");
615 case CONSTANT_Utf8: {
618 /* number of bytes in the bytes array (not string-length) */
619 if (!suck_check_classbuffer_size(cb, 2))
622 length = suck_u2(cb);
623 cptags[idx] = CONSTANT_Utf8;
625 /* validate the string */
626 if (!suck_check_classbuffer_size(cb, length))
629 #ifdef ENABLE_VERIFIER
631 !is_valid_utf((char *) cb->pos, (char *) (cb->pos + length)))
633 exceptions_throw_classformaterror(c, "Invalid UTF-8 string");
636 #endif /* ENABLE_VERIFIER */
637 /* insert utf-string into the utf-symboltable */
638 cpinfos[idx] = utf_new((char *) cb->pos, length);
640 /* skip bytes of the string (buffer size check above) */
641 suck_skip_nbytes(cb, length);
647 exceptions_throw_classformaterror(c, "Illegal constant pool type");
653 /* resolve entries in temporary structures */
655 while (forward_classes) {
657 class_getconstant(c, forward_classes->name_index, CONSTANT_Utf8);
661 #ifdef ENABLE_VERIFIER
662 if (opt_verify && !is_valid_name_utf(name)) {
663 exceptions_throw_classformaterror(c, "Class reference with invalid name");
666 #endif /* ENABLE_VERIFIER */
668 /* add all class references to the descriptor_pool */
670 if (!descriptor_pool_add_class(descpool, name))
673 cptags[forward_classes->thisindex] = CONSTANT_Class;
675 /* the classref is created later */
676 cpinfos[forward_classes->thisindex] = name;
678 nfc = forward_classes;
679 forward_classes = forward_classes->next;
682 while (forward_strings) {
684 class_getconstant(c, forward_strings->string_index, CONSTANT_Utf8);
688 /* resolve utf-string */
689 cptags[forward_strings->thisindex] = CONSTANT_String;
690 cpinfos[forward_strings->thisindex] = text;
692 nfs = forward_strings;
693 forward_strings = forward_strings->next;
696 while (forward_nameandtypes) {
697 constant_nameandtype *cn = NEW(constant_nameandtype);
699 #if defined(ENABLE_STATISTICS)
701 count_const_pool_len += sizeof(constant_nameandtype);
704 /* resolve simple name and descriptor */
705 cn->name = class_getconstant(c,
706 forward_nameandtypes->name_index,
711 cn->descriptor = class_getconstant(c,
712 forward_nameandtypes->sig_index,
717 #ifdef ENABLE_VERIFIER
720 if (!is_valid_name_utf(cn->name)) {
721 exceptions_throw_classformaterror(c,
722 "Illegal Field name \"%s\"",
728 /* disallow referencing <clinit> among others */
729 if (cn->name->text[0] == '<' && cn->name != utf_init) {
730 exceptions_throw_classformaterror(c, "Illegal reference to special method");
734 #endif /* ENABLE_VERIFIER */
736 cptags[forward_nameandtypes->thisindex] = CONSTANT_NameAndType;
737 cpinfos[forward_nameandtypes->thisindex] = cn;
739 nfn = forward_nameandtypes;
740 forward_nameandtypes = forward_nameandtypes->next;
743 while (forward_fieldmethints) {
744 constant_nameandtype *nat;
745 constant_FMIref *fmi = NEW(constant_FMIref);
747 #if defined(ENABLE_STATISTICS)
749 count_const_pool_len += sizeof(constant_FMIref);
751 /* resolve simple name and descriptor */
753 nat = class_getconstant(c,
754 forward_fieldmethints->nameandtype_index,
755 CONSTANT_NameAndType);
759 /* add all descriptors in {Field,Method}ref to the descriptor_pool */
761 if (!descriptor_pool_add(descpool, nat->descriptor, NULL))
764 /* the classref is created later */
766 fmi->p.index = forward_fieldmethints->class_index;
767 fmi->name = nat->name;
768 fmi->descriptor = nat->descriptor;
770 cptags[forward_fieldmethints->thisindex] = forward_fieldmethints->tag;
771 cpinfos[forward_fieldmethints->thisindex] = fmi;
773 nff = forward_fieldmethints;
774 forward_fieldmethints = forward_fieldmethints->next;
777 /* everything was ok */
783 /* loader_load_attribute_signature *********************************************
785 Signature_attribute {
786 u2 attribute_name_index;
791 *******************************************************************************/
793 #if defined(ENABLE_JAVASE)
794 bool loader_load_attribute_signature(classbuffer *cb, utf **signature)
804 /* check remaining bytecode */
806 if (!suck_check_classbuffer_size(cb, 4 + 2))
809 /* check attribute length */
811 attribute_length = suck_u4(cb);
813 if (attribute_length != 2) {
814 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
818 if (*signature != NULL) {
819 exceptions_throw_classformaterror(c, "Multiple Signature attributes");
825 signature_index = suck_u2(cb);
827 if (!(*signature = class_getconstant(c, signature_index, CONSTANT_Utf8)))
832 #endif /* defined(ENABLE_JAVASE) */
835 /* load_class_from_sysloader ***************************************************
837 Load the class with the given name using the system class loader
840 name.............the classname
844 NULL if an exception has been thrown
846 *******************************************************************************/
848 classinfo *load_class_from_sysloader(utf *name)
854 assert(class_java_lang_Object);
855 assert(class_java_lang_ClassLoader);
856 assert(class_java_lang_ClassLoader->state & CLASS_LINKED);
858 m = class_resolveclassmethod(class_java_lang_ClassLoader,
859 utf_getSystemClassLoader,
860 utf_void__java_lang_ClassLoader,
861 class_java_lang_Object,
867 cl = vm_call_method(m, NULL);
872 c = load_class_from_classloader(name, cl);
878 /* load_class_from_classloader *************************************************
880 Load the class with the given name using the given user-defined class loader.
883 name.............the classname
884 cl...............user-defined class loader
888 NULL if an exception has been thrown
890 *******************************************************************************/
892 classinfo *load_class_from_classloader(utf *name, classloader *cl)
897 java_handle_t *string;
898 #if defined(ENABLE_RT_TIMING)
899 struct timespec time_start, time_lookup, time_prepare, time_java,
903 RT_TIMING_GET_TIME(time_start);
907 /* lookup if this class has already been loaded */
909 c = classcache_lookup(cl, name);
911 RT_TIMING_GET_TIME(time_lookup);
912 RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_CL_LOOKUP);
917 /* if other class loader than bootstrap, call it */
925 namelen = name->blength;
927 /* handle array classes */
928 if (text[0] == '[') {
934 /* check for cases like `[L;' or `[L[I;' or `[Ljava.lang.Object' */
935 if (namelen < 4 || text[2] == '[' || text[namelen - 1] != ';') {
936 exceptions_throw_classnotfoundexception(name);
940 u = utf_new(text + 2, namelen - 3);
942 if (!(comp = load_class_from_classloader(u, cl)))
945 /* create the array class */
947 c = class_array_of(comp, false);
949 tmpc = classcache_store(cl, c, true);
952 /* exception, free the loaded class */
953 c->state &= ~CLASS_LOADING;
960 /* load the component class */
962 u = utf_new(text + 1, namelen - 1);
964 if (!(comp = load_class_from_classloader(u, cl)))
967 /* create the array class */
969 c = class_array_of(comp, false);
971 tmpc = classcache_store(cl, c, true);
974 /* exception, free the loaded class */
975 c->state &= ~CLASS_LOADING;
982 /* primitive array classes are loaded by the bootstrap loader */
984 c = load_class_bootstrap(name);
990 assert(class_java_lang_Object);
992 lc = class_resolveclassmethod(cl->vftbl->class,
994 utf_java_lang_String__java_lang_Class,
995 class_java_lang_Object,
999 return false; /* exception */
1001 /* move return value into `o' and cast it afterwards to a classinfo* */
1003 string = javastring_new_slash_to_dot(name);
1005 RT_TIMING_GET_TIME(time_prepare);
1007 o = vm_call_method(lc, cl, string);
1009 RT_TIMING_GET_TIME(time_java);
1011 c = LLNI_classinfo_unwrap(o);
1014 /* Store this class in the loaded class cache. If another
1015 class with the same (initloader,name) pair has been
1016 stored earlier it will be returned by classcache_store
1017 In this case classcache_store may not free the class
1018 because it has already been exposed to Java code which
1019 may have kept references to that class. */
1021 tmpc = classcache_store(cl, c, false);
1024 /* exception, free the loaded class */
1025 c->state &= ~CLASS_LOADING;
1032 RT_TIMING_GET_TIME(time_cache);
1034 RT_TIMING_TIME_DIFF(time_lookup , time_prepare, RT_TIMING_LOAD_CL_PREPARE);
1035 RT_TIMING_TIME_DIFF(time_prepare, time_java , RT_TIMING_LOAD_CL_JAVA);
1036 RT_TIMING_TIME_DIFF(time_java , time_cache , RT_TIMING_LOAD_CL_CACHE);
1038 /* SUN compatible -verbose:class output */
1040 if (opt_verboseclass && (c != NULL) && (c->classloader == cl)) {
1042 utf_display_printable_ascii_classname(name);
1046 #if defined(ENABLE_JVMTI)
1047 /* fire Class Load JVMTI event */
1048 if (jvmti) jvmti_ClassLoadPrepare(false, c);
1055 c = load_class_bootstrap(name);
1061 /* load_class_bootstrap ********************************************************
1063 Load the class with the given name using the bootstrap class loader.
1066 name.............the classname
1069 loaded classinfo, or
1070 NULL if an exception has been thrown
1073 load_class_bootstrap is synchronized. It can be treated as an
1076 *******************************************************************************/
1078 classinfo *load_class_bootstrap(utf *name)
1083 #if defined(ENABLE_RT_TIMING)
1084 struct timespec time_start, time_lookup, time_array, time_suck,
1085 time_load, time_cache;
1088 RT_TIMING_GET_TIME(time_start);
1094 /* lookup if this class has already been loaded */
1096 r = classcache_lookup(NULL, name);
1099 RT_TIMING_GET_TIME(time_lookup);
1100 RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_BOOT_LOOKUP);
1105 RT_TIMING_GET_TIME(time_lookup);
1106 RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_BOOT_LOOKUP);
1108 /* create the classinfo */
1110 c = class_create_classinfo(name);
1112 /* handle array classes */
1114 if (name->text[0] == '[') {
1115 c = load_newly_created_array(c, NULL);
1120 assert(c->state & CLASS_LOADED);
1122 RT_TIMING_GET_TIME(time_array);
1123 RT_TIMING_TIME_DIFF(time_start,time_array,RT_TIMING_LOAD_BOOT_ARRAY);
1128 #if defined(ENABLE_STATISTICS)
1131 if (opt_getcompilingtime)
1132 compilingtime_stop();
1134 if (opt_getloadingtime)
1135 loadingtime_start();
1138 /* load classdata, throw exception on error */
1143 /* this normally means, the classpath was not set properly */
1145 if (name == utf_java_lang_Object)
1146 vm_abort("java/lang/NoClassDefFoundError: java/lang/Object");
1148 exceptions_throw_classnotfoundexception(name);
1153 RT_TIMING_GET_TIME(time_suck);
1155 /* load the class from the buffer */
1157 r = load_class_from_classbuffer(cb);
1159 RT_TIMING_GET_TIME(time_load);
1162 /* the class could not be loaded, free the classinfo struct */
1167 /* Store this class in the loaded class cache this step also
1168 checks the loading constraints. If the class has been loaded
1169 before, the earlier loaded class is returned. */
1171 classinfo *res = classcache_store(NULL, c, true);
1181 RT_TIMING_GET_TIME(time_cache);
1183 /* SUN compatible -verbose:class output */
1185 if (opt_verboseclass && r) {
1187 utf_display_printable_ascii_classname(name);
1188 printf(" from %s]\n", cb->path);
1195 #if defined(ENABLE_STATISTICS)
1198 if (opt_getloadingtime)
1201 if (opt_getcompilingtime)
1202 compilingtime_start();
1205 RT_TIMING_TIME_DIFF(time_lookup, time_suck , RT_TIMING_LOAD_BOOT_SUCK);
1206 RT_TIMING_TIME_DIFF(time_suck , time_load , RT_TIMING_LOAD_BOOT_LOAD);
1207 RT_TIMING_TIME_DIFF(time_load , time_cache, RT_TIMING_LOAD_BOOT_CACHE);
1208 RT_TIMING_TIME_DIFF(time_lookup, time_cache, RT_TIMING_LOAD_BOOT_TOTAL);
1214 /* load_class_from_classbuffer *************************************************
1216 Loads everything interesting about a class from the class file. The
1217 'classinfo' structure must have been allocated previously.
1219 The super class and the interfaces implemented by this class need
1220 not be loaded. The link is set later by the function 'class_link'.
1223 This function is NOT synchronized!
1225 *******************************************************************************/
1227 classinfo *load_class_from_classbuffer(classbuffer *cb)
1235 descriptor_pool *descpool;
1236 #if defined(ENABLE_STATISTICS)
1240 #if defined(ENABLE_RT_TIMING)
1241 struct timespec time_start, time_checks, time_ndpool, time_cpool,
1242 time_setup, time_fields, time_methods, time_classrefs,
1243 time_descs, time_setrefs, time_parsefds, time_parsemds,
1244 time_parsecpool, time_verify, time_attrs;
1247 RT_TIMING_GET_TIME(time_start);
1249 /* get the classbuffer's class */
1253 /* the class is already loaded */
1255 if (c->state & CLASS_LOADED)
1258 #if defined(ENABLE_STATISTICS)
1260 count_class_loads++;
1263 #if !defined(NDEBUG)
1264 /* output for debugging purposes */
1267 log_message_class("Loading class: ", c);
1270 /* mark start of dump memory area */
1272 dumpsize = dump_size();
1274 /* class is currently loading */
1276 c->state |= CLASS_LOADING;
1278 if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
1279 goto return_exception;
1281 /* check signature */
1283 if (suck_u4(cb) != MAGIC) {
1284 exceptions_throw_classformaterror(c, "Bad magic number");
1286 goto return_exception;
1294 if (!(ma < MAJOR_VERSION || (ma == MAJOR_VERSION && mi <= MINOR_VERSION))) {
1295 exceptions_throw_unsupportedclassversionerror(c, ma, mi);
1296 goto return_exception;
1299 RT_TIMING_GET_TIME(time_checks);
1301 /* create a new descriptor pool */
1303 descpool = descriptor_pool_new(c);
1305 RT_TIMING_GET_TIME(time_ndpool);
1307 /* load the constant pool */
1309 if (!load_constantpool(cb, descpool))
1310 goto return_exception;
1312 RT_TIMING_GET_TIME(time_cpool);
1316 if (!suck_check_classbuffer_size(cb, 2))
1317 goto return_exception;
1319 /* We OR the flags here, as we set already some flags in
1320 class_create_classinfo. */
1322 c->flags |= suck_u2(cb);
1324 /* check ACC flags consistency */
1326 if (c->flags & ACC_INTERFACE) {
1327 if (!(c->flags & ACC_ABSTRACT)) {
1328 /* We work around this because interfaces in JDK 1.1 are
1329 * not declared abstract. */
1331 c->flags |= ACC_ABSTRACT;
1334 if (c->flags & ACC_FINAL) {
1335 exceptions_throw_classformaterror(c,
1336 "Illegal class modifiers: 0x%X",
1338 goto return_exception;
1341 if (c->flags & ACC_SUPER) {
1342 c->flags &= ~ACC_SUPER; /* kjc seems to set this on interfaces */
1346 if ((c->flags & (ACC_ABSTRACT | ACC_FINAL)) == (ACC_ABSTRACT | ACC_FINAL)) {
1347 exceptions_throw_classformaterror(c,
1348 "Illegal class modifiers: 0x%X",
1350 goto return_exception;
1353 if (!suck_check_classbuffer_size(cb, 2 + 2))
1354 goto return_exception;
1360 if (!(name = (utf *) class_getconstant(c, i, CONSTANT_Class)))
1361 goto return_exception;
1363 if (c->name == utf_not_named_yet) {
1364 /* we finally have a name for this class */
1366 class_set_packagename(c);
1368 else if (name != c->name) {
1369 exceptions_throw_noclassdeffounderror_wrong_name(c, name);
1370 goto return_exception;
1373 /* retrieve superclass */
1375 c->super.any = NULL;
1377 if ((i = suck_u2(cb))) {
1378 if (!(supername = (utf *) class_getconstant(c, i, CONSTANT_Class)))
1379 goto return_exception;
1381 /* java.lang.Object may not have a super class. */
1383 if (c->name == utf_java_lang_Object) {
1384 exceptions_throw_classformaterror(NULL, "java.lang.Object with superclass");
1385 goto return_exception;
1388 /* Interfaces must have java.lang.Object as super class. */
1390 if ((c->flags & ACC_INTERFACE) && (supername != utf_java_lang_Object)) {
1391 exceptions_throw_classformaterror(c, "Interfaces must have java.lang.Object as superclass");
1392 goto return_exception;
1398 /* This is only allowed for java.lang.Object. */
1400 if (c->name != utf_java_lang_Object) {
1401 exceptions_throw_classformaterror(c, "Bad superclass index");
1402 goto return_exception;
1406 /* retrieve interfaces */
1408 if (!suck_check_classbuffer_size(cb, 2))
1409 goto return_exception;
1411 c->interfacescount = suck_u2(cb);
1413 if (!suck_check_classbuffer_size(cb, 2 * c->interfacescount))
1414 goto return_exception;
1416 c->interfaces = MNEW(classref_or_classinfo, c->interfacescount);
1417 for (i = 0; i < c->interfacescount; i++) {
1418 /* the classrefs are created later */
1419 if (!(c->interfaces[i].any = (utf *) class_getconstant(c, suck_u2(cb), CONSTANT_Class)))
1420 goto return_exception;
1423 RT_TIMING_GET_TIME(time_setup);
1427 if (!suck_check_classbuffer_size(cb, 2))
1428 goto return_exception;
1430 c->fieldscount = suck_u2(cb);
1431 c->fields = MNEW(fieldinfo, c->fieldscount);
1433 MZERO(c->fields, fieldinfo, c->fieldscount);
1435 for (i = 0; i < c->fieldscount; i++) {
1436 if (!field_load(cb, &(c->fields[i]), descpool))
1437 goto return_exception;
1440 RT_TIMING_GET_TIME(time_fields);
1444 if (!suck_check_classbuffer_size(cb, 2))
1445 goto return_exception;
1447 c->methodscount = suck_u2(cb);
1448 c->methods = MNEW(methodinfo, c->methodscount);
1450 MZERO(c->methods, methodinfo, c->methodscount);
1452 for (i = 0; i < c->methodscount; i++) {
1453 if (!method_load(cb, &(c->methods[i]), descpool))
1454 goto return_exception;
1457 RT_TIMING_GET_TIME(time_methods);
1459 /* create the class reference table */
1462 descriptor_pool_create_classrefs(descpool, &(c->classrefcount));
1464 RT_TIMING_GET_TIME(time_classrefs);
1466 /* allocate space for the parsed descriptors */
1468 descriptor_pool_alloc_parsed_descriptors(descpool);
1470 descriptor_pool_get_parsed_descriptors(descpool, &(c->parseddescsize));
1472 #if defined(ENABLE_STATISTICS)
1474 descriptor_pool_get_sizes(descpool, &classrefsize, &descsize);
1475 count_classref_len += classrefsize;
1476 count_parsed_desc_len += descsize;
1480 RT_TIMING_GET_TIME(time_descs);
1482 /* put the classrefs in the constant pool */
1483 for (i = 0; i < c->cpcount; i++) {
1484 if (c->cptags[i] == CONSTANT_Class) {
1485 utf *name = (utf *) c->cpinfos[i];
1486 c->cpinfos[i] = descriptor_pool_lookup_classref(descpool, name);
1490 /* set the super class reference */
1493 c->super.ref = descriptor_pool_lookup_classref(descpool, supername);
1495 goto return_exception;
1498 /* set the super interfaces references */
1500 for (i = 0; i < c->interfacescount; i++) {
1501 c->interfaces[i].ref =
1502 descriptor_pool_lookup_classref(descpool,
1503 (utf *) c->interfaces[i].any);
1504 if (!c->interfaces[i].ref)
1505 goto return_exception;
1508 RT_TIMING_GET_TIME(time_setrefs);
1510 /* parse field descriptors */
1512 for (i = 0; i < c->fieldscount; i++) {
1513 c->fields[i].parseddesc =
1514 descriptor_pool_parse_field_descriptor(descpool,
1515 c->fields[i].descriptor);
1516 if (!c->fields[i].parseddesc)
1517 goto return_exception;
1520 RT_TIMING_GET_TIME(time_parsefds);
1522 /* parse method descriptors */
1524 for (i = 0; i < c->methodscount; i++) {
1525 methodinfo *m = &c->methods[i];
1527 descriptor_pool_parse_method_descriptor(descpool, m->descriptor,
1528 m->flags, class_get_self_classref(m->class));
1530 goto return_exception;
1532 for (j = 0; j < m->rawexceptiontablelength; j++) {
1533 if (!m->rawexceptiontable[j].catchtype.any)
1535 if ((m->rawexceptiontable[j].catchtype.ref =
1536 descriptor_pool_lookup_classref(descpool,
1537 (utf *) m->rawexceptiontable[j].catchtype.any)) == NULL)
1538 goto return_exception;
1541 for (j = 0; j < m->thrownexceptionscount; j++) {
1542 if (!m->thrownexceptions[j].any)
1544 if ((m->thrownexceptions[j].ref = descriptor_pool_lookup_classref(descpool,
1545 (utf *) m->thrownexceptions[j].any)) == NULL)
1546 goto return_exception;
1550 RT_TIMING_GET_TIME(time_parsemds);
1552 /* parse the loaded descriptors */
1554 for (i = 0; i < c->cpcount; i++) {
1555 constant_FMIref *fmi;
1558 switch (c->cptags[i]) {
1559 case CONSTANT_Fieldref:
1560 fmi = (constant_FMIref *) c->cpinfos[i];
1561 fmi->parseddesc.fd =
1562 descriptor_pool_parse_field_descriptor(descpool,
1564 if (!fmi->parseddesc.fd)
1565 goto return_exception;
1566 index = fmi->p.index;
1568 (constant_classref *) class_getconstant(c, index,
1570 if (!fmi->p.classref)
1571 goto return_exception;
1573 case CONSTANT_Methodref:
1574 case CONSTANT_InterfaceMethodref:
1575 fmi = (constant_FMIref *) c->cpinfos[i];
1576 index = fmi->p.index;
1578 (constant_classref *) class_getconstant(c, index,
1580 if (!fmi->p.classref)
1581 goto return_exception;
1582 fmi->parseddesc.md =
1583 descriptor_pool_parse_method_descriptor(descpool,
1587 if (!fmi->parseddesc.md)
1588 goto return_exception;
1593 RT_TIMING_GET_TIME(time_parsecpool);
1595 #ifdef ENABLE_VERIFIER
1596 /* Check if all fields and methods can be uniquely
1597 * identified by (name,descriptor). */
1600 /* We use a hash table here to avoid making the
1601 * average case quadratic in # of methods, fields.
1603 static int shift = 0;
1605 u2 *next; /* for chaining colliding hash entries */
1611 /* Allocate hashtable */
1612 len = c->methodscount;
1613 if (len < c->fieldscount) len = c->fieldscount;
1615 hashtab = MNEW(u2,(hashlen + len));
1616 next = hashtab + hashlen;
1618 /* Determine bitshift (to get good hash values) */
1628 memset(hashtab, 0, sizeof(u2) * (hashlen + len));
1630 for (i = 0; i < c->fieldscount; ++i) {
1631 fieldinfo *fi = c->fields + i;
1633 /* It's ok if we lose bits here */
1634 index = ((((size_t) fi->name) +
1635 ((size_t) fi->descriptor)) >> shift) % hashlen;
1637 if ((old = hashtab[index])) {
1641 if (c->fields[old].name == fi->name &&
1642 c->fields[old].descriptor == fi->descriptor) {
1643 exceptions_throw_classformaterror(c, "Repetitive field name/signature");
1644 goto return_exception;
1646 } while ((old = next[old]));
1648 hashtab[index] = i + 1;
1652 memset(hashtab, 0, sizeof(u2) * (hashlen + hashlen/5));
1654 for (i = 0; i < c->methodscount; ++i) {
1655 methodinfo *mi = c->methods + i;
1657 /* It's ok if we lose bits here */
1658 index = ((((size_t) mi->name) +
1659 ((size_t) mi->descriptor)) >> shift) % hashlen;
1663 for (dbg=0;dbg<hashlen+hashlen/5;++dbg){
1664 printf("Hash[%d]:%d\n",dbg,hashtab[dbg]);
1668 if ((old = hashtab[index])) {
1672 if (c->methods[old].name == mi->name &&
1673 c->methods[old].descriptor == mi->descriptor) {
1674 exceptions_throw_classformaterror(c, "Repetitive method name/signature");
1675 goto return_exception;
1677 } while ((old = next[old]));
1679 hashtab[index] = i + 1;
1682 MFREE(hashtab, u2, (hashlen + len));
1684 #endif /* ENABLE_VERIFIER */
1686 RT_TIMING_GET_TIME(time_verify);
1688 #if defined(ENABLE_STATISTICS)
1690 size_classinfo += sizeof(classinfo*) * c->interfacescount;
1691 size_fieldinfo += sizeof(fieldinfo) * c->fieldscount;
1692 size_methodinfo += sizeof(methodinfo) * c->methodscount;
1696 /* load attribute structures */
1698 if (!class_load_attributes(cb))
1699 goto return_exception;
1701 /* Pre Java 1.5 version don't check this. This implementation is like
1702 Java 1.5 do it: for class file version 45.3 we don't check it, older
1703 versions are checked.
1706 if (((ma == 45) && (mi > 3)) || (ma > 45)) {
1707 /* check if all data has been read */
1708 s4 classdata_left = ((cb->data + cb->size) - cb->pos);
1710 if (classdata_left > 0) {
1711 exceptions_throw_classformaterror(c, "Extra bytes at the end of class file");
1712 goto return_exception;
1716 RT_TIMING_GET_TIME(time_attrs);
1718 /* release dump area */
1720 dump_release(dumpsize);
1722 /* revert loading state and class is loaded */
1724 c->state = (c->state & ~CLASS_LOADING) | CLASS_LOADED;
1726 #if defined(ENABLE_JVMTI)
1727 /* fire Class Prepare JVMTI event */
1730 jvmti_ClassLoadPrepare(true, c);
1733 #if !defined(NDEBUG)
1735 log_message_class("Loading done class: ", c);
1738 RT_TIMING_TIME_DIFF(time_start , time_checks , RT_TIMING_LOAD_CHECKS);
1739 RT_TIMING_TIME_DIFF(time_checks , time_ndpool , RT_TIMING_LOAD_NDPOOL);
1740 RT_TIMING_TIME_DIFF(time_ndpool , time_cpool , RT_TIMING_LOAD_CPOOL);
1741 RT_TIMING_TIME_DIFF(time_cpool , time_setup , RT_TIMING_LOAD_SETUP);
1742 RT_TIMING_TIME_DIFF(time_setup , time_fields , RT_TIMING_LOAD_FIELDS);
1743 RT_TIMING_TIME_DIFF(time_fields , time_methods , RT_TIMING_LOAD_METHODS);
1744 RT_TIMING_TIME_DIFF(time_methods , time_classrefs , RT_TIMING_LOAD_CLASSREFS);
1745 RT_TIMING_TIME_DIFF(time_classrefs , time_descs , RT_TIMING_LOAD_DESCS);
1746 RT_TIMING_TIME_DIFF(time_descs , time_setrefs , RT_TIMING_LOAD_SETREFS);
1747 RT_TIMING_TIME_DIFF(time_setrefs , time_parsefds , RT_TIMING_LOAD_PARSEFDS);
1748 RT_TIMING_TIME_DIFF(time_parsefds , time_parsemds , RT_TIMING_LOAD_PARSEMDS);
1749 RT_TIMING_TIME_DIFF(time_parsemds , time_parsecpool, RT_TIMING_LOAD_PARSECP);
1750 RT_TIMING_TIME_DIFF(time_parsecpool, time_verify , RT_TIMING_LOAD_VERIFY);
1751 RT_TIMING_TIME_DIFF(time_verify , time_attrs , RT_TIMING_LOAD_ATTRS);
1752 RT_TIMING_TIME_DIFF(time_start , time_attrs , RT_TIMING_LOAD_TOTAL);
1757 /* release dump area */
1759 dump_release(dumpsize);
1761 /* an exception has been thrown */
1767 /* load_newly_created_array ****************************************************
1769 Load a newly created array class.
1772 c....................the array class C has been loaded
1773 other classinfo......the array class was found in the class cache,
1775 NULL.................an exception has been thrown
1778 This is an internal function. Do not use it unless you know exactly
1781 Use one of the load_class_... functions for general array class loading.
1783 *******************************************************************************/
1785 classinfo *load_newly_created_array(classinfo *c, classloader *loader)
1787 classinfo *comp = NULL;
1789 methoddesc *clonedesc;
1790 constant_classref *classrefs;
1795 text = c->name->text;
1796 namelen = c->name->blength;
1798 /* Check array class name */
1800 if ((namelen < 2) || (text[0] != '[')) {
1801 exceptions_throw_classnotfoundexception(c->name);
1805 /* Check the element type */
1809 /* c is an array of arrays. We have to create the component class. */
1811 u = utf_new(text + 1, namelen - 1);
1813 comp = load_class_from_classloader(u, loader);
1818 assert(comp->state & CLASS_LOADED);
1820 /* the array's flags are that of the component class */
1821 c->flags = (comp->flags & ~ACC_INTERFACE) | ACC_FINAL | ACC_ABSTRACT;
1822 c->classloader = comp->classloader;
1826 /* c is an array of objects. */
1828 /* check for cases like `[L;' or `[L[I;' or `[Ljava.lang.Object' */
1829 if ((namelen < 4) || (text[2] == '[') || (text[namelen - 1] != ';')) {
1830 exceptions_throw_classnotfoundexception(c->name);
1834 u = utf_new(text + 2, namelen - 3);
1836 if (!(comp = load_class_from_classloader(u, loader)))
1839 assert(comp->state & CLASS_LOADED);
1841 /* the array's flags are that of the component class */
1842 c->flags = (comp->flags & ~ACC_INTERFACE) | ACC_FINAL | ACC_ABSTRACT;
1843 c->classloader = comp->classloader;
1847 /* c is an array of a primitive type */
1849 /* check for cases like `[II' and whether the character is a
1850 valid primitive type */
1852 if ((namelen > 2) || (primitive_class_get_by_char(text[1]) == NULL)) {
1853 exceptions_throw_classnotfoundexception(c->name);
1857 /* the accessibility of the array class is public (VM Spec 5.3.3) */
1858 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
1859 c->classloader = NULL;
1862 assert(class_java_lang_Object);
1863 #if defined(ENABLE_JAVASE)
1864 assert(class_java_lang_Cloneable);
1865 assert(class_java_io_Serializable);
1868 /* setup the array class */
1870 c->super.cls = class_java_lang_Object;
1872 #if defined(ENABLE_JAVASE)
1874 c->interfacescount = 2;
1875 c->interfaces = MNEW(classref_or_classinfo, 2);
1876 c->interfaces[0].cls = class_java_lang_Cloneable;
1877 c->interfaces[1].cls = class_java_io_Serializable;
1879 #elif defined(ENABLE_JAVAME_CLDC1_1)
1881 c->interfacescount = 0;
1882 c->interfaces = NULL;
1885 # error unknow Java configuration
1888 c->methodscount = 1;
1889 c->methods = MNEW(methodinfo, c->methodscount);
1890 MZERO(c->methods, methodinfo, c->methodscount);
1892 classrefs = MNEW(constant_classref, 2);
1893 CLASSREF_INIT(classrefs[0], c, c->name);
1894 CLASSREF_INIT(classrefs[1], c, utf_java_lang_Object);
1896 /* create descriptor for clone method */
1897 /* we need one paramslot which is reserved for the 'this' parameter */
1898 clonedesc = NEW(methoddesc);
1899 clonedesc->returntype.type = TYPE_ADR;
1900 clonedesc->returntype.classref = classrefs + 1;
1901 clonedesc->returntype.arraydim = 0;
1902 /* initialize params to "empty", add real params below in
1903 descriptor_params_from_paramtypes */
1904 clonedesc->paramcount = 0;
1905 clonedesc->paramslots = 0;
1906 clonedesc->paramtypes[0].classref = classrefs + 0;
1907 clonedesc->params = NULL;
1909 /* create methodinfo */
1912 MSET(clone, 0, methodinfo, 1);
1914 #if defined(ENABLE_THREADS)
1915 lock_init_object_lock(&clone->header);
1918 /* ATTENTION: if you delete the ACC_NATIVE below, set
1919 clone->maxlocals=1 (interpreter related) */
1921 clone->flags = ACC_PUBLIC | ACC_NATIVE;
1922 clone->name = utf_clone;
1923 clone->descriptor = utf_void__java_lang_Object;
1924 clone->parseddesc = clonedesc;
1927 /* parse the descriptor to get the register allocation */
1929 if (!descriptor_params_from_paramtypes(clonedesc, clone->flags))
1932 clone->code = codegen_generate_stub_native(clone, BUILTIN_clone);
1934 /* XXX: field: length? */
1936 /* array classes are not loaded from class files */
1938 c->state |= CLASS_LOADED;
1939 c->parseddescs = (u1 *) clonedesc;
1940 c->parseddescsize = sizeof(methodinfo);
1941 c->classrefs = classrefs;
1942 c->classrefcount = 1;
1944 /* insert class into the loaded class cache */
1945 /* XXX free classinfo if NULL returned? */
1947 return classcache_store(loader, c, true);
1951 /* loader_close ****************************************************************
1953 Frees all resources.
1955 *******************************************************************************/
1957 void loader_close(void)
1964 * These are local overrides for various environment variables in Emacs.
1965 * Please do not remove this and leave it at the end of the file, where
1966 * Emacs will automagically detect them.
1967 * ---------------------------------------------------------------------
1970 * indent-tabs-mode: t
1974 * vim:noexpandtab:sw=4:ts=4: