< prev index next >

src/hotspot/share/classfile/classFileParser.cpp

Print this page




1732   assert(NULL == _fields, "invariant");
1733 
1734   _fields =
1735     MetadataFactory::new_array<u2>(_loader_data,
1736                                    index * FieldInfo::field_slots + num_generic_signature,
1737                                    CHECK);
1738   // Sometimes injected fields already exist in the Java source so
1739   // the fields array could be too long.  In that case the
1740   // fields array is trimed. Also unused slots that were reserved
1741   // for generic signature indexes are discarded.
1742   {
1743     int i = 0;
1744     for (; i < index * FieldInfo::field_slots; i++) {
1745       _fields->at_put(i, fa[i]);
1746     }
1747     for (int j = total_fields * FieldInfo::field_slots;
1748          j < generic_signature_slot; j++) {
1749       _fields->at_put(i++, fa[j]);
1750     }
1751     assert(_fields->length() == i, "");

1752   }
1753 
1754   if (_need_verify && length > 1) {
1755     // Check duplicated fields
1756     ResourceMark rm(THREAD);
1757     NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
1758       THREAD, NameSigHash*, HASH_ROW_SIZE);
1759     initialize_hashtable(names_and_sigs);
1760     bool dup = false;
1761     const Symbol* name = NULL;
1762     const Symbol* sig = NULL;
1763     {
1764       debug_only(NoSafepointVerifier nsv;)
1765       for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
1766         name = fs.name();
1767         sig = fs.signature();
1768         // If no duplicates, add name/signature in hashtable names_and_sigs.
1769         if (!put_after_lookup(name, sig, names_and_sigs)) {
1770           dup = true;
1771           break;


3206   _nest_members = nest_members;
3207 
3208   int index = 0;
3209   cfs->guarantee_more(2 * length, CHECK_0);
3210   for (int n = 0; n < length; n++) {
3211     const u2 class_info_index = cfs->get_u2_fast();
3212     check_property(
3213       valid_klass_reference_at(class_info_index),
3214       "Nest member class_info_index %u has bad constant type in class file %s",
3215       class_info_index, CHECK_0);
3216     nest_members->at_put(index++, class_info_index);
3217   }
3218   assert(index == size, "wrong size");
3219 
3220   // Restore buffer's current position.
3221   cfs->set_current(current_mark);
3222 
3223   return length;
3224 }
3225 



























































































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


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




3335   cfs->guarantee_more(2, CHECK);  // attributes_count
3336   u2 attributes_count = cfs->get_u2_fast();
3337   bool parsed_sourcefile_attribute = false;
3338   bool parsed_innerclasses_attribute = false;
3339   bool parsed_nest_members_attribute = false;

3340   bool parsed_nest_host_attribute = false;

3341   bool parsed_enclosingmethod_attribute = false;
3342   bool parsed_bootstrap_methods_attribute = false;
3343   const u1* runtime_visible_annotations = NULL;
3344   int runtime_visible_annotations_length = 0;
3345   const u1* runtime_invisible_annotations = NULL;
3346   int runtime_invisible_annotations_length = 0;
3347   const u1* runtime_visible_type_annotations = NULL;
3348   int runtime_visible_type_annotations_length = 0;
3349   const u1* runtime_invisible_type_annotations = NULL;
3350   int runtime_invisible_type_annotations_length = 0;
3351   bool runtime_invisible_type_annotations_exists = false;
3352   bool runtime_invisible_annotations_exists = false;
3353   bool parsed_source_debug_ext_annotations_exist = false;
3354   const u1* inner_classes_attribute_start = NULL;
3355   u4  inner_classes_attribute_length = 0;
3356   u2  enclosing_method_class_index = 0;
3357   u2  enclosing_method_method_index = 0;
3358   const u1* nest_members_attribute_start = NULL;
3359   u4  nest_members_attribute_length = 0;




3360 
3361   // Iterate over attributes
3362   while (attributes_count--) {
3363     cfs->guarantee_more(6, CHECK);  // attribute_name_index, attribute_length
3364     const u2 attribute_name_index = cfs->get_u2_fast();
3365     const u4 attribute_length = cfs->get_u4_fast();
3366     check_property(
3367       valid_symbol_at(attribute_name_index),
3368       "Attribute name has bad constant pool index %u in class file %s",
3369       attribute_name_index, CHECK);
3370     const Symbol* const tag = cp->symbol_at(attribute_name_index);
3371     if (tag == vmSymbols::tag_source_file()) {
3372       // Check for SourceFile tag
3373       if (_need_verify) {
3374         guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3375       }
3376       if (parsed_sourcefile_attribute) {
3377         classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
3378       } else {
3379         parsed_sourcefile_attribute = true;


3522           cfs->skip_u1(nest_members_attribute_length, CHECK);
3523         } else if (tag == vmSymbols::tag_nest_host()) {
3524           if (parsed_nest_host_attribute) {
3525             classfile_parse_error("Multiple NestHost attributes in class file %s", CHECK);
3526           } else {
3527             parsed_nest_host_attribute = true;
3528           }
3529           if (parsed_nest_members_attribute) {
3530             classfile_parse_error("Conflicting NestMembers and NestHost attributes in class file %s", CHECK);
3531           }
3532           if (_need_verify) {
3533             guarantee_property(attribute_length == 2, "Wrong NestHost attribute length in class file %s", CHECK);
3534           }
3535           cfs->guarantee_more(2, CHECK);
3536           u2 class_info_index = cfs->get_u2_fast();
3537           check_property(
3538                          valid_klass_reference_at(class_info_index),
3539                          "Nest-host class_info_index %u has bad constant type in class file %s",
3540                          class_info_index, CHECK);
3541           _nest_host = class_info_index;






















3542         } else {
3543           // Unknown attribute
3544           cfs->skip_u1(attribute_length, CHECK);
3545         }
3546       } else {
3547         // Unknown attribute
3548         cfs->skip_u1(attribute_length, CHECK);
3549       }
3550     } else {
3551       // Unknown attribute
3552       cfs->skip_u1(attribute_length, CHECK);
3553     }
3554   }
3555   _annotations = assemble_annotations(runtime_visible_annotations,
3556                                       runtime_visible_annotations_length,
3557                                       runtime_invisible_annotations,
3558                                       runtime_invisible_annotations_length,
3559                                       CHECK);
3560   _type_annotations = assemble_annotations(runtime_visible_type_annotations,
3561                                            runtime_visible_type_annotations_length,


3573                             CHECK);
3574     if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) {
3575       guarantee_property(
3576         inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
3577         "Wrong InnerClasses attribute length in class file %s", CHECK);
3578     }
3579   }
3580 
3581   if (parsed_nest_members_attribute) {
3582     const u2 num_of_classes = parse_classfile_nest_members_attribute(
3583                             cfs,
3584                             nest_members_attribute_start,
3585                             CHECK);
3586     if (_need_verify) {
3587       guarantee_property(
3588         nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes,
3589         "Wrong NestMembers attribute length in class file %s", CHECK);
3590     }
3591   }
3592 





















3593   if (_max_bootstrap_specifier_index >= 0) {
3594     guarantee_property(parsed_bootstrap_methods_attribute,
3595                        "Missing BootstrapMethods attribute in class file %s", CHECK);
3596   }
3597 }
3598 
3599 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3600   assert(k != NULL, "invariant");
3601 
3602   if (_synthetic_flag)
3603     k->set_is_synthetic();
3604   if (_sourcefile_index != 0) {
3605     k->set_source_file_name_index(_sourcefile_index);
3606   }
3607   if (_generic_signature_index != 0) {
3608     k->set_generic_signature_index(_generic_signature_index);
3609   }
3610   if (_sde_buffer != NULL) {
3611     k->set_source_debug_extension(_sde_buffer, _sde_length);
3612   }


3627     annotations->set_class_annotations(_annotations);
3628     annotations->set_class_type_annotations(_type_annotations);
3629     annotations->set_fields_annotations(_fields_annotations);
3630     annotations->set_fields_type_annotations(_fields_type_annotations);
3631 
3632     // This is the Annotations object that will be
3633     // assigned to InstanceKlass being constructed.
3634     _combined_annotations = annotations;
3635 
3636     // The annotations arrays below has been transfered the
3637     // _combined_annotations so these fields can now be cleared.
3638     _annotations             = NULL;
3639     _type_annotations        = NULL;
3640     _fields_annotations      = NULL;
3641     _fields_type_annotations = NULL;
3642 }
3643 
3644 // Transfer ownership of metadata allocated to the InstanceKlass.
3645 void ClassFileParser::apply_parsed_class_metadata(
3646                                             InstanceKlass* this_klass,
3647                                             int java_fields_count, TRAPS) {

3648   assert(this_klass != NULL, "invariant");
3649 
3650   _cp->set_pool_holder(this_klass);
3651   this_klass->set_constants(_cp);
3652   this_klass->set_fields(_fields, java_fields_count);
3653   this_klass->set_methods(_methods);
3654   this_klass->set_inner_classes(_inner_classes);
3655   this_klass->set_nest_members(_nest_members);
3656   this_klass->set_nest_host_index(_nest_host);
3657   this_klass->set_local_interfaces(_local_interfaces);
3658   this_klass->set_annotations(_combined_annotations);


3659   // Delay the setting of _transitive_interfaces until after initialize_supers() in
3660   // fill_instance_klass(). It is because the _transitive_interfaces may be shared with
3661   // its _super. If an OOM occurs while loading the current klass, its _super field
3662   // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3663   // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3664   // dereferences to the deallocated _transitive_interfaces will result in a crash.
3665 
3666   // Clear out these fields so they don't get deallocated by the destructor
3667   clear_class_metadata();
3668 }
3669 
3670 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
3671                                                        int runtime_visible_annotations_length,
3672                                                        const u1* const runtime_invisible_annotations,
3673                                                        int runtime_invisible_annotations_length,
3674                                                        TRAPS) {
3675   AnnotationArray* annotations = NULL;
3676   if (runtime_visible_annotations != NULL ||
3677       runtime_invisible_annotations != NULL) {
3678     annotations = MetadataFactory::new_array<u1>(_loader_data,


4710   const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4711   assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4712   if (is_module) {
4713     ResourceMark rm(THREAD);
4714     Exceptions::fthrow(
4715       THREAD_AND_LOCATION,
4716       vmSymbols::java_lang_NoClassDefFoundError(),
4717       "%s is not a class because access_flag ACC_MODULE is set",
4718       _class_name->as_C_string());
4719     return;
4720   }
4721 
4722   if (!_need_verify) { return; }
4723 
4724   const bool is_interface  = (flags & JVM_ACC_INTERFACE)  != 0;
4725   const bool is_abstract   = (flags & JVM_ACC_ABSTRACT)   != 0;
4726   const bool is_final      = (flags & JVM_ACC_FINAL)      != 0;
4727   const bool is_super      = (flags & JVM_ACC_SUPER)      != 0;
4728   const bool is_enum       = (flags & JVM_ACC_ENUM)       != 0;
4729   const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4730   const bool major_gte_15  = _major_version >= JAVA_1_5_VERSION;

4731 
4732   if ((is_abstract && is_final) ||
4733       (is_interface && !is_abstract) ||
4734       (is_interface && major_gte_15 && (is_super || is_enum)) ||
4735       (!is_interface && major_gte_15 && is_annotation)) {
4736     ResourceMark rm(THREAD);
4737     Exceptions::fthrow(
4738       THREAD_AND_LOCATION,
4739       vmSymbols::java_lang_ClassFormatError(),
4740       "Illegal class modifiers in class %s: 0x%X",
4741       _class_name->as_C_string(), flags
4742     );
4743     return;
4744   }
4745 }
4746 
4747 static bool has_illegal_visibility(jint flags) {
4748   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4749   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4750   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4751 
4752   return ((is_public && is_protected) ||
4753           (is_public && is_private) ||
4754           (is_protected && is_private));
4755 }


5479 
5480   assert(_field_info != NULL, "invariant");
5481   assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5482   assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count,
5483     "sanity");
5484 
5485   assert(ik->is_instance_klass(), "sanity");
5486   assert(ik->size_helper() == _field_info->instance_size, "sanity");
5487 
5488   // Fill in information already parsed
5489   ik->set_should_verify_class(_need_verify);
5490 
5491   // Not yet: supers are done below to support the new subtype-checking fields
5492   ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5493   ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5494   assert(_fac != NULL, "invariant");
5495   ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5496 
5497   // this transfers ownership of a lot of arrays from
5498   // the parser onto the InstanceKlass*
5499   apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5500 
5501   // note that is not safe to use the fields in the parser from this point on
5502   assert(NULL == _cp, "invariant");
5503   assert(NULL == _fields, "invariant");
5504   assert(NULL == _methods, "invariant");
5505   assert(NULL == _inner_classes, "invariant");
5506   assert(NULL == _nest_members, "invariant");
5507   assert(NULL == _local_interfaces, "invariant");
5508   assert(NULL == _combined_annotations, "invariant");


5509 
5510   if (_has_final_method) {
5511     ik->set_has_final_method();
5512   }
5513 
5514   ik->copy_method_ordering(_method_ordering, CHECK);
5515   // The InstanceKlass::_methods_jmethod_ids cache
5516   // is managed on the assumption that the initial cache
5517   // size is equal to the number of methods in the class. If
5518   // that changes, then InstanceKlass::idnum_can_increment()
5519   // has to be changed accordingly.
5520   ik->set_initial_method_idnum(ik->methods()->length());
5521 
5522   ik->set_this_class_index(_this_class_index);
5523 
5524   if (is_unsafe_anonymous()) {
5525     // _this_class_index is a CONSTANT_Class entry that refers to this
5526     // anonymous class itself. If this class needs to refer to its own methods or
5527     // fields, it would use a CONSTANT_MethodRef, etc, which would reference
5528     // _this_class_index. However, because this class is anonymous (it's


5668         const int length = local_interfaces->length();
5669         for (int i = 0; i < length; i++) {
5670           const InstanceKlass* const k = local_interfaces->at(i);
5671           const char * to = k->external_name();
5672           log_debug(class, resolve)("%s %s (interface)", from, to);
5673         }
5674       }
5675     }
5676   }
5677 
5678   JFR_ONLY(INIT_ID(ik);)
5679 
5680   // If we reach here, all is well.
5681   // Now remove the InstanceKlass* from the _klass_to_deallocate field
5682   // in order for it to not be destroyed in the ClassFileParser destructor.
5683   set_klass_to_deallocate(NULL);
5684 
5685   // it's official
5686   set_klass(ik);
5687 


5688   debug_only(ik->verify();)
5689 }
5690 
5691 void ClassFileParser::update_class_name(Symbol* new_class_name) {
5692   // Decrement the refcount in the old name, since we're clobbering it.
5693   _class_name->decrement_refcount();
5694 
5695   _class_name = new_class_name;
5696   // Increment the refcount of the new name.
5697   // Now the ClassFileParser owns this name and will decrement in
5698   // the destructor.
5699   _class_name->increment_refcount();
5700 }
5701 
5702 
5703 // For an unsafe anonymous class that is in the unnamed package, move it to its host class's
5704 // package by prepending its host class's package name to its class name and setting
5705 // its _class_name field.
5706 void ClassFileParser::prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS) {
5707   ResourceMark rm(THREAD);


5770                                  GrowableArray<Handle>* cp_patches,
5771                                  Publicity pub_level,
5772                                  TRAPS) :
5773   _stream(stream),
5774   _requested_name(name),
5775   _class_name(NULL),
5776   _loader_data(loader_data),
5777   _unsafe_anonymous_host(unsafe_anonymous_host),
5778   _cp_patches(cp_patches),
5779   _num_patched_klasses(0),
5780   _max_num_patched_klasses(0),
5781   _orig_cp_size(0),
5782   _first_patched_klass_resolved_index(0),
5783   _super_klass(),
5784   _cp(NULL),
5785   _fields(NULL),
5786   _methods(NULL),
5787   _inner_classes(NULL),
5788   _nest_members(NULL),
5789   _nest_host(0),


5790   _local_interfaces(NULL),
5791   _transitive_interfaces(NULL),
5792   _combined_annotations(NULL),
5793   _annotations(NULL),
5794   _type_annotations(NULL),
5795   _fields_annotations(NULL),
5796   _fields_type_annotations(NULL),
5797   _klass(NULL),
5798   _klass_to_deallocate(NULL),
5799   _parsed_annotations(NULL),
5800   _fac(NULL),
5801   _field_info(NULL),
5802   _method_ordering(NULL),
5803   _all_mirandas(NULL),
5804   _vtable_size(0),
5805   _itable_size(0),
5806   _num_miranda_methods(0),
5807   _rt(REF_NONE),
5808   _protection_domain(protection_domain),
5809   _access_flags(),
5810   _pub_level(pub_level),
5811   _bad_constant_seen(0),
5812   _synthetic_flag(false),
5813   _sde_length(false),
5814   _sde_buffer(NULL),
5815   _sourcefile_index(0),
5816   _generic_signature_index(0),
5817   _major_version(0),
5818   _minor_version(0),
5819   _this_class_index(0),
5820   _super_class_index(0),
5821   _itfs_len(0),
5822   _java_fields_count(0),

5823   _need_verify(false),
5824   _relax_verify(false),
5825   _has_nonstatic_concrete_methods(false),
5826   _declares_nonstatic_concrete_methods(false),
5827   _has_final_method(false),
5828   _has_finalizer(false),
5829   _has_empty_finalizer(false),
5830   _has_vanilla_constructor(false),
5831   _max_bootstrap_specifier_index(-1) {
5832 
5833   _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
5834   _class_name->increment_refcount();
5835 
5836   assert(THREAD->is_Java_thread(), "invariant");
5837   assert(_loader_data != NULL, "invariant");
5838   assert(stream != NULL, "invariant");
5839   assert(_stream != NULL, "invariant");
5840   assert(_stream->buffer() == _stream->current(), "invariant");
5841   assert(_class_name != NULL, "invariant");
5842   assert(0 == _access_flags.as_int(), "invariant");


5876   // synch back verification state to stream
5877   stream->set_verify(_need_verify);
5878 
5879   // Check if verification needs to be relaxed for this class file
5880   // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5881   _relax_verify = relax_format_check_for(_loader_data);
5882 
5883   parse_stream(stream, CHECK);
5884 
5885   post_process_parsed_stream(stream, _cp, CHECK);
5886 }
5887 
5888 void ClassFileParser::clear_class_metadata() {
5889   // metadata created before the instance klass is created.  Must be
5890   // deallocated if classfile parsing returns an error.
5891   _cp = NULL;
5892   _fields = NULL;
5893   _methods = NULL;
5894   _inner_classes = NULL;
5895   _nest_members = NULL;

5896   _local_interfaces = NULL;
5897   _combined_annotations = NULL;
5898   _annotations = _type_annotations = NULL;
5899   _fields_annotations = _fields_type_annotations = NULL;

5900 }
5901 
5902 // Destructor to clean up
5903 ClassFileParser::~ClassFileParser() {
5904   _class_name->decrement_refcount();
5905 
5906   if (_cp != NULL) {
5907     MetadataFactory::free_metadata(_loader_data, _cp);
5908   }
5909   if (_fields != NULL) {
5910     MetadataFactory::free_array<u2>(_loader_data, _fields);
5911   }
5912 
5913   if (_methods != NULL) {
5914     // Free methods
5915     InstanceKlass::deallocate_methods(_loader_data, _methods);
5916   }
5917 
5918   // beware of the Universe::empty_blah_array!!
5919   if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {
5920     MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
5921   }
5922 
5923   if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
5924     MetadataFactory::free_array<u2>(_loader_data, _nest_members);
5925   }
5926 








5927   // Free interfaces
5928   InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
5929                                        _local_interfaces, _transitive_interfaces);
5930 
5931   if (_combined_annotations != NULL) {
5932     // After all annotations arrays have been created, they are installed into the
5933     // Annotations object that will be assigned to the InstanceKlass being created.
5934 
5935     // Deallocate the Annotations object and the installed annotations arrays.
5936     _combined_annotations->deallocate_contents(_loader_data);
5937 
5938     // If the _combined_annotations pointer is non-NULL,
5939     // then the other annotations fields should have been cleared.
5940     assert(_annotations             == NULL, "Should have been cleared");
5941     assert(_type_annotations        == NULL, "Should have been cleared");
5942     assert(_fields_annotations      == NULL, "Should have been cleared");
5943     assert(_fields_type_annotations == NULL, "Should have been cleared");
5944   } else {
5945     // If the annotations arrays were not installed into the Annotations object,
5946     // then they have to be deallocated explicitly.


6239                                                                true,
6240                                                                CHECK);
6241   }
6242 
6243   if (_super_klass != NULL) {
6244     if (_super_klass->has_nonstatic_concrete_methods()) {
6245       _has_nonstatic_concrete_methods = true;
6246     }
6247 
6248     if (_super_klass->is_interface()) {
6249       ResourceMark rm(THREAD);
6250       Exceptions::fthrow(
6251         THREAD_AND_LOCATION,
6252         vmSymbols::java_lang_IncompatibleClassChangeError(),
6253         "class %s has interface %s as super class",
6254         _class_name->as_klass_external_name(),
6255         _super_klass->external_name()
6256       );
6257       return;
6258     }
6259     // Make sure super class is not final
6260     if (_super_klass->is_final()) {
6261       THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class");
6262     }
6263   }
6264 
6265   // Compute the transitive list of all unique interfaces implemented by this class
6266   _transitive_interfaces =
6267     compute_transitive_interfaces(_super_klass,
6268                                   _local_interfaces,
6269                                   _loader_data,
6270                                   CHECK);
6271 
6272   assert(_transitive_interfaces != NULL, "invariant");
6273 
6274   // sort methods
6275   _method_ordering = sort_methods(_methods);
6276 
6277   _all_mirandas = new GrowableArray<Method*>(20);
6278 
6279   Handle loader(THREAD, _loader_data->class_loader());




