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