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<RecordComponent*>* _record_components; 136 Array<InstanceKlass*>* _local_interfaces; 137 Array<InstanceKlass*>* _transitive_interfaces; 138 Annotations* _combined_annotations; 139 AnnotationArray* _class_annotations; 140 AnnotationArray* _class_type_annotations; 141 Array<AnnotationArray*>* _fields_annotations; 142 Array<AnnotationArray*>* _fields_type_annotations; 143 InstanceKlass* _klass; // InstanceKlass* once created. 144 InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed 145 146 ClassAnnotationCollector* _parsed_annotations; 147 FieldAllocationCount* _fac; 148 FieldLayoutInfo* _field_info; 149 const intArray* _method_ordering; 150 GrowableArray<Method*>* _all_mirandas; 151 152 enum { fixed_buffer_size = 128 }; 153 u_char _linenumbertable_buffer[fixed_buffer_size]; 154 155 // Size of Java vtable (in words) 156 int _vtable_size; 157 int _itable_size; 158 159 int _num_miranda_methods; 160 161 ReferenceType _rt; 162 Handle _protection_domain; 163 AccessFlags _access_flags; 164 165 // for tracing and notifications 166 Publicity _pub_level; 167 168 // Used to keep track of whether a constant pool item 19 or 20 is found. These 169 // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed 170 // in regular class files. For class file version >= 53, a CFE cannot be thrown 171 // immediately when these are seen because a NCDFE must be thrown if the class's 172 // access_flags have ACC_MODULE set. But, the access_flags haven't been looked 173 // at yet. So, the bad constant pool item is cached here. A value of zero 174 // means that no constant pool item 19 or 20 was found. 175 short _bad_constant_seen; 176 177 // class attributes parsed before the instance klass is created: 178 bool _synthetic_flag; 179 int _sde_length; 180 const char* _sde_buffer; 181 u2 _sourcefile_index; 182 u2 _generic_signature_index; 183 184 u2 _major_version; 185 u2 _minor_version; 186 u2 _this_class_index; 187 u2 _super_class_index; 188 u2 _itfs_len; 189 u2 _java_fields_count; 190 191 bool _need_verify; 192 bool _relax_verify; 193 194 bool _has_nonstatic_concrete_methods; 195 bool _declares_nonstatic_concrete_methods; 196 bool _has_final_method; 197 bool _has_contended_fields; 198 199 // precomputed flags 200 bool _has_finalizer; 201 bool _has_empty_finalizer; 202 bool _has_vanilla_constructor; 203 int _max_bootstrap_specifier_index; // detects BSS values 204 205 void parse_stream(const ClassFileStream* const stream, TRAPS); 206 207 void mangle_hidden_class_name(InstanceKlass* const ik); 208 209 void post_process_parsed_stream(const ClassFileStream* const stream, 210 ConstantPool* cp, 211 TRAPS); 212 213 void prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS); 214 void fix_unsafe_anonymous_class_name(TRAPS); 215 216 void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, 217 const ClassInstanceInfo& cl_inst_info, TRAPS); 218 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 FieldAllocationCount* const fac, 270 ConstantPool* cp, 271 const int cp_size, 272 u2* const java_fields_count_ptr, 273 TRAPS); 274 275 // Method parsing 276 Method* parse_method(const ClassFileStream* const cfs, 277 bool is_interface, 278 const ConstantPool* cp, 279 AccessFlags* const promoted_flags, 280 TRAPS); 281 282 void parse_methods(const ClassFileStream* const cfs, 283 bool is_interface, 284 AccessFlags* const promoted_flags, 285 bool* const has_final_method, 286 bool* const declares_nonstatic_concrete_methods, 287 TRAPS); 288 289 const unsafe_u2* parse_exception_table(const ClassFileStream* const stream, 290 u4 code_length, 291 u4 exception_table_length, 292 TRAPS); 293 294 void parse_linenumber_table(u4 code_attribute_length, 295 u4 code_length, 296 CompressedLineNumberWriteStream**const write_stream, 297 TRAPS); 298 299 const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs, 300 u4 code_length, 301 u2 max_locals, 302 u4 code_attribute_length, 303 u2* const localvariable_table_length, 304 bool isLVTT, 305 TRAPS); 306 307 const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs, 308 u2* const checked_exceptions_length, 309 u4 method_attribute_length, 310 TRAPS); 311 312 // Classfile attribute parsing 313 u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS); 314 void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS); 315 void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 316 int length, 317 TRAPS); 318 319 u2 parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 320 const u1* const inner_classes_attribute_start, 321 bool parsed_enclosingmethod_attribute, 322 u2 enclosing_method_class_index, 323 u2 enclosing_method_method_index, 324 TRAPS); 325 326 u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 327 const u1* const nest_members_attribute_start, 328 TRAPS); 329 330 u2 parse_classfile_record_attribute(const ClassFileStream* const cfs, 331 const ConstantPool* cp, 332 const u1* const record_attribute_start, 333 TRAPS); 334 335 bool supports_records(); 336 337 void parse_classfile_attributes(const ClassFileStream* const cfs, 338 ConstantPool* cp, 339 ClassAnnotationCollector* parsed_annotations, 340 TRAPS); 341 342 void parse_classfile_synthetic_attribute(TRAPS); 343 void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS); 344 void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 345 ConstantPool* cp, 346 u4 attribute_length, 347 TRAPS); 348 349 // Annotations handling 350 AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations, 351 int runtime_visible_annotations_length, 352 const u1* const runtime_invisible_annotations, 353 int runtime_invisible_annotations_length, 354 TRAPS); 355 356 void set_precomputed_flags(InstanceKlass* k); 357 358 // Format checker methods 359 void classfile_parse_error(const char* msg, TRAPS) const; 360 void classfile_parse_error(const char* msg, int index, TRAPS) const; 361 void classfile_parse_error(const char* msg, const char *name, TRAPS) const; 362 void classfile_parse_error(const char* msg, 363 int index, 364 const char *name, 365 TRAPS) const; 366 void classfile_parse_error(const char* msg, 367 const char* name, 368 const char* signature, 369 TRAPS) const; 370 371 inline void guarantee_property(bool b, const char* msg, TRAPS) const { 372 if (!b) { classfile_parse_error(msg, CHECK); } 373 } 374 375 void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN; 376 void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN; 377 378 inline void assert_property(bool b, const char* msg, TRAPS) const { 379 #ifdef ASSERT 380 if (!b) { 381 report_assert_property_failure(msg, THREAD); 382 } 383 #endif 384 } 385 386 inline void assert_property(bool b, const char* msg, int index, TRAPS) const { 387 #ifdef ASSERT 388 if (!b) { 389 report_assert_property_failure(msg, index, THREAD); 390 } 391 #endif 392 } 393 394 inline void check_property(bool property, 395 const char* msg, 396 int index, 397 TRAPS) const { 398 if (_need_verify) { 399 guarantee_property(property, msg, index, CHECK); 400 } else { 401 assert_property(property, msg, index, CHECK); 402 } 403 } 404 405 inline void check_property(bool property, const char* msg, TRAPS) const { 406 if (_need_verify) { 407 guarantee_property(property, msg, CHECK); 408 } else { 409 assert_property(property, msg, CHECK); 410 } 411 } 412 413 inline void guarantee_property(bool b, 414 const char* msg, 415 int index, 416 TRAPS) const { 417 if (!b) { classfile_parse_error(msg, index, CHECK); } 418 } 419 420 inline void guarantee_property(bool b, 421 const char* msg, 422 const char *name, 423 TRAPS) const { 424 if (!b) { classfile_parse_error(msg, name, CHECK); } 425 } 426 427 inline void guarantee_property(bool b, 428 const char* msg, 429 int index, 430 const char *name, 431 TRAPS) const { 432 if (!b) { classfile_parse_error(msg, index, name, CHECK); } 433 } 434 435 void throwIllegalSignature(const char* type, 436 const Symbol* name, 437 const Symbol* sig, 438 TRAPS) const; 439 440 void verify_constantvalue(const ConstantPool* const cp, 441 int constantvalue_index, 442 int signature_index, 443 TRAPS) const; 444 445 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const; 446 void verify_legal_class_name(const Symbol* name, TRAPS) const; 447 void verify_legal_field_name(const Symbol* name, TRAPS) const; 448 void verify_legal_method_name(const Symbol* name, TRAPS) const; 449 450 void verify_legal_field_signature(const Symbol* fieldname, 451 const Symbol* signature, 452 TRAPS) const; 453 int verify_legal_method_signature(const Symbol* methodname, 454 const Symbol* signature, 455 TRAPS) const; 456 457 void verify_legal_class_modifiers(jint flags, TRAPS) const; 458 void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const; 459 void verify_legal_method_modifiers(jint flags, 460 bool is_interface, 461 const Symbol* name, 462 TRAPS) const; 463 464 const char* skip_over_field_signature(const char* signature, 465 bool void_ok, 466 unsigned int length, 467 TRAPS) const; 468 469 bool has_cp_patch_at(int index) const { 470 assert(index >= 0, "oob"); 471 return (_cp_patches != NULL 472 && index < _cp_patches->length() 473 && _cp_patches->adr_at(index)->not_null()); 474 } 475 476 Handle cp_patch_at(int index) const { 477 assert(has_cp_patch_at(index), "oob"); 478 return _cp_patches->at(index); 479 } 480 481 Handle clear_cp_patch_at(int index); 482 483 void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name); 484 void patch_constant_pool(ConstantPool* cp, 485 int index, 486 Handle patch, 487 TRAPS); 488 489 // Wrapper for constantTag.is_klass_[or_]reference. 490 // In older versions of the VM, Klass*s cannot sneak into early phases of 491 // constant pool construction, but in later versions they can. 492 // %%% Let's phase out the old is_klass_reference. 493 bool valid_klass_reference_at(int index) const { 494 return _cp->is_within_bounds(index) && 495 _cp->tag_at(index).is_klass_or_reference(); 496 } 497 498 // Checks that the cpool index is in range and is a utf8 499 bool valid_symbol_at(int cpool_index) const { 500 return _cp->is_within_bounds(cpool_index) && 501 _cp->tag_at(cpool_index).is_utf8(); 502 } 503 504 void copy_localvariable_table(const ConstMethod* cm, 505 int lvt_cnt, 506 u2* const localvariable_table_length, 507 const unsafe_u2** const localvariable_table_start, 508 int lvtt_cnt, 509 u2* const localvariable_type_table_length, 510 const unsafe_u2** const localvariable_type_table_start, 511 TRAPS); 512 513 void copy_method_annotations(ConstMethod* cm, 514 const u1* runtime_visible_annotations, 515 int runtime_visible_annotations_length, 516 const u1* runtime_invisible_annotations, 517 int runtime_invisible_annotations_length, 518 const u1* runtime_visible_parameter_annotations, 519 int runtime_visible_parameter_annotations_length, 520 const u1* runtime_invisible_parameter_annotations, 521 int runtime_invisible_parameter_annotations_length, 522 const u1* runtime_visible_type_annotations, 523 int runtime_visible_type_annotations_length, 524 const u1* runtime_invisible_type_annotations, 525 int runtime_invisible_type_annotations_length, 526 const u1* annotation_default, 527 int annotation_default_length, 528 TRAPS); 529 530 // lays out fields in class and returns the total oopmap count 531 void layout_fields(ConstantPool* cp, 532 const FieldAllocationCount* fac, 533 const ClassAnnotationCollector* parsed_annotations, 534 FieldLayoutInfo* info, 535 TRAPS); 536 537 void update_class_name(Symbol* new_name); 538 539 public: 540 ClassFileParser(ClassFileStream* stream, 541 Symbol* name, 542 ClassLoaderData* loader_data, 543 const ClassLoadInfo* cl_info, 544 Publicity pub_level, 545 TRAPS); 546 547 ~ClassFileParser(); 548 549 InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, const ClassInstanceInfo& cl_inst_info, TRAPS); 550 551 const ClassFileStream* clone_stream() const; 552 553 void set_klass_to_deallocate(InstanceKlass* klass); 554 555 int static_field_size() const; 556 int total_oop_map_count() const; 557 jint layout_size() const; 558 559 int vtable_size() const { return _vtable_size; } 560 int itable_size() const { return _itable_size; } 561 562 u2 this_class_index() const { return _this_class_index; } 563 564 bool is_unsafe_anonymous() const { return _unsafe_anonymous_host != NULL; } 565 bool is_hidden() const { return _is_hidden; } 566 bool is_interface() const { return _access_flags.is_interface(); } 567 568 const InstanceKlass* unsafe_anonymous_host() const { return _unsafe_anonymous_host; } 569 const GrowableArray<Handle>* cp_patches() const { return _cp_patches; } 570 ClassLoaderData* loader_data() const { return _loader_data; } 571 const Symbol* class_name() const { return _class_name; } 572 const InstanceKlass* super_klass() const { return _super_klass; } 573 574 ReferenceType reference_type() const { return _rt; } 575 AccessFlags access_flags() const { return _access_flags; } 576 577 bool is_internal() const { return INTERNAL == _pub_level; } 578 579 static bool verify_unqualified_name(const char* name, unsigned int length, int type); 580 581 #ifdef ASSERT 582 static bool is_internal_format(Symbol* class_name); 583 #endif 584 585 }; 586 587 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP