1073 _method_DontInline,
1074 _method_InjectedProfile,
1075 _method_LambdaForm_Compiled,
1076 _method_Hidden,
1077 _method_HotSpotIntrinsicCandidate,
1078 _jdk_internal_vm_annotation_Contended,
1079 _field_Stable,
1080 _jdk_internal_vm_annotation_ReservedStackAccess,
1081 _annotation_LIMIT
1082 };
1083 const Location _location;
1084 int _annotations_present;
1085 u2 _contended_group;
1086
1087 AnnotationCollector(Location location)
1088 : _location(location), _annotations_present(0)
1089 {
1090 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
1091 }
1092 // If this annotation name has an ID, report it (or _none).
1093 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name);
1094 // Set the annotation name:
1095 void set_annotation(ID id) {
1096 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1097 _annotations_present |= nth_bit((int)id);
1098 }
1099
1100 void remove_annotation(ID id) {
1101 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1102 _annotations_present &= ~nth_bit((int)id);
1103 }
1104
1105 // Report if the annotation is present.
1106 bool has_any_annotations() const { return _annotations_present != 0; }
1107 bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; }
1108
1109 void set_contended_group(u2 group) { _contended_group = group; }
1110 u2 contended_group() const { return _contended_group; }
1111
1112 bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); }
1113
1206 int nval = Bytes::get_Java_u2((address)buffer + index - 2);
1207 while (--nval >= 0 && index < limit) {
1208 index = skip_annotation_value(buffer, limit, index);
1209 }
1210 }
1211 break;
1212 case '@':
1213 index = skip_annotation(buffer, limit, index);
1214 break;
1215 default:
1216 return limit; // bad tag byte
1217 }
1218 return index;
1219 }
1220
1221 // Sift through annotations, looking for those significant to the VM:
1222 static void parse_annotations(const ConstantPool* const cp,
1223 const u1* buffer, int limit,
1224 AnnotationCollector* coll,
1225 ClassLoaderData* loader_data,
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);
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 CHECK);
1398 cfs->skip_u1_fast(runtime_visible_annotations_length);
1399 } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
1400 if (runtime_invisible_annotations_exists) {
1401 classfile_parse_error(
1402 "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK);
1403 }
1404 runtime_invisible_annotations_exists = true;
1405 if (PreserveAllAnnotations) {
1406 runtime_invisible_annotations_length = attribute_length;
1407 runtime_invisible_annotations = cfs->current();
1408 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
1409 }
1410 cfs->skip_u1(attribute_length, CHECK);
1411 } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {
1412 if (runtime_visible_type_annotations != NULL) {
1413 classfile_parse_error(
1414 "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK);
1415 }
1416 runtime_visible_type_annotations_length = attribute_length;
2036 }
2037 return checked_exceptions_start;
2038 }
2039
2040 void ClassFileParser::throwIllegalSignature(const char* type,
2041 const Symbol* name,
2042 const Symbol* sig,
2043 TRAPS) const {
2044 assert(name != NULL, "invariant");
2045 assert(sig != NULL, "invariant");
2046
2047 ResourceMark rm(THREAD);
2048 Exceptions::fthrow(THREAD_AND_LOCATION,
2049 vmSymbols::java_lang_ClassFormatError(),
2050 "%s \"%s\" in class %s has illegal signature \"%s\"", type,
2051 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
2052 }
2053
2054 AnnotationCollector::ID
2055 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
2056 const Symbol* name) {
2057 const vmSymbols::SID sid = vmSymbols::find_sid(name);
2058 // Privileged code can use all annotations. Other code silently drops some.
2059 const bool privileged = loader_data->is_the_null_class_loader_data() ||
2060 loader_data->is_platform_class_loader_data() ||
2061 loader_data->is_unsafe_anonymous();
2062 switch (sid) {
2063 case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
2064 if (_location != _in_method) break; // only allow for methods
2065 if (!privileged) break; // only allow in privileged code
2066 return _method_CallerSensitive;
2067 }
2068 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
2069 if (_location != _in_method) break; // only allow for methods
2070 if (!privileged) break; // only allow in privileged code
2071 return _method_ForceInline;
2072 }
2073 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): {
2074 if (_location != _in_method) break; // only allow for methods
2075 if (!privileged) break; // only allow in privileged code
2076 return _method_DontInline;
2077 }
2078 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): {
2079 if (_location != _in_method) break; // only allow for methods
2080 if (!privileged) break; // only allow in privileged code
2081 return _method_InjectedProfile;
2648 classfile_parse_error(
2649 "Invalid Signature attribute length %u in class file %s",
2650 method_attribute_length, CHECK_NULL);
2651 }
2652 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL);
2653 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
2654 if (runtime_visible_annotations != NULL) {
2655 classfile_parse_error(
2656 "Multiple RuntimeVisibleAnnotations attributes for method in class file %s",
2657 CHECK_NULL);
2658 }
2659 runtime_visible_annotations_length = method_attribute_length;
2660 runtime_visible_annotations = cfs->current();
2661 assert(runtime_visible_annotations != NULL, "null visible annotations");
2662 cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL);
2663 parse_annotations(cp,
2664 runtime_visible_annotations,
2665 runtime_visible_annotations_length,
2666 &parsed_annotations,
2667 _loader_data,
2668 CHECK_NULL);
2669 cfs->skip_u1_fast(runtime_visible_annotations_length);
2670 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
2671 if (runtime_invisible_annotations_exists) {
2672 classfile_parse_error(
2673 "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s",
2674 CHECK_NULL);
2675 }
2676 runtime_invisible_annotations_exists = true;
2677 if (PreserveAllAnnotations) {
2678 runtime_invisible_annotations_length = method_attribute_length;
2679 runtime_invisible_annotations = cfs->current();
2680 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2681 }
2682 cfs->skip_u1(method_attribute_length, CHECK_NULL);
2683 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) {
2684 if (runtime_visible_parameter_annotations != NULL) {
2685 classfile_parse_error(
2686 "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s",
2687 CHECK_NULL);
3410 if (attribute_length != 2) {
3411 classfile_parse_error(
3412 "Wrong Signature attribute length %u in class file %s",
3413 attribute_length, CHECK);
3414 }
3415 parse_classfile_signature_attribute(cfs, CHECK);
3416 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
3417 if (runtime_visible_annotations != NULL) {
3418 classfile_parse_error(
3419 "Multiple RuntimeVisibleAnnotations attributes in class file %s", CHECK);
3420 }
3421 runtime_visible_annotations_length = attribute_length;
3422 runtime_visible_annotations = cfs->current();
3423 assert(runtime_visible_annotations != NULL, "null visible annotations");
3424 cfs->guarantee_more(runtime_visible_annotations_length, CHECK);
3425 parse_annotations(cp,
3426 runtime_visible_annotations,
3427 runtime_visible_annotations_length,
3428 parsed_annotations,
3429 _loader_data,
3430 CHECK);
3431 cfs->skip_u1_fast(runtime_visible_annotations_length);
3432 } else if (tag == vmSymbols::tag_runtime_invisible_annotations()) {
3433 if (runtime_invisible_annotations_exists) {
3434 classfile_parse_error(
3435 "Multiple RuntimeInvisibleAnnotations attributes in class file %s", CHECK);
3436 }
3437 runtime_invisible_annotations_exists = true;
3438 if (PreserveAllAnnotations) {
3439 runtime_invisible_annotations_length = attribute_length;
3440 runtime_invisible_annotations = cfs->current();
3441 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
3442 }
3443 cfs->skip_u1(attribute_length, CHECK);
3444 } else if (tag == vmSymbols::tag_enclosing_method()) {
3445 if (parsed_enclosingmethod_attribute) {
3446 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
3447 } else {
3448 parsed_enclosingmethod_attribute = true;
3449 }
5400
5401 if (!match) {
5402 char buf[1000];
5403 tty->print("Compiler intrinsic is defined for method [%s], "
5404 "but the method is not available in class [%s].%s",
5405 vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID_from(id),
5406 buf, sizeof(buf)),
5407 ik->name()->as_C_string(),
5408 NOT_DEBUG("") DEBUG_ONLY(" Exiting.")
5409 );
5410 tty->cr();
5411 DEBUG_ONLY(vm_exit(1));
5412 }
5413 }
5414 } // end for
5415 } // CheckIntrinsics
5416 #endif // ASSERT
5417 }
5418 }
5419
5420 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook, TRAPS) {
5421 if (_klass != NULL) {
5422 return _klass;
5423 }
5424
5425 InstanceKlass* const ik =
5426 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5427
5428 fill_instance_klass(ik, changed_by_loadhook, CHECK_NULL);
5429
5430 assert(_klass == ik, "invariant");
5431
5432
5433 if (ik->should_store_fingerprint()) {
5434 ik->store_fingerprint(_stream->compute_fingerprint());
5435 }
5436
5437 ik->set_has_passed_fingerprint_check(false);
5438 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
5439 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
5440 uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint();
5441 if (aot_fp != 0 && aot_fp == fp) {
5442 // This class matches with a class saved in an AOT library
5443 ik->set_has_passed_fingerprint_check(true);
5444 } else {
5445 ResourceMark rm;
5446 log_info(class, fingerprint)("%s : expected = " PTR64_FORMAT " actual = " PTR64_FORMAT,
5447 ik->external_name(), aot_fp, _stream->compute_fingerprint());
5448 }
5449 }
5450
5451 return ik;
5452 }
5453
5454 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) {
5455 assert(ik != NULL, "invariant");
5456
5457 // Set name and CLD before adding to CLD
5458 ik->set_class_loader_data(_loader_data);
5459 ik->set_name(_class_name);
5460
5461 // Add all classes to our internal class loader list here,
5462 // including classes in the bootstrap (NULL) class loader.
5463 const bool publicize = !is_internal();
5464
5465 _loader_data->add_class(ik, publicize);
5466
5467 set_klass_to_deallocate(ik);
5468
5469 assert(_field_info != NULL, "invariant");
5470 assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5471 assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count,
5472 "sanity");
5473
5474 assert(ik->is_instance_klass(), "sanity");
5475 assert(ik->size_helper() == _field_info->instance_size, "sanity");
5476
5477 // Fill in information already parsed
5478 ik->set_should_verify_class(_need_verify);
5479
5480 // Not yet: supers are done below to support the new subtype-checking fields
5481 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5482 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5483 assert(_fac != NULL, "invariant");
5484 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5485
5486 // this transfers ownership of a lot of arrays from
5487 // the parser onto the InstanceKlass*
5488 apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5489
5490 // note that is not safe to use the fields in the parser from this point on
5491 assert(NULL == _cp, "invariant");
5492 assert(NULL == _fields, "invariant");
5493 assert(NULL == _methods, "invariant");
5494 assert(NULL == _inner_classes, "invariant");
5495 assert(NULL == _nest_members, "invariant");
5496 assert(NULL == _local_interfaces, "invariant");
5497 assert(NULL == _combined_annotations, "invariant");
5498
5499 if (_has_final_method) {
5500 ik->set_has_final_method();
5501 }
5502
5503 ik->copy_method_ordering(_method_ordering, CHECK);
5504 // The InstanceKlass::_methods_jmethod_ids cache
5505 // is managed on the assumption that the initial cache
5506 // size is equal to the number of methods in the class. If
5507 // that changes, then InstanceKlass::idnum_can_increment()
5508 // has to be changed accordingly.
5509 ik->set_initial_method_idnum(ik->methods()->length());
5510
5511 ik->set_this_class_index(_this_class_index);
5512
5513 if (is_unsafe_anonymous()) {
5514 // _this_class_index is a CONSTANT_Class entry that refers to this
5515 // anonymous class itself. If this class needs to refer to its own methods or
5516 // fields, it would use a CONSTANT_MethodRef, etc, which would reference
5517 // _this_class_index. However, because this class is anonymous (it's
5518 // not stored in SystemDictionary), _this_class_index cannot be resolved
5519 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5520 // Therefore, we must eagerly resolve _this_class_index now.
5521 ik->constants()->klass_at_put(_this_class_index, ik);
5522 }
5523
5524 ik->set_minor_version(_minor_version);
5525 ik->set_major_version(_major_version);
5526 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5527 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5528
5529 if (_unsafe_anonymous_host != NULL) {
5530 assert (ik->is_unsafe_anonymous(), "should be the same");
5531 ik->set_unsafe_anonymous_host(_unsafe_anonymous_host);
5532 }
5533
5534 // Set PackageEntry for this_klass
5535 oop cl = ik->class_loader();
5536 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5537 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5538 ik->set_package(cld, CHECK);
5539
5540 const Array<Method*>* const methods = ik->methods();
5541 assert(methods != NULL, "invariant");
5542 const int methods_len = methods->length();
5543
5544 check_methods_for_intrinsics(ik, methods);
5545
5546 // Fill in field values obtained by parse_classfile_attributes
5547 if (_parsed_annotations->has_any_annotations()) {
5548 _parsed_annotations->apply_to(ik);
5549 }
5550
5551 apply_parsed_class_attributes(ik);
5552
5553 // Miranda methods
5554 if ((_num_miranda_methods > 0) ||
5555 // if this class introduced new miranda methods or
5556 (_super_klass != NULL && _super_klass->has_miranda_methods())
5557 // super class exists and this class inherited miranda methods
5558 ) {
5574 _field_info->nonstatic_oop_offsets,
5575 _field_info->nonstatic_oop_counts);
5576
5577 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
5578 set_precomputed_flags(ik);
5579
5580 // check if this class can access its super class
5581 check_super_class_access(ik, CHECK);
5582
5583 // check if this class can access its superinterfaces
5584 check_super_interface_access(ik, CHECK);
5585
5586 // check if this class overrides any final method
5587 check_final_method_override(ik, CHECK);
5588
5589 // reject static interface methods prior to Java 8
5590 if (ik->is_interface() && _major_version < JAVA_8_VERSION) {
5591 check_illegal_static_method(ik, CHECK);
5592 }
5593
5594 // Obtain this_klass' module entry
5595 ModuleEntry* module_entry = ik->module();
5596 assert(module_entry != NULL, "module_entry should always be set");
5597
5598 // Obtain java.lang.Module
5599 Handle module_handle(THREAD, module_entry->module());
5600
5601 // Allocate mirror and initialize static fields
5602 // The create_mirror() call will also call compute_modifiers()
5603 java_lang_Class::create_mirror(ik,
5604 Handle(THREAD, _loader_data->class_loader()),
5605 module_handle,
5606 _protection_domain,
5607 CHECK);
5608
5609 assert(_all_mirandas != NULL, "invariant");
5610
5611 // Generate any default methods - default methods are public interface methods
5612 // that have a default implementation. This is new with Java 8.
5613 if (_has_nonstatic_concrete_methods) {
5614 DefaultMethods::generate_default_methods(ik,
5615 _all_mirandas,
5616 CHECK);
5617 }
5618
5619 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5620 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5621 !module_entry->has_default_read_edges()) {
5622 if (!module_entry->set_has_default_read_edges()) {
5623 // We won a potential race
5624 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5625 }
5626 }
5660 const char * to = k->external_name();
5661 log_debug(class, resolve)("%s %s (interface)", from, to);
5662 }
5663 }
5664 }
5665 }
5666
5667 JFR_ONLY(INIT_ID(ik);)
5668
5669 // If we reach here, all is well.
5670 // Now remove the InstanceKlass* from the _klass_to_deallocate field
5671 // in order for it to not be destroyed in the ClassFileParser destructor.
5672 set_klass_to_deallocate(NULL);
5673
5674 // it's official
5675 set_klass(ik);
5676
5677 debug_only(ik->verify();)
5678 }
5679
5680 void ClassFileParser::update_class_name(Symbol* new_class_name) {
5681 // Decrement the refcount in the old name, since we're clobbering it.
5682 _class_name->decrement_refcount();
5683
5684 _class_name = new_class_name;
5685 // Increment the refcount of the new name.
5686 // Now the ClassFileParser owns this name and will decrement in
5687 // the destructor.
5688 _class_name->increment_refcount();
5689 }
5690
5691
5692 // For an unsafe anonymous class that is in the unnamed package, move it to its host class's
5693 // package by prepending its host class's package name to its class name and setting
5694 // its _class_name field.
5695 void ClassFileParser::prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS) {
5696 ResourceMark rm(THREAD);
5697 assert(strrchr(_class_name->as_C_string(), JVM_SIGNATURE_SLASH) == NULL,
5698 "Unsafe anonymous class should not be in a package");
5699 const char* host_pkg_name =
5700 ClassLoader::package_from_name(unsafe_anonymous_host->name()->as_C_string(), NULL);
5701
5702 if (host_pkg_name != NULL) {
5703 int host_pkg_len = (int)strlen(host_pkg_name);
5704 int class_name_len = _class_name->utf8_length();
5705 int symbol_len = host_pkg_len + 1 + class_name_len;
5706 char* new_anon_name = NEW_RESOURCE_ARRAY(char, symbol_len + 1);
5707 int n = os::snprintf(new_anon_name, symbol_len + 1, "%s/%.*s",
5708 host_pkg_name, class_name_len, _class_name->base());
5709 assert(n == symbol_len, "Unexpected number of characters in string");
5710
5711 // Decrement old _class_name to avoid leaking.
5740 }
5741 }
5742
5743 static bool relax_format_check_for(ClassLoaderData* loader_data) {
5744 bool trusted = (loader_data->is_the_null_class_loader_data() ||
5745 SystemDictionary::is_platform_class_loader(loader_data->class_loader()));
5746 bool need_verify =
5747 // verifyAll
5748 (BytecodeVerificationLocal && BytecodeVerificationRemote) ||
5749 // verifyRemote
5750 (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);
5751 return !need_verify;
5752 }
5753
5754 ClassFileParser::ClassFileParser(ClassFileStream* stream,
5755 Symbol* name,
5756 ClassLoaderData* loader_data,
5757 Handle protection_domain,
5758 const InstanceKlass* unsafe_anonymous_host,
5759 GrowableArray<Handle>* cp_patches,
5760 Publicity pub_level,
5761 TRAPS) :
5762 _stream(stream),
5763 _requested_name(name),
5764 _class_name(NULL),
5765 _loader_data(loader_data),
5766 _unsafe_anonymous_host(unsafe_anonymous_host),
5767 _cp_patches(cp_patches),
5768 _num_patched_klasses(0),
5769 _max_num_patched_klasses(0),
5770 _orig_cp_size(0),
5771 _first_patched_klass_resolved_index(0),
5772 _super_klass(),
5773 _cp(NULL),
5774 _fields(NULL),
5775 _methods(NULL),
5776 _inner_classes(NULL),
5777 _nest_members(NULL),
5778 _nest_host(0),
5779 _local_interfaces(NULL),
5780 _transitive_interfaces(NULL),
5781 _combined_annotations(NULL),
5782 _class_annotations(NULL),
5783 _class_type_annotations(NULL),
5784 _fields_annotations(NULL),
5785 _fields_type_annotations(NULL),
5786 _klass(NULL),
5787 _klass_to_deallocate(NULL),
5974 _major_version, _minor_version, _class_name->as_C_string());
5975 Exceptions::fthrow(
5976 THREAD_AND_LOCATION,
5977 vmSymbols::java_lang_UnsupportedClassVersionError(),
5978 "Unsupported major.minor version for dump time %u.%u",
5979 _major_version,
5980 _minor_version);
5981 }
5982
5983 // Check version numbers - we check this even with verifier off
5984 verify_class_version(_major_version, _minor_version, _class_name, CHECK);
5985
5986 stream->guarantee_more(3, CHECK); // length, first cp tag
5987 u2 cp_size = stream->get_u2_fast();
5988
5989 guarantee_property(
5990 cp_size >= 1, "Illegal constant pool size %u in class file %s",
5991 cp_size, CHECK);
5992
5993 _orig_cp_size = cp_size;
5994 if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
5995 THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
5996 }
5997 cp_size += _max_num_patched_klasses;
5998
5999 _cp = ConstantPool::allocate(_loader_data,
6000 cp_size,
6001 CHECK);
6002
6003 ConstantPool* const cp = _cp;
6004
6005 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6006
6007 assert(cp_size == (const u2)cp->length(), "invariant");
6008
6009 // ACCESS FLAGS
6010 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
6011
6012 // Access flags
6013 jint flags;
6014 // JVM_ACC_MODULE is defined in JDK-9 and later.
6015 if (_major_version >= JAVA_9_VERSION) {
6016 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
6017 } else {
6028 short bad_constant = class_bad_constant_seen();
6029 if (bad_constant != 0) {
6030 // Do not throw CFE until after the access_flags are checked because if
6031 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6032 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK);
6033 }
6034
6035 _access_flags.set_flags(flags);
6036
6037 // This class and superclass
6038 _this_class_index = stream->get_u2_fast();
6039 check_property(
6040 valid_cp_range(_this_class_index, cp_size) &&
6041 cp->tag_at(_this_class_index).is_unresolved_klass(),
6042 "Invalid this class index %u in constant pool in class file %s",
6043 _this_class_index, CHECK);
6044
6045 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
6046 assert(class_name_in_cp != NULL, "class_name can't be null");
6047
6048 // Update _class_name to reflect the name in the constant pool
6049 update_class_name(class_name_in_cp);
6050
6051 // Don't need to check whether this class name is legal or not.
6052 // It has been checked when constant pool is parsed.
6053 // However, make sure it is not an array type.
6054 if (_need_verify) {
6055 guarantee_property(_class_name->char_at(0) != JVM_SIGNATURE_ARRAY,
6056 "Bad class name in class file %s",
6057 CHECK);
6058 }
6059
6060 // Checks if name in class file matches requested name
6061 if (_requested_name != NULL && _requested_name != _class_name) {
6062 ResourceMark rm(THREAD);
6063 Exceptions::fthrow(
6064 THREAD_AND_LOCATION,
6065 vmSymbols::java_lang_NoClassDefFoundError(),
6066 "%s (wrong name: %s)",
6067 _class_name->as_C_string(),
6068 _requested_name != NULL ? _requested_name->as_C_string() : "NoName"
6069 );
6070 return;
6071 }
6072
6073 // if this is an anonymous class fix up its name if it's in the unnamed
6074 // package. Otherwise, throw IAE if it is in a different package than
6075 // its host class.
6076 if (_unsafe_anonymous_host != NULL) {
6077 fix_unsafe_anonymous_class_name(CHECK);
6078 }
6079
6080 // Verification prevents us from creating names with dots in them, this
6081 // asserts that that's the case.
6082 assert(is_internal_format(_class_name), "external class name format used internally");
6083
6084 if (!is_internal()) {
6085 LogTarget(Debug, class, preorder) lt;
6086 if (lt.is_enabled()){
6087 ResourceMark rm(THREAD);
6088 LogStream ls(lt);
6089 ls.print("%s", _class_name->as_klass_external_name());
6090 if (stream->source() != NULL) {
6091 ls.print(" source: %s", stream->source());
6092 }
6093 ls.cr();
6094 }
6095
6096 #if INCLUDE_CDS
6097 if (DumpLoadedClassList != NULL && stream->source() != NULL && classlist_file->is_open()) {
6098 if (!ClassLoader::has_jrt_entry()) {
6099 warning("DumpLoadedClassList and CDS are not supported in exploded build");
6100 DumpLoadedClassList = NULL;
6101 } else if (SystemDictionaryShared::is_sharing_possible(_loader_data) &&
6102 _unsafe_anonymous_host == NULL) {
6103 // Only dump the classes that can be stored into CDS archive.
6104 // Unsafe anonymous classes such as generated LambdaForm classes are also not included.
6105 oop class_loader = _loader_data->class_loader();
6106 ResourceMark rm(THREAD);
6107 bool skip = false;
6108 if (class_loader == NULL || SystemDictionary::is_platform_class_loader(class_loader)) {
6109 // For the boot and platform class loaders, skip classes that are not found in the
6110 // java runtime image, such as those found in the --patch-module entries.
6111 // These classes can't be loaded from the archive during runtime.
6112 if (!stream->from_boot_loader_modules_image() && strncmp(stream->source(), "jrt:", 4) != 0) {
6113 skip = true;
6114 }
6115
6116 if (class_loader == NULL && ClassLoader::contains_append_entry(stream->source())) {
6117 // .. but don't skip the boot classes that are loaded from -Xbootclasspath/a
6118 // as they can be loaded from the archive during runtime.
6119 skip = false;
6120 }
6121 }
6122 if (skip) {
6123 tty->print_cr("skip writing class %s from source %s to classlist file",
6124 _class_name->as_C_string(), stream->source());
|
1073 _method_DontInline,
1074 _method_InjectedProfile,
1075 _method_LambdaForm_Compiled,
1076 _method_Hidden,
1077 _method_HotSpotIntrinsicCandidate,
1078 _jdk_internal_vm_annotation_Contended,
1079 _field_Stable,
1080 _jdk_internal_vm_annotation_ReservedStackAccess,
1081 _annotation_LIMIT
1082 };
1083 const Location _location;
1084 int _annotations_present;
1085 u2 _contended_group;
1086
1087 AnnotationCollector(Location location)
1088 : _location(location), _annotations_present(0)
1089 {
1090 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
1091 }
1092 // If this annotation name has an ID, report it (or _none).
1093 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name, const bool can_access_vm_annotations);
1094 // Set the annotation name:
1095 void set_annotation(ID id) {
1096 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1097 _annotations_present |= nth_bit((int)id);
1098 }
1099
1100 void remove_annotation(ID id) {
1101 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1102 _annotations_present &= ~nth_bit((int)id);
1103 }
1104
1105 // Report if the annotation is present.
1106 bool has_any_annotations() const { return _annotations_present != 0; }
1107 bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; }
1108
1109 void set_contended_group(u2 group) { _contended_group = group; }
1110 u2 contended_group() const { return _contended_group; }
1111
1112 bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); }
1113
1206 int nval = Bytes::get_Java_u2((address)buffer + index - 2);
1207 while (--nval >= 0 && index < limit) {
1208 index = skip_annotation_value(buffer, limit, index);
1209 }
1210 }
1211 break;
1212 case '@':
1213 index = skip_annotation(buffer, limit, index);
1214 break;
1215 default:
1216 return limit; // bad tag byte
1217 }
1218 return index;
1219 }
1220
1221 // Sift through annotations, looking for those significant to the VM:
1222 static void parse_annotations(const ConstantPool* const cp,
1223 const u1* buffer, int limit,
1224 AnnotationCollector* coll,
1225 ClassLoaderData* loader_data,
1226 const bool can_access_vm_annotations,
1227 TRAPS) {
1228
1229 assert(cp != NULL, "invariant");
1230 assert(buffer != NULL, "invariant");
1231 assert(coll != NULL, "invariant");
1232 assert(loader_data != NULL, "invariant");
1233
1234 // annotations := do(nann:u2) {annotation}
1235 int index = 2; // read nann
1236 if (index >= limit) return;
1237 int nann = Bytes::get_Java_u2((address)buffer + index - 2);
1238 enum { // initial annotation layout
1239 atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;'
1240 count_off = 2, // u2 such as 1 (one value)
1241 member_off = 4, // utf8 such as 'value'
1242 tag_off = 6, // u1 such as 'c' (type) or 'e' (enum)
1243 e_tag_val = 'e',
1244 e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;'
1245 e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME'
1246 e_size = 11, // end of 'e' annotation
1252 s_size = 9,
1253 min_size = 6 // smallest possible size (zero members)
1254 };
1255 // Cannot add min_size to index in case of overflow MAX_INT
1256 while ((--nann) >= 0 && (index - 2 <= limit - min_size)) {
1257 int index0 = index;
1258 index = skip_annotation(buffer, limit, index);
1259 const u1* const abase = buffer + index0;
1260 const int atype = Bytes::get_Java_u2((address)abase + atype_off);
1261 const int count = Bytes::get_Java_u2((address)abase + count_off);
1262 const Symbol* const aname = check_symbol_at(cp, atype);
1263 if (aname == NULL) break; // invalid annotation name
1264 const Symbol* member = NULL;
1265 if (count >= 1) {
1266 const int member_index = Bytes::get_Java_u2((address)abase + member_off);
1267 member = check_symbol_at(cp, member_index);
1268 if (member == NULL) break; // invalid member name
1269 }
1270
1271 // Here is where parsing particular annotations will take place.
1272 AnnotationCollector::ID id = coll->annotation_index(loader_data, aname, can_access_vm_annotations);
1273 if (AnnotationCollector::_unknown == id) continue;
1274 coll->set_annotation(id);
1275
1276 if (AnnotationCollector::_jdk_internal_vm_annotation_Contended == id) {
1277 // @Contended can optionally specify the contention group.
1278 //
1279 // Contended group defines the equivalence class over the fields:
1280 // the fields within the same contended group are not treated distinct.
1281 // The only exception is default group, which does not incur the
1282 // equivalence. Naturally, contention group for classes is meaningless.
1283 //
1284 // While the contention group is specified as String, annotation
1285 // values are already interned, and we might as well use the constant
1286 // pool index as the group tag.
1287 //
1288 u2 group_index = 0; // default contended group
1289 if (count == 1
1290 && s_size == (index - index0) // match size
1291 && s_tag_val == *(abase + tag_off)
1292 && member == vmSymbols::value_name()) {
1378 if (attribute_length != 2) {
1379 classfile_parse_error(
1380 "Wrong size %u for field's Signature attribute in class file %s",
1381 attribute_length, CHECK);
1382 }
1383 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK);
1384 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
1385 if (runtime_visible_annotations != NULL) {
1386 classfile_parse_error(
1387 "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", CHECK);
1388 }
1389 runtime_visible_annotations_length = attribute_length;
1390 runtime_visible_annotations = cfs->current();
1391 assert(runtime_visible_annotations != NULL, "null visible annotations");
1392 cfs->guarantee_more(runtime_visible_annotations_length, CHECK);
1393 parse_annotations(cp,
1394 runtime_visible_annotations,
1395 runtime_visible_annotations_length,
1396 parsed_annotations,
1397 _loader_data,
1398 _can_access_vm_annotations,
1399 CHECK);
1400 cfs->skip_u1_fast(runtime_visible_annotations_length);
1401 } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
1402 if (runtime_invisible_annotations_exists) {
1403 classfile_parse_error(
1404 "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK);
1405 }
1406 runtime_invisible_annotations_exists = true;
1407 if (PreserveAllAnnotations) {
1408 runtime_invisible_annotations_length = attribute_length;
1409 runtime_invisible_annotations = cfs->current();
1410 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
1411 }
1412 cfs->skip_u1(attribute_length, CHECK);
1413 } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) {
1414 if (runtime_visible_type_annotations != NULL) {
1415 classfile_parse_error(
1416 "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK);
1417 }
1418 runtime_visible_type_annotations_length = attribute_length;
2038 }
2039 return checked_exceptions_start;
2040 }
2041
2042 void ClassFileParser::throwIllegalSignature(const char* type,
2043 const Symbol* name,
2044 const Symbol* sig,
2045 TRAPS) const {
2046 assert(name != NULL, "invariant");
2047 assert(sig != NULL, "invariant");
2048
2049 ResourceMark rm(THREAD);
2050 Exceptions::fthrow(THREAD_AND_LOCATION,
2051 vmSymbols::java_lang_ClassFormatError(),
2052 "%s \"%s\" in class %s has illegal signature \"%s\"", type,
2053 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
2054 }
2055
2056 AnnotationCollector::ID
2057 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data,
2058 const Symbol* name,
2059 const bool can_access_vm_annotations) {
2060 const vmSymbols::SID sid = vmSymbols::find_sid(name);
2061 // Privileged code can use all annotations. Other code silently drops some.
2062 const bool privileged = loader_data->is_the_null_class_loader_data() ||
2063 loader_data->is_platform_class_loader_data() ||
2064 loader_data->is_shortlived() ||
2065 can_access_vm_annotations;
2066 switch (sid) {
2067 case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): {
2068 if (_location != _in_method) break; // only allow for methods
2069 if (!privileged) break; // only allow in privileged code
2070 return _method_CallerSensitive;
2071 }
2072 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): {
2073 if (_location != _in_method) break; // only allow for methods
2074 if (!privileged) break; // only allow in privileged code
2075 return _method_ForceInline;
2076 }
2077 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): {
2078 if (_location != _in_method) break; // only allow for methods
2079 if (!privileged) break; // only allow in privileged code
2080 return _method_DontInline;
2081 }
2082 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): {
2083 if (_location != _in_method) break; // only allow for methods
2084 if (!privileged) break; // only allow in privileged code
2085 return _method_InjectedProfile;
2652 classfile_parse_error(
2653 "Invalid Signature attribute length %u in class file %s",
2654 method_attribute_length, CHECK_NULL);
2655 }
2656 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL);
2657 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
2658 if (runtime_visible_annotations != NULL) {
2659 classfile_parse_error(
2660 "Multiple RuntimeVisibleAnnotations attributes for method in class file %s",
2661 CHECK_NULL);
2662 }
2663 runtime_visible_annotations_length = method_attribute_length;
2664 runtime_visible_annotations = cfs->current();
2665 assert(runtime_visible_annotations != NULL, "null visible annotations");
2666 cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL);
2667 parse_annotations(cp,
2668 runtime_visible_annotations,
2669 runtime_visible_annotations_length,
2670 &parsed_annotations,
2671 _loader_data,
2672 _can_access_vm_annotations,
2673 CHECK_NULL);
2674 cfs->skip_u1_fast(runtime_visible_annotations_length);
2675 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
2676 if (runtime_invisible_annotations_exists) {
2677 classfile_parse_error(
2678 "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s",
2679 CHECK_NULL);
2680 }
2681 runtime_invisible_annotations_exists = true;
2682 if (PreserveAllAnnotations) {
2683 runtime_invisible_annotations_length = method_attribute_length;
2684 runtime_invisible_annotations = cfs->current();
2685 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2686 }
2687 cfs->skip_u1(method_attribute_length, CHECK_NULL);
2688 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) {
2689 if (runtime_visible_parameter_annotations != NULL) {
2690 classfile_parse_error(
2691 "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s",
2692 CHECK_NULL);
3415 if (attribute_length != 2) {
3416 classfile_parse_error(
3417 "Wrong Signature attribute length %u in class file %s",
3418 attribute_length, CHECK);
3419 }
3420 parse_classfile_signature_attribute(cfs, CHECK);
3421 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
3422 if (runtime_visible_annotations != NULL) {
3423 classfile_parse_error(
3424 "Multiple RuntimeVisibleAnnotations attributes in class file %s", CHECK);
3425 }
3426 runtime_visible_annotations_length = attribute_length;
3427 runtime_visible_annotations = cfs->current();
3428 assert(runtime_visible_annotations != NULL, "null visible annotations");
3429 cfs->guarantee_more(runtime_visible_annotations_length, CHECK);
3430 parse_annotations(cp,
3431 runtime_visible_annotations,
3432 runtime_visible_annotations_length,
3433 parsed_annotations,
3434 _loader_data,
3435 _can_access_vm_annotations,
3436 CHECK);
3437 cfs->skip_u1_fast(runtime_visible_annotations_length);
3438 } else if (tag == vmSymbols::tag_runtime_invisible_annotations()) {
3439 if (runtime_invisible_annotations_exists) {
3440 classfile_parse_error(
3441 "Multiple RuntimeInvisibleAnnotations attributes in class file %s", CHECK);
3442 }
3443 runtime_invisible_annotations_exists = true;
3444 if (PreserveAllAnnotations) {
3445 runtime_invisible_annotations_length = attribute_length;
3446 runtime_invisible_annotations = cfs->current();
3447 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
3448 }
3449 cfs->skip_u1(attribute_length, CHECK);
3450 } else if (tag == vmSymbols::tag_enclosing_method()) {
3451 if (parsed_enclosingmethod_attribute) {
3452 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
3453 } else {
3454 parsed_enclosingmethod_attribute = true;
3455 }
5406
5407 if (!match) {
5408 char buf[1000];
5409 tty->print("Compiler intrinsic is defined for method [%s], "
5410 "but the method is not available in class [%s].%s",
5411 vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID_from(id),
5412 buf, sizeof(buf)),
5413 ik->name()->as_C_string(),
5414 NOT_DEBUG("") DEBUG_ONLY(" Exiting.")
5415 );
5416 tty->cr();
5417 DEBUG_ONLY(vm_exit(1));
5418 }
5419 }
5420 } // end for
5421 } // CheckIntrinsics
5422 #endif // ASSERT
5423 }
5424 }
5425
5426 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook,
5427 const ClassInstanceInfo& cl_inst_info,
5428 TRAPS) {
5429 if (_klass != NULL) {
5430 return _klass;
5431 }
5432
5433 InstanceKlass* const ik =
5434 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL);
5435
5436 fill_instance_klass(ik, changed_by_loadhook, cl_inst_info, CHECK_NULL);
5437
5438 assert(_klass == ik, "invariant");
5439
5440
5441 if (ik->should_store_fingerprint()) {
5442 ik->store_fingerprint(_stream->compute_fingerprint());
5443 }
5444
5445 ik->set_has_passed_fingerprint_check(false);
5446 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
5447 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
5448 uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint();
5449 if (aot_fp != 0 && aot_fp == fp) {
5450 // This class matches with a class saved in an AOT library
5451 ik->set_has_passed_fingerprint_check(true);
5452 } else {
5453 ResourceMark rm;
5454 log_info(class, fingerprint)("%s : expected = " PTR64_FORMAT " actual = " PTR64_FORMAT,
5455 ik->external_name(), aot_fp, _stream->compute_fingerprint());
5456 }
5457 }
5458
5459 return ik;
5460 }
5461
5462 void ClassFileParser::fill_instance_klass(InstanceKlass* ik,
5463 bool changed_by_loadhook,
5464 const ClassInstanceInfo& cl_inst_info,
5465 TRAPS) {
5466 assert(ik != NULL, "invariant");
5467
5468 // Set name and CLD before adding to CLD
5469 ik->set_class_loader_data(_loader_data);
5470 ik->set_name(_class_name);
5471
5472 // Add all classes to our internal class loader list here,
5473 // including classes in the bootstrap (NULL) class loader.
5474 const bool publicize = !is_internal();
5475
5476 _loader_data->add_class(ik, publicize);
5477
5478 set_klass_to_deallocate(ik);
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 // can only set dynamic nest-host after static nest information is set
5502 if (cl_inst_info.dynamic_nest_host() != NULL) {
5503 ik->set_nest_host(cl_inst_info.dynamic_nest_host(), CHECK);
5504 }
5505
5506 // note that is not safe to use the fields in the parser from this point on
5507 assert(NULL == _cp, "invariant");
5508 assert(NULL == _fields, "invariant");
5509 assert(NULL == _methods, "invariant");
5510 assert(NULL == _inner_classes, "invariant");
5511 assert(NULL == _nest_members, "invariant");
5512 assert(NULL == _local_interfaces, "invariant");
5513 assert(NULL == _combined_annotations, "invariant");
5514
5515 if (_has_final_method) {
5516 ik->set_has_final_method();
5517 }
5518
5519 ik->copy_method_ordering(_method_ordering, CHECK);
5520 // The InstanceKlass::_methods_jmethod_ids cache
5521 // is managed on the assumption that the initial cache
5522 // size is equal to the number of methods in the class. If
5523 // that changes, then InstanceKlass::idnum_can_increment()
5524 // has to be changed accordingly.
5525 ik->set_initial_method_idnum(ik->methods()->length());
5526
5527 ik->set_this_class_index(_this_class_index);
5528
5529 if (_is_hidden || is_unsafe_anonymous()) {
5530 // _this_class_index is a CONSTANT_Class entry that refers to this
5531 // hidden or anonymous class itself. If this class needs to refer to its own
5532 // methods or fields, it would use a CONSTANT_MethodRef, etc, which would reference
5533 // _this_class_index. However, because this class is hidden or anonymous (it's
5534 // not stored in SystemDictionary), _this_class_index cannot be resolved
5535 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup.
5536 // Therefore, we must eagerly resolve _this_class_index now.
5537 ik->constants()->klass_at_put(_this_class_index, ik);
5538 }
5539
5540 ik->set_minor_version(_minor_version);
5541 ik->set_major_version(_major_version);
5542 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods);
5543 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods);
5544
5545 if (_unsafe_anonymous_host != NULL) {
5546 assert (ik->is_unsafe_anonymous(), "should be the same");
5547 ik->set_unsafe_anonymous_host(_unsafe_anonymous_host);
5548 }
5549 if (_is_hidden) {
5550 ik->set_is_hidden();
5551 }
5552
5553 // Set PackageEntry for this_klass
5554 oop cl = ik->class_loader();
5555 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl));
5556 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh());
5557 ik->set_package(cld, CHECK);
5558 // Obtain this_klass' module entry
5559 ModuleEntry* module_entry = ik->module();
5560 assert(module_entry != NULL, "module_entry should always be set");
5561
5562
5563 const Array<Method*>* const methods = ik->methods();
5564 assert(methods != NULL, "invariant");
5565 const int methods_len = methods->length();
5566
5567 check_methods_for_intrinsics(ik, methods);
5568
5569 // Fill in field values obtained by parse_classfile_attributes
5570 if (_parsed_annotations->has_any_annotations()) {
5571 _parsed_annotations->apply_to(ik);
5572 }
5573
5574 apply_parsed_class_attributes(ik);
5575
5576 // Miranda methods
5577 if ((_num_miranda_methods > 0) ||
5578 // if this class introduced new miranda methods or
5579 (_super_klass != NULL && _super_klass->has_miranda_methods())
5580 // super class exists and this class inherited miranda methods
5581 ) {
5597 _field_info->nonstatic_oop_offsets,
5598 _field_info->nonstatic_oop_counts);
5599
5600 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
5601 set_precomputed_flags(ik);
5602
5603 // check if this class can access its super class
5604 check_super_class_access(ik, CHECK);
5605
5606 // check if this class can access its superinterfaces
5607 check_super_interface_access(ik, CHECK);
5608
5609 // check if this class overrides any final method
5610 check_final_method_override(ik, CHECK);
5611
5612 // reject static interface methods prior to Java 8
5613 if (ik->is_interface() && _major_version < JAVA_8_VERSION) {
5614 check_illegal_static_method(ik, CHECK);
5615 }
5616
5617 // Obtain java.lang.Module
5618 Handle module_handle(THREAD, module_entry->module());
5619
5620 // Allocate mirror and initialize static fields
5621 // The create_mirror() call will also call compute_modifiers()
5622 java_lang_Class::create_mirror(ik,
5623 Handle(THREAD, _loader_data->class_loader()),
5624 module_handle,
5625 _protection_domain,
5626 cl_inst_info.class_data(),
5627 CHECK);
5628
5629 assert(_all_mirandas != NULL, "invariant");
5630
5631 // Generate any default methods - default methods are public interface methods
5632 // that have a default implementation. This is new with Java 8.
5633 if (_has_nonstatic_concrete_methods) {
5634 DefaultMethods::generate_default_methods(ik,
5635 _all_mirandas,
5636 CHECK);
5637 }
5638
5639 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5640 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5641 !module_entry->has_default_read_edges()) {
5642 if (!module_entry->set_has_default_read_edges()) {
5643 // We won a potential race
5644 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5645 }
5646 }
5680 const char * to = k->external_name();
5681 log_debug(class, resolve)("%s %s (interface)", from, to);
5682 }
5683 }
5684 }
5685 }
5686
5687 JFR_ONLY(INIT_ID(ik);)
5688
5689 // If we reach here, all is well.
5690 // Now remove the InstanceKlass* from the _klass_to_deallocate field
5691 // in order for it to not be destroyed in the ClassFileParser destructor.
5692 set_klass_to_deallocate(NULL);
5693
5694 // it's official
5695 set_klass(ik);
5696
5697 debug_only(ik->verify();)
5698 }
5699
5700 // For an unsafe anonymous class that is in the unnamed package, move it to its host class's
5701 // package by prepending its host class's package name to its class name and setting
5702 // its _class_name field.
5703 void ClassFileParser::prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS) {
5704 ResourceMark rm(THREAD);
5705 assert(strrchr(_class_name->as_C_string(), JVM_SIGNATURE_SLASH) == NULL,
5706 "Unsafe anonymous class should not be in a package");
5707 const char* host_pkg_name =
5708 ClassLoader::package_from_name(unsafe_anonymous_host->name()->as_C_string(), NULL);
5709
5710 if (host_pkg_name != NULL) {
5711 int host_pkg_len = (int)strlen(host_pkg_name);
5712 int class_name_len = _class_name->utf8_length();
5713 int symbol_len = host_pkg_len + 1 + class_name_len;
5714 char* new_anon_name = NEW_RESOURCE_ARRAY(char, symbol_len + 1);
5715 int n = os::snprintf(new_anon_name, symbol_len + 1, "%s/%.*s",
5716 host_pkg_name, class_name_len, _class_name->base());
5717 assert(n == symbol_len, "Unexpected number of characters in string");
5718
5719 // Decrement old _class_name to avoid leaking.
5748 }
5749 }
5750
5751 static bool relax_format_check_for(ClassLoaderData* loader_data) {
5752 bool trusted = (loader_data->is_the_null_class_loader_data() ||
5753 SystemDictionary::is_platform_class_loader(loader_data->class_loader()));
5754 bool need_verify =
5755 // verifyAll
5756 (BytecodeVerificationLocal && BytecodeVerificationRemote) ||
5757 // verifyRemote
5758 (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);
5759 return !need_verify;
5760 }
5761
5762 ClassFileParser::ClassFileParser(ClassFileStream* stream,
5763 Symbol* name,
5764 ClassLoaderData* loader_data,
5765 Handle protection_domain,
5766 const InstanceKlass* unsafe_anonymous_host,
5767 GrowableArray<Handle>* cp_patches,
5768 const bool is_hidden,
5769 const bool can_access_vm_annotations,
5770 Publicity pub_level,
5771 TRAPS) :
5772 _stream(stream),
5773 _class_name(NULL),
5774 _loader_data(loader_data),
5775 _unsafe_anonymous_host(unsafe_anonymous_host),
5776 _cp_patches(cp_patches),
5777 _is_hidden(is_hidden),
5778 _can_access_vm_annotations(can_access_vm_annotations),
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 _class_annotations(NULL),
5794 _class_type_annotations(NULL),
5795 _fields_annotations(NULL),
5796 _fields_type_annotations(NULL),
5797 _klass(NULL),
5798 _klass_to_deallocate(NULL),
5985 _major_version, _minor_version, _class_name->as_C_string());
5986 Exceptions::fthrow(
5987 THREAD_AND_LOCATION,
5988 vmSymbols::java_lang_UnsupportedClassVersionError(),
5989 "Unsupported major.minor version for dump time %u.%u",
5990 _major_version,
5991 _minor_version);
5992 }
5993
5994 // Check version numbers - we check this even with verifier off
5995 verify_class_version(_major_version, _minor_version, _class_name, CHECK);
5996
5997 stream->guarantee_more(3, CHECK); // length, first cp tag
5998 u2 cp_size = stream->get_u2_fast();
5999
6000 guarantee_property(
6001 cp_size >= 1, "Illegal constant pool size %u in class file %s",
6002 cp_size, CHECK);
6003
6004 _orig_cp_size = cp_size;
6005 if (is_hidden()) { // Add a slot for hidden class name.
6006 assert(_max_num_patched_klasses == 0, "Sanity check");
6007 cp_size++;
6008 } else {
6009 if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
6010 THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
6011 }
6012 cp_size += _max_num_patched_klasses;
6013 }
6014
6015 _cp = ConstantPool::allocate(_loader_data,
6016 cp_size,
6017 CHECK);
6018
6019 ConstantPool* const cp = _cp;
6020
6021 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6022
6023 assert(cp_size == (const u2)cp->length(), "invariant");
6024
6025 // ACCESS FLAGS
6026 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
6027
6028 // Access flags
6029 jint flags;
6030 // JVM_ACC_MODULE is defined in JDK-9 and later.
6031 if (_major_version >= JAVA_9_VERSION) {
6032 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
6033 } else {
6044 short bad_constant = class_bad_constant_seen();
6045 if (bad_constant != 0) {
6046 // Do not throw CFE until after the access_flags are checked because if
6047 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6048 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK);
6049 }
6050
6051 _access_flags.set_flags(flags);
6052
6053 // This class and superclass
6054 _this_class_index = stream->get_u2_fast();
6055 check_property(
6056 valid_cp_range(_this_class_index, cp_size) &&
6057 cp->tag_at(_this_class_index).is_unresolved_klass(),
6058 "Invalid this class index %u in constant pool in class file %s",
6059 _this_class_index, CHECK);
6060
6061 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
6062 assert(class_name_in_cp != NULL, "class_name can't be null");
6063
6064 // Don't need to check whether this class name is legal or not.
6065 // It has been checked when constant pool is parsed.
6066 // However, make sure it is not an array type.
6067 if (_need_verify) {
6068 guarantee_property(class_name_in_cp->char_at(0) != JVM_SIGNATURE_ARRAY,
6069 "Bad class name in class file %s",
6070 CHECK);
6071 }
6072
6073 #ifdef ASSERT
6074 // Basic sanity checks
6075 assert(!(_is_hidden && (_unsafe_anonymous_host != NULL)), "mutually exclusive variants");
6076
6077 if (_unsafe_anonymous_host != NULL) {
6078 assert(_class_name == vmSymbols::unknown_class_name(), "A named anonymous class???");
6079 }
6080 if (_is_hidden) {
6081 assert(_class_name != vmSymbols::unknown_class_name(), "hidden classes should have a special name");
6082 }
6083 #endif
6084
6085 // Update the _class_name as needed depending on whether this is a named,
6086 // un-named, hidden or unsafe-anonymous class.
6087
6088 if (_is_hidden) {
6089 ResourceMark rm(THREAD);
6090 assert(_class_name != NULL, "Unexpected null _class_name");
6091 #ifdef ASSERT
6092 if (_need_verify) {
6093 verify_legal_class_name(_class_name, CHECK);
6094 }
6095 #endif
6096
6097 // Construct hidden name from _class_name, "+", and &_cp. Note that we can't
6098 // use a '/' because that confuses finding the class's package. Also, can't
6099 // use an illegal char such as ';' because that causes serialization issues
6100 // and issues with hidden classes that create their own hidden classes.
6101 char addr_buf[20];
6102 jio_snprintf(addr_buf, 20, INTPTR_FORMAT, p2i(_cp));
6103 size_t new_name_len = _class_name->utf8_length() + 2 + strlen(addr_buf);
6104 char* new_name = NEW_RESOURCE_ARRAY(char, new_name_len);
6105 jio_snprintf(new_name, new_name_len, "%s+%s",
6106 _class_name->as_C_string(), addr_buf);
6107 _class_name->decrement_refcount();
6108 _class_name = SymbolTable::new_symbol(new_name);
6109 _class_name->increment_refcount();
6110
6111 // Add a Utf8 entry containing the hidden name.
6112 assert(_class_name != NULL, "Unexpected null _class_name");
6113 int hidden_index = _orig_cp_size; // this is an extra slot we added
6114 cp->symbol_at_put(hidden_index, _class_name);
6115
6116 // Update this_class_index's slot in the constant pool with the new Utf8 entry.
6117 // We have to update the resolved_klass_index and the name_index together
6118 // so extract the existing resolved_klass_index first.
6119 CPKlassSlot cp_klass_slot = cp->klass_slot_at(_this_class_index);
6120 int resolved_klass_index = cp_klass_slot.resolved_klass_index();
6121 cp->unresolved_klass_at_put(_this_class_index, hidden_index, resolved_klass_index);
6122 assert(cp->klass_slot_at(_this_class_index).name_index() == _orig_cp_size,
6123 "Bad name_index");
6124
6125 // NOTE: !_is_hidden does not imply "findable" as it could be an old-style
6126 // "hidden" unsafe-anonymous class
6127
6128 // If this is an anonymous class fix up its name if it is in the unnamed
6129 // package. Otherwise, throw IAE if it is in a different package than
6130 // its host class.
6131 } else if (_unsafe_anonymous_host != NULL) {
6132 Symbol* old_class_name = _class_name;
6133 _class_name = class_name_in_cp;
6134 _class_name->increment_refcount();
6135 fix_unsafe_anonymous_class_name(CHECK);
6136 old_class_name->decrement_refcount();
6137
6138 } else {
6139 // Check if name in class file matches given name
6140 if (_class_name != class_name_in_cp) {
6141 if (_class_name != vmSymbols::unknown_class_name()) {
6142 ResourceMark rm(THREAD);
6143 Exceptions::fthrow(THREAD_AND_LOCATION,
6144 vmSymbols::java_lang_NoClassDefFoundError(),
6145 "%s (wrong name: %s)",
6146 class_name_in_cp->as_C_string(),
6147 _class_name->as_C_string()
6148 );
6149 return;
6150 } else {
6151 // The class name was not known by the caller so we set it from
6152 // the value in the CP.
6153 _class_name = class_name_in_cp;
6154 _class_name->increment_refcount();
6155 }
6156 // else nothing to do: the expected class name matches what is in the CP
6157 }
6158 }
6159
6160 // Verification prevents us from creating names with dots in them, this
6161 // asserts that that's the case.
6162 assert(is_internal_format(_class_name), "external class name format used internally");
6163
6164 if (!is_internal()) {
6165 LogTarget(Debug, class, preorder) lt;
6166 if (lt.is_enabled()){
6167 ResourceMark rm(THREAD);
6168 LogStream ls(lt);
6169 ls.print("%s", _class_name->as_klass_external_name());
6170 if (stream->source() != NULL) {
6171 ls.print(" source: %s", stream->source());
6172 }
6173 ls.cr();
6174 }
6175
6176 #if INCLUDE_CDS
6177 if (DumpLoadedClassList != NULL && stream->source() != NULL && classlist_file->is_open()) {
6178 if (!ClassLoader::has_jrt_entry()) {
6179 warning("DumpLoadedClassList and CDS are not supported in exploded build");
6180 DumpLoadedClassList = NULL;
6181 } else if (SystemDictionaryShared::is_sharing_possible(_loader_data) &&
6182 !_is_hidden &&
6183 _unsafe_anonymous_host == NULL) {
6184 // Only dump the classes that can be stored into CDS archive.
6185 // Hidden and unsafe anonymous classes such as generated LambdaForm classes are also not included.
6186 oop class_loader = _loader_data->class_loader();
6187 ResourceMark rm(THREAD);
6188 bool skip = false;
6189 if (class_loader == NULL || SystemDictionary::is_platform_class_loader(class_loader)) {
6190 // For the boot and platform class loaders, skip classes that are not found in the
6191 // java runtime image, such as those found in the --patch-module entries.
6192 // These classes can't be loaded from the archive during runtime.
6193 if (!stream->from_boot_loader_modules_image() && strncmp(stream->source(), "jrt:", 4) != 0) {
6194 skip = true;
6195 }
6196
6197 if (class_loader == NULL && ClassLoader::contains_append_entry(stream->source())) {
6198 // .. but don't skip the boot classes that are loaded from -Xbootclasspath/a
6199 // as they can be loaded from the archive during runtime.
6200 skip = false;
6201 }
6202 }
6203 if (skip) {
6204 tty->print_cr("skip writing class %s from source %s to classlist file",
6205 _class_name->as_C_string(), stream->source());
|