6280   klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6281                                                     &_num_miranda_methods,
6282                                                     _all_mirandas,
6283                                                     _super_klass,
6284                                                     _methods,
6285                                                     _access_flags,

6286                                                     _major_version,
6287                                                     loader,
6288                                                     _class_name,
6289                                                     _local_interfaces,
6290                                                     CHECK);
6291 
6292   // Size of Java itable (in words)
6293   _itable_size = _access_flags.is_interface() ? 0 :
6294     klassItable::compute_itable_size(_transitive_interfaces);
6295 
6296   assert(_fac != NULL, "invariant");
6297   assert(_parsed_annotations != NULL, "invariant");
6298 
6299   _field_info = new FieldLayoutInfo();
6300   layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK);
6301 
6302   // Compute reference typ
6303   _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6304 
























6305 }
6306 
6307 void ClassFileParser::set_klass(InstanceKlass* klass) {
6308 
6309 #ifdef ASSERT
6310   if (klass != NULL) {
6311     assert(NULL == _klass, "leaking?");
6312   }
6313 #endif
6314 
6315   _klass = klass;
6316 }
6317 
6318 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6319 
6320 #ifdef ASSERT
6321   if (klass != NULL) {
6322     assert(NULL == _klass_to_deallocate, "leaking?");
6323   }
6324 #endif




1732   assert(NULL == _fields, "invariant");
1733 
1734   _fields =
1735     MetadataFactory::new_array<u2>(_loader_data,
1736                                    index * FieldInfo::field_slots + num_generic_signature,
1737                                    CHECK);
1738   // Sometimes injected fields already exist in the Java source so
1739   // the fields array could be too long.  In that case the
1740   // fields array is trimed. Also unused slots that were reserved
1741   // for generic signature indexes are discarded.
1742   {
1743     int i = 0;
1744     for (; i < index * FieldInfo::field_slots; i++) {
1745       _fields->at_put(i, fa[i]);
1746     }
1747     for (int j = total_fields * FieldInfo::field_slots;
1748          j < generic_signature_slot; j++) {
1749       _fields->at_put(i++, fa[j]);
1750     }
1751     assert(_fields->length() == i, "");
1752     //tty->print_cr("length of the _fields array %d for class %s", i, _class_name->as_klass_external_name());
1753   }
1754 
1755   if (_need_verify && length > 1) {
1756     // Check duplicated fields
1757     ResourceMark rm(THREAD);
1758     NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
1759       THREAD, NameSigHash*, HASH_ROW_SIZE);
1760     initialize_hashtable(names_and_sigs);
1761     bool dup = false;
1762     const Symbol* name = NULL;
1763     const Symbol* sig = NULL;
1764     {
1765       debug_only(NoSafepointVerifier nsv;)
1766       for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
1767         name = fs.name();
1768         sig = fs.signature();
1769         // If no duplicates, add name/signature in hashtable names_and_sigs.
1770         if (!put_after_lookup(name, sig, names_and_sigs)) {
1771           dup = true;
1772           break;


3207   _nest_members = nest_members;
3208 
3209   int index = 0;
3210   cfs->guarantee_more(2 * length, CHECK_0);
3211   for (int n = 0; n < length; n++) {
3212     const u2 class_info_index = cfs->get_u2_fast();
3213     check_property(
3214       valid_klass_reference_at(class_info_index),
3215       "Nest member class_info_index %u has bad constant type in class file %s",
3216       class_info_index, CHECK_0);
3217     nest_members->at_put(index++, class_info_index);
3218   }
3219   assert(index == size, "wrong size");
3220 
3221   // Restore buffer's current position.
3222   cfs->set_current(current_mark);
3223 
3224   return length;
3225 }
3226 
3227 u2 ClassFileParser::parse_classfile_permitted_subtypes_attribute(const ClassFileStream* const cfs,
3228                                                            const u1* const permitted_subtypes_attribute_start,
3229                                                            TRAPS) {
3230   const u1* const current_mark = cfs->current();
3231   u2 length = 0;
3232   if (permitted_subtypes_attribute_start != NULL) {
3233     cfs->set_current(permitted_subtypes_attribute_start);
3234     cfs->guarantee_more(2, CHECK_0);  // length
3235     length = cfs->get_u2_fast();
3236   }
3237   const int size = length;
3238   Array<u2>* const permitted_subtypes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0);
3239   _permitted_subtypes = permitted_subtypes;
3240 
3241   int index = 0;
3242   cfs->guarantee_more(2 * length, CHECK_0);
3243   for (int n = 0; n < length; n++) {
3244     const u2 class_info_index = cfs->get_u2_fast();
3245     check_property(
3246       valid_klass_reference_at(class_info_index),
3247       "Permitted subtype class_info_index %u has bad constant type in class file %s",
3248       class_info_index, CHECK_0);
3249     permitted_subtypes->at_put(index++, class_info_index);
3250   }
3251   assert(index == size, "wrong size");
3252 
3253   // Restore buffer's current position.
3254   cfs->set_current(current_mark);
3255 
3256   return length;
3257 }
3258 
3259 void ClassFileParser::parse_classfile_record_attribute(const ClassFileStream* const cfs,
3260                                                            const u1* const record_attribute_start,
3261                                                            ConstantPool* cp,
3262                                                            int* const record_params_count_ptr,
3263                                                            TRAPS) {
3264   assert(NULL == _record_params, "invariant");
3265 
3266   const u1* const current_mark = cfs->current();
3267   int num_of_params = 0;
3268   if (record_attribute_start != NULL) {
3269     cfs->set_current(record_attribute_start);
3270     cfs->guarantee_more(2, CHECK);  // length
3271     num_of_params = (int)cfs->get_u2_fast();
3272     // DEBUG
3273     // tty->print_cr("this record has %d parameters", num_of_params);
3274   }
3275 
3276   *record_params_count_ptr = num_of_params;
3277 
3278   ResourceMark rm(THREAD);
3279   u2* const record_params_array = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
3280                                               u2,
3281                                               num_of_params * (RecordParamInfo::param_slots + 1));
3282   for (int n = 0; n < num_of_params; n++) {
3283     cfs->guarantee_more(RecordParamInfo::param_slots, CHECK);
3284 
3285     const u2 method_ref_index = cfs->get_u2_fast();
3286     //check_property(valid_symbol_at(method_ref_index),
3287     //  "Invalid constant pool index %u for record parameter name in class file %s",
3288     //  method_ref_index, CHECK);
3289     //const Symbol* const method_ref = cp->symbol_at(method_ref_index);
3290     // we will need a way to verify that the symbol is correct
3291     // verify_legal_field_name(name, CHECK);
3292     // DEBUG
3293     // tty->print_cr("name read %s", name->as_klass_external_name());
3294 
3295     RecordParamInfo* const record_param_info = RecordParamInfo::from_record_params_array(record_params_array, n);
3296     record_param_info->initialize(method_ref_index);
3297   }
3298 
3299   assert(NULL == _record_params, "invariant");
3300 
3301   _record_params = MetadataFactory::new_array<u2>(_loader_data,
3302                                      num_of_params * RecordParamInfo::param_slots,
3303                                      CHECK);
3304   {
3305     int i = 0;
3306     for (; i < num_of_params * RecordParamInfo::param_slots; i++) {
3307       _record_params->at_put(i, record_params_array[i]);
3308     }
3309     assert(_record_params->length() == i, "");
3310     // DEBUG
3311     // tty->print_cr("length of the _record_params array %d for class %s", i, _class_name->as_klass_external_name());
3312   }
3313 
3314   // Restore buffer's current position.
3315   cfs->set_current(current_mark);
3316 }
3317 
3318 void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) {
3319   set_class_synthetic_flag(true);
3320 }
3321 
3322 void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) {
3323   assert(cfs != NULL, "invariant");
3324 
3325   const u2 signature_index = cfs->get_u2(CHECK);
3326   check_property(
3327     valid_symbol_at(signature_index),
3328     "Invalid constant pool index %u in Signature attribute in class file %s",
3329     signature_index, CHECK);
3330   set_class_generic_signature_index(signature_index);
3331 }
3332 
3333 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
3334                                                                   ConstantPool* cp,
3335                                                                   u4 attribute_byte_length,
3336                                                                   TRAPS) {
3337   assert(cfs != NULL, "invariant");


3407       operands->at_put(operand_fill_index++, argument_index);
3408     }
3409   }
3410   guarantee_property(current_start + attribute_byte_length == cfs->current(),
3411                      "Bad length on BootstrapMethods in class file %s",
3412                      CHECK);
3413 }
3414 
3415 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs,
3416                                                  ConstantPool* cp,
3417                  ClassFileParser::ClassAnnotationCollector* parsed_annotations,
3418                                                  TRAPS) {
3419   assert(cfs != NULL, "invariant");
3420   assert(cp != NULL, "invariant");
3421   assert(parsed_annotations != NULL, "invariant");
3422 
3423   // Set inner classes attribute to default sentinel
3424   _inner_classes = Universe::the_empty_short_array();
3425   // Set nest members attribute to default sentinel
3426   _nest_members = Universe::the_empty_short_array();
3427   // Set _permitted_subtypes attribute to default sentinel
3428   _permitted_subtypes = Universe::the_empty_short_array();
3429   // Set record params to default sentinel
3430   _record_params = Universe::the_empty_short_array();
3431   cfs->guarantee_more(2, CHECK);  // attributes_count
3432   u2 attributes_count = cfs->get_u2_fast();
3433   bool parsed_sourcefile_attribute = false;
3434   bool parsed_innerclasses_attribute = false;
3435   bool parsed_nest_members_attribute = false;
3436   bool parsed_permitted_subtypes_attribute = false;
3437   bool parsed_nest_host_attribute = false;
3438   bool parsed_record_attribute = false;
3439   bool parsed_enclosingmethod_attribute = false;
3440   bool parsed_bootstrap_methods_attribute = false;
3441   const u1* runtime_visible_annotations = NULL;
3442   int runtime_visible_annotations_length = 0;
3443   const u1* runtime_invisible_annotations = NULL;
3444   int runtime_invisible_annotations_length = 0;
3445   const u1* runtime_visible_type_annotations = NULL;
3446   int runtime_visible_type_annotations_length = 0;
3447   const u1* runtime_invisible_type_annotations = NULL;
3448   int runtime_invisible_type_annotations_length = 0;
3449   bool runtime_invisible_type_annotations_exists = false;
3450   bool runtime_invisible_annotations_exists = false;
3451   bool parsed_source_debug_ext_annotations_exist = false;
3452   const u1* inner_classes_attribute_start = NULL;
3453   u4  inner_classes_attribute_length = 0;
3454   u2  enclosing_method_class_index = 0;
3455   u2  enclosing_method_method_index = 0;
3456   const u1* nest_members_attribute_start = NULL;
3457   u4  nest_members_attribute_length = 0;
3458   const u1* record_attribute_start = NULL;
3459   u4  record_attribute_length = 0;
3460   const u1* permitted_subtypes_attribute_start = NULL;
3461   u4  permitted_subtypes_attribute_length = 0;
3462 
3463   // Iterate over attributes
3464   while (attributes_count--) {
3465     cfs->guarantee_more(6, CHECK);  // attribute_name_index, attribute_length
3466     const u2 attribute_name_index = cfs->get_u2_fast();
3467     const u4 attribute_length = cfs->get_u4_fast();
3468     check_property(
3469       valid_symbol_at(attribute_name_index),
3470       "Attribute name has bad constant pool index %u in class file %s",
3471       attribute_name_index, CHECK);
3472     const Symbol* const tag = cp->symbol_at(attribute_name_index);
3473     if (tag == vmSymbols::tag_source_file()) {
3474       // Check for SourceFile tag
3475       if (_need_verify) {
3476         guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3477       }
3478       if (parsed_sourcefile_attribute) {
3479         classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
3480       } else {
3481         parsed_sourcefile_attribute = true;


3624           cfs->skip_u1(nest_members_attribute_length, CHECK);
3625         } else if (tag == vmSymbols::tag_nest_host()) {
3626           if (parsed_nest_host_attribute) {
3627             classfile_parse_error("Multiple NestHost attributes in class file %s", CHECK);
3628           } else {
3629             parsed_nest_host_attribute = true;
3630           }
3631           if (parsed_nest_members_attribute) {
3632             classfile_parse_error("Conflicting NestMembers and NestHost attributes in class file %s", CHECK);
3633           }
3634           if (_need_verify) {
3635             guarantee_property(attribute_length == 2, "Wrong NestHost attribute length in class file %s", CHECK);
3636           }
3637           cfs->guarantee_more(2, CHECK);
3638           u2 class_info_index = cfs->get_u2_fast();
3639           check_property(
3640                          valid_klass_reference_at(class_info_index),
3641                          "Nest-host class_info_index %u has bad constant type in class file %s",
3642                          class_info_index, CHECK);
3643           _nest_host = class_info_index;
3644         } else if (tag == vmSymbols::tag_permitted_subtypes()) {
3645             // Check for PermittedSubtypes tag
3646             if (!_access_flags.is_final()) {
3647                 classfile_parse_error("PermittedSubtypes attribute in non-final class file %s", CHECK);
3648             }
3649             if (parsed_permitted_subtypes_attribute) {
3650               classfile_parse_error("Multiple PermittedSubtypes attributes in class file %s", CHECK);
3651             } else {
3652               parsed_permitted_subtypes_attribute = true;
3653             }
3654             permitted_subtypes_attribute_start = cfs->current();
3655             permitted_subtypes_attribute_length = attribute_length;
3656             cfs->skip_u1(permitted_subtypes_attribute_length, CHECK);
3657         } else if (tag == vmSymbols::tag_record()) {
3658           if (parsed_record_attribute) {
3659             classfile_parse_error("Multiple Record attributes in class file %s", CHECK);
3660           } else {
3661             parsed_record_attribute = true;
3662           }
3663           record_attribute_start = cfs->current();
3664           record_attribute_length = attribute_length;
3665           cfs->skip_u1(record_attribute_length, CHECK);
3666         } else {
3667           // Unknown attribute
3668           cfs->skip_u1(attribute_length, CHECK);
3669         }
3670       } else {
3671         // Unknown attribute
3672         cfs->skip_u1(attribute_length, CHECK);
3673       }
3674     } else {
3675       // Unknown attribute
3676       cfs->skip_u1(attribute_length, CHECK);
3677     }
3678   }
3679   _annotations = assemble_annotations(runtime_visible_annotations,
3680                                       runtime_visible_annotations_length,
3681                                       runtime_invisible_annotations,
3682                                       runtime_invisible_annotations_length,
3683                                       CHECK);
3684   _type_annotations = assemble_annotations(runtime_visible_type_annotations,
3685                                            runtime_visible_type_annotations_length,


3697                             CHECK);
3698     if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) {
3699       guarantee_property(
3700         inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
3701         "Wrong InnerClasses attribute length in class file %s", CHECK);
3702     }
3703   }
3704 
3705   if (parsed_nest_members_attribute) {
3706     const u2 num_of_classes = parse_classfile_nest_members_attribute(
3707                             cfs,
3708                             nest_members_attribute_start,
3709                             CHECK);
3710     if (_need_verify) {
3711       guarantee_property(
3712         nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes,
3713         "Wrong NestMembers attribute length in class file %s", CHECK);
3714     }
3715   }
3716 
3717   if (parsed_record_attribute) {
3718     parse_classfile_record_attribute(
3719                             cfs,
3720                             record_attribute_start,
3721                             cp,
3722                             &_record_params_count,
3723                             CHECK);
3724   }
3725 
3726   if (parsed_permitted_subtypes_attribute) {
3727     const u2 num_of_subtypes = parse_classfile_permitted_subtypes_attribute(
3728                             cfs,
3729                             permitted_subtypes_attribute_start,
3730                             CHECK);
3731     if (_need_verify) {
3732       guarantee_property(
3733         permitted_subtypes_attribute_length == sizeof(num_of_subtypes) + sizeof(u2) * num_of_subtypes,
3734         "Wrong PermittedSubtypes attribute length in class file %s", CHECK);
3735     }
3736   }
3737 
3738   if (_max_bootstrap_specifier_index >= 0) {
3739     guarantee_property(parsed_bootstrap_methods_attribute,
3740                        "Missing BootstrapMethods attribute in class file %s", CHECK);
3741   }
3742 }
3743 
3744 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) {
3745   assert(k != NULL, "invariant");
3746 
3747   if (_synthetic_flag)
3748     k->set_is_synthetic();
3749   if (_sourcefile_index != 0) {
3750     k->set_source_file_name_index(_sourcefile_index);
3751   }
3752   if (_generic_signature_index != 0) {
3753     k->set_generic_signature_index(_generic_signature_index);
3754   }
3755   if (_sde_buffer != NULL) {
3756     k->set_source_debug_extension(_sde_buffer, _sde_length);
3757   }


3772     annotations->set_class_annotations(_annotations);
3773     annotations->set_class_type_annotations(_type_annotations);
3774     annotations->set_fields_annotations(_fields_annotations);
3775     annotations->set_fields_type_annotations(_fields_type_annotations);
3776 
3777     // This is the Annotations object that will be
3778     // assigned to InstanceKlass being constructed.
3779     _combined_annotations = annotations;
3780 
3781     // The annotations arrays below has been transfered the
3782     // _combined_annotations so these fields can now be cleared.
3783     _annotations             = NULL;
3784     _type_annotations        = NULL;
3785     _fields_annotations      = NULL;
3786     _fields_type_annotations = NULL;
3787 }
3788 
3789 // Transfer ownership of metadata allocated to the InstanceKlass.
3790 void ClassFileParser::apply_parsed_class_metadata(
3791                                             InstanceKlass* this_klass,
3792                                             int java_fields_count,
3793                                             int record_params_count, TRAPS) {
3794   assert(this_klass != NULL, "invariant");
3795 
3796   _cp->set_pool_holder(this_klass);
3797   this_klass->set_constants(_cp);
3798   this_klass->set_fields(_fields, java_fields_count);
3799   this_klass->set_methods(_methods);
3800   this_klass->set_inner_classes(_inner_classes);
3801   this_klass->set_nest_members(_nest_members);
3802   this_klass->set_nest_host_index(_nest_host);
3803   this_klass->set_local_interfaces(_local_interfaces);
3804   this_klass->set_annotations(_combined_annotations);
3805   this_klass->set_record_params(_record_params, record_params_count);
3806   this_klass->set_permitted_subtypes(_permitted_subtypes);
3807   // Delay the setting of _transitive_interfaces until after initialize_supers() in
3808   // fill_instance_klass(). It is because the _transitive_interfaces may be shared with
3809   // its _super. If an OOM occurs while loading the current klass, its _super field
3810   // may not have been set. When GC tries to free the klass, the _transitive_interfaces
3811   // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent
3812   // dereferences to the deallocated _transitive_interfaces will result in a crash.
3813 
3814   // Clear out these fields so they don't get deallocated by the destructor
3815   clear_class_metadata();
3816 }
3817 
3818 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations,
3819                                                        int runtime_visible_annotations_length,
3820                                                        const u1* const runtime_invisible_annotations,
3821                                                        int runtime_invisible_annotations_length,
3822                                                        TRAPS) {
3823   AnnotationArray* annotations = NULL;
3824   if (runtime_visible_annotations != NULL ||
3825       runtime_invisible_annotations != NULL) {
3826     annotations = MetadataFactory::new_array<u1>(_loader_data,


4858   const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4859   assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4860   if (is_module) {
4861     ResourceMark rm(THREAD);
4862     Exceptions::fthrow(
4863       THREAD_AND_LOCATION,
4864       vmSymbols::java_lang_NoClassDefFoundError(),
4865       "%s is not a class because access_flag ACC_MODULE is set",
4866       _class_name->as_C_string());
4867     return;
4868   }
4869 
4870   if (!_need_verify) { return; }
4871 
4872   const bool is_interface  = (flags & JVM_ACC_INTERFACE)  != 0;
4873   const bool is_abstract   = (flags & JVM_ACC_ABSTRACT)   != 0;
4874   const bool is_final      = (flags & JVM_ACC_FINAL)      != 0;
4875   const bool is_super      = (flags & JVM_ACC_SUPER)      != 0;
4876   const bool is_enum       = (flags & JVM_ACC_ENUM)       != 0;
4877   const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4878   const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION;
4879   const bool major_gte_12  = _major_version >= JAVA_12_VERSION;
4880 
4881   if ((is_abstract && is_final && !major_gte_12) ||
4882       (is_interface && !is_abstract && !major_gte_12) ||
4883       (is_interface && major_gte_1_5 && (is_super || is_enum)) ||
4884       (!is_interface && major_gte_1_5 && is_annotation)) {
4885     ResourceMark rm(THREAD);
4886     Exceptions::fthrow(
4887       THREAD_AND_LOCATION,
4888       vmSymbols::java_lang_ClassFormatError(),
4889       "Illegal class modifiers in class %s: 0x%X",
4890       _class_name->as_C_string(), flags
4891     );
4892     return;
4893   }
4894 }
4895 
4896 static bool has_illegal_visibility(jint flags) {
4897   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4898   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4899   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4900 
4901   return ((is_public && is_protected) ||
4902           (is_public && is_private) ||
4903           (is_protected && is_private));
4904 }


5628 
5629   assert(_field_info != NULL, "invariant");
5630   assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5631   assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count,
5632     "sanity");
5633 
5634   assert(ik->is_instance_klass(), "sanity");
5635   assert(ik->size_helper() == _field_info->instance_size, "sanity");
5636 
5637   // Fill in information already parsed
5638   ik->set_should_verify_class(_need_verify);
5639 
5640   // Not yet: supers are done below to support the new subtype-checking fields
5641   ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5642   ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5643   assert(_fac != NULL, "invariant");
5644   ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5645 
5646   // this transfers ownership of a lot of arrays from
5647   // the parser onto the InstanceKlass*
5648   apply_parsed_class_metadata(ik, _java_fields_count, _record_params_count, CHECK);
5649 
5650   // note that is not safe to use the fields in the parser from this point on
5651   assert(NULL == _cp, "invariant");
5652   assert(NULL == _fields, "invariant");
5653   assert(NULL == _methods, "invariant");
5654   assert(NULL == _inner_classes, "invariant");
5655   assert(NULL == _nest_members, "invariant");
5656   assert(NULL == _local_interfaces, "invariant");
5657   assert(NULL == _combined_annotations, "invariant");
5658   assert(NULL == _record_params, "invariant");
5659   assert(NULL == _permitted_subtypes, "invariant");
5660 
5661   if (_has_final_method) {
5662     ik->set_has_final_method();
5663   }
5664 
5665   ik->copy_method_ordering(_method_ordering, CHECK);
5666   // The InstanceKlass::_methods_jmethod_ids cache
5667   // is managed on the assumption that the initial cache
5668   // size is equal to the number of methods in the class. If
5669   // that changes, then InstanceKlass::idnum_can_increment()
5670   // has to be changed accordingly.
5671   ik->set_initial_method_idnum(ik->methods()->length());
5672 
5673   ik->set_this_class_index(_this_class_index);
5674 
5675   if (is_unsafe_anonymous()) {
5676     // _this_class_index is a CONSTANT_Class entry that refers to this
5677     // anonymous class itself. If this class needs to refer to its own methods or
5678     // fields, it would use a CONSTANT_MethodRef, etc, which would reference
5679     // _this_class_index. However, because this class is anonymous (it's


5819         const int length = local_interfaces->length();
5820         for (int i = 0; i < length; i++) {
5821           const InstanceKlass* const k = local_interfaces->at(i);
5822           const char * to = k->external_name();
5823           log_debug(class, resolve)("%s %s (interface)", from, to);
5824         }
5825       }
5826     }
5827   }
5828 
5829   JFR_ONLY(INIT_ID(ik);)
5830 
5831   // If we reach here, all is well.
5832   // Now remove the InstanceKlass* from the _klass_to_deallocate field
5833   // in order for it to not be destroyed in the ClassFileParser destructor.
5834   set_klass_to_deallocate(NULL);
5835 
5836   // it's official
5837   set_klass(ik);
5838 
5839   check_subtyping(CHECK);
5840 
5841   debug_only(ik->verify();)
5842 }
5843 
5844 void ClassFileParser::update_class_name(Symbol* new_class_name) {
5845   // Decrement the refcount in the old name, since we're clobbering it.
5846   _class_name->decrement_refcount();
5847 
5848   _class_name = new_class_name;
5849   // Increment the refcount of the new name.
5850   // Now the ClassFileParser owns this name and will decrement in
5851   // the destructor.
5852   _class_name->increment_refcount();
5853 }
5854 
5855 
5856 // For an unsafe anonymous class that is in the unnamed package, move it to its host class's
5857 // package by prepending its host class's package name to its class name and setting
5858 // its _class_name field.
5859 void ClassFileParser::prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS) {
5860   ResourceMark rm(THREAD);


5923                                  GrowableArray<Handle>* cp_patches,
5924                                  Publicity pub_level,
5925                                  TRAPS) :
5926   _stream(stream),
5927   _requested_name(name),
5928   _class_name(NULL),
5929   _loader_data(loader_data),
5930   _unsafe_anonymous_host(unsafe_anonymous_host),
5931   _cp_patches(cp_patches),
5932   _num_patched_klasses(0),
5933   _max_num_patched_klasses(0),
5934   _orig_cp_size(0),
5935   _first_patched_klass_resolved_index(0),
5936   _super_klass(),
5937   _cp(NULL),
5938   _fields(NULL),
5939   _methods(NULL),
5940   _inner_classes(NULL),
5941   _nest_members(NULL),
5942   _nest_host(0),
5943   _record_params(NULL),
5944   _permitted_subtypes(NULL),
5945   _local_interfaces(NULL),
5946   _transitive_interfaces(NULL),
5947   _combined_annotations(NULL),
5948   _annotations(NULL),
5949   _type_annotations(NULL),
5950   _fields_annotations(NULL),
5951   _fields_type_annotations(NULL),
5952   _klass(NULL),
5953   _klass_to_deallocate(NULL),
5954   _parsed_annotations(NULL),
5955   _fac(NULL),
5956   _field_info(NULL),
5957   _method_ordering(NULL),
5958   _all_mirandas(NULL),
5959   _vtable_size(0),
5960   _itable_size(0),
5961   _num_miranda_methods(0),
5962   _rt(REF_NONE),
5963   _protection_domain(protection_domain),
5964   _access_flags(),
5965   _pub_level(pub_level),
5966   _bad_constant_seen(0),
5967   _synthetic_flag(false),
5968   _sde_length(false),
5969   _sde_buffer(NULL),
5970   _sourcefile_index(0),
5971   _generic_signature_index(0),
5972   _major_version(0),
5973   _minor_version(0),
5974   _this_class_index(0),
5975   _super_class_index(0),
5976   _itfs_len(0),
5977   _java_fields_count(0),
5978   _record_params_count(-1),
5979   _need_verify(false),
5980   _relax_verify(false),
5981   _has_nonstatic_concrete_methods(false),
5982   _declares_nonstatic_concrete_methods(false),
5983   _has_final_method(false),
5984   _has_finalizer(false),
5985   _has_empty_finalizer(false),
5986   _has_vanilla_constructor(false),
5987   _max_bootstrap_specifier_index(-1) {
5988 
5989   _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
5990   _class_name->increment_refcount();
5991 
5992   assert(THREAD->is_Java_thread(), "invariant");
5993   assert(_loader_data != NULL, "invariant");
5994   assert(stream != NULL, "invariant");
5995   assert(_stream != NULL, "invariant");
5996   assert(_stream->buffer() == _stream->current(), "invariant");
5997   assert(_class_name != NULL, "invariant");
5998   assert(0 == _access_flags.as_int(), "invariant");


6032   // synch back verification state to stream
6033   stream->set_verify(_need_verify);
6034 
6035   // Check if verification needs to be relaxed for this class file
6036   // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
6037   _relax_verify = relax_format_check_for(_loader_data);
6038 
6039   parse_stream(stream, CHECK);
6040 
6041   post_process_parsed_stream(stream, _cp, CHECK);
6042 }
6043 
6044 void ClassFileParser::clear_class_metadata() {
6045   // metadata created before the instance klass is created.  Must be
6046   // deallocated if classfile parsing returns an error.
6047   _cp = NULL;
6048   _fields = NULL;
6049   _methods = NULL;
6050   _inner_classes = NULL;
6051   _nest_members = NULL;
6052   _permitted_subtypes = NULL;
6053   _local_interfaces = NULL;
6054   _combined_annotations = NULL;
6055   _annotations = _type_annotations = NULL;
6056   _fields_annotations = _fields_type_annotations = NULL;
6057   _record_params = NULL;
6058 }
6059 
6060 // Destructor to clean up
6061 ClassFileParser::~ClassFileParser() {
6062   _class_name->decrement_refcount();
6063 
6064   if (_cp != NULL) {
6065     MetadataFactory::free_metadata(_loader_data, _cp);
6066   }
6067   if (_fields != NULL) {
6068     MetadataFactory::free_array<u2>(_loader_data, _fields);
6069   }
6070 
6071   if (_methods != NULL) {
6072     // Free methods
6073     InstanceKlass::deallocate_methods(_loader_data, _methods);
6074   }
6075 
6076   // beware of the Universe::empty_blah_array!!
6077   if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) {
6078     MetadataFactory::free_array<u2>(_loader_data, _inner_classes);
6079   }
6080 
6081   if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
6082     MetadataFactory::free_array<u2>(_loader_data, _nest_members);
6083   }
6084 
6085   if (_record_params != NULL && _record_params != Universe::the_empty_short_array()) {
6086     MetadataFactory::free_array<u2>(_loader_data, _record_params);
6087   }
6088   
6089   if (_permitted_subtypes != NULL && _permitted_subtypes != Universe::the_empty_short_array()) {
6090     MetadataFactory::free_array<u2>(_loader_data, _permitted_subtypes);
6091   }
6092 
6093   // Free interfaces
6094   InstanceKlass::deallocate_interfaces(_loader_data, _super_klass,
6095                                        _local_interfaces, _transitive_interfaces);
6096 
6097   if (_combined_annotations != NULL) {
6098     // After all annotations arrays have been created, they are installed into the
6099     // Annotations object that will be assigned to the InstanceKlass being created.
6100 
6101     // Deallocate the Annotations object and the installed annotations arrays.
6102     _combined_annotations->deallocate_contents(_loader_data);
6103 
6104     // If the _combined_annotations pointer is non-NULL,
6105     // then the other annotations fields should have been cleared.
6106     assert(_annotations             == NULL, "Should have been cleared");
6107     assert(_type_annotations        == NULL, "Should have been cleared");
6108     assert(_fields_annotations      == NULL, "Should have been cleared");
6109     assert(_fields_type_annotations == NULL, "Should have been cleared");
6110   } else {
6111     // If the annotations arrays were not installed into the Annotations object,
6112     // then they have to be deallocated explicitly.


6405                                                                true,
6406                                                                CHECK);
6407   }
6408 
6409   if (_super_klass != NULL) {
6410     if (_super_klass->has_nonstatic_concrete_methods()) {
6411       _has_nonstatic_concrete_methods = true;
6412     }
6413 
6414     if (_super_klass->is_interface()) {
6415       ResourceMark rm(THREAD);
6416       Exceptions::fthrow(
6417         THREAD_AND_LOCATION,
6418         vmSymbols::java_lang_IncompatibleClassChangeError(),
6419         "class %s has interface %s as super class",
6420         _class_name->as_klass_external_name(),
6421         _super_klass->external_name()
6422       );
6423       return;
6424     }




6425   }
6426 
6427   // Compute the transitive list of all unique interfaces implemented by this class
6428   _transitive_interfaces =
6429     compute_transitive_interfaces(_super_klass,
6430                                   _local_interfaces,
6431                                   _loader_data,
6432                                   CHECK);
6433 
6434   assert(_transitive_interfaces != NULL, "invariant");
6435 
6436   // sort methods
6437   _method_ordering = sort_methods(_methods);
6438 
6439   _all_mirandas = new GrowableArray<Method*>(20);
6440 
6441   Handle loader(THREAD, _loader_data->class_loader());
6442   bool is_sealed = _access_flags.is_final() &&
6443                          _permitted_subtypes != NULL &&
6444                          _permitted_subtypes != Universe::the_empty_short_array() &&
6445                          _permitted_subtypes->length() > 0;
6446   klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6447                                                     &_num_miranda_methods,
6448                                                     _all_mirandas,
6449                                                     _super_klass,
6450                                                     _methods,
6451                                                     _access_flags,
6452                                                     is_sealed,
6453                                                     _major_version,
6454                                                     loader,
6455                                                     _class_name,
6456                                                     _local_interfaces,
6457                                                     CHECK);
6458 
6459   // Size of Java itable (in words)
6460   _itable_size = _access_flags.is_interface() ? 0 :
6461     klassItable::compute_itable_size(_transitive_interfaces);
6462 
6463   assert(_fac != NULL, "invariant");
6464   assert(_parsed_annotations != NULL, "invariant");
6465 
6466   _field_info = new FieldLayoutInfo();
6467   layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK);
6468 
6469   // Compute reference typ
6470   _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6471 
6472 }
6473 
6474 void ClassFileParser::check_subtyping(TRAPS) {
6475   assert(NULL != _klass, "_klass should have been resolved before calling this method");
6476   if (_super_klass != NULL) {
6477     if (_super_klass->is_final()) {
6478       bool isPermittedSubtype = _super_klass->has_as_permitted_subtype(_klass, CHECK);
6479       if (!isPermittedSubtype) {
6480         THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class");
6481       }
6482     }
6483   }
6484   Array<InstanceKlass*>* local_interfaces = _klass->local_interfaces();
6485   if (local_interfaces != NULL && local_interfaces != Universe::the_empty_instance_klass_array()) {
6486     for (int i = 0; i < local_interfaces->length(); i++) {
6487       InstanceKlass* intf = local_interfaces->at(i);
6488       if (intf->is_final()) {
6489         bool isPermittedSubtype = intf->has_as_permitted_subtype(_klass, CHECK);
6490         if (!isPermittedSubtype) {
6491           THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final interface");
6492         }
6493       }
6494     }
6495   }
6496 }
6497 
6498 void ClassFileParser::set_klass(InstanceKlass* klass) {
6499 
6500 #ifdef ASSERT
6501   if (klass != NULL) {
6502     assert(NULL == _klass, "leaking?");
6503   }
6504 #endif
6505 
6506   _klass = klass;
6507 }
6508 
6509 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6510 
6511 #ifdef ASSERT
6512   if (klass != NULL) {
6513     assert(NULL == _klass_to_deallocate, "leaking?");
6514   }
6515 #endif


< prev index next >