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