1 /* 2 * Copyright (c) 1997, 2020, 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/instanceKlass.hpp" 32 #include "oops/typeArrayOop.hpp" 33 #include "utilities/accessFlags.hpp" 34 35 class Annotations; 36 template <typename T> 37 class Array; 38 class ClassFileStream; 39 class ClassLoaderData; 40 class CompressedLineNumberWriteStream; 41 class ConstMethod; 42 class FieldInfo; 43 template <typename T> 44 class GrowableArray; 45 class InstanceKlass; 46 class RecordComponent; 47 class Symbol; 48 class TempNewSymbol; 49 class FieldLayoutBuilder; 50 51 // Utility to collect and compact oop maps during layout 52 class OopMapBlocksBuilder : public ResourceObj { 53 public: 54 OopMapBlock* _nonstatic_oop_maps; 55 unsigned int _nonstatic_oop_map_count; 56 unsigned int _max_nonstatic_oop_maps; 57 58 OopMapBlocksBuilder(unsigned int max_blocks); 59 OopMapBlock* last_oop_map() const; 60 void initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks); 61 void add(int offset, int count); 62 void copy(OopMapBlock* dst); 63 void compact(); 64 void print_on(outputStream* st) const; 65 void print_value_on(outputStream* st) const; 66 }; 67 68 // Values needed for oopmap and InstanceKlass creation 69 class FieldLayoutInfo : public ResourceObj { 70 public: 71 OopMapBlocksBuilder* oop_map_blocks; 72 int _instance_size; 73 int _nonstatic_field_size; 74 int _static_field_size; 75 bool _has_nonstatic_fields; 76 bool _is_naturally_atomic; 77 }; 78 79 // Parser for for .class files 80 // 81 // The bytes describing the class file structure is read from a Stream object 82 83 class ClassFileParser { 84 friend class FieldLayoutBuilder; 85 friend class FieldLayout; 86 87 class ClassAnnotationCollector; 88 class FieldAllocationCount; 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 const Symbol* _requested_name; 114 Symbol* _class_name; 115 mutable ClassLoaderData* _loader_data; 116 const InstanceKlass* _unsafe_anonymous_host; 117 GrowableArray<Handle>* _cp_patches; // overrides for CP entries 118 int _num_patched_klasses; 119 int _max_num_patched_klasses; 120 int _orig_cp_size; 121 int _first_patched_klass_resolved_index; 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<u2>* _fields; 129 Array<Method*>* _methods; 130 Array<u2>* _inner_classes; 131 Array<u2>* _nest_members; 132 u2 _nest_host; 133 Array<RecordComponent*>* _record_components; 134 Array<InstanceKlass*>* _local_interfaces; 135 Array<InstanceKlass*>* _transitive_interfaces; 136 Annotations* _combined_annotations; 137 AnnotationArray* _class_annotations; 138 AnnotationArray* _class_type_annotations; 139 Array<AnnotationArray*>* _fields_annotations; 140 Array<AnnotationArray*>* _fields_type_annotations; 141 InstanceKlass* _klass; // InstanceKlass* once created. 142 InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed 143 144 ClassAnnotationCollector* _parsed_annotations; 145 FieldAllocationCount* _fac; 146 FieldLayoutInfo* _field_info; 147 const intArray* _method_ordering; 148 GrowableArray<Method*>* _all_mirandas; 149 150 enum { fixed_buffer_size = 128 }; 151 u_char _linenumbertable_buffer[fixed_buffer_size]; 152 153 // Size of Java vtable (in words) 154 int _vtable_size; 155 int _itable_size; 156 157 int _num_miranda_methods; 158 159 int _alignment; 160 int _first_field_offset; 161 int _exact_size_in_bytes; 162 163 ReferenceType _rt; 164 Handle _protection_domain; 165 AccessFlags _access_flags; 166 167 // for tracing and notifications 168 Publicity _pub_level; 169 170 // Used to keep track of whether a constant pool item 19 or 20 is found. These 171 // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed 172 // in regular class files. For class file version >= 53, a CFE cannot be thrown 173 // immediately when these are seen because a NCDFE must be thrown if the class's 174 // access_flags have ACC_MODULE set. But, the access_flags haven't been looked 175 // at yet. So, the bad constant pool item is cached here. A value of zero 176 // means that no constant pool item 19 or 20 was found. 177 short _bad_constant_seen; 178 179 // class attributes parsed before the instance klass is created: 180 bool _synthetic_flag; 181 int _sde_length; 182 const char* _sde_buffer; 183 u2 _sourcefile_index; 184 u2 _generic_signature_index; 185 186 u2 _major_version; 187 u2 _minor_version; 188 u2 _this_class_index; 189 u2 _super_class_index; 190 u2 _itfs_len; 191 u2 _java_fields_count; 192 193 bool _need_verify; 194 bool _relax_verify; 195 196 bool _has_nonstatic_concrete_methods; 197 bool _declares_nonstatic_concrete_methods; 198 bool _has_final_method; 199 bool _has_contended_fields; 200 201 bool _has_flattenable_fields; 202 bool _is_empty_value; 203 bool _is_naturally_atomic; 204 bool _is_declared_atomic; 205 206 // precomputed flags 207 bool _has_finalizer; 208 bool _has_empty_finalizer; 209 bool _has_vanilla_constructor; 210 int _max_bootstrap_specifier_index; // detects BSS values 211 212 void parse_stream(const ClassFileStream* const stream, TRAPS); 213 214 void post_process_parsed_stream(const ClassFileStream* const stream, 215 ConstantPool* cp, 216 TRAPS); 217 218 void prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS); 219 void fix_unsafe_anonymous_class_name(TRAPS); 220 221 void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, TRAPS); 222 void set_klass(InstanceKlass* instance); 223 224 void set_class_bad_constant_seen(short bad_constant); 225 short class_bad_constant_seen() { return _bad_constant_seen; } 226 void set_class_synthetic_flag(bool x) { _synthetic_flag = x; } 227 void set_class_sourcefile_index(u2 x) { _sourcefile_index = x; } 228 void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; } 229 void set_class_sde_buffer(const char* x, int len) { _sde_buffer = x; _sde_length = len; } 230 231 void create_combined_annotations(TRAPS); 232 void apply_parsed_class_attributes(InstanceKlass* k); // update k 233 void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS); 234 void clear_class_metadata(); 235 236 // Constant pool parsing 237 void parse_constant_pool_entries(const ClassFileStream* const stream, 238 ConstantPool* cp, 239 const int length, 240 TRAPS); 241 242 void parse_constant_pool(const ClassFileStream* const cfs, 243 ConstantPool* const cp, 244 const int length, 245 TRAPS); 246 247 // Interface parsing 248 void parse_interfaces(const ClassFileStream* const stream, 249 const int itfs_len, 250 ConstantPool* const cp, 251 bool* has_nonstatic_concrete_methods, 252 bool* is_declared_atomic, 253 TRAPS); 254 255 const InstanceKlass* parse_super_class(ConstantPool* const cp, 256 const int super_class_index, 257 const bool need_verify, 258 TRAPS); 259 260 // Field parsing 261 void parse_field_attributes(const ClassFileStream* const cfs, 262 u2 attributes_count, 263 bool is_static, 264 u2 signature_index, 265 u2* const constantvalue_index_addr, 266 bool* const is_synthetic_addr, 267 u2* const generic_signature_index_addr, 268 FieldAnnotationCollector* parsed_annotations, 269 TRAPS); 270 271 void parse_fields(const ClassFileStream* const cfs, 272 bool is_interface, 273 bool is_value_type, 274 FieldAllocationCount* const fac, 275 ConstantPool* cp, 276 const int cp_size, 277 u2* const java_fields_count_ptr, 278 TRAPS); 279 280 // Method parsing 281 Method* parse_method(const ClassFileStream* const cfs, 282 bool is_interface, 283 bool is_value_type, 284 const ConstantPool* cp, 285 AccessFlags* const promoted_flags, 286 TRAPS); 287 288 void parse_methods(const ClassFileStream* const cfs, 289 bool is_interface, 290 bool is_value_type, 291 AccessFlags* const promoted_flags, 292 bool* const has_final_method, 293 bool* const declares_nonstatic_concrete_methods, 294 TRAPS); 295 296 const unsafe_u2* parse_exception_table(const ClassFileStream* const stream, 297 u4 code_length, 298 u4 exception_table_length, 299 TRAPS); 300 301 void parse_linenumber_table(u4 code_attribute_length, 302 u4 code_length, 303 CompressedLineNumberWriteStream**const write_stream, 304 TRAPS); 305 306 const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs, 307 u4 code_length, 308 u2 max_locals, 309 u4 code_attribute_length, 310 u2* const localvariable_table_length, 311 bool isLVTT, 312 TRAPS); 313 314 const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs, 315 u2* const checked_exceptions_length, 316 u4 method_attribute_length, 317 TRAPS); 318 319 // Classfile attribute parsing 320 u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS); 321 void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS); 322 void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 323 int length, 324 TRAPS); 325 326 u2 parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 327 const u1* const inner_classes_attribute_start, 328 bool parsed_enclosingmethod_attribute, 329 u2 enclosing_method_class_index, 330 u2 enclosing_method_method_index, 331 TRAPS); 332 333 u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 334 const u1* const nest_members_attribute_start, 335 TRAPS); 336 337 u2 parse_classfile_record_attribute(const ClassFileStream* const cfs, 338 const ConstantPool* cp, 339 const u1* const record_attribute_start, 340 TRAPS); 341 342 bool supports_records(); 343 344 void parse_classfile_attributes(const ClassFileStream* const cfs, 345 ConstantPool* cp, 346 ClassAnnotationCollector* parsed_annotations, 347 TRAPS); 348 349 void parse_classfile_synthetic_attribute(TRAPS); 350 void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS); 351 void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 352 ConstantPool* cp, 353 u4 attribute_length, 354 TRAPS); 355 356 // Annotations handling 357 AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations, 358 int runtime_visible_annotations_length, 359 const u1* const runtime_invisible_annotations, 360 int runtime_invisible_annotations_length, 361 TRAPS); 362 363 void set_precomputed_flags(InstanceKlass* k); 364 365 // Format checker methods 366 void classfile_parse_error(const char* msg, TRAPS) const; 367 void classfile_parse_error(const char* msg, int index, TRAPS) const; 368 void classfile_parse_error(const char* msg, const char *name, TRAPS) const; 369 void classfile_parse_error(const char* msg, 370 int index, 371 const char *name, 372 TRAPS) const; 373 void classfile_parse_error(const char* msg, 374 const char* name, 375 const char* signature, 376 TRAPS) const; 377 378 inline void guarantee_property(bool b, const char* msg, TRAPS) const { 379 if (!b) { classfile_parse_error(msg, CHECK); } 380 } 381 382 void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN; 383 void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN; 384 385 inline void assert_property(bool b, const char* msg, TRAPS) const { 386 #ifdef ASSERT 387 if (!b) { 388 report_assert_property_failure(msg, THREAD); 389 } 390 #endif 391 } 392 393 inline void assert_property(bool b, const char* msg, int index, TRAPS) const { 394 #ifdef ASSERT 395 if (!b) { 396 report_assert_property_failure(msg, index, THREAD); 397 } 398 #endif 399 } 400 401 inline void check_property(bool property, 402 const char* msg, 403 int index, 404 TRAPS) const { 405 if (_need_verify) { 406 guarantee_property(property, msg, index, CHECK); 407 } else { 408 assert_property(property, msg, index, CHECK); 409 } 410 } 411 412 inline void check_property(bool property, const char* msg, TRAPS) const { 413 if (_need_verify) { 414 guarantee_property(property, msg, CHECK); 415 } else { 416 assert_property(property, msg, CHECK); 417 } 418 } 419 420 inline void guarantee_property(bool b, 421 const char* msg, 422 int index, 423 TRAPS) const { 424 if (!b) { classfile_parse_error(msg, index, CHECK); } 425 } 426 427 inline void guarantee_property(bool b, 428 const char* msg, 429 const char *name, 430 TRAPS) const { 431 if (!b) { classfile_parse_error(msg, name, CHECK); } 432 } 433 434 inline void guarantee_property(bool b, 435 const char* msg, 436 int index, 437 const char *name, 438 TRAPS) const { 439 if (!b) { classfile_parse_error(msg, index, name, CHECK); } 440 } 441 442 void throwIllegalSignature(const char* type, 443 const Symbol* name, 444 const Symbol* sig, 445 TRAPS) const; 446 447 void throwValueTypeLimitation(THREAD_AND_LOCATION_DECL, 448 const char* msg, 449 const Symbol* name = NULL, 450 const Symbol* sig = NULL) const; 451 452 void verify_constantvalue(const ConstantPool* const cp, 453 int constantvalue_index, 454 int signature_index, 455 TRAPS) const; 456 457 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const; 458 void verify_legal_class_name(const Symbol* name, TRAPS) const; 459 void verify_legal_field_name(const Symbol* name, TRAPS) const; 460 void verify_legal_method_name(const Symbol* name, TRAPS) const; 461 462 void verify_legal_field_signature(const Symbol* fieldname, 463 const Symbol* signature, 464 TRAPS) const; 465 int verify_legal_method_signature(const Symbol* methodname, 466 const Symbol* signature, 467 TRAPS) const; 468 469 void verify_legal_class_modifiers(jint flags, TRAPS) const; 470 void verify_legal_field_modifiers(jint flags, 471 bool is_interface, 472 bool is_value_type, 473 TRAPS) const; 474 void verify_legal_method_modifiers(jint flags, 475 bool is_interface, 476 bool is_value_type, 477 const Symbol* name, 478 TRAPS) const; 479 480 const char* skip_over_field_signature(const char* signature, 481 bool void_ok, 482 unsigned int length, 483 TRAPS) const; 484 485 bool has_cp_patch_at(int index) const { 486 assert(index >= 0, "oob"); 487 return (_cp_patches != NULL 488 && index < _cp_patches->length() 489 && _cp_patches->adr_at(index)->not_null()); 490 } 491 492 Handle cp_patch_at(int index) const { 493 assert(has_cp_patch_at(index), "oob"); 494 return _cp_patches->at(index); 495 } 496 497 Handle clear_cp_patch_at(int index); 498 499 void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name); 500 void patch_constant_pool(ConstantPool* cp, 501 int index, 502 Handle patch, 503 TRAPS); 504 505 // Wrapper for constantTag.is_klass_[or_]reference. 506 // In older versions of the VM, Klass*s cannot sneak into early phases of 507 // constant pool construction, but in later versions they can. 508 // %%% Let's phase out the old is_klass_reference. 509 bool valid_klass_reference_at(int index) const { 510 return _cp->is_within_bounds(index) && 511 _cp->tag_at(index).is_klass_or_reference(); 512 } 513 514 // Checks that the cpool index is in range and is a utf8 515 bool valid_symbol_at(int cpool_index) const { 516 return _cp->is_within_bounds(cpool_index) && 517 _cp->tag_at(cpool_index).is_utf8(); 518 } 519 520 void copy_localvariable_table(const ConstMethod* cm, 521 int lvt_cnt, 522 u2* const localvariable_table_length, 523 const unsafe_u2** const localvariable_table_start, 524 int lvtt_cnt, 525 u2* const localvariable_type_table_length, 526 const unsafe_u2** const localvariable_type_table_start, 527 TRAPS); 528 529 void copy_method_annotations(ConstMethod* cm, 530 const u1* runtime_visible_annotations, 531 int runtime_visible_annotations_length, 532 const u1* runtime_invisible_annotations, 533 int runtime_invisible_annotations_length, 534 const u1* runtime_visible_parameter_annotations, 535 int runtime_visible_parameter_annotations_length, 536 const u1* runtime_invisible_parameter_annotations, 537 int runtime_invisible_parameter_annotations_length, 538 const u1* runtime_visible_type_annotations, 539 int runtime_visible_type_annotations_length, 540 const u1* runtime_invisible_type_annotations, 541 int runtime_invisible_type_annotations_length, 542 const u1* annotation_default, 543 int annotation_default_length, 544 TRAPS); 545 546 // lays out fields in class and returns the total oopmap count 547 void layout_fields(ConstantPool* cp, 548 const FieldAllocationCount* fac, 549 const ClassAnnotationCollector* parsed_annotations, 550 FieldLayoutInfo* info, 551 TRAPS); 552 553 void update_class_name(Symbol* new_name); 554 555 // Check if the class file supports value types 556 bool supports_value_types() const; 557 558 public: 559 ClassFileParser(ClassFileStream* stream, 560 Symbol* name, 561 ClassLoaderData* loader_data, 562 Handle protection_domain, 563 const InstanceKlass* unsafe_anonymous_host, 564 GrowableArray<Handle>* cp_patches, 565 Publicity pub_level, 566 TRAPS); 567 568 ~ClassFileParser(); 569 570 InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, TRAPS); 571 572 const ClassFileStream* clone_stream() const; 573 574 void set_klass_to_deallocate(InstanceKlass* klass); 575 576 int static_field_size() const; 577 int total_oop_map_count() const; 578 jint layout_size() const; 579 580 int vtable_size() const { return _vtable_size; } 581 int itable_size() const { return _itable_size; } 582 583 u2 this_class_index() const { return _this_class_index; } 584 585 bool is_unsafe_anonymous() const { return _unsafe_anonymous_host != NULL; } 586 bool is_interface() const { return _access_flags.is_interface(); } 587 bool is_value_type() const { return _access_flags.is_value_type(); } 588 bool is_value_capable_class() const; 589 bool has_flattenable_fields() const { return _has_flattenable_fields; } 590 591 u2 java_fields_count() const { return _java_fields_count; } 592 593 const InstanceKlass* unsafe_anonymous_host() const { return _unsafe_anonymous_host; } 594 const GrowableArray<Handle>* cp_patches() const { return _cp_patches; } 595 ClassLoaderData* loader_data() const { return _loader_data; } 596 const Symbol* class_name() const { return _class_name; } 597 const InstanceKlass* super_klass() const { return _super_klass; } 598 599 ReferenceType reference_type() const { return _rt; } 600 AccessFlags access_flags() const { return _access_flags; } 601 602 bool is_internal() const { return INTERNAL == _pub_level; } 603 604 static bool verify_unqualified_name(const char* name, unsigned int length, int type); 605 606 #ifdef ASSERT 607 static bool is_internal_format(Symbol* class_name); 608 #endif 609 610 }; 611 612 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP