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