< prev index next >

src/hotspot/share/classfile/classFileParser.cpp

Print this page




3127   if (inner_classes_attribute_start != NULL) {
3128     cfs->set_current(inner_classes_attribute_start);
3129     cfs->guarantee_more(2, CHECK_0);  // length
3130     length = cfs->get_u2_fast();
3131   }
3132 
3133   // 4-tuples of shorts of inner classes data and 2 shorts of enclosing
3134   // method data:
3135   //   [inner_class_info_index,
3136   //    outer_class_info_index,
3137   //    inner_name_index,
3138   //    inner_class_access_flags,
3139   //    ...
3140   //    enclosing_method_class_index,
3141   //    enclosing_method_method_index]
3142   const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0);
3143   Array<u2>* const inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3144   _inner_classes = inner_classes;
3145 
3146   int index = 0;
3147   const int cp_size = _cp->length();
3148   cfs->guarantee_more(8 * length, CHECK_0);  // 4-tuples of u2
3149   for (int n = 0; n < length; n++) {
3150     // Inner class index
3151     const u2 inner_class_info_index = cfs->get_u2_fast();
3152     check_property(
3153       valid_klass_reference_at(inner_class_info_index),
3154       "inner_class_info_index %u has bad constant type in class file %s",
3155       inner_class_info_index, CHECK_0);
3156     // Outer class index
3157     const u2 outer_class_info_index = cfs->get_u2_fast();
3158     check_property(
3159       outer_class_info_index == 0 ||
3160         valid_klass_reference_at(outer_class_info_index),
3161       "outer_class_info_index %u has bad constant type in class file %s",
3162       outer_class_info_index, CHECK_0);
3163     // Inner class name
3164     const u2 inner_name_index = cfs->get_u2_fast();
3165     check_property(
3166       inner_name_index == 0 || valid_symbol_at(inner_name_index),
3167       "inner_name_index %u has bad constant type in class file %s",


3201                             inner_classes->at(i+3) != inner_classes->at(j+3)),
3202                             "Duplicate entry in InnerClasses in class file %s",
3203                             CHECK_0);
3204       }
3205     }
3206   }
3207 
3208   // Set EnclosingMethod class and method indexes.
3209   if (parsed_enclosingmethod_attribute) {
3210     inner_classes->at_put(index++, enclosing_method_class_index);
3211     inner_classes->at_put(index++, enclosing_method_method_index);
3212   }
3213   assert(index == size, "wrong size");
3214 
3215   // Restore buffer's current position.
3216   cfs->set_current(current_mark);
3217 
3218   return length;
3219 }
3220 
































3221 void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) {
3222   set_class_synthetic_flag(true);
3223 }
3224 
3225 void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) {
3226   assert(cfs != NULL, "invariant");
3227 
3228   const u2 signature_index = cfs->get_u2(CHECK);
3229   check_property(
3230     valid_symbol_at(signature_index),
3231     "Invalid constant pool index %u in Signature attribute in class file %s",
3232     signature_index, CHECK);
3233   set_class_generic_signature_index(signature_index);
3234 }
3235 
3236 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
3237                                                                   ConstantPool* cp,
3238                                                                   u4 attribute_byte_length,
3239                                                                   TRAPS) {
3240   assert(cfs != NULL, "invariant");


3308         argument_index,
3309         CHECK);
3310       operands->at_put(operand_fill_index++, argument_index);
3311     }
3312   }
3313   guarantee_property(current_start + attribute_byte_length == cfs->current(),
3314                      "Bad length on BootstrapMethods in class file %s",
3315                      CHECK);
3316 }
3317 
3318 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3319                                                  ConstantPool* cp,
3320                  ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3321                                                  TRAPS) {
3322   assert(cfs != NULL, "invariant");
3323   assert(cp != NULL, "invariant");
3324   assert(parsed_annotations != NULL, "invariant");
3325 
3326   // Set inner classes attribute to default sentinel
3327   _inner_classes = Universe::the_empty_short_array();


3328   cfs->guarantee_more(2, CHECK);  // attributes_count
3329   u2 attributes_count = cfs->get_u2_fast();
3330   bool parsed_sourcefile_attribute = false;
3331   bool parsed_innerclasses_attribute = false;


3332   bool parsed_enclosingmethod_attribute = false;
3333   bool parsed_bootstrap_methods_attribute = false;
3334   const u1* runtime_visible_annotations = NULL;
3335   int runtime_visible_annotations_length = 0;
3336   const u1* runtime_invisible_annotations = NULL;
3337   int runtime_invisible_annotations_length = 0;
3338   const u1* runtime_visible_type_annotations = NULL;
3339   int runtime_visible_type_annotations_length = 0;
3340   const u1* runtime_invisible_type_annotations = NULL;
3341   int runtime_invisible_type_annotations_length = 0;
3342   bool runtime_invisible_type_annotations_exists = false;
3343   bool runtime_invisible_annotations_exists = false;
3344   bool parsed_source_debug_ext_annotations_exist = false;
3345   const u1* inner_classes_attribute_start = NULL;
3346   u4  inner_classes_attribute_length = 0;
3347   u2  enclosing_method_class_index = 0;
3348   u2  enclosing_method_method_index = 0;



3349   // Iterate over attributes
3350   while (attributes_count--) {
3351     cfs->guarantee_more(6, CHECK);  // attribute_name_index, attribute_length
3352     const u2 attribute_name_index = cfs->get_u2_fast();
3353     const u4 attribute_length = cfs->get_u4_fast();
3354     check_property(
3355       valid_symbol_at(attribute_name_index),
3356       "Attribute name has bad constant pool index %u in class file %s",
3357       attribute_name_index, CHECK);
3358     const Symbol* const tag = cp->symbol_at(attribute_name_index);
3359     if (tag == vmSymbols::tag_source_file()) {
3360       // Check for SourceFile tag
3361       if (_need_verify) {
3362         guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3363       }
3364       if (parsed_sourcefile_attribute) {
3365         classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
3366       } else {
3367         parsed_sourcefile_attribute = true;
3368       }


3477             "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", CHECK);
3478         }
3479         runtime_visible_type_annotations_length = attribute_length;
3480         runtime_visible_type_annotations = cfs->current();
3481         assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
3482         // No need for the VM to parse Type annotations
3483         cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);
3484       } else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) {
3485         if (runtime_invisible_type_annotations_exists) {
3486           classfile_parse_error(
3487             "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", CHECK);
3488         } else {
3489           runtime_invisible_type_annotations_exists = true;
3490         }
3491         if (PreserveAllAnnotations) {
3492           runtime_invisible_type_annotations_length = attribute_length;
3493           runtime_invisible_type_annotations = cfs->current();
3494           assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
3495         }
3496         cfs->skip_u1(attribute_length, CHECK);


































3497       } else {
3498         // Unknown attribute
3499         cfs->skip_u1(attribute_length, CHECK);
3500       }
3501     } else {
3502       // Unknown attribute
3503       cfs->skip_u1(attribute_length, CHECK);
3504     }
3505   }
3506   _annotations = assemble_annotations(runtime_visible_annotations,
3507                                       runtime_visible_annotations_length,
3508                                       runtime_invisible_annotations,
3509                                       runtime_invisible_annotations_length,
3510                                       CHECK);
3511   _type_annotations = assemble_annotations(runtime_visible_type_annotations,
3512                                            runtime_visible_type_annotations_length,
3513                                            runtime_invisible_type_annotations,
3514                                            runtime_invisible_type_annotations_length,
3515                                            CHECK);
3516 
3517   if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) {
3518     const u2 num_of_classes = parse_classfile_inner_classes_attribute(
3519                             cfs,
3520                             inner_classes_attribute_start,
3521                             parsed_innerclasses_attribute,
3522                             enclosing_method_class_index,
3523                             enclosing_method_method_index,
3524                             CHECK);
3525     if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) {
3526       guarantee_property(
3527         inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
3528         "Wrong InnerClasses attribute length in class file %s", CHECK);
3529     }
3530   }
3531 












3532   if (_max_bootstrap_specifier_index >= 0) {
3533     guarantee_property(parsed_bootstrap_methods_attribute,
3534                        "Missing BootstrapMethods attribute in class file %s", CHECK);
3535   }
3536 }
3537 
3538 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3539   assert(k != NULL, "invariant");
3540 
3541   if (_synthetic_flag)
3542     k->set_is_synthetic();
3543   if (_sourcefile_index != 0) {
3544     k->set_source_file_name_index(_sourcefile_index);
3545   }
3546   if (_generic_signature_index != 0) {
3547     k->set_generic_signature_index(_generic_signature_index);
3548   }
3549   if (_sde_buffer != NULL) {
3550     k->set_source_debug_extension(_sde_buffer, _sde_length);
3551   }


3574 
3575     // The annotations arrays below has been transfered the
3576     // _combined_annotations so these fields can now be cleared.
3577     _annotations             = NULL;
3578     _type_annotations        = NULL;
3579     _fields_annotations      = NULL;
3580     _fields_type_annotations = NULL;
3581 }
3582 
3583 // Transfer ownership of metadata allocated to the InstanceKlass.
3584 void ClassFileParser::apply_parsed_class_metadata(
3585                                             InstanceKlass* this_klass,
3586                                             int java_fields_count, TRAPS) {
3587   assert(this_klass != NULL, "invariant");
3588 
3589   _cp->set_pool_holder(this_klass);
3590   this_klass->set_constants(_cp);
3591   this_klass->set_fields(_fields, java_fields_count);
3592   this_klass->set_methods(_methods);
3593   this_klass->set_inner_classes(_inner_classes);


3594   this_klass->set_local_interfaces(_local_interfaces);
3595   this_klass->set_annotations(_combined_annotations);
3596   // Delay the setting of _transitive_interfaces until after initialize_supers() in
3597   // fill_instance_klass(). It is because the _transitive_interfaces may be shared with
3598   // its _super. If an OOM occurs while loading the current klass, its _super field
3599   // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3600   // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3601   // dereferences to the deallocated _transitive_interfaces will result in a crash.
3602 
3603   // Clear out these fields so they don't get deallocated by the destructor
3604   clear_class_metadata();
3605 }
3606 
3607 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
3608                                                        int runtime_visible_annotations_length,
3609                                                        const u1* const runtime_invisible_annotations,
3610                                                        int runtime_invisible_annotations_length,
3611                                                        TRAPS) {
3612   AnnotationArray* annotations = NULL;
3613   if (runtime_visible_annotations != NULL ||


4584     const Method* const m = methods->at(index);
4585 
4586     // skip private, static, and <init> methods
4587     if ((!m->is_private() && !m->is_static()) &&
4588         (m->name() != vmSymbols::object_initializer_name())) {
4589 
4590       const Symbol* const name = m->name();
4591       const Symbol* const signature = m->signature();
4592       const Klass* k = this_klass->super();
4593       const Method* super_m = NULL;
4594       while (k != NULL) {
4595         // skip supers that don't have final methods.
4596         if (k->has_final_method()) {
4597           // lookup a matching method in the super class hierarchy
4598           super_m = InstanceKlass::cast(k)->lookup_method(name, signature);
4599           if (super_m == NULL) {
4600             break; // didn't find any match; get out
4601           }
4602 
4603           if (super_m->is_final() && !super_m->is_static() &&
4604               // matching method in super is final, and not static

4605               (Reflection::verify_field_access(this_klass,
4606                                                super_m->method_holder(),
4607                                                super_m->method_holder(),
4608                                                super_m->access_flags(), false))
4609             // this class can access super final method and therefore override
4610             ) {





4611             ResourceMark rm(THREAD);
4612             Exceptions::fthrow(
4613               THREAD_AND_LOCATION,
4614               vmSymbols::java_lang_VerifyError(),
4615               "class %s overrides final method %s.%s%s",
4616               this_klass->external_name(),
4617               super_m->method_holder()->external_name(),
4618               name->as_C_string(),
4619               signature->as_C_string()
4620             );
4621             return;
4622           }
4623 
4624           // continue to look from super_m's holder's super.
4625           k = super_m->method_holder()->super();
4626           continue;
4627         }
4628 
4629         k = k->super();
4630       }


5449   assert(ik->size_helper() == _field_info->instance_size, "sanity");
5450 
5451   // Fill in information already parsed
5452   ik->set_should_verify_class(_need_verify);
5453 
5454   // Not yet: supers are done below to support the new subtype-checking fields
5455   ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5456   ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5457   assert(_fac != NULL, "invariant");
5458   ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5459 
5460   // this transfers ownership of a lot of arrays from
5461   // the parser onto the InstanceKlass*
5462   apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5463 
5464   // note that is not safe to use the fields in the parser from this point on
5465   assert(NULL == _cp, "invariant");
5466   assert(NULL == _fields, "invariant");
5467   assert(NULL == _methods, "invariant");
5468   assert(NULL == _inner_classes, "invariant");

5469   assert(NULL == _local_interfaces, "invariant");
5470   assert(NULL == _combined_annotations, "invariant");
5471 
5472   if (_has_final_method) {
5473     ik->set_has_final_method();
5474   }
5475 
5476   ik->copy_method_ordering(_method_ordering, CHECK);
5477   // The InstanceKlass::_methods_jmethod_ids cache
5478   // is managed on the assumption that the initial cache
5479   // size is equal to the number of methods in the class. If
5480   // that changes, then InstanceKlass::idnum_can_increment()
5481   // has to be changed accordingly.
5482   ik->set_initial_method_idnum(ik->methods()->length());
5483 
5484   ik->set_this_class_index(_this_class_index);
5485 
5486   if (is_anonymous()) {
5487     // _this_class_index is a CONSTANT_Class entry that refers to this
5488     // anonymous class itself. If this class needs to refer to its own methods or


5718                                  ClassLoaderData* loader_data,
5719                                  Handle protection_domain,
5720                                  const InstanceKlass* host_klass,
5721                                  GrowableArray<Handle>* cp_patches,
5722                                  Publicity pub_level,
5723                                  TRAPS) :
5724   _stream(stream),
5725   _requested_name(name),
5726   _loader_data(loader_data),
5727   _host_klass(host_klass),
5728   _cp_patches(cp_patches),
5729   _num_patched_klasses(0),
5730   _max_num_patched_klasses(0),
5731   _orig_cp_size(0),
5732   _first_patched_klass_resolved_index(0),
5733   _super_klass(),
5734   _cp(NULL),
5735   _fields(NULL),
5736   _methods(NULL),
5737   _inner_classes(NULL),


5738   _local_interfaces(NULL),
5739   _transitive_interfaces(NULL),
5740   _combined_annotations(NULL),
5741   _annotations(NULL),
5742   _type_annotations(NULL),
5743   _fields_annotations(NULL),
5744   _fields_type_annotations(NULL),
5745   _klass(NULL),
5746   _klass_to_deallocate(NULL),
5747   _parsed_annotations(NULL),
5748   _fac(NULL),
5749   _field_info(NULL),
5750   _method_ordering(NULL),
5751   _all_mirandas(NULL),
5752   _vtable_size(0),
5753   _itable_size(0),
5754   _num_miranda_methods(0),
5755   _rt(REF_NONE),
5756   _protection_domain(protection_domain),
5757   _access_flags(),


5822 
5823   // synch back verification state to stream
5824   stream->set_verify(_need_verify);
5825 
5826   // Check if verification needs to be relaxed for this class file
5827   // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5828   _relax_verify = relax_format_check_for(_loader_data);
5829 
5830   parse_stream(stream, CHECK);
5831 
5832   post_process_parsed_stream(stream, _cp, CHECK);
5833 }
5834 
5835 void ClassFileParser::clear_class_metadata() {
5836   // metadata created before the instance klass is created.  Must be
5837   // deallocated if classfile parsing returns an error.
5838   _cp = NULL;
5839   _fields = NULL;
5840   _methods = NULL;
5841   _inner_classes = NULL;

5842   _local_interfaces = NULL;
5843   _combined_annotations = NULL;
5844   _annotations = _type_annotations = NULL;
5845   _fields_annotations = _fields_type_annotations = NULL;
5846 }
5847 
5848 // Destructor to clean up
5849 ClassFileParser::~ClassFileParser() {
5850   if (_cp != NULL) {
5851     MetadataFactory::free_metadata(_loader_data, _cp);
5852   }
5853   if (_fields != NULL) {
5854     MetadataFactory::free_array<u2>(_loader_data, _fields);
5855   }
5856 
5857   if (_methods != NULL) {
5858     // Free methods
5859     InstanceKlass::deallocate_methods(_loader_data, _methods);
5860   }
5861 
5862   // beware of the Universe::empty_blah_array!!
5863   if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {
5864     MetadataFactory::free_array<u2>(_loader_data, _inner_classes);




5865   }
5866 
5867   // Free interfaces
5868   InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5869                                        _local_interfaces, _transitive_interfaces);
5870 
5871   if (_combined_annotations != NULL) {
5872     // After all annotations arrays have been created, they are installed into the
5873     // Annotations object that will be assigned to the InstanceKlass being created.
5874 
5875     // Deallocate the Annotations object and the installed annotations arrays.
5876     _combined_annotations->deallocate_contents(_loader_data);
5877 
5878     // If the _combined_annotations pointer is non-NULL,
5879     // then the other annotations fields should have been cleared.
5880     assert(_annotations             == NULL, "Should have been cleared");
5881     assert(_type_annotations        == NULL, "Should have been cleared");
5882     assert(_fields_annotations      == NULL, "Should have been cleared");
5883     assert(_fields_type_annotations == NULL, "Should have been cleared");
5884   } else {




3127   if (inner_classes_attribute_start != NULL) {
3128     cfs->set_current(inner_classes_attribute_start);
3129     cfs->guarantee_more(2, CHECK_0);  // length
3130     length = cfs->get_u2_fast();
3131   }
3132 
3133   // 4-tuples of shorts of inner classes data and 2 shorts of enclosing
3134   // method data:
3135   //   [inner_class_info_index,
3136   //    outer_class_info_index,
3137   //    inner_name_index,
3138   //    inner_class_access_flags,
3139   //    ...
3140   //    enclosing_method_class_index,
3141   //    enclosing_method_method_index]
3142   const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0);
3143   Array<u2>* const inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3144   _inner_classes = inner_classes;
3145 
3146   int index = 0;

3147   cfs->guarantee_more(8 * length, CHECK_0);  // 4-tuples of u2
3148   for (int n = 0; n < length; n++) {
3149     // Inner class index
3150     const u2 inner_class_info_index = cfs->get_u2_fast();
3151     check_property(
3152       valid_klass_reference_at(inner_class_info_index),
3153       "inner_class_info_index %u has bad constant type in class file %s",
3154       inner_class_info_index, CHECK_0);
3155     // Outer class index
3156     const u2 outer_class_info_index = cfs->get_u2_fast();
3157     check_property(
3158       outer_class_info_index == 0 ||
3159         valid_klass_reference_at(outer_class_info_index),
3160       "outer_class_info_index %u has bad constant type in class file %s",
3161       outer_class_info_index, CHECK_0);
3162     // Inner class name
3163     const u2 inner_name_index = cfs->get_u2_fast();
3164     check_property(
3165       inner_name_index == 0 || valid_symbol_at(inner_name_index),
3166       "inner_name_index %u has bad constant type in class file %s",


3200                             inner_classes->at(i+3) != inner_classes->at(j+3)),
3201                             "Duplicate entry in InnerClasses in class file %s",
3202                             CHECK_0);
3203       }
3204     }
3205   }
3206 
3207   // Set EnclosingMethod class and method indexes.
3208   if (parsed_enclosingmethod_attribute) {
3209     inner_classes->at_put(index++, enclosing_method_class_index);
3210     inner_classes->at_put(index++, enclosing_method_method_index);
3211   }
3212   assert(index == size, "wrong size");
3213 
3214   // Restore buffer's current position.
3215   cfs->set_current(current_mark);
3216 
3217   return length;
3218 }
3219 
3220 u2 ClassFileParser::parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,
3221                                                            const u1* const nest_members_attribute_start,
3222                                                            TRAPS) {
3223   const u1* const current_mark = cfs->current();
3224   u2 length = 0;
3225   if (nest_members_attribute_start != NULL) {
3226     cfs->set_current(nest_members_attribute_start);
3227     cfs->guarantee_more(2, CHECK_0);  // length
3228     length = cfs->get_u2_fast();
3229   }
3230   const int size = length;
3231   Array<u2>* const nest_members = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3232   _nest_members = nest_members;
3233 
3234   int index = 0;
3235   cfs->guarantee_more(2 * length, CHECK_0);
3236   for (int n = 0; n < length; n++) {
3237     const u2 class_info_index = cfs->get_u2_fast();
3238     check_property(
3239       valid_klass_reference_at(class_info_index),
3240       "Nest member class_info_index %u has bad constant type in class file %s",
3241       class_info_index, CHECK_0);
3242     nest_members->at_put(index++, class_info_index);
3243   }
3244   assert(index == size, "wrong size");
3245 
3246   // Restore buffer's current position.
3247   cfs->set_current(current_mark);
3248 
3249   return length;
3250 }
3251 
3252 void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) {
3253   set_class_synthetic_flag(true);
3254 }
3255 
3256 void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) {
3257   assert(cfs != NULL, "invariant");
3258 
3259   const u2 signature_index = cfs->get_u2(CHECK);
3260   check_property(
3261     valid_symbol_at(signature_index),
3262     "Invalid constant pool index %u in Signature attribute in class file %s",
3263     signature_index, CHECK);
3264   set_class_generic_signature_index(signature_index);
3265 }
3266 
3267 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
3268                                                                   ConstantPool* cp,
3269                                                                   u4 attribute_byte_length,
3270                                                                   TRAPS) {
3271   assert(cfs != NULL, "invariant");


3339         argument_index,
3340         CHECK);
3341       operands->at_put(operand_fill_index++, argument_index);
3342     }
3343   }
3344   guarantee_property(current_start + attribute_byte_length == cfs->current(),
3345                      "Bad length on BootstrapMethods in class file %s",
3346                      CHECK);
3347 }
3348 
3349 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3350                                                  ConstantPool* cp,
3351                  ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3352                                                  TRAPS) {
3353   assert(cfs != NULL, "invariant");
3354   assert(cp != NULL, "invariant");
3355   assert(parsed_annotations != NULL, "invariant");
3356 
3357   // Set inner classes attribute to default sentinel
3358   _inner_classes = Universe::the_empty_short_array();
3359   // Set nest members attribute to default sentinel
3360   _nest_members = Universe::the_empty_short_array();
3361   cfs->guarantee_more(2, CHECK);  // attributes_count
3362   u2 attributes_count = cfs->get_u2_fast();
3363   bool parsed_sourcefile_attribute = false;
3364   bool parsed_innerclasses_attribute = false;
3365   bool parsed_nest_members_attribute = false;
3366   bool parsed_nest_host_attribute = false;
3367   bool parsed_enclosingmethod_attribute = false;
3368   bool parsed_bootstrap_methods_attribute = false;
3369   const u1* runtime_visible_annotations = NULL;
3370   int runtime_visible_annotations_length = 0;
3371   const u1* runtime_invisible_annotations = NULL;
3372   int runtime_invisible_annotations_length = 0;
3373   const u1* runtime_visible_type_annotations = NULL;
3374   int runtime_visible_type_annotations_length = 0;
3375   const u1* runtime_invisible_type_annotations = NULL;
3376   int runtime_invisible_type_annotations_length = 0;
3377   bool runtime_invisible_type_annotations_exists = false;
3378   bool runtime_invisible_annotations_exists = false;
3379   bool parsed_source_debug_ext_annotations_exist = false;
3380   const u1* inner_classes_attribute_start = NULL;
3381   u4  inner_classes_attribute_length = 0;
3382   u2  enclosing_method_class_index = 0;
3383   u2  enclosing_method_method_index = 0;
3384   const u1* nest_members_attribute_start = NULL;
3385   u4  nest_members_attribute_length = 0;
3386 
3387   // Iterate over attributes
3388   while (attributes_count--) {
3389     cfs->guarantee_more(6, CHECK);  // attribute_name_index, attribute_length
3390     const u2 attribute_name_index = cfs->get_u2_fast();
3391     const u4 attribute_length = cfs->get_u4_fast();
3392     check_property(
3393       valid_symbol_at(attribute_name_index),
3394       "Attribute name has bad constant pool index %u in class file %s",
3395       attribute_name_index, CHECK);
3396     const Symbol* const tag = cp->symbol_at(attribute_name_index);
3397     if (tag == vmSymbols::tag_source_file()) {
3398       // Check for SourceFile tag
3399       if (_need_verify) {
3400         guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3401       }
3402       if (parsed_sourcefile_attribute) {
3403         classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
3404       } else {
3405         parsed_sourcefile_attribute = true;
3406       }


3515             "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", CHECK);
3516         }
3517         runtime_visible_type_annotations_length = attribute_length;
3518         runtime_visible_type_annotations = cfs->current();
3519         assert(runtime_visible_type_annotations != NULL, "null visible type annotations");
3520         // No need for the VM to parse Type annotations
3521         cfs->skip_u1(runtime_visible_type_annotations_length, CHECK);
3522       } else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) {
3523         if (runtime_invisible_type_annotations_exists) {
3524           classfile_parse_error(
3525             "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", CHECK);
3526         } else {
3527           runtime_invisible_type_annotations_exists = true;
3528         }
3529         if (PreserveAllAnnotations) {
3530           runtime_invisible_type_annotations_length = attribute_length;
3531           runtime_invisible_type_annotations = cfs->current();
3532           assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations");
3533         }
3534         cfs->skip_u1(attribute_length, CHECK);
3535       } else if (_major_version >= JAVA_11_VERSION) {
3536         if (tag == vmSymbols::tag_nest_members()) {
3537           // Check for NestMembers tag
3538           if (parsed_nest_members_attribute) {
3539             classfile_parse_error("Multiple NestMembers attributes in class file %s", CHECK);
3540           } else {
3541             parsed_nest_members_attribute = true;
3542           }
3543           if (parsed_nest_host_attribute) {
3544             classfile_parse_error("Conflicting NestHost and NestMembers attributes in class file %s", CHECK);
3545           }
3546           nest_members_attribute_start = cfs->current();
3547           nest_members_attribute_length = attribute_length;
3548           cfs->skip_u1(nest_members_attribute_length, CHECK);
3549         } else if (tag == vmSymbols::tag_nest_host()) {
3550           if (parsed_nest_host_attribute) {
3551             classfile_parse_error("Multiple NestHost attributes in class file %s", CHECK);
3552           } else {
3553             parsed_nest_host_attribute = true;
3554           }
3555           if (parsed_nest_members_attribute) {
3556             classfile_parse_error("Conflicting NestMembers and NestHost attributes in class file %s", CHECK);
3557           }
3558           if (_need_verify) {
3559             guarantee_property(attribute_length == 2, "Wrong NestHost attribute length in class file %s", CHECK);
3560           }
3561           cfs->guarantee_more(2, CHECK);
3562           u2 class_info_index = cfs->get_u2_fast();
3563           check_property(
3564                          valid_klass_reference_at(class_info_index),
3565                          "Nest-host class_info_index %u has bad constant type in class file %s",
3566                          class_info_index, CHECK);
3567           _nest_host = class_info_index;
3568         }
3569       } else {
3570         // Unknown attribute
3571         cfs->skip_u1(attribute_length, CHECK);
3572       }
3573     } else {
3574       // Unknown attribute
3575       cfs->skip_u1(attribute_length, CHECK);
3576     }
3577   }
3578   _annotations = assemble_annotations(runtime_visible_annotations,
3579                                       runtime_visible_annotations_length,
3580                                       runtime_invisible_annotations,
3581                                       runtime_invisible_annotations_length,
3582                                       CHECK);
3583   _type_annotations = assemble_annotations(runtime_visible_type_annotations,
3584                                            runtime_visible_type_annotations_length,
3585                                            runtime_invisible_type_annotations,
3586                                            runtime_invisible_type_annotations_length,
3587                                            CHECK);
3588 
3589   if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) {
3590     const u2 num_of_classes = parse_classfile_inner_classes_attribute(
3591                             cfs,
3592                             inner_classes_attribute_start,
3593                             parsed_innerclasses_attribute,
3594                             enclosing_method_class_index,
3595                             enclosing_method_method_index,
3596                             CHECK);
3597     if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) {
3598       guarantee_property(
3599         inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
3600         "Wrong InnerClasses attribute length in class file %s", CHECK);
3601     }
3602   }
3603 
3604   if (parsed_nest_members_attribute) {
3605     const u2 num_of_classes = parse_classfile_nest_members_attribute(
3606                             cfs,
3607                             nest_members_attribute_start,
3608                             CHECK);
3609     if (_need_verify) {
3610       guarantee_property(
3611         nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes,
3612         "Wrong NestMembers attribute length in class file %s", CHECK);
3613     }
3614   }
3615 
3616   if (_max_bootstrap_specifier_index >= 0) {
3617     guarantee_property(parsed_bootstrap_methods_attribute,
3618                        "Missing BootstrapMethods attribute in class file %s", CHECK);
3619   }
3620 }
3621 
3622 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3623   assert(k != NULL, "invariant");
3624 
3625   if (_synthetic_flag)
3626     k->set_is_synthetic();
3627   if (_sourcefile_index != 0) {
3628     k->set_source_file_name_index(_sourcefile_index);
3629   }
3630   if (_generic_signature_index != 0) {
3631     k->set_generic_signature_index(_generic_signature_index);
3632   }
3633   if (_sde_buffer != NULL) {
3634     k->set_source_debug_extension(_sde_buffer, _sde_length);
3635   }


3658 
3659     // The annotations arrays below has been transfered the
3660     // _combined_annotations so these fields can now be cleared.
3661     _annotations             = NULL;
3662     _type_annotations        = NULL;
3663     _fields_annotations      = NULL;
3664     _fields_type_annotations = NULL;
3665 }
3666 
3667 // Transfer ownership of metadata allocated to the InstanceKlass.
3668 void ClassFileParser::apply_parsed_class_metadata(
3669                                             InstanceKlass* this_klass,
3670                                             int java_fields_count, TRAPS) {
3671   assert(this_klass != NULL, "invariant");
3672 
3673   _cp->set_pool_holder(this_klass);
3674   this_klass->set_constants(_cp);
3675   this_klass->set_fields(_fields, java_fields_count);
3676   this_klass->set_methods(_methods);
3677   this_klass->set_inner_classes(_inner_classes);
3678   this_klass->set_nest_members(_nest_members);
3679   this_klass->set_nest_host_index(_nest_host);
3680   this_klass->set_local_interfaces(_local_interfaces);
3681   this_klass->set_annotations(_combined_annotations);
3682   // Delay the setting of _transitive_interfaces until after initialize_supers() in
3683   // fill_instance_klass(). It is because the _transitive_interfaces may be shared with
3684   // its _super. If an OOM occurs while loading the current klass, its _super field
3685   // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3686   // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3687   // dereferences to the deallocated _transitive_interfaces will result in a crash.
3688 
3689   // Clear out these fields so they don't get deallocated by the destructor
3690   clear_class_metadata();
3691 }
3692 
3693 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
3694                                                        int runtime_visible_annotations_length,
3695                                                        const u1* const runtime_invisible_annotations,
3696                                                        int runtime_invisible_annotations_length,
3697                                                        TRAPS) {
3698   AnnotationArray* annotations = NULL;
3699   if (runtime_visible_annotations != NULL ||


4670     const Method* const m = methods->at(index);
4671 
4672     // skip private, static, and <init> methods
4673     if ((!m->is_private() && !m->is_static()) &&
4674         (m->name() != vmSymbols::object_initializer_name())) {
4675 
4676       const Symbol* const name = m->name();
4677       const Symbol* const signature = m->signature();
4678       const Klass* k = this_klass->super();
4679       const Method* super_m = NULL;
4680       while (k != NULL) {
4681         // skip supers that don't have final methods.
4682         if (k->has_final_method()) {
4683           // lookup a matching method in the super class hierarchy
4684           super_m = InstanceKlass::cast(k)->lookup_method(name, signature);
4685           if (super_m == NULL) {
4686             break; // didn't find any match; get out
4687           }
4688 
4689           if (super_m->is_final() && !super_m->is_static() &&
4690               !super_m->access_flags().is_private() &&
4691               // matching method in super is final, and not static or private
4692               (Reflection::verify_field_access(this_klass,
4693                                                super_m->method_holder(),
4694                                                super_m->method_holder(),
4695                                                super_m->access_flags(), false))
4696             // this class can access super final method and therefore override
4697             ) {
4698             // Propagate any existing exceptions that may have been thrown
4699             if (HAS_PENDING_EXCEPTION) {
4700               return;
4701             }
4702 
4703             ResourceMark rm(THREAD);
4704             Exceptions::fthrow(
4705               THREAD_AND_LOCATION,
4706               vmSymbols::java_lang_VerifyError(),
4707               "class %s overrides final method %s.%s%s",
4708               this_klass->external_name(),
4709               super_m->method_holder()->external_name(),
4710               name->as_C_string(),
4711               signature->as_C_string()
4712             );
4713             return;
4714           }
4715 
4716           // continue to look from super_m's holder's super.
4717           k = super_m->method_holder()->super();
4718           continue;
4719         }
4720 
4721         k = k->super();
4722       }


5541   assert(ik->size_helper() == _field_info->instance_size, "sanity");
5542 
5543   // Fill in information already parsed
5544   ik->set_should_verify_class(_need_verify);
5545 
5546   // Not yet: supers are done below to support the new subtype-checking fields
5547   ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5548   ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5549   assert(_fac != NULL, "invariant");
5550   ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5551 
5552   // this transfers ownership of a lot of arrays from
5553   // the parser onto the InstanceKlass*
5554   apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5555 
5556   // note that is not safe to use the fields in the parser from this point on
5557   assert(NULL == _cp, "invariant");
5558   assert(NULL == _fields, "invariant");
5559   assert(NULL == _methods, "invariant");
5560   assert(NULL == _inner_classes, "invariant");
5561   assert(NULL == _nest_members, "invariant");
5562   assert(NULL == _local_interfaces, "invariant");
5563   assert(NULL == _combined_annotations, "invariant");
5564 
5565   if (_has_final_method) {
5566     ik->set_has_final_method();
5567   }
5568 
5569   ik->copy_method_ordering(_method_ordering, CHECK);
5570   // The InstanceKlass::_methods_jmethod_ids cache
5571   // is managed on the assumption that the initial cache
5572   // size is equal to the number of methods in the class. If
5573   // that changes, then InstanceKlass::idnum_can_increment()
5574   // has to be changed accordingly.
5575   ik->set_initial_method_idnum(ik->methods()->length());
5576 
5577   ik->set_this_class_index(_this_class_index);
5578 
5579   if (is_anonymous()) {
5580     // _this_class_index is a CONSTANT_Class entry that refers to this
5581     // anonymous class itself. If this class needs to refer to its own methods or


5811                                  ClassLoaderData* loader_data,
5812                                  Handle protection_domain,
5813                                  const InstanceKlass* host_klass,
5814                                  GrowableArray<Handle>* cp_patches,
5815                                  Publicity pub_level,
5816                                  TRAPS) :
5817   _stream(stream),
5818   _requested_name(name),
5819   _loader_data(loader_data),
5820   _host_klass(host_klass),
5821   _cp_patches(cp_patches),
5822   _num_patched_klasses(0),
5823   _max_num_patched_klasses(0),
5824   _orig_cp_size(0),
5825   _first_patched_klass_resolved_index(0),
5826   _super_klass(),
5827   _cp(NULL),
5828   _fields(NULL),
5829   _methods(NULL),
5830   _inner_classes(NULL),
5831   _nest_members(NULL),
5832   _nest_host(0),
5833   _local_interfaces(NULL),
5834   _transitive_interfaces(NULL),
5835   _combined_annotations(NULL),
5836   _annotations(NULL),
5837   _type_annotations(NULL),
5838   _fields_annotations(NULL),
5839   _fields_type_annotations(NULL),
5840   _klass(NULL),
5841   _klass_to_deallocate(NULL),
5842   _parsed_annotations(NULL),
5843   _fac(NULL),
5844   _field_info(NULL),
5845   _method_ordering(NULL),
5846   _all_mirandas(NULL),
5847   _vtable_size(0),
5848   _itable_size(0),
5849   _num_miranda_methods(0),
5850   _rt(REF_NONE),
5851   _protection_domain(protection_domain),
5852   _access_flags(),


5917 
5918   // synch back verification state to stream
5919   stream->set_verify(_need_verify);
5920 
5921   // Check if verification needs to be relaxed for this class file
5922   // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5923   _relax_verify = relax_format_check_for(_loader_data);
5924 
5925   parse_stream(stream, CHECK);
5926 
5927   post_process_parsed_stream(stream, _cp, CHECK);
5928 }
5929 
5930 void ClassFileParser::clear_class_metadata() {
5931   // metadata created before the instance klass is created.  Must be
5932   // deallocated if classfile parsing returns an error.
5933   _cp = NULL;
5934   _fields = NULL;
5935   _methods = NULL;
5936   _inner_classes = NULL;
5937   _nest_members = NULL;
5938   _local_interfaces = NULL;
5939   _combined_annotations = NULL;
5940   _annotations = _type_annotations = NULL;
5941   _fields_annotations = _fields_type_annotations = NULL;
5942 }
5943 
5944 // Destructor to clean up
5945 ClassFileParser::~ClassFileParser() {
5946   if (_cp != NULL) {
5947     MetadataFactory::free_metadata(_loader_data, _cp);
5948   }
5949   if (_fields != NULL) {
5950     MetadataFactory::free_array<u2>(_loader_data, _fields);
5951   }
5952 
5953   if (_methods != NULL) {
5954     // Free methods
5955     InstanceKlass::deallocate_methods(_loader_data, _methods);
5956   }
5957 
5958   // beware of the Universe::empty_blah_array!!
5959   if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {
5960     MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5961   }
5962 
5963   if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
5964     MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5965   }
5966 
5967   // Free interfaces
5968   InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5969                                        _local_interfaces, _transitive_interfaces);
5970 
5971   if (_combined_annotations != NULL) {
5972     // After all annotations arrays have been created, they are installed into the
5973     // Annotations object that will be assigned to the InstanceKlass being created.
5974 
5975     // Deallocate the Annotations object and the installed annotations arrays.
5976     _combined_annotations->deallocate_contents(_loader_data);
5977 
5978     // If the _combined_annotations pointer is non-NULL,
5979     // then the other annotations fields should have been cleared.
5980     assert(_annotations             == NULL, "Should have been cleared");
5981     assert(_type_annotations        == NULL, "Should have been cleared");
5982     assert(_fields_annotations      == NULL, "Should have been cleared");
5983     assert(_fields_type_annotations == NULL, "Should have been cleared");
5984   } else {


< prev index next >