* src/vm/jit/optimizing/ssa.c (ssa_print_phi): Printing now phi
[cacao.git] / src / vm / loader.c
1 /* src/vm/loader.c - class loader functions
2
3    Copyright (C) 1996-2005, 2006 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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Reinhard Grafl
28
29    Changes: Andreas Krall
30             Roman Obermaiser
31             Mark Probst
32             Edwin Steiner
33             Christian Thalinger
34
35    $Id: loader.c 5184 2006-07-28 10:12:58Z twisti $
36
37 */
38
39
40 #include "config.h"
41
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
45
46 #include "vm/types.h"
47
48 #include "mm/memory.h"
49 #include "native/native.h"
50 #include "native/include/java_lang_Throwable.h"
51
52 #if defined(ENABLE_THREADS)
53 # include "threads/native/threads.h"
54 #endif
55
56 #include "toolbox/logging.h"
57 #include "vm/builtin.h"
58 #include "vm/classcache.h"
59 #include "vm/exceptions.h"
60 #include "vm/global.h"
61 #include "vm/linker.h"
62 #include "vm/loader.h"
63 #include "vm/options.h"
64 #include "vm/statistics.h"
65 #include "vm/stringlocal.h"
66 #include "vm/suck.h"
67 #include "vm/vm.h"
68
69 #if defined(ENABLE_ZLIB)
70 # include "vm/zip.h"
71 #endif
72
73 #include "vm/jit/asmpart.h"
74 #include "vm/jit/codegen-common.h"
75 #include "vm/rt-timing.h"
76
77 #if defined(ENABLE_JVMTI)
78 #include "native/jvmti/cacaodbg.h"
79 #endif
80
81 /******************************************************************************/
82 /* DEBUG HELPERS                                                              */
83 /******************************************************************************/
84
85 #ifndef NDEBUG
86 #define LOADER_DEBUG
87 #endif
88
89 #ifdef LOADER_DEBUG
90 #define LOADER_ASSERT(cond)  assert(cond)
91 #else
92 #define LOADER_ASSERT(cond)
93 #endif
94
95
96 /* loader_init *****************************************************************
97
98    Initializes all lists and loads all classes required for the system
99    or the compiler.
100
101 *******************************************************************************/
102  
103 bool loader_init(void)
104 {
105 #if defined(ENABLE_THREADS)
106         list_classpath_entry *lce;
107
108         /* Initialize the monitor pointer for zip/jar file locking. */
109
110         for (lce = list_first(list_classpath_entries); lce != NULL;
111                  lce = list_next(list_classpath_entries, lce))
112                 if (lce->type == CLASSPATH_ARCHIVE)
113                         lock_init_object_lock((java_objectheader *) lce);
114 #endif
115
116         /* load some important classes */
117
118         if (!(class_java_lang_Object = load_class_bootstrap(utf_java_lang_Object)))
119                 return false;
120
121         if (!(class_java_lang_String = load_class_bootstrap(utf_java_lang_String)))
122                 return false;
123
124         if (!(class_java_lang_Cloneable =
125                   load_class_bootstrap(utf_java_lang_Cloneable)))
126                 return false;
127
128         if (!(class_java_io_Serializable =
129                   load_class_bootstrap(utf_java_io_Serializable)))
130                 return false;
131
132
133         /* load classes for wrapping primitive types */
134
135         if (!(class_java_lang_Void = load_class_bootstrap(utf_java_lang_Void)))
136                 return false;
137
138         if (!(class_java_lang_Boolean =
139                   load_class_bootstrap(utf_java_lang_Boolean)))
140                 return false;
141
142         if (!(class_java_lang_Byte = load_class_bootstrap(utf_java_lang_Byte)))
143                 return false;
144
145         if (!(class_java_lang_Character =
146                   load_class_bootstrap(utf_java_lang_Character)))
147                 return false;
148
149         if (!(class_java_lang_Short = load_class_bootstrap(utf_java_lang_Short)))
150                 return false;
151
152         if (!(class_java_lang_Integer =
153                   load_class_bootstrap(utf_java_lang_Integer)))
154                 return false;
155
156         if (!(class_java_lang_Long = load_class_bootstrap(utf_java_lang_Long)))
157                 return false;
158
159         if (!(class_java_lang_Float = load_class_bootstrap(utf_java_lang_Float)))
160                 return false;
161
162         if (!(class_java_lang_Double = load_class_bootstrap(utf_java_lang_Double)))
163                 return false;
164
165
166         /* load some other important classes */
167
168         if (!(class_java_lang_Class = load_class_bootstrap(utf_java_lang_Class)))
169                 return false;
170
171         if (!(class_java_lang_ClassLoader =
172                   load_class_bootstrap(utf_java_lang_ClassLoader)))
173                 return false;
174
175         if (!(class_java_lang_SecurityManager =
176                   load_class_bootstrap(utf_java_lang_SecurityManager)))
177                 return false;
178
179         if (!(class_java_lang_System = load_class_bootstrap(utf_java_lang_System)))
180                 return false;
181
182         if (!(class_java_lang_Thread =
183                   load_class_bootstrap(utf_new_char("java/lang/Thread"))))
184                 return false;
185
186         if (!(class_java_lang_ThreadGroup =
187                   load_class_bootstrap(utf_java_lang_ThreadGroup)))
188                 return false;
189
190         if (!(class_java_lang_VMThread =
191                   load_class_bootstrap(utf_new_char("java/lang/VMThread"))))
192                 return false;
193
194
195         /* some classes which may be used more often */
196
197         if (!(class_java_lang_StackTraceElement =
198                   load_class_bootstrap(utf_java_lang_StackTraceElement)))
199                 return false;
200
201         if (!(class_java_lang_reflect_Constructor =
202                   load_class_bootstrap(utf_java_lang_reflect_Constructor)))
203                 return false;
204
205         if (!(class_java_lang_reflect_Field =
206                   load_class_bootstrap(utf_java_lang_reflect_Field)))
207                 return false;
208
209         if (!(class_java_lang_reflect_Method =
210                   load_class_bootstrap(utf_java_lang_reflect_Method)))
211                 return false;
212
213         if (!(class_java_security_PrivilegedAction =
214                   load_class_bootstrap(utf_new_char("java/security/PrivilegedAction"))))
215                 return false;
216
217         if (!(class_java_util_Vector = load_class_bootstrap(utf_java_util_Vector)))
218                 return false;
219
220         if (!(arrayclass_java_lang_Object =
221                   load_class_bootstrap(utf_new_char("[Ljava/lang/Object;"))))
222                 return false;
223
224         return true;
225 }
226
227
228 /* loader_load_all_classes *****************************************************
229
230    Loads all classes specified in the BOOTCLASSPATH.
231
232 *******************************************************************************/
233
234 void loader_load_all_classes(void)
235 {
236         list_classpath_entry    *lce;
237 #if defined(ENABLE_ZLIB)
238         hashtable               *ht;
239         s4                       slot;
240         hashtable_zipfile_entry *htzfe;
241         utf                     *u;
242 #endif
243
244         for (lce = list_first(list_classpath_entries); lce != NULL;
245                  lce = list_next(list_classpath_entries, lce)) {
246 #if defined(ENABLE_ZLIB)
247                 if (lce->type == CLASSPATH_ARCHIVE) {
248                         /* get the classes hashtable */
249
250                         ht = lce->htclasses;
251
252                         for (slot = 0; slot < ht->size; slot++) {
253                                 htzfe = (hashtable_zipfile_entry *) ht->ptr[slot];
254
255                                 for (; htzfe; htzfe = htzfe->hashlink) {
256                                         u = htzfe->filename;
257
258                                         /* skip all entries in META-INF and .properties,
259                        .png files */
260
261                                         if (!strncmp(u->text, "META-INF", strlen("META-INF")) ||
262                                                 strstr(u->text, ".properties") ||
263                                                 strstr(u->text, ".png"))
264                                                 continue;
265
266                                         /* load class from bootstrap classloader */
267
268                                         if (!load_class_bootstrap(u)) {
269                                                 fprintf(stderr, "Error loading: ");
270                                                 utf_fprint_printable_ascii_classname(stderr, u);
271                                                 fprintf(stderr, "\n");
272
273 #if !defined(NDEBUG)
274                                                 /* print out exception and cause */
275
276                                                 exceptions_print_exception(*exceptionptr);
277 #endif
278                                         }
279                                 }
280                         }
281
282                 } else {
283 #endif
284 #if defined(ENABLE_ZLIB)
285                 }
286 #endif
287         }
288 }
289
290
291 /* skipattributebody ***********************************************************
292
293    Skips an attribute after the 16 bit reference to attribute_name has
294    already been read.
295         
296 *******************************************************************************/
297
298 static bool skipattributebody(classbuffer *cb)
299 {
300         u4 len;
301
302         if (!suck_check_classbuffer_size(cb, 4))
303                 return false;
304
305         len = suck_u4(cb);
306
307         if (!suck_check_classbuffer_size(cb, len))
308                 return false;
309
310         suck_skip_nbytes(cb, len);
311
312         return true;
313 }
314
315
316 /************************* Function: skipattributes ****************************
317
318         skips num attribute structures
319         
320 *******************************************************************************/
321
322 static bool skipattributes(classbuffer *cb, u4 num)
323 {
324         u4 i;
325         u4 len;
326
327         for (i = 0; i < num; i++) {
328                 if (!suck_check_classbuffer_size(cb, 2 + 4))
329                         return false;
330
331                 suck_u2(cb);
332                 len = suck_u4(cb);
333
334                 if (!suck_check_classbuffer_size(cb, len))
335                         return false;
336
337                 suck_skip_nbytes(cb, len);
338         }
339
340         return true;
341 }
342
343
344 /* load_constantpool ***********************************************************
345
346    Loads the constantpool of a class, the entries are transformed into
347    a simpler format by resolving references (a detailed overview of
348    the compact structures can be found in global.h).
349
350 *******************************************************************************/
351
352 static bool load_constantpool(classbuffer *cb, descriptor_pool *descpool)
353 {
354
355         /* The following structures are used to save information which cannot be 
356            processed during the first pass. After the complete constantpool has 
357            been traversed the references can be resolved. 
358            (only in specific order)                                                */
359         
360         /* CONSTANT_Class entries */
361         typedef struct forward_class {
362                 struct forward_class *next;
363                 u2 thisindex;
364                 u2 name_index;
365         } forward_class;
366
367         /* CONSTANT_String */
368         typedef struct forward_string {
369                 struct forward_string *next;
370                 u2 thisindex;
371                 u2 string_index;
372         } forward_string;
373
374         /* CONSTANT_NameAndType */
375         typedef struct forward_nameandtype {
376                 struct forward_nameandtype *next;
377                 u2 thisindex;
378                 u2 name_index;
379                 u2 sig_index;
380         } forward_nameandtype;
381
382         /* CONSTANT_Fieldref, CONSTANT_Methodref or CONSTANT_InterfaceMethodref */
383         typedef struct forward_fieldmethint {
384                 struct forward_fieldmethint *next;
385                 u2 thisindex;
386                 u1 tag;
387                 u2 class_index;
388                 u2 nameandtype_index;
389         } forward_fieldmethint;
390
391
392         classinfo *c;
393         u4 idx;
394
395         forward_class *forward_classes = NULL;
396         forward_string *forward_strings = NULL;
397         forward_nameandtype *forward_nameandtypes = NULL;
398         forward_fieldmethint *forward_fieldmethints = NULL;
399
400         forward_class *nfc;
401         forward_string *nfs;
402         forward_nameandtype *nfn;
403         forward_fieldmethint *nff;
404
405         u4 cpcount;
406         u1 *cptags;
407         voidptr *cpinfos;
408
409         c = cb->class;
410
411         /* number of entries in the constant_pool table plus one */
412         if (!suck_check_classbuffer_size(cb, 2))
413                 return false;
414
415         cpcount = c->cpcount = suck_u2(cb);
416
417         /* allocate memory */
418         cptags  = c->cptags  = MNEW(u1, cpcount);
419         cpinfos = c->cpinfos = MNEW(voidptr, cpcount);
420
421         if (cpcount < 1) {
422                 exceptions_throw_classformaterror(c, "Illegal constant pool size");
423                 return false;
424         }
425         
426 #if defined(ENABLE_STATISTICS)
427         if (opt_stat)
428                 count_const_pool_len += (sizeof(voidptr) + 1) * cpcount;
429 #endif
430         
431         /* initialize constantpool */
432         for (idx = 0; idx < cpcount; idx++) {
433                 cptags[idx] = CONSTANT_UNUSED;
434                 cpinfos[idx] = NULL;
435         }
436
437                         
438         /******* first pass *******/
439         /* entries which cannot be resolved now are written into 
440            temporary structures and traversed again later        */
441                    
442         idx = 1;
443         while (idx < cpcount) {
444                 u4 t;
445
446                 /* get constant type */
447                 if (!suck_check_classbuffer_size(cb, 1))
448                         return false;
449
450                 t = suck_u1(cb);
451
452                 switch (t) {
453                 case CONSTANT_Class:
454                         nfc = DNEW(forward_class);
455
456                         nfc->next = forward_classes;
457                         forward_classes = nfc;
458
459                         nfc->thisindex = idx;
460                         /* reference to CONSTANT_NameAndType */
461                         if (!suck_check_classbuffer_size(cb, 2))
462                                 return false;
463
464                         nfc->name_index = suck_u2(cb);
465
466                         idx++;
467                         break;
468                         
469                 case CONSTANT_String:
470                         nfs = DNEW(forward_string);
471                                 
472                         nfs->next = forward_strings;
473                         forward_strings = nfs;
474                                 
475                         nfs->thisindex = idx;
476
477                         /* reference to CONSTANT_Utf8_info with string characters */
478                         if (!suck_check_classbuffer_size(cb, 2))
479                                 return false;
480
481                         nfs->string_index = suck_u2(cb);
482                                 
483                         idx++;
484                         break;
485
486                 case CONSTANT_NameAndType:
487                         nfn = DNEW(forward_nameandtype);
488                                 
489                         nfn->next = forward_nameandtypes;
490                         forward_nameandtypes = nfn;
491                                 
492                         nfn->thisindex = idx;
493
494                         if (!suck_check_classbuffer_size(cb, 2 + 2))
495                                 return false;
496
497                         /* reference to CONSTANT_Utf8_info containing simple name */
498                         nfn->name_index = suck_u2(cb);
499
500                         /* reference to CONSTANT_Utf8_info containing field or method
501                            descriptor */
502                         nfn->sig_index = suck_u2(cb);
503                                 
504                         idx++;
505                         break;
506
507                 case CONSTANT_Fieldref:
508                 case CONSTANT_Methodref:
509                 case CONSTANT_InterfaceMethodref:
510                         nff = DNEW(forward_fieldmethint);
511                         
512                         nff->next = forward_fieldmethints;
513                         forward_fieldmethints = nff;
514
515                         nff->thisindex = idx;
516                         /* constant type */
517                         nff->tag = t;
518
519                         if (!suck_check_classbuffer_size(cb, 2 + 2))
520                                 return false;
521
522                         /* class or interface type that contains the declaration of the
523                            field or method */
524                         nff->class_index = suck_u2(cb);
525
526                         /* name and descriptor of the field or method */
527                         nff->nameandtype_index = suck_u2(cb);
528
529                         idx++;
530                         break;
531                                 
532                 case CONSTANT_Integer: {
533                         constant_integer *ci = NEW(constant_integer);
534
535 #if defined(ENABLE_STATISTICS)
536                         if (opt_stat)
537                                 count_const_pool_len += sizeof(constant_integer);
538 #endif
539
540                         if (!suck_check_classbuffer_size(cb, 4))
541                                 return false;
542
543                         ci->value = suck_s4(cb);
544                         cptags[idx] = CONSTANT_Integer;
545                         cpinfos[idx] = ci;
546
547                         idx++;
548                         break;
549                 }
550                                 
551                 case CONSTANT_Float: {
552                         constant_float *cf = NEW(constant_float);
553
554 #if defined(ENABLE_STATISTICS)
555                         if (opt_stat)
556                                 count_const_pool_len += sizeof(constant_float);
557 #endif
558
559                         if (!suck_check_classbuffer_size(cb, 4))
560                                 return false;
561
562                         cf->value = suck_float(cb);
563                         cptags[idx] = CONSTANT_Float;
564                         cpinfos[idx] = cf;
565
566                         idx++;
567                         break;
568                 }
569                                 
570                 case CONSTANT_Long: {
571                         constant_long *cl = NEW(constant_long);
572                                         
573 #if defined(ENABLE_STATISTICS)
574                         if (opt_stat)
575                                 count_const_pool_len += sizeof(constant_long);
576 #endif
577
578                         if (!suck_check_classbuffer_size(cb, 8))
579                                 return false;
580
581                         cl->value = suck_s8(cb);
582                         cptags[idx] = CONSTANT_Long;
583                         cpinfos[idx] = cl;
584                         idx += 2;
585                         if (idx > cpcount) {
586                                 exceptions_throw_classformaterror(c, "Invalid constant pool entry");
587                                 return false;
588                         }
589                         break;
590                 }
591                         
592                 case CONSTANT_Double: {
593                         constant_double *cd = NEW(constant_double);
594                                 
595 #if defined(ENABLE_STATISTICS)
596                         if (opt_stat)
597                                 count_const_pool_len += sizeof(constant_double);
598 #endif
599
600                         if (!suck_check_classbuffer_size(cb, 8))
601                                 return false;
602
603                         cd->value = suck_double(cb);
604                         cptags[idx] = CONSTANT_Double;
605                         cpinfos[idx] = cd;
606                         idx += 2;
607                         if (idx > cpcount) {
608                                 exceptions_throw_classformaterror(c, "Invalid constant pool entry");
609                                 return false;
610                         }
611                         break;
612                 }
613                                 
614                 case CONSTANT_Utf8: { 
615                         u4 length;
616
617                         /* number of bytes in the bytes array (not string-length) */
618                         if (!suck_check_classbuffer_size(cb, 2))
619                                 return false;
620
621                         length = suck_u2(cb);
622                         cptags[idx] = CONSTANT_Utf8;
623
624                         /* validate the string */
625                         if (!suck_check_classbuffer_size(cb, length))
626                                 return false;
627
628 #ifdef ENABLE_VERIFIER
629                         if (opt_verify &&
630                                 !is_valid_utf((char *) cb->pos, (char *) (cb->pos + length))) 
631                         {
632                                 exceptions_throw_classformaterror(c, "Invalid UTF-8 string");
633                                 return false;
634                         }
635 #endif /* ENABLE_VERIFIER */
636                         /* insert utf-string into the utf-symboltable */
637                         cpinfos[idx] = utf_new((char *) cb->pos, length);
638
639                         /* skip bytes of the string (buffer size check above) */
640                         suck_skip_nbytes(cb, length);
641                         idx++;
642                         break;
643                 }
644                                                                                 
645                 default:
646                         exceptions_throw_classformaterror(c, "Illegal constant pool type");
647                         return false;
648                 }  /* end switch */
649         } /* end while */
650
651
652         /* resolve entries in temporary structures */
653
654         while (forward_classes) {
655                 utf *name =
656                         class_getconstant(c, forward_classes->name_index, CONSTANT_Utf8);
657                 if (!name)
658                         return false;
659
660 #ifdef ENABLE_VERIFIER
661                 if (opt_verify && !is_valid_name_utf(name)) {
662                         exceptions_throw_classformaterror(c, "Class reference with invalid name");
663                         return false;
664                 }
665 #endif /* ENABLE_VERIFIER */
666
667                 /* add all class references to the descriptor_pool */
668
669                 if (!descriptor_pool_add_class(descpool, name))
670                         return false;
671
672                 cptags[forward_classes->thisindex] = CONSTANT_Class;
673
674                 if (opt_eager) {
675                         classinfo *tc;
676
677                         if (!(tc = load_class_bootstrap(name)))
678                                 return false;
679
680                         /* link the class later, because we cannot link the class currently
681                            loading */
682                         list_add_first(&unlinkedclasses, tc);
683                 }
684
685                 /* the classref is created later */
686                 cpinfos[forward_classes->thisindex] = name;
687
688                 nfc = forward_classes;
689                 forward_classes = forward_classes->next;
690         }
691
692         while (forward_strings) {
693                 utf *text =
694                         class_getconstant(c, forward_strings->string_index, CONSTANT_Utf8);
695                 if (!text)
696                         return false;
697
698                 /* resolve utf-string */
699                 cptags[forward_strings->thisindex] = CONSTANT_String;
700                 cpinfos[forward_strings->thisindex] = text;
701                 
702                 nfs = forward_strings;
703                 forward_strings = forward_strings->next;
704         }
705
706         while (forward_nameandtypes) {
707                 constant_nameandtype *cn = NEW(constant_nameandtype);   
708
709 #if defined(ENABLE_STATISTICS)
710                 if (opt_stat)
711                         count_const_pool_len += sizeof(constant_nameandtype);
712 #endif
713
714                 /* resolve simple name and descriptor */
715                 cn->name = class_getconstant(c,
716                                                                          forward_nameandtypes->name_index,
717                                                                          CONSTANT_Utf8);
718                 if (!cn->name)
719                         return false;
720
721                 cn->descriptor = class_getconstant(c,
722                                                                                    forward_nameandtypes->sig_index,
723                                                                                    CONSTANT_Utf8);
724                 if (!cn->descriptor)
725                         return false;
726
727 #ifdef ENABLE_VERIFIER
728                 if (opt_verify) {
729                         /* check name */
730                         if (!is_valid_name_utf(cn->name)) {
731                                 exceptions_throw_classformaterror(c,
732                                                                                                   "Illegal Field name \"%s\"",
733                                                                                                   cn->name->text);
734
735                                 return false;
736                         }
737
738                         /* disallow referencing <clinit> among others */
739                         if (cn->name->text[0] == '<' && cn->name != utf_init) {
740                                 exceptions_throw_classformaterror(c, "Illegal reference to special method");
741                                 return false;
742                         }
743                 }
744 #endif /* ENABLE_VERIFIER */
745
746                 cptags[forward_nameandtypes->thisindex] = CONSTANT_NameAndType;
747                 cpinfos[forward_nameandtypes->thisindex] = cn;
748
749                 nfn = forward_nameandtypes;
750                 forward_nameandtypes = forward_nameandtypes->next;
751         }
752
753         while (forward_fieldmethints) {
754                 constant_nameandtype *nat;
755                 constant_FMIref *fmi = NEW(constant_FMIref);
756
757 #if defined(ENABLE_STATISTICS)
758                 if (opt_stat)
759                         count_const_pool_len += sizeof(constant_FMIref);
760 #endif
761                 /* resolve simple name and descriptor */
762
763                 nat = class_getconstant(c,
764                                                                 forward_fieldmethints->nameandtype_index,
765                                                                 CONSTANT_NameAndType);
766                 if (!nat)
767                         return false;
768
769                 /* add all descriptors in {Field,Method}ref to the descriptor_pool */
770
771                 if (!descriptor_pool_add(descpool, nat->descriptor, NULL))
772                         return false;
773
774                 /* the classref is created later */
775
776                 fmi->p.index = forward_fieldmethints->class_index;
777                 fmi->name = nat->name;
778                 fmi->descriptor = nat->descriptor;
779
780                 cptags[forward_fieldmethints->thisindex] = forward_fieldmethints->tag;
781                 cpinfos[forward_fieldmethints->thisindex] = fmi;
782         
783                 nff = forward_fieldmethints;
784                 forward_fieldmethints = forward_fieldmethints->next;
785         }
786
787         /* everything was ok */
788
789         return true;
790 }
791
792
793 /* load_field ******************************************************************
794
795    Load everything about a class field from the class file and fill a
796    'fieldinfo' structure. For static fields, space in the data segment
797    is allocated.
798
799 *******************************************************************************/
800
801 #define field_load_NOVALUE  0xffffffff /* must be bigger than any u2 value! */
802
803 static bool load_field(classbuffer *cb, fieldinfo *f, descriptor_pool *descpool)
804 {
805         classinfo *c;
806         u4 attrnum, i;
807         u4 jtype;
808         u4 pindex = field_load_NOVALUE;     /* constantvalue_index */
809         utf *u;
810
811         c = cb->class;
812
813         if (!suck_check_classbuffer_size(cb, 2 + 2 + 2))
814                 return false;
815
816         f->flags = suck_u2(cb);
817
818         if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
819                 return false;
820
821         f->name = u;
822
823         if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
824                 return false;
825
826         f->descriptor = u;
827         f->parseddesc = NULL;
828
829         if (!descriptor_pool_add(descpool, u, NULL))
830                 return false;
831
832         /* descriptor_pool_add accepts method descriptors, so we have to check  */
833         /* against them here before the call of descriptor_to_basic_type below. */
834         if (u->text[0] == '(') {
835                 exceptions_throw_classformaterror(c, "Method descriptor used for field");
836                 return false;
837         }
838
839 #ifdef ENABLE_VERIFIER
840         if (opt_verify) {
841                 /* check name */
842                 if (!is_valid_name_utf(f->name) || f->name->text[0] == '<') {
843                         exceptions_throw_classformaterror(c,
844                                                                                           "Illegal Field name \"%s\"",
845                                                                                           f->name->text);
846                         return false;
847                 }
848
849                 /* check flag consistency */
850                 i = f->flags & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED);
851
852                 if ((i != 0 && i != ACC_PUBLIC && i != ACC_PRIVATE && i != ACC_PROTECTED) ||
853                         ((f->flags & (ACC_FINAL | ACC_VOLATILE)) == (ACC_FINAL | ACC_VOLATILE))) {
854                         exceptions_throw_classformaterror(c,
855                                                                                           "Illegal field modifiers: 0x%X",
856                                                                                           f->flags);
857                         return false;
858                 }
859
860                 if (c->flags & ACC_INTERFACE) {
861                         if (((f->flags & (ACC_STATIC | ACC_PUBLIC | ACC_FINAL))
862                                 != (ACC_STATIC | ACC_PUBLIC | ACC_FINAL)) ||
863                                 f->flags & ACC_TRANSIENT) {
864                                 exceptions_throw_classformaterror(c,
865                                                                                                   "Illegal field modifiers: 0x%X",
866                                                                                                   f->flags);
867                                 return false;
868                         }
869                 }
870         }
871 #endif /* ENABLE_VERIFIER */
872
873         f->type   = jtype = descriptor_to_basic_type(f->descriptor); /* data type */
874         f->offset = 0;                             /* offset from start of object */
875         f->class  = c;
876
877         switch (f->type) {
878         case TYPE_INT:
879                 f->value.i = 0;
880                 break;
881
882         case TYPE_FLT:
883                 f->value.f = 0.0;
884                 break;
885
886         case TYPE_DBL:
887                 f->value.d = 0.0;
888                 break;
889
890         case TYPE_ADR:
891                 f->value.a = NULL;
892                 if (!(f->flags & ACC_STATIC))
893                         c->flags |= ACC_CLASS_HAS_POINTERS;
894                 break;
895
896         case TYPE_LNG:
897 #if U8_AVAILABLE
898                 f->value.l = 0;
899 #else
900                 f->value.l.low  = 0;
901                 f->value.l.high = 0;
902 #endif
903                 break;
904         }
905
906         /* read attributes */
907         if (!suck_check_classbuffer_size(cb, 2))
908                 return false;
909
910         attrnum = suck_u2(cb);
911         for (i = 0; i < attrnum; i++) {
912                 if (!suck_check_classbuffer_size(cb, 2))
913                         return false;
914
915                 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
916                         return false;
917
918                 if (u == utf_ConstantValue) {
919                         if (!suck_check_classbuffer_size(cb, 4 + 2))
920                                 return false;
921
922                         /* check attribute length */
923                         if (suck_u4(cb) != 2) {
924                                 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
925                                 return false;
926                         }
927                         
928                         /* constant value attribute */
929                         if (pindex != field_load_NOVALUE) {
930                                 exceptions_throw_classformaterror(c, "Multiple ConstantValue attributes");
931                                 return false;
932                         }
933                         
934                         /* index of value in constantpool */            
935                         pindex = suck_u2(cb);
936                 
937                         /* initialize field with value from constantpool */             
938                         switch (jtype) {
939                         case TYPE_INT: {
940                                 constant_integer *ci; 
941
942                                 if (!(ci = class_getconstant(c, pindex, CONSTANT_Integer)))
943                                         return false;
944
945                                 f->value.i = ci->value;
946                         }
947                         break;
948                                         
949                         case TYPE_LNG: {
950                                 constant_long *cl; 
951
952                                 if (!(cl = class_getconstant(c, pindex, CONSTANT_Long)))
953                                         return false;
954
955                                 f->value.l = cl->value;
956                         }
957                         break;
958
959                         case TYPE_FLT: {
960                                 constant_float *cf;
961
962                                 if (!(cf = class_getconstant(c, pindex, CONSTANT_Float)))
963                                         return false;
964
965                                 f->value.f = cf->value;
966                         }
967                         break;
968                                                                                         
969                         case TYPE_DBL: {
970                                 constant_double *cd;
971
972                                 if (!(cd = class_getconstant(c, pindex, CONSTANT_Double)))
973                                         return false;
974
975                                 f->value.d = cd->value;
976                         }
977                         break;
978                                                 
979                         case TYPE_ADR:
980                                 if (!(u = class_getconstant(c, pindex, CONSTANT_String)))
981                                         return false;
982
983                                 /* create javastring from compressed utf8-string */
984                                 f->value.a = literalstring_new(u);
985                                 break;
986         
987                         default: 
988                                 log_text("Invalid Constant - Type");
989                         }
990
991                 } else {
992                         /* unknown attribute */
993                         if (!skipattributebody(cb))
994                                 return false;
995                 }
996         }
997
998         /* everything was ok */
999
1000         return true;
1001 }
1002
1003
1004 /* load_method *****************************************************************
1005
1006    Loads a method from the class file and fills an existing
1007    'methodinfo' structure. For native methods, the function pointer
1008    field is set to the real function pointer, for JavaVM methods a
1009    pointer to the compiler is used preliminarily.
1010         
1011 *******************************************************************************/
1012
1013 static bool load_method(classbuffer *cb, methodinfo *m, descriptor_pool *descpool)
1014 {
1015         classinfo *c;
1016         int argcount;
1017         s4 i, j;
1018         u4 attrnum;
1019         u4 codeattrnum;
1020         utf *u;
1021
1022         c = cb->class;
1023
1024 #if defined(ENABLE_THREADS)
1025         lock_init_object_lock(&m->header);
1026 #endif
1027
1028 #if defined(ENABLE_STATISTICS)
1029         if (opt_stat)
1030                 count_all_methods++;
1031 #endif
1032
1033         /* all fields of m have been zeroed in load_class_from_classbuffer */
1034
1035         m->class                 = c;
1036         
1037         if (!suck_check_classbuffer_size(cb, 2 + 2 + 2))
1038                 return false;
1039
1040         m->flags = suck_u2(cb);
1041
1042         if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
1043                 return false;
1044
1045         m->name = u;
1046
1047         if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
1048                 return false;
1049
1050         m->descriptor = u;
1051
1052         if (!descriptor_pool_add(descpool, u, &argcount))
1053                 return false;
1054
1055 #ifdef ENABLE_VERIFIER
1056         if (opt_verify) {
1057                 if (!is_valid_name_utf(m->name)) {
1058                         exceptions_throw_classformaterror(c, "Method with invalid name");
1059                         return false;
1060                 }
1061
1062                 if (m->name->text[0] == '<' &&
1063                         m->name != utf_init && m->name != utf_clinit) {
1064                         exceptions_throw_classformaterror(c, "Method with invalid special name");
1065                         return false;
1066                 }
1067         }
1068 #endif /* ENABLE_VERIFIER */
1069         
1070         if (!(m->flags & ACC_STATIC))
1071                 argcount++; /* count the 'this' argument */
1072
1073 #ifdef ENABLE_VERIFIER
1074         if (opt_verify) {
1075                 if (argcount > 255) {
1076                         exceptions_throw_classformaterror(c, "Too many arguments in signature");
1077                         return false;
1078                 }
1079
1080                 /* check flag consistency */
1081                 if (m->name != utf_clinit) {
1082                         i = (m->flags & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED));
1083
1084                         if (i != 0 && i != ACC_PUBLIC && i != ACC_PRIVATE && i != ACC_PROTECTED) {
1085                                 exceptions_throw_classformaterror(c,
1086                                                                                                   "Illegal method modifiers: 0x%X",
1087                                                                                                   m->flags);
1088                                 return false;
1089                         }
1090
1091                         if (m->flags & ACC_ABSTRACT) {
1092                                 if ((m->flags & (ACC_FINAL | ACC_NATIVE | ACC_PRIVATE |
1093                                                                  ACC_STATIC | ACC_STRICT | ACC_SYNCHRONIZED))) {
1094                                         exceptions_throw_classformaterror(c,
1095                                                                                                           "Illegal method modifiers: 0x%X",
1096                                                                                                           m->flags);
1097                                         return false;
1098                                 }
1099                         }
1100
1101                         if (c->flags & ACC_INTERFACE) {
1102                                 if ((m->flags & (ACC_ABSTRACT | ACC_PUBLIC)) != (ACC_ABSTRACT | ACC_PUBLIC)) {
1103                                         exceptions_throw_classformaterror(c,
1104                                                                                                           "Illegal method modifiers: 0x%X",
1105                                                                                                           m->flags);
1106                                         return false;
1107                                 }
1108                         }
1109
1110                         if (m->name == utf_init) {
1111                                 if (m->flags & (ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
1112                                                                 ACC_NATIVE | ACC_ABSTRACT)) {
1113                                         exceptions_throw_classformaterror(c, "Instance initialization method has invalid flags set");
1114                                         return false;
1115                                 }
1116                         }
1117                 }
1118         }
1119 #endif /* ENABLE_VERIFIER */
1120                 
1121         if (!suck_check_classbuffer_size(cb, 2))
1122                 return false;
1123         
1124         attrnum = suck_u2(cb);
1125         for (i = 0; i < attrnum; i++) {
1126                 utf *aname;
1127
1128                 if (!suck_check_classbuffer_size(cb, 2))
1129                         return false;
1130
1131                 if (!(aname = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
1132                         return false;
1133
1134                 if (aname == utf_Code) {
1135                         if (m->flags & (ACC_ABSTRACT | ACC_NATIVE)) {
1136                                 exceptions_throw_classformaterror(c, "Code attribute in native or abstract methods");
1137                                 return false;
1138                         }
1139                         
1140                         if (m->jcode) {
1141                                 exceptions_throw_classformaterror(c, "Multiple Code attributes");
1142                                 return false;
1143                         }
1144
1145                         if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
1146                                 return false;
1147
1148                         suck_u4(cb);
1149                         m->maxstack = suck_u2(cb);
1150                         m->maxlocals = suck_u2(cb);
1151
1152                         if (m->maxlocals < argcount) {
1153                                 exceptions_throw_classformaterror(c, "Arguments can't fit into locals");
1154                                 return false;
1155                         }
1156                         
1157                         if (!suck_check_classbuffer_size(cb, 4))
1158                                 return false;
1159
1160                         m->jcodelength = suck_u4(cb);
1161
1162                         if (m->jcodelength == 0) {
1163                                 exceptions_throw_classformaterror(c, "Code of a method has length 0");
1164                                 return false;
1165                         }
1166                         
1167                         if (m->jcodelength > 65535) {
1168                                 exceptions_throw_classformaterror(c, "Code of a method longer than 65535 bytes");
1169                                 return false;
1170                         }
1171
1172                         if (!suck_check_classbuffer_size(cb, m->jcodelength))
1173                                 return false;
1174
1175                         m->jcode = MNEW(u1, m->jcodelength);
1176                         suck_nbytes(m->jcode, cb, m->jcodelength);
1177
1178                         if (!suck_check_classbuffer_size(cb, 2))
1179                                 return false;
1180
1181                         m->exceptiontablelength = suck_u2(cb);
1182                         if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * m->exceptiontablelength))
1183                                 return false;
1184
1185                         m->exceptiontable = MNEW(exceptiontable, m->exceptiontablelength);
1186
1187 #if defined(ENABLE_STATISTICS)
1188                         if (opt_stat) {
1189                                 count_vmcode_len += m->jcodelength + 18;
1190                                 count_extable_len +=
1191                                         m->exceptiontablelength * sizeof(exceptiontable);
1192                         }
1193 #endif
1194
1195                         for (j = 0; j < m->exceptiontablelength; j++) {
1196                                 u4 idx;
1197                                 m->exceptiontable[j].startpc = suck_u2(cb);
1198                                 m->exceptiontable[j].endpc = suck_u2(cb);
1199                                 m->exceptiontable[j].handlerpc = suck_u2(cb);
1200
1201                                 idx = suck_u2(cb);
1202                                 if (!idx) {
1203                                         m->exceptiontable[j].catchtype.any = NULL;
1204
1205                                 } else {
1206                                         /* the classref is created later */
1207                                         if (!(m->exceptiontable[j].catchtype.any =
1208                                                   (utf*)class_getconstant(c, idx, CONSTANT_Class)))
1209                                                 return false;
1210                                 }
1211                         }
1212
1213                         if (!suck_check_classbuffer_size(cb, 2))
1214                                 return false;
1215
1216                         codeattrnum = suck_u2(cb);
1217
1218                         for (; codeattrnum > 0; codeattrnum--) {
1219                                 utf *caname;
1220
1221                                 if (!suck_check_classbuffer_size(cb, 2))
1222                                         return false;
1223
1224                                 if (!(caname = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
1225                                         return false;
1226
1227                                 if (caname == utf_LineNumberTable) {
1228                                         u2 lncid;
1229
1230                                         if (!suck_check_classbuffer_size(cb, 4 + 2))
1231                                                 return false;
1232
1233                                         suck_u4(cb);
1234                                         m->linenumbercount = suck_u2(cb);
1235
1236                                         if (!suck_check_classbuffer_size(cb,
1237                                                                                                 (2 + 2) * m->linenumbercount))
1238                                                 return false;
1239
1240                                         m->linenumbers = MNEW(lineinfo, m->linenumbercount);
1241                                         
1242                                         for (lncid = 0; lncid < m->linenumbercount; lncid++) {
1243                                                 m->linenumbers[lncid].start_pc = suck_u2(cb);
1244                                                 m->linenumbers[lncid].line_number = suck_u2(cb);
1245                                         }
1246                                         codeattrnum--;
1247
1248                                         if (!skipattributes(cb, codeattrnum))
1249                                                 return false;
1250                                         
1251                                         break;
1252
1253                                 } else {
1254                                         if (!skipattributebody(cb))
1255                                                 return false;
1256                                 }
1257                         }
1258
1259                 } else if (aname == utf_Exceptions) {
1260                         s4 j;
1261
1262                         if (m->thrownexceptions) {
1263                                 exceptions_throw_classformaterror(c, "Multiple Exceptions attributes");
1264                                 return false;
1265                         }
1266
1267                         if (!suck_check_classbuffer_size(cb, 4 + 2))
1268                                 return false;
1269
1270                         suck_u4(cb); /* length */
1271                         m->thrownexceptionscount = suck_u2(cb);
1272
1273                         if (!suck_check_classbuffer_size(cb, 2 * m->thrownexceptionscount))
1274                                 return false;
1275
1276                         m->thrownexceptions = MNEW(classref_or_classinfo, m->thrownexceptionscount);
1277
1278                         for (j = 0; j < m->thrownexceptionscount; j++) {
1279                                 /* the classref is created later */
1280                                 if (!((m->thrownexceptions)[j].any =
1281                                           (utf*) class_getconstant(c, suck_u2(cb), CONSTANT_Class)))
1282                                         return false;
1283                         }
1284                                 
1285                 } else {
1286                         if (!skipattributebody(cb))
1287                                 return false;
1288                 }
1289         }
1290
1291         if (!m->jcode && !(m->flags & (ACC_ABSTRACT | ACC_NATIVE))) {
1292                 exceptions_throw_classformaterror(c, "Missing Code attribute");
1293                 return false;
1294         }
1295
1296         /* everything was ok */
1297
1298         return true;
1299 }
1300
1301
1302 /* load_attribute **************************************************************
1303
1304    Read attributes from classfile.
1305         
1306 *******************************************************************************/
1307
1308 static bool load_attributes(classbuffer *cb, u4 num)
1309 {
1310         classinfo *c;
1311         utf       *aname;
1312         u4 i, j;
1313
1314         c = cb->class;
1315
1316         for (i = 0; i < num; i++) {
1317                 /* retrieve attribute name */
1318                 if (!suck_check_classbuffer_size(cb, 2))
1319                         return false;
1320
1321                 if (!(aname = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
1322                         return false;
1323
1324                 if (aname == utf_InnerClasses) {
1325                         /* innerclasses attribute */
1326                         if (c->innerclass) {
1327                                 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
1328                                 return false;
1329                         }
1330                                 
1331                         if (!suck_check_classbuffer_size(cb, 4 + 2))
1332                                 return false;
1333
1334                         /* skip attribute length */
1335                         suck_u4(cb);
1336
1337                         /* number of records */
1338                         c->innerclasscount = suck_u2(cb);
1339
1340                         if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
1341                                 return false;
1342
1343                         /* allocate memory for innerclass structure */
1344                         c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
1345
1346                         for (j = 0; j < c->innerclasscount; j++) {
1347                                 /* The innerclass structure contains a class with an encoded
1348                                    name, its defining scope, its simple name and a bitmask of
1349                                    the access flags. If an inner class is not a member, its
1350                                    outer_class is NULL, if a class is anonymous, its name is
1351                                    NULL. */
1352                                                                 
1353                                 innerclassinfo *info = c->innerclass + j;
1354
1355                                 info->inner_class.ref =
1356                                         innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
1357                                 info->outer_class.ref =
1358                                         innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
1359                                 info->name =
1360                                         innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
1361                                 info->flags = suck_u2(cb);
1362                         }
1363
1364                 } else if (aname == utf_SourceFile) {
1365                         if (!suck_check_classbuffer_size(cb, 4 + 2))
1366                                 return false;
1367
1368                         if (suck_u4(cb) != 2) {
1369                                 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
1370                                 return false;
1371                         }
1372
1373                         if (c->sourcefile) {
1374                                 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
1375                                 return false;
1376                         }
1377
1378                         if (!(c->sourcefile = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
1379                                 return false;
1380
1381                 } else {
1382                         /* unknown attribute */
1383                         if (!skipattributebody(cb))
1384                                 return false;
1385                 }
1386         }
1387
1388         return true;
1389 }
1390
1391
1392 /* load_class_from_sysloader ***************************************************
1393
1394    Load the class with the given name using the system class loader
1395
1396    IN:
1397        name.............the classname
1398
1399    RETURN VALUE:
1400        the loaded class, or
1401            NULL if an exception has been thrown
1402
1403 *******************************************************************************/
1404
1405 classinfo *load_class_from_sysloader(utf *name)
1406 {
1407         methodinfo        *m;
1408         java_objectheader *cl;
1409         classinfo         *c;
1410
1411         LOADER_ASSERT(class_java_lang_Object);
1412         LOADER_ASSERT(class_java_lang_ClassLoader);
1413         LOADER_ASSERT(class_java_lang_ClassLoader->state & CLASS_LINKED);
1414         
1415         m = class_resolveclassmethod(class_java_lang_ClassLoader,
1416                                                                  utf_getSystemClassLoader,
1417                                                                  utf_void__java_lang_ClassLoader,
1418                                                                  class_java_lang_Object,
1419                                                                  false);
1420
1421         if (!m)
1422                 return false;
1423
1424         cl = vm_call_method(m, NULL);
1425
1426         if (!cl)
1427                 return false;
1428
1429         c = load_class_from_classloader(name, cl);
1430
1431         return c;
1432 }
1433
1434
1435 /* load_class_from_classloader *************************************************
1436
1437    Load the class with the given name using the given user-defined class loader.
1438
1439    IN:
1440        name.............the classname
1441            cl...............user-defined class loader
1442            
1443    RETURN VALUE:
1444        the loaded class, or
1445            NULL if an exception has been thrown
1446
1447 *******************************************************************************/
1448
1449 classinfo *load_class_from_classloader(utf *name, java_objectheader *cl)
1450 {
1451         java_objectheader *o;
1452         classinfo         *c;
1453         classinfo         *tmpc;
1454         java_lang_String  *s;
1455 #if defined(ENABLE_RT_TIMING)
1456         struct timespec time_start, time_lookup, time_prepare, time_java, 
1457                                         time_cache;
1458 #endif
1459
1460         RT_TIMING_GET_TIME(time_start);
1461
1462         LOADER_ASSERT(name);
1463
1464         /* lookup if this class has already been loaded */
1465
1466         c = classcache_lookup(cl, name);
1467
1468         RT_TIMING_GET_TIME(time_lookup);
1469         RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_CL_LOOKUP);
1470
1471         if (c)
1472                 return c;
1473
1474         /* if other class loader than bootstrap, call it */
1475
1476         if (cl) {
1477                 methodinfo *lc;
1478                 char       *text;
1479                 s4          namelen;
1480
1481                 text = name->text;
1482                 namelen = name->blength;
1483
1484                 /* handle array classes */
1485                 if (text[0] == '[') {
1486                         classinfo *comp;
1487                         utf       *u;
1488
1489                         switch (text[1]) {
1490                         case 'L':
1491                                 /* check for cases like `[L;' or `[L[I;' or `[Ljava.lang.Object' */
1492                                 if (namelen < 4 || text[2] == '[' || text[namelen - 1] != ';') {
1493                                         *exceptionptr = new_noclassdeffounderror(name);
1494                                         return false;
1495                                 }
1496
1497                                 u = utf_new(text + 2, namelen - 3);
1498
1499                                 if (!(comp = load_class_from_classloader(u, cl)))
1500                                         return false;
1501
1502                                 /* create the array class */
1503
1504                                 c = class_array_of(comp, false);
1505
1506                                 tmpc = classcache_store(cl, c, true);
1507
1508                                 if (tmpc == NULL) {
1509                                         /* exception, free the loaded class */
1510                                         c->state &= ~CLASS_LOADING;
1511                                         class_free(c);
1512                                 }
1513
1514                                 return tmpc;
1515
1516                         case '[':
1517                                 /* load the component class */
1518
1519                                 u = utf_new(text + 1, namelen - 1);
1520
1521                                 if (!(comp = load_class_from_classloader(u, cl)))
1522                                         return false;
1523
1524                                 /* create the array class */
1525
1526                                 c = class_array_of(comp, false);
1527
1528                                 tmpc = classcache_store(cl, c, true);
1529
1530                                 if (tmpc == NULL) {
1531                                         /* exception, free the loaded class */
1532                                         c->state &= ~CLASS_LOADING;
1533                                         class_free(c);
1534                                 }
1535
1536                                 return tmpc;
1537
1538                         default:
1539                                 /* primitive array classes are loaded by the bootstrap loader */
1540
1541                                 c = load_class_bootstrap(name);
1542
1543                                 return c;
1544                         }
1545                 }
1546                 
1547                 LOADER_ASSERT(class_java_lang_Object);
1548
1549                 lc = class_resolveclassmethod(cl->vftbl->class,
1550                                                                           utf_loadClass,
1551                                                                           utf_java_lang_String__java_lang_Class,
1552                                                                           class_java_lang_Object,
1553                                                                           true);
1554
1555                 if (!lc)
1556                         return false; /* exception */
1557
1558                 /* move return value into `o' and cast it afterwards to a classinfo* */
1559
1560                 s = javastring_new_slash_to_dot(name);
1561
1562                 RT_TIMING_GET_TIME(time_prepare);
1563
1564                 o = vm_call_method(lc, cl, s);
1565
1566                 RT_TIMING_GET_TIME(time_java);
1567
1568                 c = (classinfo *) o;
1569
1570                 if (c) {
1571                         /* Store this class in the loaded class cache. If another
1572                            class with the same (initloader,name) pair has been
1573                            stored earlier it will be returned by classcache_store
1574                            In this case classcache_store may not free the class
1575                            because it has already been exposed to Java code which
1576                            may have kept references to that class. */
1577
1578                     tmpc = classcache_store(cl, c, false);
1579
1580                         if (tmpc == NULL) {
1581                                 /* exception, free the loaded class */
1582                                 c->state &= ~CLASS_LOADING;
1583                                 class_free(c);
1584                         }
1585
1586                         c = tmpc;
1587
1588                 } else {
1589                         /* loadClass has thrown an exception.  We must convert
1590                            ClassNotFoundException into
1591                            NoClassDefFoundException. */
1592
1593                         /* XXX Maybe we should have a flag that avoids this
1594                            conversion for calling load_class_from_classloader from
1595                            Class.forName.  Currently we do a double conversion in
1596                            these cases.  */
1597
1598                         classnotfoundexception_to_noclassdeffounderror();
1599                 }
1600
1601                 RT_TIMING_GET_TIME(time_cache);
1602
1603                 RT_TIMING_TIME_DIFF(time_lookup , time_prepare, RT_TIMING_LOAD_CL_PREPARE);
1604                 RT_TIMING_TIME_DIFF(time_prepare, time_java   , RT_TIMING_LOAD_CL_JAVA);
1605                 RT_TIMING_TIME_DIFF(time_java   , time_cache  , RT_TIMING_LOAD_CL_CACHE);
1606
1607                 /* SUN compatible -verbose:class output */
1608
1609                 if (opt_verboseclass && (c != NULL) && (c->classloader == cl)) {
1610                         printf("[Loaded ");
1611                         utf_display_printable_ascii_classname(name);
1612                         printf("]\n");
1613                 }
1614
1615 #if defined(ENABLE_JVMTI)
1616                 /* fire Class Load JVMTI event */
1617                 if (jvmti) jvmti_ClassLoadPrepare(false, c);
1618 #endif
1619
1620
1621                 return c;
1622         } 
1623
1624         c = load_class_bootstrap(name);
1625
1626         return c;
1627 }
1628
1629
1630 /* load_class_bootstrap ********************************************************
1631         
1632    Load the class with the given name using the bootstrap class loader.
1633
1634    IN:
1635        name.............the classname
1636
1637    RETURN VALUE:
1638        loaded classinfo, or
1639            NULL if an exception has been thrown
1640
1641    SYNCHRONIZATION:
1642        load_class_bootstrap is synchronized. It can be treated as an
1643            atomic operation.
1644
1645 *******************************************************************************/
1646
1647 classinfo *load_class_bootstrap(utf *name)
1648 {
1649         classbuffer *cb;
1650         classinfo   *c;
1651         classinfo   *r;
1652 #if defined(ENABLE_RT_TIMING)
1653         struct timespec time_start, time_lookup, time_array, time_suck, 
1654                                         time_load, time_cache;
1655 #endif
1656
1657         RT_TIMING_GET_TIME(time_start);
1658
1659         /* for debugging */
1660
1661         LOADER_ASSERT(name);
1662
1663         /* lookup if this class has already been loaded */
1664
1665         if ((r = classcache_lookup(NULL, name))) {
1666
1667                 RT_TIMING_GET_TIME(time_lookup);
1668                 RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_BOOT_LOOKUP);
1669                 
1670                 return r;
1671         }
1672
1673         RT_TIMING_GET_TIME(time_lookup);
1674         RT_TIMING_TIME_DIFF(time_start,time_lookup,RT_TIMING_LOAD_BOOT_LOOKUP);
1675                 
1676         /* create the classinfo */
1677
1678         c = class_create_classinfo(name);
1679
1680         /* handle array classes */
1681
1682         if (name->text[0] == '[') {
1683                 c = load_newly_created_array(c, NULL);
1684                 if (c == NULL)
1685                         return NULL;
1686                 LOADER_ASSERT(c->state & CLASS_LOADED);
1687
1688                 RT_TIMING_GET_TIME(time_array);
1689                 RT_TIMING_TIME_DIFF(time_start,time_array,RT_TIMING_LOAD_BOOT_ARRAY);
1690                 
1691                 return c;
1692         }
1693
1694 #if defined(ENABLE_STATISTICS)
1695         /* measure time */
1696
1697         if (opt_getcompilingtime)
1698                 compilingtime_stop();
1699
1700         if (opt_getloadingtime)
1701                 loadingtime_start();
1702 #endif
1703
1704         /* load classdata, throw exception on error */
1705
1706         if ((cb = suck_start(c)) == NULL) {
1707                 /* this normally means, the classpath was not set properly */
1708
1709                 if (name == utf_java_lang_Object)
1710                         throw_cacao_exception_exit(string_java_lang_NoClassDefFoundError,
1711                                                                            "java/lang/Object");
1712
1713                 *exceptionptr = new_noclassdeffounderror(name);
1714
1715                 return NULL;
1716         }
1717
1718         RT_TIMING_GET_TIME(time_suck);
1719         
1720         /* load the class from the buffer */
1721
1722         r = load_class_from_classbuffer(cb);
1723
1724         RT_TIMING_GET_TIME(time_load);
1725         
1726         if (!r) {
1727                 /* the class could not be loaded, free the classinfo struct */
1728
1729                 class_free(c);
1730
1731         } else {
1732                 /* Store this class in the loaded class cache this step also
1733                 checks the loading constraints. If the class has been loaded
1734                 before, the earlier loaded class is returned. */
1735
1736                 classinfo *res = classcache_store(NULL, c, true);
1737
1738                 if (!res) {
1739                         /* exception */
1740                         class_free(c);
1741                 }
1742
1743                 r = res;
1744         }
1745
1746         RT_TIMING_GET_TIME(time_cache);
1747         
1748         /* SUN compatible -verbose:class output */
1749
1750         if (opt_verboseclass && r) {
1751                 printf("[Loaded ");
1752                 utf_display_printable_ascii_classname(name);
1753                 printf(" from %s]\n", cb->path);
1754         }
1755
1756         /* free memory */
1757
1758         suck_stop(cb);
1759
1760 #if defined(ENABLE_STATISTICS)
1761         /* measure time */
1762
1763         if (opt_getloadingtime)
1764                 loadingtime_stop();
1765
1766         if (opt_getcompilingtime)
1767                 compilingtime_start();
1768 #endif
1769
1770         RT_TIMING_TIME_DIFF(time_lookup, time_suck , RT_TIMING_LOAD_BOOT_SUCK);
1771         RT_TIMING_TIME_DIFF(time_suck  , time_load , RT_TIMING_LOAD_BOOT_LOAD);
1772         RT_TIMING_TIME_DIFF(time_load  , time_cache, RT_TIMING_LOAD_BOOT_CACHE);
1773         RT_TIMING_TIME_DIFF(time_lookup, time_cache, RT_TIMING_LOAD_BOOT_TOTAL);
1774
1775         return r;
1776 }
1777
1778
1779 /* load_class_from_classbuffer *************************************************
1780         
1781    Loads everything interesting about a class from the class file. The
1782    'classinfo' structure must have been allocated previously.
1783
1784    The super class and the interfaces implemented by this class need
1785    not be loaded. The link is set later by the function 'class_link'.
1786
1787    The loaded class is removed from the list 'unloadedclasses' and
1788    added to the list 'unlinkedclasses'.
1789         
1790    SYNCHRONIZATION:
1791        This function is NOT synchronized!
1792    
1793 *******************************************************************************/
1794
1795 classinfo *load_class_from_classbuffer(classbuffer *cb)
1796 {
1797         classinfo *c;
1798         utf *name;
1799         utf *supername;
1800         u4 i,j;
1801         u4 ma, mi;
1802         s4 dumpsize;
1803         descriptor_pool *descpool;
1804 #if defined(ENABLE_STATISTICS)
1805         u4 classrefsize;
1806         u4 descsize;
1807 #endif
1808 #if defined(ENABLE_RT_TIMING)
1809         struct timespec time_start, time_checks, time_ndpool, time_cpool,
1810                                         time_setup, time_fields, time_methods, time_classrefs,
1811                                         time_descs,     time_setrefs, time_parsefds, time_parsemds,
1812                                         time_parsecpool, time_verify, time_attrs;
1813 #endif
1814
1815         RT_TIMING_GET_TIME(time_start);
1816
1817         /* get the classbuffer's class */
1818
1819         c = cb->class;
1820
1821         /* the class is already loaded */
1822
1823         if (c->state & CLASS_LOADED)
1824                 return c;
1825
1826 #if defined(ENABLE_STATISTICS)
1827         if (opt_stat)
1828                 count_class_loads++;
1829 #endif
1830
1831 #if !defined(NDEBUG)
1832         /* output for debugging purposes */
1833
1834         if (loadverbose)
1835                 log_message_class("Loading class: ", c);
1836 #endif
1837
1838         /* mark start of dump memory area */
1839
1840         dumpsize = dump_size();
1841
1842         /* class is currently loading */
1843
1844         c->state |= CLASS_LOADING;
1845
1846         if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
1847                 goto return_exception;
1848
1849         /* check signature */
1850
1851         if (suck_u4(cb) != MAGIC) {
1852                 exceptions_throw_classformaterror(c, "Bad magic number");
1853
1854                 goto return_exception;
1855         }
1856
1857         /* check version */
1858
1859         mi = suck_u2(cb);
1860         ma = suck_u2(cb);
1861
1862         if (!(ma < MAJOR_VERSION || (ma == MAJOR_VERSION && mi <= MINOR_VERSION))) {
1863                 *exceptionptr =
1864                         new_unsupportedclassversionerror(c,
1865                                                                                          "Unsupported major.minor version %d.%d",
1866                                                                                          ma, mi);
1867
1868                 goto return_exception;
1869         }
1870         RT_TIMING_GET_TIME(time_checks);
1871
1872         /* create a new descriptor pool */
1873
1874         descpool = descriptor_pool_new(c);
1875
1876         RT_TIMING_GET_TIME(time_ndpool);
1877
1878         /* load the constant pool */
1879
1880         if (!load_constantpool(cb, descpool))
1881                 goto return_exception;
1882
1883         RT_TIMING_GET_TIME(time_cpool);
1884
1885         /* ACC flags */
1886
1887         if (!suck_check_classbuffer_size(cb, 2))
1888                 goto return_exception;
1889
1890         c->flags = suck_u2(cb);
1891
1892         /* check ACC flags consistency */
1893
1894         if (c->flags & ACC_INTERFACE) {
1895                 if (!(c->flags & ACC_ABSTRACT)) {
1896                         /* We work around this because interfaces in JDK 1.1 are
1897                          * not declared abstract. */
1898
1899                         c->flags |= ACC_ABSTRACT;
1900                 }
1901
1902                 if (c->flags & ACC_FINAL) {
1903                         exceptions_throw_classformaterror(c,
1904                                                                                           "Illegal class modifiers: 0x%X",
1905                                                                                           c->flags);
1906                         goto return_exception;
1907                 }
1908
1909                 if (c->flags & ACC_SUPER) {
1910                         c->flags &= ~ACC_SUPER; /* kjc seems to set this on interfaces */
1911                 }
1912         }
1913
1914         if ((c->flags & (ACC_ABSTRACT | ACC_FINAL)) == (ACC_ABSTRACT | ACC_FINAL)) {
1915                 exceptions_throw_classformaterror(c,
1916                                                                                   "Illegal class modifiers: 0x%X",
1917                                                                                   c->flags);
1918                 goto return_exception;
1919         }
1920
1921         if (!suck_check_classbuffer_size(cb, 2 + 2))
1922                 goto return_exception;
1923
1924         /* this class */
1925
1926         i = suck_u2(cb);
1927         if (!(name = (utf *) class_getconstant(c, i, CONSTANT_Class)))
1928                 goto return_exception;
1929
1930         if (c->name == utf_not_named_yet) {
1931                 /* we finally have a name for this class */
1932                 c->name = name;
1933                 class_set_packagename(c);
1934
1935         } else if (name != c->name) {
1936                 char *msg;
1937                 s4    msglen;
1938
1939                 msglen = utf_bytes(c->name) + strlen(" (wrong name: ") +
1940                         utf_bytes(name) + strlen(")") + strlen("0");
1941
1942                 msg = MNEW(char, msglen);
1943
1944                 utf_copy_classname(msg, c->name);
1945                 strcat(msg, " (wrong name: ");
1946                 utf_cat_classname(msg, name);
1947                 strcat(msg, ")");
1948
1949                 *exceptionptr =
1950                         new_exception_message(string_java_lang_NoClassDefFoundError, msg);
1951
1952                 MFREE(msg, char, msglen);
1953
1954                 goto return_exception;
1955         }
1956
1957         /* retrieve superclass */
1958
1959         c->super.any = NULL;
1960         if ((i = suck_u2(cb))) {
1961                 if (!(supername = (utf *) class_getconstant(c, i, CONSTANT_Class)))
1962                         goto return_exception;
1963
1964                 /* java.lang.Object may not have a super class. */
1965
1966                 if (c->name == utf_java_lang_Object) {
1967                         *exceptionptr =
1968                                 new_exception_message(string_java_lang_ClassFormatError,
1969                                                                           "java.lang.Object with superclass");
1970
1971                         goto return_exception;
1972                 }
1973
1974                 /* Interfaces must have java.lang.Object as super class. */
1975
1976                 if ((c->flags & ACC_INTERFACE) &&
1977                         supername != utf_java_lang_Object) {
1978                         *exceptionptr =
1979                                 new_exception_message(string_java_lang_ClassFormatError,
1980                                                                           "Interfaces must have java.lang.Object as superclass");
1981
1982                         goto return_exception;
1983                 }
1984
1985         } else {
1986                 supername = NULL;
1987
1988                 /* This is only allowed for java.lang.Object. */
1989
1990                 if (c->name != utf_java_lang_Object) {
1991                         exceptions_throw_classformaterror(c, "Bad superclass index");
1992                         goto return_exception;
1993                 }
1994         }
1995
1996         /* retrieve interfaces */
1997
1998         if (!suck_check_classbuffer_size(cb, 2))
1999                 goto return_exception;
2000
2001         c->interfacescount = suck_u2(cb);
2002
2003         if (!suck_check_classbuffer_size(cb, 2 * c->interfacescount))
2004                 goto return_exception;
2005
2006         c->interfaces = MNEW(classref_or_classinfo, c->interfacescount);
2007         for (i = 0; i < c->interfacescount; i++) {
2008                 /* the classrefs are created later */
2009                 if (!(c->interfaces[i].any = (utf *) class_getconstant(c, suck_u2(cb), CONSTANT_Class)))
2010                         goto return_exception;
2011         }
2012
2013         RT_TIMING_GET_TIME(time_setup);
2014
2015         /* load fields */
2016         if (!suck_check_classbuffer_size(cb, 2))
2017                 goto return_exception;
2018
2019         c->fieldscount = suck_u2(cb);
2020         c->fields = GCNEW_UNCOLLECTABLE(fieldinfo, c->fieldscount);
2021 /*      c->fields = MNEW(fieldinfo, c->fieldscount); */
2022         for (i = 0; i < c->fieldscount; i++) {
2023                 if (!load_field(cb, &(c->fields[i]),descpool))
2024                         goto return_exception;
2025         }
2026
2027         RT_TIMING_GET_TIME(time_fields);
2028
2029         /* load methods */
2030         if (!suck_check_classbuffer_size(cb, 2))
2031                 goto return_exception;
2032
2033         c->methodscount = suck_u2(cb);
2034 /*      c->methods = GCNEW(methodinfo, c->methodscount); */
2035         c->methods = MNEW(methodinfo, c->methodscount);
2036
2037         MZERO(c->methods, methodinfo, c->methodscount);
2038         
2039         for (i = 0; i < c->methodscount; i++) {
2040                 if (!load_method(cb, &(c->methods[i]),descpool))
2041                         goto return_exception;
2042         }
2043
2044         RT_TIMING_GET_TIME(time_methods);
2045
2046         /* create the class reference table */
2047
2048         c->classrefs =
2049                 descriptor_pool_create_classrefs(descpool, &(c->classrefcount));
2050
2051         RT_TIMING_GET_TIME(time_classrefs);
2052
2053         /* allocate space for the parsed descriptors */
2054
2055         descriptor_pool_alloc_parsed_descriptors(descpool);
2056         c->parseddescs =
2057                 descriptor_pool_get_parsed_descriptors(descpool, &(c->parseddescsize));
2058
2059 #if defined(ENABLE_STATISTICS)
2060         if (opt_stat) {
2061                 descriptor_pool_get_sizes(descpool, &classrefsize, &descsize);
2062                 count_classref_len += classrefsize;
2063                 count_parsed_desc_len += descsize;
2064         }
2065 #endif
2066
2067         RT_TIMING_GET_TIME(time_descs);
2068
2069         /* put the classrefs in the constant pool */
2070         for (i = 0; i < c->cpcount; i++) {
2071                 if (c->cptags[i] == CONSTANT_Class) {
2072                         utf *name = (utf *) c->cpinfos[i];
2073                         c->cpinfos[i] = descriptor_pool_lookup_classref(descpool, name);
2074                 }
2075         }
2076
2077         /* set the super class reference */
2078
2079         if (supername) {
2080                 c->super.ref = descriptor_pool_lookup_classref(descpool, supername);
2081                 if (!c->super.ref)
2082                         goto return_exception;
2083         }
2084
2085         /* set the super interfaces references */
2086
2087         for (i = 0; i < c->interfacescount; i++) {
2088                 c->interfaces[i].ref =
2089                         descriptor_pool_lookup_classref(descpool,
2090                                                                                         (utf *) c->interfaces[i].any);
2091                 if (!c->interfaces[i].ref)
2092                         goto return_exception;
2093         }
2094
2095         RT_TIMING_GET_TIME(time_setrefs);
2096
2097         /* parse field descriptors */
2098
2099         for (i = 0; i < c->fieldscount; i++) {
2100                 c->fields[i].parseddesc =
2101                         descriptor_pool_parse_field_descriptor(descpool,
2102                                                                                                    c->fields[i].descriptor);
2103                 if (!c->fields[i].parseddesc)
2104                         goto return_exception;
2105         }
2106
2107         RT_TIMING_GET_TIME(time_parsefds);
2108
2109         /* parse method descriptors */
2110
2111         for (i = 0; i < c->methodscount; i++) {
2112                 methodinfo *m = &c->methods[i];
2113                 m->parseddesc =
2114                         descriptor_pool_parse_method_descriptor(descpool, m->descriptor,
2115                                                                                                         m->flags, class_get_self_classref(m->class));
2116                 if (!m->parseddesc)
2117                         goto return_exception;
2118
2119                 for (j = 0; j < m->exceptiontablelength; j++) {
2120                         if (!m->exceptiontable[j].catchtype.any)
2121                                 continue;
2122                         if ((m->exceptiontable[j].catchtype.ref =
2123                                  descriptor_pool_lookup_classref(descpool,
2124                                                 (utf *) m->exceptiontable[j].catchtype.any)) == NULL)
2125                                 goto return_exception;
2126                 }
2127
2128                 for (j = 0; j < m->thrownexceptionscount; j++) {
2129                         if (!m->thrownexceptions[j].any)
2130                                 continue;
2131                         if ((m->thrownexceptions[j].ref = descriptor_pool_lookup_classref(descpool,
2132                                                 (utf *) m->thrownexceptions[j].any)) == NULL)
2133                                 goto return_exception;
2134                 }
2135         }
2136
2137         RT_TIMING_GET_TIME(time_parsemds);
2138
2139         /* parse the loaded descriptors */
2140
2141         for (i = 0; i < c->cpcount; i++) {
2142                 constant_FMIref *fmi;
2143                 s4               index;
2144
2145                 switch (c->cptags[i]) {
2146                 case CONSTANT_Fieldref:
2147                         fmi = (constant_FMIref *) c->cpinfos[i];
2148                         fmi->parseddesc.fd =
2149                                 descriptor_pool_parse_field_descriptor(descpool,
2150                                                                                                            fmi->descriptor);
2151                         if (!fmi->parseddesc.fd)
2152                                 goto return_exception;
2153                         index = fmi->p.index;
2154                         fmi->p.classref =
2155                                 (constant_classref *) class_getconstant(c, index,
2156                                                                                                                 CONSTANT_Class);
2157                         if (!fmi->p.classref)
2158                                 goto return_exception;
2159                         break;
2160                 case CONSTANT_Methodref:
2161                 case CONSTANT_InterfaceMethodref:
2162                         fmi = (constant_FMIref *) c->cpinfos[i];
2163                         index = fmi->p.index;
2164                         fmi->p.classref =
2165                                 (constant_classref *) class_getconstant(c, index,
2166                                                                                                                 CONSTANT_Class);
2167                         if (!fmi->p.classref)
2168                                 goto return_exception;
2169                         fmi->parseddesc.md =
2170                                 descriptor_pool_parse_method_descriptor(descpool,
2171                                                                                                                 fmi->descriptor,
2172                                                                                                                 ACC_UNDEF,
2173                                                                                                                 fmi->p.classref);
2174                         if (!fmi->parseddesc.md)
2175                                 goto return_exception;
2176                         break;
2177                 }
2178         }
2179
2180         RT_TIMING_GET_TIME(time_parsecpool);
2181
2182 #ifdef ENABLE_VERIFIER
2183         /* Check if all fields and methods can be uniquely
2184          * identified by (name,descriptor). */
2185
2186         if (opt_verify) {
2187                 /* We use a hash table here to avoid making the
2188                  * average case quadratic in # of methods, fields.
2189                  */
2190                 static int shift = 0;
2191                 u2 *hashtab;
2192                 u2 *next; /* for chaining colliding hash entries */
2193                 size_t len;
2194                 size_t hashlen;
2195                 u2 index;
2196                 u2 old;
2197
2198                 /* Allocate hashtable */
2199                 len = c->methodscount;
2200                 if (len < c->fieldscount) len = c->fieldscount;
2201                 hashlen = 5 * len;
2202                 hashtab = MNEW(u2,(hashlen + len));
2203                 next = hashtab + hashlen;
2204
2205                 /* Determine bitshift (to get good hash values) */
2206                 if (!shift) {
2207                         len = sizeof(utf);
2208                         while (len) {
2209                                 len >>= 1;
2210                                 shift++;
2211                         }
2212                 }
2213
2214                 /* Check fields */
2215                 memset(hashtab, 0, sizeof(u2) * (hashlen + len));
2216
2217                 for (i = 0; i < c->fieldscount; ++i) {
2218                         fieldinfo *fi = c->fields + i;
2219
2220                         /* It's ok if we lose bits here */
2221                         index = ((((size_t) fi->name) +
2222                                           ((size_t) fi->descriptor)) >> shift) % hashlen;
2223
2224                         if ((old = hashtab[index])) {
2225                                 old--;
2226                                 next[i] = old;
2227                                 do {
2228                                         if (c->fields[old].name == fi->name &&
2229                                                 c->fields[old].descriptor == fi->descriptor) {
2230                                                 exceptions_throw_classformaterror(c, "Repetitive field name/signature");
2231                                                 goto return_exception;
2232                                         }
2233                                 } while ((old = next[old]));
2234                         }
2235                         hashtab[index] = i + 1;
2236                 }
2237
2238                 /* Check methods */
2239                 memset(hashtab, 0, sizeof(u2) * (hashlen + hashlen/5));
2240
2241                 for (i = 0; i < c->methodscount; ++i) {
2242                         methodinfo *mi = c->methods + i;
2243
2244                         /* It's ok if we lose bits here */
2245                         index = ((((size_t) mi->name) +
2246                                           ((size_t) mi->descriptor)) >> shift) % hashlen;
2247
2248                         /*{ JOWENN
2249                                 int dbg;
2250                                 for (dbg=0;dbg<hashlen+hashlen/5;++dbg){
2251                                         printf("Hash[%d]:%d\n",dbg,hashtab[dbg]);
2252                                 }
2253                         }*/
2254
2255                         if ((old = hashtab[index])) {
2256                                 old--;
2257                                 next[i] = old;
2258                                 do {
2259                                         if (c->methods[old].name == mi->name &&
2260                                                 c->methods[old].descriptor == mi->descriptor) {
2261                                                 exceptions_throw_classformaterror(c, "Repetitive method name/signature");
2262                                                 goto return_exception;
2263                                         }
2264                                 } while ((old = next[old]));
2265                         }
2266                         hashtab[index] = i + 1;
2267                 }
2268
2269                 MFREE(hashtab, u2, (hashlen + len));
2270         }
2271 #endif /* ENABLE_VERIFIER */
2272
2273         RT_TIMING_GET_TIME(time_verify);
2274
2275 #if defined(ENABLE_STATISTICS)
2276         if (opt_stat) {
2277                 size_classinfo  += sizeof(classinfo*) * c->interfacescount;
2278                 size_fieldinfo  += sizeof(fieldinfo)  * c->fieldscount;
2279                 size_methodinfo += sizeof(methodinfo) * c->methodscount;
2280         }
2281 #endif
2282
2283         /* load attribute structures */
2284
2285         if (!suck_check_classbuffer_size(cb, 2))
2286                 goto return_exception;
2287
2288         if (!load_attributes(cb, suck_u2(cb)))
2289                 goto return_exception;
2290
2291         /* Pre Java 1.5 version don't check this. This implementation is like
2292            Java 1.5 do it: for class file version 45.3 we don't check it, older
2293            versions are checked.
2294          */
2295
2296         if (((ma == 45) && (mi > 3)) || (ma > 45)) {
2297                 /* check if all data has been read */
2298                 s4 classdata_left = ((cb->data + cb->size) - cb->pos);
2299
2300                 if (classdata_left > 0) {
2301                         exceptions_throw_classformaterror(c, "Extra bytes at the end of class file");
2302                         goto return_exception;
2303                 }
2304         }
2305
2306         RT_TIMING_GET_TIME(time_attrs);
2307
2308         /* release dump area */
2309
2310         dump_release(dumpsize);
2311
2312         /* revert loading state and class is loaded */
2313
2314         c->state = (c->state & ~CLASS_LOADING) | CLASS_LOADED;
2315
2316 #if defined(ENABLE_JVMTI)
2317         /* fire Class Prepare JVMTI event */
2318
2319         if (jvmti)
2320                 jvmti_ClassLoadPrepare(true, c);
2321 #endif
2322
2323 #if !defined(NDEBUG)
2324         if (loadverbose)
2325                 log_message_class("Loading done class: ", c);
2326 #endif
2327
2328         RT_TIMING_TIME_DIFF(time_start     , time_checks    , RT_TIMING_LOAD_CHECKS);
2329         RT_TIMING_TIME_DIFF(time_checks    , time_ndpool    , RT_TIMING_LOAD_NDPOOL);
2330         RT_TIMING_TIME_DIFF(time_ndpool    , time_cpool     , RT_TIMING_LOAD_CPOOL);
2331         RT_TIMING_TIME_DIFF(time_cpool     , time_setup     , RT_TIMING_LOAD_SETUP);
2332         RT_TIMING_TIME_DIFF(time_setup     , time_fields    , RT_TIMING_LOAD_FIELDS);
2333         RT_TIMING_TIME_DIFF(time_fields    , time_methods   , RT_TIMING_LOAD_METHODS);
2334         RT_TIMING_TIME_DIFF(time_methods   , time_classrefs , RT_TIMING_LOAD_CLASSREFS);
2335         RT_TIMING_TIME_DIFF(time_classrefs , time_descs     , RT_TIMING_LOAD_DESCS);
2336         RT_TIMING_TIME_DIFF(time_descs     , time_setrefs   , RT_TIMING_LOAD_SETREFS);
2337         RT_TIMING_TIME_DIFF(time_setrefs   , time_parsefds  , RT_TIMING_LOAD_PARSEFDS);
2338         RT_TIMING_TIME_DIFF(time_parsefds  , time_parsemds  , RT_TIMING_LOAD_PARSEMDS);
2339         RT_TIMING_TIME_DIFF(time_parsemds  , time_parsecpool, RT_TIMING_LOAD_PARSECP);
2340         RT_TIMING_TIME_DIFF(time_parsecpool, time_verify    , RT_TIMING_LOAD_VERIFY);
2341         RT_TIMING_TIME_DIFF(time_verify    , time_attrs     , RT_TIMING_LOAD_ATTRS);
2342         RT_TIMING_TIME_DIFF(time_start     , time_attrs     , RT_TIMING_LOAD_TOTAL);
2343
2344         return c;
2345
2346 return_exception:
2347         /* release dump area */
2348
2349         dump_release(dumpsize);
2350
2351         /* an exception has been thrown */
2352
2353         return NULL;
2354 }
2355
2356
2357 /* load_newly_created_array ****************************************************
2358
2359    Load a newly created array class.
2360
2361         RETURN VALUE:
2362             c....................the array class C has been loaded
2363                 other classinfo......the array class was found in the class cache, 
2364                                      C has been freed
2365             NULL.................an exception has been thrown
2366
2367         Note:
2368                 This is an internal function. Do not use it unless you know exactly
2369                 what you are doing!
2370
2371                 Use one of the load_class_... functions for general array class loading.
2372
2373 *******************************************************************************/
2374
2375 classinfo *load_newly_created_array(classinfo *c, java_objectheader *loader)
2376 {
2377         classinfo         *comp = NULL;
2378         methodinfo        *clone;
2379         methoddesc        *clonedesc;
2380         constant_classref *classrefs;
2381         char              *text;
2382         s4                 namelen;
2383         utf               *u;
2384
2385         text = c->name->text;
2386         namelen = c->name->blength;
2387
2388         /* Check array class name */
2389
2390         if (namelen < 2 || text[0] != '[') {
2391                 *exceptionptr = new_noclassdeffounderror(c->name);
2392                 return NULL;
2393         }
2394
2395         /* Check the element type */
2396
2397         switch (text[1]) {
2398         case '[':
2399                 /* c is an array of arrays. We have to create the component class. */
2400
2401                 u = utf_new(text + 1, namelen - 1);
2402                 if (!(comp = load_class_from_classloader(u, loader)))
2403                         return NULL;
2404
2405                 LOADER_ASSERT(comp->state & CLASS_LOADED);
2406
2407                 if (opt_eager)
2408                         if (!link_class(c))
2409                                 return NULL;
2410
2411                 /* the array's flags are that of the component class */
2412                 c->flags = (comp->flags & ~ACC_INTERFACE) | ACC_FINAL | ACC_ABSTRACT;
2413                 c->classloader = comp->classloader;
2414                 break;
2415
2416         case 'L':
2417                 /* c is an array of objects. */
2418
2419                 /* check for cases like `[L;' or `[L[I;' or `[Ljava.lang.Object' */
2420                 if (namelen < 4 || text[2] == '[' || text[namelen - 1] != ';') {
2421                         *exceptionptr = new_noclassdeffounderror(c->name);
2422                         return NULL;
2423                 }
2424
2425                 u = utf_new(text + 2, namelen - 3);
2426
2427                 if (!(comp = load_class_from_classloader(u, loader)))
2428                         return NULL;
2429
2430                 LOADER_ASSERT(comp->state & CLASS_LOADED);
2431
2432                 if (opt_eager)
2433                         if (!link_class(c))
2434                                 return NULL;
2435
2436                 /* the array's flags are that of the component class */
2437                 c->flags = (comp->flags & ~ACC_INTERFACE) | ACC_FINAL | ACC_ABSTRACT;
2438                 c->classloader = comp->classloader;
2439                 break;
2440
2441         default:
2442                 /* c is an array of a primitive type */
2443
2444                 /* check for cases like `[II' */
2445                 if (namelen > 2) {
2446                         *exceptionptr = new_noclassdeffounderror(c->name);
2447                         return NULL;
2448                 }
2449
2450                 /* the accessibility of the array class is public (VM Spec 5.3.3) */
2451                 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
2452                 c->classloader = NULL;
2453         }
2454
2455         LOADER_ASSERT(class_java_lang_Object);
2456         LOADER_ASSERT(class_java_lang_Cloneable);
2457         LOADER_ASSERT(class_java_io_Serializable);
2458
2459         /* setup the array class */
2460
2461         c->super.cls = class_java_lang_Object;
2462
2463     c->interfacescount = 2;
2464     c->interfaces = MNEW(classref_or_classinfo, 2);
2465
2466         if (opt_eager) {
2467                 classinfo *tc;
2468
2469                 tc = class_java_lang_Cloneable;
2470                 LOADER_ASSERT(tc->state & CLASS_LOADED);
2471                 list_add_first(&unlinkedclasses, tc);
2472                 c->interfaces[0].cls = tc;
2473
2474                 tc = class_java_io_Serializable;
2475                 LOADER_ASSERT(tc->state & CLASS_LOADED);
2476                 list_add_first(&unlinkedclasses, tc);
2477                 c->interfaces[1].cls = tc;
2478
2479         } else {
2480                 c->interfaces[0].cls = class_java_lang_Cloneable;
2481                 c->interfaces[1].cls = class_java_io_Serializable;
2482         }
2483
2484         c->methodscount = 1;
2485         c->methods = MNEW(methodinfo, c->methodscount);
2486
2487         classrefs = MNEW(constant_classref, 2);
2488         CLASSREF_INIT(classrefs[0], c, c->name);
2489         CLASSREF_INIT(classrefs[1], c, utf_java_lang_Object);
2490
2491         /* create descriptor for clone method */
2492         /* we need one paramslot which is reserved for the 'this' parameter */
2493         clonedesc = NEW(methoddesc);
2494         clonedesc->returntype.type = TYPE_ADR;
2495         clonedesc->returntype.classref = classrefs + 1;
2496         clonedesc->returntype.arraydim = 0;
2497         /* initialize params to "empty", add real params below in
2498            descriptor_params_from_paramtypes */
2499         clonedesc->paramcount = 0;
2500         clonedesc->paramslots = 0;
2501         clonedesc->paramtypes[0].classref = classrefs + 0;
2502         clonedesc->params = NULL;
2503
2504         /* create methodinfo */
2505
2506         clone = c->methods;
2507         MSET(clone, 0, methodinfo, 1);
2508
2509 #if defined(ENABLE_THREADS)
2510         lock_init_object_lock(&clone->header);
2511 #endif
2512
2513         /* ATTENTION: if you delete the ACC_NATIVE below, set
2514            clone->maxlocals=1 (interpreter related) */
2515
2516         clone->flags      = ACC_PUBLIC | ACC_NATIVE;
2517         clone->name       = utf_clone;
2518         clone->descriptor = utf_void__java_lang_Object;
2519         clone->parseddesc = clonedesc;
2520         clone->class      = c;
2521
2522         /* parse the descriptor to get the register allocation */
2523
2524         if (!descriptor_params_from_paramtypes(clonedesc, clone->flags))
2525                 return false;
2526
2527         clone->code =
2528                 codegen_createnativestub((functionptr) &builtin_clone_array, clone);
2529
2530         /* XXX: field: length? */
2531
2532         /* array classes are not loaded from class files */
2533
2534         c->state          |= CLASS_LOADED;
2535         c->parseddescs    = (u1 *) clonedesc;
2536         c->parseddescsize = sizeof(methodinfo);
2537         c->classrefs      = classrefs;
2538         c->classrefcount  = 1;
2539
2540         /* insert class into the loaded class cache */
2541         /* XXX free classinfo if NULL returned? */
2542
2543         return classcache_store(loader, c, true);
2544 }
2545
2546
2547 /* loader_close ****************************************************************
2548
2549    Frees all resources.
2550         
2551 *******************************************************************************/
2552
2553 void loader_close(void)
2554 {
2555         /* empty */
2556 }
2557
2558
2559 /*
2560  * These are local overrides for various environment variables in Emacs.
2561  * Please do not remove this and leave it at the end of the file, where
2562  * Emacs will automagically detect them.
2563  * ---------------------------------------------------------------------
2564  * Local variables:
2565  * mode: c
2566  * indent-tabs-mode: t
2567  * c-basic-offset: 4
2568  * tab-width: 4
2569  * End:
2570  * vim:noexpandtab:sw=4:ts=4:
2571  */