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