1 /* 2 * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_CLASSFILE_CLASSFILEPARSER_HPP 26 #define SHARE_CLASSFILE_CLASSFILEPARSER_HPP 27 28 #include "memory/referenceType.hpp" 29 #include "oops/annotations.hpp" 30 #include "oops/constantPool.hpp" 31 #include "oops/fieldInfo.hpp" 32 #include "oops/instanceKlass.hpp" 33 #include "oops/typeArrayOop.hpp" 34 #include "utilities/accessFlags.hpp" 35 36 class Annotations; 37 template <typename T> 38 class Array; 39 class ClassFileStream; 40 class ClassLoaderData; 41 class ClassLoadInfo; 42 class ClassInstanceInfo; 43 class CompressedLineNumberWriteStream; 44 class ConstMethod; 45 class FieldInfo; 46 template <typename T> 47 class GrowableArray; 48 class InstanceKlass; 49 class RecordComponent; 50 class Symbol; 51 class FieldLayoutBuilder; 52 53 // Utility to collect and compact oop maps during layout 54 class OopMapBlocksBuilder : public ResourceObj { 55 public: 56 OopMapBlock* _nonstatic_oop_maps; 57 unsigned int _nonstatic_oop_map_count; 58 unsigned int _max_nonstatic_oop_maps; 59 60 OopMapBlocksBuilder(unsigned int max_blocks); 61 OopMapBlock* last_oop_map() const; 62 void initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks); 63 void add(int offset, int count); 64 void copy(OopMapBlock* dst); 65 void compact(); 66 void print_on(outputStream* st) const; 67 void print_value_on(outputStream* st) const; 68 }; 69 70 // Values needed for oopmap and InstanceKlass creation 71 class FieldLayoutInfo : public ResourceObj { 72 public: 73 OopMapBlocksBuilder* oop_map_blocks; 74 int _instance_size; 75 int _nonstatic_field_size; 76 int _static_field_size; 77 bool _has_nonstatic_fields; 78 }; 79 80 // Parser for for .class files 81 // 82 // The bytes describing the class file structure is read from a Stream object 83 84 class ClassFileParser { 85 friend class FieldLayoutBuilder; 86 friend class FieldLayout; 87 88 class ClassAnnotationCollector; 89 class FieldAnnotationCollector; 90 91 public: 92 // The ClassFileParser has an associated "publicity" level 93 // It is used to control which subsystems (if any) 94 // will observe the parsing (logging, events, tracing). 95 // Default level is "BROADCAST", which is equivalent to 96 // a "public" parsing attempt. 97 // 98 // "INTERNAL" level should be entirely private to the 99 // caller - this allows for internal reuse of ClassFileParser 100 // 101 enum Publicity { 102 INTERNAL, 103 BROADCAST 104 }; 105 106 enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names 107 108 private: 109 // Potentially unaligned pointer to various 16-bit entries in the class file 110 typedef void unsafe_u2; 111 112 const ClassFileStream* _stream; // Actual input stream 113 Symbol* _class_name; 114 mutable ClassLoaderData* _loader_data; 115 const bool _is_hidden; 116 const bool _can_access_vm_annotations; 117 int _orig_cp_size; 118 unsigned int _static_oop_count; 119 120 // Metadata created before the instance klass is created. Must be deallocated 121 // if not transferred to the InstanceKlass upon successful class loading 122 // in which case these pointers have been set to null. 123 const InstanceKlass* _super_klass; 124 ConstantPool* _cp; 125 Array<u1>* _fieldinfo_stream; 126 Array<FieldStatus>* _fields_status; 127 Array<Method*>* _methods; 128 Array<u2>* _inner_classes; 129 Array<u2>* _nest_members; 130 u2 _nest_host; 131 Array<u2>* _permitted_subclasses; 132 Array<RecordComponent*>* _record_components; 133 Array<InstanceKlass*>* _local_interfaces; 134 Array<InstanceKlass*>* _transitive_interfaces; 135 Annotations* _combined_annotations; 136 AnnotationArray* _class_annotations; 137 AnnotationArray* _class_type_annotations; 138 Array<AnnotationArray*>* _fields_annotations; 139 Array<AnnotationArray*>* _fields_type_annotations; 140 InstanceKlass* _klass; // InstanceKlass* once created. 141 InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed 142 143 ClassAnnotationCollector* _parsed_annotations; 144 FieldLayoutInfo* _field_info; 145 GrowableArray<FieldInfo>* _temp_field_info; 146 const intArray* _method_ordering; 147 GrowableArray<Method*>* _all_mirandas; 148 149 enum { fixed_buffer_size = 128 }; 150 u_char _linenumbertable_buffer[fixed_buffer_size]; 151 152 // Size of Java vtable (in words) 153 int _vtable_size; 154 int _itable_size; 155 156 int _num_miranda_methods; 157 158 Handle _protection_domain; 159 AccessFlags _access_flags; 160 161 // for tracing and notifications 162 Publicity _pub_level; 163 164 // Used to keep track of whether a constant pool item 19 or 20 is found. These 165 // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed 166 // in regular class files. For class file version >= 53, a CFE cannot be thrown 167 // immediately when these are seen because a NCDFE must be thrown if the class's 168 // access_flags have ACC_MODULE set. But, the access_flags haven't been looked 169 // at yet. So, the bad constant pool item is cached here. A value of zero 170 // means that no constant pool item 19 or 20 was found. 171 short _bad_constant_seen; 172 173 // class attributes parsed before the instance klass is created: 174 bool _synthetic_flag; 175 int _sde_length; 176 const char* _sde_buffer; 177 u2 _sourcefile_index; 178 u2 _generic_signature_index; 179 180 u2 _major_version; 181 u2 _minor_version; 182 u2 _this_class_index; 183 u2 _super_class_index; 184 u2 _itfs_len; 185 u2 _java_fields_count; 186 187 bool _need_verify; 188 189 bool _has_nonstatic_concrete_methods; 190 bool _declares_nonstatic_concrete_methods; 191 bool _has_localvariable_table; 192 bool _has_final_method; 193 bool _has_contended_fields; 194 195 // precomputed flags 196 bool _has_finalizer; 197 bool _has_empty_finalizer; 198 int _max_bootstrap_specifier_index; // detects BSS values 199 200 void parse_stream(const ClassFileStream* const stream, TRAPS); 201 202 void mangle_hidden_class_name(InstanceKlass* const ik); 203 204 void post_process_parsed_stream(const ClassFileStream* const stream, 205 ConstantPool* cp, 206 TRAPS); 207 208 void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, 209 const ClassInstanceInfo& cl_inst_info, TRAPS); 210 211 void set_klass(InstanceKlass* instance); 212 213 void set_class_bad_constant_seen(short bad_constant); 214 short class_bad_constant_seen() { return _bad_constant_seen; } 215 void set_class_synthetic_flag(bool x) { _synthetic_flag = x; } 216 void set_class_sourcefile_index(u2 x) { _sourcefile_index = x; } 217 void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; } 218 void set_class_sde_buffer(const char* x, int len) { _sde_buffer = x; _sde_length = len; } 219 220 void create_combined_annotations(TRAPS); 221 void apply_parsed_class_attributes(InstanceKlass* k); // update k 222 void apply_parsed_class_metadata(InstanceKlass* k, int fields_count); 223 void clear_class_metadata(); 224 225 // Constant pool parsing 226 void parse_constant_pool_entries(const ClassFileStream* const stream, 227 ConstantPool* cp, 228 const int length, 229 TRAPS); 230 231 void parse_constant_pool(const ClassFileStream* const cfs, 232 ConstantPool* const cp, 233 const int length, 234 TRAPS); 235 236 // Interface parsing 237 void parse_interfaces(const ClassFileStream* const stream, 238 const int itfs_len, 239 ConstantPool* const cp, 240 bool* has_nonstatic_concrete_methods, 241 TRAPS); 242 243 const InstanceKlass* parse_super_class(ConstantPool* const cp, 244 const int super_class_index, 245 const bool need_verify, 246 TRAPS); 247 248 // Field parsing 249 void parse_field_attributes(const ClassFileStream* const cfs, 250 u2 attributes_count, 251 bool is_static, 252 u2 signature_index, 253 u2* const constantvalue_index_addr, 254 bool* const is_synthetic_addr, 255 u2* const generic_signature_index_addr, 256 FieldAnnotationCollector* parsed_annotations, 257 TRAPS); 258 259 void parse_fields(const ClassFileStream* const cfs, 260 bool is_interface, 261 ConstantPool* cp, 262 const int cp_size, 263 u2* const java_fields_count_ptr, 264 TRAPS); 265 266 // Method parsing 267 Method* parse_method(const ClassFileStream* const cfs, 268 bool is_interface, 269 const ConstantPool* cp, 270 bool* const has_localvariable_table, 271 TRAPS); 272 273 void parse_methods(const ClassFileStream* const cfs, 274 bool is_interface, 275 bool* const has_localvariable_table, 276 bool* const has_final_method, 277 bool* const declares_nonstatic_concrete_methods, 278 TRAPS); 279 280 const unsafe_u2* parse_exception_table(const ClassFileStream* const stream, 281 u4 code_length, 282 u4 exception_table_length, 283 TRAPS); 284 285 void parse_linenumber_table(u4 code_attribute_length, 286 u4 code_length, 287 CompressedLineNumberWriteStream**const write_stream, 288 TRAPS); 289 290 const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs, 291 u4 code_length, 292 u2 max_locals, 293 u4 code_attribute_length, 294 u2* const localvariable_table_length, 295 bool isLVTT, 296 TRAPS); 297 298 const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs, 299 u2* const checked_exceptions_length, 300 u4 method_attribute_length, 301 TRAPS); 302 303 // Classfile attribute parsing 304 u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS); 305 void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS); 306 void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 307 int length, 308 TRAPS); 309 310 // Check for circularity in InnerClasses attribute. 311 bool check_inner_classes_circularity(const ConstantPool* cp, int length, TRAPS); 312 313 u2 parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 314 const ConstantPool* cp, 315 const u1* const inner_classes_attribute_start, 316 bool parsed_enclosingmethod_attribute, 317 u2 enclosing_method_class_index, 318 u2 enclosing_method_method_index, 319 TRAPS); 320 321 u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 322 const u1* const nest_members_attribute_start, 323 TRAPS); 324 325 u2 parse_classfile_permitted_subclasses_attribute(const ClassFileStream* const cfs, 326 const u1* const permitted_subclasses_attribute_start, 327 TRAPS); 328 329 u4 parse_classfile_record_attribute(const ClassFileStream* const cfs, 330 const ConstantPool* cp, 331 const u1* const record_attribute_start, 332 TRAPS); 333 334 void parse_classfile_attributes(const ClassFileStream* const cfs, 335 ConstantPool* cp, 336 ClassAnnotationCollector* parsed_annotations, 337 TRAPS); 338 339 void parse_classfile_synthetic_attribute(); 340 void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS); 341 void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 342 ConstantPool* cp, 343 u4 attribute_length, 344 TRAPS); 345 346 // Annotations handling 347 AnnotationArray* allocate_annotations(const u1* const anno, 348 int anno_length, 349 TRAPS); 350 351 void set_precomputed_flags(InstanceKlass* k); 352 353 // Format checker methods 354 void classfile_parse_error(const char* msg, TRAPS) const; 355 void classfile_parse_error(const char* msg, int index, TRAPS) const; 356 void classfile_parse_error(const char* msg, const char *name, TRAPS) const; 357 void classfile_parse_error(const char* msg, 358 int index, 359 const char *name, 360 TRAPS) const; 361 void classfile_parse_error(const char* msg, 362 const char* name, 363 const char* signature, 364 TRAPS) const; 365 366 void classfile_icce_error(const char* msg, 367 const Klass* k, 368 TRAPS) const; 369 370 void classfile_ucve_error(const char* msg, 371 const Symbol* class_name, 372 u2 major, 373 u2 minor, 374 TRAPS) const; 375 376 inline void guarantee_property(bool b, const char* msg, TRAPS) const { 377 if (!b) { classfile_parse_error(msg, THREAD); return; } 378 } 379 380 inline void guarantee_property(bool b, 381 const char* msg, 382 int index, 383 TRAPS) const { 384 if (!b) { classfile_parse_error(msg, index, THREAD); return; } 385 } 386 387 inline void guarantee_property(bool b, 388 const char* msg, 389 const char *name, 390 TRAPS) const { 391 if (!b) { classfile_parse_error(msg, name, THREAD); return; } 392 } 393 394 inline void guarantee_property(bool b, 395 const char* msg, 396 int index, 397 const char *name, 398 TRAPS) const { 399 if (!b) { classfile_parse_error(msg, index, name, THREAD); return; } 400 } 401 402 void throwIllegalSignature(const char* type, 403 const Symbol* name, 404 const Symbol* sig, 405 TRAPS) const; 406 407 void verify_constantvalue(const ConstantPool* const cp, 408 int constantvalue_index, 409 int signature_index, 410 TRAPS) const; 411 412 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const; 413 void verify_legal_class_name(const Symbol* name, TRAPS) const; 414 void verify_legal_field_name(const Symbol* name, TRAPS) const; 415 void verify_legal_method_name(const Symbol* name, TRAPS) const; 416 417 void verify_legal_field_signature(const Symbol* fieldname, 418 const Symbol* signature, 419 TRAPS) const; 420 int verify_legal_method_signature(const Symbol* methodname, 421 const Symbol* signature, 422 TRAPS) const; 423 void verify_legal_name_with_signature(const Symbol* name, 424 const Symbol* signature, 425 TRAPS) const; 426 427 void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS); 428 429 void verify_legal_class_modifiers(jint flags, TRAPS) const; 430 void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const; 431 void verify_legal_method_modifiers(jint flags, 432 bool is_interface, 433 const Symbol* name, 434 TRAPS) const; 435 436 void check_super_class_access(const InstanceKlass* this_klass, 437 TRAPS); 438 439 void check_super_interface_access(const InstanceKlass* this_klass, 440 TRAPS); 441 442 const char* skip_over_field_signature(const char* signature, 443 bool void_ok, 444 unsigned int length, 445 TRAPS) const; 446 447 // Wrapper for constantTag.is_klass_[or_]reference. 448 // In older versions of the VM, Klass*s cannot sneak into early phases of 449 // constant pool construction, but in later versions they can. 450 // %%% Let's phase out the old is_klass_reference. 451 bool valid_klass_reference_at(int index) const { 452 return _cp->is_within_bounds(index) && 453 _cp->tag_at(index).is_klass_or_reference(); 454 } 455 456 // Checks that the cpool index is in range and is a utf8 457 bool valid_symbol_at(int cpool_index) const { 458 return _cp->is_within_bounds(cpool_index) && 459 _cp->tag_at(cpool_index).is_utf8(); 460 } 461 462 void copy_localvariable_table(const ConstMethod* cm, 463 int lvt_cnt, 464 u2* const localvariable_table_length, 465 const unsafe_u2** const localvariable_table_start, 466 int lvtt_cnt, 467 u2* const localvariable_type_table_length, 468 const unsafe_u2** const localvariable_type_table_start, 469 TRAPS); 470 471 void copy_method_annotations(ConstMethod* cm, 472 const u1* runtime_visible_annotations, 473 int runtime_visible_annotations_length, 474 const u1* runtime_visible_parameter_annotations, 475 int runtime_visible_parameter_annotations_length, 476 const u1* runtime_visible_type_annotations, 477 int runtime_visible_type_annotations_length, 478 const u1* annotation_default, 479 int annotation_default_length, 480 TRAPS); 481 482 void update_class_name(Symbol* new_name); 483 484 public: 485 ClassFileParser(ClassFileStream* stream, 486 Symbol* name, 487 ClassLoaderData* loader_data, 488 const ClassLoadInfo* cl_info, 489 Publicity pub_level, 490 TRAPS); 491 492 ~ClassFileParser(); 493 494 InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, const ClassInstanceInfo& cl_inst_info, TRAPS); 495 496 const ClassFileStream* clone_stream() const; 497 498 void set_klass_to_deallocate(InstanceKlass* klass); 499 500 int static_field_size() const; 501 int total_oop_map_count() const; 502 jint layout_size() const; 503 504 int vtable_size() const { return _vtable_size; } 505 int itable_size() const { return _itable_size; } 506 507 u2 this_class_index() const { return _this_class_index; } 508 509 bool is_hidden() const { return _is_hidden; } 510 bool is_interface() const { return _access_flags.is_interface(); } 511 bool is_abstract() const { return _access_flags.is_abstract(); } 512 513 // Returns true if the Klass to be generated will need to be addressable 514 // with a narrow Klass ID. 515 bool klass_needs_narrow_id() const; 516 517 ClassLoaderData* loader_data() const { return _loader_data; } 518 const Symbol* class_name() const { return _class_name; } 519 const InstanceKlass* super_klass() const { return _super_klass; } 520 521 ReferenceType super_reference_type() const; 522 bool is_instance_ref_klass() const; 523 bool is_java_lang_ref_Reference_subclass() const; 524 525 AccessFlags access_flags() const { return _access_flags; } 526 527 bool is_internal() const { return INTERNAL == _pub_level; } 528 529 static bool verify_unqualified_name(const char* name, unsigned int length, int type); 530 531 #ifdef ASSERT 532 static bool is_internal_format(Symbol* class_name); 533 #endif 534 535 }; 536 537 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP