comparison src/share/vm/classfile/classFileParser.cpp @ 20804:7848fc12602b

Merge with jdk8u40-b25
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Tue, 07 Apr 2015 14:58:49 +0200
parents 18e21237807b 0fa1f71a905b
children 1da7aef31a08
comparison
equal deleted inserted replaced
20184:84105dcdb05b 20804:7848fc12602b
29 #include "classfile/classLoaderData.inline.hpp" 29 #include "classfile/classLoaderData.inline.hpp"
30 #include "classfile/defaultMethods.hpp" 30 #include "classfile/defaultMethods.hpp"
31 #include "classfile/javaClasses.hpp" 31 #include "classfile/javaClasses.hpp"
32 #include "classfile/symbolTable.hpp" 32 #include "classfile/symbolTable.hpp"
33 #include "classfile/systemDictionary.hpp" 33 #include "classfile/systemDictionary.hpp"
34 #if INCLUDE_CDS
35 #include "classfile/systemDictionaryShared.hpp"
36 #endif
34 #include "classfile/verificationType.hpp" 37 #include "classfile/verificationType.hpp"
35 #include "classfile/verifier.hpp" 38 #include "classfile/verifier.hpp"
36 #include "classfile/vmSymbols.hpp" 39 #include "classfile/vmSymbols.hpp"
37 #include "memory/allocation.hpp" 40 #include "memory/allocation.hpp"
38 #include "memory/gcLocker.hpp" 41 #include "memory/gcLocker.hpp"
58 #include "runtime/timer.hpp" 61 #include "runtime/timer.hpp"
59 #include "services/classLoadingService.hpp" 62 #include "services/classLoadingService.hpp"
60 #include "services/threadService.hpp" 63 #include "services/threadService.hpp"
61 #include "utilities/array.hpp" 64 #include "utilities/array.hpp"
62 #include "utilities/globalDefinitions.hpp" 65 #include "utilities/globalDefinitions.hpp"
66 #include "utilities/ostream.hpp"
63 67
64 // We generally try to create the oops directly when parsing, rather than 68 // We generally try to create the oops directly when parsing, rather than
65 // allocating temporary data structures and copying the bytes twice. A 69 // allocating temporary data structures and copying the bytes twice. A
66 // temporary area is only needed when parsing utf8 entries in the constant 70 // temporary area is only needed when parsing utf8 entries in the constant
67 // pool and when parsing line number tables. 71 // pool and when parsing line number tables.
2529 // are added to klass's access_flags. 2533 // are added to klass's access_flags.
2530 2534
2531 Array<Method*>* ClassFileParser::parse_methods(bool is_interface, 2535 Array<Method*>* ClassFileParser::parse_methods(bool is_interface,
2532 AccessFlags* promoted_flags, 2536 AccessFlags* promoted_flags,
2533 bool* has_final_method, 2537 bool* has_final_method,
2534 bool* has_default_methods, 2538 bool* declares_default_methods,
2535 TRAPS) { 2539 TRAPS) {
2536 ClassFileStream* cfs = stream(); 2540 ClassFileStream* cfs = stream();
2537 cfs->guarantee_more(2, CHECK_NULL); // length 2541 cfs->guarantee_more(2, CHECK_NULL); // length
2538 u2 length = cfs->get_u2_fast(); 2542 u2 length = cfs->get_u2_fast();
2539 if (length == 0) { 2543 if (length == 0) {
2548 CHECK_NULL); 2552 CHECK_NULL);
2549 2553
2550 if (method->is_final()) { 2554 if (method->is_final()) {
2551 *has_final_method = true; 2555 *has_final_method = true;
2552 } 2556 }
2553 if (is_interface && !(*has_default_methods) 2557 // declares_default_methods: declares concrete instance methods, any access flags
2554 && !method->is_abstract() && !method->is_static() 2558 // used for interface initialization, and default method inheritance analysis
2555 && !method->is_private()) { 2559 if (is_interface && !(*declares_default_methods)
2556 // default method 2560 && !method->is_abstract() && !method->is_static()) {
2557 *has_default_methods = true; 2561 *declares_default_methods = true;
2558 } 2562 }
2559 _methods->at_put(index, method()); 2563 _methods->at_put(index, method());
2560 } 2564 }
2561 2565
2562 if (_need_verify && length > 1) { 2566 if (_need_verify && length > 1) {
2784 2788
2785 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(u4 attribute_byte_length, TRAPS) { 2789 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(u4 attribute_byte_length, TRAPS) {
2786 ClassFileStream* cfs = stream(); 2790 ClassFileStream* cfs = stream();
2787 u1* current_start = cfs->current(); 2791 u1* current_start = cfs->current();
2788 2792
2789 cfs->guarantee_more(attribute_byte_length, CHECK);
2790
2791 int attribute_array_length = cfs->get_u2_fast();
2792
2793 guarantee_property(_max_bootstrap_specifier_index < attribute_array_length,
2794 "Short length on BootstrapMethods in class file %s",
2795 CHECK);
2796
2797 guarantee_property(attribute_byte_length >= sizeof(u2), 2793 guarantee_property(attribute_byte_length >= sizeof(u2),
2798 "Invalid BootstrapMethods attribute length %u in class file %s", 2794 "Invalid BootstrapMethods attribute length %u in class file %s",
2799 attribute_byte_length, 2795 attribute_byte_length,
2796 CHECK);
2797
2798 cfs->guarantee_more(attribute_byte_length, CHECK);
2799
2800 int attribute_array_length = cfs->get_u2_fast();
2801
2802 guarantee_property(_max_bootstrap_specifier_index < attribute_array_length,
2803 "Short length on BootstrapMethods in class file %s",
2800 CHECK); 2804 CHECK);
2801 2805
2802 // The attribute contains a counted array of counted tuples of shorts, 2806 // The attribute contains a counted array of counted tuples of shorts,
2803 // represending bootstrap specifiers: 2807 // represending bootstrap specifiers:
2804 // length*{bootstrap_method_index, argument_count*{argument_index}} 2808 // length*{bootstrap_method_index, argument_count*{argument_index}}
3058 if (_sde_buffer != NULL) { 3062 if (_sde_buffer != NULL) {
3059 k->set_source_debug_extension(_sde_buffer, _sde_length); 3063 k->set_source_debug_extension(_sde_buffer, _sde_length);
3060 } 3064 }
3061 } 3065 }
3062 3066
3063 // Transfer ownership of metadata allocated to the InstanceKlass. 3067 // Create the Annotations object that will
3064 void ClassFileParser::apply_parsed_class_metadata( 3068 // hold the annotations array for the Klass.
3065 instanceKlassHandle this_klass, 3069 void ClassFileParser::create_combined_annotations(TRAPS) {
3066 int java_fields_count, TRAPS) { 3070 if (_annotations == NULL &&
3067 // Assign annotations if needed 3071 _type_annotations == NULL &&
3068 if (_annotations != NULL || _type_annotations != NULL || 3072 _fields_annotations == NULL &&
3069 _fields_annotations != NULL || _fields_type_annotations != NULL) { 3073 _fields_type_annotations == NULL) {
3074 // Don't create the Annotations object unnecessarily.
3075 return;
3076 }
3077
3070 Annotations* annotations = Annotations::allocate(_loader_data, CHECK); 3078 Annotations* annotations = Annotations::allocate(_loader_data, CHECK);
3071 annotations->set_class_annotations(_annotations); 3079 annotations->set_class_annotations(_annotations);
3072 annotations->set_class_type_annotations(_type_annotations); 3080 annotations->set_class_type_annotations(_type_annotations);
3073 annotations->set_fields_annotations(_fields_annotations); 3081 annotations->set_fields_annotations(_fields_annotations);
3074 annotations->set_fields_type_annotations(_fields_type_annotations); 3082 annotations->set_fields_type_annotations(_fields_type_annotations);
3075 this_klass->set_annotations(annotations); 3083
3076 } 3084 // This is the Annotations object that will be
3077 3085 // assigned to InstanceKlass being constructed.
3086 _combined_annotations = annotations;
3087
3088 // The annotations arrays below has been transfered the
3089 // _combined_annotations so these fields can now be cleared.
3090 _annotations = NULL;
3091 _type_annotations = NULL;
3092 _fields_annotations = NULL;
3093 _fields_type_annotations = NULL;
3094 }
3095
3096 // Transfer ownership of metadata allocated to the InstanceKlass.
3097 void ClassFileParser::apply_parsed_class_metadata(
3098 instanceKlassHandle this_klass,
3099 int java_fields_count, TRAPS) {
3078 _cp->set_pool_holder(this_klass()); 3100 _cp->set_pool_holder(this_klass());
3079 this_klass->set_constants(_cp); 3101 this_klass->set_constants(_cp);
3080 this_klass->set_fields(_fields, java_fields_count); 3102 this_klass->set_fields(_fields, java_fields_count);
3081 this_klass->set_methods(_methods); 3103 this_klass->set_methods(_methods);
3082 this_klass->set_inner_classes(_inner_classes); 3104 this_klass->set_inner_classes(_inner_classes);
3083 this_klass->set_local_interfaces(_local_interfaces); 3105 this_klass->set_local_interfaces(_local_interfaces);
3084 this_klass->set_transitive_interfaces(_transitive_interfaces); 3106 this_klass->set_transitive_interfaces(_transitive_interfaces);
3107 this_klass->set_annotations(_combined_annotations);
3085 3108
3086 // Clear out these fields so they don't get deallocated by the destructor 3109 // Clear out these fields so they don't get deallocated by the destructor
3087 clear_class_metadata(); 3110 clear_class_metadata();
3088 } 3111 }
3089 3112
3691 // agent attached, then the cached class bytes may not be the 3714 // agent attached, then the cached class bytes may not be the
3692 // original class bytes. 3715 // original class bytes.
3693 JvmtiCachedClassFileData *cached_class_file = NULL; 3716 JvmtiCachedClassFileData *cached_class_file = NULL;
3694 Handle class_loader(THREAD, loader_data->class_loader()); 3717 Handle class_loader(THREAD, loader_data->class_loader());
3695 bool has_default_methods = false; 3718 bool has_default_methods = false;
3719 bool declares_default_methods = false;
3696 ResourceMark rm(THREAD); 3720 ResourceMark rm(THREAD);
3697 3721
3698 ClassFileStream* cfs = stream(); 3722 ClassFileStream* cfs = stream();
3699 // Timing 3723 // Timing
3700 assert(THREAD->is_Java_thread(), "must be a JavaThread"); 3724 assert(THREAD->is_Java_thread(), "must be a JavaThread");
3745 _cp_patches = cp_patches; 3769 _cp_patches = cp_patches;
3746 3770
3747 instanceKlassHandle nullHandle; 3771 instanceKlassHandle nullHandle;
3748 3772
3749 // Figure out whether we can skip format checking (matching classic VM behavior) 3773 // Figure out whether we can skip format checking (matching classic VM behavior)
3750 _need_verify = Verifier::should_verify_for(class_loader(), verify); 3774 if (DumpSharedSpaces) {
3775 // verify == true means it's a 'remote' class (i.e., non-boot class)
3776 // Verification decision is based on BytecodeVerificationRemote flag
3777 // for those classes.
3778 _need_verify = (verify) ? BytecodeVerificationRemote :
3779 BytecodeVerificationLocal;
3780 } else {
3781 _need_verify = Verifier::should_verify_for(class_loader(), verify);
3782 }
3751 3783
3752 // Set the verify flag in stream 3784 // Set the verify flag in stream
3753 cfs->set_verify(_need_verify); 3785 cfs->set_verify(_need_verify);
3754 3786
3755 // Save the class file name for easier error message printing. 3787 // Save the class file name for easier error message printing.
3763 magic, CHECK_(nullHandle)); 3795 magic, CHECK_(nullHandle));
3764 3796
3765 // Version numbers 3797 // Version numbers
3766 u2 minor_version = cfs->get_u2_fast(); 3798 u2 minor_version = cfs->get_u2_fast();
3767 u2 major_version = cfs->get_u2_fast(); 3799 u2 major_version = cfs->get_u2_fast();
3800
3801 if (DumpSharedSpaces && major_version < JAVA_1_5_VERSION) {
3802 ResourceMark rm;
3803 warning("Pre JDK 1.5 class not supported by CDS: %u.%u %s",
3804 major_version, minor_version, name->as_C_string());
3805 Exceptions::fthrow(
3806 THREAD_AND_LOCATION,
3807 vmSymbols::java_lang_UnsupportedClassVersionError(),
3808 "Unsupported major.minor version for dump time %u.%u",
3809 major_version,
3810 minor_version);
3811 }
3768 3812
3769 // Check version numbers - we check this even with verifier off 3813 // Check version numbers - we check this even with verifier off
3770 if (!is_supported_version(major_version, minor_version)) { 3814 if (!is_supported_version(major_version, minor_version)) {
3771 if (name == NULL) { 3815 if (name == NULL) {
3772 Exceptions::fthrow( 3816 Exceptions::fthrow(
3871 if (TraceClassLoadingPreorder) { 3915 if (TraceClassLoadingPreorder) {
3872 tty->print("[Loading %s", (name != NULL) ? name->as_klass_external_name() : "NoName"); 3916 tty->print("[Loading %s", (name != NULL) ? name->as_klass_external_name() : "NoName");
3873 if (cfs->source() != NULL) tty->print(" from %s", cfs->source()); 3917 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
3874 tty->print_cr("]"); 3918 tty->print_cr("]");
3875 } 3919 }
3920 #if INCLUDE_CDS
3921 if (DumpLoadedClassList != NULL && cfs->source() != NULL && classlist_file->is_open()) {
3922 // Only dump the classes that can be stored into CDS archive
3923 if (SystemDictionaryShared::is_sharing_possible(loader_data)) {
3924 if (name != NULL) {
3925 ResourceMark rm(THREAD);
3926 classlist_file->print_cr("%s", name->as_C_string());
3927 classlist_file->flush();
3928 }
3929 }
3930 }
3931 #endif
3876 3932
3877 u2 super_class_index = cfs->get_u2_fast(); 3933 u2 super_class_index = cfs->get_u2_fast();
3878 instanceKlassHandle super_klass = parse_super_class(super_class_index, 3934 instanceKlassHandle super_klass = parse_super_class(super_class_index,
3879 CHECK_NULL); 3935 CHECK_NULL);
3880 3936
3896 AccessFlags promoted_flags; 3952 AccessFlags promoted_flags;
3897 promoted_flags.set_flags(0); 3953 promoted_flags.set_flags(0);
3898 Array<Method*>* methods = parse_methods(access_flags.is_interface(), 3954 Array<Method*>* methods = parse_methods(access_flags.is_interface(),
3899 &promoted_flags, 3955 &promoted_flags,
3900 &has_final_method, 3956 &has_final_method,
3901 &has_default_methods, 3957 &declares_default_methods,
3902 CHECK_(nullHandle)); 3958 CHECK_(nullHandle));
3959 if (declares_default_methods) {
3960 has_default_methods = true;
3961 }
3903 3962
3904 // Additional attributes 3963 // Additional attributes
3905 ClassAnnotationCollector parsed_annotations; 3964 ClassAnnotationCollector parsed_annotations;
3906 parse_classfile_attributes(&parsed_annotations, CHECK_(nullHandle)); 3965 parse_classfile_attributes(&parsed_annotations, CHECK_(nullHandle));
3966
3967 // Finalize the Annotations metadata object,
3968 // now that all annotation arrays have been created.
3969 create_combined_annotations(CHECK_(nullHandle));
3907 3970
3908 // Make sure this is the end of class file stream 3971 // Make sure this is the end of class file stream
3909 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle)); 3972 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3910 3973
3911 // We check super class after class file is parsed and format is checked 3974 // We check super class after class file is parsed and format is checked
4040 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve 4103 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
4041 4104
4042 this_klass->set_minor_version(minor_version); 4105 this_klass->set_minor_version(minor_version);
4043 this_klass->set_major_version(major_version); 4106 this_klass->set_major_version(major_version);
4044 this_klass->set_has_default_methods(has_default_methods); 4107 this_klass->set_has_default_methods(has_default_methods);
4108 this_klass->set_declares_default_methods(declares_default_methods);
4045 4109
4046 if (!host_klass.is_null()) { 4110 if (!host_klass.is_null()) {
4047 assert (this_klass->is_anonymous(), "should be the same"); 4111 assert (this_klass->is_anonymous(), "should be the same");
4048 this_klass->set_host_klass(host_klass()); 4112 this_klass->set_host_klass(host_klass());
4049 } 4113 }
4110 check_illegal_static_method(this_klass, CHECK_(nullHandle)); 4174 check_illegal_static_method(this_klass, CHECK_(nullHandle));
4111 } 4175 }
4112 } 4176 }
4113 4177
4114 // Allocate mirror and initialize static fields 4178 // Allocate mirror and initialize static fields
4115 java_lang_Class::create_mirror(this_klass, protection_domain, CHECK_(nullHandle)); 4179 java_lang_Class::create_mirror(this_klass, class_loader, protection_domain,
4116 4180 CHECK_(nullHandle));
4117 4181
4118 // Generate any default methods - default methods are interface methods 4182 // Generate any default methods - default methods are interface methods
4119 // that have a default implementation. This is new with Lambda project. 4183 // that have a default implementation. This is new with Lambda project.
4120 if (has_default_methods ) { 4184 if (has_default_methods ) {
4121 DefaultMethods::generate_default_methods( 4185 DefaultMethods::generate_default_methods(
4133 // print in a single call to reduce interleaving of output 4197 // print in a single call to reduce interleaving of output
4134 if (cfs->source() != NULL) { 4198 if (cfs->source() != NULL) {
4135 tty->print("[Loaded %s from %s]\n", this_klass->external_name(), 4199 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
4136 cfs->source()); 4200 cfs->source());
4137 } else if (class_loader.is_null()) { 4201 } else if (class_loader.is_null()) {
4138 if (THREAD->is_Java_thread()) { 4202 Klass* caller =
4139 Klass* caller = ((JavaThread*)THREAD)->security_get_caller_class(1); 4203 THREAD->is_Java_thread()
4204 ? ((JavaThread*)THREAD)->security_get_caller_class(1)
4205 : NULL;
4206 // caller can be NULL, for example, during a JVMTI VM_Init hook
4207 if (caller != NULL) {
4140 tty->print("[Loaded %s by instance of %s]\n", 4208 tty->print("[Loaded %s by instance of %s]\n",
4141 this_klass->external_name(), 4209 this_klass->external_name(),
4142 InstanceKlass::cast(caller)->external_name()); 4210 InstanceKlass::cast(caller)->external_name());
4143 } else { 4211 } else {
4144 tty->print("[Loaded %s]\n", this_klass->external_name()); 4212 tty->print("[Loaded %s]\n", this_klass->external_name());
4202 4270
4203 // Free interfaces 4271 // Free interfaces
4204 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass(), 4272 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass(),
4205 _local_interfaces, _transitive_interfaces); 4273 _local_interfaces, _transitive_interfaces);
4206 4274
4207 MetadataFactory::free_array<u1>(_loader_data, _annotations); 4275 if (_combined_annotations != NULL) {
4208 MetadataFactory::free_array<u1>(_loader_data, _type_annotations); 4276 // After all annotations arrays have been created, they are installed into the
4209 Annotations::free_contents(_loader_data, _fields_annotations); 4277 // Annotations object that will be assigned to the InstanceKlass being created.
4210 Annotations::free_contents(_loader_data, _fields_type_annotations); 4278
4279 // Deallocate the Annotations object and the installed annotations arrays.
4280 _combined_annotations->deallocate_contents(_loader_data);
4281
4282 // If the _combined_annotations pointer is non-NULL,
4283 // then the other annotations fields should have been cleared.
4284 assert(_annotations == NULL, "Should have been cleared");
4285 assert(_type_annotations == NULL, "Should have been cleared");
4286 assert(_fields_annotations == NULL, "Should have been cleared");
4287 assert(_fields_type_annotations == NULL, "Should have been cleared");
4288 } else {
4289 // If the annotations arrays were not installed into the Annotations object,
4290 // then they have to be deallocated explicitly.
4291 MetadataFactory::free_array<u1>(_loader_data, _annotations);
4292 MetadataFactory::free_array<u1>(_loader_data, _type_annotations);
4293 Annotations::free_contents(_loader_data, _fields_annotations);
4294 Annotations::free_contents(_loader_data, _fields_type_annotations);
4295 }
4211 4296
4212 clear_class_metadata(); 4297 clear_class_metadata();
4213 4298
4214 // deallocate the klass if already created. Don't directly deallocate, but add 4299 // deallocate the klass if already created. Don't directly deallocate, but add
4215 // to the deallocate list so that the klass is removed from the CLD::_klasses list 4300 // to the deallocate list so that the klass is removed from the CLD::_klasses list