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 ClassLoadInfo; 41 class ClassInstanceInfo; 42 class CompressedLineNumberWriteStream; 43 class ConstMethod; 44 class FieldInfo; 45 template <typename T> 46 class GrowableArray; 47 class InstanceKlass; 48 class RecordComponent; 49 class Symbol; 50 class TempNewSymbol; 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 }; 79 80 // Parser for for .class files 81 // 82 // The bytes describing the class file structure is read from a Stream object 83 84 class ClassFileParser { 85 friend class FieldLayoutBuilder; 86 friend class FieldLayout; 87 88 class ClassAnnotationCollector; 89 class FieldAllocationCount; 90 class FieldAnnotationCollector; 91 92 public: 93 // The ClassFileParser has an associated "publicity" level 94 // It is used to control which subsystems (if any) 95 // will observe the parsing (logging, events, tracing). 96 // Default level is "BROADCAST", which is equivalent to 97 // a "public" parsing attempt. 98 // 99 // "INTERNAL" level should be entirely private to the 100 // caller - this allows for internal reuse of ClassFileParser 101 // 102 enum Publicity { 103 INTERNAL, 104 BROADCAST 105 }; 106 107 enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names 108 109 private: 110 // Potentially unaligned pointer to various 16-bit entries in the class file 111 typedef void unsafe_u2; 112 113 const ClassFileStream* _stream; // Actual input stream 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 const bool _is_hidden; 119 const bool _can_access_vm_annotations; 120 int _num_patched_klasses; 121 int _max_num_patched_klasses; 122 int _orig_cp_size; 123 int _first_patched_klass_resolved_index; 124 125 // Metadata created before the instance klass is created. Must be deallocated 126 // if not transferred to the InstanceKlass upon successful class loading 127 // in which case these pointers have been set to NULL. 128 const InstanceKlass* _super_klass; 129 ConstantPool* _cp; 130 Array<u2>* _fields; 131 Array<Method*>* _methods; 132 Array<u2>* _inner_classes; 133 Array<u2>* _nest_members; 134 u2 _nest_host; 135 Array<u2>* _permitted_subclasses; 136 Array<RecordComponent*>* _record_components; 137 Array<InstanceKlass*>* _local_interfaces; 138 Array<InstanceKlass*>* _transitive_interfaces; 139 Annotations* _combined_annotations; 140 AnnotationArray* _class_annotations; 141 AnnotationArray* _class_type_annotations; 142 Array<AnnotationArray*>* _fields_annotations; 143 Array<AnnotationArray*>* _fields_type_annotations; 144 InstanceKlass* _klass; // InstanceKlass* once created. 145 InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed 146 147 ClassAnnotationCollector* _parsed_annotations; 148 FieldAllocationCount* _fac; 149 FieldLayoutInfo* _field_info; 150 const intArray* _method_ordering; 151 GrowableArray<Method*>* _all_mirandas; 152 153 enum { fixed_buffer_size = 128 }; 154 u_char _linenumbertable_buffer[fixed_buffer_size]; 155 156 // Size of Java vtable (in words) 157 int _vtable_size; 158 int _itable_size; 159 160 int _num_miranda_methods; 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 // precomputed flags 201 bool _has_finalizer; 202 bool _has_empty_finalizer; 203 bool _has_vanilla_constructor; 204 int _max_bootstrap_specifier_index; // detects BSS values 205 206 void parse_stream(const ClassFileStream* const stream, TRAPS); 207 208 void mangle_hidden_class_name(InstanceKlass* const ik); 209 210 void post_process_parsed_stream(const ClassFileStream* const stream, 211 ConstantPool* cp, 212 TRAPS); 213 214 void prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS); 215 void fix_unsafe_anonymous_class_name(TRAPS); 216 217 void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, 218 const ClassInstanceInfo& cl_inst_info, TRAPS); 219 220 void set_klass(InstanceKlass* instance); 221 222 void set_class_bad_constant_seen(short bad_constant); 223 short class_bad_constant_seen() { return _bad_constant_seen; } 224 void set_class_synthetic_flag(bool x) { _synthetic_flag = x; } 225 void set_class_sourcefile_index(u2 x) { _sourcefile_index = x; } 226 void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; } 227 void set_class_sde_buffer(const char* x, int len) { _sde_buffer = x; _sde_length = len; } 228 229 void create_combined_annotations(TRAPS); 230 void apply_parsed_class_attributes(InstanceKlass* k); // update k 231 void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS); 232 void clear_class_metadata(); 233 234 // Constant pool parsing 235 void parse_constant_pool_entries(const ClassFileStream* const stream, 236 ConstantPool* cp, 237 const int length, 238 TRAPS); 239 240 void parse_constant_pool(const ClassFileStream* const cfs, 241 ConstantPool* const cp, 242 const int length, 243 TRAPS); 244 245 // Interface parsing 246 void parse_interfaces(const ClassFileStream* const stream, 247 const int itfs_len, 248 ConstantPool* const cp, 249 bool* has_nonstatic_concrete_methods, 250 TRAPS); 251 252 const InstanceKlass* parse_super_class(ConstantPool* const cp, 253 const int super_class_index, 254 const bool need_verify, 255 TRAPS); 256 257 // Field parsing 258 void parse_field_attributes(const ClassFileStream* const cfs, 259 u2 attributes_count, 260 bool is_static, 261 u2 signature_index, 262 u2* const constantvalue_index_addr, 263 bool* const is_synthetic_addr, 264 u2* const generic_signature_index_addr, 265 FieldAnnotationCollector* parsed_annotations, 266 TRAPS); 267 268 void parse_fields(const ClassFileStream* const cfs, 269 bool is_interface, 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 const ConstantPool* cp, 280 AccessFlags* const promoted_flags, 281 TRAPS); 282 283 void parse_methods(const ClassFileStream* const cfs, 284 bool is_interface, 285 AccessFlags* const promoted_flags, 286 bool* const has_final_method, 287 bool* const declares_nonstatic_concrete_methods, 288 TRAPS); 289 290 const unsafe_u2* parse_exception_table(const ClassFileStream* const stream, 291 u4 code_length, 292 u4 exception_table_length, 293 TRAPS); 294 295 void parse_linenumber_table(u4 code_attribute_length, 296 u4 code_length, 297 CompressedLineNumberWriteStream**const write_stream, 298 TRAPS); 299 300 const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs, 301 u4 code_length, 302 u2 max_locals, 303 u4 code_attribute_length, 304 u2* const localvariable_table_length, 305 bool isLVTT, 306 TRAPS); 307 308 const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs, 309 u2* const checked_exceptions_length, 310 u4 method_attribute_length, 311 TRAPS); 312 313 // Classfile attribute parsing 314 u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS); 315 void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS); 316 void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 317 int length, 318 TRAPS); 319 320 u2 parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 321 const u1* const inner_classes_attribute_start, 322 bool parsed_enclosingmethod_attribute, 323 u2 enclosing_method_class_index, 324 u2 enclosing_method_method_index, 325 TRAPS); 326 327 u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 328 const u1* const nest_members_attribute_start, 329 TRAPS); 330 331 u2 parse_classfile_permitted_subclasses_attribute(const ClassFileStream* const cfs, 332 const u1* const permitted_subclasses_attribute_start, 333 TRAPS); 334 335 u2 parse_classfile_record_attribute(const ClassFileStream* const cfs, 336 const ConstantPool* cp, 337 const u1* const record_attribute_start, 338 TRAPS); 339 340 bool supports_sealed_types(); 341 bool supports_records(); 342 343 void parse_classfile_attributes(const ClassFileStream* const cfs, 344 ConstantPool* cp, 345 ClassAnnotationCollector* parsed_annotations, 346 TRAPS); 347 348 void parse_classfile_synthetic_attribute(TRAPS); 349 void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS); 350 void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 351 ConstantPool* cp, 352 u4 attribute_length, 353 TRAPS); 354 355 // Annotations handling 356 AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations, 357 int runtime_visible_annotations_length, 358 const u1* const runtime_invisible_annotations, 359 int runtime_invisible_annotations_length, 360 TRAPS); 361 362 void set_precomputed_flags(InstanceKlass* k); 363 364 // Format checker methods 365 void classfile_parse_error(const char* msg, TRAPS) const; 366 void classfile_parse_error(const char* msg, int index, TRAPS) const; 367 void classfile_parse_error(const char* msg, const char *name, TRAPS) const; 368 void classfile_parse_error(const char* msg, 369 int index, 370 const char *name, 371 TRAPS) const; 372 void classfile_parse_error(const char* msg, 373 const char* name, 374 const char* signature, 375 TRAPS) const; 376 377 inline void guarantee_property(bool b, const char* msg, TRAPS) const { 378 if (!b) { classfile_parse_error(msg, CHECK); } 379 } 380 381 void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN; 382 void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN; 383 384 inline void assert_property(bool b, const char* msg, TRAPS) const { 385 #ifdef ASSERT 386 if (!b) { 387 report_assert_property_failure(msg, THREAD); 388 } 389 #endif 390 } 391 392 inline void assert_property(bool b, const char* msg, int index, TRAPS) const { 393 #ifdef ASSERT 394 if (!b) { 395 report_assert_property_failure(msg, index, THREAD); 396 } 397 #endif 398 } 399 400 inline void check_property(bool property, 401 const char* msg, 402 int index, 403 TRAPS) const { 404 if (_need_verify) { 405 guarantee_property(property, msg, index, CHECK); 406 } else { 407 assert_property(property, msg, index, CHECK); 408 } 409 } 410 411 inline void check_property(bool property, const char* msg, TRAPS) const { 412 if (_need_verify) { 413 guarantee_property(property, msg, CHECK); 414 } else { 415 assert_property(property, msg, CHECK); 416 } 417 } 418 419 inline void guarantee_property(bool b, 420 const char* msg, 421 int index, 422 TRAPS) const { 423 if (!b) { classfile_parse_error(msg, index, CHECK); } 424 } 425 426 inline void guarantee_property(bool b, 427 const char* msg, 428 const char *name, 429 TRAPS) const { 430 if (!b) { classfile_parse_error(msg, name, CHECK); } 431 } 432 433 inline void guarantee_property(bool b, 434 const char* msg, 435 int index, 436 const char *name, 437 TRAPS) const { 438 if (!b) { classfile_parse_error(msg, index, name, CHECK); } 439 } 440 441 void throwIllegalSignature(const char* type, 442 const Symbol* name, 443 const Symbol* sig, 444 TRAPS) const; 445 446 void verify_constantvalue(const ConstantPool* const cp, 447 int constantvalue_index, 448 int signature_index, 449 TRAPS) const; 450 451 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const; 452 void verify_legal_class_name(const Symbol* name, TRAPS) const; 453 void verify_legal_field_name(const Symbol* name, TRAPS) const; 454 void verify_legal_method_name(const Symbol* name, TRAPS) const; 455 456 void verify_legal_field_signature(const Symbol* fieldname, 457 const Symbol* signature, 458 TRAPS) const; 459 int verify_legal_method_signature(const Symbol* methodname, 460 const Symbol* signature, 461 TRAPS) const; 462 463 void verify_legal_class_modifiers(jint flags, TRAPS) const; 464 void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const; 465 void verify_legal_method_modifiers(jint flags, 466 bool is_interface, 467 const Symbol* name, 468 TRAPS) const; 469 470 const char* skip_over_field_signature(const char* signature, 471 bool void_ok, 472 unsigned int length, 473 TRAPS) const; 474 475 bool has_cp_patch_at(int index) const { 476 assert(index >= 0, "oob"); 477 return (_cp_patches != NULL 478 && index < _cp_patches->length() 479 && _cp_patches->adr_at(index)->not_null()); 480 } 481 482 Handle cp_patch_at(int index) const { 483 assert(has_cp_patch_at(index), "oob"); 484 return _cp_patches->at(index); 485 } 486 487 Handle clear_cp_patch_at(int index); 488 489 void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name); 490 void patch_constant_pool(ConstantPool* cp, 491 int index, 492 Handle patch, 493 TRAPS); 494 495 // Wrapper for constantTag.is_klass_[or_]reference. 496 // In older versions of the VM, Klass*s cannot sneak into early phases of 497 // constant pool construction, but in later versions they can. 498 // %%% Let's phase out the old is_klass_reference. 499 bool valid_klass_reference_at(int index) const { 500 return _cp->is_within_bounds(index) && 501 _cp->tag_at(index).is_klass_or_reference(); 502 } 503 504 // Checks that the cpool index is in range and is a utf8 505 bool valid_symbol_at(int cpool_index) const { 506 return _cp->is_within_bounds(cpool_index) && 507 _cp->tag_at(cpool_index).is_utf8(); 508 } 509 510 void copy_localvariable_table(const ConstMethod* cm, 511 int lvt_cnt, 512 u2* const localvariable_table_length, 513 const unsafe_u2** const localvariable_table_start, 514 int lvtt_cnt, 515 u2* const localvariable_type_table_length, 516 const unsafe_u2** const localvariable_type_table_start, 517 TRAPS); 518 519 void copy_method_annotations(ConstMethod* cm, 520 const u1* runtime_visible_annotations, 521 int runtime_visible_annotations_length, 522 const u1* runtime_invisible_annotations, 523 int runtime_invisible_annotations_length, 524 const u1* runtime_visible_parameter_annotations, 525 int runtime_visible_parameter_annotations_length, 526 const u1* runtime_invisible_parameter_annotations, 527 int runtime_invisible_parameter_annotations_length, 528 const u1* runtime_visible_type_annotations, 529 int runtime_visible_type_annotations_length, 530 const u1* runtime_invisible_type_annotations, 531 int runtime_invisible_type_annotations_length, 532 const u1* annotation_default, 533 int annotation_default_length, 534 TRAPS); 535 536 void update_class_name(Symbol* new_name); 537 538 public: 539 ClassFileParser(ClassFileStream* stream, 540 Symbol* name, 541 ClassLoaderData* loader_data, 542 const ClassLoadInfo* cl_info, 543 Publicity pub_level, 544 TRAPS); 545 546 ~ClassFileParser(); 547 548 InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, const ClassInstanceInfo& cl_inst_info, TRAPS); 549 550 const ClassFileStream* clone_stream() const; 551 552 void set_klass_to_deallocate(InstanceKlass* klass); 553 554 int static_field_size() const; 555 int total_oop_map_count() const; 556 jint layout_size() const; 557 558 int vtable_size() const { return _vtable_size; } 559 int itable_size() const { return _itable_size; } 560 561 u2 this_class_index() const { return _this_class_index; } 562 563 bool is_unsafe_anonymous() const { return _unsafe_anonymous_host != NULL; } 564 bool is_hidden() const { return _is_hidden; } 565 bool is_interface() const { return _access_flags.is_interface(); } 566 567 const InstanceKlass* unsafe_anonymous_host() const { return _unsafe_anonymous_host; } 568 const GrowableArray<Handle>* cp_patches() const { return _cp_patches; } 569 ClassLoaderData* loader_data() const { return _loader_data; } 570 const Symbol* class_name() const { return _class_name; } 571 const InstanceKlass* super_klass() const { return _super_klass; } 572 573 ReferenceType reference_type() const { return _rt; } 574 AccessFlags access_flags() const { return _access_flags; } 575 576 bool is_internal() const { return INTERNAL == _pub_level; } 577 578 static bool verify_unqualified_name(const char* name, unsigned int length, int type); 579 580 #ifdef ASSERT 581 static bool is_internal_format(Symbol* class_name); 582 #endif 583 584 }; 585 586 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP