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