1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 1999, 2000 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 Author: Kresten Krab Thorup <krab@gnu.org>
14 Written using the online versions of Java Language Specification (1st
15 ed.) and The Java Virtual Machine Specification (2nd ed.).
17 Future work may include reading (and handling) attributes which are
18 currently being ignored ("InnerClasses", "LineNumber", etc...).
23 #include <java-interp.h>
30 #include <java-cpool.h>
33 #include <java/lang/Class.h>
34 #include <java/lang/Float.h>
35 #include <java/lang/Double.h>
36 #include <java/lang/Character.h>
37 #include <java/lang/LinkageError.h>
38 #include <java/lang/InternalError.h>
39 #include <java/lang/ClassFormatError.h>
40 #include <java/lang/NoClassDefFoundError.h>
41 #include <java/lang/ClassCircularityError.h>
42 #include <java/lang/ClassNotFoundException.h>
43 #include <java/lang/IncompatibleClassChangeError.h>
44 #include <java/lang/reflect/Modifier.h>
46 // we don't verify method names that match these.
47 static _Jv_Utf8Const
*clinit_name
= _Jv_makeUtf8Const ("<clinit>", 8);
48 static _Jv_Utf8Const
*init_name
= _Jv_makeUtf8Const ("<init>", 6);
51 // these go in some seperate functions, to avoid having _Jv_InitClass
52 // inserted all over the place.
53 static void throw_internal_error (char *msg
)
54 __attribute__ ((__noreturn__
));
55 static void throw_no_class_def_found_error (jstring msg
)
56 __attribute__ ((__noreturn__
));
57 static void throw_no_class_def_found_error (char *msg
)
58 __attribute__ ((__noreturn__
));
59 static void throw_class_format_error (jstring msg
)
60 __attribute__ ((__noreturn__
));
61 static void throw_class_format_error (char *msg
)
62 __attribute__ ((__noreturn__
));
63 static void throw_incompatible_class_change_error (jstring msg
)
64 __attribute__ ((__noreturn__
));
65 static void throw_class_circularity_error (jstring msg
)
66 __attribute__ ((__noreturn__
));
68 static jdouble
long_bits_to_double (jlong
);
69 static jfloat
int_bits_to_float (jint
);
72 * We define class reading using a class. It is practical, since then
73 * the entire class-reader can be a friend of class Class (it needs to
74 * write all it's different structures); but also because this makes it
75 * easy to make class definition reentrant, and thus two threads can be
76 * defining classes at the same time. This class (_Jv_ClassReader) is
77 * never exposed outside this file, so we don't have to worry about
78 * public or private members here.
81 struct _Jv_ClassReader
{
83 // do verification? Currently, there is no option to disable this.
84 // This flag just controls the verificaiton done by the class loader;
85 // i.e., checking the integrity of the constant pool; and it is
86 // allways on. You always want this as far as I can see, but it also
87 // controls weither identifiers and type descriptors/signatures are
88 // verified as legal. This could be somewhat more expensive since it
89 // will call Characher.isJavaIdentifier{Start,Part} for each character
90 // in any identifier (field name or method name) it comes by. Thus,
91 // it might be useful to turn off this verification for classes that
92 // come from a trusted source. However, for GCJ, trusted classes are
93 // most likely to be linked in.
101 // current input position
104 // the constant pool data
107 unsigned int *offsets
;
109 // the class to define (see java-interp.h)
110 _Jv_InterpClass
*def
;
112 /* check that the given number of input bytes are available */
113 inline void check (int num
)
116 throw_class_format_error ("Premature end of data");
119 /* skip a given number of bytes in input */
120 inline void skip (int num
)
126 /* read an unsignend 1-byte unit */
127 inline static jint
get1u (unsigned char* bytes
)
132 /* read an unsigned 1-byte unit */
133 inline jint
read1u ()
136 return get1u (bytes
+pos
-1);
139 /* read an unsigned 2-byte unit */
140 inline static jint
get2u (unsigned char *bytes
)
142 return (((jint
)bytes
[0]) << 8) | ((jint
)bytes
[1]);
145 /* read an unsigned 2-byte unit */
146 inline jint
read2u ()
149 return get2u (bytes
+pos
-2);
152 /* read a 4-byte unit */
153 static jint
get4 (unsigned char *bytes
)
155 return (((jint
)bytes
[0]) << 24)
156 | (((jint
)bytes
[1]) << 16)
157 | (((jint
)bytes
[2]) << 8)
158 | (((jint
)bytes
[3]) << 0);
161 /* read a 4-byte unit, (we don't do that quite so often) */
165 return get4 (bytes
+pos
-4);
168 /* read a 8-byte unit */
169 static jlong
get8 (unsigned char* bytes
)
171 return (((jlong
)bytes
[0]) << 56)
172 | (((jlong
)bytes
[1]) << 48)
173 | (((jlong
)bytes
[2]) << 40)
174 | (((jlong
)bytes
[3]) << 32)
175 | (((jlong
)bytes
[4]) << 24)
176 | (((jlong
)bytes
[5]) << 16)
177 | (((jlong
)bytes
[6]) << 8)
178 | (((jlong
)bytes
[7]) << 0);
181 /* read a 8-byte unit */
182 inline jlong
read8 ()
185 return get8 (bytes
+pos
-8);
188 inline void check_tag (int index
, char expected_tag
)
191 || index
> pool_count
192 || tags
[index
] != expected_tag
)
193 throw_class_format_error ("erroneous constant pool tag");
196 _Jv_ClassReader (jclass klass
, jbyteArray data
, jint offset
, jint length
)
198 if (klass
== 0 || length
< 0 || offset
+length
> data
->length
)
199 throw_internal_error ("arguments to _Jv_DefineClass");
202 bytes
= (unsigned char*) (elements (data
)+offset
);
205 def
= (_Jv_InterpClass
*) klass
;
208 /** and here goes the parser members defined out-of-line */
210 void read_constpool ();
211 void prepare_pool_entry (int index
, unsigned char tag
);
213 void read_methods ();
214 void read_one_class_attribute ();
215 void read_one_method_attribute (int method
);
216 void read_one_code_attribute (int method
);
217 void read_one_field_attribute (int field
);
219 /** check an utf8 entry, without creating a Utf8Const object */
220 bool is_attribute_name (int index
, char *name
);
222 /** here goes the class-loader members defined out-of-line */
223 void handleConstantPool ();
224 void handleClassBegin (int, int, int);
225 void handleInterfacesBegin (int);
226 void handleInterface (int, int);
227 void handleFieldsBegin (int);
228 void handleField (int, int, int, int);
229 void handleFieldsEnd ();
230 void handleConstantValueAttribute (int,int);
231 void handleMethodsBegin (int);
232 void handleMethod (int, int, int, int);
233 void handleMethodsEnd ();
234 void handleCodeAttribute (int, int, int, int, int, int);
235 void handleExceptionTableEntry (int, int, int, int, int, int);
237 void checkExtends (jclass sub
, jclass super
);
238 void checkImplements (jclass sub
, jclass super
);
241 * FIXME: we should keep a hash table of utf8-strings, since many will
242 * be the same. It's a little tricky, however, because the hash table
243 * needs to interact gracefully with the garbage collector. Much
244 * memory is to be saved by this, however! perhaps the improvement
245 * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
246 * computes the hash value anyway.
250 /* This is used for the isJavaIdentifierStart & isJavaIdentifierPart
251 methods, so we avoid doing _Jv_InitClass all the time */
253 static const java::lang::Character
*character
= 0;
254 static void prepare_character ();
257 _Jv_DefineClass (jclass klass
, jbyteArray data
, jint offset
, jint length
)
260 prepare_character ();
262 _Jv_ClassReader
reader (klass
, data
, offset
, length
);
268 /** put it after _Jv_DefineClass, so it doesn't get inlined */
269 static void prepare_character ()
271 character
= new java::lang::Character ('!');
275 /** This section defines the parsing/scanning of the class data */
278 _Jv_ClassReader::parse ()
280 int magic
= read4 ();
282 /* FIXME: Decide which range of version numbers to allow */
284 /* int minor_version = */ read2u ();
285 /* int major_verson = */ read2u ();
287 if (magic
!= (int) 0xCAFEBABE)
288 throw_class_format_error ("bad magic number");
290 pool_count
= read2u ();
294 int access_flags
= read2u ();
295 int this_class
= read2u ();
296 int super_class
= read2u ();
298 check_tag (this_class
, JV_CONSTANT_Class
);
299 if (super_class
!= 0)
300 check_tag (super_class
, JV_CONSTANT_Class
);
302 handleClassBegin (access_flags
, this_class
, super_class
);
304 int interfaces_count
= read2u ();
306 handleInterfacesBegin (interfaces_count
);
308 for (int i
= 0; i
< interfaces_count
; i
++)
310 int iface
= read2u ();
311 check_tag (iface
, JV_CONSTANT_Class
);
312 handleInterface (i
, iface
);
318 int attributes_count
= read2u ();
320 for (int i
= 0; i
< attributes_count
; i
++)
322 read_one_class_attribute ();
326 throw_class_format_error ("unused data before end of file");
328 // tell everyone we're done.
329 def
->state
= JV_STATE_LOADED
;
334 void _Jv_ClassReader::read_constpool ()
336 tags
= (unsigned char*) _Jv_AllocBytesChecked (pool_count
);
337 offsets
= (unsigned int *) _Jv_AllocBytesChecked (sizeof (int)
340 /** first, we scan the constant pool, collecting tags and offsets */
341 tags
[0] = JV_CONSTANT_Undefined
;
343 for (int c
= 1; c
< pool_count
; c
++)
350 case JV_CONSTANT_String
:
351 case JV_CONSTANT_Class
:
355 case JV_CONSTANT_Fieldref
:
356 case JV_CONSTANT_Methodref
:
357 case JV_CONSTANT_InterfaceMethodref
:
358 case JV_CONSTANT_NameAndType
:
359 case JV_CONSTANT_Integer
:
360 case JV_CONSTANT_Float
:
364 case JV_CONSTANT_Double
:
365 case JV_CONSTANT_Long
:
367 tags
[++c
] = JV_CONSTANT_Undefined
;
370 case JV_CONSTANT_Utf8
:
377 case JV_CONSTANT_Unicode
:
378 throw_class_format_error ("unicode not supported");
382 throw_class_format_error ("erroneous constant pool tag");
386 handleConstantPool ();
390 void _Jv_ClassReader::read_fields ()
392 int fields_count
= read2u ();
393 handleFieldsBegin (fields_count
);
395 for (int i
= 0; i
< fields_count
; i
++)
397 int access_flags
= read2u ();
398 int name_index
= read2u ();
399 int descriptor_index
= read2u ();
400 int attributes_count
= read2u ();
402 check_tag (name_index
, JV_CONSTANT_Utf8
);
403 prepare_pool_entry (name_index
, JV_CONSTANT_Utf8
);
405 check_tag (descriptor_index
, JV_CONSTANT_Utf8
);
406 prepare_pool_entry (descriptor_index
, JV_CONSTANT_Utf8
);
408 handleField (i
, access_flags
, name_index
, descriptor_index
);
410 for (int j
= 0; j
< attributes_count
; j
++)
412 read_one_field_attribute (i
);
420 _Jv_ClassReader::is_attribute_name (int index
, char *name
)
422 check_tag (index
, JV_CONSTANT_Utf8
);
423 int len
= get2u (bytes
+offsets
[index
]);
424 if (len
!= (int) strlen (name
))
427 return !memcmp (bytes
+offsets
[index
]+2, name
, len
);
430 void _Jv_ClassReader::read_one_field_attribute (int field_index
)
432 int name
= read2u ();
433 int length
= read4 ();
435 if (is_attribute_name (name
, "ConstantValue"))
441 && (tags
[cv
] == JV_CONSTANT_Integer
442 || tags
[cv
] == JV_CONSTANT_Float
443 || tags
[cv
] == JV_CONSTANT_Long
444 || tags
[cv
] == JV_CONSTANT_Double
445 || tags
[cv
] == JV_CONSTANT_String
))
447 handleConstantValueAttribute (field_index
, cv
);
451 throw_class_format_error ("erroneous ConstantValue attribute");
455 throw_class_format_error ("erroneous ConstantValue attribute");
464 void _Jv_ClassReader::read_methods ()
466 int methods_count
= read2u ();
468 handleMethodsBegin (methods_count
);
470 for (int i
= 0; i
< methods_count
; i
++)
472 int access_flags
= read2u ();
473 int name_index
= read2u ();
474 int descriptor_index
= read2u ();
475 int attributes_count
= read2u ();
477 check_tag (name_index
, JV_CONSTANT_Utf8
);
478 prepare_pool_entry (descriptor_index
, JV_CONSTANT_Utf8
);
480 check_tag (name_index
, JV_CONSTANT_Utf8
);
481 prepare_pool_entry (descriptor_index
, JV_CONSTANT_Utf8
);
483 handleMethod (i
, access_flags
, name_index
,
486 for (int j
= 0; j
< attributes_count
; j
++)
488 read_one_method_attribute (i
);
495 void _Jv_ClassReader::read_one_method_attribute (int method_index
)
497 int name
= read2u ();
498 int length
= read4 ();
500 if (is_attribute_name (name
, "Exceptions"))
502 /* we ignore this for now */
506 else if (is_attribute_name (name
, "Code"))
509 int max_stack
= read2u ();
510 int max_locals
= read2u ();
511 int code_length
= read4 ();
513 int code_start
= pos
;
515 int exception_table_length
= read2u ();
517 handleCodeAttribute (method_index
,
518 max_stack
, max_locals
,
519 code_start
, code_length
,
520 exception_table_length
);
523 for (int i
= 0; i
< exception_table_length
; i
++)
525 int start_pc
= read2u ();
526 int end_pc
= read2u ();
527 int handler_pc
= read2u ();
528 int catch_type
= read2u ();
530 if (start_pc
> end_pc
532 || end_pc
>= code_length
533 || handler_pc
>= code_length
)
534 throw_class_format_error ("erroneous exception handler info");
536 if (! (tags
[catch_type
] == JV_CONSTANT_Class
537 || tags
[catch_type
] == 0))
539 throw_class_format_error ("erroneous exception handler info");
542 handleExceptionTableEntry (method_index
,
551 int attributes_count
= read2u ();
553 for (int i
= 0; i
< attributes_count
; i
++)
555 read_one_code_attribute (method_index
);
558 if ((pos
- start_off
) != length
)
559 throw_class_format_error ("code attribute too short");
564 /* ignore unknown attributes */
569 void _Jv_ClassReader::read_one_code_attribute (int /*method*/)
571 /* ignore for now, ... later we may want to pick up
572 line number information, for debugging purposes;
573 in fact, the whole debugger issue is open! */
575 /* int name = */ read2u ();
576 int length
= read4 ();
581 void _Jv_ClassReader::read_one_class_attribute ()
583 /* we also ignore the class attributes, ...
584 some day we'll add inner-classes support. */
586 /* int name = */ read2u ();
587 int length
= read4 ();
594 /* this section defines the semantic actions of the parser */
596 void _Jv_ClassReader::handleConstantPool ()
598 /** now, we actually define the class' constant pool */
600 // the pool is scanned explicitly by the collector
601 jbyte
*pool_tags
= (jbyte
*) _Jv_AllocBytesChecked (pool_count
);
603 = (_Jv_word
*) _Jv_AllocBytesChecked (pool_count
* sizeof (_Jv_word
));
605 def
->constants
.tags
= pool_tags
;
606 def
->constants
.data
= pool_data
;
607 def
->constants
.size
= pool_count
;
609 // Here we make a pass to collect the strings! We do this, because
610 // internally in the GCJ runtime, classes are encoded with .'s not /'s.
611 // Therefore, we first collect the strings, and then translate the rest
612 // of the utf8-entries (thus not representing strings) from /-notation
614 for (int i
= 1; i
< pool_count
; i
++)
616 if (tags
[i
] == JV_CONSTANT_String
)
618 unsigned char* str_data
= bytes
+ offsets
[i
];
619 int utf_index
= get2u (str_data
);
620 check_tag (utf_index
, JV_CONSTANT_Utf8
);
621 unsigned char *utf_data
= bytes
+ offsets
[utf_index
];
622 int len
= get2u (utf_data
);
623 pool_data
[i
].utf8
= _Jv_makeUtf8Const ((char*)(utf_data
+2), len
);
624 pool_tags
[i
] = JV_CONSTANT_String
;
628 pool_tags
[i
] = JV_CONSTANT_Undefined
;
632 // and now, we scan everything else but strings & utf8-entries. This
633 // leaves out those utf8-entries which are not used; which will be left
634 // with a tag of JV_CONSTANT_Undefined in the class definition.
635 for (int index
= 1; index
< pool_count
; index
++)
639 case JV_CONSTANT_Undefined
:
640 case JV_CONSTANT_String
:
641 case JV_CONSTANT_Utf8
:
645 prepare_pool_entry (index
, tags
[index
]);
651 /* this is a recursive procedure, which will prepare pool entries as needed.
652 Which is how we avoid initializing those entries which go unused. */
654 _Jv_ClassReader::prepare_pool_entry (int index
, unsigned char this_tag
)
656 /* these two, pool_data and pool_tags, point into the class
657 structure we are currently defining */
659 unsigned char *pool_tags
= (unsigned char*) def
->constants
.tags
;
660 _Jv_word
*pool_data
= def
->constants
.data
;
662 /* this entry was already prepared */
663 if (pool_tags
[index
] == this_tag
)
666 /* this_data points to the constant-pool information for the current
667 constant-pool entry */
669 unsigned char *this_data
= bytes
+ offsets
[index
];
673 case JV_CONSTANT_Utf8
:
675 // If we came here, it is because some other tag needs this
676 // utf8-entry for type information! Thus, we translate /'s to .'s in
677 // order to accomondate gcj's internal representation.
679 int len
= get2u (this_data
);
680 char *buffer
= (char*) alloca (len
);
681 char *s
= ((char*) this_data
)+2;
683 /* FIXME: avoid using a buffer here */
684 for (int i
= 0; i
< len
; i
++)
689 buffer
[i
] = (char) s
[i
];
692 pool_data
[index
].utf8
= _Jv_makeUtf8Const (buffer
, len
);
693 pool_tags
[index
] = JV_CONSTANT_Utf8
;
697 case JV_CONSTANT_Class
:
699 int utf_index
= get2u (this_data
);
700 check_tag (utf_index
, JV_CONSTANT_Utf8
);
701 prepare_pool_entry (utf_index
, JV_CONSTANT_Utf8
);
704 _Jv_VerifyClassName (pool_data
[utf_index
].utf8
);
706 pool_data
[index
].utf8
= pool_data
[utf_index
].utf8
;
707 pool_tags
[index
] = JV_CONSTANT_Class
;
711 case JV_CONSTANT_String
:
712 // already handled before...
715 case JV_CONSTANT_Fieldref
:
716 case JV_CONSTANT_Methodref
:
717 case JV_CONSTANT_InterfaceMethodref
:
719 int class_index
= get2u (this_data
);
720 int nat_index
= get2u (this_data
+2);
722 check_tag (class_index
, JV_CONSTANT_Class
);
723 prepare_pool_entry (class_index
, JV_CONSTANT_Class
);
725 check_tag (nat_index
, JV_CONSTANT_NameAndType
);
726 prepare_pool_entry (nat_index
, JV_CONSTANT_NameAndType
);
728 // here, verify the signature and identifier name
731 _Jv_ushort name_index
, type_index
;
732 _Jv_loadIndexes (&pool_data
[nat_index
],
733 name_index
, type_index
);
735 if (this_tag
== JV_CONSTANT_Fieldref
)
736 _Jv_VerifyFieldSignature (pool_data
[type_index
].utf8
);
738 _Jv_VerifyMethodSignature (pool_data
[type_index
].utf8
);
740 _Jv_Utf8Const
* name
= pool_data
[name_index
].utf8
;
742 if (this_tag
!= JV_CONSTANT_Fieldref
743 && ( _Jv_equalUtf8Consts (name
, clinit_name
)
744 || _Jv_equalUtf8Consts (name
, init_name
)))
747 _Jv_VerifyIdentifier (pool_data
[name_index
].utf8
);
750 _Jv_storeIndexes (&pool_data
[index
], class_index
, nat_index
);
751 pool_tags
[index
] = this_tag
;
755 case JV_CONSTANT_NameAndType
:
757 _Jv_ushort name_index
= get2u (this_data
);
758 _Jv_ushort type_index
= get2u (this_data
+2);
760 check_tag (name_index
, JV_CONSTANT_Utf8
);
761 prepare_pool_entry (name_index
, JV_CONSTANT_Utf8
);
763 check_tag (type_index
, JV_CONSTANT_Utf8
);
764 prepare_pool_entry (type_index
, JV_CONSTANT_Utf8
);
766 _Jv_storeIndexes (&pool_data
[index
], name_index
, type_index
);
767 pool_tags
[index
] = JV_CONSTANT_NameAndType
;
771 case JV_CONSTANT_Float
:
773 jfloat f
= int_bits_to_float ((jint
) get4 (this_data
));
774 _Jv_storeFloat (&pool_data
[index
], f
);
775 pool_tags
[index
] = JV_CONSTANT_Float
;
779 case JV_CONSTANT_Integer
:
781 int i
= get4 (this_data
);
782 _Jv_storeInt (&pool_data
[index
], i
);
783 pool_tags
[index
] = JV_CONSTANT_Integer
;
787 case JV_CONSTANT_Double
:
789 jdouble d
= long_bits_to_double ((jlong
) get8 (this_data
));
790 _Jv_storeDouble (&pool_data
[index
], d
);
791 pool_tags
[index
] = JV_CONSTANT_Double
;
795 case JV_CONSTANT_Long
:
797 jlong i
= get8 (this_data
);
798 _Jv_storeLong (&pool_data
[index
], i
);
799 pool_tags
[index
] = JV_CONSTANT_Long
;
804 throw_class_format_error ("erroneous constant pool tag");
810 _Jv_ClassReader::handleClassBegin
811 (int access_flags
, int this_class
, int super_class
)
813 using namespace java::lang::reflect
;
815 unsigned char *pool_tags
= (unsigned char*) def
->constants
.tags
;
816 _Jv_word
*pool_data
= def
->constants
.data
;
818 check_tag (this_class
, JV_CONSTANT_Class
);
819 _Jv_Utf8Const
*loadedName
= pool_data
[this_class
].utf8
;
821 // was ClassLoader.defineClass called with an expected class name?
824 jclass orig
= _Jv_FindClassInCache (loadedName
, def
->loader
);
828 def
->name
= loadedName
;
832 jstring msg
= JvNewStringUTF ("anonymous "
833 "class data denotes "
835 msg
= msg
->concat (orig
->getName ());
837 throw_no_class_def_found_error (msg
);
841 // assert that the loaded class has the expected name, 5.3.5
842 else if (! _Jv_equalUtf8Consts (loadedName
, def
->name
))
844 jstring msg
= JvNewStringUTF ("loaded class ");
845 msg
= msg
->concat (def
->getName ());
846 msg
= msg
->concat (_Jv_NewStringUTF (" was in fact named "));
847 jstring klass_name
= _Jv_NewStringUTF (loadedName
->data
);
848 msg
= msg
->concat (klass_name
);
850 throw_no_class_def_found_error (msg
);
853 def
->accflags
= access_flags
;
854 pool_data
[this_class
].clazz
= def
;
855 pool_tags
[this_class
] = JV_CONSTANT_ResolvedClass
;
857 if (super_class
== 0)
859 // interfaces have java.lang.Object as super.
860 if (access_flags
& Modifier::INTERFACE
)
862 def
->superclass
= (jclass
)&java::lang::Class::class$
;
865 // FIXME: Consider this carefully!
866 else if (!_Jv_equalUtf8Consts (def
->name
,
867 java::lang::Class::class$
.name
))
869 throw_no_class_def_found_error ("loading java.lang.Object");
873 // In the pre-loading state, it can be looked up in the
874 // cache only by this thread! This allows the super-class
875 // to include references to this class.
877 def
->state
= JV_STATE_PRELOADING
;
878 _Jv_RegisterClass (def
);
880 if (super_class
!= 0)
882 // load the super class
883 check_tag (super_class
, JV_CONSTANT_Class
);
884 _Jv_Utf8Const
* super_name
= pool_data
[super_class
].utf8
;
886 // load the super class using our defining loader
887 jclass the_super
= _Jv_FindClass (super_name
,
890 // This will establish that we are allowed to be a subclass,
891 // and check for class circularity error
892 checkExtends (def
, the_super
);
894 def
->superclass
= the_super
;
895 pool_data
[super_class
].clazz
= the_super
;
896 pool_tags
[super_class
] = JV_CONSTANT_ResolvedClass
;
899 // now we've come past the circularity problem, we can
900 // now say that we're loading...
902 def
->state
= JV_STATE_LOADING
;
906 ///// implements the checks described in sect. 5.3.5.3
908 _Jv_ClassReader::checkExtends (jclass sub
, jclass super
)
910 using namespace java::lang::reflect
;
912 // having an interface or a final class as a superclass is no good
913 if ((super
->accflags
& (Modifier::INTERFACE
| Modifier::FINAL
)) != 0)
915 throw_incompatible_class_change_error (sub
->getName ());
918 // if the super class is not public, we need to check some more
919 if ((super
->accflags
& Modifier::PUBLIC
) == 0)
921 // With package scope, the classes must have the same
923 if ( sub
->loader
!= super
->loader
924 || !_Jv_ClassNameSamePackage (sub
->name
, super
->name
))
926 throw_incompatible_class_change_error (sub
->getName ());
930 for (; super
!= 0; super
= super
->superclass
)
933 throw_class_circularity_error (sub
->getName ());
939 void _Jv_ClassReader::handleInterfacesBegin (int count
)
941 def
->interfaces
= (jclass
*) _Jv_AllocBytesChecked (count
*sizeof (jclass
));
942 def
->interface_count
= count
;
945 void _Jv_ClassReader::handleInterface (int if_number
, int offset
)
947 _Jv_word
* pool_data
= def
->constants
.data
;
948 unsigned char * pool_tags
= (unsigned char*) def
->constants
.tags
;
950 jclass the_interface
;
952 if (pool_tags
[offset
] == JV_CONSTANT_Class
)
954 _Jv_Utf8Const
* name
= pool_data
[offset
].utf8
;
955 the_interface
= _Jv_FindClass (name
, def
->loader
);
957 else if (pool_tags
[offset
] == JV_CONSTANT_ResolvedClass
)
959 the_interface
= pool_data
[offset
].clazz
;
963 throw_no_class_def_found_error ("erroneous constant pool tag");
966 // checks the validity of the_interface, and that we are in fact
967 // allowed to implement that interface.
968 checkImplements (def
, the_interface
);
970 pool_data
[offset
].clazz
= the_interface
;
971 pool_tags
[offset
] = JV_CONSTANT_ResolvedClass
;
973 def
->interfaces
[if_number
] = the_interface
;
977 _Jv_ClassReader::checkImplements (jclass sub
, jclass super
)
979 using namespace java::lang::reflect
;
981 // well, it *must* be an interface
982 if ((super
->accflags
& Modifier::INTERFACE
) == 0)
984 throw_incompatible_class_change_error (sub
->getName ());
987 // if it has package scope, it must also be defined by the
989 if ((super
->accflags
& Modifier::PUBLIC
) == 0)
991 if ( sub
->loader
!= super
->loader
992 || !_Jv_ClassNameSamePackage (sub
->name
, super
->name
))
994 throw_incompatible_class_change_error (sub
->getName ());
998 // FIXME: add interface circularity check here
1001 throw_class_circularity_error (sub
->getName ());
1005 void _Jv_ClassReader::handleFieldsBegin (int count
)
1007 def
->fields
= (_Jv_Field
*)
1008 _Jv_AllocBytesChecked (count
* sizeof (_Jv_Field
));
1009 def
->field_count
= count
;
1010 def
->field_initializers
= (_Jv_ushort
*)
1011 _Jv_AllocBytesChecked (count
* sizeof (_Jv_ushort
));
1012 for (int i
= 0; i
< count
; i
++)
1013 def
->field_initializers
[i
] = (_Jv_ushort
) 0;
1016 void _Jv_ClassReader::handleField (int field_no
,
1021 using namespace java::lang::reflect
;
1023 _Jv_word
*pool_data
= def
->constants
.data
;
1025 _Jv_Field
*field
= &def
->fields
[field_no
];
1026 _Jv_Utf8Const
*field_name
= pool_data
[name
].utf8
;
1028 #ifndef COMPACT_FIELDS
1029 field
->name
= field_name
;
1031 field
->nameIndex
= name
;
1035 _Jv_VerifyIdentifier (field_name
);
1037 // ignore flags we don't know about.
1038 field
->flags
= flags
& Modifier::ALL_FLAGS
;
1042 if (field
->flags
& (Modifier::SYNCHRONIZED
1044 | Modifier::INTERFACE
1045 | Modifier::ABSTRACT
))
1046 throw_class_format_error ("erroneous field access flags");
1048 if (1 < ( ((field
->flags
& Modifier::PUBLIC
) ? 1 : 0)
1049 +((field
->flags
& Modifier::PRIVATE
) ? 1 : 0)
1050 +((field
->flags
& Modifier::PROTECTED
) ? 1 : 0)))
1051 throw_class_format_error ("erroneous field access flags");
1054 _Jv_Utf8Const
* sig
= pool_data
[desc
].utf8
;
1057 _Jv_VerifyFieldSignature (sig
);
1059 // field->type is really a jclass, but while it is still
1060 // unresolved we keep an _Jv_Utf8Const* instead.
1061 field
->type
= (jclass
) sig
;
1062 field
->flags
|= _Jv_FIELD_UNRESOLVED_FLAG
;
1063 field
->u
.boffset
= 0;
1067 void _Jv_ClassReader::handleConstantValueAttribute (int field_index
,
1070 using namespace java::lang::reflect
;
1072 _Jv_Field
*field
= &def
->fields
[field_index
];
1074 if ((field
->flags
& (Modifier::STATIC
1076 | Modifier::PRIVATE
)) == 0)
1078 // Ignore, as per vmspec #4.7.2
1082 // do not allow multiple constant fields!
1083 if (field
->flags
& _Jv_FIELD_CONSTANT_VALUE
)
1084 throw_class_format_error ("field has multiple ConstantValue attributes");
1086 field
->flags
|= _Jv_FIELD_CONSTANT_VALUE
;
1087 def
->field_initializers
[field_index
] = value
;
1089 /* type check the initializer */
1091 if (value
<= 0 || value
>= pool_count
)
1092 throw_class_format_error ("erroneous ConstantValue attribute");
1094 /* FIXME: do the rest */
1097 void _Jv_ClassReader::handleFieldsEnd ()
1099 using namespace java::lang::reflect
;
1101 // We need to reorganize the fields so that the static ones are first,
1102 // to conform to GCJ class layout.
1105 int high
= def
->field_count
-1;
1106 _Jv_Field
*fields
= def
->fields
;
1107 _Jv_ushort
*inits
= def
->field_initializers
;
1109 // this is kind of a raw version of quicksort.
1112 // go forward on low, while it's a static
1113 while (low
< high
&& (fields
[low
].flags
& Modifier::STATIC
) != 0)
1116 // go backwards on high, while it's a non-static
1117 while (low
< high
&& (fields
[high
].flags
& Modifier::STATIC
) == 0)
1123 _Jv_Field tmp
= fields
[low
];
1124 _Jv_ushort itmp
= inits
[low
];
1126 fields
[low
] = fields
[high
];
1127 inits
[low
] = inits
[high
];
1136 if ((fields
[low
].flags
& Modifier::STATIC
) != 0)
1139 def
->static_field_count
= low
;
1145 _Jv_ClassReader::handleMethodsBegin (int count
)
1147 def
->methods
= (_Jv_Method
*)
1148 _Jv_AllocBytesChecked (sizeof (_Jv_Method
)*count
);
1150 def
->interpreted_methods
1151 = (_Jv_MethodBase
**) _Jv_AllocBytesChecked (sizeof (_Jv_MethodBase
*)
1154 for (int i
= 0; i
< count
; i
++)
1155 def
->interpreted_methods
[i
] = 0;
1157 def
->method_count
= count
;
1161 void _Jv_ClassReader::handleMethod
1162 (int mth_index
, int accflags
, int name
, int desc
)
1164 using namespace java::lang::reflect
;
1166 _Jv_word
*pool_data
= def
->constants
.data
;
1167 _Jv_Method
*method
= &def
->methods
[mth_index
];
1169 check_tag (name
, JV_CONSTANT_Utf8
);
1170 prepare_pool_entry (name
, JV_CONSTANT_Utf8
);
1171 method
->name
= pool_data
[name
].utf8
;
1173 check_tag (desc
, JV_CONSTANT_Utf8
);
1174 prepare_pool_entry (desc
, JV_CONSTANT_Utf8
);
1175 method
->signature
= pool_data
[desc
].utf8
;
1177 // ignore unknown flags
1178 method
->accflags
= accflags
& Modifier::ALL_FLAGS
;
1185 if (_Jv_equalUtf8Consts (method
->name
, clinit_name
)
1186 || _Jv_equalUtf8Consts (method
->name
, init_name
))
1189 _Jv_VerifyIdentifier (method
->name
);
1191 _Jv_VerifyMethodSignature (method
->signature
);
1193 if (method
->accflags
& (Modifier::VOLATILE
1194 | Modifier::TRANSIENT
1195 | Modifier::INTERFACE
))
1196 throw_class_format_error ("erroneous method access flags");
1198 if (1 < ( ((method
->accflags
& Modifier::PUBLIC
) ? 1 : 0)
1199 +((method
->accflags
& Modifier::PRIVATE
) ? 1 : 0)
1200 +((method
->accflags
& Modifier::PROTECTED
) ? 1 : 0)))
1201 throw_class_format_error ("erroneous method access flags");
1205 void _Jv_ClassReader::handleCodeAttribute
1206 (int method_index
, int max_stack
, int max_locals
,
1207 int code_start
, int code_length
, int exc_table_length
)
1209 int size
= _Jv_InterpMethod::size (exc_table_length
, code_length
);
1210 _Jv_InterpMethod
*method
=
1211 (_Jv_InterpMethod
*) (_Jv_AllocBytesChecked (size
));
1213 method
->max_stack
= max_stack
;
1214 method
->max_locals
= max_locals
;
1215 method
->code_length
= code_length
;
1216 method
->exc_count
= exc_table_length
;
1217 method
->defining_class
= def
;
1218 method
->self
= &def
->methods
[method_index
];
1220 // grab the byte code!
1221 memcpy ((void*) method
->bytecode (),
1222 (void*) (bytes
+code_start
),
1225 def
->interpreted_methods
[method_index
] = method
;
1227 /* that's all we do for now */
1230 void _Jv_ClassReader::handleExceptionTableEntry
1231 (int method_index
, int exc_index
,
1232 int start_pc
, int end_pc
, int handler_pc
, int catch_type
)
1234 _Jv_InterpMethod
*method
= reinterpret_cast<_Jv_InterpMethod
*>
1235 (def
->interpreted_methods
[method_index
]);
1236 _Jv_InterpException
*exc
= method
->exceptions ();
1238 exc
[exc_index
].start_pc
= start_pc
;
1239 exc
[exc_index
].end_pc
= end_pc
;
1240 exc
[exc_index
].handler_pc
= handler_pc
;
1241 exc
[exc_index
].handler_type
= catch_type
;
1244 void _Jv_ClassReader::handleMethodsEnd ()
1246 using namespace java::lang::reflect
;
1248 for (int i
= 0; i
< def
->method_count
; i
++)
1250 _Jv_Method
*method
= &def
->methods
[i
];
1251 if ((method
->accflags
& Modifier::NATIVE
) != 0)
1253 if (def
->interpreted_methods
[i
] != 0)
1254 throw_class_format_error ("code provided for native method");
1257 _Jv_JNIMethod
*m
= (_Jv_JNIMethod
*)
1258 _Jv_AllocBytesChecked (sizeof (_Jv_JNIMethod
));
1259 m
->defining_class
= def
;
1262 def
->interpreted_methods
[i
] = m
;
1265 else if ((method
->accflags
& Modifier::ABSTRACT
) != 0)
1267 if (def
->interpreted_methods
[i
] != 0)
1268 throw_class_format_error ("code provided for abstract method");
1272 if (def
->interpreted_methods
[i
] == 0)
1273 throw_class_format_error ("method with no code");
1280 /** This section takes care of verifying integrity of identifiers,
1281 signatures, field ddescriptors, and class names */
1283 #define UTF8_PEEK(PTR, LIMIT) \
1284 ({ unsigned char* xxkeep = (PTR); \
1285 int xxch = UTF8_GET(PTR,LIMIT); \
1286 PTR = xxkeep; xxch; })
1288 /* verify one element of a type descriptor or signature */
1289 static unsigned char*
1290 _Jv_VerifyOne (unsigned char* ptr
, unsigned char* limit
, bool void_ok
)
1295 int ch
= UTF8_GET (ptr
, limit
);
1300 if (! void_ok
) return 0;
1302 case 'S': case 'B': case 'I': case 'J':
1303 case 'Z': case 'C': case 'F': case 'D':
1308 unsigned char *start
= ptr
, *end
;
1315 if ((ch
= UTF8_GET (ptr
, limit
)) == -1)
1318 } while (ch
!= ';');
1319 _Jv_VerifyClassName (start
, (unsigned short) (end
-start
));
1324 return _Jv_VerifyOne (ptr
, limit
, false);
1336 /** verification and loading procedures **/
1339 _Jv_VerifyFieldSignature (_Jv_Utf8Const
*sig
)
1341 unsigned char* ptr
= (unsigned char*) sig
->data
;
1342 unsigned char* limit
= ptr
+ sig
->length
;
1344 ptr
= _Jv_VerifyOne (ptr
, limit
, false);
1347 throw_class_format_error ("erroneous type descriptor");
1351 _Jv_VerifyMethodSignature (_Jv_Utf8Const
*sig
)
1353 unsigned char* ptr
= (unsigned char*) sig
->data
;
1354 unsigned char* limit
= ptr
+ sig
->length
;
1357 throw_class_format_error ("erroneous type descriptor");
1359 if (UTF8_GET(ptr
,limit
) != '(')
1360 throw_class_format_error ("erroneous type descriptor");
1362 while (ptr
&& UTF8_PEEK (ptr
, limit
) != ')')
1363 ptr
= _Jv_VerifyOne (ptr
, limit
, false);
1365 if (UTF8_GET (ptr
, limit
) != ')')
1366 throw_class_format_error ("erroneous type descriptor");
1368 // get the return type
1369 ptr
= _Jv_VerifyOne (ptr
, limit
, true);
1372 throw_class_format_error ("erroneous type descriptor");
1378 /* we try to avoid calling the Character methods all the time,
1379 in fact, they will only be called for non-standard things */
1381 static __inline__
int
1382 is_identifier_start (int c
)
1384 unsigned int ch
= (unsigned)c
;
1386 if ((ch
- 0x41U
) < 29U) /* A ... Z */
1388 if ((ch
- 0x61U
) < 29U) /* a ... z */
1390 if (ch
== 0x5FU
) /* _ */
1393 return character
->isJavaIdentifierStart ((jchar
) ch
);
1396 static __inline__
int
1397 is_identifier_part (int c
)
1399 unsigned int ch
= (unsigned)c
;
1401 if ((ch
- 0x41U
) < 29U) /* A ... Z */
1403 if ((ch
- 0x61U
) < 29U) /* a ... z */
1405 if ((ch
- 0x30) < 10U) /* 0 .. 9 */
1407 if (ch
== 0x5FU
|| ch
== 0x24U
) /* _ $ */
1410 return character
->isJavaIdentifierStart ((jchar
) ch
);
1414 _Jv_VerifyIdentifier (_Jv_Utf8Const
* name
)
1416 unsigned char *ptr
= (unsigned char*) name
->data
;
1417 unsigned char *limit
= ptr
+ name
->length
;
1420 if ((ch
= UTF8_GET (ptr
, limit
))==-1
1421 || ! is_identifier_start (ch
))
1422 throw_class_format_error ("erroneous identifier");
1424 while (ptr
!= limit
)
1426 if ((ch
= UTF8_GET (ptr
, limit
))==-1
1427 || ! is_identifier_part (ch
))
1428 throw_class_format_error ("erroneous identifier");
1434 _Jv_VerifyClassName (unsigned char* ptr
, _Jv_ushort length
)
1436 unsigned char *limit
= ptr
+length
;
1439 if ('[' == UTF8_PEEK (ptr
, limit
))
1441 if (! _Jv_VerifyOne (++ptr
, limit
, false))
1442 throw_class_format_error ("erroneous class name");
1449 if ((ch
= UTF8_GET (ptr
, limit
))==-1)
1450 throw_class_format_error ("erroneous class name");
1451 if (! is_identifier_start (ch
))
1452 throw_class_format_error ("erroneous class name");
1456 else if ((ch
= UTF8_GET (ptr
, limit
))==-1)
1457 throw_class_format_error ("erroneous class name");
1460 else if (! is_identifier_part (ch
))
1461 throw_class_format_error ("erroneous class name");
1468 _Jv_VerifyClassName (_Jv_Utf8Const
*name
)
1470 _Jv_VerifyClassName ((unsigned char*)&name
->data
[0],
1471 (_Jv_ushort
) name
->length
);
1475 /** returns true, if name1 and name2 represents classes in the same
1479 _Jv_ClassNameSamePackage (_Jv_Utf8Const
*name1
, _Jv_Utf8Const
*name2
)
1481 unsigned char* ptr1
= (unsigned char*) name1
->data
;
1482 unsigned char* limit1
= ptr1
+ name1
->length
;
1484 unsigned char* last1
= ptr1
;
1486 // scan name1, and find the last occurrence of '.'
1487 while (ptr1
< limit1
) {
1488 int ch1
= UTF8_GET (ptr1
, limit1
);
1497 // now the length of name1's package name is len
1498 int len
= last1
- (unsigned char*) name1
->data
;
1500 // if this is longer than name2, then we're off
1501 if (len
> name2
->length
)
1504 // then compare the first len bytes for equality
1505 if (memcmp ((void*) name1
->data
, (void*) name2
->data
, len
) == 0)
1507 // check that there are no .'s after position len in name2
1509 unsigned char* ptr2
= (unsigned char*) name2
->data
+ len
;
1510 unsigned char* limit2
=
1511 (unsigned char*) name2
->data
+ name2
->length
;
1513 while (ptr2
< limit2
)
1515 int ch2
= UTF8_GET (ptr2
, limit2
);
1516 if (ch2
== -1 || ch2
== '.')
1526 /** Here we define the exceptions that can be thrown */
1529 throw_no_class_def_found_error (jstring msg
)
1532 JvThrow (new java::lang::NoClassDefFoundError
);
1534 JvThrow (new java::lang::NoClassDefFoundError (msg
));
1538 throw_no_class_def_found_error (char *msg
)
1540 throw_no_class_def_found_error (JvNewStringLatin1 (msg
));
1544 throw_class_format_error (jstring msg
)
1547 JvThrow (new java::lang::ClassFormatError
);
1549 JvThrow (new java::lang::ClassFormatError (msg
));
1553 throw_class_format_error (char *msg
)
1555 throw_class_format_error (JvNewStringLatin1 (msg
));
1559 throw_internal_error (char *msg
)
1562 (new java::lang::InternalError (JvNewStringLatin1 (msg
)));
1565 static jfloat
int_bits_to_float (jint value
)
1567 return java::lang::Float::intBitsToFloat (value
);
1570 static jdouble
long_bits_to_double (jlong value
)
1572 return java::lang::Double::longBitsToDouble (value
);
1575 static void throw_incompatible_class_change_error (jstring msg
)
1577 JvThrow (new java::lang::IncompatibleClassChangeError (msg
));
1580 static void throw_class_circularity_error (jstring msg
)
1582 JvThrow (new java::lang::ClassCircularityError (msg
));
1585 #endif /* INTERPRETER */