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