1 #ifdef USE_PRAGMA_IDENT_HDR 2 #pragma ident "@(#)classFileParser.hpp 1.85 07/07/09 11:19:50 JVM" 3 #endif 4 /* 5 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7 * 8 * This code is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 only, as 10 * published by the Free Software Foundation. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 23 * CA 95054 USA or visit www.sun.com if you need additional information or 24 * have any questions. 25 * 26 */ 27 28 // Parser for for .class files 29 // 30 // The bytes describing the class file structure is read from a Stream object 31 32 class ClassFileParser VALUE_OBJ_CLASS_SPEC { 33 private: 34 bool _need_verify; 35 bool _relax_verify; 36 u2 _major_version; 37 u2 _minor_version; 38 symbolHandle _class_name; 39 40 bool _has_finalizer; 41 bool _has_empty_finalizer; 42 bool _has_vanilla_constructor; 43 44 enum { fixed_buffer_size = 128 }; 45 u_char linenumbertable_buffer[fixed_buffer_size]; 46 47 ClassFileStream* _stream; // Actual input stream 48 49 enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names 50 51 // Accessors 52 ClassFileStream* stream() { return _stream; } 53 void set_stream(ClassFileStream* st) { _stream = st; } 54 55 // Constant pool parsing 56 void parse_constant_pool_entries(constantPoolHandle cp, int length, TRAPS); 57 58 constantPoolHandle parse_constant_pool(TRAPS); 59 60 // Interface parsing 61 objArrayHandle parse_interfaces(constantPoolHandle cp, 62 int length, 63 Handle class_loader, 64 Handle protection_domain, 65 PerfTraceTime* vmtimer, 66 symbolHandle class_name, 67 TRAPS); 68 69 // Field parsing 70 void parse_field_attributes(constantPoolHandle cp, u2 attributes_count, 71 bool is_static, u2 signature_index, 72 u2* constantvalue_index_addr, 73 bool* is_synthetic_addr, 74 u2* generic_signature_index_addr, 75 typeArrayHandle* field_annotations, TRAPS); 76 typeArrayHandle parse_fields(constantPoolHandle cp, bool is_interface, 77 struct FieldAllocationCount *fac, 78 objArrayHandle* fields_annotations, TRAPS); 79 80 // Method parsing 81 methodHandle parse_method(constantPoolHandle cp, bool is_interface, 82 AccessFlags* promoted_flags, 83 typeArrayHandle* method_annotations, 84 typeArrayHandle* method_parameter_annotations, 85 typeArrayHandle* method_default_annotations, 86 TRAPS); 87 objArrayHandle parse_methods (constantPoolHandle cp, bool is_interface, 88 AccessFlags* promoted_flags, 89 bool* has_final_method, 90 objArrayOop* methods_annotations_oop, 91 objArrayOop* methods_parameter_annotations_oop, 92 objArrayOop* methods_default_annotations_oop, 93 TRAPS); 94 typeArrayHandle sort_methods (objArrayHandle methods, 95 objArrayHandle methods_annotations, 96 objArrayHandle methods_parameter_annotations, 97 objArrayHandle methods_default_annotations, 98 TRAPS); 99 typeArrayHandle parse_exception_table(u4 code_length, u4 exception_table_length, 100 constantPoolHandle cp, TRAPS); 101 void parse_linenumber_table( 102 u4 code_attribute_length, u4 code_length, 103 CompressedLineNumberWriteStream** write_stream, TRAPS); 104 u2* parse_localvariable_table(u4 code_length, u2 max_locals, u4 code_attribute_length, 105 constantPoolHandle cp, u2* localvariable_table_length, 106 bool isLVTT, TRAPS); 107 u2* parse_checked_exceptions(u2* checked_exceptions_length, u4 method_attribute_length, 108 constantPoolHandle cp, TRAPS); 109 void parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index, 110 u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS); 111 typeArrayOop parse_stackmap_table(u4 code_attribute_length, TRAPS); 112 113 // Classfile attribute parsing 114 void parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS); 115 void parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, 116 instanceKlassHandle k, int length, TRAPS); 117 u2 parse_classfile_inner_classes_attribute(constantPoolHandle cp, 118 instanceKlassHandle k, TRAPS); 119 void parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS); 120 void parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS); 121 void parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS); 122 123 // Annotations handling 124 typeArrayHandle assemble_annotations(u1* runtime_visible_annotations, 125 int runtime_visible_annotations_length, 126 u1* runtime_invisible_annotations, 127 int runtime_invisible_annotations_length, TRAPS); 128 129 // Final setup 130 int compute_oop_map_size(instanceKlassHandle super, int nonstatic_oop_count, 131 int first_nonstatic_oop_offset); 132 void fill_oop_maps(instanceKlassHandle k, int nonstatic_oop_map_count, 133 u2* nonstatic_oop_offsets, u2* nonstatic_oop_length); 134 void set_precomputed_flags(instanceKlassHandle k); 135 objArrayHandle compute_transitive_interfaces(instanceKlassHandle super, 136 objArrayHandle local_ifs, TRAPS); 137 138 // Special handling for certain classes. 139 // Add the "discovered" field to java.lang.ref.Reference if 140 // it does not exist. 141 void java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr, 142 constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS); 143 // Adjust the field allocation counts for java.lang.Class to add 144 // fake fields. 145 void java_lang_Class_fix_pre(objArrayHandle* methods_ptr, 146 FieldAllocationCount *fac_ptr, TRAPS); 147 // Adjust the next_nonstatic_oop_offset to place the fake fields 148 // before any Java fields. 149 void java_lang_Class_fix_post(int* next_nonstatic_oop_offset); 150 151 // Format checker methods 152 void classfile_parse_error(const char* msg, TRAPS); 153 void classfile_parse_error(const char* msg, int index, TRAPS); 154 void classfile_parse_error(const char* msg, const char *name, TRAPS); 155 void classfile_parse_error(const char* msg, int index, const char *name, TRAPS); 156 inline void guarantee_property(bool b, const char* msg, TRAPS) { 157 if (!b) { classfile_parse_error(msg, CHECK); } 158 } 159 160 inline void assert_property(bool b, const char* msg, TRAPS) { 161 #ifdef ASSERT 162 if (!b) { fatal(msg); } 163 #endif 164 } 165 166 inline void check_property(bool property, const char* msg, int index, TRAPS) { 167 if (_need_verify) { 168 guarantee_property(property, msg, index, CHECK); 169 } else { 170 assert_property(property, msg, CHECK); 171 } 172 } 173 174 inline void check_property(bool property, const char* msg, TRAPS) { 175 if (_need_verify) { 176 guarantee_property(property, msg, CHECK); 177 } else { 178 assert_property(property, msg, CHECK); 179 } 180 } 181 182 inline void guarantee_property(bool b, const char* msg, int index, TRAPS) { 183 if (!b) { classfile_parse_error(msg, index, CHECK); } 184 } 185 inline void guarantee_property(bool b, const char* msg, const char *name, TRAPS) { 186 if (!b) { classfile_parse_error(msg, name, CHECK); } 187 } 188 inline void guarantee_property(bool b, const char* msg, int index, const char *name, TRAPS) { 189 if (!b) { classfile_parse_error(msg, index, name, CHECK); } 190 } 191 192 bool is_supported_version(u2 major, u2 minor); 193 bool has_illegal_visibility(jint flags); 194 195 void verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS); 196 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS); 197 void verify_legal_class_name(symbolHandle name, TRAPS); 198 void verify_legal_field_name(symbolHandle name, TRAPS); 199 void verify_legal_method_name(symbolHandle name, TRAPS); 200 void verify_legal_field_signature(symbolHandle fieldname, symbolHandle signature, TRAPS); 201 int verify_legal_method_signature(symbolHandle methodname, symbolHandle signature, TRAPS); 202 void verify_legal_class_modifiers(jint flags, TRAPS); 203 void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS); 204 void verify_legal_method_modifiers(jint flags, bool is_interface, symbolHandle name, TRAPS); 205 bool verify_unqualified_name(char* name, unsigned int length, int type); 206 char* skip_over_field_name(char* name, bool slash_ok, unsigned int length); 207 char* skip_over_field_signature(char* signature, bool void_ok, unsigned int length, TRAPS); 208 209 public: 210 // Constructor 211 ClassFileParser(ClassFileStream* st) { set_stream(st); } 212 213 // Parse .class file and return new klassOop. The klassOop is not hooked up 214 // to the system dictionary or any other structures, so a .class file can 215 // be loaded several times if desired. 216 // The system dictionary hookup is done by the caller. 217 // 218 // "parsed_name" is updated by this method, and is the name found 219 // while parsing the stream. 220 instanceKlassHandle parseClassFile(symbolHandle name, 221 Handle class_loader, 222 Handle protection_domain, 223 symbolHandle& parsed_name, 224 TRAPS); 225 226 // Verifier checks 227 static void check_super_class_access(instanceKlassHandle this_klass, TRAPS); 228 static void check_super_interface_access(instanceKlassHandle this_klass, TRAPS); 229 static void check_final_method_override(instanceKlassHandle this_klass, TRAPS); 230 static void check_illegal_static_method(instanceKlassHandle this_klass, TRAPS); 231 };