hotspot/src/share/vm/classfile/verifier.cpp
changeset 46554 aa1cfd918c4f
parent 46403 f2b91b928476
child 46620 750c6edff33b
equal deleted inserted replaced
45608:9927a9f16738 46554:aa1cfd918c4f
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    52 #include "logging/log.hpp"
    52 #include "logging/log.hpp"
    53 
    53 
    54 #define NOFAILOVER_MAJOR_VERSION                       51
    54 #define NOFAILOVER_MAJOR_VERSION                       51
    55 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION  51
    55 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION  51
    56 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION       52
    56 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION       52
       
    57 #define MAX_ARRAY_DIMENSIONS 255
    57 
    58 
    58 // Access to external entry for VerifyClassCodes - old byte code verifier
    59 // Access to external entry for VerifyClassCodes - old byte code verifier
    59 
    60 
    60 extern "C" {
    61 extern "C" {
    61   typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint);
    62   typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint);
   123     st->print_cr("Verification for %s failed", klassName);
   124     st->print_cr("Verification for %s failed", klassName);
   124   }
   125   }
   125   st->print_cr("End class verification for: %s", klassName);
   126   st->print_cr("End class verification for: %s", klassName);
   126 }
   127 }
   127 
   128 
   128 bool Verifier::verify(instanceKlassHandle klass, Verifier::Mode mode, bool should_verify_class, TRAPS) {
   129 bool Verifier::verify(InstanceKlass* klass, Verifier::Mode mode, bool should_verify_class, TRAPS) {
   129   HandleMark hm;
   130   HandleMark hm(THREAD);
   130   ResourceMark rm(THREAD);
   131   ResourceMark rm(THREAD);
   131 
   132 
   132   // Eagerly allocate the identity hash code for a klass. This is a fallout
   133   // Eagerly allocate the identity hash code for a klass. This is a fallout
   133   // from 6320749 and 8059924: hash code generator is not supposed to be called
   134   // from 6320749 and 8059924: hash code generator is not supposed to be called
   134   // during the safepoint, but it allows to sneak the hashcode in during
   135   // during the safepoint, but it allows to sneak the hashcode in during
   201     return false; // use the existing exception
   202     return false; // use the existing exception
   202   } else if (exception_name == NULL) {
   203   } else if (exception_name == NULL) {
   203     return true; // verifcation succeeded
   204     return true; // verifcation succeeded
   204   } else { // VerifyError or ClassFormatError to be created and thrown
   205   } else { // VerifyError or ClassFormatError to be created and thrown
   205     ResourceMark rm(THREAD);
   206     ResourceMark rm(THREAD);
   206     instanceKlassHandle kls =
   207     Klass* kls =
   207       SystemDictionary::resolve_or_fail(exception_name, true, CHECK_false);
   208       SystemDictionary::resolve_or_fail(exception_name, true, CHECK_false);
   208     if (log_is_enabled(Debug, class, resolve)) {
   209     if (log_is_enabled(Debug, class, resolve)) {
   209       Verifier::trace_class_resolution(kls(), klass());
   210       Verifier::trace_class_resolution(kls, klass);
   210     }
   211     }
   211 
   212 
   212     while (!kls.is_null()) {
   213     while (kls != NULL) {
   213       if (kls == klass) {
   214       if (kls == klass) {
   214         // If the class being verified is the exception we're creating
   215         // If the class being verified is the exception we're creating
   215         // or one of it's superclasses, we're in trouble and are going
   216         // or one of it's superclasses, we're in trouble and are going
   216         // to infinitely recurse when we try to initialize the exception.
   217         // to infinitely recurse when we try to initialize the exception.
   217         // So bail out here by throwing the preallocated VM error.
   218         // So bail out here by throwing the preallocated VM error.
   222     message_buffer[message_buffer_len - 1] = '\0'; // just to be sure
   223     message_buffer[message_buffer_len - 1] = '\0'; // just to be sure
   223     THROW_MSG_(exception_name, exception_message, false);
   224     THROW_MSG_(exception_name, exception_message, false);
   224   }
   225   }
   225 }
   226 }
   226 
   227 
   227 bool Verifier::is_eligible_for_verification(instanceKlassHandle klass, bool should_verify_class) {
   228 bool Verifier::is_eligible_for_verification(InstanceKlass* klass, bool should_verify_class) {
   228   Symbol* name = klass->name();
   229   Symbol* name = klass->name();
   229   Klass* refl_magic_klass = SystemDictionary::reflect_MagicAccessorImpl_klass();
   230   Klass* refl_magic_klass = SystemDictionary::reflect_MagicAccessorImpl_klass();
   230 
   231 
   231   bool is_reflect = refl_magic_klass != NULL && klass->is_subtype_of(refl_magic_klass);
   232   bool is_reflect = refl_magic_klass != NULL && klass->is_subtype_of(refl_magic_klass);
   232 
   233 
   241 
   242 
   242     // Can not verify the bytecodes for shared classes because they have
   243     // Can not verify the bytecodes for shared classes because they have
   243     // already been rewritten to contain constant pool cache indices,
   244     // already been rewritten to contain constant pool cache indices,
   244     // which the verifier can't understand.
   245     // which the verifier can't understand.
   245     // Shared classes shouldn't have stackmaps either.
   246     // Shared classes shouldn't have stackmaps either.
   246     !klass()->is_shared() &&
   247     !klass->is_shared() &&
   247 
   248 
   248     // As of the fix for 4486457 we disable verification for all of the
   249     // As of the fix for 4486457 we disable verification for all of the
   249     // dynamically-generated bytecodes associated with the 1.4
   250     // dynamically-generated bytecodes associated with the 1.4
   250     // reflection implementation, not just those associated with
   251     // reflection implementation, not just those associated with
   251     // jdk/internal/reflect/SerializationConstructorAccessor.
   252     // jdk/internal/reflect/SerializationConstructorAccessor.
   254     // Also for lambda generated code, gte jdk8
   255     // Also for lambda generated code, gte jdk8
   255     (!is_reflect));
   256     (!is_reflect));
   256 }
   257 }
   257 
   258 
   258 Symbol* Verifier::inference_verify(
   259 Symbol* Verifier::inference_verify(
   259     instanceKlassHandle klass, char* message, size_t message_len, TRAPS) {
   260     InstanceKlass* klass, char* message, size_t message_len, TRAPS) {
   260   JavaThread* thread = (JavaThread*)THREAD;
   261   JavaThread* thread = (JavaThread*)THREAD;
   261   JNIEnv *env = thread->jni_environment();
   262   JNIEnv *env = thread->jni_environment();
   262 
   263 
   263   void* verify_func = verify_byte_codes_fn();
   264   void* verify_func = verify_byte_codes_fn();
   264 
   265 
   557 }
   558 }
   558 
   559 
   559 // Methods in ClassVerifier
   560 // Methods in ClassVerifier
   560 
   561 
   561 ClassVerifier::ClassVerifier(
   562 ClassVerifier::ClassVerifier(
   562     instanceKlassHandle klass, TRAPS)
   563     InstanceKlass* klass, TRAPS)
   563     : _thread(THREAD), _exception_type(NULL), _message(NULL), _klass(klass) {
   564     : _thread(THREAD), _exception_type(NULL), _message(NULL), _klass(klass) {
   564   _this_type = VerificationType::reference_type(klass->name());
   565   _this_type = VerificationType::reference_type(klass->name());
   565   // Create list to hold symbols in reference area.
   566   // Create list to hold symbols in reference area.
   566   _symbols = new GrowableArray<Symbol*>(100, 0, NULL);
   567   _symbols = new GrowableArray<Symbol*>(100, 0, NULL);
   567 }
   568 }
  1989   }
  1990   }
  1990   _message = ss.as_string();
  1991   _message = ss.as_string();
  1991 }
  1992 }
  1992 
  1993 
  1993 Klass* ClassVerifier::load_class(Symbol* name, TRAPS) {
  1994 Klass* ClassVerifier::load_class(Symbol* name, TRAPS) {
       
  1995   HandleMark hm(THREAD);
  1994   // Get current loader and protection domain first.
  1996   // Get current loader and protection domain first.
  1995   oop loader = current_class()->class_loader();
  1997   oop loader = current_class()->class_loader();
  1996   oop protection_domain = current_class()->protection_domain();
  1998   oop protection_domain = current_class()->protection_domain();
  1997 
  1999 
  1998   Klass* kls = SystemDictionary::resolve_or_fail(
  2000   Klass* kls = SystemDictionary::resolve_or_fail(
  1999     name, Handle(THREAD, loader), Handle(THREAD, protection_domain),
  2001     name, Handle(THREAD, loader), Handle(THREAD, protection_domain),
  2000     true, THREAD);
  2002     true, THREAD);
  2001 
  2003 
  2002   if (log_is_enabled(Debug, class, resolve)) {
  2004   if (log_is_enabled(Debug, class, resolve)) {
  2003     instanceKlassHandle cur_class = current_class();
  2005     InstanceKlass* cur_class = InstanceKlass::cast(current_class());
  2004     Verifier::trace_class_resolution(kls, cur_class());
  2006     Verifier::trace_class_resolution(kls, cur_class);
  2005   }
  2007   }
  2006   return kls;
  2008   return kls;
  2007 }
  2009 }
  2008 
  2010 
  2009 bool ClassVerifier::is_protected_access(instanceKlassHandle this_class,
  2011 bool ClassVerifier::is_protected_access(InstanceKlass* this_class,
  2010                                         Klass* target_class,
  2012                                         Klass* target_class,
  2011                                         Symbol* field_name,
  2013                                         Symbol* field_name,
  2012                                         Symbol* field_sig,
  2014                                         Symbol* field_sig,
  2013                                         bool is_method) {
  2015                                         bool is_method) {
  2014   NoSafepointVerifier nosafepoint;
  2016   NoSafepointVerifier nosafepoint;
  2167   }
  2169   }
  2168   NOT_PRODUCT(aligned_bcp = NULL);  // no longer valid at this point
  2170   NOT_PRODUCT(aligned_bcp = NULL);  // no longer valid at this point
  2169 }
  2171 }
  2170 
  2172 
  2171 bool ClassVerifier::name_in_supers(
  2173 bool ClassVerifier::name_in_supers(
  2172     Symbol* ref_name, instanceKlassHandle current) {
  2174     Symbol* ref_name, InstanceKlass* current) {
  2173   Klass* super = current->super();
  2175   Klass* super = current->super();
  2174   while (super != NULL) {
  2176   while (super != NULL) {
  2175     if (super->name() == ref_name) {
  2177     if (super->name() == ref_name) {
  2176       return true;
  2178       return true;
  2177     }
  2179     }
  2597         vmSymbols::object_initializer_name(),
  2599         vmSymbols::object_initializer_name(),
  2598         cp->signature_ref_at(bcs->get_index_u2()),
  2600         cp->signature_ref_at(bcs->get_index_u2()),
  2599         Klass::find_overpass);
  2601         Klass::find_overpass);
  2600       // Do nothing if method is not found.  Let resolution detect the error.
  2602       // Do nothing if method is not found.  Let resolution detect the error.
  2601       if (m != NULL) {
  2603       if (m != NULL) {
  2602         instanceKlassHandle mh(THREAD, m->method_holder());
  2604         InstanceKlass* mh = m->method_holder();
  2603         if (m->is_protected() && !mh->is_same_class_package(_klass())) {
  2605         if (m->is_protected() && !mh->is_same_class_package(_klass)) {
  2604           bool assignable = current_type().is_assignable_from(
  2606           bool assignable = current_type().is_assignable_from(
  2605             objectref_type, this, true, CHECK_VERIFY(this));
  2607             objectref_type, this, true, CHECK_VERIFY(this));
  2606           if (!assignable) {
  2608           if (!assignable) {
  2607             verify_error(ErrorContext::bad_type(bci,
  2609             verify_error(ErrorContext::bad_type(bci,
  2608                 TypeOrigin::cp(new_class_index, objectref_type),
  2610                 TypeOrigin::cp(new_class_index, objectref_type),
  2628     return;
  2630     return;
  2629   }
  2631   }
  2630 }
  2632 }
  2631 
  2633 
  2632 bool ClassVerifier::is_same_or_direct_interface(
  2634 bool ClassVerifier::is_same_or_direct_interface(
  2633     instanceKlassHandle klass,
  2635     InstanceKlass* klass,
  2634     VerificationType klass_type,
  2636     VerificationType klass_type,
  2635     VerificationType ref_class_type) {
  2637     VerificationType ref_class_type) {
  2636   if (ref_class_type.equals(klass_type)) return true;
  2638   if (ref_class_type.equals(klass_type)) return true;
  2637   Array<Klass*>* local_interfaces = klass->local_interfaces();
  2639   Array<Klass*>* local_interfaces = klass->local_interfaces();
  2638   if (local_interfaces != NULL) {
  2640   if (local_interfaces != NULL) {
  2928     cp_index_to_type(index, cp, CHECK_VERIFY(this));
  2930     cp_index_to_type(index, cp, CHECK_VERIFY(this));
  2929   int length;
  2931   int length;
  2930   char* arr_sig_str;
  2932   char* arr_sig_str;
  2931   if (component_type.is_array()) {     // it's an array
  2933   if (component_type.is_array()) {     // it's an array
  2932     const char* component_name = component_type.name()->as_utf8();
  2934     const char* component_name = component_type.name()->as_utf8();
       
  2935     // Check for more than MAX_ARRAY_DIMENSIONS
       
  2936     length = (int)strlen(component_name);
       
  2937     if (length > MAX_ARRAY_DIMENSIONS &&
       
  2938         component_name[MAX_ARRAY_DIMENSIONS - 1] == '[') {
       
  2939       verify_error(ErrorContext::bad_code(bci),
       
  2940         "Illegal anewarray instruction, array has more than 255 dimensions");
       
  2941     }
  2933     // add one dimension to component
  2942     // add one dimension to component
  2934     length = (int)strlen(component_name) + 1;
  2943     length++;
  2935     arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length);
  2944     arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length);
  2936     arr_sig_str[0] = '[';
  2945     arr_sig_str[0] = '[';
  2937     strncpy(&arr_sig_str[1], component_name, length - 1);
  2946     strncpy(&arr_sig_str[1], component_name, length - 1);
  2938   } else {         // it's an object or interface
  2947   } else {         // it's an object or interface
  2939     const char* component_name = component_type.name()->as_utf8();
  2948     const char* component_name = component_type.name()->as_utf8();