X-Git-Url: http://git.sourceforge.jp/view?p=pf3gnuchains%2Fgcc-fork.git;a=blobdiff_plain;f=libjava%2Fdefineclass.cc;h=4c4e0a7f45de396a6ea093d65c26e40600fffb19;hp=26606815836bfc0a7b3645479b30bb5e9cfb616e;hb=4ce12f0156e9df62da3049b9c4aa7ec7fb7af153;hpb=166395e2fc518ad1e278115f0c1719067a1da3f4 diff --git a/libjava/defineclass.cc b/libjava/defineclass.cc index 26606815836..4c4e0a7f45d 100644 --- a/libjava/defineclass.cc +++ b/libjava/defineclass.cc @@ -1,6 +1,7 @@ // defineclass.cc - defining a class from .class format. -/* Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation +/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 + Free Software Foundation This file is part of libgcj. @@ -23,8 +24,10 @@ details. */ #include #include +#include #include #include +#include #include #include @@ -37,6 +40,11 @@ details. */ #include #include #include +#include +#include +#include +#include +#include using namespace gcj; @@ -44,11 +52,11 @@ using namespace gcj; // these go in some separate functions, to avoid having _Jv_InitClass // inserted all over the place. -static void throw_internal_error (char *msg) +static void throw_internal_error (const char *msg) __attribute__ ((__noreturn__)); static void throw_no_class_def_found_error (jstring msg) __attribute__ ((__noreturn__)); -static void throw_no_class_def_found_error (char *msg) +static void throw_no_class_def_found_error (const char *msg) __attribute__ ((__noreturn__)); static void throw_class_format_error (jstring msg) __attribute__ ((__noreturn__)); @@ -67,7 +75,8 @@ static void throw_class_circularity_error (jstring msg) * public or private members here. */ -struct _Jv_ClassReader { +struct _Jv_ClassReader +{ // do verification? Currently, there is no option to disable this. // This flag just controls the verificaiton done by the class loader; @@ -83,6 +92,10 @@ struct _Jv_ClassReader { bool verify; + // original input data. + jbyteArray input_data; + jint input_offset; + // input data. unsigned char *bytes; int len; @@ -97,10 +110,21 @@ struct _Jv_ClassReader { // the class to define (see java-interp.h) jclass def; - + // the classes associated interpreter data. _Jv_InterpClass *def_interp; + // The name we found. + _Jv_Utf8Const **found_name; + + // True if this is a 1.5 class file. + bool is_15; + + // Buffer holding extra reflection data. + ::java::io::ByteArrayOutputStream *reflection_data; + ::java::io::DataOutputStream *data_stream; + + /* check that the given number of input bytes are available */ inline void check (int num) { @@ -115,7 +139,7 @@ struct _Jv_ClassReader { pos += num; } - /* read an unsignend 1-byte unit */ + /* read an unsigned 1-byte unit */ inline static jint get1u (unsigned char* bytes) { return bytes[0]; @@ -215,33 +239,70 @@ struct _Jv_ClassReader { throw_class_format_error ("erroneous type descriptor"); } - _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length) + ::java::io::DataOutputStream *get_reflection_stream () + { + if (reflection_data == NULL) + { + reflection_data = new ::java::io::ByteArrayOutputStream(); + data_stream = new ::java::io::DataOutputStream(reflection_data); + } + return data_stream; + } + + _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length, + java::security::ProtectionDomain *pd, + _Jv_Utf8Const **name_result) { if (klass == 0 || length < 0 || offset+length > data->length) throw_internal_error ("arguments to _Jv_DefineClass"); verify = true; + input_data = data; + input_offset = offset; bytes = (unsigned char*) (elements (data)+offset); len = length; pos = 0; + is_15 = false; + def = klass; - def_interp = (_Jv_InterpClass *) def->aux_info; + found_name = name_result; + reflection_data = NULL; + data_stream = NULL; + + def->size_in_bytes = -1; + def->vtable_method_count = -1; + def->engine = &_Jv_soleInterpreterEngine; + def->protectionDomain = pd; } /** and here goes the parser members defined out-of-line */ void parse (); void read_constpool (); - void prepare_pool_entry (int index, unsigned char tag); + void prepare_pool_entry (int index, unsigned char tag, + bool rewrite = true); void read_fields (); void read_methods (); void read_one_class_attribute (); void read_one_method_attribute (int method); void read_one_code_attribute (int method); - void read_one_field_attribute (int field); - void throw_class_format_error (char *msg); + void read_one_field_attribute (int field, bool *); + void throw_class_format_error (const char *msg); + + void handleEnclosingMethod(int); + void handleGenericSignature(jv_attr_type, unsigned short, int); + void handleAnnotationElement(); + void handleAnnotation(); + void handleAnnotations(); + void handleMemberAnnotations(jv_attr_type, int, int); + void handleAnnotationDefault(int, int); + void handleParameterAnnotations(int, int); + void finish_reflection_data (); /** check an utf8 entry, without creating a Utf8Const object */ - bool is_attribute_name (int index, char *name); + bool is_attribute_name (int index, const char *name); + + /** return the value of a utf8 entry in the passed array */ + int pool_Utf8_to_char_arr (int index, char **entry); /** here goes the class-loader members defined out-of-line */ void handleConstantPool (); @@ -249,9 +310,8 @@ struct _Jv_ClassReader { void handleInterfacesBegin (int); void handleInterface (int, int); void handleFieldsBegin (int); - void handleField (int, int, int, int); - void handleFieldsEnd (); - void handleConstantValueAttribute (int,int); + void handleField (int, int, int, int, int *); + void handleConstantValueAttribute (int, int, bool *); void handleMethodsBegin (int); void handleMethod (int, int, int, int); void handleMethodsEnd (); @@ -271,10 +331,15 @@ struct _Jv_ClassReader { */ }; +// Note that *NAME_RESULT will only be set if the class is registered +// with the class loader. This is how the caller can know whether +// unregistration is require. void -_Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length) +_Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length, + java::security::ProtectionDomain *pd, + _Jv_Utf8Const **name_result) { - _Jv_ClassReader reader (klass, data, offset, length); + _Jv_ClassReader reader (klass, data, offset, length, pd, name_result); reader.parse(); /* that's it! */ @@ -283,19 +348,34 @@ _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length) /** This section defines the parsing/scanning of the class data */ +// Major and minor version numbers for various releases. +#define MAJOR_1_1 45 +#define MINOR_1_1 3 +#define MAJOR_1_2 46 +#define MINOR_1_2 0 +#define MAJOR_1_3 47 +#define MINOR_1_3 0 +#define MAJOR_1_4 48 +#define MINOR_1_4 0 +#define MAJOR_1_5 49 +#define MINOR_1_5 0 +#define MAJOR_1_6 50 +#define MINOR_1_6 0 + void _Jv_ClassReader::parse () { int magic = read4 (); - - /* FIXME: Decide which range of version numbers to allow */ - - /* int minor_version = */ read2u (); - /* int major_verson = */ read2u (); - if (magic != (int) 0xCAFEBABE) throw_class_format_error ("bad magic number"); + int minor_version = read2u (); + int major_version = read2u (); + if (major_version < MAJOR_1_1 || major_version > MAJOR_1_6 + || (major_version == MAJOR_1_6 && minor_version > MINOR_1_6)) + throw_class_format_error ("unrecognized class file version"); + is_15 = (major_version >= MAJOR_1_5); + pool_count = read2u (); read_constpool (); @@ -310,8 +390,13 @@ _Jv_ClassReader::parse () handleClassBegin (access_flags, this_class, super_class); + // Allocate our aux_info here, after the name is set, to fulfill our + // contract with the collector interface. + def->aux_info = (void *) _Jv_AllocRawObj (sizeof (_Jv_InterpClass)); + def_interp = (_Jv_InterpClass *) def->aux_info; + int interfaces_count = read2u (); - + handleInterfacesBegin (interfaces_count); for (int i = 0; i < interfaces_count; i++) @@ -334,17 +419,246 @@ _Jv_ClassReader::parse () if (pos != len) throw_class_format_error ("unused data before end of file"); - // tell everyone we're done. - def->state = JV_STATE_LOADED; + finish_reflection_data (); + + // Tell everyone we're done. + def->state = JV_STATE_READ; + if (gcj::verbose_class_flag) + _Jv_Linker::print_class_loaded (def); + ++gcj::loadedClasses; def->notifyAll (); +} + +void +_Jv_ClassReader::finish_reflection_data () +{ + if (data_stream == NULL) + return; + data_stream->writeByte(JV_DONE_ATTR); + data_stream->flush(); + int nbytes = reflection_data->count; + unsigned char *new_bytes = (unsigned char *) _Jv_AllocBytes (nbytes); + memcpy (new_bytes, elements (reflection_data->buf), nbytes); + def->reflection_data = new_bytes; +} + +void +_Jv_ClassReader::handleEnclosingMethod (int len) +{ + if (len != 4) + throw_class_format_error ("invalid EnclosingMethod attribute"); + // FIXME: only allow one... + int class_index = read2u(); + check_tag (class_index, JV_CONSTANT_Class); + prepare_pool_entry (class_index, JV_CONSTANT_Class); + + int method_index = read2u(); + // Zero is ok and means no enclosing method. + if (method_index != 0) + { + check_tag (method_index, JV_CONSTANT_NameAndType); + prepare_pool_entry (method_index, JV_CONSTANT_NameAndType); + } + + ::java::io::DataOutputStream *stream = get_reflection_stream (); + stream->writeByte(JV_CLASS_ATTR); + stream->writeInt(5); + stream->writeByte(JV_ENCLOSING_METHOD_KIND); + stream->writeShort(class_index); + stream->writeShort(method_index); +} + +void +_Jv_ClassReader::handleGenericSignature (jv_attr_type type, + unsigned short index, + int len) +{ + if (len != 2) + throw_class_format_error ("invalid Signature attribute"); + + int cpool_idx = read2u(); + check_tag (cpool_idx, JV_CONSTANT_Utf8); + prepare_pool_entry (cpool_idx, JV_CONSTANT_Utf8, false); + + ::java::io::DataOutputStream *stream = get_reflection_stream (); + stream->writeByte(type); + int attrlen = 3; + if (type != JV_CLASS_ATTR) + attrlen += 2; + stream->writeInt(attrlen); + if (type != JV_CLASS_ATTR) + stream->writeShort(index); + stream->writeByte(JV_SIGNATURE_KIND); + stream->writeShort(cpool_idx); +} + +void +_Jv_ClassReader::handleAnnotationElement() +{ + int tag = read1u(); + switch (tag) + { + case 'B': + case 'C': + case 'S': + case 'Z': + case 'I': + { + int index = read2u(); + check_tag (index, JV_CONSTANT_Integer); + prepare_pool_entry (index, JV_CONSTANT_Integer); + } + break; + case 'D': + { + int index = read2u(); + check_tag (index, JV_CONSTANT_Double); + prepare_pool_entry (index, JV_CONSTANT_Double); + } + break; + case 'F': + { + int index = read2u(); + check_tag (index, JV_CONSTANT_Float); + prepare_pool_entry (index, JV_CONSTANT_Float); + } + break; + case 'J': + { + int index = read2u(); + check_tag (index, JV_CONSTANT_Long); + prepare_pool_entry (index, JV_CONSTANT_Long); + } + break; + case 's': + { + int index = read2u(); + // Despite what the JVM spec says, compilers generate a Utf8 + // constant here, not a String. + check_tag (index, JV_CONSTANT_Utf8); + prepare_pool_entry (index, JV_CONSTANT_Utf8, false); + } + break; + + case 'e': + { + int type_name_index = read2u(); + int const_name_index = read2u (); + check_tag (type_name_index, JV_CONSTANT_Utf8); + prepare_pool_entry (type_name_index, JV_CONSTANT_Utf8); + check_tag (const_name_index, JV_CONSTANT_Utf8); + prepare_pool_entry (const_name_index, JV_CONSTANT_Utf8, false); + } + break; + case 'c': + { + int index = read2u(); + check_tag (index, JV_CONSTANT_Utf8); + prepare_pool_entry (index, JV_CONSTANT_Utf8); + } + break; + case '@': + handleAnnotation(); + break; + case '[': + { + int n_array_elts = read2u (); + for (int i = 0; i < n_array_elts; ++i) + handleAnnotationElement(); + } + break; + default: + throw_class_format_error ("invalid annotation element"); + } +} + +void +_Jv_ClassReader::handleAnnotation() +{ + int type_index = read2u(); + check_tag (type_index, JV_CONSTANT_Utf8); + prepare_pool_entry (type_index, JV_CONSTANT_Utf8); + + int npairs = read2u(); + for (int i = 0; i < npairs; ++i) + { + int name_index = read2u(); + check_tag (name_index, JV_CONSTANT_Utf8); + prepare_pool_entry (name_index, JV_CONSTANT_Utf8, false); + handleAnnotationElement(); + } +} + +void +_Jv_ClassReader::handleAnnotations() +{ + int num = read2u(); + while (num--) + handleAnnotation(); +} + +void +_Jv_ClassReader::handleMemberAnnotations(jv_attr_type member_type, + int member_index, + int len) +{ + // We're going to copy the bytes in verbatim. But first we want to + // make sure the attribute is well-formed, and we want to prepare + // the constant pool. So, we save our starting point. + int orig_pos = pos; + + handleAnnotations(); + // FIXME: check that we read all LEN bytes? + + ::java::io::DataOutputStream *stream = get_reflection_stream (); + stream->writeByte(member_type); + int newLen = len + 1; + if (member_type != JV_CLASS_ATTR) + newLen += 2; + stream->writeInt(newLen); + stream->writeByte(JV_ANNOTATIONS_KIND); + if (member_type != JV_CLASS_ATTR) + stream->writeShort(member_index); + // Write the data as-is. + stream->write(input_data, input_offset + orig_pos, len); +} + +void +_Jv_ClassReader::handleAnnotationDefault(int member_index, int len) +{ + int orig_pos = pos; + handleAnnotationElement(); + + ::java::io::DataOutputStream *stream = get_reflection_stream (); + stream->writeByte(JV_METHOD_ATTR); + stream->writeInt(len + 3); + stream->writeByte(JV_ANNOTATION_DEFAULT_KIND); + stream->writeShort(member_index); + stream->write(input_data, input_offset + orig_pos, len); +} + +void +_Jv_ClassReader::handleParameterAnnotations(int member_index, int len) +{ + int orig_pos = pos; + + int n_params = read1u(); + for (int i = 0; i < n_params; ++i) + handleAnnotations(); + + ::java::io::DataOutputStream *stream = get_reflection_stream (); + stream->writeByte(JV_METHOD_ATTR); + stream->writeInt(len + 3); + stream->writeByte(JV_PARAMETER_ANNOTATIONS_KIND); + stream->writeShort(member_index); + stream->write(input_data, input_offset + orig_pos, len); } void _Jv_ClassReader::read_constpool () { tags = (unsigned char*) _Jv_AllocBytes (pool_count); - offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int) - * pool_count) ; + offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int) * pool_count) ; /** first, we scan the constant pool, collecting tags and offsets */ tags[0] = JV_CONSTANT_Undefined; @@ -401,6 +715,47 @@ void _Jv_ClassReader::read_fields () int fields_count = read2u (); handleFieldsBegin (fields_count); + // We want to sort the fields so that static fields come first, + // followed by instance fields. We do this before parsing the + // fields so that we can have the new indices available when + // creating the annotation data structures. + + // Allocate this on the heap in case there are a large number of + // fields. + int *fieldmap = (int *) _Jv_AllocBytes (fields_count * sizeof (int)); + int save_pos = pos; + int static_count = 0, instance_count = -1; + for (int i = 0; i < fields_count; ++i) + { + using namespace java::lang::reflect; + + int access_flags = read2u (); + skip (4); + int attributes_count = read2u (); + + if ((access_flags & Modifier::STATIC) != 0) + fieldmap[i] = static_count++; + else + fieldmap[i] = instance_count--; + + for (int j = 0; j < attributes_count; ++j) + { + skip (2); + int length = read4 (); + skip (length); + } + } + pos = save_pos; + + // In the loop above, instance fields are represented by negative + // numbers. Here we rewrite these to be proper offsets. + for (int i = 0; i < fields_count; ++i) + { + if (fieldmap[i] < 0) + fieldmap[i] = static_count - 1 - fieldmap[i]; + } + def->static_field_count = static_count; + for (int i = 0; i < fields_count; i++) { int access_flags = read2u (); @@ -414,19 +769,18 @@ void _Jv_ClassReader::read_fields () check_tag (descriptor_index, JV_CONSTANT_Utf8); prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8); - handleField (i, access_flags, name_index, descriptor_index); + handleField (i, access_flags, name_index, descriptor_index, fieldmap); + bool found_value = false; for (int j = 0; j < attributes_count; j++) { - read_one_field_attribute (i); + read_one_field_attribute (fieldmap[i], &found_value); } } - - handleFieldsEnd (); } bool -_Jv_ClassReader::is_attribute_name (int index, char *name) +_Jv_ClassReader::is_attribute_name (int index, const char *name) { check_tag (index, JV_CONSTANT_Utf8); int len = get2u (bytes+offsets[index]); @@ -436,7 +790,20 @@ _Jv_ClassReader::is_attribute_name (int index, char *name) return !memcmp (bytes+offsets[index]+2, name, len); } -void _Jv_ClassReader::read_one_field_attribute (int field_index) +// Get a UTF8 value from the constant pool and turn it into a garbage +// collected char array. +int _Jv_ClassReader::pool_Utf8_to_char_arr (int index, char** entry) +{ + check_tag (index, JV_CONSTANT_Utf8); + int len = get2u (bytes + offsets[index]); + *entry = reinterpret_cast (_Jv_AllocBytes (len + 1)); + (*entry)[len] = '\0'; + memcpy (*entry, bytes + offsets[index] + 2, len); + return len + 1; +} + +void _Jv_ClassReader::read_one_field_attribute (int field_index, + bool *found_value) { int name = read2u (); int length = read4 (); @@ -452,22 +819,23 @@ void _Jv_ClassReader::read_one_field_attribute (int field_index) || tags[cv] == JV_CONSTANT_Long || tags[cv] == JV_CONSTANT_Double || tags[cv] == JV_CONSTANT_String)) - { - handleConstantValueAttribute (field_index, cv); - } - else - { - throw_class_format_error ("erroneous ConstantValue attribute"); - } - - if (length != 2) + { + handleConstantValueAttribute (field_index, cv, found_value); + } + else + { throw_class_format_error ("erroneous ConstantValue attribute"); - } + } - else - { - skip (length); - } + if (length != 2) + throw_class_format_error ("erroneous ConstantValue attribute"); + } + else if (is_attribute_name (name, "Signature")) + handleGenericSignature(JV_FIELD_ATTR, field_index, length); + else if (is_attribute_name (name, "RuntimeVisibleAnnotations")) + handleMemberAnnotations(JV_FIELD_ATTR, field_index, length); + else + skip (length); } void _Jv_ClassReader::read_methods () @@ -484,9 +852,9 @@ void _Jv_ClassReader::read_methods () int attributes_count = read2u (); check_tag (name_index, JV_CONSTANT_Utf8); - prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8); + prepare_pool_entry (name_index, JV_CONSTANT_Utf8); - check_tag (name_index, JV_CONSTANT_Utf8); + check_tag (descriptor_index, JV_CONSTANT_Utf8); prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8); handleMethod (i, access_flags, name_index, @@ -514,30 +882,20 @@ void _Jv_ClassReader::read_one_method_attribute (int method_index) throw_class_format_error ("only one Exceptions attribute allowed per method"); int num_exceptions = read2u (); - // We use malloc here because the GC won't scan the method - // objects. FIXME this means a memory leak if we GC a class. - // (Currently we never do.) _Jv_Utf8Const **exceptions = - (_Jv_Utf8Const **) _Jv_Malloc ((num_exceptions + 1) * sizeof (_Jv_Utf8Const *)); + (_Jv_Utf8Const **) _Jv_AllocBytes ((num_exceptions + 1) + * sizeof (_Jv_Utf8Const *)); int out = 0; _Jv_word *pool_data = def->constants.data; for (int i = 0; i < num_exceptions; ++i) { - try - { - int ndx = read2u (); - // JLS 2nd Ed. 4.7.5 requires that the tag not be 0. - if (ndx != 0) - { - check_tag (ndx, JV_CONSTANT_Class); - exceptions[out++] = pool_data[ndx].utf8; - } - } - catch (java::lang::Throwable *exc) + int ndx = read2u (); + // JLS 2nd Ed. 4.7.5 requires that the tag not be 0. + if (ndx != 0) { - _Jv_Free (exceptions); - throw exc; + check_tag (ndx, JV_CONSTANT_Class); + exceptions[out++] = pool_data[ndx].utf8; } } exceptions[out] = NULL; @@ -601,7 +959,14 @@ void _Jv_ClassReader::read_one_method_attribute (int method_index) if ((pos - start_off) != length) throw_class_format_error ("code attribute too short"); } - + else if (is_attribute_name (name, "Signature")) + handleGenericSignature(JV_METHOD_ATTR, method_index, length); + else if (is_attribute_name (name, "RuntimeVisibleAnnotations")) + handleMemberAnnotations(JV_METHOD_ATTR, method_index, length); + else if (is_attribute_name (name, "RuntimeVisibleParameterAnnotations")) + handleParameterAnnotations(method_index, length); + else if (is_attribute_name (name, "AnnotationDefault")) + handleAnnotationDefault(method_index, length); else { /* ignore unknown attributes */ @@ -609,26 +974,95 @@ void _Jv_ClassReader::read_one_method_attribute (int method_index) } } -void _Jv_ClassReader::read_one_code_attribute (int /*method*/) +void _Jv_ClassReader::read_one_code_attribute (int method_index) { - /* ignore for now, ... later we may want to pick up - line number information, for debugging purposes; - in fact, the whole debugger issue is open! */ - - /* int name = */ read2u (); + int name = read2u (); int length = read4 (); - skip (length); - + if (is_attribute_name (name, "LineNumberTable")) + { + _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *> + (def_interp->interpreted_methods[method_index]); + if (method->line_table != NULL) + throw_class_format_error ("Method already has LineNumberTable"); + + int table_len = read2u (); + _Jv_LineTableEntry* table + = (_Jv_LineTableEntry *) _Jv_AllocBytes (table_len + * sizeof (_Jv_LineTableEntry)); + for (int i = 0; i < table_len; i++) + { + table[i].bytecode_pc = read2u (); + table[i].line = read2u (); + } + method->line_table_len = table_len; + method->line_table = table; + } + else if (is_attribute_name (name, "LocalVariableTable")) + { + _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *> + (def_interp->interpreted_methods[method_index]); + if (method->local_var_table != NULL) + throw_class_format_error ("Method already has LocalVariableTable"); + + int table_len = read2u (); + _Jv_LocalVarTableEntry *table + = reinterpret_cast<_Jv_LocalVarTableEntry *> + (_Jv_AllocRawObj (table_len * sizeof (_Jv_LocalVarTableEntry))); + + for (int i = 0; i < table_len; i++) + { + table[i].bytecode_pc = read2u (); + table[i].length = read2u (); + pool_Utf8_to_char_arr (read2u (), &table[i].name); + pool_Utf8_to_char_arr (read2u (), &table[i].descriptor); + table[i].slot = read2u (); + + if (table[i].slot > method->max_locals || table[i].slot < 0) + throw_class_format_error ("Malformed Local Variable Table: Invalid Slot"); + } + + method->local_var_table_len = table_len; + method->local_var_table = table; + } + else + { + /* ignore unknown code attributes */ + skip (length); + } } void _Jv_ClassReader::read_one_class_attribute () { - /* we also ignore the class attributes, ... - some day we'll add inner-classes support. */ - - /* int name = */ read2u (); + int name = read2u (); int length = read4 (); - skip (length); + if (is_attribute_name (name, "SourceFile")) + { + int source_index = read2u (); + check_tag (source_index, JV_CONSTANT_Utf8); + prepare_pool_entry (source_index, JV_CONSTANT_Utf8, false); + def_interp->source_file_name = _Jv_NewStringUtf8Const + (def->constants.data[source_index].utf8); + } + else if (is_attribute_name (name, "Signature")) + handleGenericSignature(JV_CLASS_ATTR, 0, length); + else if (is_attribute_name (name, "EnclosingMethod")) + handleEnclosingMethod(length); + else if (is_attribute_name (name, "RuntimeVisibleAnnotations")) + handleMemberAnnotations(JV_CLASS_ATTR, 0, length); + else if (is_attribute_name (name, "InnerClasses")) + { + ::java::io::DataOutputStream *stream = get_reflection_stream (); + stream->writeByte(JV_CLASS_ATTR); + stream->writeInt(length + 1); + stream->writeByte(JV_INNER_CLASSES_KIND); + stream->write(input_data, input_offset + pos, length); + skip (length); + } + else + { + /* Currently, we ignore most class attributes. */ + skip (length); + } } @@ -640,11 +1074,10 @@ void _Jv_ClassReader::handleConstantPool () { /** now, we actually define the class' constant pool */ - // the pool is scanned explicitly by the collector jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count); _Jv_word *pool_data - = (_Jv_word*) _Jv_AllocBytes (pool_count * sizeof (_Jv_word)); - + = (_Jv_word*) _Jv_AllocRawObj (pool_count * sizeof (_Jv_word)); + def->constants.tags = pool_tags; def->constants.data = pool_data; def->constants.size = pool_count; @@ -692,9 +1125,15 @@ void _Jv_ClassReader::handleConstantPool () } /* this is a recursive procedure, which will prepare pool entries as needed. - Which is how we avoid initializing those entries which go unused. */ + Which is how we avoid initializing those entries which go unused. + + REWRITE is true iff this pool entry is the Utf8 representation of a + class name or a signature. +*/ + void -_Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag) +_Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag, + bool rewrite) { /* these two, pool_data and pool_tags, point into the class structure we are currently defining */ @@ -715,25 +1154,29 @@ _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag) { case JV_CONSTANT_Utf8: { - // If we came here, it is because some other tag needs this - // utf8-entry for type information! Thus, we translate /'s to .'s in - // order to accomondate gcj's internal representation. - int len = get2u (this_data); - char *buffer = (char*) __builtin_alloca (len); char *s = ((char*) this_data)+2; + pool_tags[index] = JV_CONSTANT_Utf8; - /* FIXME: avoid using a buffer here */ + if (! rewrite) + { + pool_data[index].utf8 = _Jv_makeUtf8Const (s, len); + break; + } + + // If REWRITE is set, it is because some other tag needs this + // utf8-entry for type information: it is a class or a + // signature. Thus, we translate /'s to .'s in order to + // accomondate gcj's internal representation. + char *buffer = (char*) __builtin_alloca (len); for (int i = 0; i < len; i++) { if (s[i] == '/') buffer[i] = '.'; else - buffer[i] = (char) s[i]; + buffer[i] = s[i]; } - pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len); - pool_tags[index] = JV_CONSTANT_Utf8; } break; @@ -801,8 +1244,7 @@ _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag) _Jv_ushort type_index = get2u (this_data+2); check_tag (name_index, JV_CONSTANT_Utf8); - prepare_pool_entry (name_index, JV_CONSTANT_Utf8); - + prepare_pool_entry (name_index, JV_CONSTANT_Utf8, false); check_tag (type_index, JV_CONSTANT_Utf8); prepare_pool_entry (type_index, JV_CONSTANT_Utf8); @@ -851,8 +1293,7 @@ _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag) void -_Jv_ClassReader::handleClassBegin - (int access_flags, int this_class, int super_class) +_Jv_ClassReader::handleClassBegin (int access_flags, int this_class, int super_class) { using namespace java::lang::reflect; @@ -865,7 +1306,7 @@ _Jv_ClassReader::handleClassBegin // was ClassLoader.defineClass called with an expected class name? if (def->name == 0) { - jclass orig = _Jv_FindClassInCache (loadedName, def->loader); + jclass orig = def->loader->findLoadedClass(loadedName->toString()); if (orig == 0) { @@ -888,7 +1329,7 @@ _Jv_ClassReader::handleClassBegin jstring msg = JvNewStringUTF ("loaded class "); msg = msg->concat (def->getName ()); msg = msg->concat (_Jv_NewStringUTF (" was in fact named ")); - jstring klass_name = _Jv_NewStringUTF (loadedName->data); + jstring klass_name = loadedName->toString(); msg = msg->concat (klass_name); throw_no_class_def_found_error (msg); @@ -898,23 +1339,25 @@ _Jv_ClassReader::handleClassBegin pool_data[this_class].clazz = def; pool_tags[this_class] = JV_CONSTANT_ResolvedClass; - if (super_class == 0 && ! (access_flags & Modifier::INTERFACE)) + if (super_class == 0) { - // FIXME: Consider this carefully! - if (! _Jv_equalUtf8Consts (def->name, java::lang::Object::class$.name)) - throw_no_class_def_found_error ("loading java.lang.Object"); + // Note that this is ok if we are defining java.lang.Object. + // But there is no way to have this class be interpreted. + throw_class_format_error ("no superclass reference"); } - // In the pre-loading state, it can be looked up in the - // cache only by this thread! This allows the super-class - // to include references to this class. - def->state = JV_STATE_PRELOADING; - { - JvSynchronize sync (&java::lang::Class::class$); - _Jv_RegisterClass (def); - } + // Register this class with its defining loader as well (despite the + // name of the function we're calling), so that super class lookups + // work properly. If there is an error, our caller will unregister + // this class from the class loader. Also, we don't need to hold a + // lock here, as our caller has acquired it. + _Jv_RegisterInitiatingLoader (def, def->loader); + + // Note that we found a name so that unregistration can happen if + // needed. + *found_name = def->name; if (super_class != 0) { @@ -923,8 +1366,7 @@ _Jv_ClassReader::handleClassBegin _Jv_Utf8Const* super_name = pool_data[super_class].utf8; // Load the superclass using our defining loader. - jclass the_super = _Jv_FindClass (super_name, - def->loader); + jclass the_super = _Jv_FindClass (super_name, def->loader); // This will establish that we are allowed to be a subclass, // and check for class circularity error. @@ -947,23 +1389,25 @@ _Jv_ClassReader::handleClassBegin def->notifyAll (); } -///// implements the checks described in sect. 5.3.5.3 +///// Implements the checks described in sect. 5.3.5.3 void _Jv_ClassReader::checkExtends (jclass sub, jclass super) { using namespace java::lang::reflect; - // having an interface or a final class as a superclass is no good + _Jv_Linker::wait_for_state (super, JV_STATE_LOADING); + + // Having an interface or a final class as a superclass is no good. if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0) { throw_incompatible_class_change_error (sub->getName ()); } - // if the super class is not public, we need to check some more + // If the super class is not public, we need to check some more. if ((super->accflags & Modifier::PUBLIC) == 0) { - // With package scope, the classes must have the same - // class loader. + // With package scope, the classes must have the same class + // loader. if ( sub->loader != super->loader || !_Jv_ClassNameSamePackage (sub->name, super->name)) { @@ -971,7 +1415,7 @@ _Jv_ClassReader::checkExtends (jclass sub, jclass super) } } - for (; super != 0; super = super->superclass) + for (; super != 0; super = super->getSuperclass ()) { if (super == sub) throw_class_circularity_error (sub->getName ()); @@ -982,7 +1426,7 @@ _Jv_ClassReader::checkExtends (jclass sub, jclass super) void _Jv_ClassReader::handleInterfacesBegin (int count) { - def->interfaces = (jclass*) _Jv_AllocBytes (count*sizeof (jclass)); + def->interfaces = (jclass*) _Jv_AllocRawObj (count*sizeof (jclass)); def->interface_count = count; } @@ -1048,11 +1492,10 @@ _Jv_ClassReader::checkImplements (jclass sub, jclass super) void _Jv_ClassReader::handleFieldsBegin (int count) { - def->fields = (_Jv_Field*) - _Jv_AllocBytes (count * sizeof (_Jv_Field)); + def->fields = (_Jv_Field*) _Jv_AllocRawObj (count * sizeof (_Jv_Field)); def->field_count = count; - def_interp->field_initializers = (_Jv_ushort*) - _Jv_AllocBytes (count * sizeof (_Jv_ushort)); + def_interp->field_initializers + = (_Jv_ushort*) _Jv_AllocRawObj (count * sizeof (_Jv_ushort)); for (int i = 0; i < count; i++) def_interp->field_initializers[i] = (_Jv_ushort) 0; } @@ -1060,23 +1503,22 @@ void _Jv_ClassReader::handleFieldsBegin (int count) void _Jv_ClassReader::handleField (int field_no, int flags, int name, - int desc) + int desc, + int *fieldmap) { using namespace java::lang::reflect; _Jv_word *pool_data = def->constants.data; - _Jv_Field *field = &def->fields[field_no]; + _Jv_Field *field = &def->fields[fieldmap[field_no]]; _Jv_Utf8Const *field_name = pool_data[name].utf8; -#ifndef COMPACT_FIELDS field->name = field_name; -#else - field->nameIndex = name; -#endif // Ignore flags we don't know about. - field->flags = flags & Modifier::ALL_FLAGS; + field->flags = flags & (Field::FIELD_MODIFIERS + | Modifier::SYNTHETIC + | Modifier::ENUM); _Jv_Utf8Const* sig = pool_data[desc].utf8; @@ -1086,7 +1528,7 @@ void _Jv_ClassReader::handleField (int field_no, for (int i = 0; i < field_no; ++i) { - if (_Jv_equalUtf8Consts (field_name, def->fields[i].name) + if (_Jv_equalUtf8Consts (field_name, def->fields[fieldmap[i]].name) && _Jv_equalUtf8Consts (sig, // We know the other fields are // unresolved. @@ -1094,16 +1536,15 @@ void _Jv_ClassReader::handleField (int field_no, throw_class_format_error ("duplicate field name"); } - if (field->flags & (Modifier::SYNCHRONIZED - | Modifier::NATIVE - | Modifier::INTERFACE - | Modifier::ABSTRACT)) - throw_class_format_error ("erroneous field access flags"); - + // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed. if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0) +((field->flags & Modifier::PRIVATE) ? 1 : 0) +((field->flags & Modifier::PROTECTED) ? 1 : 0))) throw_class_format_error ("erroneous field access flags"); + + // FIXME: JVM spec S4.5: Verify ACC_FINAL and ACC_VOLATILE are not + // both set. Verify modifiers for interface fields. + } if (verify) @@ -1118,7 +1559,8 @@ void _Jv_ClassReader::handleField (int field_no, void _Jv_ClassReader::handleConstantValueAttribute (int field_index, - int value) + int value, + bool *found_value) { using namespace java::lang::reflect; @@ -1133,10 +1575,10 @@ void _Jv_ClassReader::handleConstantValueAttribute (int field_index, } // do not allow multiple constant fields! - if (field->flags & _Jv_FIELD_CONSTANT_VALUE) + if (*found_value) throw_class_format_error ("field has multiple ConstantValue attributes"); - field->flags |= _Jv_FIELD_CONSTANT_VALUE; + *found_value = true; def_interp->field_initializers[field_index] = value; /* type check the initializer */ @@ -1147,61 +1589,14 @@ void _Jv_ClassReader::handleConstantValueAttribute (int field_index, /* FIXME: do the rest */ } -void _Jv_ClassReader::handleFieldsEnd () -{ - using namespace java::lang::reflect; - - // We need to reorganize the fields so that the static ones are first, - // to conform to GCJ class layout. - - int low = 0; - int high = def->field_count-1; - _Jv_Field *fields = def->fields; - _Jv_ushort *inits = def_interp->field_initializers; - - // this is kind of a raw version of quicksort. - while (low < high) - { - // go forward on low, while it's a static - while (low < high && (fields[low].flags & Modifier::STATIC) != 0) - low++; - - // go backwards on high, while it's a non-static - while (low < high && (fields[high].flags & Modifier::STATIC) == 0) - high--; - - if (low==high) - break; - - _Jv_Field tmp = fields[low]; - _Jv_ushort itmp = inits[low]; - - fields[low] = fields[high]; - inits[low] = inits[high]; - - fields[high] = tmp; - inits[high] = itmp; - - high -= 1; - low += 1; - } - - if ((fields[low].flags & Modifier::STATIC) != 0) - low += 1; - - def->static_field_count = low; -} - - - void _Jv_ClassReader::handleMethodsBegin (int count) { - def->methods = (_Jv_Method *) _Jv_AllocBytes (sizeof (_Jv_Method) * count); + def->methods = (_Jv_Method *) _Jv_AllocRawObj (sizeof (_Jv_Method) * count); def_interp->interpreted_methods - = (_Jv_MethodBase **) _Jv_AllocBytes (sizeof (_Jv_MethodBase *) - * count); + = (_Jv_MethodBase **) _Jv_AllocRawObj (sizeof (_Jv_MethodBase *) + * count); for (int i = 0; i < count; i++) { @@ -1222,7 +1617,7 @@ void _Jv_ClassReader::handleMethod _Jv_Method *method = &def->methods[mth_index]; check_tag (name, JV_CONSTANT_Utf8); - prepare_pool_entry (name, JV_CONSTANT_Utf8); + prepare_pool_entry (name, JV_CONSTANT_Utf8, false); method->name = pool_data[name].utf8; check_tag (desc, JV_CONSTANT_Utf8); @@ -1230,9 +1625,12 @@ void _Jv_ClassReader::handleMethod method->signature = pool_data[desc].utf8; // ignore unknown flags - method->accflags = accflags & Modifier::ALL_FLAGS; + method->accflags = accflags & (Method::METHOD_MODIFIERS + | Modifier::BRIDGE + | Modifier::SYNTHETIC + | Modifier::VARARGS); - // intialize... + // Initialize... method->ncode = 0; method->throws = NULL; @@ -1254,15 +1652,15 @@ void _Jv_ClassReader::handleMethod throw_class_format_error ("duplicate method"); } - if (method->accflags & (Modifier::VOLATILE - | Modifier::TRANSIENT - | Modifier::INTERFACE)) - throw_class_format_error ("erroneous method access flags"); - + // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed. if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0) +((method->accflags & Modifier::PRIVATE) ? 1 : 0) +((method->accflags & Modifier::PROTECTED) ? 1 : 0))) throw_class_format_error ("erroneous method access flags"); + + // FIXME: JVM spec S4.6: if ABSTRACT modifier is set, verify other + // flags are not set. Verify flags for interface methods. Verify + // modifiers for initializers. } } @@ -1272,16 +1670,21 @@ void _Jv_ClassReader::handleCodeAttribute { int size = _Jv_InterpMethod::size (exc_table_length, code_length); _Jv_InterpMethod *method = - (_Jv_InterpMethod*) (_Jv_AllocBytes (size)); + (_Jv_InterpMethod*) (_Jv_AllocRawObj (size)); - method->deferred = NULL; method->max_stack = max_stack; method->max_locals = max_locals; method->code_length = code_length; method->exc_count = exc_table_length; + method->is_15 = is_15; method->defining_class = def; method->self = &def->methods[method_index]; method->prepared = NULL; + method->line_table_len = 0; + method->line_table = NULL; +#ifdef DIRECT_THREADED + method->thread_count = 0; +#endif // grab the byte code! memcpy ((void*) method->bytecode (), @@ -1296,7 +1699,7 @@ void _Jv_ClassReader::handleCodeAttribute // call a static method of an interpreted class from precompiled // code without first resolving the class (that will happen // during class initialization instead). - method->self->ncode = method->ncode (); + method->self->ncode = method->ncode (def); } } @@ -1328,12 +1731,11 @@ void _Jv_ClassReader::handleMethodsEnd () else { _Jv_JNIMethod *m = (_Jv_JNIMethod *) - _Jv_AllocBytes (sizeof (_Jv_JNIMethod)); + _Jv_AllocRawObj (sizeof (_Jv_JNIMethod)); m->defining_class = def; m->self = method; m->function = NULL; def_interp->interpreted_methods[i] = m; - m->deferred = NULL; if ((method->accflags & Modifier::STATIC)) { @@ -1342,7 +1744,7 @@ void _Jv_ClassReader::handleMethodsEnd () // interpreted class from precompiled code without // first resolving the class (that will happen // during class initialization instead). - method->ncode = m->ncode (); + method->ncode = m->ncode (def); } } } @@ -1350,6 +1752,7 @@ void _Jv_ClassReader::handleMethodsEnd () { if (def_interp->interpreted_methods[i] != 0) throw_class_format_error ("code provided for abstract method"); + method->ncode = (void *) &_Jv_ThrowAbstractMethodError; } else { @@ -1359,14 +1762,14 @@ void _Jv_ClassReader::handleMethodsEnd () } } -void _Jv_ClassReader::throw_class_format_error (char *msg) +void _Jv_ClassReader::throw_class_format_error (const char *msg) { jstring str; if (def->name != NULL) { jsize mlen = strlen (msg); - unsigned char* data = (unsigned char*) def->name->data; - int ulen = def->name->length; + unsigned char* data = (unsigned char*) def->name->chars(); + int ulen = def->name->len(); unsigned char* limit = data + ulen; jsize nlen = _Jv_strLengthUtf8 ((char *) data, ulen); jsize len = nlen + mlen + 3; @@ -1401,7 +1804,7 @@ throw_no_class_def_found_error (jstring msg) } static void -throw_no_class_def_found_error (char *msg) +throw_no_class_def_found_error (const char *msg) { throw_no_class_def_found_error (JvNewStringLatin1 (msg)); } @@ -1415,7 +1818,7 @@ throw_class_format_error (jstring msg) } static void -throw_internal_error (char *msg) +throw_internal_error (const char *msg) { throw new java::lang::InternalError (JvNewStringLatin1 (msg)); } @@ -1498,8 +1901,8 @@ _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok) bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig) { - unsigned char* ptr = (unsigned char*) sig->data; - unsigned char* limit = ptr + sig->length; + unsigned char* ptr = (unsigned char*) sig->chars(); + unsigned char* limit = ptr + sig->len(); ptr = _Jv_VerifyOne (ptr, limit, false); @@ -1509,8 +1912,8 @@ _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig) bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig) { - unsigned char* ptr = (unsigned char*) sig->data; - unsigned char* limit = ptr + sig->length; + unsigned char* ptr = (unsigned char*) sig->chars(); + unsigned char* limit = ptr + sig->len(); if (ptr == limit || UTF8_GET(ptr,limit) != '(') return false; @@ -1518,7 +1921,7 @@ _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig) while (ptr && UTF8_PEEK (ptr, limit) != ')') ptr = _Jv_VerifyOne (ptr, limit, false); - if (UTF8_GET (ptr, limit) != ')') + if (! ptr || UTF8_GET (ptr, limit) != ')') return false; // get the return type @@ -1564,8 +1967,8 @@ is_identifier_part (int c) bool _Jv_VerifyIdentifier (_Jv_Utf8Const* name) { - unsigned char *ptr = (unsigned char*) name->data; - unsigned char *limit = ptr + name->length; + unsigned char *ptr = (unsigned char*) name->chars(); + unsigned char *limit = (unsigned char*) name->limit(); int ch; if ((ch = UTF8_GET (ptr, limit))==-1 @@ -1620,17 +2023,16 @@ _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length) bool _Jv_VerifyClassName (_Jv_Utf8Const *name) { - return _Jv_VerifyClassName ((unsigned char*)&name->data[0], - (_Jv_ushort) name->length); + return _Jv_VerifyClassName ((unsigned char*)name->chars(), name->len()); } /* Returns true, if NAME1 and NAME2 represent classes in the same - package. */ + package. Neither NAME2 nor NAME2 may name an array type. */ bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2) { - unsigned char* ptr1 = (unsigned char*) name1->data; - unsigned char* limit1 = ptr1 + name1->length; + unsigned char* ptr1 = (unsigned char*) name1->chars(); + unsigned char* limit1 = (unsigned char*) name1->limit(); unsigned char* last1 = ptr1; @@ -1646,20 +2048,19 @@ _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2) } // Now the length of NAME1's package name is LEN. - int len = last1 - (unsigned char*) name1->data; + int len = last1 - (unsigned char*) name1->chars(); // If this is longer than NAME2, then we're off. - if (len > name2->length) + if (len > name2->len()) return false; // Then compare the first len bytes for equality. - if (memcmp ((void*) name1->data, (void*) name2->data, len) == 0) + if (memcmp ((void*) name1->chars(), (void*) name2->chars(), len) == 0) { // Check that there are no .'s after position LEN in NAME2. - unsigned char* ptr2 = (unsigned char*) name2->data + len; - unsigned char* limit2 = - (unsigned char*) name2->data + name2->length; + unsigned char* ptr2 = (unsigned char*) name2->chars() + len; + unsigned char* limit2 = (unsigned char*) name2->limit(); while (ptr2 < limit2) {