1 /* 2 * Copyright (c) 1997, 2023, 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 bool _is_naturally_atomic; 79 bool _has_inline_fields; 80 bool _has_null_marker_offsets; 81 }; 82 83 // Parser for for .class files 84 // 85 // The bytes describing the class file structure is read from a Stream object 86 87 class ClassFileParser { 88 friend class FieldLayoutBuilder; 89 friend class FieldLayout; 90 91 class ClassAnnotationCollector; 92 class FieldAllocationCount; 93 class FieldAnnotationCollector; 94 95 public: 96 // The ClassFileParser has an associated "publicity" level 97 // It is used to control which subsystems (if any) 98 // will observe the parsing (logging, events, tracing). 99 // Default level is "BROADCAST", which is equivalent to 100 // a "public" parsing attempt. 101 // 102 // "INTERNAL" level should be entirely private to the 103 // caller - this allows for internal reuse of ClassFileParser 104 // 105 enum Publicity { 106 INTERNAL, 107 BROADCAST 108 }; 109 110 enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names 111 112 private: 113 // Potentially unaligned pointer to various 16-bit entries in the class file 114 typedef void unsafe_u2; 115 116 const ClassFileStream* _stream; // Actual input stream 117 Symbol* _class_name; 118 mutable ClassLoaderData* _loader_data; 119 const bool _is_hidden; 120 const bool _can_access_vm_annotations; 121 int _orig_cp_size; 122 123 // Metadata created before the instance klass is created. Must be deallocated 124 // if not transferred to the InstanceKlass upon successful class loading 125 // in which case these pointers have been set to null. 126 const InstanceKlass* _super_klass; 127 ConstantPool* _cp; 128 Array<u1>* _fieldinfo_stream; 129 Array<FieldStatus>* _fields_status; 130 Array<Method*>* _methods; 131 Array<u2>* _inner_classes; 132 Array<u2>* _nest_members; 133 u2 _nest_host; 134 Array<u2>* _permitted_subclasses; 135 Array<u2>* _loadable_descriptors; 136 Array<RecordComponent*>* _record_components; 137 Array<InstanceKlass*>* _local_interfaces; 138 GrowableArray<u2>* _local_interface_indexes; 139 Array<InstanceKlass*>* _transitive_interfaces; 140 Annotations* _combined_annotations; 141 AnnotationArray* _class_annotations; 142 AnnotationArray* _class_type_annotations; 143 Array<AnnotationArray*>* _fields_annotations; 144 Array<AnnotationArray*>* _fields_type_annotations; 145 InstanceKlass* _klass; // InstanceKlass* once created. 146 InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed 147 148 ClassAnnotationCollector* _parsed_annotations; 149 FieldAllocationCount* _fac; 150 FieldLayoutInfo* _field_info; 151 Array<InlineKlass*>* _inline_type_field_klasses; 152 Array<int>* _null_marker_offsets; 153 GrowableArray<FieldInfo>* _temp_field_info; 154 const intArray* _method_ordering; 155 GrowableArray<Method*>* _all_mirandas; 156 157 enum { fixed_buffer_size = 128 }; 158 u_char _linenumbertable_buffer[fixed_buffer_size]; 159 160 // Size of Java vtable (in words) 161 int _vtable_size; 162 int _itable_size; 163 164 int _num_miranda_methods; 165 166 int _alignment; 167 int _first_field_offset; 168 int _payload_size_in_bytes; 169 int _internal_null_marker_offset; 170 171 Handle _protection_domain; 172 AccessFlags _access_flags; 173 174 // for tracing and notifications 175 Publicity _pub_level; 176 177 // Used to keep track of whether a constant pool item 19 or 20 is found. These 178 // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed 179 // in regular class files. For class file version >= 53, a CFE cannot be thrown 180 // immediately when these are seen because a NCDFE must be thrown if the class's 181 // access_flags have ACC_MODULE set. But, the access_flags haven't been looked 182 // at yet. So, the bad constant pool item is cached here. A value of zero 183 // means that no constant pool item 19 or 20 was found. 184 short _bad_constant_seen; 185 186 // class attributes parsed before the instance klass is created: 187 bool _synthetic_flag; 188 int _sde_length; 189 const char* _sde_buffer; 190 u2 _sourcefile_index; 191 u2 _generic_signature_index; 192 193 u2 _major_version; 194 u2 _minor_version; 195 u2 _this_class_index; 196 u2 _super_class_index; 197 u2 _itfs_len; 198 u2 _java_fields_count; 199 200 bool _need_verify; 201 bool _relax_verify; 202 203 bool _has_nonstatic_concrete_methods; 204 bool _declares_nonstatic_concrete_methods; 205 bool _has_localvariable_table; 206 bool _has_final_method; 207 bool _has_contended_fields; 208 209 bool _has_inline_type_fields; 210 bool _has_null_marker_offsets; 211 bool _has_nonstatic_fields; 212 bool _is_empty_inline_type; 213 bool _is_naturally_atomic; 214 bool _must_be_atomic; 215 bool _is_implicitly_constructible; 216 bool _has_loosely_consistent_annotation; 217 bool _has_implicitly_constructible_annotation; 218 219 // precomputed flags 220 bool _has_finalizer; 221 bool _has_empty_finalizer; 222 int _max_bootstrap_specifier_index; // detects BSS values 223 224 void parse_stream(const ClassFileStream* const stream, TRAPS); 225 226 void mangle_hidden_class_name(InstanceKlass* const ik); 227 228 void post_process_parsed_stream(const ClassFileStream* const stream, 229 ConstantPool* cp, 230 TRAPS); 231 232 void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, 233 const ClassInstanceInfo& cl_inst_info, TRAPS); 234 235 void set_klass(InstanceKlass* instance); 236 237 void set_class_bad_constant_seen(short bad_constant); 238 short class_bad_constant_seen() { return _bad_constant_seen; } 239 void set_class_synthetic_flag(bool x) { _synthetic_flag = x; } 240 void set_class_sourcefile_index(u2 x) { _sourcefile_index = x; } 241 void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; } 242 void set_class_sde_buffer(const char* x, int len) { _sde_buffer = x; _sde_length = len; } 243 244 void create_combined_annotations(TRAPS); 245 void apply_parsed_class_attributes(InstanceKlass* k); // update k 246 void apply_parsed_class_metadata(InstanceKlass* k, int fields_count); 247 void clear_class_metadata(); 248 249 // Constant pool parsing 250 void parse_constant_pool_entries(const ClassFileStream* const stream, 251 ConstantPool* cp, 252 const int length, 253 TRAPS); 254 255 void parse_constant_pool(const ClassFileStream* const cfs, 256 ConstantPool* const cp, 257 const int length, 258 TRAPS); 259 260 // Interface parsing 261 void parse_interfaces(const ClassFileStream* const stream, 262 const int itfs_len, 263 ConstantPool* const cp, 264 bool* has_nonstatic_concrete_methods, 265 TRAPS); 266 267 const InstanceKlass* parse_super_class(ConstantPool* const cp, 268 const int super_class_index, 269 const bool need_verify, 270 TRAPS); 271 272 // Field parsing 273 void parse_field_attributes(const ClassFileStream* const cfs, 274 u2 attributes_count, 275 bool is_static, 276 u2 signature_index, 277 u2* const constantvalue_index_addr, 278 bool* const is_synthetic_addr, 279 u2* const generic_signature_index_addr, 280 FieldAnnotationCollector* parsed_annotations, 281 TRAPS); 282 283 void parse_fields(const ClassFileStream* const cfs, 284 AccessFlags class_access_flags, 285 FieldAllocationCount* const fac, 286 ConstantPool* cp, 287 const int cp_size, 288 u2* const java_fields_count_ptr, 289 TRAPS); 290 291 // Method parsing 292 Method* parse_method(const ClassFileStream* const cfs, 293 bool is_interface, 294 bool is_value_class, 295 bool is_abstract_class, 296 const ConstantPool* cp, 297 bool* const has_localvariable_table, 298 TRAPS); 299 300 void parse_methods(const ClassFileStream* const cfs, 301 bool is_interface, 302 bool is_value_class, 303 bool is_abstract_class, 304 bool* const has_localvariable_table, 305 bool* const has_final_method, 306 bool* const declares_nonstatic_concrete_methods, 307 TRAPS); 308 309 const unsafe_u2* parse_exception_table(const ClassFileStream* const stream, 310 u4 code_length, 311 u4 exception_table_length, 312 TRAPS); 313 314 void parse_linenumber_table(u4 code_attribute_length, 315 u4 code_length, 316 CompressedLineNumberWriteStream**const write_stream, 317 TRAPS); 318 319 const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs, 320 u4 code_length, 321 u2 max_locals, 322 u4 code_attribute_length, 323 u2* const localvariable_table_length, 324 bool isLVTT, 325 TRAPS); 326 327 const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs, 328 u2* const checked_exceptions_length, 329 u4 method_attribute_length, 330 TRAPS); 331 332 // Classfile attribute parsing 333 u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS); 334 void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS); 335 void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 336 int length, 337 TRAPS); 338 339 // Check for circularity in InnerClasses attribute. 340 bool check_inner_classes_circularity(const ConstantPool* cp, int length, TRAPS); 341 342 u2 parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 343 const ConstantPool* cp, 344 const u1* const inner_classes_attribute_start, 345 bool parsed_enclosingmethod_attribute, 346 u2 enclosing_method_class_index, 347 u2 enclosing_method_method_index, 348 TRAPS); 349 350 u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 351 const u1* const nest_members_attribute_start, 352 TRAPS); 353 354 u2 parse_classfile_permitted_subclasses_attribute(const ClassFileStream* const cfs, 355 const u1* const permitted_subclasses_attribute_start, 356 TRAPS); 357 358 u2 parse_classfile_loadable_descriptors_attribute(const ClassFileStream* const cfs, 359 const u1* const loadable_descriptors_attribute_start, 360 TRAPS); 361 362 u4 parse_classfile_record_attribute(const ClassFileStream* const cfs, 363 const ConstantPool* cp, 364 const u1* const record_attribute_start, 365 TRAPS); 366 367 void parse_classfile_attributes(const ClassFileStream* const cfs, 368 ConstantPool* cp, 369 ClassAnnotationCollector* parsed_annotations, 370 TRAPS); 371 372 void parse_classfile_synthetic_attribute(); 373 void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS); 374 void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 375 ConstantPool* cp, 376 u4 attribute_length, 377 TRAPS); 378 379 // Annotations handling 380 AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations, 381 int runtime_visible_annotations_length, 382 const u1* const runtime_invisible_annotations, 383 int runtime_invisible_annotations_length, 384 TRAPS); 385 386 void set_precomputed_flags(InstanceKlass* k); 387 388 // Format checker methods 389 void classfile_parse_error(const char* msg, TRAPS) const; 390 void classfile_parse_error(const char* msg, int index, TRAPS) const; 391 void classfile_parse_error(const char* msg, const char *name, TRAPS) const; 392 void classfile_parse_error(const char* msg, 393 int index, 394 const char *name, 395 TRAPS) const; 396 void classfile_parse_error(const char* msg, 397 const char* name, 398 const char* signature, 399 TRAPS) const; 400 401 void classfile_icce_error(const char* msg, 402 const Klass* k, 403 TRAPS) const; 404 405 void classfile_ucve_error(const char* msg, 406 const Symbol* class_name, 407 u2 major, 408 u2 minor, 409 TRAPS) const; 410 411 inline void guarantee_property(bool b, const char* msg, TRAPS) const { 412 if (!b) { classfile_parse_error(msg, THREAD); return; } 413 } 414 415 void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN; 416 void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN; 417 418 inline void assert_property(bool b, const char* msg, TRAPS) const { 419 #ifdef ASSERT 420 if (!b) { 421 report_assert_property_failure(msg, THREAD); 422 } 423 #endif 424 } 425 426 inline void assert_property(bool b, const char* msg, int index, TRAPS) const { 427 #ifdef ASSERT 428 if (!b) { 429 report_assert_property_failure(msg, index, THREAD); 430 } 431 #endif 432 } 433 434 inline void check_property(bool property, 435 const char* msg, 436 int index, 437 TRAPS) const { 438 if (_need_verify) { 439 guarantee_property(property, msg, index, CHECK); 440 } else { 441 assert_property(property, msg, index, CHECK); 442 } 443 } 444 445 inline void check_property(bool property, const char* msg, TRAPS) const { 446 if (_need_verify) { 447 guarantee_property(property, msg, CHECK); 448 } else { 449 assert_property(property, msg, CHECK); 450 } 451 } 452 453 inline void guarantee_property(bool b, 454 const char* msg, 455 int index, 456 TRAPS) const { 457 if (!b) { classfile_parse_error(msg, index, THREAD); return; } 458 } 459 460 inline void guarantee_property(bool b, 461 const char* msg, 462 const char *name, 463 TRAPS) const { 464 if (!b) { classfile_parse_error(msg, name, THREAD); return; } 465 } 466 467 inline void guarantee_property(bool b, 468 const char* msg, 469 int index, 470 const char *name, 471 TRAPS) const { 472 if (!b) { classfile_parse_error(msg, index, name, THREAD); return; } 473 } 474 475 void throwIllegalSignature(const char* type, 476 const Symbol* name, 477 const Symbol* sig, 478 TRAPS) const; 479 480 void verify_constantvalue(const ConstantPool* const cp, 481 int constantvalue_index, 482 int signature_index, 483 TRAPS) const; 484 485 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const; 486 void verify_legal_class_name(const Symbol* name, TRAPS) const; 487 void verify_legal_field_name(const Symbol* name, TRAPS) const; 488 void verify_legal_method_name(const Symbol* name, TRAPS) const; 489 490 bool legal_field_signature(const Symbol* signature, TRAPS) const; 491 492 void verify_legal_field_signature(const Symbol* fieldname, 493 const Symbol* signature, 494 TRAPS) const; 495 int verify_legal_method_signature(const Symbol* methodname, 496 const Symbol* signature, 497 TRAPS) const; 498 void verify_legal_name_with_signature(const Symbol* name, 499 const Symbol* signature, 500 TRAPS) const; 501 502 void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS); 503 504 void verify_legal_class_modifiers(jint flags, const char* name, bool is_Object, TRAPS) const; 505 void verify_legal_field_modifiers(jint flags, 506 AccessFlags class_access_flags, 507 TRAPS) const; 508 void verify_legal_method_modifiers(jint flags, 509 AccessFlags class_access_flags, 510 const Symbol* name, 511 TRAPS) const; 512 513 void check_super_class_access(const InstanceKlass* this_klass, 514 TRAPS); 515 516 void check_super_interface_access(const InstanceKlass* this_klass, 517 TRAPS); 518 519 const char* skip_over_field_signature(const char* signature, 520 bool void_ok, 521 unsigned int length, 522 TRAPS) const; 523 524 // Wrapper for constantTag.is_klass_[or_]reference. 525 // In older versions of the VM, Klass*s cannot sneak into early phases of 526 // constant pool construction, but in later versions they can. 527 // %%% Let's phase out the old is_klass_reference. 528 bool valid_klass_reference_at(int index) const { 529 return _cp->is_within_bounds(index) && 530 _cp->tag_at(index).is_klass_or_reference(); 531 } 532 533 // Checks that the cpool index is in range and is a utf8 534 bool valid_symbol_at(int cpool_index) const { 535 return _cp->is_within_bounds(cpool_index) && 536 _cp->tag_at(cpool_index).is_utf8(); 537 } 538 539 void copy_localvariable_table(const ConstMethod* cm, 540 int lvt_cnt, 541 u2* const localvariable_table_length, 542 const unsafe_u2** const localvariable_table_start, 543 int lvtt_cnt, 544 u2* const localvariable_type_table_length, 545 const unsafe_u2** const localvariable_type_table_start, 546 TRAPS); 547 548 void copy_method_annotations(ConstMethod* cm, 549 const u1* runtime_visible_annotations, 550 int runtime_visible_annotations_length, 551 const u1* runtime_invisible_annotations, 552 int runtime_invisible_annotations_length, 553 const u1* runtime_visible_parameter_annotations, 554 int runtime_visible_parameter_annotations_length, 555 const u1* runtime_invisible_parameter_annotations, 556 int runtime_invisible_parameter_annotations_length, 557 const u1* runtime_visible_type_annotations, 558 int runtime_visible_type_annotations_length, 559 const u1* runtime_invisible_type_annotations, 560 int runtime_invisible_type_annotations_length, 561 const u1* annotation_default, 562 int annotation_default_length, 563 TRAPS); 564 565 void update_class_name(Symbol* new_name); 566 567 // Check if the class file supports inline types 568 bool supports_inline_types() const; 569 570 public: 571 ClassFileParser(ClassFileStream* stream, 572 Symbol* name, 573 ClassLoaderData* loader_data, 574 const ClassLoadInfo* cl_info, 575 Publicity pub_level, 576 TRAPS); 577 578 ~ClassFileParser(); 579 580 InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, const ClassInstanceInfo& cl_inst_info, TRAPS); 581 582 const ClassFileStream* clone_stream() const; 583 584 void set_klass_to_deallocate(InstanceKlass* klass); 585 586 int static_field_size() const; 587 int total_oop_map_count() const; 588 jint layout_size() const; 589 590 int vtable_size() const { return _vtable_size; } 591 int itable_size() const { return _itable_size; } 592 593 u2 this_class_index() const { return _this_class_index; } 594 595 bool is_hidden() const { return _is_hidden; } 596 bool is_interface() const { return _access_flags.is_interface(); } 597 // Being an inline type means being a concrete value class 598 bool is_inline_type() const { return !_access_flags.is_identity_class() && !_access_flags.is_interface() && !_access_flags.is_abstract(); } 599 bool is_abstract_class() const { return _access_flags.is_abstract(); } 600 bool is_identity_class() const { return _access_flags.is_identity_class(); } 601 bool has_inline_fields() const { return _has_inline_type_fields; } 602 603 u2 java_fields_count() const { return _java_fields_count; } 604 605 ClassLoaderData* loader_data() const { return _loader_data; } 606 const Symbol* class_name() const { return _class_name; } 607 const InstanceKlass* super_klass() const { return _super_klass; } 608 609 ReferenceType super_reference_type() const; 610 bool is_instance_ref_klass() const; 611 bool is_java_lang_ref_Reference_subclass() const; 612 613 AccessFlags access_flags() const { return _access_flags; } 614 615 bool is_internal() const { return INTERNAL == _pub_level; } 616 617 bool is_class_in_loadable_descriptors_attribute(Symbol *klass); 618 619 static bool verify_unqualified_name(const char* name, unsigned int length, int type); 620 621 #ifdef ASSERT 622 static bool is_internal_format(Symbol* class_name); 623 #endif 624 625 }; 626 627 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP