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 bool _is_empty_value; 257 258 // precomputed flags 259 bool _has_finalizer; 260 bool _has_empty_finalizer; 261 bool _has_vanilla_constructor; 262 int _max_bootstrap_specifier_index; // detects BSS values 263 264 void parse_stream(const ClassFileStream* const stream, TRAPS); 265 266 void post_process_parsed_stream(const ClassFileStream* const stream, 267 ConstantPool* cp, 268 TRAPS); 269 270 void prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS); 271 void fix_unsafe_anonymous_class_name(TRAPS); 272 273 void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, TRAPS); 274 void set_klass(InstanceKlass* instance); 275 276 void set_class_bad_constant_seen(short bad_constant); 277 short class_bad_constant_seen() { return _bad_constant_seen; } 278 void set_class_synthetic_flag(bool x) { _synthetic_flag = x; } 279 void set_class_sourcefile_index(u2 x) { _sourcefile_index = x; } 280 void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; } 281 void set_class_sde_buffer(const char* x, int len) { _sde_buffer = x; _sde_length = len; } 282 283 void create_combined_annotations(TRAPS); 284 void apply_parsed_class_attributes(InstanceKlass* k); // update k 285 void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS); 286 void clear_class_metadata(); 287 288 // Constant pool parsing 289 void parse_constant_pool_entries(const ClassFileStream* const stream, 290 ConstantPool* cp, 291 const int length, 292 TRAPS); 293 294 void parse_constant_pool(const ClassFileStream* const cfs, 295 ConstantPool* const cp, 296 const int length, 297 TRAPS); 298 299 // Interface parsing 300 void parse_interfaces(const ClassFileStream* const stream, 301 const int itfs_len, 302 ConstantPool* const cp, 303 bool* has_nonstatic_concrete_methods, 304 TRAPS); 305 306 const InstanceKlass* parse_super_class(ConstantPool* const cp, 307 const int super_class_index, 308 const bool need_verify, 309 TRAPS); 310 311 // Field parsing 312 void parse_field_attributes(const ClassFileStream* const cfs, 313 u2 attributes_count, 314 bool is_static, 315 u2 signature_index, 316 u2* const constantvalue_index_addr, 317 bool* const is_synthetic_addr, 318 u2* const generic_signature_index_addr, 319 FieldAnnotationCollector* parsed_annotations, 320 TRAPS); 321 322 void parse_fields(const ClassFileStream* const cfs, 323 bool is_interface, 324 bool is_value_type, 325 FieldAllocationCount* const fac, 326 ConstantPool* cp, 327 const int cp_size, 328 u2* const java_fields_count_ptr, 329 TRAPS); 330 331 // Method parsing 332 Method* parse_method(const ClassFileStream* const cfs, 333 bool is_interface, 334 bool is_value_type, 335 const ConstantPool* cp, 336 AccessFlags* const promoted_flags, 337 TRAPS); 338 339 void parse_methods(const ClassFileStream* const cfs, 340 bool is_interface, 341 bool is_value_type, 342 AccessFlags* const promoted_flags, 343 bool* const has_final_method, 344 bool* const declares_nonstatic_concrete_methods, 345 TRAPS); 346 347 const unsafe_u2* parse_exception_table(const ClassFileStream* const stream, 348 u4 code_length, 349 u4 exception_table_length, 350 TRAPS); 351 352 void parse_linenumber_table(u4 code_attribute_length, 353 u4 code_length, 354 CompressedLineNumberWriteStream**const write_stream, 355 TRAPS); 356 357 const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs, 358 u4 code_length, 359 u2 max_locals, 360 u4 code_attribute_length, 361 u2* const localvariable_table_length, 362 bool isLVTT, 363 TRAPS); 364 365 const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs, 366 u2* const checked_exceptions_length, 367 u4 method_attribute_length, 368 TRAPS); 369 370 // Classfile attribute parsing 371 u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS); 372 void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS); 373 void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 374 int length, 375 TRAPS); 376 377 u2 parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 378 const u1* const inner_classes_attribute_start, 379 bool parsed_enclosingmethod_attribute, 380 u2 enclosing_method_class_index, 381 u2 enclosing_method_method_index, 382 TRAPS); 383 384 u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 385 const u1* const nest_members_attribute_start, 386 TRAPS); 387 388 void parse_classfile_attributes(const ClassFileStream* const cfs, 389 ConstantPool* cp, 390 ClassAnnotationCollector* parsed_annotations, 391 TRAPS); 392 393 void parse_classfile_synthetic_attribute(TRAPS); 394 void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS); 395 void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 396 ConstantPool* cp, 397 u4 attribute_length, 398 TRAPS); 399 400 // Annotations handling 401 AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations, 402 int runtime_visible_annotations_length, 403 const u1* const runtime_invisible_annotations, 404 int runtime_invisible_annotations_length, 405 TRAPS); 406 407 void set_precomputed_flags(InstanceKlass* k, TRAPS); 408 409 // Format checker methods 410 void classfile_parse_error(const char* msg, TRAPS) const; 411 void classfile_parse_error(const char* msg, int index, TRAPS) const; 412 void classfile_parse_error(const char* msg, const char *name, TRAPS) const; 413 void classfile_parse_error(const char* msg, 414 int index, 415 const char *name, 416 TRAPS) const; 417 void classfile_parse_error(const char* msg, 418 const char* name, 419 const char* signature, 420 TRAPS) const; 421 422 inline void guarantee_property(bool b, const char* msg, TRAPS) const { 423 if (!b) { classfile_parse_error(msg, CHECK); } 424 } 425 426 void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN; 427 void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN; 428 429 inline void assert_property(bool b, const char* msg, TRAPS) const { 430 #ifdef ASSERT 431 if (!b) { 432 report_assert_property_failure(msg, THREAD); 433 } 434 #endif 435 } 436 437 inline void assert_property(bool b, const char* msg, int index, TRAPS) const { 438 #ifdef ASSERT 439 if (!b) { 440 report_assert_property_failure(msg, index, THREAD); 441 } 442 #endif 443 } 444 445 inline void check_property(bool property, 446 const char* msg, 447 int index, 448 TRAPS) const { 449 if (_need_verify) { 450 guarantee_property(property, msg, index, CHECK); 451 } else { 452 assert_property(property, msg, index, CHECK); 453 } 454 } 455 456 inline void check_property(bool property, const char* msg, TRAPS) const { 457 if (_need_verify) { 458 guarantee_property(property, msg, CHECK); 459 } else { 460 assert_property(property, msg, CHECK); 461 } 462 } 463 464 inline void guarantee_property(bool b, 465 const char* msg, 466 int index, 467 TRAPS) const { 468 if (!b) { classfile_parse_error(msg, index, CHECK); } 469 } 470 471 inline void guarantee_property(bool b, 472 const char* msg, 473 const char *name, 474 TRAPS) const { 475 if (!b) { classfile_parse_error(msg, name, CHECK); } 476 } 477 478 inline void guarantee_property(bool b, 479 const char* msg, 480 int index, 481 const char *name, 482 TRAPS) const { 483 if (!b) { classfile_parse_error(msg, index, name, CHECK); } 484 } 485 486 void throwIllegalSignature(const char* type, 487 const Symbol* name, 488 const Symbol* sig, 489 TRAPS) const; 490 491 void throwValueTypeLimitation(THREAD_AND_LOCATION_DECL, 492 const char* msg, 493 const Symbol* name = NULL, 494 const Symbol* sig = NULL) const; 495 496 void verify_constantvalue(const ConstantPool* const cp, 497 int constantvalue_index, 498 int signature_index, 499 TRAPS) const; 500 501 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const; 502 void verify_legal_class_name(const Symbol* name, TRAPS) const; 503 void verify_legal_field_name(const Symbol* name, TRAPS) const; 504 void verify_legal_method_name(const Symbol* name, TRAPS) const; 505 506 void verify_legal_field_signature(const Symbol* fieldname, 507 const Symbol* signature, 508 TRAPS) const; 509 int verify_legal_method_signature(const Symbol* methodname, 510 const Symbol* signature, 511 TRAPS) const; 512 513 void verify_legal_class_modifiers(jint flags, TRAPS) const; 514 void verify_legal_field_modifiers(jint flags, 515 bool is_interface, 516 bool is_value_type, 517 TRAPS) const; 518 void verify_legal_method_modifiers(jint flags, 519 bool is_interface, 520 bool is_value_type, 521 const Symbol* name, 522 TRAPS) const; 523 524 const char* skip_over_field_signature(const char* signature, 525 bool void_ok, 526 unsigned int length, 527 TRAPS) const; 528 529 bool has_cp_patch_at(int index) const { 530 assert(index >= 0, "oob"); 531 return (_cp_patches != NULL 532 && index < _cp_patches->length() 533 && _cp_patches->adr_at(index)->not_null()); 534 } 535 536 Handle cp_patch_at(int index) const { 537 assert(has_cp_patch_at(index), "oob"); 538 return _cp_patches->at(index); 539 } 540 541 Handle clear_cp_patch_at(int index); 542 543 void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name); 544 void patch_constant_pool(ConstantPool* cp, 545 int index, 546 Handle patch, 547 TRAPS); 548 549 // Wrapper for constantTag.is_klass_[or_]reference. 550 // In older versions of the VM, Klass*s cannot sneak into early phases of 551 // constant pool construction, but in later versions they can. 552 // %%% Let's phase out the old is_klass_reference. 553 bool valid_klass_reference_at(int index) const { 554 return _cp->is_within_bounds(index) && 555 _cp->tag_at(index).is_klass_or_reference(); 556 } 557 558 // Checks that the cpool index is in range and is a utf8 559 bool valid_symbol_at(int cpool_index) const { 560 return _cp->is_within_bounds(cpool_index) && 561 _cp->tag_at(cpool_index).is_utf8(); 562 } 563 564 void copy_localvariable_table(const ConstMethod* cm, 565 int lvt_cnt, 566 u2* const localvariable_table_length, 567 const unsafe_u2** const localvariable_table_start, 568 int lvtt_cnt, 569 u2* const localvariable_type_table_length, 570 const unsafe_u2** const localvariable_type_table_start, 571 TRAPS); 572 573 void copy_method_annotations(ConstMethod* cm, 574 const u1* runtime_visible_annotations, 575 int runtime_visible_annotations_length, 576 const u1* runtime_invisible_annotations, 577 int runtime_invisible_annotations_length, 578 const u1* runtime_visible_parameter_annotations, 579 int runtime_visible_parameter_annotations_length, 580 const u1* runtime_invisible_parameter_annotations, 581 int runtime_invisible_parameter_annotations_length, 582 const u1* runtime_visible_type_annotations, 583 int runtime_visible_type_annotations_length, 584 const u1* runtime_invisible_type_annotations, 585 int runtime_invisible_type_annotations_length, 586 const u1* annotation_default, 587 int annotation_default_length, 588 TRAPS); 589 590 // lays out fields in class and returns the total oopmap count 591 void layout_fields(ConstantPool* cp, 592 const FieldAllocationCount* fac, 593 const ClassAnnotationCollector* parsed_annotations, 594 FieldLayoutInfo* info, 595 TRAPS); 596 597 void update_class_name(Symbol* new_name); 598 599 // Check if the class file supports value types 600 bool supports_value_types() const; 601 602 public: 603 ClassFileParser(ClassFileStream* stream, 604 Symbol* name, 605 ClassLoaderData* loader_data, 606 Handle protection_domain, 607 const InstanceKlass* unsafe_anonymous_host, 608 GrowableArray<Handle>* cp_patches, 609 Publicity pub_level, 610 TRAPS); 611 612 ~ClassFileParser(); 613 614 InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, TRAPS); 615 616 const ClassFileStream* clone_stream() const; 617 618 void set_klass_to_deallocate(InstanceKlass* klass); 619 620 int static_field_size() const; 621 int total_oop_map_count() const; 622 jint layout_size() const; 623 624 int vtable_size() const { return _vtable_size; } 625 int itable_size() const { return _itable_size; } 626 627 u2 this_class_index() const { return _this_class_index; } 628 629 bool is_unsafe_anonymous() const { return _unsafe_anonymous_host != NULL; } 630 bool is_interface() const { return _access_flags.is_interface(); } 631 bool is_value_type() const { return _access_flags.is_value_type(); } 632 bool is_value_capable_class() const; 633 bool has_flattenable_fields() const { return _has_flattenable_fields; } 634 635 u2 java_fields_count() const { return _java_fields_count; } 636 637 const InstanceKlass* unsafe_anonymous_host() const { return _unsafe_anonymous_host; } 638 const GrowableArray<Handle>* cp_patches() const { return _cp_patches; } 639 ClassLoaderData* loader_data() const { return _loader_data; } 640 const Symbol* class_name() const { return _class_name; } 641 const InstanceKlass* super_klass() const { return _super_klass; } 642 643 ReferenceType reference_type() const { return _rt; } 644 AccessFlags access_flags() const { return _access_flags; } 645 646 bool is_internal() const { return INTERNAL == _pub_level; } 647 648 static bool verify_unqualified_name(const char* name, unsigned int length, int type); 649 650 #ifdef ASSERT 651 static bool is_internal_format(Symbol* class_name); 652 #endif 653 654 }; 655 656 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP