5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #include "precompiled.hpp"
25 #include "classfile/classFileParser.hpp"
26 #include "classfile/classFileStream.hpp"
27 #include "classfile/classLoader.hpp"
28 #include "classfile/classLoaderData.inline.hpp"
29 #include "classfile/classLoadInfo.hpp"
30 #include "classfile/defaultMethods.hpp"
31 #include "classfile/fieldLayoutBuilder.hpp"
32 #include "classfile/javaClasses.inline.hpp"
33 #include "classfile/moduleEntry.hpp"
34 #include "classfile/packageEntry.hpp"
35 #include "classfile/symbolTable.hpp"
36 #include "classfile/systemDictionary.hpp"
37 #include "classfile/verificationType.hpp"
38 #include "classfile/verifier.hpp"
39 #include "classfile/vmClasses.hpp"
40 #include "classfile/vmSymbols.hpp"
41 #include "jvm.h"
42 #include "logging/log.hpp"
43 #include "logging/logStream.hpp"
44 #include "memory/allocation.hpp"
2835 const Method* const m = _methods->at(i);
2836 NameSigHash name_and_sig(m->name(), m->signature());
2837 // If no duplicates, add name/signature in hashtable names_and_sigs.
2838 if(!names_and_sigs->put(name_and_sig, 0)) {
2839 classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s",
2840 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
2841 return;
2842 }
2843 }
2844 }
2845 }
2846 }
2847
2848 static const intArray* sort_methods(Array<Method*>* methods) {
2849 const int length = methods->length();
2850 // If JVMTI original method ordering or sharing is enabled we have to
2851 // remember the original class file ordering.
2852 // We temporarily use the vtable_index field in the Method* to store the
2853 // class file index, so we can read in after calling qsort.
2854 // Put the method ordering in the shared archive.
2855 if (JvmtiExport::can_maintain_original_method_order() || Arguments::is_dumping_archive()) {
2856 for (int index = 0; index < length; index++) {
2857 Method* const m = methods->at(index);
2858 assert(!m->valid_vtable_index(), "vtable index should not be set");
2859 m->set_vtable_index(index);
2860 }
2861 }
2862 // Sort method array by ascending method name (for faster lookups & vtable construction)
2863 // Note that the ordering is not alphabetical, see Symbol::fast_compare
2864 Method::sort_methods(methods);
2865
2866 intArray* method_ordering = nullptr;
2867 // If JVMTI original method ordering or sharing is enabled construct int
2868 // array remembering the original ordering
2869 if (JvmtiExport::can_maintain_original_method_order() || Arguments::is_dumping_archive()) {
2870 method_ordering = new intArray(length, length, -1);
2871 for (int index = 0; index < length; index++) {
2872 Method* const m = methods->at(index);
2873 const int old_index = m->vtable_index();
2874 assert(old_index >= 0 && old_index < length, "invalid method index");
2875 method_ordering->at_put(index, old_index);
2876 m->set_vtable_index(Method::invalid_vtable_index);
2877 }
2878 }
2879 return method_ordering;
2880 }
2881
2882 // Parse generic_signature attribute for methods and fields
2883 u2 ClassFileParser::parse_generic_signature_attribute(const ClassFileStream* const cfs,
2884 TRAPS) {
2885 assert(cfs != nullptr, "invariant");
2886
2887 cfs->guarantee_more(2, CHECK_0); // generic_signature_index
2888 const u2 generic_signature_index = cfs->get_u2_fast();
2889 check_property(
4307 }
4308
4309 Reflection::VerifyClassAccessResults vca_result =
4310 Reflection::verify_class_access(this_klass, k, false);
4311 if (vca_result != Reflection::ACCESS_OK) {
4312 ResourceMark rm(THREAD);
4313 char* msg = Reflection::verify_class_access_msg(this_klass,
4314 k,
4315 vca_result);
4316 if (msg == nullptr) {
4317 bool same_module = (this_klass->module() == k->module());
4318 Exceptions::fthrow(
4319 THREAD_AND_LOCATION,
4320 vmSymbols::java_lang_IllegalAccessError(),
4321 "class %s cannot access its superinterface %s (%s%s%s)",
4322 this_klass->external_name(),
4323 k->external_name(),
4324 (same_module) ? this_klass->joint_in_module_of_loader(k) : this_klass->class_in_module_of_loader(),
4325 (same_module) ? "" : "; ",
4326 (same_module) ? "" : k->class_in_module_of_loader());
4327 } else {
4328 // Add additional message content.
4329 Exceptions::fthrow(
4330 THREAD_AND_LOCATION,
4331 vmSymbols::java_lang_IllegalAccessError(),
4332 "superinterface check failed: %s",
4333 msg);
4334 }
4335 }
4336 }
4337 }
4338
4339
4340 static void check_final_method_override(const InstanceKlass* this_klass, TRAPS) {
4341 assert(this_klass != nullptr, "invariant");
4342 const Array<Method*>* const methods = this_klass->methods();
4343 const int num_methods = methods->length();
4344
4345 // go thru each method and check if it overrides a final method
4346 for (int index = 0; index < num_methods; index++) {
4347 const Method* const m = methods->at(index);
4348
4349 // skip private, static, and <init> methods
4350 if ((!m->is_private() && !m->is_static()) &&
4351 (m->name() != vmSymbols::object_initializer_name())) {
4352
4353 const Symbol* const name = m->name();
5190 _loader_data->add_class(ik, publicize);
5191
5192 set_klass_to_deallocate(ik);
5193
5194 assert(_field_info != nullptr, "invariant");
5195 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5196 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5197 "sanity");
5198
5199 assert(ik->is_instance_klass(), "sanity");
5200 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5201
5202 // Fill in information already parsed
5203 ik->set_should_verify_class(_need_verify);
5204
5205 // Not yet: supers are done below to support the new subtype-checking fields
5206 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5207 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5208 assert(_fac != nullptr, "invariant");
5209 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5210
5211 // this transfers ownership of a lot of arrays from
5212 // the parser onto the InstanceKlass*
5213 apply_parsed_class_metadata(ik, _java_fields_count);
5214
5215 // can only set dynamic nest-host after static nest information is set
5216 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5217 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5218 }
5219
5220 // note that is not safe to use the fields in the parser from this point on
5221 assert(nullptr == _cp, "invariant");
5222 assert(nullptr == _fieldinfo_stream, "invariant");
5223 assert(nullptr == _fields_status, "invariant");
5224 assert(nullptr == _methods, "invariant");
5225 assert(nullptr == _inner_classes, "invariant");
5226 assert(nullptr == _nest_members, "invariant");
5227 assert(nullptr == _combined_annotations, "invariant");
5228 assert(nullptr == _record_components, "invariant");
5229 assert(nullptr == _permitted_subclasses, "invariant");
5499 _declares_nonstatic_concrete_methods(false),
5500 _has_localvariable_table(false),
5501 _has_final_method(false),
5502 _has_contended_fields(false),
5503 _has_finalizer(false),
5504 _has_empty_finalizer(false),
5505 _has_vanilla_constructor(false),
5506 _max_bootstrap_specifier_index(-1) {
5507
5508 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5509 _class_name->increment_refcount();
5510
5511 assert(_loader_data != nullptr, "invariant");
5512 assert(stream != nullptr, "invariant");
5513 assert(_stream != nullptr, "invariant");
5514 assert(_stream->buffer() == _stream->current(), "invariant");
5515 assert(_class_name != nullptr, "invariant");
5516 assert(0 == _access_flags.as_int(), "invariant");
5517
5518 // Figure out whether we can skip format checking (matching classic VM behavior)
5519 if (DumpSharedSpaces) {
5520 // verify == true means it's a 'remote' class (i.e., non-boot class)
5521 // Verification decision is based on BytecodeVerificationRemote flag
5522 // for those classes.
5523 _need_verify = (stream->need_verify()) ? BytecodeVerificationRemote :
5524 BytecodeVerificationLocal;
5525 }
5526 else {
5527 _need_verify = Verifier::should_verify_for(_loader_data->class_loader(),
5528 stream->need_verify());
5529 }
5530
5531 // synch back verification state to stream
5532 stream->set_verify(_need_verify);
5533
5534 // Check if verification needs to be relaxed for this class file
5535 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5536 _relax_verify = relax_format_check_for(_loader_data);
5537
5538 parse_stream(stream, CHECK);
5539
5830
5831 // Finalize the Annotations metadata object,
5832 // now that all annotation arrays have been created.
5833 create_combined_annotations(CHECK);
5834
5835 // Make sure this is the end of class file stream
5836 guarantee_property(stream->at_eos(),
5837 "Extra bytes at the end of class file %s",
5838 CHECK);
5839
5840 // all bytes in stream read and parsed
5841 }
5842
5843 void ClassFileParser::mangle_hidden_class_name(InstanceKlass* const ik) {
5844 ResourceMark rm;
5845 // Construct hidden name from _class_name, "+", and &ik. Note that we can't
5846 // use a '/' because that confuses finding the class's package. Also, can't
5847 // use an illegal char such as ';' because that causes serialization issues
5848 // and issues with hidden classes that create their own hidden classes.
5849 char addr_buf[20];
5850 if (DumpSharedSpaces) {
5851 // We want stable names for the archived hidden classes (only for static
5852 // archive for now). Spaces under default_SharedBaseAddress() will be
5853 // occupied by the archive at run time, so we know that no dynamically
5854 // loaded InstanceKlass will be placed under there.
5855 static volatile size_t counter = 0;
5856 Atomic::cmpxchg(&counter, (size_t)0, Arguments::default_SharedBaseAddress()); // initialize it
5857 size_t new_id = Atomic::add(&counter, (size_t)1);
5858 jio_snprintf(addr_buf, 20, SIZE_FORMAT_X, new_id);
5859 } else {
5860 jio_snprintf(addr_buf, 20, INTPTR_FORMAT, p2i(ik));
5861 }
5862 size_t new_name_len = _class_name->utf8_length() + 2 + strlen(addr_buf);
5863 char* new_name = NEW_RESOURCE_ARRAY(char, new_name_len);
5864 jio_snprintf(new_name, new_name_len, "%s+%s",
5865 _class_name->as_C_string(), addr_buf);
5866 update_class_name(SymbolTable::new_symbol(new_name));
5867
5868 // Add a Utf8 entry containing the hidden name.
5869 assert(_class_name != nullptr, "Unexpected null _class_name");
5870 int hidden_index = _orig_cp_size; // this is an extra slot we added
|
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #include "precompiled.hpp"
25 #include "cds/cdsConfig.hpp"
26 #include "classfile/classFileParser.hpp"
27 #include "classfile/classFileStream.hpp"
28 #include "classfile/classLoader.hpp"
29 #include "classfile/classLoaderData.inline.hpp"
30 #include "classfile/classLoadInfo.hpp"
31 #include "classfile/defaultMethods.hpp"
32 #include "classfile/fieldLayoutBuilder.hpp"
33 #include "classfile/javaClasses.inline.hpp"
34 #include "classfile/moduleEntry.hpp"
35 #include "classfile/packageEntry.hpp"
36 #include "classfile/symbolTable.hpp"
37 #include "classfile/systemDictionary.hpp"
38 #include "classfile/verificationType.hpp"
39 #include "classfile/verifier.hpp"
40 #include "classfile/vmClasses.hpp"
41 #include "classfile/vmSymbols.hpp"
42 #include "jvm.h"
43 #include "logging/log.hpp"
44 #include "logging/logStream.hpp"
45 #include "memory/allocation.hpp"
2836 const Method* const m = _methods->at(i);
2837 NameSigHash name_and_sig(m->name(), m->signature());
2838 // If no duplicates, add name/signature in hashtable names_and_sigs.
2839 if(!names_and_sigs->put(name_and_sig, 0)) {
2840 classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s",
2841 name_and_sig._name->as_C_string(), name_and_sig._sig->as_klass_external_name(), THREAD);
2842 return;
2843 }
2844 }
2845 }
2846 }
2847 }
2848
2849 static const intArray* sort_methods(Array<Method*>* methods) {
2850 const int length = methods->length();
2851 // If JVMTI original method ordering or sharing is enabled we have to
2852 // remember the original class file ordering.
2853 // We temporarily use the vtable_index field in the Method* to store the
2854 // class file index, so we can read in after calling qsort.
2855 // Put the method ordering in the shared archive.
2856 if (JvmtiExport::can_maintain_original_method_order() || CDSConfig::is_dumping_archive()) {
2857 for (int index = 0; index < length; index++) {
2858 Method* const m = methods->at(index);
2859 assert(!m->valid_vtable_index(), "vtable index should not be set");
2860 m->set_vtable_index(index);
2861 }
2862 }
2863 // Sort method array by ascending method name (for faster lookups & vtable construction)
2864 // Note that the ordering is not alphabetical, see Symbol::fast_compare
2865 Method::sort_methods(methods);
2866
2867 intArray* method_ordering = nullptr;
2868 // If JVMTI original method ordering or sharing is enabled construct int
2869 // array remembering the original ordering
2870 if (JvmtiExport::can_maintain_original_method_order() || CDSConfig::is_dumping_archive()) {
2871 method_ordering = new intArray(length, length, -1);
2872 for (int index = 0; index < length; index++) {
2873 Method* const m = methods->at(index);
2874 const int old_index = m->vtable_index();
2875 assert(old_index >= 0 && old_index < length, "invalid method index");
2876 method_ordering->at_put(index, old_index);
2877 m->set_vtable_index(Method::invalid_vtable_index);
2878 }
2879 }
2880 return method_ordering;
2881 }
2882
2883 // Parse generic_signature attribute for methods and fields
2884 u2 ClassFileParser::parse_generic_signature_attribute(const ClassFileStream* const cfs,
2885 TRAPS) {
2886 assert(cfs != nullptr, "invariant");
2887
2888 cfs->guarantee_more(2, CHECK_0); // generic_signature_index
2889 const u2 generic_signature_index = cfs->get_u2_fast();
2890 check_property(
4308 }
4309
4310 Reflection::VerifyClassAccessResults vca_result =
4311 Reflection::verify_class_access(this_klass, k, false);
4312 if (vca_result != Reflection::ACCESS_OK) {
4313 ResourceMark rm(THREAD);
4314 char* msg = Reflection::verify_class_access_msg(this_klass,
4315 k,
4316 vca_result);
4317 if (msg == nullptr) {
4318 bool same_module = (this_klass->module() == k->module());
4319 Exceptions::fthrow(
4320 THREAD_AND_LOCATION,
4321 vmSymbols::java_lang_IllegalAccessError(),
4322 "class %s cannot access its superinterface %s (%s%s%s)",
4323 this_klass->external_name(),
4324 k->external_name(),
4325 (same_module) ? this_klass->joint_in_module_of_loader(k) : this_klass->class_in_module_of_loader(),
4326 (same_module) ? "" : "; ",
4327 (same_module) ? "" : k->class_in_module_of_loader());
4328 return;
4329 } else {
4330 // Add additional message content.
4331 Exceptions::fthrow(
4332 THREAD_AND_LOCATION,
4333 vmSymbols::java_lang_IllegalAccessError(),
4334 "superinterface check failed: %s",
4335 msg);
4336 return;
4337 }
4338 }
4339 }
4340 }
4341
4342
4343 static void check_final_method_override(const InstanceKlass* this_klass, TRAPS) {
4344 assert(this_klass != nullptr, "invariant");
4345 const Array<Method*>* const methods = this_klass->methods();
4346 const int num_methods = methods->length();
4347
4348 // go thru each method and check if it overrides a final method
4349 for (int index = 0; index < num_methods; index++) {
4350 const Method* const m = methods->at(index);
4351
4352 // skip private, static, and <init> methods
4353 if ((!m->is_private() && !m->is_static()) &&
4354 (m->name() != vmSymbols::object_initializer_name())) {
4355
4356 const Symbol* const name = m->name();
5193 _loader_data->add_class(ik, publicize);
5194
5195 set_klass_to_deallocate(ik);
5196
5197 assert(_field_info != nullptr, "invariant");
5198 assert(ik->static_field_size() == _field_info->_static_field_size, "sanity");
5199 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->_nonstatic_oop_map_count,
5200 "sanity");
5201
5202 assert(ik->is_instance_klass(), "sanity");
5203 assert(ik->size_helper() == _field_info->_instance_size, "sanity");
5204
5205 // Fill in information already parsed
5206 ik->set_should_verify_class(_need_verify);
5207
5208 // Not yet: supers are done below to support the new subtype-checking fields
5209 ik->set_nonstatic_field_size(_field_info->_nonstatic_field_size);
5210 ik->set_has_nonstatic_fields(_field_info->_has_nonstatic_fields);
5211 assert(_fac != nullptr, "invariant");
5212 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5213 ik->set_nonstatic_oop_field_count(_fac->count[NONSTATIC_OOP]);
5214
5215 // this transfers ownership of a lot of arrays from
5216 // the parser onto the InstanceKlass*
5217 apply_parsed_class_metadata(ik, _java_fields_count);
5218
5219 // can only set dynamic nest-host after static nest information is set
5220 if (cl_inst_info.dynamic_nest_host() != nullptr) {
5221 ik->set_nest_host(cl_inst_info.dynamic_nest_host());
5222 }
5223
5224 // note that is not safe to use the fields in the parser from this point on
5225 assert(nullptr == _cp, "invariant");
5226 assert(nullptr == _fieldinfo_stream, "invariant");
5227 assert(nullptr == _fields_status, "invariant");
5228 assert(nullptr == _methods, "invariant");
5229 assert(nullptr == _inner_classes, "invariant");
5230 assert(nullptr == _nest_members, "invariant");
5231 assert(nullptr == _combined_annotations, "invariant");
5232 assert(nullptr == _record_components, "invariant");
5233 assert(nullptr == _permitted_subclasses, "invariant");
5503 _declares_nonstatic_concrete_methods(false),
5504 _has_localvariable_table(false),
5505 _has_final_method(false),
5506 _has_contended_fields(false),
5507 _has_finalizer(false),
5508 _has_empty_finalizer(false),
5509 _has_vanilla_constructor(false),
5510 _max_bootstrap_specifier_index(-1) {
5511
5512 _class_name = name != nullptr ? name : vmSymbols::unknown_class_name();
5513 _class_name->increment_refcount();
5514
5515 assert(_loader_data != nullptr, "invariant");
5516 assert(stream != nullptr, "invariant");
5517 assert(_stream != nullptr, "invariant");
5518 assert(_stream->buffer() == _stream->current(), "invariant");
5519 assert(_class_name != nullptr, "invariant");
5520 assert(0 == _access_flags.as_int(), "invariant");
5521
5522 // Figure out whether we can skip format checking (matching classic VM behavior)
5523 if (CDSConfig::is_dumping_static_archive()) {
5524 // verify == true means it's a 'remote' class (i.e., non-boot class)
5525 // Verification decision is based on BytecodeVerificationRemote flag
5526 // for those classes.
5527 _need_verify = (stream->need_verify()) ? BytecodeVerificationRemote :
5528 BytecodeVerificationLocal;
5529 }
5530 else {
5531 _need_verify = Verifier::should_verify_for(_loader_data->class_loader(),
5532 stream->need_verify());
5533 }
5534
5535 // synch back verification state to stream
5536 stream->set_verify(_need_verify);
5537
5538 // Check if verification needs to be relaxed for this class file
5539 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
5540 _relax_verify = relax_format_check_for(_loader_data);
5541
5542 parse_stream(stream, CHECK);
5543
5834
5835 // Finalize the Annotations metadata object,
5836 // now that all annotation arrays have been created.
5837 create_combined_annotations(CHECK);
5838
5839 // Make sure this is the end of class file stream
5840 guarantee_property(stream->at_eos(),
5841 "Extra bytes at the end of class file %s",
5842 CHECK);
5843
5844 // all bytes in stream read and parsed
5845 }
5846
5847 void ClassFileParser::mangle_hidden_class_name(InstanceKlass* const ik) {
5848 ResourceMark rm;
5849 // Construct hidden name from _class_name, "+", and &ik. Note that we can't
5850 // use a '/' because that confuses finding the class's package. Also, can't
5851 // use an illegal char such as ';' because that causes serialization issues
5852 // and issues with hidden classes that create their own hidden classes.
5853 char addr_buf[20];
5854 if (CDSConfig::is_dumping_static_archive()) {
5855 // We want stable names for the archived hidden classes (only for static
5856 // archive for now). Spaces under default_SharedBaseAddress() will be
5857 // occupied by the archive at run time, so we know that no dynamically
5858 // loaded InstanceKlass will be placed under there.
5859 static volatile size_t counter = 0;
5860 Atomic::cmpxchg(&counter, (size_t)0, Arguments::default_SharedBaseAddress()); // initialize it
5861 size_t new_id = Atomic::add(&counter, (size_t)1);
5862 jio_snprintf(addr_buf, 20, SIZE_FORMAT_X, new_id);
5863 } else {
5864 jio_snprintf(addr_buf, 20, INTPTR_FORMAT, p2i(ik));
5865 }
5866 size_t new_name_len = _class_name->utf8_length() + 2 + strlen(addr_buf);
5867 char* new_name = NEW_RESOURCE_ARRAY(char, new_name_len);
5868 jio_snprintf(new_name, new_name_len, "%s+%s",
5869 _class_name->as_C_string(), addr_buf);
5870 update_class_name(SymbolTable::new_symbol(new_name));
5871
5872 // Add a Utf8 entry containing the hidden name.
5873 assert(_class_name != nullptr, "Unexpected null _class_name");
5874 int hidden_index = _orig_cp_size; // this is an extra slot we added
|