< prev index next >

src/hotspot/share/classfile/classFileParser.cpp

Print this page




 108 // - also used as the max version when running in jdk6
 109 #define JAVA_6_VERSION                    50
 110 
 111 // Used for backward compatibility reasons:
 112 // - to disallow argument and require ACC_STATIC for <clinit> methods
 113 #define JAVA_7_VERSION                    51
 114 
 115 // Extension method support.
 116 #define JAVA_8_VERSION                    52
 117 
 118 #define JAVA_9_VERSION                    53
 119 
 120 #define JAVA_10_VERSION                   54
 121 
 122 #define JAVA_11_VERSION                   55
 123 
 124 #define JAVA_12_VERSION                   56
 125 
 126 #define JAVA_13_VERSION                   57
 127 
 128 #define JAVA_14_VERSION                   58
 129 
 130 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
 131   assert((bad_constant == JVM_CONSTANT_Module ||
 132           bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
 133          "Unexpected bad constant pool entry");
 134   if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
 135 }
 136 
 137 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
 138                                                   ConstantPool* cp,
 139                                                   const int length,
 140                                                   TRAPS) {
 141   assert(stream != NULL, "invariant");
 142   assert(cp != NULL, "invariant");
 143 
 144   // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
 145   // this function (_current can be allocated in a register, with scalar
 146   // replacement of aggregates). The _current pointer is copied back to
 147   // stream() when this function returns. DON'T call another method within
 148   // this method that uses stream().
 149   const ClassFileStream cfs1 = *stream;


4739       vmSymbols::java_lang_ClassFormatError(),
4740       "Illegal class modifiers in class %s: 0x%X",
4741       _class_name->as_C_string(), flags
4742     );
4743     return;
4744   }
4745 }
4746 
4747 static bool has_illegal_visibility(jint flags) {
4748   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4749   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4750   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4751 
4752   return ((is_public && is_protected) ||
4753           (is_public && is_private) ||
4754           (is_protected && is_private));
4755 }
4756 
4757 // A legal major_version.minor_version must be one of the following:
4758 //
4759 //  Major_version >= 45 and major_version < 56, any minor_version.
4760 //  Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0.
4761 //  Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present.
4762 //
4763 static void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){
4764   ResourceMark rm(THREAD);
4765   const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION;
4766   if (major < JAVA_MIN_SUPPORTED_VERSION) {
4767     Exceptions::fthrow(
4768       THREAD_AND_LOCATION,
4769       vmSymbols::java_lang_UnsupportedClassVersionError(),
4770       "%s (class file version %u.%u) was compiled with an invalid major version",
4771       class_name->as_C_string(), major, minor);
4772     return;
4773   }
4774 
4775   if (major > max_version) {
4776     Exceptions::fthrow(
4777       THREAD_AND_LOCATION,
4778       vmSymbols::java_lang_UnsupportedClassVersionError(),
4779       "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "
4780       "this version of the Java Runtime only recognizes class file versions up to %u.0",
4781       class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION);
4782     return;
4783   }
4784 
4785   if (major < JAVA_12_VERSION || minor == 0) {
4786     return;
4787   }
4788 
4789   if (minor == JAVA_PREVIEW_MINOR_VERSION) {
4790     if (major != max_version) {
4791       Exceptions::fthrow(
4792         THREAD_AND_LOCATION,
4793         vmSymbols::java_lang_UnsupportedClassVersionError(),
4794         "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4795         "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4796         class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4797       return;
4798     }
4799 
4800     if (!Arguments::enable_preview()) {
4801       Exceptions::fthrow(
4802         THREAD_AND_LOCATION,
4803         vmSymbols::java_lang_UnsupportedClassVersionError(),
4804         "Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4805         class_name->as_C_string(), major, minor);
4806       return;

















4807     }
4808 
4809   } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4810     Exceptions::fthrow(
4811         THREAD_AND_LOCATION,
4812         vmSymbols::java_lang_UnsupportedClassVersionError(),
4813         "%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4814         class_name->as_C_string(), major, minor);
4815   }
4816 }
4817 
4818 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4819                                                    bool is_interface,
4820                                                    TRAPS) const {
4821   if (!_need_verify) { return; }
4822 
4823   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4824   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4825   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4826   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4827   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4828   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4829   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4830   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
4831   const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4832 
4833   bool is_illegal = false;
4834 


4941                                         int length,
4942                                         TRAPS) const {
4943   assert(_need_verify, "only called when _need_verify is true");
4944   if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4945     classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4946   }
4947 }
4948 
4949 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4950 // In class names, '/' separates unqualified names.  This is verified in this function also.
4951 // Method names also may not contain the characters '<' or '>', unless <init>
4952 // or <clinit>.  Note that method names may not be <init> or <clinit> in this
4953 // method.  Because these names have been checked as special cases before
4954 // calling this method in verify_legal_method_name.
4955 //
4956 // This method is also called from the modular system APIs in modules.cpp
4957 // to verify the validity of module and package names.
4958 bool ClassFileParser::verify_unqualified_name(const char* name,
4959                                               unsigned int length,
4960                                               int type) {
4961   if (length == 0) return false;  // Must have at least one char.
4962   for (const char* p = name; p != name + length; p++) {
4963     switch(*p) {
4964       case '.':
4965       case ';':
4966       case '[':
4967         // do not permit '.', ';', or '['
4968         return false;
4969       case '/':
4970         // check for '//' or leading or trailing '/' which are not legal
4971         // unqualified name must not be empty
4972         if (type == ClassFileParser::LegalClass) {
4973           if (p == name || p+1 >= name+length || *(p+1) == '/') {
4974             return false;
4975           }
4976         } else {
4977           return false;   // do not permit '/' unless it's class name
4978         }
4979         break;
4980       case '<':
4981       case '>':


5091       return signature + 1;
5092     case JVM_SIGNATURE_CLASS: {
5093       if (_major_version < JAVA_1_5_VERSION) {
5094         // Skip over the class name if one is there
5095         const char* const p = skip_over_field_name(signature + 1, true, --length);
5096 
5097         // The next character better be a semicolon
5098         if (p && (p - signature) > 1 && p[0] == ';') {
5099           return p + 1;
5100         }
5101       }
5102       else {
5103         // Skip leading 'L' and ignore first appearance of ';'
5104         signature++;
5105         const char* c = (const char*) memchr(signature, ';', length - 1);
5106         // Format check signature
5107         if (c != NULL) {
5108           int newlen = c - (char*) signature;
5109           bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5110           if (!legal) {
5111             classfile_parse_error("Class name is empty or contains illegal character "
5112                                   "in descriptor in class file %s",
5113                                   CHECK_0);
5114             return NULL;
5115           }
5116           return signature + newlen + 1;
5117         }
5118       }
5119       return NULL;
5120     }
5121     case JVM_SIGNATURE_ARRAY:
5122       array_dim++;
5123       if (array_dim > 255) {
5124         // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.
5125         classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0);
5126       }
5127       // The rest of what's there better be a legal signature
5128       signature++;
5129       length--;
5130       void_ok = false;
5131       break;


5629 
5630   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5631   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5632       !module_entry->has_default_read_edges()) {
5633     if (!module_entry->set_has_default_read_edges()) {
5634       // We won a potential race
5635       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5636     }
5637   }
5638 
5639   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5640 
5641   if (!is_internal()) {
5642     if (log_is_enabled(Info, class, load)) {
5643       ResourceMark rm;
5644       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
5645       ik->print_class_load_logging(_loader_data, module_name, _stream);
5646     }
5647 
5648     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5649         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5650         log_is_enabled(Info, class, preview)) {
5651       ResourceMark rm;
5652       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5653                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5654     }
5655 
5656     if (log_is_enabled(Debug, class, resolve))  {
5657       ResourceMark rm;
5658       // print out the superclass.
5659       const char * from = ik->external_name();
5660       if (ik->java_super() != NULL) {
5661         log_debug(class, resolve)("%s %s (super)",
5662                    from,
5663                    ik->java_super()->external_name());
5664       }
5665       // print out each of the interface classes referred to by this class.
5666       const Array<InstanceKlass*>* const local_interfaces = ik->local_interfaces();
5667       if (local_interfaces != NULL) {
5668         const int length = local_interfaces->length();
5669         for (int i = 0; i < length; i++) {
5670           const InstanceKlass* const k = local_interfaces->at(i);
5671           const char * to = k->external_name();
5672           log_debug(class, resolve)("%s %s (interface)", from, to);
5673         }




 108 // - also used as the max version when running in jdk6
 109 #define JAVA_6_VERSION                    50
 110 
 111 // Used for backward compatibility reasons:
 112 // - to disallow argument and require ACC_STATIC for <clinit> methods
 113 #define JAVA_7_VERSION                    51
 114 
 115 // Extension method support.
 116 #define JAVA_8_VERSION                    52
 117 
 118 #define JAVA_9_VERSION                    53
 119 
 120 #define JAVA_10_VERSION                   54
 121 
 122 #define JAVA_11_VERSION                   55
 123 
 124 #define JAVA_12_VERSION                   56
 125 
 126 #define JAVA_13_VERSION                   57
 127 


 128 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
 129   assert((bad_constant == JVM_CONSTANT_Module ||
 130           bad_constant == JVM_CONSTANT_Package) && _major_version >= JAVA_9_VERSION,
 131          "Unexpected bad constant pool entry");
 132   if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
 133 }
 134 
 135 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
 136                                                   ConstantPool* cp,
 137                                                   const int length,
 138                                                   TRAPS) {
 139   assert(stream != NULL, "invariant");
 140   assert(cp != NULL, "invariant");
 141 
 142   // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
 143   // this function (_current can be allocated in a register, with scalar
 144   // replacement of aggregates). The _current pointer is copied back to
 145   // stream() when this function returns. DON'T call another method within
 146   // this method that uses stream().
 147   const ClassFileStream cfs1 = *stream;


4737       vmSymbols::java_lang_ClassFormatError(),
4738       "Illegal class modifiers in class %s: 0x%X",
4739       _class_name->as_C_string(), flags
4740     );
4741     return;
4742   }
4743 }
4744 
4745 static bool has_illegal_visibility(jint flags) {
4746   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4747   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4748   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4749 
4750   return ((is_public && is_protected) ||
4751           (is_public && is_private) ||
4752           (is_protected && is_private));
4753 }
4754 
4755 // A legal major_version.minor_version must be one of the following:
4756 //
4757 //   Major_version = 45, any minor_version.
4758 //   Major_version >= 46 and major_version <= current_major_version and minor_version = 0.
4759 //   Major_version = current_major_version and minor_version = 65535 and --enable-preview is present.
4760 //
4761 static void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){

4762   const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION;
4763   if (major != JAVA_MIN_SUPPORTED_VERSION) { // All 45.* are ok including 45.65535
4764     if (minor == JAVA_PREVIEW_MINOR_VERSION) {
4765       if (major != max_version) {
4766         ResourceMark rm(THREAD);
4767         Exceptions::fthrow(
4768           THREAD_AND_LOCATION,
4769           vmSymbols::java_lang_UnsupportedClassVersionError(),
4770           "%s (class file version %u.%u) was compiled with preview features that are unsupported. "
4771           "This version of the Java Runtime only recognizes preview features for class file version %u.%u",
4772           class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION);
4773         return;
4774       }










4775 
4776       if (!Arguments::enable_preview()) {
4777         ResourceMark rm(THREAD);
4778         Exceptions::fthrow(
4779           THREAD_AND_LOCATION,
4780           vmSymbols::java_lang_UnsupportedClassVersionError(),
4781           "Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'",
4782           class_name->as_C_string(), major, minor);
4783         return;
4784       }

4785 
4786     } else { // minor != JAVA_PREVIEW_MINOR_VERSION
4787       if (major > max_version) {
4788         ResourceMark rm(THREAD);
4789         Exceptions::fthrow(
4790           THREAD_AND_LOCATION,
4791           vmSymbols::java_lang_UnsupportedClassVersionError(),
4792           "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "
4793           "this version of the Java Runtime only recognizes class file versions up to %u.0",
4794           class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION);
4795       } else if (major < JAVA_MIN_SUPPORTED_VERSION) {
4796         ResourceMark rm(THREAD);
4797         Exceptions::fthrow(
4798           THREAD_AND_LOCATION,
4799           vmSymbols::java_lang_UnsupportedClassVersionError(),
4800           "%s (class file version %u.%u) was compiled with an invalid major version",
4801           class_name->as_C_string(), major, minor);
4802       } else if (minor != 0) {
4803         ResourceMark rm(THREAD);
4804         Exceptions::fthrow(
4805           THREAD_AND_LOCATION,
4806           vmSymbols::java_lang_UnsupportedClassVersionError(),
4807           "%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4808           class_name->as_C_string(), major, minor);
4809       }
4810     }







4811   }
4812 }
4813 
4814 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4815                                                    bool is_interface,
4816                                                    TRAPS) const {
4817   if (!_need_verify) { return; }
4818 
4819   const bool is_public    = (flags & JVM_ACC_PUBLIC)    != 0;
4820   const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4821   const bool is_private   = (flags & JVM_ACC_PRIVATE)   != 0;
4822   const bool is_static    = (flags & JVM_ACC_STATIC)    != 0;
4823   const bool is_final     = (flags & JVM_ACC_FINAL)     != 0;
4824   const bool is_volatile  = (flags & JVM_ACC_VOLATILE)  != 0;
4825   const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4826   const bool is_enum      = (flags & JVM_ACC_ENUM)      != 0;
4827   const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4828 
4829   bool is_illegal = false;
4830 


4937                                         int length,
4938                                         TRAPS) const {
4939   assert(_need_verify, "only called when _need_verify is true");
4940   if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4941     classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4942   }
4943 }
4944 
4945 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4946 // In class names, '/' separates unqualified names.  This is verified in this function also.
4947 // Method names also may not contain the characters '<' or '>', unless <init>
4948 // or <clinit>.  Note that method names may not be <init> or <clinit> in this
4949 // method.  Because these names have been checked as special cases before
4950 // calling this method in verify_legal_method_name.
4951 //
4952 // This method is also called from the modular system APIs in modules.cpp
4953 // to verify the validity of module and package names.
4954 bool ClassFileParser::verify_unqualified_name(const char* name,
4955                                               unsigned int length,
4956                                               int type) {

4957   for (const char* p = name; p != name + length; p++) {
4958     switch(*p) {
4959       case '.':
4960       case ';':
4961       case '[':
4962         // do not permit '.', ';', or '['
4963         return false;
4964       case '/':
4965         // check for '//' or leading or trailing '/' which are not legal
4966         // unqualified name must not be empty
4967         if (type == ClassFileParser::LegalClass) {
4968           if (p == name || p+1 >= name+length || *(p+1) == '/') {
4969             return false;
4970           }
4971         } else {
4972           return false;   // do not permit '/' unless it's class name
4973         }
4974         break;
4975       case '<':
4976       case '>':


5086       return signature + 1;
5087     case JVM_SIGNATURE_CLASS: {
5088       if (_major_version < JAVA_1_5_VERSION) {
5089         // Skip over the class name if one is there
5090         const char* const p = skip_over_field_name(signature + 1, true, --length);
5091 
5092         // The next character better be a semicolon
5093         if (p && (p - signature) > 1 && p[0] == ';') {
5094           return p + 1;
5095         }
5096       }
5097       else {
5098         // Skip leading 'L' and ignore first appearance of ';'
5099         signature++;
5100         const char* c = (const char*) memchr(signature, ';', length - 1);
5101         // Format check signature
5102         if (c != NULL) {
5103           int newlen = c - (char*) signature;
5104           bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5105           if (!legal) {
5106             classfile_parse_error("Class name contains illegal character "
5107                                   "in descriptor in class file %s",
5108                                   CHECK_0);
5109             return NULL;
5110           }
5111           return signature + newlen + 1;
5112         }
5113       }
5114       return NULL;
5115     }
5116     case JVM_SIGNATURE_ARRAY:
5117       array_dim++;
5118       if (array_dim > 255) {
5119         // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.
5120         classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0);
5121       }
5122       // The rest of what's there better be a legal signature
5123       signature++;
5124       length--;
5125       void_ok = false;
5126       break;


5624 
5625   // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5626   if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5627       !module_entry->has_default_read_edges()) {
5628     if (!module_entry->set_has_default_read_edges()) {
5629       // We won a potential race
5630       JvmtiExport::add_default_read_edges(module_handle, THREAD);
5631     }
5632   }
5633 
5634   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5635 
5636   if (!is_internal()) {
5637     if (log_is_enabled(Info, class, load)) {
5638       ResourceMark rm;
5639       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
5640       ik->print_class_load_logging(_loader_data, module_name, _stream);
5641     }
5642 
5643     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5644         ik->major_version() != JAVA_MIN_SUPPORTED_VERSION &&
5645         log_is_enabled(Info, class, preview)) {
5646       ResourceMark rm;
5647       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5648                                ik->external_name(), ik->major_version());
5649     }
5650 
5651     if (log_is_enabled(Debug, class, resolve))  {
5652       ResourceMark rm;
5653       // print out the superclass.
5654       const char * from = ik->external_name();
5655       if (ik->java_super() != NULL) {
5656         log_debug(class, resolve)("%s %s (super)",
5657                    from,
5658                    ik->java_super()->external_name());
5659       }
5660       // print out each of the interface classes referred to by this class.
5661       const Array<InstanceKlass*>* const local_interfaces = ik->local_interfaces();
5662       if (local_interfaces != NULL) {
5663         const int length = local_interfaces->length();
5664         for (int i = 0; i < length; i++) {
5665           const InstanceKlass* const k = local_interfaces->at(i);
5666           const char * to = k->external_name();
5667           log_debug(class, resolve)("%s %s (interface)", from, to);
5668         }


< prev index next >