1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
13 Author: Kresten Krab Thorup <krab@gnu.org>
15 Written using the online versions of Java Language Specification (1st
16 ed.) and The Java Virtual Machine Specification (2nd ed.).
18 Future work may include reading (and handling) attributes which are
19 currently being ignored ("InnerClasses", "LineNumber", etc...).
24 #include <java-interp.h>
28 #include <java-cpool.h>
30 #include <execution.h>
32 #include <java/lang/Class.h>
33 #include <java/lang/Float.h>
34 #include <java/lang/Double.h>
35 #include <java/lang/Character.h>
36 #include <java/lang/LinkageError.h>
37 #include <java/lang/InternalError.h>
38 #include <java/lang/ClassFormatError.h>
39 #include <java/lang/NoClassDefFoundError.h>
40 #include <java/lang/ClassCircularityError.h>
41 #include <java/lang/IncompatibleClassChangeError.h>
42 #include <java/lang/reflect/Modifier.h>
43 #include <java/lang/reflect/Field.h>
44 #include <java/lang/reflect/Method.h>
45 #include <java/security/ProtectionDomain.h>
46 #include <java/io/DataOutputStream.h>
47 #include <java/io/ByteArrayOutputStream.h>
53 // these go in some separate functions, to avoid having _Jv_InitClass
54 // inserted all over the place.
55 static void throw_internal_error (const char *msg
)
56 __attribute__ ((__noreturn__
));
57 static void throw_no_class_def_found_error (jstring msg
)
58 __attribute__ ((__noreturn__
));
59 static void throw_no_class_def_found_error (const char *msg
)
60 __attribute__ ((__noreturn__
));
61 static void throw_class_format_error (jstring 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__
));
69 * We define class reading using a class. It is practical, since then
70 * the entire class-reader can be a friend of class Class (it needs to
71 * write all it's different structures); but also because this makes it
72 * easy to make class definition reentrant, and thus two threads can be
73 * defining classes at the same time. This class (_Jv_ClassReader) is
74 * never exposed outside this file, so we don't have to worry about
75 * public or private members here.
78 struct _Jv_ClassReader
81 // do verification? Currently, there is no option to disable this.
82 // This flag just controls the verificaiton done by the class loader;
83 // i.e., checking the integrity of the constant pool; and it is
84 // allways on. You always want this as far as I can see, but it also
85 // controls weither identifiers and type descriptors/signatures are
86 // verified as legal. This could be somewhat more expensive since it
87 // will call Character.isJavaIdentifier{Start,Part} for each character
88 // in any identifier (field name or method name) it comes by. Thus,
89 // it might be useful to turn off this verification for classes that
90 // come from a trusted source. However, for GCJ, trusted classes are
91 // most likely to be linked in.
95 // original input data.
96 jbyteArray input_data
;
100 unsigned char *bytes
;
103 // current input position
106 // the constant pool data
109 unsigned int *offsets
;
111 // the class to define (see java-interp.h)
114 // the classes associated interpreter data.
115 _Jv_InterpClass
*def_interp
;
117 // The name we found.
118 _Jv_Utf8Const
**found_name
;
120 // True if this is a 1.5 class file.
123 // Buffer holding extra reflection data.
124 ::java::io::ByteArrayOutputStream
*reflection_data
;
125 ::java::io::DataOutputStream
*data_stream
;
128 /* check that the given number of input bytes are available */
129 inline void check (int num
)
132 throw_class_format_error ("Premature end of data");
135 /* skip a given number of bytes in input */
136 inline void skip (int num
)
142 /* read an unsigned 1-byte unit */
143 inline static jint
get1u (unsigned char* bytes
)
148 /* read an unsigned 1-byte unit */
149 inline jint
read1u ()
152 return get1u (bytes
+pos
-1);
155 /* read an unsigned 2-byte unit */
156 inline static jint
get2u (unsigned char *bytes
)
158 return (((jint
)bytes
[0]) << 8) | ((jint
)bytes
[1]);
161 /* read an unsigned 2-byte unit */
162 inline jint
read2u ()
165 return get2u (bytes
+pos
-2);
168 /* read a 4-byte unit */
169 static jint
get4 (unsigned char *bytes
)
171 return (((jint
)bytes
[0]) << 24)
172 | (((jint
)bytes
[1]) << 16)
173 | (((jint
)bytes
[2]) << 8)
174 | (((jint
)bytes
[3]) << 0);
177 /* read a 4-byte unit, (we don't do that quite so often) */
181 return get4 (bytes
+pos
-4);
184 /* read a 8-byte unit */
185 static jlong
get8 (unsigned char* bytes
)
187 return (((jlong
)bytes
[0]) << 56)
188 | (((jlong
)bytes
[1]) << 48)
189 | (((jlong
)bytes
[2]) << 40)
190 | (((jlong
)bytes
[3]) << 32)
191 | (((jlong
)bytes
[4]) << 24)
192 | (((jlong
)bytes
[5]) << 16)
193 | (((jlong
)bytes
[6]) << 8)
194 | (((jlong
)bytes
[7]) << 0);
197 /* read a 8-byte unit */
198 inline jlong
read8 ()
201 return get8 (bytes
+pos
-8);
204 inline void check_tag (int index
, char expected_tag
)
207 || index
> pool_count
208 || tags
[index
] != expected_tag
)
209 throw_class_format_error ("erroneous constant pool tag");
212 inline void verify_identifier (_Jv_Utf8Const
* name
)
214 if (! _Jv_VerifyIdentifier (name
))
215 throw_class_format_error ("erroneous identifier");
218 inline void verify_classname (unsigned char* ptr
, _Jv_ushort length
)
220 if (! _Jv_VerifyClassName (ptr
, length
))
221 throw_class_format_error ("erroneous class name");
224 inline void verify_classname (_Jv_Utf8Const
*name
)
226 if (! _Jv_VerifyClassName (name
))
227 throw_class_format_error ("erroneous class name");
230 inline void verify_field_signature (_Jv_Utf8Const
*sig
)
232 if (! _Jv_VerifyFieldSignature (sig
))
233 throw_class_format_error ("erroneous type descriptor");
236 inline void verify_method_signature (_Jv_Utf8Const
*sig
)
238 if (! _Jv_VerifyMethodSignature (sig
))
239 throw_class_format_error ("erroneous type descriptor");
242 ::java::io::DataOutputStream
*get_reflection_stream ()
244 if (reflection_data
== NULL
)
246 reflection_data
= new ::java::io::ByteArrayOutputStream();
247 data_stream
= new ::java::io::DataOutputStream(reflection_data
);
252 _Jv_ClassReader (jclass klass
, jbyteArray data
, jint offset
, jint length
,
253 java::security::ProtectionDomain
*pd
,
254 _Jv_Utf8Const
**name_result
)
256 if (klass
== 0 || length
< 0 || offset
+length
> data
->length
)
257 throw_internal_error ("arguments to _Jv_DefineClass");
261 input_offset
= offset
;
262 bytes
= (unsigned char*) (elements (data
)+offset
);
268 found_name
= name_result
;
269 reflection_data
= NULL
;
272 def
->size_in_bytes
= -1;
273 def
->vtable_method_count
= -1;
274 def
->engine
= &_Jv_soleInterpreterEngine
;
275 def
->protectionDomain
= pd
;
278 /** and here goes the parser members defined out-of-line */
280 void read_constpool ();
281 void prepare_pool_entry (int index
, unsigned char tag
,
282 bool rewrite
= true);
284 void read_methods ();
285 void read_one_class_attribute ();
286 void read_one_method_attribute (int method
);
287 void read_one_code_attribute (int method
);
288 void read_one_field_attribute (int field
, bool *);
289 void throw_class_format_error (const char *msg
);
291 void handleEnclosingMethod(int);
292 void handleGenericSignature(jv_attr_type
, unsigned short, int);
293 void handleAnnotationElement();
294 void handleAnnotation();
295 void handleAnnotations();
296 void handleMemberAnnotations(jv_attr_type
, int, int);
297 void handleAnnotationDefault(int, int);
298 void handleParameterAnnotations(int, int);
299 void finish_reflection_data ();
301 /** check an utf8 entry, without creating a Utf8Const object */
302 bool is_attribute_name (int index
, const char *name
);
304 /** return the value of a utf8 entry in the passed array */
305 int pool_Utf8_to_char_arr (int index
, char **entry
);
307 /** here goes the class-loader members defined out-of-line */
308 void handleConstantPool ();
309 void handleClassBegin (int, int, int);
310 void handleInterfacesBegin (int);
311 void handleInterface (int, int);
312 void handleFieldsBegin (int);
313 void handleField (int, int, int, int, int *);
314 void handleConstantValueAttribute (int, int, bool *);
315 void handleMethodsBegin (int);
316 void handleMethod (int, int, int, int);
317 void handleMethodsEnd ();
318 void handleCodeAttribute (int, int, int, int, int, int);
319 void handleExceptionTableEntry (int, int, int, int, int, int);
321 void checkExtends (jclass sub
, jclass super
);
322 void checkImplements (jclass sub
, jclass super
);
325 * FIXME: we should keep a hash table of utf8-strings, since many will
326 * be the same. It's a little tricky, however, because the hash table
327 * needs to interact gracefully with the garbage collector. Much
328 * memory is to be saved by this, however! perhaps the improvement
329 * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
330 * computes the hash value anyway.
334 // Note that *NAME_RESULT will only be set if the class is registered
335 // with the class loader. This is how the caller can know whether
336 // unregistration is require.
338 _Jv_DefineClass (jclass klass
, jbyteArray data
, jint offset
, jint length
,
339 java::security::ProtectionDomain
*pd
,
340 _Jv_Utf8Const
**name_result
)
342 _Jv_ClassReader
reader (klass
, data
, offset
, length
, pd
, name_result
);
349 /** This section defines the parsing/scanning of the class data */
351 // Major and minor version numbers for various releases.
366 _Jv_ClassReader::parse ()
368 int magic
= read4 ();
369 if (magic
!= (int) 0xCAFEBABE)
370 throw_class_format_error ("bad magic number");
372 int minor_version
= read2u ();
373 int major_version
= read2u ();
374 if (major_version
< MAJOR_1_1
|| major_version
> MAJOR_1_6
375 || (major_version
== MAJOR_1_6
&& minor_version
> MINOR_1_6
))
376 throw_class_format_error ("unrecognized class file version");
377 is_15
= (major_version
>= MAJOR_1_5
);
379 pool_count
= read2u ();
383 int access_flags
= read2u ();
384 int this_class
= read2u ();
385 int super_class
= read2u ();
387 check_tag (this_class
, JV_CONSTANT_Class
);
388 if (super_class
!= 0)
389 check_tag (super_class
, JV_CONSTANT_Class
);
391 handleClassBegin (access_flags
, this_class
, super_class
);
393 // Allocate our aux_info here, after the name is set, to fulfill our
394 // contract with the collector interface.
395 def
->aux_info
= (void *) _Jv_AllocRawObj (sizeof (_Jv_InterpClass
));
396 def_interp
= (_Jv_InterpClass
*) def
->aux_info
;
398 int interfaces_count
= read2u ();
400 handleInterfacesBegin (interfaces_count
);
402 for (int i
= 0; i
< interfaces_count
; i
++)
404 int iface
= read2u ();
405 check_tag (iface
, JV_CONSTANT_Class
);
406 handleInterface (i
, iface
);
412 int attributes_count
= read2u ();
414 for (int i
= 0; i
< attributes_count
; i
++)
416 read_one_class_attribute ();
420 throw_class_format_error ("unused data before end of file");
422 finish_reflection_data ();
424 // Tell everyone we're done.
425 def
->state
= JV_STATE_READ
;
426 if (gcj::verbose_class_flag
)
427 _Jv_Linker::print_class_loaded (def
);
428 ++gcj::loadedClasses
;
433 _Jv_ClassReader::finish_reflection_data ()
435 if (data_stream
== NULL
)
437 data_stream
->writeByte(JV_DONE_ATTR
);
438 data_stream
->flush();
439 int nbytes
= reflection_data
->count
;
440 unsigned char *new_bytes
= (unsigned char *) _Jv_AllocBytes (nbytes
);
441 memcpy (new_bytes
, elements (reflection_data
->buf
), nbytes
);
442 def
->reflection_data
= new_bytes
;
446 _Jv_ClassReader::handleEnclosingMethod (int len
)
449 throw_class_format_error ("invalid EnclosingMethod attribute");
450 // FIXME: only allow one...
452 int class_index
= read2u();
453 check_tag (class_index
, JV_CONSTANT_Class
);
454 prepare_pool_entry (class_index
, JV_CONSTANT_Class
);
456 int method_index
= read2u();
457 // Zero is ok and means no enclosing method.
458 if (method_index
!= 0)
460 check_tag (method_index
, JV_CONSTANT_NameAndType
);
461 prepare_pool_entry (method_index
, JV_CONSTANT_NameAndType
);
464 ::java::io::DataOutputStream
*stream
= get_reflection_stream ();
465 stream
->writeByte(JV_CLASS_ATTR
);
467 stream
->writeByte(JV_ENCLOSING_METHOD_KIND
);
468 stream
->writeShort(class_index
);
469 stream
->writeShort(method_index
);
473 _Jv_ClassReader::handleGenericSignature (jv_attr_type type
,
474 unsigned short index
,
478 throw_class_format_error ("invalid Signature attribute");
480 int cpool_idx
= read2u();
481 check_tag (cpool_idx
, JV_CONSTANT_Utf8
);
482 prepare_pool_entry (cpool_idx
, JV_CONSTANT_Utf8
, false);
484 ::java::io::DataOutputStream
*stream
= get_reflection_stream ();
485 stream
->writeByte(type
);
487 if (type
!= JV_CLASS_ATTR
)
489 stream
->writeInt(attrlen
);
490 if (type
!= JV_CLASS_ATTR
)
491 stream
->writeShort(index
);
492 stream
->writeByte(JV_SIGNATURE_KIND
);
493 stream
->writeShort(cpool_idx
);
497 _Jv_ClassReader::handleAnnotationElement()
508 int index
= read2u();
509 check_tag (index
, JV_CONSTANT_Integer
);
510 prepare_pool_entry (index
, JV_CONSTANT_Integer
);
515 int index
= read2u();
516 check_tag (index
, JV_CONSTANT_Double
);
517 prepare_pool_entry (index
, JV_CONSTANT_Double
);
522 int index
= read2u();
523 check_tag (index
, JV_CONSTANT_Float
);
524 prepare_pool_entry (index
, JV_CONSTANT_Float
);
529 int index
= read2u();
530 check_tag (index
, JV_CONSTANT_Long
);
531 prepare_pool_entry (index
, JV_CONSTANT_Long
);
536 int index
= read2u();
537 // Despite what the JVM spec says, compilers generate a Utf8
538 // constant here, not a String.
539 check_tag (index
, JV_CONSTANT_Utf8
);
540 prepare_pool_entry (index
, JV_CONSTANT_Utf8
, false);
546 int type_name_index
= read2u();
547 int const_name_index
= read2u ();
548 check_tag (type_name_index
, JV_CONSTANT_Utf8
);
549 prepare_pool_entry (type_name_index
, JV_CONSTANT_Utf8
);
550 check_tag (const_name_index
, JV_CONSTANT_Utf8
);
551 prepare_pool_entry (const_name_index
, JV_CONSTANT_Utf8
, false);
556 int index
= read2u();
557 check_tag (index
, JV_CONSTANT_Utf8
);
558 prepare_pool_entry (index
, JV_CONSTANT_Utf8
);
566 int n_array_elts
= read2u ();
567 for (int i
= 0; i
< n_array_elts
; ++i
)
568 handleAnnotationElement();
572 throw_class_format_error ("invalid annotation element");
577 _Jv_ClassReader::handleAnnotation()
579 int type_index
= read2u();
580 check_tag (type_index
, JV_CONSTANT_Utf8
);
581 prepare_pool_entry (type_index
, JV_CONSTANT_Utf8
);
583 int npairs
= read2u();
584 for (int i
= 0; i
< npairs
; ++i
)
586 int name_index
= read2u();
587 check_tag (name_index
, JV_CONSTANT_Utf8
);
588 prepare_pool_entry (name_index
, JV_CONSTANT_Utf8
, false);
589 handleAnnotationElement();
594 _Jv_ClassReader::handleAnnotations()
602 _Jv_ClassReader::handleMemberAnnotations(jv_attr_type member_type
,
606 // We're going to copy the bytes in verbatim. But first we want to
607 // make sure the attribute is well-formed, and we want to prepare
608 // the constant pool. So, we save our starting point.
612 // FIXME: check that we read all LEN bytes?
614 ::java::io::DataOutputStream
*stream
= get_reflection_stream ();
615 stream
->writeByte(member_type
);
616 int newLen
= len
+ 1;
617 if (member_type
!= JV_CLASS_ATTR
)
619 stream
->writeInt(newLen
);
620 stream
->writeByte(JV_ANNOTATIONS_KIND
);
621 if (member_type
!= JV_CLASS_ATTR
)
622 stream
->writeShort(member_index
);
623 // Write the data as-is.
624 stream
->write(input_data
, input_offset
+ orig_pos
, len
);
628 _Jv_ClassReader::handleAnnotationDefault(int member_index
, int len
)
631 handleAnnotationElement();
633 ::java::io::DataOutputStream
*stream
= get_reflection_stream ();
634 stream
->writeByte(JV_METHOD_ATTR
);
635 stream
->writeInt(len
+ 3);
636 stream
->writeByte(JV_ANNOTATION_DEFAULT_KIND
);
637 stream
->writeShort(member_index
);
638 stream
->write(input_data
, input_offset
+ orig_pos
, len
);
642 _Jv_ClassReader::handleParameterAnnotations(int member_index
, int len
)
646 int n_params
= read1u();
647 for (int i
= 0; i
< n_params
; ++i
)
650 ::java::io::DataOutputStream
*stream
= get_reflection_stream ();
651 stream
->writeByte(JV_METHOD_ATTR
);
652 stream
->writeInt(len
+ 3);
653 stream
->writeByte(JV_PARAMETER_ANNOTATIONS_KIND
);
654 stream
->writeShort(member_index
);
655 stream
->write(input_data
, input_offset
+ orig_pos
, len
);
658 void _Jv_ClassReader::read_constpool ()
660 tags
= (unsigned char*) _Jv_AllocBytes (pool_count
);
661 offsets
= (unsigned int *) _Jv_AllocBytes (sizeof (int) * pool_count
) ;
663 /** first, we scan the constant pool, collecting tags and offsets */
664 tags
[0] = JV_CONSTANT_Undefined
;
666 for (int c
= 1; c
< pool_count
; c
++)
673 case JV_CONSTANT_String
:
674 case JV_CONSTANT_Class
:
678 case JV_CONSTANT_Fieldref
:
679 case JV_CONSTANT_Methodref
:
680 case JV_CONSTANT_InterfaceMethodref
:
681 case JV_CONSTANT_NameAndType
:
682 case JV_CONSTANT_Integer
:
683 case JV_CONSTANT_Float
:
687 case JV_CONSTANT_Double
:
688 case JV_CONSTANT_Long
:
690 tags
[++c
] = JV_CONSTANT_Undefined
;
693 case JV_CONSTANT_Utf8
:
700 case JV_CONSTANT_Unicode
:
701 throw_class_format_error ("unicode not supported");
705 throw_class_format_error ("erroneous constant pool tag");
709 handleConstantPool ();
713 void _Jv_ClassReader::read_fields ()
715 int fields_count
= read2u ();
716 handleFieldsBegin (fields_count
);
718 // We want to sort the fields so that static fields come first,
719 // followed by instance fields. We do this before parsing the
720 // fields so that we can have the new indices available when
721 // creating the annotation data structures.
723 // Allocate this on the heap in case there are a large number of
725 int *fieldmap
= (int *) _Jv_AllocBytes (fields_count
* sizeof (int));
727 int static_count
= 0, instance_count
= -1;
728 for (int i
= 0; i
< fields_count
; ++i
)
730 using namespace java::lang::reflect
;
732 int access_flags
= read2u ();
734 int attributes_count
= read2u ();
736 if ((access_flags
& Modifier::STATIC
) != 0)
737 fieldmap
[i
] = static_count
++;
739 fieldmap
[i
] = instance_count
--;
741 for (int j
= 0; j
< attributes_count
; ++j
)
744 int length
= read4 ();
750 // In the loop above, instance fields are represented by negative
751 // numbers. Here we rewrite these to be proper offsets.
752 for (int i
= 0; i
< fields_count
; ++i
)
755 fieldmap
[i
] = static_count
- 1 - fieldmap
[i
];
757 def
->static_field_count
= static_count
;
759 for (int i
= 0; i
< fields_count
; i
++)
761 int access_flags
= read2u ();
762 int name_index
= read2u ();
763 int descriptor_index
= read2u ();
764 int attributes_count
= read2u ();
766 check_tag (name_index
, JV_CONSTANT_Utf8
);
767 prepare_pool_entry (name_index
, JV_CONSTANT_Utf8
);
769 check_tag (descriptor_index
, JV_CONSTANT_Utf8
);
770 prepare_pool_entry (descriptor_index
, JV_CONSTANT_Utf8
);
772 handleField (i
, access_flags
, name_index
, descriptor_index
, fieldmap
);
774 bool found_value
= false;
775 for (int j
= 0; j
< attributes_count
; j
++)
777 read_one_field_attribute (fieldmap
[i
], &found_value
);
783 _Jv_ClassReader::is_attribute_name (int index
, const char *name
)
785 check_tag (index
, JV_CONSTANT_Utf8
);
786 int len
= get2u (bytes
+offsets
[index
]);
787 if (len
!= (int) strlen (name
))
790 return !memcmp (bytes
+offsets
[index
]+2, name
, len
);
793 // Get a UTF8 value from the constant pool and turn it into a garbage
794 // collected char array.
795 int _Jv_ClassReader::pool_Utf8_to_char_arr (int index
, char** entry
)
797 check_tag (index
, JV_CONSTANT_Utf8
);
798 int len
= get2u (bytes
+ offsets
[index
]);
799 *entry
= reinterpret_cast<char *> (_Jv_AllocBytes (len
+ 1));
800 (*entry
)[len
] = '\0';
801 memcpy (*entry
, bytes
+ offsets
[index
] + 2, len
);
805 void _Jv_ClassReader::read_one_field_attribute (int field_index
,
808 int name
= read2u ();
809 int length
= read4 ();
811 if (is_attribute_name (name
, "ConstantValue"))
817 && (tags
[cv
] == JV_CONSTANT_Integer
818 || tags
[cv
] == JV_CONSTANT_Float
819 || tags
[cv
] == JV_CONSTANT_Long
820 || tags
[cv
] == JV_CONSTANT_Double
821 || tags
[cv
] == JV_CONSTANT_String
))
823 handleConstantValueAttribute (field_index
, cv
, found_value
);
827 throw_class_format_error ("erroneous ConstantValue attribute");
831 throw_class_format_error ("erroneous ConstantValue attribute");
833 else if (is_attribute_name (name
, "Signature"))
834 handleGenericSignature(JV_FIELD_ATTR
, field_index
, length
);
835 else if (is_attribute_name (name
, "RuntimeVisibleAnnotations"))
836 handleMemberAnnotations(JV_FIELD_ATTR
, field_index
, length
);
841 void _Jv_ClassReader::read_methods ()
843 int methods_count
= read2u ();
845 handleMethodsBegin (methods_count
);
847 for (int i
= 0; i
< methods_count
; i
++)
849 int access_flags
= read2u ();
850 int name_index
= read2u ();
851 int descriptor_index
= read2u ();
852 int attributes_count
= read2u ();
854 check_tag (name_index
, JV_CONSTANT_Utf8
);
855 prepare_pool_entry (name_index
, JV_CONSTANT_Utf8
);
857 check_tag (descriptor_index
, JV_CONSTANT_Utf8
);
858 prepare_pool_entry (descriptor_index
, JV_CONSTANT_Utf8
);
860 handleMethod (i
, access_flags
, name_index
,
863 for (int j
= 0; j
< attributes_count
; j
++)
865 read_one_method_attribute (i
);
872 void _Jv_ClassReader::read_one_method_attribute (int method_index
)
874 int name
= read2u ();
875 int length
= read4 ();
877 if (is_attribute_name (name
, "Exceptions"))
879 _Jv_Method
*method
= reinterpret_cast<_Jv_Method
*>
880 (&def
->methods
[method_index
]);
881 if (method
->throws
!= NULL
)
882 throw_class_format_error ("only one Exceptions attribute allowed per method");
884 int num_exceptions
= read2u ();
885 _Jv_Utf8Const
**exceptions
=
886 (_Jv_Utf8Const
**) _Jv_AllocBytes ((num_exceptions
+ 1)
887 * sizeof (_Jv_Utf8Const
*));
890 _Jv_word
*pool_data
= def
->constants
.data
;
891 for (int i
= 0; i
< num_exceptions
; ++i
)
894 // JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
897 check_tag (ndx
, JV_CONSTANT_Class
);
898 exceptions
[out
++] = pool_data
[ndx
].utf8
;
901 exceptions
[out
] = NULL
;
902 method
->throws
= exceptions
;
905 else if (is_attribute_name (name
, "Code"))
908 int max_stack
= read2u ();
909 int max_locals
= read2u ();
910 int code_length
= read4 ();
912 int code_start
= pos
;
914 int exception_table_length
= read2u ();
916 handleCodeAttribute (method_index
,
917 max_stack
, max_locals
,
918 code_start
, code_length
,
919 exception_table_length
);
922 for (int i
= 0; i
< exception_table_length
; i
++)
924 int start_pc
= read2u ();
925 int end_pc
= read2u ();
926 int handler_pc
= read2u ();
927 int catch_type
= read2u ();
929 if (start_pc
> end_pc
931 // END_PC can be equal to CODE_LENGTH.
932 // See JVM Spec 4.7.4.
933 || end_pc
> code_length
934 || handler_pc
>= code_length
)
935 throw_class_format_error ("erroneous exception handler info");
937 if (! (tags
[catch_type
] == JV_CONSTANT_Class
938 || tags
[catch_type
] == 0))
940 throw_class_format_error ("erroneous exception handler info");
943 handleExceptionTableEntry (method_index
,
952 int attributes_count
= read2u ();
954 for (int i
= 0; i
< attributes_count
; i
++)
956 read_one_code_attribute (method_index
);
959 if ((pos
- start_off
) != length
)
960 throw_class_format_error ("code attribute too short");
962 else if (is_attribute_name (name
, "Signature"))
963 handleGenericSignature(JV_METHOD_ATTR
, method_index
, length
);
964 else if (is_attribute_name (name
, "RuntimeVisibleAnnotations"))
965 handleMemberAnnotations(JV_METHOD_ATTR
, method_index
, length
);
966 else if (is_attribute_name (name
, "RuntimeVisibleParameterAnnotations"))
967 handleParameterAnnotations(method_index
, length
);
968 else if (is_attribute_name (name
, "AnnotationDefault"))
969 handleAnnotationDefault(method_index
, length
);
972 /* ignore unknown attributes */
977 void _Jv_ClassReader::read_one_code_attribute (int method_index
)
979 int name
= read2u ();
980 int length
= read4 ();
981 if (is_attribute_name (name
, "LineNumberTable"))
983 _Jv_InterpMethod
*method
= reinterpret_cast<_Jv_InterpMethod
*>
984 (def_interp
->interpreted_methods
[method_index
]);
985 if (method
->line_table
!= NULL
)
986 throw_class_format_error ("Method already has LineNumberTable");
988 int table_len
= read2u ();
989 _Jv_LineTableEntry
* table
990 = (_Jv_LineTableEntry
*) _Jv_AllocBytes (table_len
991 * sizeof (_Jv_LineTableEntry
));
992 for (int i
= 0; i
< table_len
; i
++)
994 table
[i
].bytecode_pc
= read2u ();
995 table
[i
].line
= read2u ();
997 method
->line_table_len
= table_len
;
998 method
->line_table
= table
;
1000 else if (is_attribute_name (name
, "LocalVariableTable"))
1002 _Jv_InterpMethod
*method
= reinterpret_cast<_Jv_InterpMethod
*>
1003 (def_interp
->interpreted_methods
[method_index
]);
1004 if (method
->local_var_table
!= NULL
)
1005 throw_class_format_error ("Method already has LocalVariableTable");
1007 int table_len
= read2u ();
1008 _Jv_LocalVarTableEntry
*table
1009 = reinterpret_cast<_Jv_LocalVarTableEntry
*>
1010 (_Jv_AllocRawObj (table_len
* sizeof (_Jv_LocalVarTableEntry
)));
1012 for (int i
= 0; i
< table_len
; i
++)
1014 table
[i
].bytecode_pc
= read2u ();
1015 table
[i
].length
= read2u ();
1016 pool_Utf8_to_char_arr (read2u (), &table
[i
].name
);
1017 pool_Utf8_to_char_arr (read2u (), &table
[i
].descriptor
);
1018 table
[i
].slot
= read2u ();
1020 if (table
[i
].slot
> method
->max_locals
|| table
[i
].slot
< 0)
1021 throw_class_format_error ("Malformed Local Variable Table: Invalid Slot");
1024 method
->local_var_table_len
= table_len
;
1025 method
->local_var_table
= table
;
1029 /* ignore unknown code attributes */
1034 void _Jv_ClassReader::read_one_class_attribute ()
1036 int name
= read2u ();
1037 int length
= read4 ();
1038 if (is_attribute_name (name
, "SourceFile"))
1040 int source_index
= read2u ();
1041 check_tag (source_index
, JV_CONSTANT_Utf8
);
1042 prepare_pool_entry (source_index
, JV_CONSTANT_Utf8
, false);
1043 def_interp
->source_file_name
= _Jv_NewStringUtf8Const
1044 (def
->constants
.data
[source_index
].utf8
);
1046 else if (is_attribute_name (name
, "Signature"))
1047 handleGenericSignature(JV_CLASS_ATTR
, 0, length
);
1048 else if (is_attribute_name (name
, "EnclosingMethod"))
1049 handleEnclosingMethod(length
);
1050 else if (is_attribute_name (name
, "RuntimeVisibleAnnotations"))
1051 handleMemberAnnotations(JV_CLASS_ATTR
, 0, length
);
1052 else if (is_attribute_name (name
, "InnerClasses"))
1054 ::java::io::DataOutputStream
*stream
= get_reflection_stream ();
1055 stream
->writeByte(JV_CLASS_ATTR
);
1056 stream
->writeInt(length
+ 1);
1057 stream
->writeByte(JV_INNER_CLASSES_KIND
);
1058 stream
->write(input_data
, input_offset
+ pos
, length
);
1063 /* Currently, we ignore most class attributes. */
1071 /* this section defines the semantic actions of the parser */
1073 void _Jv_ClassReader::handleConstantPool ()
1075 /** now, we actually define the class' constant pool */
1077 jbyte
*pool_tags
= (jbyte
*) _Jv_AllocBytes (pool_count
);
1079 = (_Jv_word
*) _Jv_AllocRawObj (pool_count
* sizeof (_Jv_word
));
1081 def
->constants
.tags
= pool_tags
;
1082 def
->constants
.data
= pool_data
;
1083 def
->constants
.size
= pool_count
;
1085 // Here we make a pass to collect the strings! We do this, because
1086 // internally in the GCJ runtime, classes are encoded with .'s not /'s.
1087 // Therefore, we first collect the strings, and then translate the rest
1088 // of the utf8-entries (thus not representing strings) from /-notation
1090 for (int i
= 1; i
< pool_count
; i
++)
1092 if (tags
[i
] == JV_CONSTANT_String
)
1094 unsigned char* str_data
= bytes
+ offsets
[i
];
1095 int utf_index
= get2u (str_data
);
1096 check_tag (utf_index
, JV_CONSTANT_Utf8
);
1097 unsigned char *utf_data
= bytes
+ offsets
[utf_index
];
1098 int len
= get2u (utf_data
);
1099 pool_data
[i
].utf8
= _Jv_makeUtf8Const ((char*)(utf_data
+2), len
);
1100 pool_tags
[i
] = JV_CONSTANT_String
;
1104 pool_tags
[i
] = JV_CONSTANT_Undefined
;
1108 // and now, we scan everything else but strings & utf8-entries. This
1109 // leaves out those utf8-entries which are not used; which will be left
1110 // with a tag of JV_CONSTANT_Undefined in the class definition.
1111 for (int index
= 1; index
< pool_count
; index
++)
1113 switch (tags
[index
])
1115 case JV_CONSTANT_Undefined
:
1116 case JV_CONSTANT_String
:
1117 case JV_CONSTANT_Utf8
:
1121 prepare_pool_entry (index
, tags
[index
]);
1127 /* this is a recursive procedure, which will prepare pool entries as needed.
1128 Which is how we avoid initializing those entries which go unused.
1130 REWRITE is true iff this pool entry is the Utf8 representation of a
1131 class name or a signature.
1135 _Jv_ClassReader::prepare_pool_entry (int index
, unsigned char this_tag
,
1138 /* these two, pool_data and pool_tags, point into the class
1139 structure we are currently defining */
1141 unsigned char *pool_tags
= (unsigned char*) def
->constants
.tags
;
1142 _Jv_word
*pool_data
= def
->constants
.data
;
1144 /* this entry was already prepared */
1145 if (pool_tags
[index
] == this_tag
)
1148 /* this_data points to the constant-pool information for the current
1149 constant-pool entry */
1151 unsigned char *this_data
= bytes
+ offsets
[index
];
1155 case JV_CONSTANT_Utf8
:
1157 int len
= get2u (this_data
);
1158 char *s
= ((char*) this_data
)+2;
1159 pool_tags
[index
] = JV_CONSTANT_Utf8
;
1163 pool_data
[index
].utf8
= _Jv_makeUtf8Const (s
, len
);
1167 // If REWRITE is set, it is because some other tag needs this
1168 // utf8-entry for type information: it is a class or a
1169 // signature. Thus, we translate /'s to .'s in order to
1170 // accomondate gcj's internal representation.
1171 char *buffer
= (char*) __builtin_alloca (len
);
1172 for (int i
= 0; i
< len
; i
++)
1179 pool_data
[index
].utf8
= _Jv_makeUtf8Const (buffer
, len
);
1183 case JV_CONSTANT_Class
:
1185 int utf_index
= get2u (this_data
);
1186 check_tag (utf_index
, JV_CONSTANT_Utf8
);
1187 prepare_pool_entry (utf_index
, JV_CONSTANT_Utf8
);
1190 verify_classname (pool_data
[utf_index
].utf8
);
1192 pool_data
[index
].utf8
= pool_data
[utf_index
].utf8
;
1193 pool_tags
[index
] = JV_CONSTANT_Class
;
1197 case JV_CONSTANT_String
:
1198 // already handled before...
1201 case JV_CONSTANT_Fieldref
:
1202 case JV_CONSTANT_Methodref
:
1203 case JV_CONSTANT_InterfaceMethodref
:
1205 int class_index
= get2u (this_data
);
1206 int nat_index
= get2u (this_data
+2);
1208 check_tag (class_index
, JV_CONSTANT_Class
);
1209 prepare_pool_entry (class_index
, JV_CONSTANT_Class
);
1211 check_tag (nat_index
, JV_CONSTANT_NameAndType
);
1212 prepare_pool_entry (nat_index
, JV_CONSTANT_NameAndType
);
1214 // here, verify the signature and identifier name
1217 _Jv_ushort name_index
, type_index
;
1218 _Jv_loadIndexes (&pool_data
[nat_index
],
1219 name_index
, type_index
);
1221 if (this_tag
== JV_CONSTANT_Fieldref
)
1222 verify_field_signature (pool_data
[type_index
].utf8
);
1224 verify_method_signature (pool_data
[type_index
].utf8
);
1226 _Jv_Utf8Const
* name
= pool_data
[name_index
].utf8
;
1228 if (this_tag
!= JV_CONSTANT_Fieldref
1229 && ( _Jv_equalUtf8Consts (name
, clinit_name
)
1230 || _Jv_equalUtf8Consts (name
, init_name
)))
1233 verify_identifier (pool_data
[name_index
].utf8
);
1236 _Jv_storeIndexes (&pool_data
[index
], class_index
, nat_index
);
1237 pool_tags
[index
] = this_tag
;
1241 case JV_CONSTANT_NameAndType
:
1243 _Jv_ushort name_index
= get2u (this_data
);
1244 _Jv_ushort type_index
= get2u (this_data
+2);
1246 check_tag (name_index
, JV_CONSTANT_Utf8
);
1247 prepare_pool_entry (name_index
, JV_CONSTANT_Utf8
, false);
1248 check_tag (type_index
, JV_CONSTANT_Utf8
);
1249 prepare_pool_entry (type_index
, JV_CONSTANT_Utf8
);
1251 _Jv_storeIndexes (&pool_data
[index
], name_index
, type_index
);
1252 pool_tags
[index
] = JV_CONSTANT_NameAndType
;
1256 case JV_CONSTANT_Float
:
1258 jfloat f
= java::lang::Float::intBitsToFloat ((jint
) get4 (this_data
));
1259 _Jv_storeFloat (&pool_data
[index
], f
);
1260 pool_tags
[index
] = JV_CONSTANT_Float
;
1264 case JV_CONSTANT_Integer
:
1266 int i
= get4 (this_data
);
1267 _Jv_storeInt (&pool_data
[index
], i
);
1268 pool_tags
[index
] = JV_CONSTANT_Integer
;
1272 case JV_CONSTANT_Double
:
1275 = java::lang::Double::longBitsToDouble ((jlong
) get8 (this_data
));
1276 _Jv_storeDouble (&pool_data
[index
], d
);
1277 pool_tags
[index
] = JV_CONSTANT_Double
;
1281 case JV_CONSTANT_Long
:
1283 jlong i
= get8 (this_data
);
1284 _Jv_storeLong (&pool_data
[index
], i
);
1285 pool_tags
[index
] = JV_CONSTANT_Long
;
1290 throw_class_format_error ("erroneous constant pool tag");
1296 _Jv_ClassReader::handleClassBegin (int access_flags
, int this_class
, int super_class
)
1298 using namespace java::lang::reflect
;
1300 unsigned char *pool_tags
= (unsigned char*) def
->constants
.tags
;
1301 _Jv_word
*pool_data
= def
->constants
.data
;
1303 check_tag (this_class
, JV_CONSTANT_Class
);
1304 _Jv_Utf8Const
*loadedName
= pool_data
[this_class
].utf8
;
1306 // was ClassLoader.defineClass called with an expected class name?
1309 jclass orig
= def
->loader
->findLoadedClass(loadedName
->toString());
1313 def
->name
= loadedName
;
1317 jstring msg
= JvNewStringUTF ("anonymous "
1318 "class data denotes "
1320 msg
= msg
->concat (orig
->getName ());
1322 throw_no_class_def_found_error (msg
);
1326 // assert that the loaded class has the expected name, 5.3.5
1327 else if (! _Jv_equalUtf8Consts (loadedName
, def
->name
))
1329 jstring msg
= JvNewStringUTF ("loaded class ");
1330 msg
= msg
->concat (def
->getName ());
1331 msg
= msg
->concat (_Jv_NewStringUTF (" was in fact named "));
1332 jstring klass_name
= loadedName
->toString();
1333 msg
= msg
->concat (klass_name
);
1335 throw_no_class_def_found_error (msg
);
1338 def
->accflags
= access_flags
| java::lang::reflect::Modifier::INTERPRETED
;
1339 pool_data
[this_class
].clazz
= def
;
1340 pool_tags
[this_class
] = JV_CONSTANT_ResolvedClass
;
1342 if (super_class
== 0)
1344 // Note that this is ok if we are defining java.lang.Object.
1345 // But there is no way to have this class be interpreted.
1346 throw_class_format_error ("no superclass reference");
1349 def
->state
= JV_STATE_PRELOADING
;
1351 // Register this class with its defining loader as well (despite the
1352 // name of the function we're calling), so that super class lookups
1353 // work properly. If there is an error, our caller will unregister
1354 // this class from the class loader. Also, we don't need to hold a
1355 // lock here, as our caller has acquired it.
1356 _Jv_RegisterInitiatingLoader (def
, def
->loader
);
1358 // Note that we found a name so that unregistration can happen if
1360 *found_name
= def
->name
;
1362 if (super_class
!= 0)
1364 // Load the superclass.
1365 check_tag (super_class
, JV_CONSTANT_Class
);
1366 _Jv_Utf8Const
* super_name
= pool_data
[super_class
].utf8
;
1368 // Load the superclass using our defining loader.
1369 jclass the_super
= _Jv_FindClass (super_name
, def
->loader
);
1371 // This will establish that we are allowed to be a subclass,
1372 // and check for class circularity error.
1373 checkExtends (def
, the_super
);
1375 // Note: for an interface we will find Object as the
1376 // superclass. We still check it above to ensure class file
1377 // validity, but we simply assign `null' to the actual field in
1379 def
->superclass
= (((access_flags
& Modifier::INTERFACE
))
1380 ? NULL
: the_super
);
1381 pool_data
[super_class
].clazz
= the_super
;
1382 pool_tags
[super_class
] = JV_CONSTANT_ResolvedClass
;
1385 // Now we've come past the circularity problem, we can
1386 // now say that we're loading.
1388 def
->state
= JV_STATE_LOADING
;
1392 ///// Implements the checks described in sect. 5.3.5.3
1394 _Jv_ClassReader::checkExtends (jclass sub
, jclass super
)
1396 using namespace java::lang::reflect
;
1398 _Jv_Linker::wait_for_state (super
, JV_STATE_LOADING
);
1400 // Having an interface or a final class as a superclass is no good.
1401 if ((super
->accflags
& (Modifier::INTERFACE
| Modifier::FINAL
)) != 0)
1403 throw_incompatible_class_change_error (sub
->getName ());
1406 // If the super class is not public, we need to check some more.
1407 if ((super
->accflags
& Modifier::PUBLIC
) == 0)
1409 // With package scope, the classes must have the same class
1411 if ( sub
->loader
!= super
->loader
1412 || !_Jv_ClassNameSamePackage (sub
->name
, super
->name
))
1414 throw_incompatible_class_change_error (sub
->getName ());
1418 for (; super
!= 0; super
= super
->getSuperclass ())
1421 throw_class_circularity_error (sub
->getName ());
1427 void _Jv_ClassReader::handleInterfacesBegin (int count
)
1429 def
->interfaces
= (jclass
*) _Jv_AllocRawObj (count
*sizeof (jclass
));
1430 def
->interface_count
= count
;
1433 void _Jv_ClassReader::handleInterface (int if_number
, int offset
)
1435 _Jv_word
* pool_data
= def
->constants
.data
;
1436 unsigned char * pool_tags
= (unsigned char*) def
->constants
.tags
;
1438 jclass the_interface
;
1440 if (pool_tags
[offset
] == JV_CONSTANT_Class
)
1442 _Jv_Utf8Const
* name
= pool_data
[offset
].utf8
;
1443 the_interface
= _Jv_FindClass (name
, def
->loader
);
1445 else if (pool_tags
[offset
] == JV_CONSTANT_ResolvedClass
)
1447 the_interface
= pool_data
[offset
].clazz
;
1451 throw_no_class_def_found_error ("erroneous constant pool tag");
1454 // checks the validity of the_interface, and that we are in fact
1455 // allowed to implement that interface.
1456 checkImplements (def
, the_interface
);
1458 pool_data
[offset
].clazz
= the_interface
;
1459 pool_tags
[offset
] = JV_CONSTANT_ResolvedClass
;
1461 def
->interfaces
[if_number
] = the_interface
;
1465 _Jv_ClassReader::checkImplements (jclass sub
, jclass super
)
1467 using namespace java::lang::reflect
;
1469 // well, it *must* be an interface
1470 if ((super
->accflags
& Modifier::INTERFACE
) == 0)
1472 throw_incompatible_class_change_error (sub
->getName ());
1475 // if it has package scope, it must also be defined by the
1477 if ((super
->accflags
& Modifier::PUBLIC
) == 0)
1479 if ( sub
->loader
!= super
->loader
1480 || !_Jv_ClassNameSamePackage (sub
->name
, super
->name
))
1482 throw_incompatible_class_change_error (sub
->getName ());
1486 // FIXME: add interface circularity check here
1489 throw_class_circularity_error (sub
->getName ());
1493 void _Jv_ClassReader::handleFieldsBegin (int count
)
1495 def
->fields
= (_Jv_Field
*) _Jv_AllocRawObj (count
* sizeof (_Jv_Field
));
1496 def
->field_count
= count
;
1497 def_interp
->field_initializers
1498 = (_Jv_ushort
*) _Jv_AllocRawObj (count
* sizeof (_Jv_ushort
));
1499 for (int i
= 0; i
< count
; i
++)
1500 def_interp
->field_initializers
[i
] = (_Jv_ushort
) 0;
1503 void _Jv_ClassReader::handleField (int field_no
,
1509 using namespace java::lang::reflect
;
1511 _Jv_word
*pool_data
= def
->constants
.data
;
1513 _Jv_Field
*field
= &def
->fields
[fieldmap
[field_no
]];
1514 _Jv_Utf8Const
*field_name
= pool_data
[name
].utf8
;
1516 field
->name
= field_name
;
1518 // Ignore flags we don't know about.
1519 field
->flags
= flags
& (Field::FIELD_MODIFIERS
1520 | Modifier::SYNTHETIC
1523 _Jv_Utf8Const
* sig
= pool_data
[desc
].utf8
;
1527 verify_identifier (field_name
);
1529 for (int i
= 0; i
< field_no
; ++i
)
1531 if (_Jv_equalUtf8Consts (field_name
, def
->fields
[fieldmap
[i
]].name
)
1532 && _Jv_equalUtf8Consts (sig
,
1533 // We know the other fields are
1535 (_Jv_Utf8Const
*) def
->fields
[i
].type
))
1536 throw_class_format_error ("duplicate field name");
1539 // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
1540 if (1 < ( ((field
->flags
& Modifier::PUBLIC
) ? 1 : 0)
1541 +((field
->flags
& Modifier::PRIVATE
) ? 1 : 0)
1542 +((field
->flags
& Modifier::PROTECTED
) ? 1 : 0)))
1543 throw_class_format_error ("erroneous field access flags");
1545 // FIXME: JVM spec S4.5: Verify ACC_FINAL and ACC_VOLATILE are not
1546 // both set. Verify modifiers for interface fields.
1551 verify_field_signature (sig
);
1553 // field->type is really a jclass, but while it is still
1554 // unresolved we keep an _Jv_Utf8Const* instead.
1555 field
->type
= (jclass
) sig
;
1556 field
->flags
|= _Jv_FIELD_UNRESOLVED_FLAG
;
1557 field
->u
.boffset
= 0;
1561 void _Jv_ClassReader::handleConstantValueAttribute (int field_index
,
1565 using namespace java::lang::reflect
;
1567 _Jv_Field
*field
= &def
->fields
[field_index
];
1569 if ((field
->flags
& (Modifier::STATIC
1571 | Modifier::PRIVATE
)) == 0)
1573 // Ignore, as per vmspec #4.7.2
1577 // do not allow multiple constant fields!
1579 throw_class_format_error ("field has multiple ConstantValue attributes");
1581 *found_value
= true;
1582 def_interp
->field_initializers
[field_index
] = value
;
1584 /* type check the initializer */
1586 if (value
<= 0 || value
>= pool_count
)
1587 throw_class_format_error ("erroneous ConstantValue attribute");
1589 /* FIXME: do the rest */
1593 _Jv_ClassReader::handleMethodsBegin (int count
)
1595 def
->methods
= (_Jv_Method
*) _Jv_AllocRawObj (sizeof (_Jv_Method
) * count
);
1597 def_interp
->interpreted_methods
1598 = (_Jv_MethodBase
**) _Jv_AllocRawObj (sizeof (_Jv_MethodBase
*)
1601 for (int i
= 0; i
< count
; i
++)
1603 def_interp
->interpreted_methods
[i
] = 0;
1604 def
->methods
[i
].index
= (_Jv_ushort
) -1;
1607 def
->method_count
= count
;
1611 void _Jv_ClassReader::handleMethod
1612 (int mth_index
, int accflags
, int name
, int desc
)
1614 using namespace java::lang::reflect
;
1616 _Jv_word
*pool_data
= def
->constants
.data
;
1617 _Jv_Method
*method
= &def
->methods
[mth_index
];
1619 check_tag (name
, JV_CONSTANT_Utf8
);
1620 prepare_pool_entry (name
, JV_CONSTANT_Utf8
, false);
1621 method
->name
= pool_data
[name
].utf8
;
1623 check_tag (desc
, JV_CONSTANT_Utf8
);
1624 prepare_pool_entry (desc
, JV_CONSTANT_Utf8
);
1625 method
->signature
= pool_data
[desc
].utf8
;
1627 // ignore unknown flags
1628 method
->accflags
= accflags
& (Method::METHOD_MODIFIERS
1630 | Modifier::SYNTHETIC
1631 | Modifier::VARARGS
);
1635 method
->throws
= NULL
;
1639 if (_Jv_equalUtf8Consts (method
->name
, clinit_name
)
1640 || _Jv_equalUtf8Consts (method
->name
, init_name
))
1643 verify_identifier (method
->name
);
1645 verify_method_signature (method
->signature
);
1647 for (int i
= 0; i
< mth_index
; ++i
)
1649 if (_Jv_equalUtf8Consts (method
->name
, def
->methods
[i
].name
)
1650 && _Jv_equalUtf8Consts (method
->signature
,
1651 def
->methods
[i
].signature
))
1652 throw_class_format_error ("duplicate method");
1655 // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
1656 if (1 < ( ((method
->accflags
& Modifier::PUBLIC
) ? 1 : 0)
1657 +((method
->accflags
& Modifier::PRIVATE
) ? 1 : 0)
1658 +((method
->accflags
& Modifier::PROTECTED
) ? 1 : 0)))
1659 throw_class_format_error ("erroneous method access flags");
1661 // FIXME: JVM spec S4.6: if ABSTRACT modifier is set, verify other
1662 // flags are not set. Verify flags for interface methods. Verify
1663 // modifiers for initializers.
1667 void _Jv_ClassReader::handleCodeAttribute
1668 (int method_index
, int max_stack
, int max_locals
,
1669 int code_start
, int code_length
, int exc_table_length
)
1671 int size
= _Jv_InterpMethod::size (exc_table_length
, code_length
);
1672 _Jv_InterpMethod
*method
=
1673 (_Jv_InterpMethod
*) (_Jv_AllocRawObj (size
));
1675 method
->max_stack
= max_stack
;
1676 method
->max_locals
= max_locals
;
1677 method
->code_length
= code_length
;
1678 method
->exc_count
= exc_table_length
;
1679 method
->is_15
= is_15
;
1680 method
->defining_class
= def
;
1681 method
->self
= &def
->methods
[method_index
];
1682 method
->prepared
= NULL
;
1683 method
->line_table_len
= 0;
1684 method
->line_table
= NULL
;
1687 // grab the byte code!
1688 memcpy ((void*) method
->bytecode (),
1689 (void*) (bytes
+code_start
),
1692 def_interp
->interpreted_methods
[method_index
] = method
;
1694 if ((method
->self
->accflags
& java::lang::reflect::Modifier::STATIC
))
1696 // Precompute the ncode field for a static method. This lets us
1697 // call a static method of an interpreted class from precompiled
1698 // code without first resolving the class (that will happen
1699 // during class initialization instead).
1700 method
->self
->ncode
= method
->ncode (def
);
1704 void _Jv_ClassReader::handleExceptionTableEntry
1705 (int method_index
, int exc_index
,
1706 int start_pc
, int end_pc
, int handler_pc
, int catch_type
)
1708 _Jv_InterpMethod
*method
= reinterpret_cast<_Jv_InterpMethod
*>
1709 (def_interp
->interpreted_methods
[method_index
]);
1710 _Jv_InterpException
*exc
= method
->exceptions ();
1712 exc
[exc_index
].start_pc
.i
= start_pc
;
1713 exc
[exc_index
].end_pc
.i
= end_pc
;
1714 exc
[exc_index
].handler_pc
.i
= handler_pc
;
1715 exc
[exc_index
].handler_type
.i
= catch_type
;
1718 void _Jv_ClassReader::handleMethodsEnd ()
1720 using namespace java::lang::reflect
;
1722 for (int i
= 0; i
< def
->method_count
; i
++)
1724 _Jv_Method
*method
= &def
->methods
[i
];
1725 if ((method
->accflags
& Modifier::NATIVE
) != 0)
1727 if (def_interp
->interpreted_methods
[i
] != 0)
1728 throw_class_format_error ("code provided for native method");
1731 _Jv_JNIMethod
*m
= (_Jv_JNIMethod
*)
1732 _Jv_AllocRawObj (sizeof (_Jv_JNIMethod
));
1733 m
->defining_class
= def
;
1736 def_interp
->interpreted_methods
[i
] = m
;
1738 if ((method
->accflags
& Modifier::STATIC
))
1740 // Precompute the ncode field for a static method.
1741 // This lets us call a static method of an
1742 // interpreted class from precompiled code without
1743 // first resolving the class (that will happen
1744 // during class initialization instead).
1745 method
->ncode
= m
->ncode (def
);
1749 else if ((method
->accflags
& Modifier::ABSTRACT
) != 0)
1751 if (def_interp
->interpreted_methods
[i
] != 0)
1752 throw_class_format_error ("code provided for abstract method");
1753 method
->ncode
= (void *) &_Jv_ThrowAbstractMethodError
;
1757 if (def_interp
->interpreted_methods
[i
] == 0)
1758 throw_class_format_error ("method with no code");
1763 void _Jv_ClassReader::throw_class_format_error (const char *msg
)
1766 if (def
->name
!= NULL
)
1768 jsize mlen
= strlen (msg
);
1769 unsigned char* data
= (unsigned char*) def
->name
->chars();
1770 int ulen
= def
->name
->len();
1771 unsigned char* limit
= data
+ ulen
;
1772 jsize nlen
= _Jv_strLengthUtf8 ((char *) data
, ulen
);
1773 jsize len
= nlen
+ mlen
+ 3;
1774 str
= JvAllocString(len
);
1775 jchar
*chrs
= JvGetStringChars(str
);
1776 while (data
< limit
)
1777 *chrs
++ = UTF8_GET(data
, limit
);
1785 *chrs
++ = c
& 0xFFFF;
1790 str
= JvNewStringLatin1 (msg
);
1791 ::throw_class_format_error (str
);
1794 /** Here we define the exceptions that can be thrown */
1797 throw_no_class_def_found_error (jstring msg
)
1800 ? new java::lang::NoClassDefFoundError (msg
)
1801 : new java::lang::NoClassDefFoundError
);
1805 throw_no_class_def_found_error (const char *msg
)
1807 throw_no_class_def_found_error (JvNewStringLatin1 (msg
));
1811 throw_class_format_error (jstring msg
)
1814 ? new java::lang::ClassFormatError (msg
)
1815 : new java::lang::ClassFormatError
);
1819 throw_internal_error (const char *msg
)
1821 throw new java::lang::InternalError (JvNewStringLatin1 (msg
));
1825 throw_incompatible_class_change_error (jstring msg
)
1827 throw new java::lang::IncompatibleClassChangeError (msg
);
1831 throw_class_circularity_error (jstring msg
)
1833 throw new java::lang::ClassCircularityError (msg
);
1836 #endif /* INTERPRETER */
1840 /** This section takes care of verifying integrity of identifiers,
1841 signatures, field ddescriptors, and class names */
1843 #define UTF8_PEEK(PTR, LIMIT) \
1844 ({ unsigned char* xxkeep = (PTR); \
1845 int xxch = UTF8_GET(PTR,LIMIT); \
1846 PTR = xxkeep; xxch; })
1848 /* Verify one element of a type descriptor or signature. */
1849 static unsigned char*
1850 _Jv_VerifyOne (unsigned char* ptr
, unsigned char* limit
, bool void_ok
)
1855 int ch
= UTF8_GET (ptr
, limit
);
1863 case 'S': case 'B': case 'I': case 'J':
1864 case 'Z': case 'C': case 'F': case 'D':
1869 unsigned char *start
= ptr
, *end
;
1877 if ((ch
= UTF8_GET (ptr
, limit
)) == -1)
1882 if (! _Jv_VerifyClassName (start
, (unsigned short) (end
-start
)))
1888 return _Jv_VerifyOne (ptr
, limit
, false);
1898 /* Verification and loading procedures. */
1900 _Jv_VerifyFieldSignature (_Jv_Utf8Const
*sig
)
1902 unsigned char* ptr
= (unsigned char*) sig
->chars();
1903 unsigned char* limit
= ptr
+ sig
->len();
1905 ptr
= _Jv_VerifyOne (ptr
, limit
, false);
1907 return ptr
== limit
;
1911 _Jv_VerifyMethodSignature (_Jv_Utf8Const
*sig
)
1913 unsigned char* ptr
= (unsigned char*) sig
->chars();
1914 unsigned char* limit
= ptr
+ sig
->len();
1916 if (ptr
== limit
|| UTF8_GET(ptr
,limit
) != '(')
1919 while (ptr
&& UTF8_PEEK (ptr
, limit
) != ')')
1920 ptr
= _Jv_VerifyOne (ptr
, limit
, false);
1922 if (! ptr
|| UTF8_GET (ptr
, limit
) != ')')
1925 // get the return type
1926 ptr
= _Jv_VerifyOne (ptr
, limit
, true);
1928 return ptr
== limit
;
1931 /* We try to avoid calling the Character methods all the time, in
1932 fact, they will only be called for non-standard things. */
1933 static __inline__
int
1934 is_identifier_start (int c
)
1936 unsigned int ch
= (unsigned)c
;
1938 if ((ch
- 0x41U
) < 29U) /* A ... Z */
1940 if ((ch
- 0x61U
) < 29U) /* a ... z */
1942 if (ch
== 0x5FU
) /* _ */
1945 return java::lang::Character::isJavaIdentifierStart ((jchar
) ch
);
1948 static __inline__
int
1949 is_identifier_part (int c
)
1951 unsigned int ch
= (unsigned)c
;
1953 if ((ch
- 0x41U
) < 29U) /* A ... Z */
1955 if ((ch
- 0x61U
) < 29U) /* a ... z */
1957 if ((ch
- 0x30) < 10U) /* 0 .. 9 */
1959 if (ch
== 0x5FU
|| ch
== 0x24U
) /* _ $ */
1962 return java::lang::Character::isJavaIdentifierStart ((jchar
) ch
);
1966 _Jv_VerifyIdentifier (_Jv_Utf8Const
* name
)
1968 unsigned char *ptr
= (unsigned char*) name
->chars();
1969 unsigned char *limit
= (unsigned char*) name
->limit();
1972 if ((ch
= UTF8_GET (ptr
, limit
))==-1
1973 || ! is_identifier_start (ch
))
1976 while (ptr
!= limit
)
1978 if ((ch
= UTF8_GET (ptr
, limit
))==-1
1979 || ! is_identifier_part (ch
))
1986 _Jv_VerifyClassName (unsigned char* ptr
, _Jv_ushort length
)
1988 unsigned char *limit
= ptr
+length
;
1991 if ('[' == UTF8_PEEK (ptr
, limit
))
1993 unsigned char *end
= _Jv_VerifyOne (++ptr
, limit
, false);
1994 // _Jv_VerifyOne must leave us looking at the terminating nul
2004 if ((ch
= UTF8_GET (ptr
, limit
))==-1)
2006 if (! is_identifier_start (ch
))
2011 else if ((ch
= UTF8_GET (ptr
, limit
))==-1)
2015 else if (! is_identifier_part (ch
))
2022 _Jv_VerifyClassName (_Jv_Utf8Const
*name
)
2024 return _Jv_VerifyClassName ((unsigned char*)name
->chars(), name
->len());
2027 /* Returns true, if NAME1 and NAME2 represent classes in the same
2028 package. Neither NAME2 nor NAME2 may name an array type. */
2030 _Jv_ClassNameSamePackage (_Jv_Utf8Const
*name1
, _Jv_Utf8Const
*name2
)
2032 unsigned char* ptr1
= (unsigned char*) name1
->chars();
2033 unsigned char* limit1
= (unsigned char*) name1
->limit();
2035 unsigned char* last1
= ptr1
;
2037 // scan name1, and find the last occurrence of '.'
2038 while (ptr1
< limit1
) {
2039 int ch1
= UTF8_GET (ptr1
, limit1
);
2048 // Now the length of NAME1's package name is LEN.
2049 int len
= last1
- (unsigned char*) name1
->chars();
2051 // If this is longer than NAME2, then we're off.
2052 if (len
> name2
->len())
2055 // Then compare the first len bytes for equality.
2056 if (memcmp ((void*) name1
->chars(), (void*) name2
->chars(), len
) == 0)
2058 // Check that there are no .'s after position LEN in NAME2.
2060 unsigned char* ptr2
= (unsigned char*) name2
->chars() + len
;
2061 unsigned char* limit2
= (unsigned char*) name2
->limit();
2063 while (ptr2
< limit2
)
2065 int ch2
= UTF8_GET (ptr2
, limit2
);
2066 if (ch2
== -1 || ch2
== '.')