src/hotspot/share/classfile/classFileParser.cpp
changeset 49716 450d709262c1
parent 49677 a1a7456dd8b9
child 49948 ff8dbb56740a
equal deleted inserted replaced
49715:947560700a09 49716:450d709262c1
    55 #include "oops/method.hpp"
    55 #include "oops/method.hpp"
    56 #include "oops/oop.inline.hpp"
    56 #include "oops/oop.inline.hpp"
    57 #include "oops/symbol.hpp"
    57 #include "oops/symbol.hpp"
    58 #include "prims/jvmtiExport.hpp"
    58 #include "prims/jvmtiExport.hpp"
    59 #include "prims/jvmtiThreadState.hpp"
    59 #include "prims/jvmtiThreadState.hpp"
       
    60 #include "runtime/arguments.hpp"
    60 #include "runtime/handles.inline.hpp"
    61 #include "runtime/handles.inline.hpp"
    61 #include "runtime/javaCalls.hpp"
    62 #include "runtime/javaCalls.hpp"
    62 #include "runtime/perfData.hpp"
    63 #include "runtime/perfData.hpp"
    63 #include "runtime/reflection.hpp"
    64 #include "runtime/reflection.hpp"
    64 #include "runtime/safepointVerifiers.hpp"
    65 #include "runtime/safepointVerifiers.hpp"
    87 
    88 
    88 // We add assert in debug mode when class format is not checked.
    89 // We add assert in debug mode when class format is not checked.
    89 
    90 
    90 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
    91 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
    91 #define JAVA_MIN_SUPPORTED_VERSION        45
    92 #define JAVA_MIN_SUPPORTED_VERSION        45
       
    93 #define JAVA_PREVIEW_MINOR_VERSION        65535
    92 
    94 
    93 // Used for two backward compatibility reasons:
    95 // Used for two backward compatibility reasons:
    94 // - to check for new additions to the class file format in JDK1.5
    96 // - to check for new additions to the class file format in JDK1.5
    95 // - to check for bug fixes in the format checker in JDK1.5
    97 // - to check for bug fixes in the format checker in JDK1.5
    96 #define JAVA_1_5_VERSION                  49
    98 #define JAVA_1_5_VERSION                  49
  4698   return ((is_public && is_protected) ||
  4700   return ((is_public && is_protected) ||
  4699           (is_public && is_private) ||
  4701           (is_public && is_private) ||
  4700           (is_protected && is_private));
  4702           (is_protected && is_private));
  4701 }
  4703 }
  4702 
  4704 
  4703 static bool is_supported_version(u2 major, u2 minor){
  4705 // A legal major_version.minor_version must be one of the following:
       
  4706 //
       
  4707 //   Major_version = 45, any minor_version.
       
  4708 //   Major_version >= 46 and major_version <= current_major_version and minor_version = 0.
       
  4709 //   Major_version = current_major_version and minor_version = 65535 and --enable-preview is present.
       
  4710 //
       
  4711 static void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
  4704   const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION;
  4712   const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION;
  4705   return (major >= JAVA_MIN_SUPPORTED_VERSION) &&
  4713   if (major != JAVA_MIN_SUPPORTED_VERSION) { // All 45.* are ok including 45.65535
  4706          (major <= max_version) &&
  4714     if (minor == JAVA_PREVIEW_MINOR_VERSION) {
  4707          ((major != max_version) ||
  4715       if (major != max_version) {
  4708           (minor <= JVM_CLASSFILE_MINOR_VERSION));
  4716         ResourceMark rm(THREAD);
       
  4717         Exceptions::fthrow(
       
  4718           THREAD_AND_LOCATION,
       
  4719           vmSymbols::java_lang_UnsupportedClassVersionError(),
       
  4720           "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
       
  4721           "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
       
  4722           class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
       
  4723         return;
       
  4724       }
       
  4725 
       
  4726       if (!Arguments::enable_preview()) {
       
  4727         ResourceMark rm(THREAD);
       
  4728         Exceptions::fthrow(
       
  4729           THREAD_AND_LOCATION,
       
  4730           vmSymbols::java_lang_UnsupportedClassVersionError(),
       
  4731           "Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
       
  4732           class_name->as_C_string(), major, minor);
       
  4733         return;
       
  4734       }
       
  4735 
       
  4736     } else { // minor != JAVA_PREVIEW_MINOR_VERSION
       
  4737       if (major > max_version) {
       
  4738         ResourceMark rm(THREAD);
       
  4739         Exceptions::fthrow(
       
  4740           THREAD_AND_LOCATION,
       
  4741           vmSymbols::java_lang_UnsupportedClassVersionError(),
       
  4742           "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "
       
  4743           "this version of the Java Runtime only recognizes class file versions up to %u.0",
       
  4744           class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION);
       
  4745       } else if (major < JAVA_MIN_SUPPORTED_VERSION) {
       
  4746         ResourceMark rm(THREAD);
       
  4747         Exceptions::fthrow(
       
  4748           THREAD_AND_LOCATION,
       
  4749           vmSymbols::java_lang_UnsupportedClassVersionError(),
       
  4750           "%s (class file version %u.%u) was compiled with an invalid major version",
       
  4751           class_name->as_C_string(), major, minor);
       
  4752       } else if (minor != 0) {
       
  4753         ResourceMark rm(THREAD);
       
  4754         Exceptions::fthrow(
       
  4755           THREAD_AND_LOCATION,
       
  4756           vmSymbols::java_lang_UnsupportedClassVersionError(),
       
  4757           "%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
       
  4758           class_name->as_C_string(), major, minor);
       
  4759       }
       
  4760     }
       
  4761   }
  4709 }
  4762 }
  4710 
  4763 
  4711 void ClassFileParser::verify_legal_field_modifiers(jint flags,
  4764 void ClassFileParser::verify_legal_field_modifiers(jint flags,
  4712                                                    bool is_interface,
  4765                                                    bool is_interface,
  4713                                                    TRAPS) const {
  4766                                                    TRAPS) const {
  5547   if (!is_internal()) {
  5600   if (!is_internal()) {
  5548     if (log_is_enabled(Info, class, load)) {
  5601     if (log_is_enabled(Info, class, load)) {
  5549       ResourceMark rm;
  5602       ResourceMark rm;
  5550       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
  5603       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
  5551       ik->print_class_load_logging(_loader_data, module_name, _stream);
  5604       ik->print_class_load_logging(_loader_data, module_name, _stream);
       
  5605     }
       
  5606 
       
  5607     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
       
  5608         ik->major_version() != JAVA_MIN_SUPPORTED_VERSION &&
       
  5609         log_is_enabled(Info, class, preview)) {
       
  5610       ResourceMark rm;
       
  5611       log_info(class, preview)("Loading preview feature type %s", ik->external_name());
  5552     }
  5612     }
  5553 
  5613 
  5554     if (log_is_enabled(Debug, class, resolve))  {
  5614     if (log_is_enabled(Debug, class, resolve))  {
  5555       ResourceMark rm;
  5615       ResourceMark rm;
  5556       // print out the superclass.
  5616       // print out the superclass.
  5864       _major_version,
  5924       _major_version,
  5865       _minor_version);
  5925       _minor_version);
  5866   }
  5926   }
  5867 
  5927 
  5868   // Check version numbers - we check this even with verifier off
  5928   // Check version numbers - we check this even with verifier off
  5869   if (!is_supported_version(_major_version, _minor_version)) {
  5929   verify_class_version(_major_version, _minor_version, _class_name, CHECK);
  5870     ResourceMark rm(THREAD);
       
  5871     Exceptions::fthrow(
       
  5872       THREAD_AND_LOCATION,
       
  5873       vmSymbols::java_lang_UnsupportedClassVersionError(),
       
  5874       "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "
       
  5875       "this version of the Java Runtime only recognizes class file versions up to %u.%u",
       
  5876       _class_name->as_C_string(),
       
  5877       _major_version,
       
  5878       _minor_version,
       
  5879       JVM_CLASSFILE_MAJOR_VERSION,
       
  5880       JVM_CLASSFILE_MINOR_VERSION);
       
  5881     return;
       
  5882   }
       
  5883 
  5930 
  5884   stream->guarantee_more(3, CHECK); // length, first cp tag
  5931   stream->guarantee_more(3, CHECK); // length, first cp tag
  5885   u2 cp_size = stream->get_u2_fast();
  5932   u2 cp_size = stream->get_u2_fast();
  5886 
  5933 
  5887   guarantee_property(
  5934   guarantee_property(