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