duke@435: /* duke@435: * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Parser for for .class files duke@435: // duke@435: // The bytes describing the class file structure is read from a Stream object duke@435: duke@435: class ClassFileParser VALUE_OBJ_CLASS_SPEC { duke@435: private: duke@435: bool _need_verify; duke@435: bool _relax_verify; duke@435: u2 _major_version; duke@435: u2 _minor_version; duke@435: symbolHandle _class_name; duke@435: duke@435: bool _has_finalizer; duke@435: bool _has_empty_finalizer; duke@435: bool _has_vanilla_constructor; duke@435: duke@435: enum { fixed_buffer_size = 128 }; duke@435: u_char linenumbertable_buffer[fixed_buffer_size]; duke@435: duke@435: ClassFileStream* _stream; // Actual input stream duke@435: duke@435: enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names duke@435: duke@435: // Accessors duke@435: ClassFileStream* stream() { return _stream; } duke@435: void set_stream(ClassFileStream* st) { _stream = st; } duke@435: duke@435: // Constant pool parsing duke@435: void parse_constant_pool_entries(constantPoolHandle cp, int length, TRAPS); duke@435: duke@435: constantPoolHandle parse_constant_pool(TRAPS); duke@435: duke@435: // Interface parsing duke@435: objArrayHandle parse_interfaces(constantPoolHandle cp, duke@435: int length, duke@435: Handle class_loader, duke@435: Handle protection_domain, duke@435: PerfTraceTime* vmtimer, duke@435: symbolHandle class_name, duke@435: TRAPS); duke@435: duke@435: // Field parsing duke@435: void parse_field_attributes(constantPoolHandle cp, u2 attributes_count, duke@435: bool is_static, u2 signature_index, duke@435: u2* constantvalue_index_addr, duke@435: bool* is_synthetic_addr, duke@435: u2* generic_signature_index_addr, duke@435: typeArrayHandle* field_annotations, TRAPS); duke@435: typeArrayHandle parse_fields(constantPoolHandle cp, bool is_interface, duke@435: struct FieldAllocationCount *fac, duke@435: objArrayHandle* fields_annotations, TRAPS); duke@435: duke@435: // Method parsing duke@435: methodHandle parse_method(constantPoolHandle cp, bool is_interface, duke@435: AccessFlags* promoted_flags, duke@435: typeArrayHandle* method_annotations, duke@435: typeArrayHandle* method_parameter_annotations, duke@435: typeArrayHandle* method_default_annotations, duke@435: TRAPS); duke@435: objArrayHandle parse_methods (constantPoolHandle cp, bool is_interface, duke@435: AccessFlags* promoted_flags, duke@435: bool* has_final_method, duke@435: objArrayOop* methods_annotations_oop, duke@435: objArrayOop* methods_parameter_annotations_oop, duke@435: objArrayOop* methods_default_annotations_oop, duke@435: TRAPS); duke@435: typeArrayHandle sort_methods (objArrayHandle methods, duke@435: objArrayHandle methods_annotations, duke@435: objArrayHandle methods_parameter_annotations, duke@435: objArrayHandle methods_default_annotations, duke@435: TRAPS); duke@435: typeArrayHandle parse_exception_table(u4 code_length, u4 exception_table_length, duke@435: constantPoolHandle cp, TRAPS); duke@435: void parse_linenumber_table( duke@435: u4 code_attribute_length, u4 code_length, duke@435: CompressedLineNumberWriteStream** write_stream, TRAPS); duke@435: u2* parse_localvariable_table(u4 code_length, u2 max_locals, u4 code_attribute_length, duke@435: constantPoolHandle cp, u2* localvariable_table_length, duke@435: bool isLVTT, TRAPS); duke@435: u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length, duke@435: constantPoolHandle cp, TRAPS); duke@435: void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index, duke@435: u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS); duke@435: typeArrayOop parse_stackmap_table(u4 code_attribute_length, TRAPS); duke@435: duke@435: // Classfile attribute parsing duke@435: void parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS); duke@435: void parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, duke@435: instanceKlassHandle k, int length, TRAPS); duke@435: u2 parse_classfile_inner_classes_attribute(constantPoolHandle cp, duke@435: instanceKlassHandle k, TRAPS); duke@435: void parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS); duke@435: void parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS); duke@435: void parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS); duke@435: duke@435: // Annotations handling duke@435: typeArrayHandle assemble_annotations(u1* runtime_visible_annotations, duke@435: int runtime_visible_annotations_length, duke@435: u1* runtime_invisible_annotations, duke@435: int runtime_invisible_annotations_length, TRAPS); duke@435: duke@435: // Final setup duke@435: int compute_oop_map_size(instanceKlassHandle super, int nonstatic_oop_count, duke@435: int first_nonstatic_oop_offset); duke@435: void fill_oop_maps(instanceKlassHandle k, int nonstatic_oop_map_count, duke@435: u2* nonstatic_oop_offsets, u2* nonstatic_oop_length); duke@435: void set_precomputed_flags(instanceKlassHandle k); duke@435: objArrayHandle compute_transitive_interfaces(instanceKlassHandle super, duke@435: objArrayHandle local_ifs, TRAPS); duke@435: duke@435: // Special handling for certain classes. duke@435: // Add the "discovered" field to java.lang.ref.Reference if duke@435: // it does not exist. duke@435: void java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr, duke@435: constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS); duke@435: // Adjust the field allocation counts for java.lang.Class to add duke@435: // fake fields. duke@435: void java_lang_Class_fix_pre(objArrayHandle* methods_ptr, duke@435: FieldAllocationCount *fac_ptr, TRAPS); duke@435: // Adjust the next_nonstatic_oop_offset to place the fake fields duke@435: // before any Java fields. duke@435: void java_lang_Class_fix_post(int* next_nonstatic_oop_offset); duke@435: duke@435: // Format checker methods duke@435: void classfile_parse_error(const char* msg, TRAPS); duke@435: void classfile_parse_error(const char* msg, int index, TRAPS); duke@435: void classfile_parse_error(const char* msg, const char *name, TRAPS); duke@435: void classfile_parse_error(const char* msg, int index, const char *name, TRAPS); duke@435: inline void guarantee_property(bool b, const char* msg, TRAPS) { duke@435: if (!b) { classfile_parse_error(msg, CHECK); } duke@435: } duke@435: duke@435: inline void assert_property(bool b, const char* msg, TRAPS) { duke@435: #ifdef ASSERT duke@435: if (!b) { fatal(msg); } duke@435: #endif duke@435: } duke@435: duke@435: inline void check_property(bool property, const char* msg, int index, TRAPS) { duke@435: if (_need_verify) { duke@435: guarantee_property(property, msg, index, CHECK); duke@435: } else { duke@435: assert_property(property, msg, CHECK); duke@435: } duke@435: } duke@435: duke@435: inline void check_property(bool property, const char* msg, TRAPS) { duke@435: if (_need_verify) { duke@435: guarantee_property(property, msg, CHECK); duke@435: } else { duke@435: assert_property(property, msg, CHECK); duke@435: } duke@435: } duke@435: duke@435: inline void guarantee_property(bool b, const char* msg, int index, TRAPS) { duke@435: if (!b) { classfile_parse_error(msg, index, CHECK); } duke@435: } duke@435: inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) { duke@435: if (!b) { classfile_parse_error(msg, name, CHECK); } duke@435: } duke@435: inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) { duke@435: if (!b) { classfile_parse_error(msg, index, name, CHECK); } duke@435: } duke@435: duke@435: bool is_supported_version(u2 major, u2 minor); duke@435: bool has_illegal_visibility(jint flags); duke@435: duke@435: void verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS); duke@435: void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS); duke@435: void verify_legal_class_name(symbolHandle name, TRAPS); duke@435: void verify_legal_field_name(symbolHandle name, TRAPS); duke@435: void verify_legal_method_name(symbolHandle name, TRAPS); duke@435: void verify_legal_field_signature(symbolHandle fieldname, symbolHandle signature, TRAPS); duke@435: int verify_legal_method_signature(symbolHandle methodname, symbolHandle signature, TRAPS); duke@435: void verify_legal_class_modifiers(jint flags, TRAPS); duke@435: void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS); duke@435: void verify_legal_method_modifiers(jint flags, bool is_interface, symbolHandle name, TRAPS); duke@435: bool verify_unqualified_name(char* name, unsigned int length, int type); duke@435: char* skip_over_field_name(char* name, bool slash_ok, unsigned int length); duke@435: char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS); duke@435: duke@435: public: duke@435: // Constructor duke@435: ClassFileParser(ClassFileStream* st) { set_stream(st); } duke@435: duke@435: // Parse .class file and return new klassOop. The klassOop is not hooked up duke@435: // to the system dictionary or any other structures, so a .class file can duke@435: // be loaded several times if desired. duke@435: // The system dictionary hookup is done by the caller. duke@435: // duke@435: // "parsed_name" is updated by this method, and is the name found duke@435: // while parsing the stream. duke@435: instanceKlassHandle parseClassFile(symbolHandle name, duke@435: Handle class_loader, duke@435: Handle protection_domain, duke@435: symbolHandle& parsed_name, duke@435: TRAPS); duke@435: duke@435: // Verifier checks duke@435: static void check_super_class_access(instanceKlassHandle this_klass, TRAPS); duke@435: static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS); duke@435: static void check_final_method_override(instanceKlassHandle this_klass, TRAPS); duke@435: static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS); duke@435: };