1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 1999 Cygnus Solutions
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...).
21 #include <java-interp.h>
25 #include <java-cpool.h>
28 #include <java/lang/Class.h>
29 #include <java/lang/Float.h>
30 #include <java/lang/Double.h>
31 #include <java/lang/Character.h>
32 #include <java/lang/LinkageError.h>
33 #include <java/lang/InternalError.h>
34 #include <java/lang/ClassFormatError.h>
35 #include <java/lang/NoClassDefFoundError.h>
36 #include <java/lang/ClassCircularityError.h>
37 #include <java/lang/ClassNotFoundException.h>
38 #include <java/lang/IncompatibleClassChangeError.h>
40 #define ClassClass _CL_Q34java4lang5Class
41 extern java::lang::Class ClassClass;
42 #define StringClass _CL_Q34java4lang6String
43 extern java::lang::Class StringClass;
44 #define ClassObject _CL_Q34java4lang6Object
45 extern java::lang::Class ClassObject;
47 // we don't verify method names that match these.
48 static _Jv_Utf8Const *clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
49 static _Jv_Utf8Const *init_name = _Jv_makeUtf8Const ("<init>", 6);
52 // these go in some seperate functions, to avoid having _Jv_InitClass
53 // inserted all over the place.
54 static void throw_internal_error (char *msg)
55 __attribute__ ((__noreturn__));
56 static void throw_no_class_def_found_error (jstring msg)
57 __attribute__ ((__noreturn__));
58 static void throw_no_class_def_found_error (char *msg)
59 __attribute__ ((__noreturn__));
60 static void throw_class_format_error (jstring msg)
61 __attribute__ ((__noreturn__));
62 static void throw_class_format_error (char *msg)
63 __attribute__ ((__noreturn__));
64 static void throw_incompatible_class_change_error (jstring msg)
65 __attribute__ ((__noreturn__));
66 static void throw_class_circularity_error (jstring msg)
67 __attribute__ ((__noreturn__));
69 static jdouble long_bits_to_double (jlong);
70 static jfloat int_bits_to_float (jint);
73 * We define class reading using a class. It is practical, since then
74 * the entire class-reader can be a friend of class Class (it needs to
75 * write all it's different structures); but also because this makes it
76 * easy to make class definition reentrant, and thus two threads can be
77 * defining classes at the same time. This class (_Jv_ClassReader) is
78 * never exposed outside this file, so we don't have to worry about
79 * public or private members here.
82 struct _Jv_ClassReader {
84 // do verification? Currently, there is no option to disable this.
85 // This flag just controls the verificaiton done by the class loader;
86 // i.e., checking the integrity of the constant pool; and it is
87 // allways on. You always want this as far as I can see, but it also
88 // controls weither identifiers and type descriptors/signatures are
89 // verified as legal. This could be somewhat more expensive since it
90 // will call Characher.isJavaIdentifier{Start,Part} for each character
91 // in any identifier (field name or method name) it comes by. Thus,
92 // it might be useful to turn off this verification for classes that
93 // come from a trusted source. However, for GCJ, trusted classes are
94 // most likely to be linked in.
102 // current input position
105 // the constant pool data
108 unsigned int *offsets;
110 // the class to define (see java-interp.h)
111 _Jv_InterpClass *def;
113 /* check that the given number of input bytes are available */
114 inline void check (int num)
117 throw_class_format_error ("Premature end of data");
120 /* skip a given number of bytes in input */
121 inline void skip (int num)
127 /* read an unsignend 1-byte unit */
128 inline static jint get1u (unsigned char* bytes)
133 /* read an unsigned 1-byte unit */
134 inline jint read1u ()
137 return get1u (bytes+pos-1);
140 /* read an unsigned 2-byte unit */
141 inline static jint get2u (unsigned char *bytes)
143 return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
146 /* read an unsigned 2-byte unit */
147 inline jint read2u ()
150 return get2u (bytes+pos-2);
153 /* read a 4-byte unit */
154 static jint get4 (unsigned char *bytes)
156 return (((jint)bytes[0]) << 24)
157 | (((jint)bytes[1]) << 16)
158 | (((jint)bytes[2]) << 8)
159 | (((jint)bytes[3]) << 0);
162 /* read a 4-byte unit, (we don't do that quite so often) */
166 return get4 (bytes+pos-4);
169 /* read a 8-byte unit */
170 static jlong get8 (unsigned char* bytes)
172 return (((jlong)bytes[0]) << 56)
173 | (((jlong)bytes[1]) << 48)
174 | (((jlong)bytes[2]) << 40)
175 | (((jlong)bytes[3]) << 32)
176 | (((jlong)bytes[4]) << 24)
177 | (((jlong)bytes[5]) << 16)
178 | (((jlong)bytes[6]) << 8)
179 | (((jlong)bytes[7]) << 0);
182 /* read a 8-byte unit */
183 inline jlong read8 ()
186 return get8 (bytes+pos-8);
189 inline void check_tag (int index, char expected_tag)
192 || index > pool_count
193 || tags[index] != expected_tag)
194 throw_class_format_error ("erroneous constant pool tag");
197 _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length)
199 if (klass == 0 || length < 0 || offset+length > data->length)
200 throw_internal_error ("arguments to _Jv_DefineClass");
203 bytes = (unsigned char*) (elements (data)+offset);
206 def = (_Jv_InterpClass*) klass;
209 /** and here goes the parser members defined out-of-line */
211 void read_constpool ();
212 void prepare_pool_entry (int index, unsigned char tag);
214 void read_methods ();
215 void read_one_class_attribute ();
216 void read_one_method_attribute (int method);
217 void read_one_code_attribute (int method);
218 void read_one_field_attribute (int field);
220 /** check an utf8 entry, without creating a Utf8Const object */
221 bool is_attribute_name (int index, char *name);
223 /** here goes the class-loader members defined out-of-line */
224 void handleConstantPool ();
225 void handleClassBegin (int, int, int);
226 void handleInterfacesBegin (int);
227 void handleInterface (int, int);
228 void handleFieldsBegin (int);
229 void handleField (int, int, int, int);
230 void handleFieldsEnd ();
231 void handleConstantValueAttribute (int,int);
232 void handleMethodsBegin (int);
233 void handleMethod (int, int, int, int);
234 void handleMethodsEnd ();
235 void handleCodeAttribute (int, int, int, int, int, int);
236 void handleExceptionTableEntry (int, int, int, int, int, int);
238 void checkExtends (jclass sub, jclass super);
239 void checkImplements (jclass sub, jclass super);
242 * FIXME: we should keep a hash table of utf8-strings, since many will
243 * be the same. It's a little tricky, however, because the hash table
244 * needs to interact gracefully with the garbage collector. Much
245 * memory is to be saved by this, however! perhaps the improvement
246 * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
247 * computes the hash value anyway.
250 static const int PUBLIC = 0x001;
251 static const int PRIVATE = 0x002;
252 static const int PROTECTED = 0x004;
253 static const int STATIC = 0x008;
254 static const int FINAL = 0x010;
255 static const int SYNCHRONIZED = 0x020;
256 static const int VOLATILE = 0x040;
257 static const int TRANSIENT = 0x080;
258 static const int NATIVE = 0x100;
259 static const int INTERFACE = 0x200;
260 static const int ABSTRACT = 0x400;
261 static const int ALL_FLAGS = 0x7FF;
265 /* This is used for the isJavaIdentifierStart & isJavaIdentifierPart
266 methods, so we avoid doing _Jv_InitClass all the time */
268 static const java::lang::Character *character = 0;
269 static void prepare_character ();
272 _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length)
275 prepare_character ();
277 _Jv_ClassReader reader (klass, data, offset, length);
283 /** put it after _Jv_DefineClass, so it doesn't get inlined */
284 static void prepare_character ()
286 character = new java::lang::Character ('!');
290 /** This section defines the parsing/scanning of the class data */
293 _Jv_ClassReader::parse ()
295 int magic = read4 ();
297 /* FIXME: Decide which range of version numbers to allow */
299 /* int minor_version = */ read2u ();
300 /* int major_verson = */ read2u ();
302 if (magic != (int) 0xCAFEBABE)
303 throw_class_format_error ("bad magic number");
305 pool_count = read2u ();
309 int access_flags = read2u ();
310 int this_class = read2u ();
311 int super_class = read2u ();
313 check_tag (this_class, JV_CONSTANT_Class);
314 if (super_class != 0)
315 check_tag (super_class, JV_CONSTANT_Class);
317 handleClassBegin (access_flags, this_class, super_class);
319 int interfaces_count = read2u ();
321 handleInterfacesBegin (interfaces_count);
323 for (int i = 0; i < interfaces_count; i++)
325 int iface = read2u ();
326 check_tag (iface, JV_CONSTANT_Class);
327 handleInterface (i, iface);
333 int attributes_count = read2u ();
335 for (int i = 0; i < attributes_count; i++)
337 read_one_class_attribute ();
341 throw_class_format_error ("unused data before end of file");
343 // tell everyone we're done.
344 def->state = JV_STATE_LOADED;
349 void _Jv_ClassReader::read_constpool ()
351 tags = (unsigned char*) _Jv_AllocBytesChecked (pool_count);
352 offsets = (unsigned int *) _Jv_AllocBytesChecked (sizeof (int)
355 /** first, we scan the constant pool, collecting tags and offsets */
356 tags[0] = JV_CONSTANT_Undefined;
358 for (int c = 1; c < pool_count; c++)
365 case JV_CONSTANT_String:
366 case JV_CONSTANT_Class:
370 case JV_CONSTANT_Fieldref:
371 case JV_CONSTANT_Methodref:
372 case JV_CONSTANT_InterfaceMethodref:
373 case JV_CONSTANT_NameAndType:
374 case JV_CONSTANT_Integer:
375 case JV_CONSTANT_Float:
379 case JV_CONSTANT_Double:
380 case JV_CONSTANT_Long:
382 tags[++c] = JV_CONSTANT_Undefined;
385 case JV_CONSTANT_Utf8:
392 case JV_CONSTANT_Unicode:
393 throw_class_format_error ("unicode not supported");
397 throw_class_format_error ("erroneous constant pool tag");
401 handleConstantPool ();
405 void _Jv_ClassReader::read_fields ()
407 int fields_count = read2u ();
408 handleFieldsBegin (fields_count);
410 for (int i = 0; i < fields_count; i++)
412 int access_flags = read2u ();
413 int name_index = read2u ();
414 int descriptor_index = read2u ();
415 int attributes_count = read2u ();
417 check_tag (name_index, JV_CONSTANT_Utf8);
418 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
420 check_tag (descriptor_index, JV_CONSTANT_Utf8);
421 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
423 handleField (i, access_flags, name_index, descriptor_index);
425 for (int j = 0; j < attributes_count; j++)
427 read_one_field_attribute (i);
435 _Jv_ClassReader::is_attribute_name (int index, char *name)
437 check_tag (index, JV_CONSTANT_Utf8);
438 int len = get2u (bytes+offsets[index]);
439 if (len != (int) strlen (name))
442 return !memcmp (bytes+offsets[index]+2, name, len);
445 void _Jv_ClassReader::read_one_field_attribute (int field_index)
447 int name = read2u ();
448 int length = read4 ();
450 if (is_attribute_name (name, "ConstantValue"))
456 && (tags[cv] == JV_CONSTANT_Integer
457 || tags[cv] == JV_CONSTANT_Float
458 || tags[cv] == JV_CONSTANT_Long
459 || tags[cv] == JV_CONSTANT_Double
460 || tags[cv] == JV_CONSTANT_String))
462 handleConstantValueAttribute (field_index, cv);
466 throw_class_format_error ("erroneous ConstantValue attribute");
470 throw_class_format_error ("erroneous ConstantValue attribute");
479 void _Jv_ClassReader::read_methods ()
481 int methods_count = read2u ();
483 handleMethodsBegin (methods_count);
485 for (int i = 0; i < methods_count; i++)
487 int access_flags = read2u ();
488 int name_index = read2u ();
489 int descriptor_index = read2u ();
490 int attributes_count = read2u ();
492 check_tag (name_index, JV_CONSTANT_Utf8);
493 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
495 check_tag (name_index, JV_CONSTANT_Utf8);
496 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
498 handleMethod (i, access_flags, name_index,
501 for (int j = 0; j < attributes_count; j++)
503 read_one_method_attribute (i);
510 void _Jv_ClassReader::read_one_method_attribute (int method_index)
512 int name = read2u ();
513 int length = read4 ();
515 if (is_attribute_name (name, "Exceptions"))
517 /* we ignore this for now */
521 else if (is_attribute_name (name, "Code"))
524 int max_stack = read2u ();
525 int max_locals = read2u ();
526 int code_length = read4 ();
528 int code_start = pos;
530 int exception_table_length = read2u ();
532 handleCodeAttribute (method_index,
533 max_stack, max_locals,
534 code_start, code_length,
535 exception_table_length);
538 for (int i = 0; i < exception_table_length; i++)
540 int start_pc = read2u ();
541 int end_pc = read2u ();
542 int handler_pc = read2u ();
543 int catch_type = read2u ();
545 if (start_pc > end_pc
547 || end_pc >= code_length
548 || handler_pc >= code_length)
549 throw_class_format_error ("erroneous exception handler info");
551 if (! (tags[catch_type] == JV_CONSTANT_Class
552 || tags[catch_type] == 0))
554 throw_class_format_error ("erroneous exception handler info");
557 handleExceptionTableEntry (method_index,
566 int attributes_count = read2u ();
568 for (int i = 0; i < attributes_count; i++)
570 read_one_code_attribute (method_index);
573 if ((pos - start_off) != length)
574 throw_class_format_error ("code attribute too short");
579 /* ignore unknown attributes */
584 void _Jv_ClassReader::read_one_code_attribute (int /*method*/)
586 /* ignore for now, ... later we may want to pick up
587 line number information, for debugging purposes;
588 in fact, the whole debugger issue is open! */
590 /* int name = */ read2u ();
591 int length = read4 ();
596 void _Jv_ClassReader::read_one_class_attribute ()
598 /* we also ignore the class attributes, ...
599 some day we'll add inner-classes support. */
601 /* int name = */ read2u ();
602 int length = read4 ();
609 /* this section defines the semantic actions of the parser */
611 void _Jv_ClassReader::handleConstantPool ()
613 /** now, we actually define the class' constant pool */
615 // the pool is scanned explicitly by the collector
616 jbyte *pool_tags = (jbyte*) _Jv_AllocBytesChecked (pool_count);
617 void **pool_data = (void**) _Jv_AllocBytesChecked (pool_count * sizeof (void*));
619 def->constants.tags = pool_tags;
620 def->constants.data = pool_data;
621 def->constants.size = pool_count;
623 // Here we make a pass to collect the strings! We do this, because
624 // internally in the GCJ runtime, classes are encoded with .'s not /'s.
625 // Therefore, we first collect the strings, and then translate the rest
626 // of the utf8-entries (thus not representing strings) from /-notation
628 for (int i = 1; i < pool_count; i++)
630 if (tags[i] == JV_CONSTANT_String)
632 unsigned char* str_data = bytes + offsets [i];
633 int utf_index = get2u (str_data);
634 check_tag (utf_index, JV_CONSTANT_Utf8);
635 unsigned char *utf_data = bytes + offsets[utf_index];
636 int len = get2u (utf_data);
637 pool_data[i] = (void*)_Jv_makeUtf8Const ((char*)(utf_data+2), len);
638 pool_tags[i] = JV_CONSTANT_String;
642 pool_tags[i] = JV_CONSTANT_Undefined;
646 // and now, we scan everything else but strings & utf8-entries. This
647 // leaves out those utf8-entries which are not used; which will be left
648 // with a tag of JV_CONSTANT_Undefined in the class definition.
649 for (int index = 1; index < pool_count; index++)
653 case JV_CONSTANT_Undefined:
654 case JV_CONSTANT_String:
655 case JV_CONSTANT_Utf8:
659 prepare_pool_entry (index, tags[index]);
665 /* this is a recursive procedure, which will prepare pool entries as needed.
666 Which is how we avoid initializing those entries which go unused. */
668 _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag)
670 /* these two, pool_data and pool_tags, point into the class
671 structure we are currently defining */
673 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
674 void **pool_data = (void**) def->constants.data;
676 /* this entry was already prepared */
677 if (pool_tags[index] == this_tag)
680 /* this_data points to the constant-pool information for the current
681 constant-pool entry */
683 unsigned char *this_data = bytes + offsets[index];
687 case JV_CONSTANT_Utf8:
689 // If we came here, it is because some other tag needs this
690 // utf8-entry for type information! Thus, we translate /'s to .'s in
691 // order to accomondate gcj's internal representation.
693 int len = get2u (this_data);
694 char *buffer = (char*) alloca (len);
695 char *s = ((char*) this_data)+2;
697 /* FIXME: avoid using a buffer here */
698 for (int i = 0; i < len; i++)
703 buffer[i] = (char) s[i];
706 pool_data[index] = (void*)_Jv_makeUtf8Const (buffer, len);
707 pool_tags[index] = JV_CONSTANT_Utf8;
711 case JV_CONSTANT_Class:
713 int utf_index = get2u (this_data);
714 check_tag (utf_index, JV_CONSTANT_Utf8);
715 prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
718 _Jv_VerifyClassName ((_Jv_Utf8Const*)pool_data[utf_index]);
720 pool_data[index] = pool_data[utf_index];
721 pool_tags[index] = JV_CONSTANT_Class;
725 case JV_CONSTANT_String:
726 // already handled before...
729 case JV_CONSTANT_Fieldref:
730 case JV_CONSTANT_Methodref:
731 case JV_CONSTANT_InterfaceMethodref:
733 int class_index = get2u (this_data);
734 int nat_index = get2u (this_data+2);
736 check_tag (class_index, JV_CONSTANT_Class);
737 prepare_pool_entry (class_index, JV_CONSTANT_Class);
739 check_tag (nat_index, JV_CONSTANT_NameAndType);
740 prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
742 // here, verify the signature and identifier name
745 _Jv_ushort name_index, type_index;
746 _Jv_loadIndexes ((const void**)&pool_data[nat_index],
747 name_index, type_index);
749 if (this_tag == JV_CONSTANT_Fieldref)
750 _Jv_VerifyFieldSignature
751 ((_Jv_Utf8Const*)pool_data[type_index]);
753 _Jv_VerifyMethodSignature
754 ((_Jv_Utf8Const*)pool_data[type_index]);
756 _Jv_Utf8Const* name = (_Jv_Utf8Const*)pool_data[name_index];
758 if (this_tag != JV_CONSTANT_Fieldref
759 && ( _Jv_equalUtf8Consts (name, clinit_name)
760 || _Jv_equalUtf8Consts (name, init_name)))
763 _Jv_VerifyIdentifier ((_Jv_Utf8Const*)pool_data[name_index]);
766 _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
767 pool_tags[index] = this_tag;
771 case JV_CONSTANT_NameAndType:
773 _Jv_ushort name_index = get2u (this_data);
774 _Jv_ushort type_index = get2u (this_data+2);
776 check_tag (name_index, JV_CONSTANT_Utf8);
777 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
779 check_tag (type_index, JV_CONSTANT_Utf8);
780 prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
782 _Jv_storeIndexes (&pool_data[index], name_index, type_index);
783 pool_tags[index] = JV_CONSTANT_NameAndType;
787 case JV_CONSTANT_Float:
789 jfloat f = int_bits_to_float ((jint) get4 (this_data));
790 _Jv_storeFloat (&pool_data[index], f);
791 pool_tags[index] = JV_CONSTANT_Float;
795 case JV_CONSTANT_Integer:
797 int i = get4 (this_data);
798 _Jv_storeInt (&pool_data[index], i);
799 pool_tags[index] = JV_CONSTANT_Integer;
803 case JV_CONSTANT_Double:
805 jdouble d = long_bits_to_double ((jlong) get8 (this_data));
806 _Jv_storeDouble (&pool_data[index], d);
807 pool_tags[index] = JV_CONSTANT_Double;
811 case JV_CONSTANT_Long:
813 jlong i = get8 (this_data);
814 _Jv_storeLong (&pool_data[index], i);
815 pool_tags[index] = JV_CONSTANT_Long;
820 throw_class_format_error ("erroneous constant pool tag");
826 _Jv_ClassReader::handleClassBegin
827 (int access_flags, int this_class, int super_class)
829 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
830 void **pool_data = (void**) def->constants.data;
832 check_tag (this_class, JV_CONSTANT_Class);
833 _Jv_Utf8Const *loadedName = (_Jv_Utf8Const*)pool_data[this_class];
835 // was ClassLoader.defineClass called with an expected class name?
838 jclass orig = _Jv_FindClassInCache (loadedName, def->loader);
842 def->name = loadedName;
846 jstring msg = JvNewStringUTF ("anonymous "
847 "class data denotes "
849 msg = msg->concat (orig->getName ());
851 throw_no_class_def_found_error (msg);
855 // assert that the loaded class has the expected name, 5.3.5
856 else if (! _Jv_equalUtf8Consts (loadedName, def->name))
858 jstring msg = JvNewStringUTF ("loaded class ");
859 msg = msg->concat (def->getName ());
860 msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
861 jstring klass_name = _Jv_NewStringUTF (loadedName->data);
862 msg = msg->concat (klass_name);
864 throw_no_class_def_found_error (msg);
867 def->accflags = access_flags;
868 pool_data[this_class] = (void*)def;
869 pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
871 if (super_class == 0)
873 // interfaces have java.lang.Object as super.
874 if (access_flags & INTERFACE)
876 def->superclass = (jclass)&ClassObject;
879 // FIXME: Consider this carefully!
880 else if (!_Jv_equalUtf8Consts (def->name, ClassObject.name))
882 throw_no_class_def_found_error ("loading java.lang.Object");
886 // In the pre-loading state, it can be looked up in the
887 // cache only by this thread! This allows the super-class
888 // to include references to this class.
890 def->state = JV_STATE_PRELOADING;
891 _Jv_RegisterClass (def);
893 if (super_class != 0)
895 // load the super class
896 check_tag (super_class, JV_CONSTANT_Class);
897 _Jv_Utf8Const* super_name =
898 (_Jv_Utf8Const*)pool_data[super_class];
900 // load the super class using our defining loader
901 jclass the_super = _Jv_FindClass (super_name,
904 // This will establish that we are allowed to be a subclass,
905 // and check for class circularity error
906 checkExtends (def, the_super);
908 def->superclass = the_super;
909 pool_data[super_class] = (void*) the_super;
910 pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
913 // now we've come past the circularity problem, we can
914 // now say that we're loading...
916 def->state = JV_STATE_LOADING;
920 ///// implements the checks described in sect. 5.3.5.3
922 _Jv_ClassReader::checkExtends (jclass sub, jclass super)
924 // having an interface or a final class as a superclass is no good
925 if ((super->accflags & (INTERFACE | FINAL)) != 0)
927 throw_incompatible_class_change_error (sub->getName ());
930 // if the super class is not public, we need to check some more
931 if ((super->accflags & PUBLIC) == 0)
933 // With package scope, the classes must have the same
935 if ( sub->loader != super->loader
936 || !_Jv_ClassNameSamePackage (sub->name, super->name))
938 throw_incompatible_class_change_error (sub->getName ());
942 for (; super != 0; super = super->superclass)
945 throw_class_circularity_error (sub->getName ());
951 void _Jv_ClassReader::handleInterfacesBegin (int count)
953 def->interfaces = (jclass*) _Jv_AllocBytesChecked (count*sizeof (jclass));
954 def->interface_count = count;
957 void _Jv_ClassReader::handleInterface (int if_number, int offset)
959 void ** pool_data = def->constants.data;
960 unsigned char * pool_tags = (unsigned char*) def->constants.tags;
962 jclass the_interface;
964 if (pool_tags[offset] == JV_CONSTANT_Class)
966 _Jv_Utf8Const* name = (_Jv_Utf8Const*) pool_data[offset];
967 the_interface = _Jv_FindClass (name, def->loader);
969 else if (pool_tags[offset] == JV_CONSTANT_ResolvedClass)
971 the_interface = (jclass)pool_data[offset];
975 throw_no_class_def_found_error ("erroneous constant pool tag");
978 // checks the validity of the_interface, and that we are in fact
979 // allowed to implement that interface.
980 checkImplements (def, the_interface);
982 pool_data[offset] = (void*)the_interface;
983 pool_tags[offset] = JV_CONSTANT_ResolvedClass;
985 def->interfaces[if_number] = the_interface;
989 _Jv_ClassReader::checkImplements (jclass sub, jclass super)
991 // well, it *must* be an interface
992 if ((super->accflags & INTERFACE) == 0)
994 throw_incompatible_class_change_error (sub->getName ());
997 // if it has package scope, it must also be defined by the
999 if ((super->accflags & PUBLIC) == 0)
1001 if ( sub->loader != super->loader
1002 || !_Jv_ClassNameSamePackage (sub->name, super->name))
1004 throw_incompatible_class_change_error (sub->getName ());
1008 // FIXME: add interface circularity check here
1011 throw_class_circularity_error (sub->getName ());
1015 void _Jv_ClassReader::handleFieldsBegin (int count)
1017 def->fields = (_Jv_Field*)
1018 _Jv_AllocBytesChecked (count * sizeof (_Jv_Field));
1019 def->field_count = count;
1020 def->field_initializers = (_Jv_ushort*)
1021 _Jv_AllocBytesChecked (count * sizeof (_Jv_ushort));
1022 for (int i = 0; i < count; i++)
1023 def->field_initializers[i] = (_Jv_ushort) 0;
1026 void _Jv_ClassReader::handleField (int field_no,
1031 void **const pool_data = def->constants.data;
1033 _Jv_Field *field = &def->fields[field_no];
1034 _Jv_Utf8Const *field_name = (_Jv_Utf8Const*) pool_data[name];
1036 #ifndef COMPACT_FIELDS
1037 field->name = field_name;
1039 field->nameIndex = name;
1043 _Jv_VerifyIdentifier (field_name);
1045 // ignore flags we don't know about.
1046 field->flags = flags & ALL_FLAGS;
1050 if (field->flags & (SYNCHRONIZED|NATIVE|INTERFACE|ABSTRACT))
1051 throw_class_format_error ("erroneous field access flags");
1053 if (1 < ( ((field->flags & PUBLIC) ? 1 : 0)
1054 +((field->flags & PRIVATE) ? 1 : 0)
1055 +((field->flags & PROTECTED) ? 1 : 0)))
1056 throw_class_format_error ("erroneous field access flags");
1059 _Jv_Utf8Const* sig = (_Jv_Utf8Const*) pool_data[desc];
1062 _Jv_VerifyFieldSignature (sig);
1064 // field->type is really a jclass, but while it is still
1065 // unresolved we keep an _Jv_Utf8Const* instead.
1066 field->type = (jclass) sig;
1067 field->flags |= _Jv_FIELD_UNRESOLVED_FLAG;
1068 field->u.boffset = 0;
1072 void _Jv_ClassReader::handleConstantValueAttribute (int field_index,
1075 _Jv_Field *field = &def->fields[field_index];
1077 if ((field->flags & (STATIC|FINAL|PRIVATE)) == 0)
1079 // Ignore, as per vmspec #4.7.2
1083 // do not allow multiple constant fields!
1084 if (field->flags & _Jv_FIELD_CONSTANT_VALUE)
1085 throw_class_format_error ("field has multiple ConstantValue attributes");
1087 field->flags |= _Jv_FIELD_CONSTANT_VALUE;
1088 def->field_initializers[field_index] = value;
1090 /* type check the initializer */
1092 if (value <= 0 || value >= pool_count)
1093 throw_class_format_error ("erroneous ConstantValue attribute");
1095 /* FIXME: do the rest */
1098 void _Jv_ClassReader::handleFieldsEnd ()
1100 // We need to reorganize the fields so that the static ones are first,
1101 // to conform to GCJ class layout.
1104 int high = def->field_count-1;
1105 _Jv_Field *fields = def->fields;
1106 _Jv_ushort *inits = def->field_initializers;
1108 // this is kind of a raw version of quicksort.
1111 // go forward on low, while it's a static
1112 while (low < high && (fields[low].flags & STATIC) != 0)
1115 // go backwards on high, while it's a non-static
1116 while (low < high && (fields[high].flags & STATIC) == 0)
1122 _Jv_Field tmp = fields[low];
1123 _Jv_ushort itmp = inits[low];
1125 fields[low] = fields[high];
1126 inits[low] = inits[high];
1135 if ((fields[low].flags & STATIC) != 0)
1138 def->static_field_count = low;
1143 void _Jv_ClassReader::handleMethodsBegin (int count)
1145 def->methods = (_Jv_Method*)
1146 _Jv_AllocBytesChecked (sizeof (_Jv_Method)*count);
1148 def->interpreted_methods = (_Jv_InterpMethod**)
1149 _Jv_AllocBytesChecked (sizeof (_Jv_InterpMethod*) * count);
1151 for (int i = 0; i < count; i++)
1152 def->interpreted_methods[i] = 0;
1154 def->method_count = count;
1158 void _Jv_ClassReader::handleMethod
1159 (int mth_index, int accflags, int name, int desc)
1161 void **const pool_data = def->constants.data;
1162 _Jv_Method *method = &def->methods[mth_index];
1164 check_tag (name, JV_CONSTANT_Utf8);
1165 prepare_pool_entry (name, JV_CONSTANT_Utf8);
1166 method->name = (_Jv_Utf8Const*)pool_data[name];
1168 check_tag (desc, JV_CONSTANT_Utf8);
1169 prepare_pool_entry (desc, JV_CONSTANT_Utf8);
1170 method->signature = (_Jv_Utf8Const*)pool_data[desc];
1172 // ignore unknown flags
1173 method->accflags = accflags & ALL_FLAGS;
1180 if (_Jv_equalUtf8Consts (method->name, clinit_name)
1181 || _Jv_equalUtf8Consts (method->name, init_name))
1184 _Jv_VerifyIdentifier (method->name);
1186 _Jv_VerifyMethodSignature (method->signature);
1188 if (method->accflags & (VOLATILE|TRANSIENT|INTERFACE))
1189 throw_class_format_error ("erroneous method access flags");
1191 if (1 < ( ((method->accflags & PUBLIC) ? 1 : 0)
1192 +((method->accflags & PRIVATE) ? 1 : 0)
1193 +((method->accflags & PROTECTED) ? 1 : 0)))
1194 throw_class_format_error ("erroneous method access flags");
1198 void _Jv_ClassReader::handleCodeAttribute
1199 (int method_index, int max_stack, int max_locals,
1200 int code_start, int code_length, int exc_table_length)
1202 int size = _Jv_InterpMethod::size (exc_table_length, code_length);
1203 _Jv_InterpMethod *method =
1204 (_Jv_InterpMethod*) (_Jv_AllocBytesChecked (size));
1206 method->max_stack = max_stack;
1207 method->max_locals = max_locals;
1208 method->code_length = code_length;
1209 method->exc_count = exc_table_length;
1210 method->defining_class = def;
1211 method->self = &def->methods[method_index];
1213 // grab the byte code!
1214 memcpy ((void*) method->bytecode (),
1215 (void*) (bytes+code_start),
1218 def->interpreted_methods[method_index] = method;
1220 /* that's all we do for now */
1223 void _Jv_ClassReader::handleExceptionTableEntry
1224 (int method_index, int exc_index,
1225 int start_pc, int end_pc, int handler_pc, int catch_type)
1227 _Jv_InterpMethod *method = def->interpreted_methods[method_index];
1228 _Jv_InterpException *exc = method->exceptions ();
1230 exc[exc_index].start_pc = start_pc;
1231 exc[exc_index].end_pc = end_pc;
1232 exc[exc_index].handler_pc = handler_pc;
1233 exc[exc_index].handler_type = catch_type;
1236 void _Jv_ClassReader::handleMethodsEnd ()
1238 for (int i = 0; i < def->method_count; i++)
1240 _Jv_Method *method = &def->methods[i];
1241 if (method->accflags & (NATIVE|ABSTRACT))
1243 if (def->interpreted_methods[i] != 0)
1244 throw_class_format_error ("code provided "
1245 "for abstract or native method");
1249 if (def->interpreted_methods[i] == 0)
1250 throw_class_format_error ("abstract or native method "
1258 /** This section takes care of verifying integrity of identifiers,
1259 signatures, field ddescriptors, and class names */
1261 #define UTF8_PEEK(PTR, LIMIT) \
1262 ({ unsigned char* xxkeep = (PTR); \
1263 int xxch = UTF8_GET(PTR,LIMIT); \
1264 PTR = xxkeep; xxch; })
1266 /* verify one element of a type descriptor or signature */
1267 static unsigned char*
1268 _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
1273 int ch = UTF8_GET (ptr, limit);
1278 if (! void_ok) return 0;
1280 case 'S': case 'B': case 'I': case 'J':
1281 case 'Z': case 'C': case 'F': case 'D':
1286 unsigned char *start = ptr, *end;
1293 if ((ch = UTF8_GET (ptr, limit)) == -1)
1296 } while (ch != ';');
1297 _Jv_VerifyClassName (start, (unsigned short) (end-start));
1302 return _Jv_VerifyOne (ptr, limit, false);
1314 /** verification and loading procedures **/
1317 _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
1319 unsigned char* ptr = (unsigned char*) sig->data;
1320 unsigned char* limit = ptr + sig->length;
1322 ptr = _Jv_VerifyOne (ptr, limit, false);
1325 throw_class_format_error ("erroneous type descriptor");
1329 _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
1331 unsigned char* ptr = (unsigned char*) sig->data;
1332 unsigned char* limit = ptr + sig->length;
1335 throw_class_format_error ("erroneous type descriptor");
1337 if (UTF8_GET(ptr,limit) != '(')
1338 throw_class_format_error ("erroneous type descriptor");
1340 while (ptr && UTF8_PEEK (ptr, limit) != ')')
1341 ptr = _Jv_VerifyOne (ptr, limit, false);
1343 if (UTF8_GET (ptr, limit) != ')')
1344 throw_class_format_error ("erroneous type descriptor");
1346 // get the return type
1347 ptr = _Jv_VerifyOne (ptr, limit, true);
1350 throw_class_format_error ("erroneous type descriptor");
1356 /* we try to avoid calling the Character methods all the time,
1357 in fact, they will only be called for non-standard things */
1359 static __inline__ int
1360 is_identifier_start (int c)
1362 unsigned int ch = (unsigned)c;
1364 if ((ch - 0x41U) < 29U) /* A ... Z */
1366 if ((ch - 0x61U) < 29U) /* a ... z */
1368 if (ch == 0x5FU) /* _ */
1371 return character->isJavaIdentifierStart ((jchar) ch);
1374 static __inline__ int
1375 is_identifier_part (int c)
1377 unsigned int ch = (unsigned)c;
1379 if ((ch - 0x41U) < 29U) /* A ... Z */
1381 if ((ch - 0x61U) < 29U) /* a ... z */
1383 if ((ch - 0x30) < 10U) /* 0 .. 9 */
1385 if (ch == 0x5FU || ch == 0x24U) /* _ $ */
1388 return character->isJavaIdentifierStart ((jchar) ch);
1392 _Jv_VerifyIdentifier (_Jv_Utf8Const* name)
1394 unsigned char *ptr = (unsigned char*) name->data;
1395 unsigned char *limit = ptr + name->length;
1398 if ((ch = UTF8_GET (ptr, limit))==-1
1399 || ! is_identifier_start (ch))
1400 throw_class_format_error ("erroneous identifier");
1402 while (ptr != limit)
1404 if ((ch = UTF8_GET (ptr, limit))==-1
1405 || ! is_identifier_part (ch))
1406 throw_class_format_error ("erroneous identifier");
1412 _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
1414 unsigned char *limit = ptr+length;
1417 if ('[' == UTF8_PEEK (ptr, limit))
1419 if (! _Jv_VerifyOne (++ptr, limit, false))
1420 throw_class_format_error ("erroneous class name");
1427 if ((ch = UTF8_GET (ptr, limit))==-1)
1428 throw_class_format_error ("erroneous class name");
1429 if (! is_identifier_start (ch))
1430 throw_class_format_error ("erroneous class name");
1434 else if ((ch = UTF8_GET (ptr, limit))==-1)
1435 throw_class_format_error ("erroneous class name");
1438 else if (! is_identifier_part (ch))
1439 throw_class_format_error ("erroneous class name");
1446 _Jv_VerifyClassName (_Jv_Utf8Const *name)
1448 _Jv_VerifyClassName ((unsigned char*)&name->data[0],
1449 (_Jv_ushort) name->length);
1453 /** returns true, if name1 and name2 represents classes in the same
1457 _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
1459 unsigned char* ptr1 = (unsigned char*) name1->data;
1460 unsigned char* limit1 = ptr1 + name1->length;
1462 unsigned char* last1 = ptr1;
1464 // scan name1, and find the last occurrence of '.'
1465 while (ptr1 < limit1) {
1466 int ch1 = UTF8_GET (ptr1, limit1);
1475 // now the length of name1's package name is len
1476 int len = last1 - (unsigned char*) name1->data;
1478 // if this is longer than name2, then we're off
1479 if (len > name2->length)
1482 // then compare the first len bytes for equality
1483 if (memcmp ((void*) name1->data, (void*) name2->data, len) == 0)
1485 // check that there are no .'s after position len in name2
1487 unsigned char* ptr2 = (unsigned char*) name2->data + len;
1488 unsigned char* limit2 =
1489 (unsigned char*) name2->data + name2->length;
1491 while (ptr2 < limit2)
1493 int ch2 = UTF8_GET (ptr2, limit2);
1494 if (ch2 == -1 || ch2 == '.')
1504 /** Here we define the exceptions that can be thrown */
1507 throw_no_class_def_found_error (jstring msg)
1510 JvThrow (new java::lang::NoClassDefFoundError);
1512 JvThrow (new java::lang::NoClassDefFoundError (msg));
1516 throw_no_class_def_found_error (char *msg)
1518 throw_no_class_def_found_error (JvNewStringLatin1 (msg));
1522 throw_class_format_error (jstring msg)
1525 JvThrow (new java::lang::ClassFormatError);
1527 JvThrow (new java::lang::ClassFormatError (msg));
1531 throw_class_format_error (char *msg)
1533 throw_class_format_error (JvNewStringLatin1 (msg));
1537 throw_internal_error (char *msg)
1540 (new java::lang::InternalError (JvNewStringLatin1 (msg)));
1543 static jfloat int_bits_to_float (jint value)
1545 return java::lang::Float::intBitsToFloat (value);
1548 static jdouble long_bits_to_double (jlong value)
1550 return java::lang::Double::longBitsToDouble (value);
1553 static void throw_incompatible_class_change_error (jstring msg)
1555 JvThrow (new java::lang::IncompatibleClassChangeError (msg));
1558 static void throw_class_circularity_error (jstring msg)
1560 JvThrow (new java::lang::ClassCircularityError (msg));
1563 #endif /* INTERPRETER */