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 #define ClassClass _CL_Q34java4lang5Class
47 extern java::lang::Class ClassClass;
48 #define ClassObject _CL_Q34java4lang6Object
49 extern java::lang::Class ClassObject;
51 // we don't verify method names that match these.
52 static _Jv_Utf8Const *clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
53 static _Jv_Utf8Const *init_name = _Jv_makeUtf8Const ("<init>", 6);
56 // these go in some seperate functions, to avoid having _Jv_InitClass
57 // inserted all over the place.
58 static void throw_internal_error (char *msg)
59 __attribute__ ((__noreturn__));
60 static void throw_no_class_def_found_error (jstring msg)
61 __attribute__ ((__noreturn__));
62 static void throw_no_class_def_found_error (char *msg)
63 __attribute__ ((__noreturn__));
64 static void throw_class_format_error (jstring msg)
65 __attribute__ ((__noreturn__));
66 static void throw_class_format_error (char *msg)
67 __attribute__ ((__noreturn__));
68 static void throw_incompatible_class_change_error (jstring msg)
69 __attribute__ ((__noreturn__));
70 static void throw_class_circularity_error (jstring msg)
71 __attribute__ ((__noreturn__));
73 static jdouble long_bits_to_double (jlong);
74 static jfloat int_bits_to_float (jint);
77 * We define class reading using a class. It is practical, since then
78 * the entire class-reader can be a friend of class Class (it needs to
79 * write all it's different structures); but also because this makes it
80 * easy to make class definition reentrant, and thus two threads can be
81 * defining classes at the same time. This class (_Jv_ClassReader) is
82 * never exposed outside this file, so we don't have to worry about
83 * public or private members here.
86 struct _Jv_ClassReader {
88 // do verification? Currently, there is no option to disable this.
89 // This flag just controls the verificaiton done by the class loader;
90 // i.e., checking the integrity of the constant pool; and it is
91 // allways on. You always want this as far as I can see, but it also
92 // controls weither identifiers and type descriptors/signatures are
93 // verified as legal. This could be somewhat more expensive since it
94 // will call Characher.isJavaIdentifier{Start,Part} for each character
95 // in any identifier (field name or method name) it comes by. Thus,
96 // it might be useful to turn off this verification for classes that
97 // come from a trusted source. However, for GCJ, trusted classes are
98 // most likely to be linked in.
103 unsigned char *bytes;
106 // current input position
109 // the constant pool data
112 unsigned int *offsets;
114 // the class to define (see java-interp.h)
115 _Jv_InterpClass *def;
117 /* check that the given number of input bytes are available */
118 inline void check (int num)
121 throw_class_format_error ("Premature end of data");
124 /* skip a given number of bytes in input */
125 inline void skip (int num)
131 /* read an unsignend 1-byte unit */
132 inline static jint get1u (unsigned char* bytes)
137 /* read an unsigned 1-byte unit */
138 inline jint read1u ()
141 return get1u (bytes+pos-1);
144 /* read an unsigned 2-byte unit */
145 inline static jint get2u (unsigned char *bytes)
147 return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
150 /* read an unsigned 2-byte unit */
151 inline jint read2u ()
154 return get2u (bytes+pos-2);
157 /* read a 4-byte unit */
158 static jint get4 (unsigned char *bytes)
160 return (((jint)bytes[0]) << 24)
161 | (((jint)bytes[1]) << 16)
162 | (((jint)bytes[2]) << 8)
163 | (((jint)bytes[3]) << 0);
166 /* read a 4-byte unit, (we don't do that quite so often) */
170 return get4 (bytes+pos-4);
173 /* read a 8-byte unit */
174 static jlong get8 (unsigned char* bytes)
176 return (((jlong)bytes[0]) << 56)
177 | (((jlong)bytes[1]) << 48)
178 | (((jlong)bytes[2]) << 40)
179 | (((jlong)bytes[3]) << 32)
180 | (((jlong)bytes[4]) << 24)
181 | (((jlong)bytes[5]) << 16)
182 | (((jlong)bytes[6]) << 8)
183 | (((jlong)bytes[7]) << 0);
186 /* read a 8-byte unit */
187 inline jlong read8 ()
190 return get8 (bytes+pos-8);
193 inline void check_tag (int index, char expected_tag)
196 || index > pool_count
197 || tags[index] != expected_tag)
198 throw_class_format_error ("erroneous constant pool tag");
201 _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length)
203 if (klass == 0 || length < 0 || offset+length > data->length)
204 throw_internal_error ("arguments to _Jv_DefineClass");
207 bytes = (unsigned char*) (elements (data)+offset);
210 def = (_Jv_InterpClass*) klass;
213 /** and here goes the parser members defined out-of-line */
215 void read_constpool ();
216 void prepare_pool_entry (int index, unsigned char tag);
218 void read_methods ();
219 void read_one_class_attribute ();
220 void read_one_method_attribute (int method);
221 void read_one_code_attribute (int method);
222 void read_one_field_attribute (int field);
224 /** check an utf8 entry, without creating a Utf8Const object */
225 bool is_attribute_name (int index, char *name);
227 /** here goes the class-loader members defined out-of-line */
228 void handleConstantPool ();
229 void handleClassBegin (int, int, int);
230 void handleInterfacesBegin (int);
231 void handleInterface (int, int);
232 void handleFieldsBegin (int);
233 void handleField (int, int, int, int);
234 void handleFieldsEnd ();
235 void handleConstantValueAttribute (int,int);
236 void handleMethodsBegin (int);
237 void handleMethod (int, int, int, int);
238 void handleMethodsEnd ();
239 void handleCodeAttribute (int, int, int, int, int, int);
240 void handleExceptionTableEntry (int, int, int, int, int, int);
242 void checkExtends (jclass sub, jclass super);
243 void checkImplements (jclass sub, jclass super);
246 * FIXME: we should keep a hash table of utf8-strings, since many will
247 * be the same. It's a little tricky, however, because the hash table
248 * needs to interact gracefully with the garbage collector. Much
249 * memory is to be saved by this, however! perhaps the improvement
250 * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
251 * computes the hash value anyway.
255 /* This is used for the isJavaIdentifierStart & isJavaIdentifierPart
256 methods, so we avoid doing _Jv_InitClass all the time */
258 static const java::lang::Character *character = 0;
259 static void prepare_character ();
262 _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length)
265 prepare_character ();
267 _Jv_ClassReader reader (klass, data, offset, length);
273 /** put it after _Jv_DefineClass, so it doesn't get inlined */
274 static void prepare_character ()
276 character = new java::lang::Character ('!');
280 /** This section defines the parsing/scanning of the class data */
283 _Jv_ClassReader::parse ()
285 int magic = read4 ();
287 /* FIXME: Decide which range of version numbers to allow */
289 /* int minor_version = */ read2u ();
290 /* int major_verson = */ read2u ();
292 if (magic != (int) 0xCAFEBABE)
293 throw_class_format_error ("bad magic number");
295 pool_count = read2u ();
299 int access_flags = read2u ();
300 int this_class = read2u ();
301 int super_class = read2u ();
303 check_tag (this_class, JV_CONSTANT_Class);
304 if (super_class != 0)
305 check_tag (super_class, JV_CONSTANT_Class);
307 handleClassBegin (access_flags, this_class, super_class);
309 int interfaces_count = read2u ();
311 handleInterfacesBegin (interfaces_count);
313 for (int i = 0; i < interfaces_count; i++)
315 int iface = read2u ();
316 check_tag (iface, JV_CONSTANT_Class);
317 handleInterface (i, iface);
323 int attributes_count = read2u ();
325 for (int i = 0; i < attributes_count; i++)
327 read_one_class_attribute ();
331 throw_class_format_error ("unused data before end of file");
333 // tell everyone we're done.
334 def->state = JV_STATE_LOADED;
339 void _Jv_ClassReader::read_constpool ()
341 tags = (unsigned char*) _Jv_AllocBytesChecked (pool_count);
342 offsets = (unsigned int *) _Jv_AllocBytesChecked (sizeof (int)
345 /** first, we scan the constant pool, collecting tags and offsets */
346 tags[0] = JV_CONSTANT_Undefined;
348 for (int c = 1; c < pool_count; c++)
355 case JV_CONSTANT_String:
356 case JV_CONSTANT_Class:
360 case JV_CONSTANT_Fieldref:
361 case JV_CONSTANT_Methodref:
362 case JV_CONSTANT_InterfaceMethodref:
363 case JV_CONSTANT_NameAndType:
364 case JV_CONSTANT_Integer:
365 case JV_CONSTANT_Float:
369 case JV_CONSTANT_Double:
370 case JV_CONSTANT_Long:
372 tags[++c] = JV_CONSTANT_Undefined;
375 case JV_CONSTANT_Utf8:
382 case JV_CONSTANT_Unicode:
383 throw_class_format_error ("unicode not supported");
387 throw_class_format_error ("erroneous constant pool tag");
391 handleConstantPool ();
395 void _Jv_ClassReader::read_fields ()
397 int fields_count = read2u ();
398 handleFieldsBegin (fields_count);
400 for (int i = 0; i < fields_count; i++)
402 int access_flags = read2u ();
403 int name_index = read2u ();
404 int descriptor_index = read2u ();
405 int attributes_count = read2u ();
407 check_tag (name_index, JV_CONSTANT_Utf8);
408 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
410 check_tag (descriptor_index, JV_CONSTANT_Utf8);
411 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
413 handleField (i, access_flags, name_index, descriptor_index);
415 for (int j = 0; j < attributes_count; j++)
417 read_one_field_attribute (i);
425 _Jv_ClassReader::is_attribute_name (int index, char *name)
427 check_tag (index, JV_CONSTANT_Utf8);
428 int len = get2u (bytes+offsets[index]);
429 if (len != (int) strlen (name))
432 return !memcmp (bytes+offsets[index]+2, name, len);
435 void _Jv_ClassReader::read_one_field_attribute (int field_index)
437 int name = read2u ();
438 int length = read4 ();
440 if (is_attribute_name (name, "ConstantValue"))
446 && (tags[cv] == JV_CONSTANT_Integer
447 || tags[cv] == JV_CONSTANT_Float
448 || tags[cv] == JV_CONSTANT_Long
449 || tags[cv] == JV_CONSTANT_Double
450 || tags[cv] == JV_CONSTANT_String))
452 handleConstantValueAttribute (field_index, cv);
456 throw_class_format_error ("erroneous ConstantValue attribute");
460 throw_class_format_error ("erroneous ConstantValue attribute");
469 void _Jv_ClassReader::read_methods ()
471 int methods_count = read2u ();
473 handleMethodsBegin (methods_count);
475 for (int i = 0; i < methods_count; i++)
477 int access_flags = read2u ();
478 int name_index = read2u ();
479 int descriptor_index = read2u ();
480 int attributes_count = read2u ();
482 check_tag (name_index, JV_CONSTANT_Utf8);
483 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
485 check_tag (name_index, JV_CONSTANT_Utf8);
486 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
488 handleMethod (i, access_flags, name_index,
491 for (int j = 0; j < attributes_count; j++)
493 read_one_method_attribute (i);
500 void _Jv_ClassReader::read_one_method_attribute (int method_index)
502 int name = read2u ();
503 int length = read4 ();
505 if (is_attribute_name (name, "Exceptions"))
507 /* we ignore this for now */
511 else if (is_attribute_name (name, "Code"))
514 int max_stack = read2u ();
515 int max_locals = read2u ();
516 int code_length = read4 ();
518 int code_start = pos;
520 int exception_table_length = read2u ();
522 handleCodeAttribute (method_index,
523 max_stack, max_locals,
524 code_start, code_length,
525 exception_table_length);
528 for (int i = 0; i < exception_table_length; i++)
530 int start_pc = read2u ();
531 int end_pc = read2u ();
532 int handler_pc = read2u ();
533 int catch_type = read2u ();
535 if (start_pc > end_pc
537 || end_pc >= code_length
538 || handler_pc >= code_length)
539 throw_class_format_error ("erroneous exception handler info");
541 if (! (tags[catch_type] == JV_CONSTANT_Class
542 || tags[catch_type] == 0))
544 throw_class_format_error ("erroneous exception handler info");
547 handleExceptionTableEntry (method_index,
556 int attributes_count = read2u ();
558 for (int i = 0; i < attributes_count; i++)
560 read_one_code_attribute (method_index);
563 if ((pos - start_off) != length)
564 throw_class_format_error ("code attribute too short");
569 /* ignore unknown attributes */
574 void _Jv_ClassReader::read_one_code_attribute (int /*method*/)
576 /* ignore for now, ... later we may want to pick up
577 line number information, for debugging purposes;
578 in fact, the whole debugger issue is open! */
580 /* int name = */ read2u ();
581 int length = read4 ();
586 void _Jv_ClassReader::read_one_class_attribute ()
588 /* we also ignore the class attributes, ...
589 some day we'll add inner-classes support. */
591 /* int name = */ read2u ();
592 int length = read4 ();
599 /* this section defines the semantic actions of the parser */
601 void _Jv_ClassReader::handleConstantPool ()
603 /** now, we actually define the class' constant pool */
605 // the pool is scanned explicitly by the collector
606 jbyte *pool_tags = (jbyte*) _Jv_AllocBytesChecked (pool_count);
608 = (_Jv_word*) _Jv_AllocBytesChecked (pool_count * sizeof (_Jv_word));
610 def->constants.tags = pool_tags;
611 def->constants.data = pool_data;
612 def->constants.size = pool_count;
614 // Here we make a pass to collect the strings! We do this, because
615 // internally in the GCJ runtime, classes are encoded with .'s not /'s.
616 // Therefore, we first collect the strings, and then translate the rest
617 // of the utf8-entries (thus not representing strings) from /-notation
619 for (int i = 1; i < pool_count; i++)
621 if (tags[i] == JV_CONSTANT_String)
623 unsigned char* str_data = bytes + offsets [i];
624 int utf_index = get2u (str_data);
625 check_tag (utf_index, JV_CONSTANT_Utf8);
626 unsigned char *utf_data = bytes + offsets[utf_index];
627 int len = get2u (utf_data);
628 pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
629 pool_tags[i] = JV_CONSTANT_String;
633 pool_tags[i] = JV_CONSTANT_Undefined;
637 // and now, we scan everything else but strings & utf8-entries. This
638 // leaves out those utf8-entries which are not used; which will be left
639 // with a tag of JV_CONSTANT_Undefined in the class definition.
640 for (int index = 1; index < pool_count; index++)
644 case JV_CONSTANT_Undefined:
645 case JV_CONSTANT_String:
646 case JV_CONSTANT_Utf8:
650 prepare_pool_entry (index, tags[index]);
656 /* this is a recursive procedure, which will prepare pool entries as needed.
657 Which is how we avoid initializing those entries which go unused. */
659 _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag)
661 /* these two, pool_data and pool_tags, point into the class
662 structure we are currently defining */
664 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
665 _Jv_word *pool_data = def->constants.data;
667 /* this entry was already prepared */
668 if (pool_tags[index] == this_tag)
671 /* this_data points to the constant-pool information for the current
672 constant-pool entry */
674 unsigned char *this_data = bytes + offsets[index];
678 case JV_CONSTANT_Utf8:
680 // If we came here, it is because some other tag needs this
681 // utf8-entry for type information! Thus, we translate /'s to .'s in
682 // order to accomondate gcj's internal representation.
684 int len = get2u (this_data);
685 char *buffer = (char*) alloca (len);
686 char *s = ((char*) this_data)+2;
688 /* FIXME: avoid using a buffer here */
689 for (int i = 0; i < len; i++)
694 buffer[i] = (char) s[i];
697 pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
698 pool_tags[index] = JV_CONSTANT_Utf8;
702 case JV_CONSTANT_Class:
704 int utf_index = get2u (this_data);
705 check_tag (utf_index, JV_CONSTANT_Utf8);
706 prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
709 _Jv_VerifyClassName (pool_data[utf_index].utf8);
711 pool_data[index].utf8 = pool_data[utf_index].utf8;
712 pool_tags[index] = JV_CONSTANT_Class;
716 case JV_CONSTANT_String:
717 // already handled before...
720 case JV_CONSTANT_Fieldref:
721 case JV_CONSTANT_Methodref:
722 case JV_CONSTANT_InterfaceMethodref:
724 int class_index = get2u (this_data);
725 int nat_index = get2u (this_data+2);
727 check_tag (class_index, JV_CONSTANT_Class);
728 prepare_pool_entry (class_index, JV_CONSTANT_Class);
730 check_tag (nat_index, JV_CONSTANT_NameAndType);
731 prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
733 // here, verify the signature and identifier name
736 _Jv_ushort name_index, type_index;
737 _Jv_loadIndexes (&pool_data[nat_index],
738 name_index, type_index);
740 if (this_tag == JV_CONSTANT_Fieldref)
741 _Jv_VerifyFieldSignature (pool_data[type_index].utf8);
743 _Jv_VerifyMethodSignature (pool_data[type_index].utf8);
745 _Jv_Utf8Const* name = pool_data[name_index].utf8;
747 if (this_tag != JV_CONSTANT_Fieldref
748 && ( _Jv_equalUtf8Consts (name, clinit_name)
749 || _Jv_equalUtf8Consts (name, init_name)))
752 _Jv_VerifyIdentifier (pool_data[name_index].utf8);
755 _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
756 pool_tags[index] = this_tag;
760 case JV_CONSTANT_NameAndType:
762 _Jv_ushort name_index = get2u (this_data);
763 _Jv_ushort type_index = get2u (this_data+2);
765 check_tag (name_index, JV_CONSTANT_Utf8);
766 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
768 check_tag (type_index, JV_CONSTANT_Utf8);
769 prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
771 _Jv_storeIndexes (&pool_data[index], name_index, type_index);
772 pool_tags[index] = JV_CONSTANT_NameAndType;
776 case JV_CONSTANT_Float:
778 jfloat f = int_bits_to_float ((jint) get4 (this_data));
779 _Jv_storeFloat (&pool_data[index], f);
780 pool_tags[index] = JV_CONSTANT_Float;
784 case JV_CONSTANT_Integer:
786 int i = get4 (this_data);
787 _Jv_storeInt (&pool_data[index], i);
788 pool_tags[index] = JV_CONSTANT_Integer;
792 case JV_CONSTANT_Double:
794 jdouble d = long_bits_to_double ((jlong) get8 (this_data));
795 _Jv_storeDouble (&pool_data[index], d);
796 pool_tags[index] = JV_CONSTANT_Double;
800 case JV_CONSTANT_Long:
802 jlong i = get8 (this_data);
803 _Jv_storeLong (&pool_data[index], i);
804 pool_tags[index] = JV_CONSTANT_Long;
809 throw_class_format_error ("erroneous constant pool tag");
815 _Jv_ClassReader::handleClassBegin
816 (int access_flags, int this_class, int super_class)
818 using namespace java::lang::reflect;
820 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
821 _Jv_word *pool_data = def->constants.data;
823 check_tag (this_class, JV_CONSTANT_Class);
824 _Jv_Utf8Const *loadedName = pool_data[this_class].utf8;
826 // was ClassLoader.defineClass called with an expected class name?
829 jclass orig = _Jv_FindClassInCache (loadedName, def->loader);
833 def->name = loadedName;
837 jstring msg = JvNewStringUTF ("anonymous "
838 "class data denotes "
840 msg = msg->concat (orig->getName ());
842 throw_no_class_def_found_error (msg);
846 // assert that the loaded class has the expected name, 5.3.5
847 else if (! _Jv_equalUtf8Consts (loadedName, def->name))
849 jstring msg = JvNewStringUTF ("loaded class ");
850 msg = msg->concat (def->getName ());
851 msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
852 jstring klass_name = _Jv_NewStringUTF (loadedName->data);
853 msg = msg->concat (klass_name);
855 throw_no_class_def_found_error (msg);
858 def->accflags = access_flags;
859 pool_data[this_class].clazz = def;
860 pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
862 if (super_class == 0)
864 // interfaces have java.lang.Object as super.
865 if (access_flags & Modifier::INTERFACE)
867 def->superclass = (jclass)&ClassObject;
870 // FIXME: Consider this carefully!
871 else if (!_Jv_equalUtf8Consts (def->name, ClassObject.name))
873 throw_no_class_def_found_error ("loading java.lang.Object");
877 // In the pre-loading state, it can be looked up in the
878 // cache only by this thread! This allows the super-class
879 // to include references to this class.
881 def->state = JV_STATE_PRELOADING;
882 _Jv_RegisterClass (def);
884 if (super_class != 0)
886 // load the super class
887 check_tag (super_class, JV_CONSTANT_Class);
888 _Jv_Utf8Const* super_name = pool_data[super_class].utf8;
890 // load the super class using our defining loader
891 jclass the_super = _Jv_FindClass (super_name,
894 // This will establish that we are allowed to be a subclass,
895 // and check for class circularity error
896 checkExtends (def, the_super);
898 def->superclass = the_super;
899 pool_data[super_class].clazz = the_super;
900 pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
903 // now we've come past the circularity problem, we can
904 // now say that we're loading...
906 def->state = JV_STATE_LOADING;
910 ///// implements the checks described in sect. 5.3.5.3
912 _Jv_ClassReader::checkExtends (jclass sub, jclass super)
914 using namespace java::lang::reflect;
916 // having an interface or a final class as a superclass is no good
917 if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0)
919 throw_incompatible_class_change_error (sub->getName ());
922 // if the super class is not public, we need to check some more
923 if ((super->accflags & Modifier::PUBLIC) == 0)
925 // With package scope, the classes must have the same
927 if ( sub->loader != super->loader
928 || !_Jv_ClassNameSamePackage (sub->name, super->name))
930 throw_incompatible_class_change_error (sub->getName ());
934 for (; super != 0; super = super->superclass)
937 throw_class_circularity_error (sub->getName ());
943 void _Jv_ClassReader::handleInterfacesBegin (int count)
945 def->interfaces = (jclass*) _Jv_AllocBytesChecked (count*sizeof (jclass));
946 def->interface_count = count;
949 void _Jv_ClassReader::handleInterface (int if_number, int offset)
951 _Jv_word * pool_data = def->constants.data;
952 unsigned char * pool_tags = (unsigned char*) def->constants.tags;
954 jclass the_interface;
956 if (pool_tags[offset] == JV_CONSTANT_Class)
958 _Jv_Utf8Const* name = pool_data[offset].utf8;
959 the_interface = _Jv_FindClass (name, def->loader);
961 else if (pool_tags[offset] == JV_CONSTANT_ResolvedClass)
963 the_interface = pool_data[offset].clazz;
967 throw_no_class_def_found_error ("erroneous constant pool tag");
970 // checks the validity of the_interface, and that we are in fact
971 // allowed to implement that interface.
972 checkImplements (def, the_interface);
974 pool_data[offset].clazz = the_interface;
975 pool_tags[offset] = JV_CONSTANT_ResolvedClass;
977 def->interfaces[if_number] = the_interface;
981 _Jv_ClassReader::checkImplements (jclass sub, jclass super)
983 using namespace java::lang::reflect;
985 // well, it *must* be an interface
986 if ((super->accflags & Modifier::INTERFACE) == 0)
988 throw_incompatible_class_change_error (sub->getName ());
991 // if it has package scope, it must also be defined by the
993 if ((super->accflags & Modifier::PUBLIC) == 0)
995 if ( sub->loader != super->loader
996 || !_Jv_ClassNameSamePackage (sub->name, super->name))
998 throw_incompatible_class_change_error (sub->getName ());
1002 // FIXME: add interface circularity check here
1005 throw_class_circularity_error (sub->getName ());
1009 void _Jv_ClassReader::handleFieldsBegin (int count)
1011 def->fields = (_Jv_Field*)
1012 _Jv_AllocBytesChecked (count * sizeof (_Jv_Field));
1013 def->field_count = count;
1014 def->field_initializers = (_Jv_ushort*)
1015 _Jv_AllocBytesChecked (count * sizeof (_Jv_ushort));
1016 for (int i = 0; i < count; i++)
1017 def->field_initializers[i] = (_Jv_ushort) 0;
1020 void _Jv_ClassReader::handleField (int field_no,
1025 using namespace java::lang::reflect;
1027 _Jv_word *pool_data = def->constants.data;
1029 _Jv_Field *field = &def->fields[field_no];
1030 _Jv_Utf8Const *field_name = pool_data[name].utf8;
1032 #ifndef COMPACT_FIELDS
1033 field->name = field_name;
1035 field->nameIndex = name;
1039 _Jv_VerifyIdentifier (field_name);
1041 // ignore flags we don't know about.
1042 field->flags = flags & Modifier::ALL_FLAGS;
1046 if (field->flags & (Modifier::SYNCHRONIZED
1048 | Modifier::INTERFACE
1049 | Modifier::ABSTRACT))
1050 throw_class_format_error ("erroneous field access flags");
1052 if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0)
1053 +((field->flags & Modifier::PRIVATE) ? 1 : 0)
1054 +((field->flags & Modifier::PROTECTED) ? 1 : 0)))
1055 throw_class_format_error ("erroneous field access flags");
1058 _Jv_Utf8Const* sig = pool_data[desc].utf8;
1061 _Jv_VerifyFieldSignature (sig);
1063 // field->type is really a jclass, but while it is still
1064 // unresolved we keep an _Jv_Utf8Const* instead.
1065 field->type = (jclass) sig;
1066 field->flags |= _Jv_FIELD_UNRESOLVED_FLAG;
1067 field->u.boffset = 0;
1071 void _Jv_ClassReader::handleConstantValueAttribute (int field_index,
1074 using namespace java::lang::reflect;
1076 _Jv_Field *field = &def->fields[field_index];
1078 if ((field->flags & (Modifier::STATIC
1080 | Modifier::PRIVATE)) == 0)
1082 // Ignore, as per vmspec #4.7.2
1086 // do not allow multiple constant fields!
1087 if (field->flags & _Jv_FIELD_CONSTANT_VALUE)
1088 throw_class_format_error ("field has multiple ConstantValue attributes");
1090 field->flags |= _Jv_FIELD_CONSTANT_VALUE;
1091 def->field_initializers[field_index] = value;
1093 /* type check the initializer */
1095 if (value <= 0 || value >= pool_count)
1096 throw_class_format_error ("erroneous ConstantValue attribute");
1098 /* FIXME: do the rest */
1101 void _Jv_ClassReader::handleFieldsEnd ()
1103 using namespace java::lang::reflect;
1105 // We need to reorganize the fields so that the static ones are first,
1106 // to conform to GCJ class layout.
1109 int high = def->field_count-1;
1110 _Jv_Field *fields = def->fields;
1111 _Jv_ushort *inits = def->field_initializers;
1113 // this is kind of a raw version of quicksort.
1116 // go forward on low, while it's a static
1117 while (low < high && (fields[low].flags & Modifier::STATIC) != 0)
1120 // go backwards on high, while it's a non-static
1121 while (low < high && (fields[high].flags & Modifier::STATIC) == 0)
1127 _Jv_Field tmp = fields[low];
1128 _Jv_ushort itmp = inits[low];
1130 fields[low] = fields[high];
1131 inits[low] = inits[high];
1140 if ((fields[low].flags & Modifier::STATIC) != 0)
1143 def->static_field_count = low;
1149 _Jv_ClassReader::handleMethodsBegin (int count)
1151 def->methods = (_Jv_Method*)
1152 _Jv_AllocBytesChecked (sizeof (_Jv_Method)*count);
1154 def->interpreted_methods
1155 = (_Jv_MethodBase **) _Jv_AllocBytesChecked (sizeof (_Jv_MethodBase *)
1158 for (int i = 0; i < count; i++)
1159 def->interpreted_methods[i] = 0;
1161 def->method_count = count;
1165 void _Jv_ClassReader::handleMethod
1166 (int mth_index, int accflags, int name, int desc)
1168 using namespace java::lang::reflect;
1170 _Jv_word *pool_data = def->constants.data;
1171 _Jv_Method *method = &def->methods[mth_index];
1173 check_tag (name, JV_CONSTANT_Utf8);
1174 prepare_pool_entry (name, JV_CONSTANT_Utf8);
1175 method->name = pool_data[name].utf8;
1177 check_tag (desc, JV_CONSTANT_Utf8);
1178 prepare_pool_entry (desc, JV_CONSTANT_Utf8);
1179 method->signature = pool_data[desc].utf8;
1181 // ignore unknown flags
1182 method->accflags = accflags & Modifier::ALL_FLAGS;
1189 if (_Jv_equalUtf8Consts (method->name, clinit_name)
1190 || _Jv_equalUtf8Consts (method->name, init_name))
1193 _Jv_VerifyIdentifier (method->name);
1195 _Jv_VerifyMethodSignature (method->signature);
1197 if (method->accflags & (Modifier::VOLATILE
1198 | Modifier::TRANSIENT
1199 | Modifier::INTERFACE))
1200 throw_class_format_error ("erroneous method access flags");
1202 if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0)
1203 +((method->accflags & Modifier::PRIVATE) ? 1 : 0)
1204 +((method->accflags & Modifier::PROTECTED) ? 1 : 0)))
1205 throw_class_format_error ("erroneous method access flags");
1209 void _Jv_ClassReader::handleCodeAttribute
1210 (int method_index, int max_stack, int max_locals,
1211 int code_start, int code_length, int exc_table_length)
1213 int size = _Jv_InterpMethod::size (exc_table_length, code_length);
1214 _Jv_InterpMethod *method =
1215 (_Jv_InterpMethod*) (_Jv_AllocBytesChecked (size));
1217 method->max_stack = max_stack;
1218 method->max_locals = max_locals;
1219 method->code_length = code_length;
1220 method->exc_count = exc_table_length;
1221 method->defining_class = def;
1222 method->self = &def->methods[method_index];
1224 // grab the byte code!
1225 memcpy ((void*) method->bytecode (),
1226 (void*) (bytes+code_start),
1229 def->interpreted_methods[method_index] = method;
1231 /* that's all we do for now */
1234 void _Jv_ClassReader::handleExceptionTableEntry
1235 (int method_index, int exc_index,
1236 int start_pc, int end_pc, int handler_pc, int catch_type)
1238 _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
1239 (def->interpreted_methods[method_index]);
1240 _Jv_InterpException *exc = method->exceptions ();
1242 exc[exc_index].start_pc = start_pc;
1243 exc[exc_index].end_pc = end_pc;
1244 exc[exc_index].handler_pc = handler_pc;
1245 exc[exc_index].handler_type = catch_type;
1248 void _Jv_ClassReader::handleMethodsEnd ()
1250 using namespace java::lang::reflect;
1252 for (int i = 0; i < def->method_count; i++)
1254 _Jv_Method *method = &def->methods[i];
1255 if ((method->accflags & Modifier::NATIVE) != 0)
1257 if (def->interpreted_methods[i] != 0)
1258 throw_class_format_error ("code provided for native method");
1261 _Jv_JNIMethod *m = (_Jv_JNIMethod *)
1262 _Jv_AllocBytesChecked (sizeof (_Jv_JNIMethod));
1263 m->defining_class = def;
1266 def->interpreted_methods[i] = m;
1269 else if ((method->accflags & Modifier::ABSTRACT) != 0)
1271 if (def->interpreted_methods[i] != 0)
1272 throw_class_format_error ("code provided for abstract method");
1276 if (def->interpreted_methods[i] == 0)
1277 throw_class_format_error ("method with no code");
1284 /** This section takes care of verifying integrity of identifiers,
1285 signatures, field ddescriptors, and class names */
1287 #define UTF8_PEEK(PTR, LIMIT) \
1288 ({ unsigned char* xxkeep = (PTR); \
1289 int xxch = UTF8_GET(PTR,LIMIT); \
1290 PTR = xxkeep; xxch; })
1292 /* verify one element of a type descriptor or signature */
1293 static unsigned char*
1294 _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
1299 int ch = UTF8_GET (ptr, limit);
1304 if (! void_ok) return 0;
1306 case 'S': case 'B': case 'I': case 'J':
1307 case 'Z': case 'C': case 'F': case 'D':
1312 unsigned char *start = ptr, *end;
1319 if ((ch = UTF8_GET (ptr, limit)) == -1)
1322 } while (ch != ';');
1323 _Jv_VerifyClassName (start, (unsigned short) (end-start));
1328 return _Jv_VerifyOne (ptr, limit, false);
1340 /** verification and loading procedures **/
1343 _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
1345 unsigned char* ptr = (unsigned char*) sig->data;
1346 unsigned char* limit = ptr + sig->length;
1348 ptr = _Jv_VerifyOne (ptr, limit, false);
1351 throw_class_format_error ("erroneous type descriptor");
1355 _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
1357 unsigned char* ptr = (unsigned char*) sig->data;
1358 unsigned char* limit = ptr + sig->length;
1361 throw_class_format_error ("erroneous type descriptor");
1363 if (UTF8_GET(ptr,limit) != '(')
1364 throw_class_format_error ("erroneous type descriptor");
1366 while (ptr && UTF8_PEEK (ptr, limit) != ')')
1367 ptr = _Jv_VerifyOne (ptr, limit, false);
1369 if (UTF8_GET (ptr, limit) != ')')
1370 throw_class_format_error ("erroneous type descriptor");
1372 // get the return type
1373 ptr = _Jv_VerifyOne (ptr, limit, true);
1376 throw_class_format_error ("erroneous type descriptor");
1382 /* we try to avoid calling the Character methods all the time,
1383 in fact, they will only be called for non-standard things */
1385 static __inline__ int
1386 is_identifier_start (int c)
1388 unsigned int ch = (unsigned)c;
1390 if ((ch - 0x41U) < 29U) /* A ... Z */
1392 if ((ch - 0x61U) < 29U) /* a ... z */
1394 if (ch == 0x5FU) /* _ */
1397 return character->isJavaIdentifierStart ((jchar) ch);
1400 static __inline__ int
1401 is_identifier_part (int c)
1403 unsigned int ch = (unsigned)c;
1405 if ((ch - 0x41U) < 29U) /* A ... Z */
1407 if ((ch - 0x61U) < 29U) /* a ... z */
1409 if ((ch - 0x30) < 10U) /* 0 .. 9 */
1411 if (ch == 0x5FU || ch == 0x24U) /* _ $ */
1414 return character->isJavaIdentifierStart ((jchar) ch);
1418 _Jv_VerifyIdentifier (_Jv_Utf8Const* name)
1420 unsigned char *ptr = (unsigned char*) name->data;
1421 unsigned char *limit = ptr + name->length;
1424 if ((ch = UTF8_GET (ptr, limit))==-1
1425 || ! is_identifier_start (ch))
1426 throw_class_format_error ("erroneous identifier");
1428 while (ptr != limit)
1430 if ((ch = UTF8_GET (ptr, limit))==-1
1431 || ! is_identifier_part (ch))
1432 throw_class_format_error ("erroneous identifier");
1438 _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
1440 unsigned char *limit = ptr+length;
1443 if ('[' == UTF8_PEEK (ptr, limit))
1445 if (! _Jv_VerifyOne (++ptr, limit, false))
1446 throw_class_format_error ("erroneous class name");
1453 if ((ch = UTF8_GET (ptr, limit))==-1)
1454 throw_class_format_error ("erroneous class name");
1455 if (! is_identifier_start (ch))
1456 throw_class_format_error ("erroneous class name");
1460 else if ((ch = UTF8_GET (ptr, limit))==-1)
1461 throw_class_format_error ("erroneous class name");
1464 else if (! is_identifier_part (ch))
1465 throw_class_format_error ("erroneous class name");
1472 _Jv_VerifyClassName (_Jv_Utf8Const *name)
1474 _Jv_VerifyClassName ((unsigned char*)&name->data[0],
1475 (_Jv_ushort) name->length);
1479 /** returns true, if name1 and name2 represents classes in the same
1483 _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
1485 unsigned char* ptr1 = (unsigned char*) name1->data;
1486 unsigned char* limit1 = ptr1 + name1->length;
1488 unsigned char* last1 = ptr1;
1490 // scan name1, and find the last occurrence of '.'
1491 while (ptr1 < limit1) {
1492 int ch1 = UTF8_GET (ptr1, limit1);
1501 // now the length of name1's package name is len
1502 int len = last1 - (unsigned char*) name1->data;
1504 // if this is longer than name2, then we're off
1505 if (len > name2->length)
1508 // then compare the first len bytes for equality
1509 if (memcmp ((void*) name1->data, (void*) name2->data, len) == 0)
1511 // check that there are no .'s after position len in name2
1513 unsigned char* ptr2 = (unsigned char*) name2->data + len;
1514 unsigned char* limit2 =
1515 (unsigned char*) name2->data + name2->length;
1517 while (ptr2 < limit2)
1519 int ch2 = UTF8_GET (ptr2, limit2);
1520 if (ch2 == -1 || ch2 == '.')
1530 /** Here we define the exceptions that can be thrown */
1533 throw_no_class_def_found_error (jstring msg)
1536 JvThrow (new java::lang::NoClassDefFoundError);
1538 JvThrow (new java::lang::NoClassDefFoundError (msg));
1542 throw_no_class_def_found_error (char *msg)
1544 throw_no_class_def_found_error (JvNewStringLatin1 (msg));
1548 throw_class_format_error (jstring msg)
1551 JvThrow (new java::lang::ClassFormatError);
1553 JvThrow (new java::lang::ClassFormatError (msg));
1557 throw_class_format_error (char *msg)
1559 throw_class_format_error (JvNewStringLatin1 (msg));
1563 throw_internal_error (char *msg)
1566 (new java::lang::InternalError (JvNewStringLatin1 (msg)));
1569 static jfloat int_bits_to_float (jint value)
1571 return java::lang::Float::intBitsToFloat (value);
1574 static jdouble long_bits_to_double (jlong value)
1576 return java::lang::Double::longBitsToDouble (value);
1579 static void throw_incompatible_class_change_error (jstring msg)
1581 JvThrow (new java::lang::IncompatibleClassChangeError (msg));
1584 static void throw_class_circularity_error (jstring msg)
1586 JvThrow (new java::lang::ClassCircularityError (msg));
1589 #endif /* INTERPRETER */