3129 if (inner_classes_attribute_start != NULL) {
3130 cfs->set_current(inner_classes_attribute_start);
3131 cfs->guarantee_more(2, CHECK_0); // length
3132 length = cfs->get_u2_fast();
3133 }
3134
3135 // 4-tuples of shorts of inner classes data and 2 shorts of enclosing
3136 // method data:
3137 // [inner_class_info_index,
3138 // outer_class_info_index,
3139 // inner_name_index,
3140 // inner_class_access_flags,
3141 // ...
3142 // enclosing_method_class_index,
3143 // enclosing_method_method_index]
3144 const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0);
3145 Array<u2>* const inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3146 _inner_classes = inner_classes;
3147
3148 int index = 0;
3149 const int cp_size = _cp->length();
3150 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2
3151 for (int n = 0; n < length; n++) {
3152 // Inner class index
3153 const u2 inner_class_info_index = cfs->get_u2_fast();
3154 check_property(
3155 valid_klass_reference_at(inner_class_info_index),
3156 "inner_class_info_index %u has bad constant type in class file %s",
3157 inner_class_info_index, CHECK_0);
3158 // Outer class index
3159 const u2 outer_class_info_index = cfs->get_u2_fast();
3160 check_property(
3161 outer_class_info_index == 0 ||
3162 valid_klass_reference_at(outer_class_info_index),
3163 "outer_class_info_index %u has bad constant type in class file %s",
3164 outer_class_info_index, CHECK_0);
3165 // Inner class name
3166 const u2 inner_name_index = cfs->get_u2_fast();
3167 check_property(
3168 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3169 "inner_name_index %u has bad constant type in class file %s",
3203 inner_classes->at(i+3) != inner_classes->at(j+3)),
3204 "Duplicate entry in InnerClasses in class file %s",
3205 CHECK_0);
3206 }
3207 }
3208 }
3209
3210 // Set EnclosingMethod class and method indexes.
3211 if (parsed_enclosingmethod_attribute) {
3212 inner_classes->at_put(index++, enclosing_method_class_index);
3213 inner_classes->at_put(index++, enclosing_method_method_index);
3214 }
3215 assert(index == size, "wrong size");
3216
3217 // Restore buffer's current position.
3218 cfs->set_current(current_mark);
3219
3220 return length;
3221 }
3222
3223 void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) {
3224 set_class_synthetic_flag(true);
3225 }
3226
3227 void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) {
3228 assert(cfs != NULL, "invariant");
3229
3230 const u2 signature_index = cfs->get_u2(CHECK);
3231 check_property(
3232 valid_symbol_at(signature_index),
3233 "Invalid constant pool index %u in Signature attribute in class file %s",
3234 signature_index, CHECK);
3235 set_class_generic_signature_index(signature_index);
3236 }
3237
3238 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
3239 ConstantPool* cp,
3240 u4 attribute_byte_length,
3241 TRAPS) {
3242 assert(cfs != NULL, "invariant");
3310 argument_index,
3311 CHECK);
3312 operands->at_put(operand_fill_index++, argument_index);
3313 }
3314 }
3315 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3316 "Bad length on BootstrapMethods in class file %s",
3317 CHECK);
3318 }
3319
3320 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3321 ConstantPool* cp,
3322 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3323 TRAPS) {
3324 assert(cfs != NULL, "invariant");
3325 assert(cp != NULL, "invariant");
3326 assert(parsed_annotations != NULL, "invariant");
3327
3328 // Set inner classes attribute to default sentinel
3329 _inner_classes = Universe::the_empty_short_array();
3330 cfs->guarantee_more(2, CHECK); // attributes_count
3331 u2 attributes_count = cfs->get_u2_fast();
3332 bool parsed_sourcefile_attribute = false;
3333 bool parsed_innerclasses_attribute = false;
3334 bool parsed_enclosingmethod_attribute = false;
3335 bool parsed_bootstrap_methods_attribute = false;
3336 const u1* runtime_visible_annotations = NULL;
3337 int runtime_visible_annotations_length = 0;
3338 const u1* runtime_invisible_annotations = NULL;
3339 int runtime_invisible_annotations_length = 0;
3340 const u1* runtime_visible_type_annotations = NULL;
3341 int runtime_visible_type_annotations_length = 0;
3342 const u1* runtime_invisible_type_annotations = NULL;
3343 int runtime_invisible_type_annotations_length = 0;
3344 bool runtime_invisible_type_annotations_exists = false;
3345 bool runtime_invisible_annotations_exists = false;
3346 bool parsed_source_debug_ext_annotations_exist = false;
3347 const u1* inner_classes_attribute_start = NULL;
3348 u4 inner_classes_attribute_length = 0;
3349 u2 enclosing_method_class_index = 0;
3350 u2 enclosing_method_method_index = 0;
3351 // Iterate over attributes
3352 while (attributes_count--) {
3353 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3354 const u2 attribute_name_index = cfs->get_u2_fast();
3355 const u4 attribute_length = cfs->get_u4_fast();
3356 check_property(
3357 valid_symbol_at(attribute_name_index),
3358 "Attribute name has bad constant pool index %u in class file %s",
3359 attribute_name_index, CHECK);
3360 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3361 if (tag == vmSymbols::tag_source_file()) {
3362 // Check for SourceFile tag
3363 if (_need_verify) {
3364 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3365 }
3366 if (parsed_sourcefile_attribute) {
3367 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
3368 } else {
3369 parsed_sourcefile_attribute = true;
3370 }
3479 "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", CHECK);
3480 }
3481 runtime_visible_type_annotations_length = attribute_length;
3482 runtime_visible_type_annotations = cfs->current();
3483 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
3484 // No need for the VM to parse Type annotations
3485 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);
3486 } else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) {
3487 if (runtime_invisible_type_annotations_exists) {
3488 classfile_parse_error(
3489 "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", CHECK);
3490 } else {
3491 runtime_invisible_type_annotations_exists = true;
3492 }
3493 if (PreserveAllAnnotations) {
3494 runtime_invisible_type_annotations_length = attribute_length;
3495 runtime_invisible_type_annotations = cfs->current();
3496 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
3497 }
3498 cfs->skip_u1(attribute_length, CHECK);
3499 } else {
3500 // Unknown attribute
3501 cfs->skip_u1(attribute_length, CHECK);
3502 }
3503 } else {
3504 // Unknown attribute
3505 cfs->skip_u1(attribute_length, CHECK);
3506 }
3507 }
3508 _annotations = assemble_annotations(runtime_visible_annotations,
3509 runtime_visible_annotations_length,
3510 runtime_invisible_annotations,
3511 runtime_invisible_annotations_length,
3512 CHECK);
3513 _type_annotations = assemble_annotations(runtime_visible_type_annotations,
3514 runtime_visible_type_annotations_length,
3515 runtime_invisible_type_annotations,
3516 runtime_invisible_type_annotations_length,
3517 CHECK);
3518
3519 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) {
3520 const u2 num_of_classes = parse_classfile_inner_classes_attribute(
3521 cfs,
3522 inner_classes_attribute_start,
3523 parsed_innerclasses_attribute,
3524 enclosing_method_class_index,
3525 enclosing_method_method_index,
3526 CHECK);
3527 if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) {
3528 guarantee_property(
3529 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
3530 "Wrong InnerClasses attribute length in class file %s", CHECK);
3531 }
3532 }
3533
3534 if (_max_bootstrap_specifier_index >= 0) {
3535 guarantee_property(parsed_bootstrap_methods_attribute,
3536 "Missing BootstrapMethods attribute in class file %s", CHECK);
3537 }
3538 }
3539
3540 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3541 assert(k != NULL, "invariant");
3542
3543 if (_synthetic_flag)
3544 k->set_is_synthetic();
3545 if (_sourcefile_index != 0) {
3546 k->set_source_file_name_index(_sourcefile_index);
3547 }
3548 if (_generic_signature_index != 0) {
3549 k->set_generic_signature_index(_generic_signature_index);
3550 }
3551 if (_sde_buffer != NULL) {
3552 k->set_source_debug_extension(_sde_buffer, _sde_length);
3553 }
3576
3577 // The annotations arrays below has been transfered the
3578 // _combined_annotations so these fields can now be cleared.
3579 _annotations = NULL;
3580 _type_annotations = NULL;
3581 _fields_annotations = NULL;
3582 _fields_type_annotations = NULL;
3583 }
3584
3585 // Transfer ownership of metadata allocated to the InstanceKlass.
3586 void ClassFileParser::apply_parsed_class_metadata(
3587 InstanceKlass* this_klass,
3588 int java_fields_count, TRAPS) {
3589 assert(this_klass != NULL, "invariant");
3590
3591 _cp->set_pool_holder(this_klass);
3592 this_klass->set_constants(_cp);
3593 this_klass->set_fields(_fields, java_fields_count);
3594 this_klass->set_methods(_methods);
3595 this_klass->set_inner_classes(_inner_classes);
3596 this_klass->set_local_interfaces(_local_interfaces);
3597 this_klass->set_annotations(_combined_annotations);
3598 // Delay the setting of _transitive_interfaces until after initialize_supers() in
3599 // fill_instance_klass(). It is because the _transitive_interfaces may be shared with
3600 // its _super. If an OOM occurs while loading the current klass, its _super field
3601 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3602 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3603 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3604
3605 // Clear out these fields so they don't get deallocated by the destructor
3606 clear_class_metadata();
3607 }
3608
3609 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
3610 int runtime_visible_annotations_length,
3611 const u1* const runtime_invisible_annotations,
3612 int runtime_invisible_annotations_length,
3613 TRAPS) {
3614 AnnotationArray* annotations = NULL;
3615 if (runtime_visible_annotations != NULL ||
4586 const Method* const m = methods->at(index);
4587
4588 // skip private, static, and <init> methods
4589 if ((!m->is_private() && !m->is_static()) &&
4590 (m->name() != vmSymbols::object_initializer_name())) {
4591
4592 const Symbol* const name = m->name();
4593 const Symbol* const signature = m->signature();
4594 const Klass* k = this_klass->super();
4595 const Method* super_m = NULL;
4596 while (k != NULL) {
4597 // skip supers that don't have final methods.
4598 if (k->has_final_method()) {
4599 // lookup a matching method in the super class hierarchy
4600 super_m = InstanceKlass::cast(k)->lookup_method(name, signature);
4601 if (super_m == NULL) {
4602 break; // didn't find any match; get out
4603 }
4604
4605 if (super_m->is_final() && !super_m->is_static() &&
4606 // matching method in super is final, and not static
4607 (Reflection::verify_field_access(this_klass,
4608 super_m->method_holder(),
4609 super_m->method_holder(),
4610 super_m->access_flags(), false))
4611 // this class can access super final method and therefore override
4612 ) {
4613 ResourceMark rm(THREAD);
4614 Exceptions::fthrow(
4615 THREAD_AND_LOCATION,
4616 vmSymbols::java_lang_VerifyError(),
4617 "class %s overrides final method %s.%s%s",
4618 this_klass->external_name(),
4619 super_m->method_holder()->external_name(),
4620 name->as_C_string(),
4621 signature->as_C_string()
4622 );
4623 return;
4624 }
4625
4626 // continue to look from super_m's holder's super.
4627 k = super_m->method_holder()->super();
4628 continue;
4629 }
4630
4631 k = k->super();
4632 }
4633 }
4634 }
4635 }
4636
4637
4638 // assumes that this_klass is an interface
4639 static void check_illegal_static_method(const InstanceKlass* this_klass, TRAPS) {
4640 assert(this_klass != NULL, "invariant");
4641 assert(this_klass->is_interface(), "not an interface");
4642 const Array<Method*>* methods = this_klass->methods();
4643 const int num_methods = methods->length();
4644
5451 assert(ik->size_helper() == _field_info->instance_size, "sanity");
5452
5453 // Fill in information already parsed
5454 ik->set_should_verify_class(_need_verify);
5455
5456 // Not yet: supers are done below to support the new subtype-checking fields
5457 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5458 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5459 assert(_fac != NULL, "invariant");
5460 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5461
5462 // this transfers ownership of a lot of arrays from
5463 // the parser onto the InstanceKlass*
5464 apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5465
5466 // note that is not safe to use the fields in the parser from this point on
5467 assert(NULL == _cp, "invariant");
5468 assert(NULL == _fields, "invariant");
5469 assert(NULL == _methods, "invariant");
5470 assert(NULL == _inner_classes, "invariant");
5471 assert(NULL == _local_interfaces, "invariant");
5472 assert(NULL == _combined_annotations, "invariant");
5473
5474 if (_has_final_method) {
5475 ik->set_has_final_method();
5476 }
5477
5478 ik->copy_method_ordering(_method_ordering, CHECK);
5479 // The InstanceKlass::_methods_jmethod_ids cache
5480 // is managed on the assumption that the initial cache
5481 // size is equal to the number of methods in the class. If
5482 // that changes, then InstanceKlass::idnum_can_increment()
5483 // has to be changed accordingly.
5484 ik->set_initial_method_idnum(ik->methods()->length());
5485
5486 ik->set_this_class_index(_this_class_index);
5487
5488 if (is_anonymous()) {
5489 // _this_class_index is a CONSTANT_Class entry that refers to this
5490 // anonymous class itself. If this class needs to refer to its own methods or
5720 ClassLoaderData* loader_data,
5721 Handle protection_domain,
5722 const InstanceKlass* host_klass,
5723 GrowableArray<Handle>* cp_patches,
5724 Publicity pub_level,
5725 TRAPS) :
5726 _stream(stream),
5727 _requested_name(name),
5728 _loader_data(loader_data),
5729 _host_klass(host_klass),
5730 _cp_patches(cp_patches),
5731 _num_patched_klasses(0),
5732 _max_num_patched_klasses(0),
5733 _orig_cp_size(0),
5734 _first_patched_klass_resolved_index(0),
5735 _super_klass(),
5736 _cp(NULL),
5737 _fields(NULL),
5738 _methods(NULL),
5739 _inner_classes(NULL),
5740 _local_interfaces(NULL),
5741 _transitive_interfaces(NULL),
5742 _combined_annotations(NULL),
5743 _annotations(NULL),
5744 _type_annotations(NULL),
5745 _fields_annotations(NULL),
5746 _fields_type_annotations(NULL),
5747 _klass(NULL),
5748 _klass_to_deallocate(NULL),
5749 _parsed_annotations(NULL),
5750 _fac(NULL),
5751 _field_info(NULL),
5752 _method_ordering(NULL),
5753 _all_mirandas(NULL),
5754 _vtable_size(0),
5755 _itable_size(0),
5756 _num_miranda_methods(0),
5757 _rt(REF_NONE),
5758 _protection_domain(protection_domain),
5759 _access_flags(),
5824
5825 // synch back verification state to stream
5826 stream->set_verify(_need_verify);
5827
5828 // Check if verification needs to be relaxed for this class file
5829 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5830 _relax_verify = relax_format_check_for(_loader_data);
5831
5832 parse_stream(stream, CHECK);
5833
5834 post_process_parsed_stream(stream, _cp, CHECK);
5835 }
5836
5837 void ClassFileParser::clear_class_metadata() {
5838 // metadata created before the instance klass is created. Must be
5839 // deallocated if classfile parsing returns an error.
5840 _cp = NULL;
5841 _fields = NULL;
5842 _methods = NULL;
5843 _inner_classes = NULL;
5844 _local_interfaces = NULL;
5845 _combined_annotations = NULL;
5846 _annotations = _type_annotations = NULL;
5847 _fields_annotations = _fields_type_annotations = NULL;
5848 }
5849
5850 // Destructor to clean up
5851 ClassFileParser::~ClassFileParser() {
5852 if (_cp != NULL) {
5853 MetadataFactory::free_metadata(_loader_data, _cp);
5854 }
5855 if (_fields != NULL) {
5856 MetadataFactory::free_array<u2>(_loader_data, _fields);
5857 }
5858
5859 if (_methods != NULL) {
5860 // Free methods
5861 InstanceKlass::deallocate_methods(_loader_data, _methods);
5862 }
5863
5864 // beware of the Universe::empty_blah_array!!
5865 if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {
5866 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5867 }
5868
5869 // Free interfaces
5870 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5871 _local_interfaces, _transitive_interfaces);
5872
5873 if (_combined_annotations != NULL) {
5874 // After all annotations arrays have been created, they are installed into the
5875 // Annotations object that will be assigned to the InstanceKlass being created.
5876
5877 // Deallocate the Annotations object and the installed annotations arrays.
5878 _combined_annotations->deallocate_contents(_loader_data);
5879
5880 // If the _combined_annotations pointer is non-NULL,
5881 // then the other annotations fields should have been cleared.
5882 assert(_annotations == NULL, "Should have been cleared");
5883 assert(_type_annotations == NULL, "Should have been cleared");
5884 assert(_fields_annotations == NULL, "Should have been cleared");
5885 assert(_fields_type_annotations == NULL, "Should have been cleared");
5886 } else {
|
3129 if (inner_classes_attribute_start != NULL) {
3130 cfs->set_current(inner_classes_attribute_start);
3131 cfs->guarantee_more(2, CHECK_0); // length
3132 length = cfs->get_u2_fast();
3133 }
3134
3135 // 4-tuples of shorts of inner classes data and 2 shorts of enclosing
3136 // method data:
3137 // [inner_class_info_index,
3138 // outer_class_info_index,
3139 // inner_name_index,
3140 // inner_class_access_flags,
3141 // ...
3142 // enclosing_method_class_index,
3143 // enclosing_method_method_index]
3144 const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0);
3145 Array<u2>* const inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3146 _inner_classes = inner_classes;
3147
3148 int index = 0;
3149 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2
3150 for (int n = 0; n < length; n++) {
3151 // Inner class index
3152 const u2 inner_class_info_index = cfs->get_u2_fast();
3153 check_property(
3154 valid_klass_reference_at(inner_class_info_index),
3155 "inner_class_info_index %u has bad constant type in class file %s",
3156 inner_class_info_index, CHECK_0);
3157 // Outer class index
3158 const u2 outer_class_info_index = cfs->get_u2_fast();
3159 check_property(
3160 outer_class_info_index == 0 ||
3161 valid_klass_reference_at(outer_class_info_index),
3162 "outer_class_info_index %u has bad constant type in class file %s",
3163 outer_class_info_index, CHECK_0);
3164 // Inner class name
3165 const u2 inner_name_index = cfs->get_u2_fast();
3166 check_property(
3167 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3168 "inner_name_index %u has bad constant type in class file %s",
3202 inner_classes->at(i+3) != inner_classes->at(j+3)),
3203 "Duplicate entry in InnerClasses in class file %s",
3204 CHECK_0);
3205 }
3206 }
3207 }
3208
3209 // Set EnclosingMethod class and method indexes.
3210 if (parsed_enclosingmethod_attribute) {
3211 inner_classes->at_put(index++, enclosing_method_class_index);
3212 inner_classes->at_put(index++, enclosing_method_method_index);
3213 }
3214 assert(index == size, "wrong size");
3215
3216 // Restore buffer's current position.
3217 cfs->set_current(current_mark);
3218
3219 return length;
3220 }
3221
3222 u2 ClassFileParser::parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,
3223 const u1* const nest_members_attribute_start,
3224 TRAPS) {
3225 const u1* const current_mark = cfs->current();
3226 u2 length = 0;
3227 if (nest_members_attribute_start != NULL) {
3228 cfs->set_current(nest_members_attribute_start);
3229 cfs->guarantee_more(2, CHECK_0); // length
3230 length = cfs->get_u2_fast();
3231 }
3232 const int size = length;
3233 Array<u2>* const nest_members = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3234 _nest_members = nest_members;
3235
3236 int index = 0;
3237 cfs->guarantee_more(2 * length, CHECK_0);
3238 for (int n = 0; n < length; n++) {
3239 const u2 class_info_index = cfs->get_u2_fast();
3240 check_property(
3241 valid_klass_reference_at(class_info_index),
3242 "Nest member class_info_index %u has bad constant type in class file %s",
3243 class_info_index, CHECK_0);
3244 nest_members->at_put(index++, class_info_index);
3245 }
3246 assert(index == size, "wrong size");
3247
3248 // Restore buffer's current position.
3249 cfs->set_current(current_mark);
3250
3251 return length;
3252 }
3253
3254 void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) {
3255 set_class_synthetic_flag(true);
3256 }
3257
3258 void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) {
3259 assert(cfs != NULL, "invariant");
3260
3261 const u2 signature_index = cfs->get_u2(CHECK);
3262 check_property(
3263 valid_symbol_at(signature_index),
3264 "Invalid constant pool index %u in Signature attribute in class file %s",
3265 signature_index, CHECK);
3266 set_class_generic_signature_index(signature_index);
3267 }
3268
3269 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
3270 ConstantPool* cp,
3271 u4 attribute_byte_length,
3272 TRAPS) {
3273 assert(cfs != NULL, "invariant");
3341 argument_index,
3342 CHECK);
3343 operands->at_put(operand_fill_index++, argument_index);
3344 }
3345 }
3346 guarantee_property(current_start + attribute_byte_length == cfs->current(),
3347 "Bad length on BootstrapMethods in class file %s",
3348 CHECK);
3349 }
3350
3351 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3352 ConstantPool* cp,
3353 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3354 TRAPS) {
3355 assert(cfs != NULL, "invariant");
3356 assert(cp != NULL, "invariant");
3357 assert(parsed_annotations != NULL, "invariant");
3358
3359 // Set inner classes attribute to default sentinel
3360 _inner_classes = Universe::the_empty_short_array();
3361 // Set nest members attribute to default sentinel
3362 _nest_members = Universe::the_empty_short_array();
3363 cfs->guarantee_more(2, CHECK); // attributes_count
3364 u2 attributes_count = cfs->get_u2_fast();
3365 bool parsed_sourcefile_attribute = false;
3366 bool parsed_innerclasses_attribute = false;
3367 bool parsed_nest_members_attribute = false;
3368 bool parsed_nest_host_attribute = false;
3369 bool parsed_enclosingmethod_attribute = false;
3370 bool parsed_bootstrap_methods_attribute = false;
3371 const u1* runtime_visible_annotations = NULL;
3372 int runtime_visible_annotations_length = 0;
3373 const u1* runtime_invisible_annotations = NULL;
3374 int runtime_invisible_annotations_length = 0;
3375 const u1* runtime_visible_type_annotations = NULL;
3376 int runtime_visible_type_annotations_length = 0;
3377 const u1* runtime_invisible_type_annotations = NULL;
3378 int runtime_invisible_type_annotations_length = 0;
3379 bool runtime_invisible_type_annotations_exists = false;
3380 bool runtime_invisible_annotations_exists = false;
3381 bool parsed_source_debug_ext_annotations_exist = false;
3382 const u1* inner_classes_attribute_start = NULL;
3383 u4 inner_classes_attribute_length = 0;
3384 u2 enclosing_method_class_index = 0;
3385 u2 enclosing_method_method_index = 0;
3386 const u1* nest_members_attribute_start = NULL;
3387 u4 nest_members_attribute_length = 0;
3388
3389 // Iterate over attributes
3390 while (attributes_count--) {
3391 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3392 const u2 attribute_name_index = cfs->get_u2_fast();
3393 const u4 attribute_length = cfs->get_u4_fast();
3394 check_property(
3395 valid_symbol_at(attribute_name_index),
3396 "Attribute name has bad constant pool index %u in class file %s",
3397 attribute_name_index, CHECK);
3398 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3399 if (tag == vmSymbols::tag_source_file()) {
3400 // Check for SourceFile tag
3401 if (_need_verify) {
3402 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3403 }
3404 if (parsed_sourcefile_attribute) {
3405 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
3406 } else {
3407 parsed_sourcefile_attribute = true;
3408 }
3517 "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", CHECK);
3518 }
3519 runtime_visible_type_annotations_length = attribute_length;
3520 runtime_visible_type_annotations = cfs->current();
3521 assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
3522 // No need for the VM to parse Type annotations
3523 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);
3524 } else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) {
3525 if (runtime_invisible_type_annotations_exists) {
3526 classfile_parse_error(
3527 "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", CHECK);
3528 } else {
3529 runtime_invisible_type_annotations_exists = true;
3530 }
3531 if (PreserveAllAnnotations) {
3532 runtime_invisible_type_annotations_length = attribute_length;
3533 runtime_invisible_type_annotations = cfs->current();
3534 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
3535 }
3536 cfs->skip_u1(attribute_length, CHECK);
3537 } else if (_major_version >= JAVA_11_VERSION) {
3538 if (tag == vmSymbols::tag_nest_members()) {
3539 // Check for NestMembers tag
3540 if (parsed_nest_members_attribute) {
3541 classfile_parse_error("Multiple NestMembers attributes in class file %s", CHECK);
3542 } else {
3543 parsed_nest_members_attribute = true;
3544 }
3545 if (parsed_nest_host_attribute) {
3546 classfile_parse_error("Conflicting NestHost and NestMembers attributes in class file %s", CHECK);
3547 }
3548 nest_members_attribute_start = cfs->current();
3549 nest_members_attribute_length = attribute_length;
3550 cfs->skip_u1(nest_members_attribute_length, CHECK);
3551 } else if (tag == vmSymbols::tag_nest_host()) {
3552 if (parsed_nest_host_attribute) {
3553 classfile_parse_error("Multiple NestHost attributes in class file %s", CHECK);
3554 } else {
3555 parsed_nest_host_attribute = true;
3556 }
3557 if (parsed_nest_members_attribute) {
3558 classfile_parse_error("Conflicting NestMembers and NestHost attributes in class file %s", CHECK);
3559 }
3560 if (_need_verify) {
3561 guarantee_property(attribute_length == 2, "Wrong NestHost attribute length in class file %s", CHECK);
3562 }
3563 cfs->guarantee_more(2, CHECK);
3564 u2 class_info_index = cfs->get_u2_fast();
3565 check_property(
3566 valid_klass_reference_at(class_info_index),
3567 "Nest-host class_info_index %u has bad constant type in class file %s",
3568 class_info_index, CHECK);
3569 _nest_host = class_info_index;
3570 }
3571 } else {
3572 // Unknown attribute
3573 cfs->skip_u1(attribute_length, CHECK);
3574 }
3575 } else {
3576 // Unknown attribute
3577 cfs->skip_u1(attribute_length, CHECK);
3578 }
3579 }
3580 _annotations = assemble_annotations(runtime_visible_annotations,
3581 runtime_visible_annotations_length,
3582 runtime_invisible_annotations,
3583 runtime_invisible_annotations_length,
3584 CHECK);
3585 _type_annotations = assemble_annotations(runtime_visible_type_annotations,
3586 runtime_visible_type_annotations_length,
3587 runtime_invisible_type_annotations,
3588 runtime_invisible_type_annotations_length,
3589 CHECK);
3590
3591 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) {
3592 const u2 num_of_classes = parse_classfile_inner_classes_attribute(
3593 cfs,
3594 inner_classes_attribute_start,
3595 parsed_innerclasses_attribute,
3596 enclosing_method_class_index,
3597 enclosing_method_method_index,
3598 CHECK);
3599 if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) {
3600 guarantee_property(
3601 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
3602 "Wrong InnerClasses attribute length in class file %s", CHECK);
3603 }
3604 }
3605
3606 if (parsed_nest_members_attribute) {
3607 const u2 num_of_classes = parse_classfile_nest_members_attribute(
3608 cfs,
3609 nest_members_attribute_start,
3610 CHECK);
3611 if (_need_verify) {
3612 guarantee_property(
3613 nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes,
3614 "Wrong NestMembers attribute length in class file %s", CHECK);
3615 }
3616 }
3617
3618 if (_max_bootstrap_specifier_index >= 0) {
3619 guarantee_property(parsed_bootstrap_methods_attribute,
3620 "Missing BootstrapMethods attribute in class file %s", CHECK);
3621 }
3622 }
3623
3624 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3625 assert(k != NULL, "invariant");
3626
3627 if (_synthetic_flag)
3628 k->set_is_synthetic();
3629 if (_sourcefile_index != 0) {
3630 k->set_source_file_name_index(_sourcefile_index);
3631 }
3632 if (_generic_signature_index != 0) {
3633 k->set_generic_signature_index(_generic_signature_index);
3634 }
3635 if (_sde_buffer != NULL) {
3636 k->set_source_debug_extension(_sde_buffer, _sde_length);
3637 }
3660
3661 // The annotations arrays below has been transfered the
3662 // _combined_annotations so these fields can now be cleared.
3663 _annotations = NULL;
3664 _type_annotations = NULL;
3665 _fields_annotations = NULL;
3666 _fields_type_annotations = NULL;
3667 }
3668
3669 // Transfer ownership of metadata allocated to the InstanceKlass.
3670 void ClassFileParser::apply_parsed_class_metadata(
3671 InstanceKlass* this_klass,
3672 int java_fields_count, TRAPS) {
3673 assert(this_klass != NULL, "invariant");
3674
3675 _cp->set_pool_holder(this_klass);
3676 this_klass->set_constants(_cp);
3677 this_klass->set_fields(_fields, java_fields_count);
3678 this_klass->set_methods(_methods);
3679 this_klass->set_inner_classes(_inner_classes);
3680 this_klass->set_nest_members(_nest_members);
3681 this_klass->set_nest_host_index(_nest_host);
3682 this_klass->set_local_interfaces(_local_interfaces);
3683 this_klass->set_annotations(_combined_annotations);
3684 // Delay the setting of _transitive_interfaces until after initialize_supers() in
3685 // fill_instance_klass(). It is because the _transitive_interfaces may be shared with
3686 // its _super. If an OOM occurs while loading the current klass, its _super field
3687 // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3688 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3689 // dereferences to the deallocated _transitive_interfaces will result in a crash.
3690
3691 // Clear out these fields so they don't get deallocated by the destructor
3692 clear_class_metadata();
3693 }
3694
3695 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
3696 int runtime_visible_annotations_length,
3697 const u1* const runtime_invisible_annotations,
3698 int runtime_invisible_annotations_length,
3699 TRAPS) {
3700 AnnotationArray* annotations = NULL;
3701 if (runtime_visible_annotations != NULL ||
4672 const Method* const m = methods->at(index);
4673
4674 // skip private, static, and <init> methods
4675 if ((!m->is_private() && !m->is_static()) &&
4676 (m->name() != vmSymbols::object_initializer_name())) {
4677
4678 const Symbol* const name = m->name();
4679 const Symbol* const signature = m->signature();
4680 const Klass* k = this_klass->super();
4681 const Method* super_m = NULL;
4682 while (k != NULL) {
4683 // skip supers that don't have final methods.
4684 if (k->has_final_method()) {
4685 // lookup a matching method in the super class hierarchy
4686 super_m = InstanceKlass::cast(k)->lookup_method(name, signature);
4687 if (super_m == NULL) {
4688 break; // didn't find any match; get out
4689 }
4690
4691 if (super_m->is_final() && !super_m->is_static() &&
4692 !super_m->access_flags().is_private()) {
4693 // matching method in super is final, and not static or private
4694 bool can_access = Reflection::verify_member_access(this_klass,
4695 super_m->method_holder(),
4696 super_m->method_holder(),
4697 super_m->access_flags(),
4698 false, false, CHECK);
4699 if (can_access) {
4700 // this class can access super final method and therefore override
4701 ResourceMark rm(THREAD);
4702 Exceptions::fthrow(THREAD_AND_LOCATION,
4703 vmSymbols::java_lang_VerifyError(),
4704 "class %s overrides final method %s.%s%s",
4705 this_klass->external_name(),
4706 super_m->method_holder()->external_name(),
4707 name->as_C_string(),
4708 signature->as_C_string()
4709 );
4710 return;
4711 }
4712 }
4713
4714 // continue to look from super_m's holder's super.
4715 k = super_m->method_holder()->super();
4716 continue;
4717 }
4718
4719 k = k->super();
4720 }
4721 }
4722 }
4723 }
4724
4725
4726 // assumes that this_klass is an interface
4727 static void check_illegal_static_method(const InstanceKlass* this_klass, TRAPS) {
4728 assert(this_klass != NULL, "invariant");
4729 assert(this_klass->is_interface(), "not an interface");
4730 const Array<Method*>* methods = this_klass->methods();
4731 const int num_methods = methods->length();
4732
5539 assert(ik->size_helper() == _field_info->instance_size, "sanity");
5540
5541 // Fill in information already parsed
5542 ik->set_should_verify_class(_need_verify);
5543
5544 // Not yet: supers are done below to support the new subtype-checking fields
5545 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5546 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5547 assert(_fac != NULL, "invariant");
5548 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5549
5550 // this transfers ownership of a lot of arrays from
5551 // the parser onto the InstanceKlass*
5552 apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5553
5554 // note that is not safe to use the fields in the parser from this point on
5555 assert(NULL == _cp, "invariant");
5556 assert(NULL == _fields, "invariant");
5557 assert(NULL == _methods, "invariant");
5558 assert(NULL == _inner_classes, "invariant");
5559 assert(NULL == _nest_members, "invariant");
5560 assert(NULL == _local_interfaces, "invariant");
5561 assert(NULL == _combined_annotations, "invariant");
5562
5563 if (_has_final_method) {
5564 ik->set_has_final_method();
5565 }
5566
5567 ik->copy_method_ordering(_method_ordering, CHECK);
5568 // The InstanceKlass::_methods_jmethod_ids cache
5569 // is managed on the assumption that the initial cache
5570 // size is equal to the number of methods in the class. If
5571 // that changes, then InstanceKlass::idnum_can_increment()
5572 // has to be changed accordingly.
5573 ik->set_initial_method_idnum(ik->methods()->length());
5574
5575 ik->set_this_class_index(_this_class_index);
5576
5577 if (is_anonymous()) {
5578 // _this_class_index is a CONSTANT_Class entry that refers to this
5579 // anonymous class itself. If this class needs to refer to its own methods or
5809 ClassLoaderData* loader_data,
5810 Handle protection_domain,
5811 const InstanceKlass* host_klass,
5812 GrowableArray<Handle>* cp_patches,
5813 Publicity pub_level,
5814 TRAPS) :
5815 _stream(stream),
5816 _requested_name(name),
5817 _loader_data(loader_data),
5818 _host_klass(host_klass),
5819 _cp_patches(cp_patches),
5820 _num_patched_klasses(0),
5821 _max_num_patched_klasses(0),
5822 _orig_cp_size(0),
5823 _first_patched_klass_resolved_index(0),
5824 _super_klass(),
5825 _cp(NULL),
5826 _fields(NULL),
5827 _methods(NULL),
5828 _inner_classes(NULL),
5829 _nest_members(NULL),
5830 _nest_host(0),
5831 _local_interfaces(NULL),
5832 _transitive_interfaces(NULL),
5833 _combined_annotations(NULL),
5834 _annotations(NULL),
5835 _type_annotations(NULL),
5836 _fields_annotations(NULL),
5837 _fields_type_annotations(NULL),
5838 _klass(NULL),
5839 _klass_to_deallocate(NULL),
5840 _parsed_annotations(NULL),
5841 _fac(NULL),
5842 _field_info(NULL),
5843 _method_ordering(NULL),
5844 _all_mirandas(NULL),
5845 _vtable_size(0),
5846 _itable_size(0),
5847 _num_miranda_methods(0),
5848 _rt(REF_NONE),
5849 _protection_domain(protection_domain),
5850 _access_flags(),
5915
5916 // synch back verification state to stream
5917 stream->set_verify(_need_verify);
5918
5919 // Check if verification needs to be relaxed for this class file
5920 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5921 _relax_verify = relax_format_check_for(_loader_data);
5922
5923 parse_stream(stream, CHECK);
5924
5925 post_process_parsed_stream(stream, _cp, CHECK);
5926 }
5927
5928 void ClassFileParser::clear_class_metadata() {
5929 // metadata created before the instance klass is created. Must be
5930 // deallocated if classfile parsing returns an error.
5931 _cp = NULL;
5932 _fields = NULL;
5933 _methods = NULL;
5934 _inner_classes = NULL;
5935 _nest_members = NULL;
5936 _local_interfaces = NULL;
5937 _combined_annotations = NULL;
5938 _annotations = _type_annotations = NULL;
5939 _fields_annotations = _fields_type_annotations = NULL;
5940 }
5941
5942 // Destructor to clean up
5943 ClassFileParser::~ClassFileParser() {
5944 if (_cp != NULL) {
5945 MetadataFactory::free_metadata(_loader_data, _cp);
5946 }
5947 if (_fields != NULL) {
5948 MetadataFactory::free_array<u2>(_loader_data, _fields);
5949 }
5950
5951 if (_methods != NULL) {
5952 // Free methods
5953 InstanceKlass::deallocate_methods(_loader_data, _methods);
5954 }
5955
5956 // beware of the Universe::empty_blah_array!!
5957 if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {
5958 MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5959 }
5960
5961 if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
5962 MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5963 }
5964
5965 // Free interfaces
5966 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5967 _local_interfaces, _transitive_interfaces);
5968
5969 if (_combined_annotations != NULL) {
5970 // After all annotations arrays have been created, they are installed into the
5971 // Annotations object that will be assigned to the InstanceKlass being created.
5972
5973 // Deallocate the Annotations object and the installed annotations arrays.
5974 _combined_annotations->deallocate_contents(_loader_data);
5975
5976 // If the _combined_annotations pointer is non-NULL,
5977 // then the other annotations fields should have been cleared.
5978 assert(_annotations == NULL, "Should have been cleared");
5979 assert(_type_annotations == NULL, "Should have been cleared");
5980 assert(_fields_annotations == NULL, "Should have been cleared");
5981 assert(_fields_type_annotations == NULL, "Should have been cleared");
5982 } else {
|