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