< prev index next >

src/hotspot/share/classfile/classFileParser.cpp

Print this page




1072     _method_DontInline,
1073     _method_InjectedProfile,
1074     _method_LambdaForm_Compiled,
1075     _method_Hidden,
1076     _method_HotSpotIntrinsicCandidate,
1077     _jdk_internal_vm_annotation_Contended,
1078     _field_Stable,
1079     _jdk_internal_vm_annotation_ReservedStackAccess,
1080     _annotation_LIMIT
1081   };
1082   const Location _location;
1083   int _annotations_present;
1084   u2 _contended_group;
1085 
1086   AnnotationCollector(Location location)
1087     : _location(location), _annotations_present(0)
1088   {
1089     assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
1090   }
1091   // If this annotation name has an ID, report it (or _none).
1092   ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name);
1093   // Set the annotation name:
1094   void set_annotation(ID id) {
1095     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1096     _annotations_present |= nth_bit((int)id);
1097   }
1098 
1099   void remove_annotation(ID id) {
1100     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1101     _annotations_present &= ~nth_bit((int)id);
1102   }
1103 
1104   // Report if the annotation is present.
1105   bool has_any_annotations() const { return _annotations_present != 0; }
1106   bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; }
1107 
1108   void set_contended_group(u2 group) { _contended_group = group; }
1109   u2 contended_group() const { return _contended_group; }
1110 
1111   bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); }
1112 


1205       int nval = Bytes::get_Java_u2((address)buffer + index - 2);
1206       while (--nval >= 0 && index < limit) {
1207         index = skip_annotation_value(buffer, limit, index);
1208       }
1209     }
1210     break;
1211     case '@':
1212       index = skip_annotation(buffer, limit, index);
1213       break;
1214     default:
1215       return limit;  //  bad tag byte
1216   }
1217   return index;
1218 }
1219 
1220 // Sift through annotations, looking for those significant to the VM:
1221 static void parse_annotations(const ConstantPool* const cp,
1222                               const u1* buffer, int limit,
1223                               AnnotationCollector* coll,
1224                               ClassLoaderData* loader_data,

1225                               TRAPS) {
1226 
1227   assert(cp != NULL, "invariant");
1228   assert(buffer != NULL, "invariant");
1229   assert(coll != NULL, "invariant");
1230   assert(loader_data != NULL, "invariant");
1231 
1232   // annotations := do(nann:u2) {annotation}
1233   int index = 2; // read nann
1234   if (index >= limit)  return;
1235   int nann = Bytes::get_Java_u2((address)buffer + index - 2);
1236   enum {  // initial annotation layout
1237     atype_off = 0,      // utf8 such as 'Ljava/lang/annotation/Retention;'
1238     count_off = 2,      // u2   such as 1 (one value)
1239     member_off = 4,     // utf8 such as 'value'
1240     tag_off = 6,        // u1   such as 'c' (type) or 'e' (enum)
1241     e_tag_val = 'e',
1242     e_type_off = 7,   // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;'
1243     e_con_off = 9,    // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME'
1244     e_size = 11,     // end of 'e' annotation


1250     s_size = 9,
1251     min_size = 6        // smallest possible size (zero members)
1252   };
1253   // Cannot add min_size to index in case of overflow MAX_INT
1254   while ((--nann) >= 0 && (index - 2 <= limit - min_size)) {
1255     int index0 = index;
1256     index = skip_annotation(buffer, limit, index);
1257     const u1* const abase = buffer + index0;
1258     const int atype = Bytes::get_Java_u2((address)abase + atype_off);
1259     const int count = Bytes::get_Java_u2((address)abase + count_off);
1260     const Symbol* const aname = check_symbol_at(cp, atype);
1261     if (aname == NULL)  break;  // invalid annotation name
1262     const Symbol* member = NULL;
1263     if (count >= 1) {
1264       const int member_index = Bytes::get_Java_u2((address)abase + member_off);
1265       member = check_symbol_at(cp, member_index);
1266       if (member == NULL)  break;  // invalid member name
1267     }
1268 
1269     // Here is where parsing particular annotations will take place.
1270     AnnotationCollector::ID id = coll->annotation_index(loader_data, aname);
1271     if (AnnotationCollector::_unknown == id)  continue;
1272     coll->set_annotation(id);
1273 
1274     if (AnnotationCollector::_jdk_internal_vm_annotation_Contended == id) {
1275       // @Contended can optionally specify the contention group.
1276       //
1277       // Contended group defines the equivalence class over the fields:
1278       // the fields within the same contended group are not treated distinct.
1279       // The only exception is default group, which does not incur the
1280       // equivalence. Naturally, contention group for classes is meaningless.
1281       //
1282       // While the contention group is specified as String, annotation
1283       // values are already interned, and we might as well use the constant
1284       // pool index as the group tag.
1285       //
1286       u2 group_index = 0; // default contended group
1287       if (count == 1
1288         && s_size == (index - index0)  // match size
1289         && s_tag_val == *(abase + tag_off)
1290         && member == vmSymbols::value_name()) {


1376         if (attribute_length != 2) {
1377           classfile_parse_error(
1378             "Wrong size %u for field's Signature attribute in class file %s",
1379             attribute_length, CHECK);
1380         }
1381         generic_signature_index = parse_generic_signature_attribute(cfs, CHECK);
1382       } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
1383         if (runtime_visible_annotations != NULL) {
1384           classfile_parse_error(
1385             "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", CHECK);
1386         }
1387         runtime_visible_annotations_length = attribute_length;
1388         runtime_visible_annotations = cfs->current();
1389         assert(runtime_visible_annotations != NULL, "null visible annotations");
1390         cfs->guarantee_more(runtime_visible_annotations_length, CHECK);
1391         parse_annotations(cp,
1392                           runtime_visible_annotations,
1393                           runtime_visible_annotations_length,
1394                           parsed_annotations,
1395                           _loader_data,

1396                           CHECK);
1397         cfs->skip_u1_fast(runtime_visible_annotations_length);
1398       } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
1399         if (runtime_invisible_annotations_exists) {
1400           classfile_parse_error(
1401             "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK);
1402         }
1403         runtime_invisible_annotations_exists = true;
1404         if (PreserveAllAnnotations) {
1405           runtime_invisible_annotations_length = attribute_length;
1406           runtime_invisible_annotations = cfs->current();
1407           assert(runtime_invisible_annotations != NULL, "null invisible annotations");
1408         }
1409         cfs->skip_u1(attribute_length, CHECK);
1410       } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {
1411         if (runtime_visible_type_annotations != NULL) {
1412           classfile_parse_error(
1413             "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK);
1414         }
1415         runtime_visible_type_annotations_length = attribute_length;


2035   }
2036   return checked_exceptions_start;
2037 }
2038 
2039 void ClassFileParser::throwIllegalSignature(const char* type,
2040                                             const Symbol* name,
2041                                             const Symbol* sig,
2042                                             TRAPS) const {
2043   assert(name != NULL, "invariant");
2044   assert(sig != NULL, "invariant");
2045 
2046   ResourceMark rm(THREAD);
2047   Exceptions::fthrow(THREAD_AND_LOCATION,
2048       vmSymbols::java_lang_ClassFormatError(),
2049       "%s \"%s\" in class %s has illegal signature \"%s\"", type,
2050       name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
2051 }
2052 
2053 AnnotationCollector::ID
2054 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
2055                                       const Symbol* name) {

2056   const vmSymbols::SID sid = vmSymbols::find_sid(name);
2057   // Privileged code can use all annotations.  Other code silently drops some.
2058   const bool privileged = loader_data->is_the_null_class_loader_data() ||
2059                           loader_data->is_platform_class_loader_data() ||
2060                           loader_data->is_unsafe_anonymous();

2061   switch (sid) {
2062     case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
2063       if (_location != _in_method)  break;  // only allow for methods
2064       if (!privileged)              break;  // only allow in privileged code
2065       return _method_CallerSensitive;
2066     }
2067     case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
2068       if (_location != _in_method)  break;  // only allow for methods
2069       if (!privileged)              break;  // only allow in privileged code
2070       return _method_ForceInline;
2071     }
2072     case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): {
2073       if (_location != _in_method)  break;  // only allow for methods
2074       if (!privileged)              break;  // only allow in privileged code
2075       return _method_DontInline;
2076     }
2077     case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): {
2078       if (_location != _in_method)  break;  // only allow for methods
2079       if (!privileged)              break;  // only allow in privileged code
2080       return _method_InjectedProfile;


2659           classfile_parse_error(
2660             "Invalid Signature attribute length %u in class file %s",
2661             method_attribute_length, CHECK_NULL);
2662         }
2663         generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL);
2664       } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
2665         if (runtime_visible_annotations != NULL) {
2666           classfile_parse_error(
2667             "Multiple RuntimeVisibleAnnotations attributes for method in class file %s",
2668             CHECK_NULL);
2669         }
2670         runtime_visible_annotations_length = method_attribute_length;
2671         runtime_visible_annotations = cfs->current();
2672         assert(runtime_visible_annotations != NULL, "null visible annotations");
2673         cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL);
2674         parse_annotations(cp,
2675                           runtime_visible_annotations,
2676                           runtime_visible_annotations_length,
2677                           &parsed_annotations,
2678                           _loader_data,

2679                           CHECK_NULL);
2680         cfs->skip_u1_fast(runtime_visible_annotations_length);
2681       } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
2682         if (runtime_invisible_annotations_exists) {
2683           classfile_parse_error(
2684             "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s",
2685             CHECK_NULL);
2686         }
2687         runtime_invisible_annotations_exists = true;
2688         if (PreserveAllAnnotations) {
2689           runtime_invisible_annotations_length = method_attribute_length;
2690           runtime_invisible_annotations = cfs->current();
2691           assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2692         }
2693         cfs->skip_u1(method_attribute_length, CHECK_NULL);
2694       } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) {
2695         if (runtime_visible_parameter_annotations != NULL) {
2696           classfile_parse_error(
2697             "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s",
2698             CHECK_NULL);


3421         if (attribute_length != 2) {
3422           classfile_parse_error(
3423             "Wrong Signature attribute length %u in class file %s",
3424             attribute_length, CHECK);
3425         }
3426         parse_classfile_signature_attribute(cfs, CHECK);
3427       } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
3428         if (runtime_visible_annotations != NULL) {
3429           classfile_parse_error(
3430             "Multiple RuntimeVisibleAnnotations attributes in class file %s", CHECK);
3431         }
3432         runtime_visible_annotations_length = attribute_length;
3433         runtime_visible_annotations = cfs->current();
3434         assert(runtime_visible_annotations != NULL, "null visible annotations");
3435         cfs->guarantee_more(runtime_visible_annotations_length, CHECK);
3436         parse_annotations(cp,
3437                           runtime_visible_annotations,
3438                           runtime_visible_annotations_length,
3439                           parsed_annotations,
3440                           _loader_data,

3441                           CHECK);
3442         cfs->skip_u1_fast(runtime_visible_annotations_length);
3443       } else if (tag == vmSymbols::tag_runtime_invisible_annotations()) {
3444         if (runtime_invisible_annotations_exists) {
3445           classfile_parse_error(
3446             "Multiple RuntimeInvisibleAnnotations attributes in class file %s", CHECK);
3447         }
3448         runtime_invisible_annotations_exists = true;
3449         if (PreserveAllAnnotations) {
3450           runtime_invisible_annotations_length = attribute_length;
3451           runtime_invisible_annotations = cfs->current();
3452           assert(runtime_invisible_annotations != NULL, "null invisible annotations");
3453         }
3454         cfs->skip_u1(attribute_length, CHECK);
3455       } else if (tag == vmSymbols::tag_enclosing_method()) {
3456         if (parsed_enclosingmethod_attribute) {
3457           classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
3458         } else {
3459           parsed_enclosingmethod_attribute = true;
3460         }


5407 
5408           if (!match) {
5409             char buf[1000];
5410             tty->print("Compiler intrinsic is defined for method [%s], "
5411                        "but the method is not available in class [%s].%s",
5412                         vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID_from(id),
5413                                                              buf, sizeof(buf)),
5414                         ik->name()->as_C_string(),
5415                         NOT_DEBUG("") DEBUG_ONLY(" Exiting.")
5416             );
5417             tty->cr();
5418             DEBUG_ONLY(vm_exit(1));
5419           }
5420         }
5421       } // end for
5422     } // CheckIntrinsics
5423 #endif // ASSERT
5424   }
5425 }
5426 
5427 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook, TRAPS) {



5428   if (_klass != NULL) {
5429     return _klass;
5430   }
5431 
5432   InstanceKlass* const ik =
5433     InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5434 
5435   fill_instance_klass(ik, changed_by_loadhook, CHECK_NULL);
5436 
5437   assert(_klass == ik, "invariant");
5438 
5439 
5440   if (ik->should_store_fingerprint()) {
5441     ik->store_fingerprint(_stream->compute_fingerprint());
5442   }
5443 
5444   ik->set_has_passed_fingerprint_check(false);
5445   if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
5446     uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
5447     uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint();
5448     if (aot_fp != 0 && aot_fp == fp) {
5449       // This class matches with a class saved in an AOT library
5450       ik->set_has_passed_fingerprint_check(true);
5451     } else {
5452       ResourceMark rm;
5453       log_info(class, fingerprint)("%s :  expected = " PTR64_FORMAT " actual = " PTR64_FORMAT,
5454                                  ik->external_name(), aot_fp, _stream->compute_fingerprint());
5455     }
5456   }
5457 
5458   return ik;
5459 }
5460 
5461 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) {




5462   assert(ik != NULL, "invariant");
5463 
5464   // Set name and CLD before adding to CLD
5465   ik->set_class_loader_data(_loader_data);
5466   ik->set_name(_class_name);
5467 
5468   // Add all classes to our internal class loader list here,
5469   // including classes in the bootstrap (NULL) class loader.
5470   const bool publicize = !is_internal();
5471 
5472   _loader_data->add_class(ik, publicize);
5473 
5474   set_klass_to_deallocate(ik);
5475 
5476   assert(_field_info != NULL, "invariant");
5477   assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5478   assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count,
5479     "sanity");
5480 
5481   assert(ik->is_instance_klass(), "sanity");
5482   assert(ik->size_helper() == _field_info->instance_size, "sanity");
5483 
5484   // Fill in information already parsed
5485   ik->set_should_verify_class(_need_verify);
5486 
5487   // Not yet: supers are done below to support the new subtype-checking fields
5488   ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5489   ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5490   assert(_fac != NULL, "invariant");
5491   ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5492 
5493   // this transfers ownership of a lot of arrays from
5494   // the parser onto the InstanceKlass*
5495   apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5496 





5497   // note that is not safe to use the fields in the parser from this point on
5498   assert(NULL == _cp, "invariant");
5499   assert(NULL == _fields, "invariant");
5500   assert(NULL == _methods, "invariant");
5501   assert(NULL == _inner_classes, "invariant");
5502   assert(NULL == _nest_members, "invariant");
5503   assert(NULL == _local_interfaces, "invariant");
5504   assert(NULL == _combined_annotations, "invariant");
5505 
5506   if (_has_final_method) {
5507     ik->set_has_final_method();
5508   }
5509 
5510   ik->copy_method_ordering(_method_ordering, CHECK);
5511   // The InstanceKlass::_methods_jmethod_ids cache
5512   // is managed on the assumption that the initial cache
5513   // size is equal to the number of methods in the class. If
5514   // that changes, then InstanceKlass::idnum_can_increment()
5515   // has to be changed accordingly.
5516   ik->set_initial_method_idnum(ik->methods()->length());
5517 
5518   ik->set_this_class_index(_this_class_index);
5519 
5520   if (is_unsafe_anonymous()) {
5521     // _this_class_index is a CONSTANT_Class entry that refers to this
5522     // anonymous class itself. If this class needs to refer to its own methods or
5523     // fields, it would use a CONSTANT_MethodRef, etc, which would reference
5524     // _this_class_index. However, because this class is anonymous (it's
5525     // not stored in SystemDictionary), _this_class_index cannot be resolved
5526     // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5527     // Therefore, we must eagerly resolve _this_class_index now.
5528     ik->constants()->klass_at_put(_this_class_index, ik);
5529   }
5530 
5531   ik->set_minor_version(_minor_version);
5532   ik->set_major_version(_major_version);
5533   ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5534   ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5535 
5536   if (_unsafe_anonymous_host != NULL) {
5537     assert (ik->is_unsafe_anonymous(), "should be the same");
5538     ik->set_unsafe_anonymous_host(_unsafe_anonymous_host);
5539   }
5540 
5541   // Set PackageEntry for this_klass
5542   oop cl = ik->class_loader();
5543   Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5544   ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5545   ik->set_package(cld, CHECK);




5546 
5547   const Array<Method*>* const methods = ik->methods();
5548   assert(methods != NULL, "invariant");
5549   const int methods_len = methods->length();
5550 
5551   check_methods_for_intrinsics(ik, methods);
5552 
5553   // Fill in field values obtained by parse_classfile_attributes
5554   if (_parsed_annotations->has_any_annotations()) {
5555     _parsed_annotations->apply_to(ik);
5556   }
5557 
5558   apply_parsed_class_attributes(ik);
5559 
5560   // Miranda methods
5561   if ((_num_miranda_methods > 0) ||
5562       // if this class introduced new miranda methods or
5563       (_super_klass != NULL && _super_klass->has_miranda_methods())
5564         // super class exists and this class inherited miranda methods
5565      ) {


5581                 _field_info->nonstatic_oop_offsets,
5582                 _field_info->nonstatic_oop_counts);
5583 
5584   // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
5585   set_precomputed_flags(ik);
5586 
5587   // check if this class can access its super class
5588   check_super_class_access(ik, CHECK);
5589 
5590   // check if this class can access its superinterfaces
5591   check_super_interface_access(ik, CHECK);
5592 
5593   // check if this class overrides any final method
5594   check_final_method_override(ik, CHECK);
5595 
5596   // reject static interface methods prior to Java 8
5597   if (ik->is_interface() && _major_version < JAVA_8_VERSION) {
5598     check_illegal_static_method(ik, CHECK);
5599   }
5600 
5601   // Obtain this_klass' module entry
5602   ModuleEntry* module_entry = ik->module();
5603   assert(module_entry != NULL, "module_entry should always be set");
5604 
5605   // Obtain java.lang.Module
5606   Handle module_handle(THREAD, module_entry->module());
5607 
5608   // Allocate mirror and initialize static fields
5609   // The create_mirror() call will also call compute_modifiers()
5610   java_lang_Class::create_mirror(ik,
5611                                  Handle(THREAD, _loader_data->class_loader()),
5612                                  module_handle,
5613                                  _protection_domain,

5614                                  CHECK);
5615 
5616   assert(_all_mirandas != NULL, "invariant");
5617 
5618   // Generate any default methods - default methods are public interface methods
5619   // that have a default implementation.  This is new with Java 8.
5620   if (_has_nonstatic_concrete_methods) {
5621     DefaultMethods::generate_default_methods(ik,
5622                                              _all_mirandas,
5623                                              CHECK);
5624   }
5625 
5626   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5627   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5628       !module_entry->has_default_read_edges()) {
5629     if (!module_entry->set_has_default_read_edges()) {
5630       // We won a potential race
5631       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5632     }
5633   }


5667           const char * to = k->external_name();
5668           log_debug(class, resolve)("%s %s (interface)", from, to);
5669         }
5670       }
5671     }
5672   }
5673 
5674   JFR_ONLY(INIT_ID(ik);)
5675 
5676   // If we reach here, all is well.
5677   // Now remove the InstanceKlass* from the _klass_to_deallocate field
5678   // in order for it to not be destroyed in the ClassFileParser destructor.
5679   set_klass_to_deallocate(NULL);
5680 
5681   // it's official
5682   set_klass(ik);
5683 
5684   debug_only(ik->verify();)
5685 }
5686 
5687 void ClassFileParser::update_class_name(Symbol* new_class_name) {
5688   // Decrement the refcount in the old name, since we're clobbering it.
5689   _class_name->decrement_refcount();
5690 
5691   _class_name = new_class_name;
5692   // Increment the refcount of the new name.
5693   // Now the ClassFileParser owns this name and will decrement in
5694   // the destructor.
5695   _class_name->increment_refcount();
5696 }
5697 
5698 
5699 // For an unsafe anonymous class that is in the unnamed package, move it to its host class's
5700 // package by prepending its host class's package name to its class name and setting
5701 // its _class_name field.
5702 void ClassFileParser::prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS) {
5703   ResourceMark rm(THREAD);
5704   assert(strrchr(_class_name->as_C_string(), '/') == NULL,
5705          "Unsafe anonymous class should not be in a package");
5706   const char* host_pkg_name =
5707     ClassLoader::package_from_name(unsafe_anonymous_host->name()->as_C_string(), NULL);
5708 
5709   if (host_pkg_name != NULL) {
5710     int host_pkg_len = (int)strlen(host_pkg_name);
5711     int class_name_len = _class_name->utf8_length();
5712     int symbol_len = host_pkg_len + 1 + class_name_len;
5713     char* new_anon_name = NEW_RESOURCE_ARRAY(char, symbol_len + 1);
5714     int n = os::snprintf(new_anon_name, symbol_len + 1, "%s/%.*s",
5715                          host_pkg_name, class_name_len, _class_name->base());
5716     assert(n == symbol_len, "Unexpected number of characters in string");
5717 
5718     // Decrement old _class_name to avoid leaking.


5747   }
5748 }
5749 
5750 static bool relax_format_check_for(ClassLoaderData* loader_data) {
5751   bool trusted = (loader_data->is_the_null_class_loader_data() ||
5752                   SystemDictionary::is_platform_class_loader(loader_data->class_loader()));
5753   bool need_verify =
5754     // verifyAll
5755     (BytecodeVerificationLocal && BytecodeVerificationRemote) ||
5756     // verifyRemote
5757     (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);
5758   return !need_verify;
5759 }
5760 
5761 ClassFileParser::ClassFileParser(ClassFileStream* stream,
5762                                  Symbol* name,
5763                                  ClassLoaderData* loader_data,
5764                                  Handle protection_domain,
5765                                  const InstanceKlass* unsafe_anonymous_host,
5766                                  GrowableArray<Handle>* cp_patches,


5767                                  Publicity pub_level,
5768                                  TRAPS) :
5769   _stream(stream),
5770   _requested_name(name),
5771   _class_name(NULL),
5772   _loader_data(loader_data),
5773   _unsafe_anonymous_host(unsafe_anonymous_host),
5774   _cp_patches(cp_patches),


5775   _num_patched_klasses(0),
5776   _max_num_patched_klasses(0),
5777   _orig_cp_size(0),
5778   _first_patched_klass_resolved_index(0),
5779   _super_klass(),
5780   _cp(NULL),
5781   _fields(NULL),
5782   _methods(NULL),
5783   _inner_classes(NULL),
5784   _nest_members(NULL),
5785   _nest_host(0),
5786   _local_interfaces(NULL),
5787   _transitive_interfaces(NULL),
5788   _combined_annotations(NULL),
5789   _annotations(NULL),
5790   _type_annotations(NULL),
5791   _fields_annotations(NULL),
5792   _fields_type_annotations(NULL),
5793   _klass(NULL),
5794   _klass_to_deallocate(NULL),


5981             _major_version,  _minor_version, _class_name->as_C_string());
5982     Exceptions::fthrow(
5983       THREAD_AND_LOCATION,
5984       vmSymbols::java_lang_UnsupportedClassVersionError(),
5985       "Unsupported major.minor version for dump time %u.%u",
5986       _major_version,
5987       _minor_version);
5988   }
5989 
5990   // Check version numbers - we check this even with verifier off
5991   verify_class_version(_major_version, _minor_version, _class_name, CHECK);
5992 
5993   stream->guarantee_more(3, CHECK); // length, first cp tag
5994   u2 cp_size = stream->get_u2_fast();
5995 
5996   guarantee_property(
5997     cp_size >= 1, "Illegal constant pool size %u in class file %s",
5998     cp_size, CHECK);
5999 
6000   _orig_cp_size = cp_size;
6001   if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
6002     THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");






6003   }
6004   cp_size += _max_num_patched_klasses;
6005 
6006   _cp = ConstantPool::allocate(_loader_data,
6007                                cp_size,
6008                                CHECK);
6009 
6010   ConstantPool* const cp = _cp;
6011 
6012   parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6013 
6014   assert(cp_size == (const u2)cp->length(), "invariant");
6015 
6016   // ACCESS FLAGS
6017   stream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len
6018 
6019   // Access flags
6020   jint flags;
6021   // JVM_ACC_MODULE is defined in JDK-9 and later.
6022   if (_major_version >= JAVA_9_VERSION) {
6023     flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
6024   } else {


6035   short bad_constant = class_bad_constant_seen();
6036   if (bad_constant != 0) {
6037     // Do not throw CFE until after the access_flags are checked because if
6038     // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6039     classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK);
6040   }
6041 
6042   _access_flags.set_flags(flags);
6043 
6044   // This class and superclass
6045   _this_class_index = stream->get_u2_fast();
6046   check_property(
6047     valid_cp_range(_this_class_index, cp_size) &&
6048       cp->tag_at(_this_class_index).is_unresolved_klass(),
6049     "Invalid this class index %u in constant pool in class file %s",
6050     _this_class_index, CHECK);
6051 
6052   Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
6053   assert(class_name_in_cp != NULL, "class_name can't be null");
6054 
6055   // Update _class_name to reflect the name in the constant pool
6056   update_class_name(class_name_in_cp);
6057 
6058   // Don't need to check whether this class name is legal or not.
6059   // It has been checked when constant pool is parsed.
6060   // However, make sure it is not an array type.
6061   if (_need_verify) {
6062     guarantee_property(_class_name->char_at(0) != JVM_SIGNATURE_ARRAY,
6063                        "Bad class name in class file %s",
6064                        CHECK);
6065   }
6066 
6067   // Checks if name in class file matches requested name
6068   if (_requested_name != NULL && _requested_name != _class_name) {
6069     ResourceMark rm(THREAD);
6070     Exceptions::fthrow(
6071       THREAD_AND_LOCATION,
6072       vmSymbols::java_lang_NoClassDefFoundError(),
6073       "%s (wrong name: %s)",
6074       _class_name->as_C_string(),
6075       _requested_name != NULL ? _requested_name->as_C_string() : "NoName"
6076     );
6077     return;
6078   }















6079 
6080   // if this is an anonymous class fix up its name if it's in the unnamed


















6081   // package.  Otherwise, throw IAE if it is in a different package than
6082   // its host class.
6083   if (_unsafe_anonymous_host != NULL) {



6084     fix_unsafe_anonymous_class_name(CHECK);






















6085   }
6086 
6087   // Verification prevents us from creating names with dots in them, this
6088   // asserts that that's the case.
6089   assert(is_internal_format(_class_name), "external class name format used internally");
6090 
6091   if (!is_internal()) {
6092     LogTarget(Debug, class, preorder) lt;
6093     if (lt.is_enabled()){
6094       ResourceMark rm(THREAD);
6095       LogStream ls(lt);
6096       ls.print("%s", _class_name->as_klass_external_name());
6097       if (stream->source() != NULL) {
6098         ls.print(" source: %s", stream->source());
6099       }
6100       ls.cr();
6101     }
6102 
6103 #if INCLUDE_CDS
6104     if (DumpLoadedClassList != NULL && stream->source() != NULL && classlist_file->is_open()) {
6105       if (!ClassLoader::has_jrt_entry()) {
6106         warning("DumpLoadedClassList and CDS are not supported in exploded build");
6107         DumpLoadedClassList = NULL;
6108       } else if (SystemDictionaryShared::is_sharing_possible(_loader_data) &&

6109                  _unsafe_anonymous_host == NULL) {
6110         // Only dump the classes that can be stored into CDS archive.
6111         // Unsafe anonymous classes such as generated LambdaForm classes are also not included.
6112         oop class_loader = _loader_data->class_loader();
6113         ResourceMark rm(THREAD);
6114         bool skip = false;
6115         if (class_loader == NULL || SystemDictionary::is_platform_class_loader(class_loader)) {
6116           // For the boot and platform class loaders, skip classes that are not found in the
6117           // java runtime image, such as those found in the --patch-module entries.
6118           // These classes can't be loaded from the archive during runtime.
6119           if (!stream->from_boot_loader_modules_image() && strncmp(stream->source(), "jrt:", 4) != 0) {
6120             skip = true;
6121           }
6122 
6123           if (class_loader == NULL && ClassLoader::contains_append_entry(stream->source())) {
6124             // .. but don't skip the boot classes that are loaded from -Xbootclasspath/a
6125             // as they can be loaded from the archive during runtime.
6126             skip = false;
6127           }
6128         }
6129         if (skip) {
6130           tty->print_cr("skip writing class %s from source %s to classlist file",
6131             _class_name->as_C_string(), stream->source());




1072     _method_DontInline,
1073     _method_InjectedProfile,
1074     _method_LambdaForm_Compiled,
1075     _method_Hidden,
1076     _method_HotSpotIntrinsicCandidate,
1077     _jdk_internal_vm_annotation_Contended,
1078     _field_Stable,
1079     _jdk_internal_vm_annotation_ReservedStackAccess,
1080     _annotation_LIMIT
1081   };
1082   const Location _location;
1083   int _annotations_present;
1084   u2 _contended_group;
1085 
1086   AnnotationCollector(Location location)
1087     : _location(location), _annotations_present(0)
1088   {
1089     assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
1090   }
1091   // If this annotation name has an ID, report it (or _none).
1092   ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, const bool can_access_vm_annotations);
1093   // Set the annotation name:
1094   void set_annotation(ID id) {
1095     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1096     _annotations_present |= nth_bit((int)id);
1097   }
1098 
1099   void remove_annotation(ID id) {
1100     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1101     _annotations_present &= ~nth_bit((int)id);
1102   }
1103 
1104   // Report if the annotation is present.
1105   bool has_any_annotations() const { return _annotations_present != 0; }
1106   bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; }
1107 
1108   void set_contended_group(u2 group) { _contended_group = group; }
1109   u2 contended_group() const { return _contended_group; }
1110 
1111   bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); }
1112 


1205       int nval = Bytes::get_Java_u2((address)buffer + index - 2);
1206       while (--nval >= 0 && index < limit) {
1207         index = skip_annotation_value(buffer, limit, index);
1208       }
1209     }
1210     break;
1211     case '@':
1212       index = skip_annotation(buffer, limit, index);
1213       break;
1214     default:
1215       return limit;  //  bad tag byte
1216   }
1217   return index;
1218 }
1219 
1220 // Sift through annotations, looking for those significant to the VM:
1221 static void parse_annotations(const ConstantPool* const cp,
1222                               const u1* buffer, int limit,
1223                               AnnotationCollector* coll,
1224                               ClassLoaderData* loader_data,
1225                               const bool can_access_vm_annotations,
1226                               TRAPS) {
1227 
1228   assert(cp != NULL, "invariant");
1229   assert(buffer != NULL, "invariant");
1230   assert(coll != NULL, "invariant");
1231   assert(loader_data != NULL, "invariant");
1232 
1233   // annotations := do(nann:u2) {annotation}
1234   int index = 2; // read nann
1235   if (index >= limit)  return;
1236   int nann = Bytes::get_Java_u2((address)buffer + index - 2);
1237   enum {  // initial annotation layout
1238     atype_off = 0,      // utf8 such as 'Ljava/lang/annotation/Retention;'
1239     count_off = 2,      // u2   such as 1 (one value)
1240     member_off = 4,     // utf8 such as 'value'
1241     tag_off = 6,        // u1   such as 'c' (type) or 'e' (enum)
1242     e_tag_val = 'e',
1243     e_type_off = 7,   // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;'
1244     e_con_off = 9,    // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME'
1245     e_size = 11,     // end of 'e' annotation


1251     s_size = 9,
1252     min_size = 6        // smallest possible size (zero members)
1253   };
1254   // Cannot add min_size to index in case of overflow MAX_INT
1255   while ((--nann) >= 0 && (index - 2 <= limit - min_size)) {
1256     int index0 = index;
1257     index = skip_annotation(buffer, limit, index);
1258     const u1* const abase = buffer + index0;
1259     const int atype = Bytes::get_Java_u2((address)abase + atype_off);
1260     const int count = Bytes::get_Java_u2((address)abase + count_off);
1261     const Symbol* const aname = check_symbol_at(cp, atype);
1262     if (aname == NULL)  break;  // invalid annotation name
1263     const Symbol* member = NULL;
1264     if (count >= 1) {
1265       const int member_index = Bytes::get_Java_u2((address)abase + member_off);
1266       member = check_symbol_at(cp, member_index);
1267       if (member == NULL)  break;  // invalid member name
1268     }
1269 
1270     // Here is where parsing particular annotations will take place.
1271     AnnotationCollector::ID id = coll->annotation_index(loader_data, aname, can_access_vm_annotations);
1272     if (AnnotationCollector::_unknown == id)  continue;
1273     coll->set_annotation(id);
1274 
1275     if (AnnotationCollector::_jdk_internal_vm_annotation_Contended == id) {
1276       // @Contended can optionally specify the contention group.
1277       //
1278       // Contended group defines the equivalence class over the fields:
1279       // the fields within the same contended group are not treated distinct.
1280       // The only exception is default group, which does not incur the
1281       // equivalence. Naturally, contention group for classes is meaningless.
1282       //
1283       // While the contention group is specified as String, annotation
1284       // values are already interned, and we might as well use the constant
1285       // pool index as the group tag.
1286       //
1287       u2 group_index = 0; // default contended group
1288       if (count == 1
1289         && s_size == (index - index0)  // match size
1290         && s_tag_val == *(abase + tag_off)
1291         && member == vmSymbols::value_name()) {


1377         if (attribute_length != 2) {
1378           classfile_parse_error(
1379             "Wrong size %u for field's Signature attribute in class file %s",
1380             attribute_length, CHECK);
1381         }
1382         generic_signature_index = parse_generic_signature_attribute(cfs, CHECK);
1383       } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
1384         if (runtime_visible_annotations != NULL) {
1385           classfile_parse_error(
1386             "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", CHECK);
1387         }
1388         runtime_visible_annotations_length = attribute_length;
1389         runtime_visible_annotations = cfs->current();
1390         assert(runtime_visible_annotations != NULL, "null visible annotations");
1391         cfs->guarantee_more(runtime_visible_annotations_length, CHECK);
1392         parse_annotations(cp,
1393                           runtime_visible_annotations,
1394                           runtime_visible_annotations_length,
1395                           parsed_annotations,
1396                           _loader_data,
1397                           _can_access_vm_annotations,
1398                           CHECK);
1399         cfs->skip_u1_fast(runtime_visible_annotations_length);
1400       } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
1401         if (runtime_invisible_annotations_exists) {
1402           classfile_parse_error(
1403             "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK);
1404         }
1405         runtime_invisible_annotations_exists = true;
1406         if (PreserveAllAnnotations) {
1407           runtime_invisible_annotations_length = attribute_length;
1408           runtime_invisible_annotations = cfs->current();
1409           assert(runtime_invisible_annotations != NULL, "null invisible annotations");
1410         }
1411         cfs->skip_u1(attribute_length, CHECK);
1412       } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {
1413         if (runtime_visible_type_annotations != NULL) {
1414           classfile_parse_error(
1415             "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK);
1416         }
1417         runtime_visible_type_annotations_length = attribute_length;


2037   }
2038   return checked_exceptions_start;
2039 }
2040 
2041 void ClassFileParser::throwIllegalSignature(const char* type,
2042                                             const Symbol* name,
2043                                             const Symbol* sig,
2044                                             TRAPS) const {
2045   assert(name != NULL, "invariant");
2046   assert(sig != NULL, "invariant");
2047 
2048   ResourceMark rm(THREAD);
2049   Exceptions::fthrow(THREAD_AND_LOCATION,
2050       vmSymbols::java_lang_ClassFormatError(),
2051       "%s \"%s\" in class %s has illegal signature \"%s\"", type,
2052       name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
2053 }
2054 
2055 AnnotationCollector::ID
2056 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
2057                                       const Symbol* name,
2058                                       const bool can_access_vm_annotations) {
2059   const vmSymbols::SID sid = vmSymbols::find_sid(name);
2060   // Privileged code can use all annotations.  Other code silently drops some.
2061   const bool privileged = loader_data->is_the_null_class_loader_data() ||
2062                           loader_data->is_platform_class_loader_data() ||
2063                           loader_data->is_shortlived() ||
2064                           can_access_vm_annotations;
2065   switch (sid) {
2066     case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
2067       if (_location != _in_method)  break;  // only allow for methods
2068       if (!privileged)              break;  // only allow in privileged code
2069       return _method_CallerSensitive;
2070     }
2071     case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
2072       if (_location != _in_method)  break;  // only allow for methods
2073       if (!privileged)              break;  // only allow in privileged code
2074       return _method_ForceInline;
2075     }
2076     case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): {
2077       if (_location != _in_method)  break;  // only allow for methods
2078       if (!privileged)              break;  // only allow in privileged code
2079       return _method_DontInline;
2080     }
2081     case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): {
2082       if (_location != _in_method)  break;  // only allow for methods
2083       if (!privileged)              break;  // only allow in privileged code
2084       return _method_InjectedProfile;


2663           classfile_parse_error(
2664             "Invalid Signature attribute length %u in class file %s",
2665             method_attribute_length, CHECK_NULL);
2666         }
2667         generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL);
2668       } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
2669         if (runtime_visible_annotations != NULL) {
2670           classfile_parse_error(
2671             "Multiple RuntimeVisibleAnnotations attributes for method in class file %s",
2672             CHECK_NULL);
2673         }
2674         runtime_visible_annotations_length = method_attribute_length;
2675         runtime_visible_annotations = cfs->current();
2676         assert(runtime_visible_annotations != NULL, "null visible annotations");
2677         cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL);
2678         parse_annotations(cp,
2679                           runtime_visible_annotations,
2680                           runtime_visible_annotations_length,
2681                           &parsed_annotations,
2682                           _loader_data,
2683                           _can_access_vm_annotations,
2684                           CHECK_NULL);
2685         cfs->skip_u1_fast(runtime_visible_annotations_length);
2686       } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
2687         if (runtime_invisible_annotations_exists) {
2688           classfile_parse_error(
2689             "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s",
2690             CHECK_NULL);
2691         }
2692         runtime_invisible_annotations_exists = true;
2693         if (PreserveAllAnnotations) {
2694           runtime_invisible_annotations_length = method_attribute_length;
2695           runtime_invisible_annotations = cfs->current();
2696           assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2697         }
2698         cfs->skip_u1(method_attribute_length, CHECK_NULL);
2699       } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) {
2700         if (runtime_visible_parameter_annotations != NULL) {
2701           classfile_parse_error(
2702             "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s",
2703             CHECK_NULL);


3426         if (attribute_length != 2) {
3427           classfile_parse_error(
3428             "Wrong Signature attribute length %u in class file %s",
3429             attribute_length, CHECK);
3430         }
3431         parse_classfile_signature_attribute(cfs, CHECK);
3432       } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
3433         if (runtime_visible_annotations != NULL) {
3434           classfile_parse_error(
3435             "Multiple RuntimeVisibleAnnotations attributes in class file %s", CHECK);
3436         }
3437         runtime_visible_annotations_length = attribute_length;
3438         runtime_visible_annotations = cfs->current();
3439         assert(runtime_visible_annotations != NULL, "null visible annotations");
3440         cfs->guarantee_more(runtime_visible_annotations_length, CHECK);
3441         parse_annotations(cp,
3442                           runtime_visible_annotations,
3443                           runtime_visible_annotations_length,
3444                           parsed_annotations,
3445                           _loader_data,
3446                           _can_access_vm_annotations,
3447                           CHECK);
3448         cfs->skip_u1_fast(runtime_visible_annotations_length);
3449       } else if (tag == vmSymbols::tag_runtime_invisible_annotations()) {
3450         if (runtime_invisible_annotations_exists) {
3451           classfile_parse_error(
3452             "Multiple RuntimeInvisibleAnnotations attributes in class file %s", CHECK);
3453         }
3454         runtime_invisible_annotations_exists = true;
3455         if (PreserveAllAnnotations) {
3456           runtime_invisible_annotations_length = attribute_length;
3457           runtime_invisible_annotations = cfs->current();
3458           assert(runtime_invisible_annotations != NULL, "null invisible annotations");
3459         }
3460         cfs->skip_u1(attribute_length, CHECK);
3461       } else if (tag == vmSymbols::tag_enclosing_method()) {
3462         if (parsed_enclosingmethod_attribute) {
3463           classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
3464         } else {
3465           parsed_enclosingmethod_attribute = true;
3466         }


5413 
5414           if (!match) {
5415             char buf[1000];
5416             tty->print("Compiler intrinsic is defined for method [%s], "
5417                        "but the method is not available in class [%s].%s",
5418                         vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID_from(id),
5419                                                              buf, sizeof(buf)),
5420                         ik->name()->as_C_string(),
5421                         NOT_DEBUG("") DEBUG_ONLY(" Exiting.")
5422             );
5423             tty->cr();
5424             DEBUG_ONLY(vm_exit(1));
5425           }
5426         }
5427       } // end for
5428     } // CheckIntrinsics
5429 #endif // ASSERT
5430   }
5431 }
5432 
5433 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5434                                                       InstanceKlass* dynamic_nest_host,
5435                                                       Handle classData,
5436                                                       TRAPS) {
5437   if (_klass != NULL) {
5438     return _klass;
5439   }
5440 
5441   InstanceKlass* const ik =
5442     InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5443 
5444   fill_instance_klass(ik, changed_by_loadhook, dynamic_nest_host, classData, CHECK_NULL);
5445 
5446   assert(_klass == ik, "invariant");
5447 
5448 
5449   if (ik->should_store_fingerprint()) {
5450     ik->store_fingerprint(_stream->compute_fingerprint());
5451   }
5452 
5453   ik->set_has_passed_fingerprint_check(false);
5454   if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
5455     uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
5456     uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint();
5457     if (aot_fp != 0 && aot_fp == fp) {
5458       // This class matches with a class saved in an AOT library
5459       ik->set_has_passed_fingerprint_check(true);
5460     } else {
5461       ResourceMark rm;
5462       log_info(class, fingerprint)("%s :  expected = " PTR64_FORMAT " actual = " PTR64_FORMAT,
5463                                  ik->external_name(), aot_fp, _stream->compute_fingerprint());
5464     }
5465   }
5466 
5467   return ik;
5468 }
5469 
5470 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5471                                           bool changed_by_loadhook,
5472                                           InstanceKlass* dynamic_nest_host,
5473                                           Handle classData,
5474                                           TRAPS) {
5475   assert(ik != NULL, "invariant");
5476 
5477   // Set name and CLD before adding to CLD
5478   ik->set_class_loader_data(_loader_data);
5479   ik->set_name(_class_name);
5480 
5481   // Add all classes to our internal class loader list here,
5482   // including classes in the bootstrap (NULL) class loader.
5483   const bool publicize = !is_internal();
5484 
5485   _loader_data->add_class(ik, publicize);
5486 
5487   set_klass_to_deallocate(ik);
5488 
5489   assert(_field_info != NULL, "invariant");
5490   assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5491   assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count,
5492     "sanity");
5493 
5494   assert(ik->is_instance_klass(), "sanity");
5495   assert(ik->size_helper() == _field_info->instance_size, "sanity");
5496 
5497   // Fill in information already parsed
5498   ik->set_should_verify_class(_need_verify);
5499 
5500   // Not yet: supers are done below to support the new subtype-checking fields
5501   ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5502   ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5503   assert(_fac != NULL, "invariant");
5504   ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5505 
5506   // this transfers ownership of a lot of arrays from
5507   // the parser onto the InstanceKlass*
5508   apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5509 
5510   // can only set dynamic nest-host after static nest information is set
5511   if (dynamic_nest_host != NULL) {
5512     ik->set_nest_host(dynamic_nest_host, CHECK);
5513   }
5514 
5515   // note that is not safe to use the fields in the parser from this point on
5516   assert(NULL == _cp, "invariant");
5517   assert(NULL == _fields, "invariant");
5518   assert(NULL == _methods, "invariant");
5519   assert(NULL == _inner_classes, "invariant");
5520   assert(NULL == _nest_members, "invariant");
5521   assert(NULL == _local_interfaces, "invariant");
5522   assert(NULL == _combined_annotations, "invariant");
5523 
5524   if (_has_final_method) {
5525     ik->set_has_final_method();
5526   }
5527 
5528   ik->copy_method_ordering(_method_ordering, CHECK);
5529   // The InstanceKlass::_methods_jmethod_ids cache
5530   // is managed on the assumption that the initial cache
5531   // size is equal to the number of methods in the class. If
5532   // that changes, then InstanceKlass::idnum_can_increment()
5533   // has to be changed accordingly.
5534   ik->set_initial_method_idnum(ik->methods()->length());
5535 
5536   ik->set_this_class_index(_this_class_index);
5537 
5538   if (_is_nonfindable || is_unsafe_anonymous()) {
5539     // _this_class_index is a CONSTANT_Class entry that refers to this
5540     // nonfindable or anonymous class itself. If this class needs to refer to its own
5541     // methods or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5542     // _this_class_index. However, because this class is nonfindable or anonymous (it's
5543     // not stored in SystemDictionary), _this_class_index cannot be resolved
5544     // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5545     // Therefore, we must eagerly resolve _this_class_index now.
5546     ik->constants()->klass_at_put(_this_class_index, ik);
5547   }
5548 
5549   ik->set_minor_version(_minor_version);
5550   ik->set_major_version(_major_version);
5551   ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5552   ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5553 
5554   if (_unsafe_anonymous_host != NULL) {
5555     assert (ik->is_unsafe_anonymous(), "should be the same");
5556     ik->set_unsafe_anonymous_host(_unsafe_anonymous_host);
5557   }
5558 
5559   // Set PackageEntry for this_klass
5560   oop cl = ik->class_loader();
5561   Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5562   ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5563   ik->set_package(cld, CHECK);
5564   // Obtain this_klass' module entry
5565   ModuleEntry* module_entry = ik->module();
5566   assert(module_entry != NULL, "module_entry should always be set");
5567 
5568 
5569   const Array<Method*>* const methods = ik->methods();
5570   assert(methods != NULL, "invariant");
5571   const int methods_len = methods->length();
5572 
5573   check_methods_for_intrinsics(ik, methods);
5574 
5575   // Fill in field values obtained by parse_classfile_attributes
5576   if (_parsed_annotations->has_any_annotations()) {
5577     _parsed_annotations->apply_to(ik);
5578   }
5579 
5580   apply_parsed_class_attributes(ik);
5581 
5582   // Miranda methods
5583   if ((_num_miranda_methods > 0) ||
5584       // if this class introduced new miranda methods or
5585       (_super_klass != NULL && _super_klass->has_miranda_methods())
5586         // super class exists and this class inherited miranda methods
5587      ) {


5603                 _field_info->nonstatic_oop_offsets,
5604                 _field_info->nonstatic_oop_counts);
5605 
5606   // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
5607   set_precomputed_flags(ik);
5608 
5609   // check if this class can access its super class
5610   check_super_class_access(ik, CHECK);
5611 
5612   // check if this class can access its superinterfaces
5613   check_super_interface_access(ik, CHECK);
5614 
5615   // check if this class overrides any final method
5616   check_final_method_override(ik, CHECK);
5617 
5618   // reject static interface methods prior to Java 8
5619   if (ik->is_interface() && _major_version < JAVA_8_VERSION) {
5620     check_illegal_static_method(ik, CHECK);
5621   }
5622 




5623   // Obtain java.lang.Module
5624   Handle module_handle(THREAD, module_entry->module());
5625 
5626   // Allocate mirror and initialize static fields
5627   // The create_mirror() call will also call compute_modifiers()
5628   java_lang_Class::create_mirror(ik,
5629                                  Handle(THREAD, _loader_data->class_loader()),
5630                                  module_handle,
5631                                  _protection_domain,
5632                                  classData,
5633                                  CHECK);
5634 
5635   assert(_all_mirandas != NULL, "invariant");
5636 
5637   // Generate any default methods - default methods are public interface methods
5638   // that have a default implementation.  This is new with Java 8.
5639   if (_has_nonstatic_concrete_methods) {
5640     DefaultMethods::generate_default_methods(ik,
5641                                              _all_mirandas,
5642                                              CHECK);
5643   }
5644 
5645   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5646   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5647       !module_entry->has_default_read_edges()) {
5648     if (!module_entry->set_has_default_read_edges()) {
5649       // We won a potential race
5650       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5651     }
5652   }


5686           const char * to = k->external_name();
5687           log_debug(class, resolve)("%s %s (interface)", from, to);
5688         }
5689       }
5690     }
5691   }
5692 
5693   JFR_ONLY(INIT_ID(ik);)
5694 
5695   // If we reach here, all is well.
5696   // Now remove the InstanceKlass* from the _klass_to_deallocate field
5697   // in order for it to not be destroyed in the ClassFileParser destructor.
5698   set_klass_to_deallocate(NULL);
5699 
5700   // it's official
5701   set_klass(ik);
5702 
5703   debug_only(ik->verify();)
5704 }
5705 












5706 // For an unsafe anonymous class that is in the unnamed package, move it to its host class's
5707 // package by prepending its host class's package name to its class name and setting
5708 // its _class_name field.
5709 void ClassFileParser::prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS) {
5710   ResourceMark rm(THREAD);
5711   assert(strrchr(_class_name->as_C_string(), '/') == NULL,
5712          "Unsafe anonymous class should not be in a package");
5713   const char* host_pkg_name =
5714     ClassLoader::package_from_name(unsafe_anonymous_host->name()->as_C_string(), NULL);
5715 
5716   if (host_pkg_name != NULL) {
5717     int host_pkg_len = (int)strlen(host_pkg_name);
5718     int class_name_len = _class_name->utf8_length();
5719     int symbol_len = host_pkg_len + 1 + class_name_len;
5720     char* new_anon_name = NEW_RESOURCE_ARRAY(char, symbol_len + 1);
5721     int n = os::snprintf(new_anon_name, symbol_len + 1, "%s/%.*s",
5722                          host_pkg_name, class_name_len, _class_name->base());
5723     assert(n == symbol_len, "Unexpected number of characters in string");
5724 
5725     // Decrement old _class_name to avoid leaking.


5754   }
5755 }
5756 
5757 static bool relax_format_check_for(ClassLoaderData* loader_data) {
5758   bool trusted = (loader_data->is_the_null_class_loader_data() ||
5759                   SystemDictionary::is_platform_class_loader(loader_data->class_loader()));
5760   bool need_verify =
5761     // verifyAll
5762     (BytecodeVerificationLocal && BytecodeVerificationRemote) ||
5763     // verifyRemote
5764     (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);
5765   return !need_verify;
5766 }
5767 
5768 ClassFileParser::ClassFileParser(ClassFileStream* stream,
5769                                  Symbol* name,
5770                                  ClassLoaderData* loader_data,
5771                                  Handle protection_domain,
5772                                  const InstanceKlass* unsafe_anonymous_host,
5773                                  GrowableArray<Handle>* cp_patches,
5774                                  const bool is_nonfindable,
5775                                  const bool can_access_vm_annotations,
5776                                  Publicity pub_level,
5777                                  TRAPS) :
5778   _stream(stream),

5779   _class_name(NULL),
5780   _loader_data(loader_data),
5781   _unsafe_anonymous_host(unsafe_anonymous_host),
5782   _cp_patches(cp_patches),
5783   _is_nonfindable(is_nonfindable),
5784   _can_access_vm_annotations(can_access_vm_annotations),
5785   _num_patched_klasses(0),
5786   _max_num_patched_klasses(0),
5787   _orig_cp_size(0),
5788   _first_patched_klass_resolved_index(0),
5789   _super_klass(),
5790   _cp(NULL),
5791   _fields(NULL),
5792   _methods(NULL),
5793   _inner_classes(NULL),
5794   _nest_members(NULL),
5795   _nest_host(0),
5796   _local_interfaces(NULL),
5797   _transitive_interfaces(NULL),
5798   _combined_annotations(NULL),
5799   _annotations(NULL),
5800   _type_annotations(NULL),
5801   _fields_annotations(NULL),
5802   _fields_type_annotations(NULL),
5803   _klass(NULL),
5804   _klass_to_deallocate(NULL),


5991             _major_version,  _minor_version, _class_name->as_C_string());
5992     Exceptions::fthrow(
5993       THREAD_AND_LOCATION,
5994       vmSymbols::java_lang_UnsupportedClassVersionError(),
5995       "Unsupported major.minor version for dump time %u.%u",
5996       _major_version,
5997       _minor_version);
5998   }
5999 
6000   // Check version numbers - we check this even with verifier off
6001   verify_class_version(_major_version, _minor_version, _class_name, CHECK);
6002 
6003   stream->guarantee_more(3, CHECK); // length, first cp tag
6004   u2 cp_size = stream->get_u2_fast();
6005 
6006   guarantee_property(
6007     cp_size >= 1, "Illegal constant pool size %u in class file %s",
6008     cp_size, CHECK);
6009 
6010   _orig_cp_size = cp_size;
6011   if (is_nonfindable()) { // Add a slot for nonfindable class name.
6012     assert(_max_num_patched_klasses == 0, "Sanity check");
6013     cp_size++;
6014   } else {
6015     if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
6016       THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
6017     }
6018     cp_size += _max_num_patched_klasses;
6019   }

6020 
6021   _cp = ConstantPool::allocate(_loader_data,
6022                                cp_size,
6023                                CHECK);
6024 
6025   ConstantPool* const cp = _cp;
6026 
6027   parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6028 
6029   assert(cp_size == (const u2)cp->length(), "invariant");
6030 
6031   // ACCESS FLAGS
6032   stream->guarantee_more(8, CHECK);  // flags, this_class, super_class, infs_len
6033 
6034   // Access flags
6035   jint flags;
6036   // JVM_ACC_MODULE is defined in JDK-9 and later.
6037   if (_major_version >= JAVA_9_VERSION) {
6038     flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
6039   } else {


6050   short bad_constant = class_bad_constant_seen();
6051   if (bad_constant != 0) {
6052     // Do not throw CFE until after the access_flags are checked because if
6053     // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6054     classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK);
6055   }
6056 
6057   _access_flags.set_flags(flags);
6058 
6059   // This class and superclass
6060   _this_class_index = stream->get_u2_fast();
6061   check_property(
6062     valid_cp_range(_this_class_index, cp_size) &&
6063       cp->tag_at(_this_class_index).is_unresolved_klass(),
6064     "Invalid this class index %u in constant pool in class file %s",
6065     _this_class_index, CHECK);
6066 
6067   Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
6068   assert(class_name_in_cp != NULL, "class_name can't be null");
6069 



6070   // Don't need to check whether this class name is legal or not.
6071   // It has been checked when constant pool is parsed.
6072   // However, make sure it is not an array type.
6073   if (_need_verify) {
6074     guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
6075                        "Bad class name in class file %s",
6076                        CHECK);
6077   }
6078 
6079 #ifdef ASSERT
6080   // Basic sanity checks
6081   assert(!(_is_nonfindable && (_unsafe_anonymous_host != NULL)), "mutually exclusive variants");
6082 
6083   if (_unsafe_anonymous_host != NULL) {
6084     assert(_class_name == vmSymbols::unknown_class_name(), "A named anonymous class???");





6085   }
6086   if (_is_nonfindable) {
6087     assert(_class_name != vmSymbols::unknown_class_name(), "non-findable classes should have a special name");
6088   }
6089 #endif
6090 
6091   // Update the _class_name as needed depending on whether this is a named,
6092   // un-named, nonfindable or unsafe-anonymous class.
6093 
6094   if (_is_nonfindable) {
6095     _class_name->increment_refcount();
6096 
6097     // Add a Utf8 entry containing the nonfindable name.
6098     assert(_class_name != NULL, "Unexpected null _class_name");
6099     int nonfindable_name_index = _orig_cp_size; // this is an extra slot we added
6100     cp->symbol_at_put(nonfindable_name_index, _class_name);
6101 
6102     if (_need_verify) {
6103       // Since this name was not in the original constant pool, it didn't get
6104       // checked during constantpool parsing.  So, check it here.
6105       verify_legal_class_name(_class_name, CHECK);
6106     }
6107 
6108     // Update this_class_index's slot in the constant pool with the new Utf8 entry.
6109     // We have to update the resolved_klass_index and the name_index together
6110     // so extract the existing resolved_klass_index first.
6111     CPKlassSlot cp_klass_slot = cp->klass_slot_at(_this_class_index);
6112     int resolved_klass_index = cp_klass_slot.resolved_klass_index();
6113     cp->unresolved_klass_at_put(_this_class_index, nonfindable_name_index, resolved_klass_index);
6114     assert(cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
6115            "Bad name_index");
6116 
6117   // NOTE: !_is_nonfindable does not imply "findable" as it could be an old-style
6118   //       "non-findable" unsafe-anonymous class
6119 
6120   // If this is an anonymous class fix up its name if it is in the unnamed
6121   // package.  Otherwise, throw IAE if it is in a different package than
6122   // its host class.
6123   } else if (_unsafe_anonymous_host != NULL) {
6124     Symbol* old_class_name = _class_name;
6125     _class_name = class_name_in_cp;
6126     _class_name->increment_refcount();
6127     fix_unsafe_anonymous_class_name(CHECK);
6128     old_class_name->decrement_refcount();
6129 
6130   } else {
6131     // Check if name in class file matches given name
6132     if (_class_name != class_name_in_cp) {
6133       if (_class_name != vmSymbols::unknown_class_name()) {
6134         ResourceMark rm(THREAD);
6135         Exceptions::fthrow(THREAD_AND_LOCATION,
6136                            vmSymbols::java_lang_NoClassDefFoundError(),
6137                            "%s (wrong name: %s)",
6138                            class_name_in_cp->as_C_string(),
6139                            _class_name->as_C_string()
6140                            );
6141         return;
6142       } else {
6143         // The class name was not known by the caller so we set it from
6144         // the value in the CP.
6145         _class_name = class_name_in_cp;
6146         _class_name->increment_refcount();
6147       }
6148       // else nothing to do: the expected class name matches what is in the CP
6149     }
6150   }
6151 
6152   // Verification prevents us from creating names with dots in them, this
6153   // asserts that that's the case.
6154   assert(is_internal_format(_class_name), "external class name format used internally");
6155 
6156   if (!is_internal()) {
6157     LogTarget(Debug, class, preorder) lt;
6158     if (lt.is_enabled()){
6159       ResourceMark rm(THREAD);
6160       LogStream ls(lt);
6161       ls.print("%s", _class_name->as_klass_external_name());
6162       if (stream->source() != NULL) {
6163         ls.print(" source: %s", stream->source());
6164       }
6165       ls.cr();
6166     }
6167 
6168 #if INCLUDE_CDS
6169     if (DumpLoadedClassList != NULL && stream->source() != NULL && classlist_file->is_open()) {
6170       if (!ClassLoader::has_jrt_entry()) {
6171         warning("DumpLoadedClassList and CDS are not supported in exploded build");
6172         DumpLoadedClassList = NULL;
6173       } else if (SystemDictionaryShared::is_sharing_possible(_loader_data) &&
6174                  !_is_nonfindable &&
6175                  _unsafe_anonymous_host == NULL) {
6176         // Only dump the classes that can be stored into CDS archive.
6177         // Nonfindable and unsafe anonymous classes such as generated LambdaForm classes are also not included.
6178         oop class_loader = _loader_data->class_loader();
6179         ResourceMark rm(THREAD);
6180         bool skip = false;
6181         if (class_loader == NULL || SystemDictionary::is_platform_class_loader(class_loader)) {
6182           // For the boot and platform class loaders, skip classes that are not found in the
6183           // java runtime image, such as those found in the --patch-module entries.
6184           // These classes can't be loaded from the archive during runtime.
6185           if (!stream->from_boot_loader_modules_image() && strncmp(stream->source(), "jrt:", 4) != 0) {
6186             skip = true;
6187           }
6188 
6189           if (class_loader == NULL && ClassLoader::contains_append_entry(stream->source())) {
6190             // .. but don't skip the boot classes that are loaded from -Xbootclasspath/a
6191             // as they can be loaded from the archive during runtime.
6192             skip = false;
6193           }
6194         }
6195         if (skip) {
6196           tty->print_cr("skip writing class %s from source %s to classlist file",
6197             _class_name->as_C_string(), stream->source());


< prev index next >