< prev index next >

src/hotspot/share/classfile/classFileParser.cpp

Print this page




  40 #include "logging/log.hpp"
  41 #include "logging/logStream.hpp"
  42 #include "memory/allocation.hpp"
  43 #include "memory/metadataFactory.hpp"
  44 #include "memory/oopFactory.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/annotations.hpp"
  48 #include "oops/constantPool.inline.hpp"
  49 #include "oops/fieldStreams.hpp"
  50 #include "oops/instanceKlass.hpp"
  51 #include "oops/instanceMirrorKlass.hpp"
  52 #include "oops/klass.inline.hpp"
  53 #include "oops/klassVtable.hpp"
  54 #include "oops/metadata.hpp"
  55 #include "oops/method.hpp"
  56 #include "oops/oop.inline.hpp"
  57 #include "oops/symbol.hpp"
  58 #include "prims/jvmtiExport.hpp"
  59 #include "prims/jvmtiThreadState.hpp"

  60 #include "runtime/handles.inline.hpp"
  61 #include "runtime/javaCalls.hpp"
  62 #include "runtime/perfData.hpp"
  63 #include "runtime/reflection.hpp"
  64 #include "runtime/safepointVerifiers.hpp"
  65 #include "runtime/signature.hpp"
  66 #include "runtime/timer.hpp"
  67 #include "services/classLoadingService.hpp"
  68 #include "services/threadService.hpp"
  69 #include "trace/traceMacros.hpp"
  70 #include "utilities/align.hpp"
  71 #include "utilities/bitMap.inline.hpp"
  72 #include "utilities/copy.hpp"
  73 #include "utilities/exceptions.hpp"
  74 #include "utilities/globalDefinitions.hpp"
  75 #include "utilities/growableArray.hpp"
  76 #include "utilities/macros.hpp"
  77 #include "utilities/ostream.hpp"
  78 #include "utilities/resourceHash.hpp"
  79 #if INCLUDE_CDS
  80 #include "classfile/systemDictionaryShared.hpp"
  81 #endif
  82 
  83 // We generally try to create the oops directly when parsing, rather than
  84 // allocating temporary data structures and copying the bytes twice. A
  85 // temporary area is only needed when parsing utf8 entries in the constant
  86 // pool and when parsing line number tables.
  87 
  88 // We add assert in debug mode when class format is not checked.
  89 
  90 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
  91 #define JAVA_MIN_SUPPORTED_VERSION        45

  92 
  93 // Used for two backward compatibility reasons:
  94 // - 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
  96 #define JAVA_1_5_VERSION                  49
  97 
  98 // Used for backward compatibility reasons:
  99 // - to check for javac bug fixes that happened after 1.5
 100 // - also used as the max version when running in jdk6
 101 #define JAVA_6_VERSION                    50
 102 
 103 // Used for backward compatibility reasons:
 104 // - to disallow argument and require ACC_STATIC for <clinit> methods
 105 #define JAVA_7_VERSION                    51
 106 
 107 // Extension method support.
 108 #define JAVA_8_VERSION                    52
 109 
 110 #define JAVA_9_VERSION                    53
 111 


4683     Exceptions::fthrow(
4684       THREAD_AND_LOCATION,
4685       vmSymbols::java_lang_ClassFormatError(),
4686       "Illegal class modifiers in class %s: 0x%X",
4687       _class_name->as_C_string(), flags
4688     );
4689     return;
4690   }
4691 }
4692 
4693 static bool has_illegal_visibility(jint flags) {
4694   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4695   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4696   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4697 
4698   return ((is_public && is_protected) ||
4699           (is_public && is_private) ||
4700           (is_protected && is_private));
4701 }
4702 
4703 static bool is_supported_version(u2 major, u2 minor){
4704   const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION;
4705   return (major >= JAVA_MIN_SUPPORTED_VERSION) &&
4706          (major <= max_version) &&
4707          ((major != max_version) ||
4708           (minor <= JVM_CLASSFILE_MINOR_VERSION));













































4709 }
4710 
4711 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4712                                                    bool is_interface,
4713                                                    TRAPS) const {
4714   if (!_need_verify) { return; }
4715 
4716   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4717   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4718   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4719   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4720   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4721   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4722   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4723   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
4724   const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4725 
4726   bool is_illegal = false;
4727 
4728   if (is_interface) {


5532   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5533       !module_entry->has_default_read_edges()) {
5534     if (!module_entry->set_has_default_read_edges()) {
5535       // We won a potential race
5536       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5537     }
5538   }
5539 
5540   // Update the loader_data graph.
5541   record_defined_class_dependencies(ik, CHECK);
5542 
5543   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5544 
5545   if (!is_internal()) {
5546     if (log_is_enabled(Info, class, load)) {
5547       ResourceMark rm;
5548       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
5549       ik->print_class_load_logging(_loader_data, module_name, _stream);
5550     }
5551 







5552     if (log_is_enabled(Debug, class, resolve))  {
5553       ResourceMark rm;
5554       // print out the superclass.
5555       const char * from = ik->external_name();
5556       if (ik->java_super() != NULL) {
5557         log_debug(class, resolve)("%s %s (super)",
5558                    from,
5559                    ik->java_super()->external_name());
5560       }
5561       // print out each of the interface classes referred to by this class.
5562       const Array<Klass*>* const local_interfaces = ik->local_interfaces();
5563       if (local_interfaces != NULL) {
5564         const int length = local_interfaces->length();
5565         for (int i = 0; i < length; i++) {
5566           const Klass* const k = local_interfaces->at(i);
5567           const char * to = k->external_name();
5568           log_debug(class, resolve)("%s %s (interface)", from, to);
5569         }
5570       }
5571     }


5847                      "Incompatible magic value %u in class file %s",
5848                      magic, CHECK);
5849 
5850   // Version numbers
5851   _minor_version = stream->get_u2_fast();
5852   _major_version = stream->get_u2_fast();
5853 
5854   if (DumpSharedSpaces && _major_version < JAVA_1_5_VERSION) {
5855     ResourceMark rm;
5856     warning("Pre JDK 1.5 class not supported by CDS: %u.%u %s",
5857             _major_version,  _minor_version, _class_name->as_C_string());
5858     Exceptions::fthrow(
5859       THREAD_AND_LOCATION,
5860       vmSymbols::java_lang_UnsupportedClassVersionError(),
5861       "Unsupported major.minor version for dump time %u.%u",
5862       _major_version,
5863       _minor_version);
5864   }
5865 
5866   // Check version numbers - we check this even with verifier off
5867   if (!is_supported_version(_major_version, _minor_version)) {
5868     ResourceMark rm(THREAD);
5869     Exceptions::fthrow(
5870       THREAD_AND_LOCATION,
5871       vmSymbols::java_lang_UnsupportedClassVersionError(),
5872       "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "
5873       "this version of the Java Runtime only recognizes class file versions up to %u.%u",
5874       _class_name->as_C_string(),
5875       _major_version,
5876       _minor_version,
5877       JVM_CLASSFILE_MAJOR_VERSION,
5878       JVM_CLASSFILE_MINOR_VERSION);
5879     return;
5880   }
5881 
5882   stream->guarantee_more(3, CHECK); // length, first cp tag
5883   u2 cp_size = stream->get_u2_fast();
5884 
5885   guarantee_property(
5886     cp_size >= 1, "Illegal constant pool size %u in class file %s",
5887     cp_size, CHECK);
5888 
5889   _orig_cp_size = cp_size;
5890   if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
5891     THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
5892   }
5893   cp_size += _max_num_patched_klasses;
5894 
5895   _cp = ConstantPool::allocate(_loader_data,
5896                                cp_size,
5897                                CHECK);
5898 
5899   ConstantPool* const cp = _cp;
5900 




  40 #include "logging/log.hpp"
  41 #include "logging/logStream.hpp"
  42 #include "memory/allocation.hpp"
  43 #include "memory/metadataFactory.hpp"
  44 #include "memory/oopFactory.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/annotations.hpp"
  48 #include "oops/constantPool.inline.hpp"
  49 #include "oops/fieldStreams.hpp"
  50 #include "oops/instanceKlass.hpp"
  51 #include "oops/instanceMirrorKlass.hpp"
  52 #include "oops/klass.inline.hpp"
  53 #include "oops/klassVtable.hpp"
  54 #include "oops/metadata.hpp"
  55 #include "oops/method.hpp"
  56 #include "oops/oop.inline.hpp"
  57 #include "oops/symbol.hpp"
  58 #include "prims/jvmtiExport.hpp"
  59 #include "prims/jvmtiThreadState.hpp"
  60 #include "runtime/arguments.hpp"
  61 #include "runtime/handles.inline.hpp"
  62 #include "runtime/javaCalls.hpp"
  63 #include "runtime/perfData.hpp"
  64 #include "runtime/reflection.hpp"
  65 #include "runtime/safepointVerifiers.hpp"
  66 #include "runtime/signature.hpp"
  67 #include "runtime/timer.hpp"
  68 #include "services/classLoadingService.hpp"
  69 #include "services/threadService.hpp"
  70 #include "trace/traceMacros.hpp"
  71 #include "utilities/align.hpp"
  72 #include "utilities/bitMap.inline.hpp"
  73 #include "utilities/copy.hpp"
  74 #include "utilities/exceptions.hpp"
  75 #include "utilities/globalDefinitions.hpp"
  76 #include "utilities/growableArray.hpp"
  77 #include "utilities/macros.hpp"
  78 #include "utilities/ostream.hpp"
  79 #include "utilities/resourceHash.hpp"
  80 #if INCLUDE_CDS
  81 #include "classfile/systemDictionaryShared.hpp"
  82 #endif
  83 
  84 // We generally try to create the oops directly when parsing, rather than
  85 // allocating temporary data structures and copying the bytes twice. A
  86 // temporary area is only needed when parsing utf8 entries in the constant
  87 // pool and when parsing line number tables.
  88 
  89 // We add assert in debug mode when class format is not checked.
  90 
  91 #define JAVA_CLASSFILE_MAGIC              0xCAFEBABE
  92 #define JAVA_MIN_SUPPORTED_VERSION        45
  93 #define JAVA_PREVIEW_MINOR_VERSION        65535
  94 
  95 // Used for two backward compatibility reasons:
  96 // - to check for new additions to the class file format in JDK1.5
  97 // - to check for bug fixes in the format checker in JDK1.5
  98 #define JAVA_1_5_VERSION                  49
  99 
 100 // Used for backward compatibility reasons:
 101 // - to check for javac bug fixes that happened after 1.5
 102 // - also used as the max version when running in jdk6
 103 #define JAVA_6_VERSION                    50
 104 
 105 // Used for backward compatibility reasons:
 106 // - to disallow argument and require ACC_STATIC for <clinit> methods
 107 #define JAVA_7_VERSION                    51
 108 
 109 // Extension method support.
 110 #define JAVA_8_VERSION                    52
 111 
 112 #define JAVA_9_VERSION                    53
 113 


4685     Exceptions::fthrow(
4686       THREAD_AND_LOCATION,
4687       vmSymbols::java_lang_ClassFormatError(),
4688       "Illegal class modifiers in class %s: 0x%X",
4689       _class_name->as_C_string(), flags
4690     );
4691     return;
4692   }
4693 }
4694 
4695 static bool has_illegal_visibility(jint flags) {
4696   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4697   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4698   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4699 
4700   return ((is_public && is_protected) ||
4701           (is_public && is_private) ||
4702           (is_protected && is_private));
4703 }
4704 
4705 static void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4706   const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION;
4707   if (major != JAVA_MIN_SUPPORTED_VERSION) { // All 45.* are ok including 45.65535
4708     if (minor == JAVA_PREVIEW_MINOR_VERSION) {
4709       if (major != max_version) {
4710         ResourceMark rm(THREAD);
4711         Exceptions::fthrow(
4712           THREAD_AND_LOCATION,
4713           vmSymbols::java_lang_UnsupportedClassVersionError(),
4714           "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4715           "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4716           class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4717         return;
4718       }
4719 
4720       if (!Arguments::enable_preview()) {
4721         ResourceMark rm(THREAD);
4722         Exceptions::fthrow(
4723           THREAD_AND_LOCATION,
4724           vmSymbols::java_lang_UnsupportedClassVersionError(),
4725           "Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4726           class_name->as_C_string(), major, minor);
4727         return;
4728       }
4729 
4730     } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4731       if (major > max_version) {
4732         ResourceMark rm(THREAD);
4733         Exceptions::fthrow(
4734           THREAD_AND_LOCATION,
4735           vmSymbols::java_lang_UnsupportedClassVersionError(),
4736           "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "
4737           "this version of the Java Runtime only recognizes class file versions up to %u.0",
4738           class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION);
4739       } else if (major < JAVA_MIN_SUPPORTED_VERSION) {
4740         ResourceMark rm(THREAD);
4741         Exceptions::fthrow(
4742           THREAD_AND_LOCATION,
4743           vmSymbols::java_lang_UnsupportedClassVersionError(),
4744           "%s (class file version %u.%u) was compiled with an invalid major version",
4745           class_name->as_C_string(), major, minor);
4746       } else if (minor != 0) {
4747         ResourceMark rm(THREAD);
4748         Exceptions::fthrow(
4749           THREAD_AND_LOCATION,
4750           vmSymbols::java_lang_UnsupportedClassVersionError(),
4751           "%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4752           class_name->as_C_string(), major, minor);
4753       }
4754     }
4755   }
4756 }
4757 
4758 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4759                                                    bool is_interface,
4760                                                    TRAPS) const {
4761   if (!_need_verify) { return; }
4762 
4763   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4764   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4765   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4766   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4767   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4768   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4769   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4770   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
4771   const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4772 
4773   bool is_illegal = false;
4774 
4775   if (is_interface) {


5579   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5580       !module_entry->has_default_read_edges()) {
5581     if (!module_entry->set_has_default_read_edges()) {
5582       // We won a potential race
5583       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5584     }
5585   }
5586 
5587   // Update the loader_data graph.
5588   record_defined_class_dependencies(ik, CHECK);
5589 
5590   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5591 
5592   if (!is_internal()) {
5593     if (log_is_enabled(Info, class, load)) {
5594       ResourceMark rm;
5595       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
5596       ik->print_class_load_logging(_loader_data, module_name, _stream);
5597     }
5598 
5599     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5600         ik->major_version() != JAVA_MIN_SUPPORTED_VERSION &&
5601         log_is_enabled(Info, class, preview)) {
5602       ResourceMark rm;
5603       log_info(class, preview)("Loading preview feature type %s", ik->external_name());
5604     }
5605 
5606     if (log_is_enabled(Debug, class, resolve))  {
5607       ResourceMark rm;
5608       // print out the superclass.
5609       const char * from = ik->external_name();
5610       if (ik->java_super() != NULL) {
5611         log_debug(class, resolve)("%s %s (super)",
5612                    from,
5613                    ik->java_super()->external_name());
5614       }
5615       // print out each of the interface classes referred to by this class.
5616       const Array<Klass*>* const local_interfaces = ik->local_interfaces();
5617       if (local_interfaces != NULL) {
5618         const int length = local_interfaces->length();
5619         for (int i = 0; i < length; i++) {
5620           const Klass* const k = local_interfaces->at(i);
5621           const char * to = k->external_name();
5622           log_debug(class, resolve)("%s %s (interface)", from, to);
5623         }
5624       }
5625     }


5901                      "Incompatible magic value %u in class file %s",
5902                      magic, CHECK);
5903 
5904   // Version numbers
5905   _minor_version = stream->get_u2_fast();
5906   _major_version = stream->get_u2_fast();
5907 
5908   if (DumpSharedSpaces && _major_version < JAVA_1_5_VERSION) {
5909     ResourceMark rm;
5910     warning("Pre JDK 1.5 class not supported by CDS: %u.%u %s",
5911             _major_version,  _minor_version, _class_name->as_C_string());
5912     Exceptions::fthrow(
5913       THREAD_AND_LOCATION,
5914       vmSymbols::java_lang_UnsupportedClassVersionError(),
5915       "Unsupported major.minor version for dump time %u.%u",
5916       _major_version,
5917       _minor_version);
5918   }
5919 
5920   // Check version numbers - we check this even with verifier off
5921   verify_class_version(_major_version, _minor_version, _class_name, CHECK);













5922 
5923   stream->guarantee_more(3, CHECK); // length, first cp tag
5924   u2 cp_size = stream->get_u2_fast();
5925 
5926   guarantee_property(
5927     cp_size >= 1, "Illegal constant pool size %u in class file %s",
5928     cp_size, CHECK);
5929 
5930   _orig_cp_size = cp_size;
5931   if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
5932     THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
5933   }
5934   cp_size += _max_num_patched_klasses;
5935 
5936   _cp = ConstantPool::allocate(_loader_data,
5937                                cp_size,
5938                                CHECK);
5939 
5940   ConstantPool* const cp = _cp;
5941 


< prev index next >