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* _host_klass; 86 GrowableArray<Handle>* _cp_patches; // overrides for CP entries 87 int _num_patched_klasses; 88 int _max_num_patched_klasses; 89 int _orig_cp_size; 90 int _first_patched_klass_resolved_index; 91 92 // Metadata created before the instance klass is created. Must be deallocated 93 // if not transferred to the InstanceKlass upon successful class loading 94 // in which case these pointers have been set to NULL. 95 const InstanceKlass* _super_klass; 96 ConstantPool* _cp; 97 Array<u2>* _fields; 98 Array<Method*>* _methods; 99 Array<u2>* _inner_classes; 100 Array<u2>* _nest_members; 101 u2 _nest_host; 102 Array<Klass*>* _local_interfaces; 103 Array<Klass*>* _transitive_interfaces; 104 Annotations* _combined_annotations; 105 AnnotationArray* _annotations; 106 AnnotationArray* _type_annotations; 107 Array<AnnotationArray*>* _fields_annotations; 108 Array<AnnotationArray*>* _fields_type_annotations; 109 InstanceKlass* _klass; // InstanceKlass* once created. 110 InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed 111 112 ClassAnnotationCollector* _parsed_annotations; 113 FieldAllocationCount* _fac; 114 FieldLayoutInfo* _field_info; 115 const intArray* _method_ordering; 116 GrowableArray<Method*>* _all_mirandas; 117 118 enum { fixed_buffer_size = 128 }; 119 u_char _linenumbertable_buffer[fixed_buffer_size]; 120 121 // Size of Java vtable (in words) 122 int _vtable_size; 123 int _itable_size; 124 125 int _num_miranda_methods; 126 127 ReferenceType _rt; 128 Handle _protection_domain; 129 AccessFlags _access_flags; 130 131 // for tracing and notifications 132 Publicity _pub_level; 133 134 // Used to keep track of whether a constant pool item 19 or 20 is found. These 135 // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed 136 // in regular class files. For class file version >= 53, a CFE cannot be thrown 137 // immediately when these are seen because a NCDFE must be thrown if the class's 138 // access_flags have ACC_MODULE set. But, the access_flags haven't been looked 139 // at yet. So, the bad constant pool item is cached here. A value of zero 140 // means that no constant pool item 19 or 20 was found. 141 short _bad_constant_seen; 142 143 // class attributes parsed before the instance klass is created: 144 bool _synthetic_flag; 145 int _sde_length; 146 const char* _sde_buffer; 147 u2 _sourcefile_index; 148 u2 _generic_signature_index; 149 150 u2 _major_version; 151 u2 _minor_version; 152 u2 _this_class_index; 153 u2 _super_class_index; 154 u2 _itfs_len; 155 u2 _java_fields_count; 156 157 bool _need_verify; 158 bool _relax_verify; 159 160 bool _has_nonstatic_concrete_methods; 161 bool _declares_nonstatic_concrete_methods; 162 bool _has_final_method; 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 FieldAllocationCount* const fac, 231 ConstantPool* cp, 232 const int cp_size, 233 u2* const java_fields_count_ptr, 234 TRAPS); 235 236 // Method parsing 237 Method* parse_method(const ClassFileStream* const cfs, 238 bool is_interface, 239 const ConstantPool* cp, 240 AccessFlags* const promoted_flags, 241 TRAPS); 242 243 void parse_methods(const ClassFileStream* const cfs, 244 bool is_interface, 245 AccessFlags* const promoted_flags, 246 bool* const has_final_method, 247 bool* const declares_nonstatic_concrete_methods, 248 TRAPS); 249 250 const unsafe_u2* parse_exception_table(const ClassFileStream* const stream, 251 u4 code_length, 252 u4 exception_table_length, 253 TRAPS); 254 255 void parse_linenumber_table(u4 code_attribute_length, 256 u4 code_length, 257 CompressedLineNumberWriteStream**const write_stream, 258 TRAPS); 259 260 const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs, 261 u4 code_length, 262 u2 max_locals, 263 u4 code_attribute_length, 264 u2* const localvariable_table_length, 265 bool isLVTT, 266 TRAPS); 267 268 const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs, 269 u2* const checked_exceptions_length, 270 u4 method_attribute_length, 271 TRAPS); 272 273 void parse_type_array(u2 array_length, 274 u4 code_length, 275 u4* const u1_index, 276 u4* const u2_index, 277 u1* const u1_array, 278 u2* const u2_array, 279 TRAPS); 280 281 // Classfile attribute parsing 282 u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS); 283 void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS); 284 void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 285 int length, 286 TRAPS); 287 288 u2 parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 289 const u1* const inner_classes_attribute_start, 290 bool parsed_enclosingmethod_attribute, 291 u2 enclosing_method_class_index, 292 u2 enclosing_method_method_index, 293 TRAPS); 294 295 u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 296 const u1* const nest_members_attribute_start, 297 TRAPS); 298 299 void parse_classfile_attributes(const ClassFileStream* const cfs, 300 ConstantPool* cp, 301 ClassAnnotationCollector* parsed_annotations, 302 TRAPS); 303 304 void parse_classfile_synthetic_attribute(TRAPS); 305 void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS); 306 void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 307 ConstantPool* cp, 308 u4 attribute_length, 309 TRAPS); 310 311 // Annotations handling 312 AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations, 313 int runtime_visible_annotations_length, 314 const u1* const runtime_invisible_annotations, 315 int runtime_invisible_annotations_length, 316 TRAPS); 317 318 void set_precomputed_flags(InstanceKlass* k); 319 320 // Format checker methods 321 void classfile_parse_error(const char* msg, TRAPS) const; 322 void classfile_parse_error(const char* msg, int index, TRAPS) const; 323 void classfile_parse_error(const char* msg, const char *name, TRAPS) const; 324 void classfile_parse_error(const char* msg, 325 int index, 326 const char *name, 327 TRAPS) const; 328 void classfile_parse_error(const char* msg, 329 const char* name, 330 const char* signature, 331 TRAPS) const; 332 333 inline void guarantee_property(bool b, const char* msg, TRAPS) const { 334 if (!b) { classfile_parse_error(msg, CHECK); } 335 } 336 337 void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN; 338 void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN; 339 340 inline void assert_property(bool b, const char* msg, TRAPS) const { 341 #ifdef ASSERT 342 if (!b) { 343 report_assert_property_failure(msg, THREAD); 344 } 345 #endif 346 } 347 348 inline void assert_property(bool b, const char* msg, int index, TRAPS) const { 349 #ifdef ASSERT 350 if (!b) { 351 report_assert_property_failure(msg, index, THREAD); 352 } 353 #endif 354 } 355 356 inline void check_property(bool property, 357 const char* msg, 358 int index, 359 TRAPS) const { 360 if (_need_verify) { 361 guarantee_property(property, msg, index, CHECK); 362 } else { 363 assert_property(property, msg, index, CHECK); 364 } 365 } 366 367 inline void check_property(bool property, const char* msg, TRAPS) const { 368 if (_need_verify) { 369 guarantee_property(property, msg, CHECK); 370 } else { 371 assert_property(property, msg, CHECK); 372 } 373 } 374 375 inline void guarantee_property(bool b, 376 const char* msg, 377 int index, 378 TRAPS) const { 379 if (!b) { classfile_parse_error(msg, index, CHECK); } 380 } 381 382 inline void guarantee_property(bool b, 383 const char* msg, 384 const char *name, 385 TRAPS) const { 386 if (!b) { classfile_parse_error(msg, name, CHECK); } 387 } 388 389 inline void guarantee_property(bool b, 390 const char* msg, 391 int index, 392 const char *name, 393 TRAPS) const { 394 if (!b) { classfile_parse_error(msg, index, name, CHECK); } 395 } 396 397 void throwIllegalSignature(const char* type, 398 const Symbol* name, 399 const Symbol* sig, 400 TRAPS) const; 401 402 void verify_constantvalue(const ConstantPool* const cp, 403 int constantvalue_index, 404 int signature_index, 405 TRAPS) const; 406 407 void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const; 408 void verify_legal_class_name(const Symbol* name, TRAPS) const; 409 void verify_legal_field_name(const Symbol* name, TRAPS) const; 410 void verify_legal_method_name(const Symbol* name, TRAPS) const; 411 412 void verify_legal_field_signature(const Symbol* fieldname, 413 const Symbol* signature, 414 TRAPS) const; 415 int verify_legal_method_signature(const Symbol* methodname, 416 const Symbol* signature, 417 TRAPS) const; 418 419 void verify_legal_class_modifiers(jint flags, TRAPS) const; 420 void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const; 421 void verify_legal_method_modifiers(jint flags, 422 bool is_interface, 423 const Symbol* name, 424 TRAPS) const; 425 426 const char* skip_over_field_signature(const char* signature, 427 bool void_ok, 428 unsigned int length, 429 TRAPS) const; 430 431 bool has_cp_patch_at(int index) const { 432 assert(index >= 0, "oob"); 433 return (_cp_patches != NULL 434 && index < _cp_patches->length() 435 && _cp_patches->adr_at(index)->not_null()); 436 } 437 438 Handle cp_patch_at(int index) const { 439 assert(has_cp_patch_at(index), "oob"); 440 return _cp_patches->at(index); 441 } 442 443 Handle clear_cp_patch_at(int index); 444 445 void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name); 446 void patch_constant_pool(ConstantPool* cp, 447 int index, 448 Handle patch, 449 TRAPS); 450 451 // Wrapper for constantTag.is_klass_[or_]reference. 452 // In older versions of the VM, Klass*s cannot sneak into early phases of 453 // constant pool construction, but in later versions they can. 454 // %%% Let's phase out the old is_klass_reference. 455 bool valid_klass_reference_at(int index) const { 456 return _cp->is_within_bounds(index) && 457 _cp->tag_at(index).is_klass_or_reference(); 458 } 459 460 // Checks that the cpool index is in range and is a utf8 461 bool valid_symbol_at(int cpool_index) const { 462 return _cp->is_within_bounds(cpool_index) && 463 _cp->tag_at(cpool_index).is_utf8(); 464 } 465 466 void copy_localvariable_table(const ConstMethod* cm, 467 int lvt_cnt, 468 u2* const localvariable_table_length, 469 const unsafe_u2** const localvariable_table_start, 470 int lvtt_cnt, 471 u2* const localvariable_type_table_length, 472 const unsafe_u2** const localvariable_type_table_start, 473 TRAPS); 474 475 void copy_method_annotations(ConstMethod* cm, 476 const u1* runtime_visible_annotations, 477 int runtime_visible_annotations_length, 478 const u1* runtime_invisible_annotations, 479 int runtime_invisible_annotations_length, 480 const u1* runtime_visible_parameter_annotations, 481 int runtime_visible_parameter_annotations_length, 482 const u1* runtime_invisible_parameter_annotations, 483 int runtime_invisible_parameter_annotations_length, 484 const u1* runtime_visible_type_annotations, 485 int runtime_visible_type_annotations_length, 486 const u1* runtime_invisible_type_annotations, 487 int runtime_invisible_type_annotations_length, 488 const u1* annotation_default, 489 int annotation_default_length, 490 TRAPS); 491 492 // lays out fields in class and returns the total oopmap count 493 void layout_fields(ConstantPool* cp, 494 const FieldAllocationCount* fac, 495 const ClassAnnotationCollector* parsed_annotations, 496 FieldLayoutInfo* info, 497 TRAPS); 498 499 public: 500 ClassFileParser(ClassFileStream* stream, 501 Symbol* name, 502 ClassLoaderData* loader_data, 503 Handle protection_domain, 504 const InstanceKlass* host_klass, 505 GrowableArray<Handle>* cp_patches, 506 Publicity pub_level, 507 TRAPS); 508 509 ~ClassFileParser(); 510 511 InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, TRAPS); 512 513 const ClassFileStream* clone_stream() const; 514 515 void set_klass_to_deallocate(InstanceKlass* klass); 516 517 int static_field_size() const; 518 int total_oop_map_count() const; 519 jint layout_size() const; 520 521 int vtable_size() const { return _vtable_size; } 522 int itable_size() const { return _itable_size; } 523 524 u2 this_class_index() const { return _this_class_index; } 525 u2 super_class_index() const { return _super_class_index; } 526 527 bool is_anonymous() const { return _host_klass != NULL; } 528 bool is_interface() const { return _access_flags.is_interface(); } 529 530 const InstanceKlass* host_klass() const { return _host_klass; } 531 const GrowableArray<Handle>* cp_patches() const { return _cp_patches; } 532 ClassLoaderData* loader_data() const { return _loader_data; } 533 const Symbol* class_name() const { return _class_name; } 534 const InstanceKlass* super_klass() const { return _super_klass; } 535 536 ReferenceType reference_type() const { return _rt; } 537 AccessFlags access_flags() const { return _access_flags; } 538 539 bool is_internal() const { return INTERNAL == _pub_level; } 540 541 static bool verify_unqualified_name(const char* name, unsigned int length, int type); 542 543 #ifdef ASSERT 544 static bool is_internal_format(Symbol* class_name); 545 #endif 546 547 }; 548 549 #endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP