src/share/vm/classfile/verifier.cpp

Wed, 13 Mar 2013 15:15:56 -0400

author
coleenp
date
Wed, 13 Mar 2013 15:15:56 -0400
changeset 4718
0ede345ec7c9
parent 4601
3a531d40ad93
child 4816
729be16a470b
permissions
-rw-r--r--

8009829: CDS: JDK JPRT test fails crash in Symbol::equals()
Summary: -Xshare:dump was creating a Symbol in C_heap. There's an assert there that jdk jprt wasn't hitting because it was only done in product
Reviewed-by: dholmes, hseigel, iklam

     1 /*
     2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     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  */
    25 #include "precompiled.hpp"
    26 #include "classfile/classFileStream.hpp"
    27 #include "classfile/javaClasses.hpp"
    28 #include "classfile/stackMapTable.hpp"
    29 #include "classfile/stackMapFrame.hpp"
    30 #include "classfile/stackMapTableFormat.hpp"
    31 #include "classfile/systemDictionary.hpp"
    32 #include "classfile/verifier.hpp"
    33 #include "classfile/vmSymbols.hpp"
    34 #include "interpreter/bytecodes.hpp"
    35 #include "interpreter/bytecodeStream.hpp"
    36 #include "memory/oopFactory.hpp"
    37 #include "memory/resourceArea.hpp"
    38 #include "oops/instanceKlass.hpp"
    39 #include "oops/oop.inline.hpp"
    40 #include "oops/typeArrayOop.hpp"
    41 #include "prims/jvm.h"
    42 #include "runtime/fieldDescriptor.hpp"
    43 #include "runtime/handles.inline.hpp"
    44 #include "runtime/interfaceSupport.hpp"
    45 #include "runtime/javaCalls.hpp"
    46 #include "runtime/orderAccess.hpp"
    47 #include "runtime/os.hpp"
    48 #ifdef TARGET_ARCH_x86
    49 # include "bytes_x86.hpp"
    50 #endif
    51 #ifdef TARGET_ARCH_sparc
    52 # include "bytes_sparc.hpp"
    53 #endif
    54 #ifdef TARGET_ARCH_zero
    55 # include "bytes_zero.hpp"
    56 #endif
    57 #ifdef TARGET_ARCH_arm
    58 # include "bytes_arm.hpp"
    59 #endif
    60 #ifdef TARGET_ARCH_ppc
    61 # include "bytes_ppc.hpp"
    62 #endif
    64 #define NOFAILOVER_MAJOR_VERSION                  51
    65 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION  52
    67 // Access to external entry for VerifyClassCodes - old byte code verifier
    69 extern "C" {
    70   typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint);
    71   typedef jboolean (*verify_byte_codes_fn_new_t)(JNIEnv *, jclass, char *, jint, jint);
    72 }
    74 static void* volatile _verify_byte_codes_fn = NULL;
    76 static volatile jint _is_new_verify_byte_codes_fn = (jint) true;
    78 static void* verify_byte_codes_fn() {
    79   if (_verify_byte_codes_fn == NULL) {
    80     void *lib_handle = os::native_java_library();
    81     void *func = os::dll_lookup(lib_handle, "VerifyClassCodesForMajorVersion");
    82     OrderAccess::release_store_ptr(&_verify_byte_codes_fn, func);
    83     if (func == NULL) {
    84       OrderAccess::release_store(&_is_new_verify_byte_codes_fn, false);
    85       func = os::dll_lookup(lib_handle, "VerifyClassCodes");
    86       OrderAccess::release_store_ptr(&_verify_byte_codes_fn, func);
    87     }
    88   }
    89   return (void*)_verify_byte_codes_fn;
    90 }
    93 // Methods in Verifier
    95 bool Verifier::should_verify_for(oop class_loader, bool should_verify_class) {
    96   return (class_loader == NULL || !should_verify_class) ?
    97     BytecodeVerificationLocal : BytecodeVerificationRemote;
    98 }
   100 bool Verifier::relax_verify_for(oop loader) {
   101   bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
   102   bool need_verify =
   103     // verifyAll
   104     (BytecodeVerificationLocal && BytecodeVerificationRemote) ||
   105     // verifyRemote
   106     (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);
   107   return !need_verify;
   108 }
   110 bool Verifier::verify(instanceKlassHandle klass, Verifier::Mode mode, bool should_verify_class, TRAPS) {
   111   HandleMark hm;
   112   ResourceMark rm(THREAD);
   114   Symbol* exception_name = NULL;
   115   const size_t message_buffer_len = klass->name()->utf8_length() + 1024;
   116   char* message_buffer = NEW_RESOURCE_ARRAY(char, message_buffer_len);
   117   char* exception_message = message_buffer;
   119   const char* klassName = klass->external_name();
   120   bool can_failover = FailOverToOldVerifier &&
   121       klass->major_version() < NOFAILOVER_MAJOR_VERSION;
   123   // If the class should be verified, first see if we can use the split
   124   // verifier.  If not, or if verification fails and FailOverToOldVerifier
   125   // is set, then call the inference verifier.
   126   if (is_eligible_for_verification(klass, should_verify_class)) {
   127     if (TraceClassInitialization) {
   128       tty->print_cr("Start class verification for: %s", klassName);
   129     }
   130     if (UseSplitVerifier &&
   131         klass->major_version() >= STACKMAP_ATTRIBUTE_MAJOR_VERSION) {
   132       ClassVerifier split_verifier(klass, THREAD);
   133       split_verifier.verify_class(THREAD);
   134       exception_name = split_verifier.result();
   135       if (can_failover && !HAS_PENDING_EXCEPTION &&
   136           (exception_name == vmSymbols::java_lang_VerifyError() ||
   137            exception_name == vmSymbols::java_lang_ClassFormatError())) {
   138         if (TraceClassInitialization || VerboseVerification) {
   139           tty->print_cr(
   140             "Fail over class verification to old verifier for: %s", klassName);
   141         }
   142         exception_name = inference_verify(
   143           klass, message_buffer, message_buffer_len, THREAD);
   144       }
   145       if (exception_name != NULL) {
   146         exception_message = split_verifier.exception_message();
   147       }
   148     } else {
   149       exception_name = inference_verify(
   150           klass, message_buffer, message_buffer_len, THREAD);
   151     }
   153     if (TraceClassInitialization || VerboseVerification) {
   154       if (HAS_PENDING_EXCEPTION) {
   155         tty->print("Verification for %s has", klassName);
   156         tty->print_cr(" exception pending %s ",
   157           InstanceKlass::cast(PENDING_EXCEPTION->klass())->external_name());
   158       } else if (exception_name != NULL) {
   159         tty->print_cr("Verification for %s failed", klassName);
   160       }
   161       tty->print_cr("End class verification for: %s", klassName);
   162     }
   163   }
   165   if (HAS_PENDING_EXCEPTION) {
   166     return false; // use the existing exception
   167   } else if (exception_name == NULL) {
   168     return true; // verifcation succeeded
   169   } else { // VerifyError or ClassFormatError to be created and thrown
   170     ResourceMark rm(THREAD);
   171     instanceKlassHandle kls =
   172       SystemDictionary::resolve_or_fail(exception_name, true, CHECK_false);
   173     while (!kls.is_null()) {
   174       if (kls == klass) {
   175         // If the class being verified is the exception we're creating
   176         // or one of it's superclasses, we're in trouble and are going
   177         // to infinitely recurse when we try to initialize the exception.
   178         // So bail out here by throwing the preallocated VM error.
   179         THROW_OOP_(Universe::virtual_machine_error_instance(), false);
   180       }
   181       kls = kls->super();
   182     }
   183     message_buffer[message_buffer_len - 1] = '\0'; // just to be sure
   184     THROW_MSG_(exception_name, exception_message, false);
   185   }
   186 }
   188 bool Verifier::is_eligible_for_verification(instanceKlassHandle klass, bool should_verify_class) {
   189   Symbol* name = klass->name();
   190   Klass* refl_magic_klass = SystemDictionary::reflect_MagicAccessorImpl_klass();
   192   return (should_verify_for(klass->class_loader(), should_verify_class) &&
   193     // return if the class is a bootstrapping class
   194     // or defineClass specified not to verify by default (flags override passed arg)
   195     // We need to skip the following four for bootstraping
   196     name != vmSymbols::java_lang_Object() &&
   197     name != vmSymbols::java_lang_Class() &&
   198     name != vmSymbols::java_lang_String() &&
   199     name != vmSymbols::java_lang_Throwable() &&
   201     // Can not verify the bytecodes for shared classes because they have
   202     // already been rewritten to contain constant pool cache indices,
   203     // which the verifier can't understand.
   204     // Shared classes shouldn't have stackmaps either.
   205     !klass()->is_shared() &&
   207     // As of the fix for 4486457 we disable verification for all of the
   208     // dynamically-generated bytecodes associated with the 1.4
   209     // reflection implementation, not just those associated with
   210     // sun/reflect/SerializationConstructorAccessor.
   211     // NOTE: this is called too early in the bootstrapping process to be
   212     // guarded by Universe::is_gte_jdk14x_version()/UseNewReflection.
   213     (refl_magic_klass == NULL ||
   214      !klass->is_subtype_of(refl_magic_klass) ||
   215      VerifyReflectionBytecodes)
   216   );
   217 }
   219 Symbol* Verifier::inference_verify(
   220     instanceKlassHandle klass, char* message, size_t message_len, TRAPS) {
   221   JavaThread* thread = (JavaThread*)THREAD;
   222   JNIEnv *env = thread->jni_environment();
   224   void* verify_func = verify_byte_codes_fn();
   226   if (verify_func == NULL) {
   227     jio_snprintf(message, message_len, "Could not link verifier");
   228     return vmSymbols::java_lang_VerifyError();
   229   }
   231   ResourceMark rm(THREAD);
   232   if (VerboseVerification) {
   233     tty->print_cr("Verifying class %s with old format", klass->external_name());
   234   }
   236   jclass cls = (jclass) JNIHandles::make_local(env, klass->java_mirror());
   237   jint result;
   239   {
   240     HandleMark hm(thread);
   241     ThreadToNativeFromVM ttn(thread);
   242     // ThreadToNativeFromVM takes care of changing thread_state, so safepoint
   243     // code knows that we have left the VM
   245     if (_is_new_verify_byte_codes_fn) {
   246       verify_byte_codes_fn_new_t func =
   247         CAST_TO_FN_PTR(verify_byte_codes_fn_new_t, verify_func);
   248       result = (*func)(env, cls, message, (int)message_len,
   249           klass->major_version());
   250     } else {
   251       verify_byte_codes_fn_t func =
   252         CAST_TO_FN_PTR(verify_byte_codes_fn_t, verify_func);
   253       result = (*func)(env, cls, message, (int)message_len);
   254     }
   255   }
   257   JNIHandles::destroy_local(cls);
   259   // These numbers are chosen so that VerifyClassCodes interface doesn't need
   260   // to be changed (still return jboolean (unsigned char)), and result is
   261   // 1 when verification is passed.
   262   if (result == 0) {
   263     return vmSymbols::java_lang_VerifyError();
   264   } else if (result == 1) {
   265     return NULL; // verified.
   266   } else if (result == 2) {
   267     THROW_MSG_(vmSymbols::java_lang_OutOfMemoryError(), message, NULL);
   268   } else if (result == 3) {
   269     return vmSymbols::java_lang_ClassFormatError();
   270   } else {
   271     ShouldNotReachHere();
   272     return NULL;
   273   }
   274 }
   276 TypeOrigin TypeOrigin::null() {
   277   return TypeOrigin();
   278 }
   279 TypeOrigin TypeOrigin::local(u2 index, StackMapFrame* frame) {
   280   assert(frame != NULL, "Must have a frame");
   281   return TypeOrigin(CF_LOCALS, index, StackMapFrame::copy(frame),
   282      frame->local_at(index));
   283 }
   284 TypeOrigin TypeOrigin::stack(u2 index, StackMapFrame* frame) {
   285   assert(frame != NULL, "Must have a frame");
   286   return TypeOrigin(CF_STACK, index, StackMapFrame::copy(frame),
   287       frame->stack_at(index));
   288 }
   289 TypeOrigin TypeOrigin::sm_local(u2 index, StackMapFrame* frame) {
   290   assert(frame != NULL, "Must have a frame");
   291   return TypeOrigin(SM_LOCALS, index, StackMapFrame::copy(frame),
   292       frame->local_at(index));
   293 }
   294 TypeOrigin TypeOrigin::sm_stack(u2 index, StackMapFrame* frame) {
   295   assert(frame != NULL, "Must have a frame");
   296   return TypeOrigin(SM_STACK, index, StackMapFrame::copy(frame),
   297       frame->stack_at(index));
   298 }
   299 TypeOrigin TypeOrigin::bad_index(u2 index) {
   300   return TypeOrigin(BAD_INDEX, index, NULL, VerificationType::bogus_type());
   301 }
   302 TypeOrigin TypeOrigin::cp(u2 index, VerificationType vt) {
   303   return TypeOrigin(CONST_POOL, index, NULL, vt);
   304 }
   305 TypeOrigin TypeOrigin::signature(VerificationType vt) {
   306   return TypeOrigin(SIG, 0, NULL, vt);
   307 }
   308 TypeOrigin TypeOrigin::implicit(VerificationType t) {
   309   return TypeOrigin(IMPLICIT, 0, NULL, t);
   310 }
   311 TypeOrigin TypeOrigin::frame(StackMapFrame* frame) {
   312   return TypeOrigin(FRAME_ONLY, 0, StackMapFrame::copy(frame),
   313                     VerificationType::bogus_type());
   314 }
   316 void TypeOrigin::reset_frame() {
   317   if (_frame != NULL) {
   318     _frame->restore();
   319   }
   320 }
   322 void TypeOrigin::details(outputStream* ss) const {
   323   _type.print_on(ss);
   324   switch (_origin) {
   325     case CF_LOCALS:
   326       ss->print(" (current frame, locals[%d])", _index);
   327       break;
   328     case CF_STACK:
   329       ss->print(" (current frame, stack[%d])", _index);
   330       break;
   331     case SM_LOCALS:
   332       ss->print(" (stack map, locals[%d])", _index);
   333       break;
   334     case SM_STACK:
   335       ss->print(" (stack map, stack[%d])", _index);
   336       break;
   337     case CONST_POOL:
   338       ss->print(" (constant pool %d)", _index);
   339       break;
   340     case SIG:
   341       ss->print(" (from method signature)");
   342       break;
   343     case IMPLICIT:
   344     case FRAME_ONLY:
   345     case NONE:
   346     default:
   347       ;
   348   }
   349 }
   351 #ifdef ASSERT
   352 void TypeOrigin::print_on(outputStream* str) const {
   353   str->print("{%d,%d,%p:", _origin, _index, _frame);
   354   if (_frame != NULL) {
   355     _frame->print_on(str);
   356   } else {
   357     str->print("null");
   358   }
   359   str->print(",");
   360   _type.print_on(str);
   361   str->print("}");
   362 }
   363 #endif
   365 void ErrorContext::details(outputStream* ss, Method* method) const {
   366   if (is_valid()) {
   367     ss->print_cr("");
   368     ss->print_cr("Exception Details:");
   369     location_details(ss, method);
   370     reason_details(ss);
   371     frame_details(ss);
   372     bytecode_details(ss, method);
   373     handler_details(ss, method);
   374     stackmap_details(ss, method);
   375   }
   376 }
   378 void ErrorContext::reason_details(outputStream* ss) const {
   379   streamIndentor si(ss);
   380   ss->indent().print_cr("Reason:");
   381   streamIndentor si2(ss);
   382   ss->indent().print("");
   383   switch (_fault) {
   384     case INVALID_BYTECODE:
   385       ss->print("Error exists in the bytecode");
   386       break;
   387     case WRONG_TYPE:
   388       if (_expected.is_valid()) {
   389         ss->print("Type ");
   390         _type.details(ss);
   391         ss->print(" is not assignable to ");
   392         _expected.details(ss);
   393       } else {
   394         ss->print("Invalid type: ");
   395         _type.details(ss);
   396       }
   397       break;
   398     case FLAGS_MISMATCH:
   399       if (_expected.is_valid()) {
   400         ss->print("Current frame's flags are not assignable "
   401                   "to stack map frame's.");
   402       } else {
   403         ss->print("Current frame's flags are invalid in this context.");
   404       }
   405       break;
   406     case BAD_CP_INDEX:
   407       ss->print("Constant pool index %d is invalid", _type.index());
   408       break;
   409     case BAD_LOCAL_INDEX:
   410       ss->print("Local index %d is invalid", _type.index());
   411       break;
   412     case LOCALS_SIZE_MISMATCH:
   413       ss->print("Current frame's local size doesn't match stackmap.");
   414       break;
   415     case STACK_SIZE_MISMATCH:
   416       ss->print("Current frame's stack size doesn't match stackmap.");
   417       break;
   418     case STACK_OVERFLOW:
   419       ss->print("Exceeded max stack size.");
   420       break;
   421     case STACK_UNDERFLOW:
   422       ss->print("Attempt to pop empty stack.");
   423       break;
   424     case MISSING_STACKMAP:
   425       ss->print("Expected stackmap frame at this location.");
   426       break;
   427     case BAD_STACKMAP:
   428       ss->print("Invalid stackmap specification.");
   429       break;
   430     case UNKNOWN:
   431     default:
   432       ShouldNotReachHere();
   433       ss->print_cr("Unknown");
   434   }
   435   ss->print_cr("");
   436 }
   438 void ErrorContext::location_details(outputStream* ss, Method* method) const {
   439   if (_bci != -1 && method != NULL) {
   440     streamIndentor si(ss);
   441     const char* bytecode_name = "<invalid>";
   442     if (method->validate_bci_from_bcx(_bci) != -1) {
   443       Bytecodes::Code code = Bytecodes::code_or_bp_at(method->bcp_from(_bci));
   444       if (Bytecodes::is_defined(code)) {
   445           bytecode_name = Bytecodes::name(code);
   446       } else {
   447           bytecode_name = "<illegal>";
   448       }
   449     }
   450     InstanceKlass* ik = method->method_holder();
   451     ss->indent().print_cr("Location:");
   452     streamIndentor si2(ss);
   453     ss->indent().print_cr("%s.%s%s @%d: %s",
   454         ik->name()->as_C_string(), method->name()->as_C_string(),
   455         method->signature()->as_C_string(), _bci, bytecode_name);
   456   }
   457 }
   459 void ErrorContext::frame_details(outputStream* ss) const {
   460   streamIndentor si(ss);
   461   if (_type.is_valid() && _type.frame() != NULL) {
   462     ss->indent().print_cr("Current Frame:");
   463     streamIndentor si2(ss);
   464     _type.frame()->print_on(ss);
   465   }
   466   if (_expected.is_valid() && _expected.frame() != NULL) {
   467     ss->indent().print_cr("Stackmap Frame:");
   468     streamIndentor si2(ss);
   469     _expected.frame()->print_on(ss);
   470   }
   471 }
   473 void ErrorContext::bytecode_details(outputStream* ss, Method* method) const {
   474   if (method != NULL) {
   475     streamIndentor si(ss);
   476     ss->indent().print_cr("Bytecode:");
   477     streamIndentor si2(ss);
   478     ss->print_data(method->code_base(), method->code_size(), false);
   479   }
   480 }
   482 void ErrorContext::handler_details(outputStream* ss, Method* method) const {
   483   if (method != NULL) {
   484     streamIndentor si(ss);
   485     ExceptionTable table(method);
   486     if (table.length() > 0) {
   487       ss->indent().print_cr("Exception Handler Table:");
   488       streamIndentor si2(ss);
   489       for (int i = 0; i < table.length(); ++i) {
   490         ss->indent().print_cr("bci [%d, %d] => handler: %d", table.start_pc(i),
   491             table.end_pc(i), table.handler_pc(i));
   492       }
   493     }
   494   }
   495 }
   497 void ErrorContext::stackmap_details(outputStream* ss, Method* method) const {
   498   if (method != NULL && method->has_stackmap_table()) {
   499     streamIndentor si(ss);
   500     ss->indent().print_cr("Stackmap Table:");
   501     Array<u1>* data = method->stackmap_data();
   502     stack_map_table* sm_table =
   503         stack_map_table::at((address)data->adr_at(0));
   504     stack_map_frame* sm_frame = sm_table->entries();
   505     streamIndentor si2(ss);
   506     int current_offset = -1;
   507     for (u2 i = 0; i < sm_table->number_of_entries(); ++i) {
   508       ss->indent();
   509       sm_frame->print_on(ss, current_offset);
   510       ss->print_cr("");
   511       current_offset += sm_frame->offset_delta();
   512       sm_frame = sm_frame->next();
   513     }
   514   }
   515 }
   517 // Methods in ClassVerifier
   519 ClassVerifier::ClassVerifier(
   520     instanceKlassHandle klass, TRAPS)
   521     : _thread(THREAD), _exception_type(NULL), _message(NULL), _klass(klass) {
   522   _this_type = VerificationType::reference_type(klass->name());
   523   // Create list to hold symbols in reference area.
   524   _symbols = new GrowableArray<Symbol*>(100, 0, NULL);
   525 }
   527 ClassVerifier::~ClassVerifier() {
   528   // Decrement the reference count for any symbols created.
   529   for (int i = 0; i < _symbols->length(); i++) {
   530     Symbol* s = _symbols->at(i);
   531     s->decrement_refcount();
   532   }
   533 }
   535 VerificationType ClassVerifier::object_type() const {
   536   return VerificationType::reference_type(vmSymbols::java_lang_Object());
   537 }
   539 TypeOrigin ClassVerifier::ref_ctx(const char* sig, TRAPS) {
   540   VerificationType vt = VerificationType::reference_type(
   541       create_temporary_symbol(sig, (int)strlen(sig), THREAD));
   542   return TypeOrigin::implicit(vt);
   543 }
   545 void ClassVerifier::verify_class(TRAPS) {
   546   if (VerboseVerification) {
   547     tty->print_cr("Verifying class %s with new format",
   548       _klass->external_name());
   549   }
   551   Array<Method*>* methods = _klass->methods();
   552   int num_methods = methods->length();
   554   for (int index = 0; index < num_methods; index++) {
   555     // Check for recursive re-verification before each method.
   556     if (was_recursively_verified())  return;
   558     Method* m = methods->at(index);
   559     if (m->is_native() || m->is_abstract() || m->is_overpass()) {
   560       // If m is native or abstract, skip it.  It is checked in class file
   561       // parser that methods do not override a final method.  Overpass methods
   562       // are trusted since the VM generates them.
   563       continue;
   564     }
   565     verify_method(methodHandle(THREAD, m), CHECK_VERIFY(this));
   566   }
   568   if (VerboseVerification || TraceClassInitialization) {
   569     if (was_recursively_verified())
   570       tty->print_cr("Recursive verification detected for: %s",
   571           _klass->external_name());
   572   }
   573 }
   575 void ClassVerifier::verify_method(methodHandle m, TRAPS) {
   576   HandleMark hm(THREAD);
   577   _method = m;   // initialize _method
   578   if (VerboseVerification) {
   579     tty->print_cr("Verifying method %s", m->name_and_sig_as_C_string());
   580   }
   582   const char* bad_type_msg = "Bad type on operand stack in %s";
   584   int32_t max_stack = m->verifier_max_stack();
   585   int32_t max_locals = m->max_locals();
   586   constantPoolHandle cp(THREAD, m->constants());
   588   if (!SignatureVerifier::is_valid_method_signature(m->signature())) {
   589     class_format_error("Invalid method signature");
   590     return;
   591   }
   593   // Initial stack map frame: offset is 0, stack is initially empty.
   594   StackMapFrame current_frame(max_locals, max_stack, this);
   595   // Set initial locals
   596   VerificationType return_type = current_frame.set_locals_from_arg(
   597     m, current_type(), CHECK_VERIFY(this));
   599   int32_t stackmap_index = 0; // index to the stackmap array
   601   u4 code_length = m->code_size();
   603   // Scan the bytecode and map each instruction's start offset to a number.
   604   char* code_data = generate_code_data(m, code_length, CHECK_VERIFY(this));
   606   int ex_min = code_length;
   607   int ex_max = -1;
   608   // Look through each item on the exception table. Each of the fields must refer
   609   // to a legal instruction.
   610   verify_exception_handler_table(
   611     code_length, code_data, ex_min, ex_max, CHECK_VERIFY(this));
   613   // Look through each entry on the local variable table and make sure
   614   // its range of code array offsets is valid. (4169817)
   615   if (m->has_localvariable_table()) {
   616     verify_local_variable_table(code_length, code_data, CHECK_VERIFY(this));
   617   }
   619   Array<u1>* stackmap_data = m->stackmap_data();
   620   StackMapStream stream(stackmap_data);
   621   StackMapReader reader(this, &stream, code_data, code_length, THREAD);
   622   StackMapTable stackmap_table(&reader, &current_frame, max_locals, max_stack,
   623                                code_data, code_length, CHECK_VERIFY(this));
   625   if (VerboseVerification) {
   626     stackmap_table.print_on(tty);
   627   }
   629   RawBytecodeStream bcs(m);
   631   // Scan the byte code linearly from the start to the end
   632   bool no_control_flow = false; // Set to true when there is no direct control
   633                                 // flow from current instruction to the next
   634                                 // instruction in sequence
   635   Bytecodes::Code opcode;
   636   while (!bcs.is_last_bytecode()) {
   637     // Check for recursive re-verification before each bytecode.
   638     if (was_recursively_verified())  return;
   640     opcode = bcs.raw_next();
   641     u2 bci = bcs.bci();
   643     // Set current frame's offset to bci
   644     current_frame.set_offset(bci);
   645     current_frame.set_mark();
   647     // Make sure every offset in stackmap table point to the beginning to
   648     // an instruction. Match current_frame to stackmap_table entry with
   649     // the same offset if exists.
   650     stackmap_index = verify_stackmap_table(
   651       stackmap_index, bci, &current_frame, &stackmap_table,
   652       no_control_flow, CHECK_VERIFY(this));
   655     bool this_uninit = false;  // Set to true when invokespecial <init> initialized 'this'
   657     // Merge with the next instruction
   658     {
   659       u2 index;
   660       int target;
   661       VerificationType type, type2;
   662       VerificationType atype;
   664 #ifndef PRODUCT
   665       if (VerboseVerification) {
   666         current_frame.print_on(tty);
   667         tty->print_cr("offset = %d,  opcode = %s", bci, Bytecodes::name(opcode));
   668       }
   669 #endif
   671       // Make sure wide instruction is in correct format
   672       if (bcs.is_wide()) {
   673         if (opcode != Bytecodes::_iinc   && opcode != Bytecodes::_iload  &&
   674             opcode != Bytecodes::_aload  && opcode != Bytecodes::_lload  &&
   675             opcode != Bytecodes::_istore && opcode != Bytecodes::_astore &&
   676             opcode != Bytecodes::_lstore && opcode != Bytecodes::_fload  &&
   677             opcode != Bytecodes::_dload  && opcode != Bytecodes::_fstore &&
   678             opcode != Bytecodes::_dstore) {
   679           /* Unreachable?  RawBytecodeStream's raw_next() returns 'illegal'
   680            * if we encounter a wide instruction that modifies an invalid
   681            * opcode (not one of the ones listed above) */
   682           verify_error(ErrorContext::bad_code(bci), "Bad wide instruction");
   683           return;
   684         }
   685       }
   687       switch (opcode) {
   688         case Bytecodes::_nop :
   689           no_control_flow = false; break;
   690         case Bytecodes::_aconst_null :
   691           current_frame.push_stack(
   692             VerificationType::null_type(), CHECK_VERIFY(this));
   693           no_control_flow = false; break;
   694         case Bytecodes::_iconst_m1 :
   695         case Bytecodes::_iconst_0 :
   696         case Bytecodes::_iconst_1 :
   697         case Bytecodes::_iconst_2 :
   698         case Bytecodes::_iconst_3 :
   699         case Bytecodes::_iconst_4 :
   700         case Bytecodes::_iconst_5 :
   701           current_frame.push_stack(
   702             VerificationType::integer_type(), CHECK_VERIFY(this));
   703           no_control_flow = false; break;
   704         case Bytecodes::_lconst_0 :
   705         case Bytecodes::_lconst_1 :
   706           current_frame.push_stack_2(
   707             VerificationType::long_type(),
   708             VerificationType::long2_type(), CHECK_VERIFY(this));
   709           no_control_flow = false; break;
   710         case Bytecodes::_fconst_0 :
   711         case Bytecodes::_fconst_1 :
   712         case Bytecodes::_fconst_2 :
   713           current_frame.push_stack(
   714             VerificationType::float_type(), CHECK_VERIFY(this));
   715           no_control_flow = false; break;
   716         case Bytecodes::_dconst_0 :
   717         case Bytecodes::_dconst_1 :
   718           current_frame.push_stack_2(
   719             VerificationType::double_type(),
   720             VerificationType::double2_type(), CHECK_VERIFY(this));
   721           no_control_flow = false; break;
   722         case Bytecodes::_sipush :
   723         case Bytecodes::_bipush :
   724           current_frame.push_stack(
   725             VerificationType::integer_type(), CHECK_VERIFY(this));
   726           no_control_flow = false; break;
   727         case Bytecodes::_ldc :
   728           verify_ldc(
   729             opcode, bcs.get_index_u1(), &current_frame,
   730             cp, bci, CHECK_VERIFY(this));
   731           no_control_flow = false; break;
   732         case Bytecodes::_ldc_w :
   733         case Bytecodes::_ldc2_w :
   734           verify_ldc(
   735             opcode, bcs.get_index_u2(), &current_frame,
   736             cp, bci, CHECK_VERIFY(this));
   737           no_control_flow = false; break;
   738         case Bytecodes::_iload :
   739           verify_iload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   740           no_control_flow = false; break;
   741         case Bytecodes::_iload_0 :
   742         case Bytecodes::_iload_1 :
   743         case Bytecodes::_iload_2 :
   744         case Bytecodes::_iload_3 :
   745           index = opcode - Bytecodes::_iload_0;
   746           verify_iload(index, &current_frame, CHECK_VERIFY(this));
   747           no_control_flow = false; break;
   748         case Bytecodes::_lload :
   749           verify_lload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   750           no_control_flow = false; break;
   751         case Bytecodes::_lload_0 :
   752         case Bytecodes::_lload_1 :
   753         case Bytecodes::_lload_2 :
   754         case Bytecodes::_lload_3 :
   755           index = opcode - Bytecodes::_lload_0;
   756           verify_lload(index, &current_frame, CHECK_VERIFY(this));
   757           no_control_flow = false; break;
   758         case Bytecodes::_fload :
   759           verify_fload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   760           no_control_flow = false; break;
   761         case Bytecodes::_fload_0 :
   762         case Bytecodes::_fload_1 :
   763         case Bytecodes::_fload_2 :
   764         case Bytecodes::_fload_3 :
   765           index = opcode - Bytecodes::_fload_0;
   766           verify_fload(index, &current_frame, CHECK_VERIFY(this));
   767           no_control_flow = false; break;
   768         case Bytecodes::_dload :
   769           verify_dload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   770           no_control_flow = false; break;
   771         case Bytecodes::_dload_0 :
   772         case Bytecodes::_dload_1 :
   773         case Bytecodes::_dload_2 :
   774         case Bytecodes::_dload_3 :
   775           index = opcode - Bytecodes::_dload_0;
   776           verify_dload(index, &current_frame, CHECK_VERIFY(this));
   777           no_control_flow = false; break;
   778         case Bytecodes::_aload :
   779           verify_aload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   780           no_control_flow = false; break;
   781         case Bytecodes::_aload_0 :
   782         case Bytecodes::_aload_1 :
   783         case Bytecodes::_aload_2 :
   784         case Bytecodes::_aload_3 :
   785           index = opcode - Bytecodes::_aload_0;
   786           verify_aload(index, &current_frame, CHECK_VERIFY(this));
   787           no_control_flow = false; break;
   788         case Bytecodes::_iaload :
   789           type = current_frame.pop_stack(
   790             VerificationType::integer_type(), CHECK_VERIFY(this));
   791           atype = current_frame.pop_stack(
   792             VerificationType::reference_check(), CHECK_VERIFY(this));
   793           if (!atype.is_int_array()) {
   794             verify_error(ErrorContext::bad_type(bci,
   795                 current_frame.stack_top_ctx(), ref_ctx("[I", THREAD)),
   796                 bad_type_msg, "iaload");
   797             return;
   798           }
   799           current_frame.push_stack(
   800             VerificationType::integer_type(), CHECK_VERIFY(this));
   801           no_control_flow = false; break;
   802         case Bytecodes::_baload :
   803           type = current_frame.pop_stack(
   804             VerificationType::integer_type(), CHECK_VERIFY(this));
   805           atype = current_frame.pop_stack(
   806             VerificationType::reference_check(), CHECK_VERIFY(this));
   807           if (!atype.is_bool_array() && !atype.is_byte_array()) {
   808             verify_error(
   809                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
   810                 bad_type_msg, "baload");
   811             return;
   812           }
   813           current_frame.push_stack(
   814             VerificationType::integer_type(), CHECK_VERIFY(this));
   815           no_control_flow = false; break;
   816         case Bytecodes::_caload :
   817           type = current_frame.pop_stack(
   818             VerificationType::integer_type(), CHECK_VERIFY(this));
   819           atype = current_frame.pop_stack(
   820             VerificationType::reference_check(), CHECK_VERIFY(this));
   821           if (!atype.is_char_array()) {
   822             verify_error(ErrorContext::bad_type(bci,
   823                 current_frame.stack_top_ctx(), ref_ctx("[C", THREAD)),
   824                 bad_type_msg, "caload");
   825             return;
   826           }
   827           current_frame.push_stack(
   828             VerificationType::integer_type(), CHECK_VERIFY(this));
   829           no_control_flow = false; break;
   830         case Bytecodes::_saload :
   831           type = current_frame.pop_stack(
   832             VerificationType::integer_type(), CHECK_VERIFY(this));
   833           atype = current_frame.pop_stack(
   834             VerificationType::reference_check(), CHECK_VERIFY(this));
   835           if (!atype.is_short_array()) {
   836             verify_error(ErrorContext::bad_type(bci,
   837                 current_frame.stack_top_ctx(), ref_ctx("[S", THREAD)),
   838                 bad_type_msg, "saload");
   839             return;
   840           }
   841           current_frame.push_stack(
   842             VerificationType::integer_type(), CHECK_VERIFY(this));
   843           no_control_flow = false; break;
   844         case Bytecodes::_laload :
   845           type = current_frame.pop_stack(
   846             VerificationType::integer_type(), CHECK_VERIFY(this));
   847           atype = current_frame.pop_stack(
   848             VerificationType::reference_check(), CHECK_VERIFY(this));
   849           if (!atype.is_long_array()) {
   850             verify_error(ErrorContext::bad_type(bci,
   851                 current_frame.stack_top_ctx(), ref_ctx("[J", THREAD)),
   852                 bad_type_msg, "laload");
   853             return;
   854           }
   855           current_frame.push_stack_2(
   856             VerificationType::long_type(),
   857             VerificationType::long2_type(), CHECK_VERIFY(this));
   858           no_control_flow = false; break;
   859         case Bytecodes::_faload :
   860           type = current_frame.pop_stack(
   861             VerificationType::integer_type(), CHECK_VERIFY(this));
   862           atype = current_frame.pop_stack(
   863             VerificationType::reference_check(), CHECK_VERIFY(this));
   864           if (!atype.is_float_array()) {
   865             verify_error(ErrorContext::bad_type(bci,
   866                 current_frame.stack_top_ctx(), ref_ctx("[F", THREAD)),
   867                 bad_type_msg, "faload");
   868             return;
   869           }
   870           current_frame.push_stack(
   871             VerificationType::float_type(), CHECK_VERIFY(this));
   872           no_control_flow = false; break;
   873         case Bytecodes::_daload :
   874           type = current_frame.pop_stack(
   875             VerificationType::integer_type(), CHECK_VERIFY(this));
   876           atype = current_frame.pop_stack(
   877             VerificationType::reference_check(), CHECK_VERIFY(this));
   878           if (!atype.is_double_array()) {
   879             verify_error(ErrorContext::bad_type(bci,
   880                 current_frame.stack_top_ctx(), ref_ctx("[D", THREAD)),
   881                 bad_type_msg, "daload");
   882             return;
   883           }
   884           current_frame.push_stack_2(
   885             VerificationType::double_type(),
   886             VerificationType::double2_type(), CHECK_VERIFY(this));
   887           no_control_flow = false; break;
   888         case Bytecodes::_aaload : {
   889           type = current_frame.pop_stack(
   890             VerificationType::integer_type(), CHECK_VERIFY(this));
   891           atype = current_frame.pop_stack(
   892             VerificationType::reference_check(), CHECK_VERIFY(this));
   893           if (!atype.is_reference_array()) {
   894             verify_error(ErrorContext::bad_type(bci,
   895                 current_frame.stack_top_ctx(),
   896                 TypeOrigin::implicit(VerificationType::reference_check())),
   897                 bad_type_msg, "aaload");
   898             return;
   899           }
   900           if (atype.is_null()) {
   901             current_frame.push_stack(
   902               VerificationType::null_type(), CHECK_VERIFY(this));
   903           } else {
   904             VerificationType component =
   905               atype.get_component(this, CHECK_VERIFY(this));
   906             current_frame.push_stack(component, CHECK_VERIFY(this));
   907           }
   908           no_control_flow = false; break;
   909         }
   910         case Bytecodes::_istore :
   911           verify_istore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   912           no_control_flow = false; break;
   913         case Bytecodes::_istore_0 :
   914         case Bytecodes::_istore_1 :
   915         case Bytecodes::_istore_2 :
   916         case Bytecodes::_istore_3 :
   917           index = opcode - Bytecodes::_istore_0;
   918           verify_istore(index, &current_frame, CHECK_VERIFY(this));
   919           no_control_flow = false; break;
   920         case Bytecodes::_lstore :
   921           verify_lstore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   922           no_control_flow = false; break;
   923         case Bytecodes::_lstore_0 :
   924         case Bytecodes::_lstore_1 :
   925         case Bytecodes::_lstore_2 :
   926         case Bytecodes::_lstore_3 :
   927           index = opcode - Bytecodes::_lstore_0;
   928           verify_lstore(index, &current_frame, CHECK_VERIFY(this));
   929           no_control_flow = false; break;
   930         case Bytecodes::_fstore :
   931           verify_fstore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   932           no_control_flow = false; break;
   933         case Bytecodes::_fstore_0 :
   934         case Bytecodes::_fstore_1 :
   935         case Bytecodes::_fstore_2 :
   936         case Bytecodes::_fstore_3 :
   937           index = opcode - Bytecodes::_fstore_0;
   938           verify_fstore(index, &current_frame, CHECK_VERIFY(this));
   939           no_control_flow = false; break;
   940         case Bytecodes::_dstore :
   941           verify_dstore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   942           no_control_flow = false; break;
   943         case Bytecodes::_dstore_0 :
   944         case Bytecodes::_dstore_1 :
   945         case Bytecodes::_dstore_2 :
   946         case Bytecodes::_dstore_3 :
   947           index = opcode - Bytecodes::_dstore_0;
   948           verify_dstore(index, &current_frame, CHECK_VERIFY(this));
   949           no_control_flow = false; break;
   950         case Bytecodes::_astore :
   951           verify_astore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
   952           no_control_flow = false; break;
   953         case Bytecodes::_astore_0 :
   954         case Bytecodes::_astore_1 :
   955         case Bytecodes::_astore_2 :
   956         case Bytecodes::_astore_3 :
   957           index = opcode - Bytecodes::_astore_0;
   958           verify_astore(index, &current_frame, CHECK_VERIFY(this));
   959           no_control_flow = false; break;
   960         case Bytecodes::_iastore :
   961           type = current_frame.pop_stack(
   962             VerificationType::integer_type(), CHECK_VERIFY(this));
   963           type2 = current_frame.pop_stack(
   964             VerificationType::integer_type(), CHECK_VERIFY(this));
   965           atype = current_frame.pop_stack(
   966             VerificationType::reference_check(), CHECK_VERIFY(this));
   967           if (!atype.is_int_array()) {
   968             verify_error(ErrorContext::bad_type(bci,
   969                 current_frame.stack_top_ctx(), ref_ctx("[I", THREAD)),
   970                 bad_type_msg, "iastore");
   971             return;
   972           }
   973           no_control_flow = false; break;
   974         case Bytecodes::_bastore :
   975           type = current_frame.pop_stack(
   976             VerificationType::integer_type(), CHECK_VERIFY(this));
   977           type2 = current_frame.pop_stack(
   978             VerificationType::integer_type(), CHECK_VERIFY(this));
   979           atype = current_frame.pop_stack(
   980             VerificationType::reference_check(), CHECK_VERIFY(this));
   981           if (!atype.is_bool_array() && !atype.is_byte_array()) {
   982             verify_error(
   983                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
   984                 bad_type_msg, "bastore");
   985             return;
   986           }
   987           no_control_flow = false; break;
   988         case Bytecodes::_castore :
   989           current_frame.pop_stack(
   990             VerificationType::integer_type(), CHECK_VERIFY(this));
   991           current_frame.pop_stack(
   992             VerificationType::integer_type(), CHECK_VERIFY(this));
   993           atype = current_frame.pop_stack(
   994             VerificationType::reference_check(), CHECK_VERIFY(this));
   995           if (!atype.is_char_array()) {
   996             verify_error(ErrorContext::bad_type(bci,
   997                 current_frame.stack_top_ctx(), ref_ctx("[C", THREAD)),
   998                 bad_type_msg, "castore");
   999             return;
  1001           no_control_flow = false; break;
  1002         case Bytecodes::_sastore :
  1003           current_frame.pop_stack(
  1004             VerificationType::integer_type(), CHECK_VERIFY(this));
  1005           current_frame.pop_stack(
  1006             VerificationType::integer_type(), CHECK_VERIFY(this));
  1007           atype = current_frame.pop_stack(
  1008             VerificationType::reference_check(), CHECK_VERIFY(this));
  1009           if (!atype.is_short_array()) {
  1010             verify_error(ErrorContext::bad_type(bci,
  1011                 current_frame.stack_top_ctx(), ref_ctx("[S", THREAD)),
  1012                 bad_type_msg, "sastore");
  1013             return;
  1015           no_control_flow = false; break;
  1016         case Bytecodes::_lastore :
  1017           current_frame.pop_stack_2(
  1018             VerificationType::long2_type(),
  1019             VerificationType::long_type(), CHECK_VERIFY(this));
  1020           current_frame.pop_stack(
  1021             VerificationType::integer_type(), CHECK_VERIFY(this));
  1022           atype = current_frame.pop_stack(
  1023             VerificationType::reference_check(), CHECK_VERIFY(this));
  1024           if (!atype.is_long_array()) {
  1025             verify_error(ErrorContext::bad_type(bci,
  1026                 current_frame.stack_top_ctx(), ref_ctx("[J", THREAD)),
  1027                 bad_type_msg, "lastore");
  1028             return;
  1030           no_control_flow = false; break;
  1031         case Bytecodes::_fastore :
  1032           current_frame.pop_stack(
  1033             VerificationType::float_type(), CHECK_VERIFY(this));
  1034           current_frame.pop_stack
  1035             (VerificationType::integer_type(), CHECK_VERIFY(this));
  1036           atype = current_frame.pop_stack(
  1037             VerificationType::reference_check(), CHECK_VERIFY(this));
  1038           if (!atype.is_float_array()) {
  1039             verify_error(ErrorContext::bad_type(bci,
  1040                 current_frame.stack_top_ctx(), ref_ctx("[F", THREAD)),
  1041                 bad_type_msg, "fastore");
  1042             return;
  1044           no_control_flow = false; break;
  1045         case Bytecodes::_dastore :
  1046           current_frame.pop_stack_2(
  1047             VerificationType::double2_type(),
  1048             VerificationType::double_type(), CHECK_VERIFY(this));
  1049           current_frame.pop_stack(
  1050             VerificationType::integer_type(), CHECK_VERIFY(this));
  1051           atype = current_frame.pop_stack(
  1052             VerificationType::reference_check(), CHECK_VERIFY(this));
  1053           if (!atype.is_double_array()) {
  1054             verify_error(ErrorContext::bad_type(bci,
  1055                 current_frame.stack_top_ctx(), ref_ctx("[D", THREAD)),
  1056                 bad_type_msg, "dastore");
  1057             return;
  1059           no_control_flow = false; break;
  1060         case Bytecodes::_aastore :
  1061           type = current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
  1062           type2 = current_frame.pop_stack(
  1063             VerificationType::integer_type(), CHECK_VERIFY(this));
  1064           atype = current_frame.pop_stack(
  1065             VerificationType::reference_check(), CHECK_VERIFY(this));
  1066           // more type-checking is done at runtime
  1067           if (!atype.is_reference_array()) {
  1068             verify_error(ErrorContext::bad_type(bci,
  1069                 current_frame.stack_top_ctx(),
  1070                 TypeOrigin::implicit(VerificationType::reference_check())),
  1071                 bad_type_msg, "aastore");
  1072             return;
  1074           // 4938384: relaxed constraint in JVMS 3nd edition.
  1075           no_control_flow = false; break;
  1076         case Bytecodes::_pop :
  1077           current_frame.pop_stack(
  1078             VerificationType::category1_check(), CHECK_VERIFY(this));
  1079           no_control_flow = false; break;
  1080         case Bytecodes::_pop2 :
  1081           type = current_frame.pop_stack(CHECK_VERIFY(this));
  1082           if (type.is_category1()) {
  1083             current_frame.pop_stack(
  1084               VerificationType::category1_check(), CHECK_VERIFY(this));
  1085           } else if (type.is_category2_2nd()) {
  1086             current_frame.pop_stack(
  1087               VerificationType::category2_check(), CHECK_VERIFY(this));
  1088           } else {
  1089             /* Unreachable? Would need a category2_1st on TOS
  1090              * which does not appear possible. */
  1091             verify_error(
  1092                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
  1093                 bad_type_msg, "pop2");
  1094             return;
  1096           no_control_flow = false; break;
  1097         case Bytecodes::_dup :
  1098           type = current_frame.pop_stack(
  1099             VerificationType::category1_check(), CHECK_VERIFY(this));
  1100           current_frame.push_stack(type, CHECK_VERIFY(this));
  1101           current_frame.push_stack(type, CHECK_VERIFY(this));
  1102           no_control_flow = false; break;
  1103         case Bytecodes::_dup_x1 :
  1104           type = current_frame.pop_stack(
  1105             VerificationType::category1_check(), CHECK_VERIFY(this));
  1106           type2 = current_frame.pop_stack(
  1107             VerificationType::category1_check(), CHECK_VERIFY(this));
  1108           current_frame.push_stack(type, CHECK_VERIFY(this));
  1109           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1110           current_frame.push_stack(type, CHECK_VERIFY(this));
  1111           no_control_flow = false; break;
  1112         case Bytecodes::_dup_x2 :
  1114           VerificationType type3;
  1115           type = current_frame.pop_stack(
  1116             VerificationType::category1_check(), CHECK_VERIFY(this));
  1117           type2 = current_frame.pop_stack(CHECK_VERIFY(this));
  1118           if (type2.is_category1()) {
  1119             type3 = current_frame.pop_stack(
  1120               VerificationType::category1_check(), CHECK_VERIFY(this));
  1121           } else if (type2.is_category2_2nd()) {
  1122             type3 = current_frame.pop_stack(
  1123               VerificationType::category2_check(), CHECK_VERIFY(this));
  1124           } else {
  1125             /* Unreachable? Would need a category2_1st at stack depth 2 with
  1126              * a category1 on TOS which does not appear possible. */
  1127             verify_error(ErrorContext::bad_type(
  1128                 bci, current_frame.stack_top_ctx()), bad_type_msg, "dup_x2");
  1129             return;
  1131           current_frame.push_stack(type, CHECK_VERIFY(this));
  1132           current_frame.push_stack(type3, CHECK_VERIFY(this));
  1133           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1134           current_frame.push_stack(type, CHECK_VERIFY(this));
  1135           no_control_flow = false; break;
  1137         case Bytecodes::_dup2 :
  1138           type = current_frame.pop_stack(CHECK_VERIFY(this));
  1139           if (type.is_category1()) {
  1140             type2 = current_frame.pop_stack(
  1141               VerificationType::category1_check(), CHECK_VERIFY(this));
  1142           } else if (type.is_category2_2nd()) {
  1143             type2 = current_frame.pop_stack(
  1144               VerificationType::category2_check(), CHECK_VERIFY(this));
  1145           } else {
  1146             /* Unreachable?  Would need a category2_1st on TOS which does not
  1147              * appear possible. */
  1148             verify_error(
  1149                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
  1150                 bad_type_msg, "dup2");
  1151             return;
  1153           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1154           current_frame.push_stack(type, CHECK_VERIFY(this));
  1155           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1156           current_frame.push_stack(type, CHECK_VERIFY(this));
  1157           no_control_flow = false; break;
  1158         case Bytecodes::_dup2_x1 :
  1160           VerificationType type3;
  1161           type = current_frame.pop_stack(CHECK_VERIFY(this));
  1162           if (type.is_category1()) {
  1163             type2 = current_frame.pop_stack(
  1164               VerificationType::category1_check(), CHECK_VERIFY(this));
  1165           } else if (type.is_category2_2nd()) {
  1166             type2 = current_frame.pop_stack(
  1167               VerificationType::category2_check(), CHECK_VERIFY(this));
  1168           } else {
  1169             /* Unreachable?  Would need a category2_1st on TOS which does
  1170              * not appear possible. */
  1171             verify_error(
  1172                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
  1173                 bad_type_msg, "dup2_x1");
  1174             return;
  1176           type3 = current_frame.pop_stack(
  1177             VerificationType::category1_check(), CHECK_VERIFY(this));
  1178           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1179           current_frame.push_stack(type, CHECK_VERIFY(this));
  1180           current_frame.push_stack(type3, CHECK_VERIFY(this));
  1181           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1182           current_frame.push_stack(type, CHECK_VERIFY(this));
  1183           no_control_flow = false; break;
  1185         case Bytecodes::_dup2_x2 :
  1187           VerificationType type3, type4;
  1188           type = current_frame.pop_stack(CHECK_VERIFY(this));
  1189           if (type.is_category1()) {
  1190             type2 = current_frame.pop_stack(
  1191               VerificationType::category1_check(), CHECK_VERIFY(this));
  1192           } else if (type.is_category2_2nd()) {
  1193             type2 = current_frame.pop_stack(
  1194               VerificationType::category2_check(), CHECK_VERIFY(this));
  1195           } else {
  1196             /* Unreachable?  Would need a category2_1st on TOS which does
  1197              * not appear possible. */
  1198             verify_error(
  1199                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
  1200                 bad_type_msg, "dup2_x2");
  1201             return;
  1203           type3 = current_frame.pop_stack(CHECK_VERIFY(this));
  1204           if (type3.is_category1()) {
  1205             type4 = current_frame.pop_stack(
  1206               VerificationType::category1_check(), CHECK_VERIFY(this));
  1207           } else if (type3.is_category2_2nd()) {
  1208             type4 = current_frame.pop_stack(
  1209               VerificationType::category2_check(), CHECK_VERIFY(this));
  1210           } else {
  1211             /* Unreachable?  Would need a category2_1st on TOS after popping
  1212              * a long/double or two category 1's, which does not
  1213              * appear possible. */
  1214             verify_error(
  1215                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
  1216                 bad_type_msg, "dup2_x2");
  1217             return;
  1219           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1220           current_frame.push_stack(type, CHECK_VERIFY(this));
  1221           current_frame.push_stack(type4, CHECK_VERIFY(this));
  1222           current_frame.push_stack(type3, CHECK_VERIFY(this));
  1223           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1224           current_frame.push_stack(type, CHECK_VERIFY(this));
  1225           no_control_flow = false; break;
  1227         case Bytecodes::_swap :
  1228           type = current_frame.pop_stack(
  1229             VerificationType::category1_check(), CHECK_VERIFY(this));
  1230           type2 = current_frame.pop_stack(
  1231             VerificationType::category1_check(), CHECK_VERIFY(this));
  1232           current_frame.push_stack(type, CHECK_VERIFY(this));
  1233           current_frame.push_stack(type2, CHECK_VERIFY(this));
  1234           no_control_flow = false; break;
  1235         case Bytecodes::_iadd :
  1236         case Bytecodes::_isub :
  1237         case Bytecodes::_imul :
  1238         case Bytecodes::_idiv :
  1239         case Bytecodes::_irem :
  1240         case Bytecodes::_ishl :
  1241         case Bytecodes::_ishr :
  1242         case Bytecodes::_iushr :
  1243         case Bytecodes::_ior :
  1244         case Bytecodes::_ixor :
  1245         case Bytecodes::_iand :
  1246           current_frame.pop_stack(
  1247             VerificationType::integer_type(), CHECK_VERIFY(this));
  1248           // fall through
  1249         case Bytecodes::_ineg :
  1250           current_frame.pop_stack(
  1251             VerificationType::integer_type(), CHECK_VERIFY(this));
  1252           current_frame.push_stack(
  1253             VerificationType::integer_type(), CHECK_VERIFY(this));
  1254           no_control_flow = false; break;
  1255         case Bytecodes::_ladd :
  1256         case Bytecodes::_lsub :
  1257         case Bytecodes::_lmul :
  1258         case Bytecodes::_ldiv :
  1259         case Bytecodes::_lrem :
  1260         case Bytecodes::_land :
  1261         case Bytecodes::_lor :
  1262         case Bytecodes::_lxor :
  1263           current_frame.pop_stack_2(
  1264             VerificationType::long2_type(),
  1265             VerificationType::long_type(), CHECK_VERIFY(this));
  1266           // fall through
  1267         case Bytecodes::_lneg :
  1268           current_frame.pop_stack_2(
  1269             VerificationType::long2_type(),
  1270             VerificationType::long_type(), CHECK_VERIFY(this));
  1271           current_frame.push_stack_2(
  1272             VerificationType::long_type(),
  1273             VerificationType::long2_type(), CHECK_VERIFY(this));
  1274           no_control_flow = false; break;
  1275         case Bytecodes::_lshl :
  1276         case Bytecodes::_lshr :
  1277         case Bytecodes::_lushr :
  1278           current_frame.pop_stack(
  1279             VerificationType::integer_type(), CHECK_VERIFY(this));
  1280           current_frame.pop_stack_2(
  1281             VerificationType::long2_type(),
  1282             VerificationType::long_type(), CHECK_VERIFY(this));
  1283           current_frame.push_stack_2(
  1284             VerificationType::long_type(),
  1285             VerificationType::long2_type(), CHECK_VERIFY(this));
  1286           no_control_flow = false; break;
  1287         case Bytecodes::_fadd :
  1288         case Bytecodes::_fsub :
  1289         case Bytecodes::_fmul :
  1290         case Bytecodes::_fdiv :
  1291         case Bytecodes::_frem :
  1292           current_frame.pop_stack(
  1293             VerificationType::float_type(), CHECK_VERIFY(this));
  1294           // fall through
  1295         case Bytecodes::_fneg :
  1296           current_frame.pop_stack(
  1297             VerificationType::float_type(), CHECK_VERIFY(this));
  1298           current_frame.push_stack(
  1299             VerificationType::float_type(), CHECK_VERIFY(this));
  1300           no_control_flow = false; break;
  1301         case Bytecodes::_dadd :
  1302         case Bytecodes::_dsub :
  1303         case Bytecodes::_dmul :
  1304         case Bytecodes::_ddiv :
  1305         case Bytecodes::_drem :
  1306           current_frame.pop_stack_2(
  1307             VerificationType::double2_type(),
  1308             VerificationType::double_type(), CHECK_VERIFY(this));
  1309           // fall through
  1310         case Bytecodes::_dneg :
  1311           current_frame.pop_stack_2(
  1312             VerificationType::double2_type(),
  1313             VerificationType::double_type(), CHECK_VERIFY(this));
  1314           current_frame.push_stack_2(
  1315             VerificationType::double_type(),
  1316             VerificationType::double2_type(), CHECK_VERIFY(this));
  1317           no_control_flow = false; break;
  1318         case Bytecodes::_iinc :
  1319           verify_iinc(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
  1320           no_control_flow = false; break;
  1321         case Bytecodes::_i2l :
  1322           type = current_frame.pop_stack(
  1323             VerificationType::integer_type(), CHECK_VERIFY(this));
  1324           current_frame.push_stack_2(
  1325             VerificationType::long_type(),
  1326             VerificationType::long2_type(), CHECK_VERIFY(this));
  1327           no_control_flow = false; break;
  1328        case Bytecodes::_l2i :
  1329           current_frame.pop_stack_2(
  1330             VerificationType::long2_type(),
  1331             VerificationType::long_type(), CHECK_VERIFY(this));
  1332           current_frame.push_stack(
  1333             VerificationType::integer_type(), CHECK_VERIFY(this));
  1334           no_control_flow = false; break;
  1335         case Bytecodes::_i2f :
  1336           current_frame.pop_stack(
  1337             VerificationType::integer_type(), CHECK_VERIFY(this));
  1338           current_frame.push_stack(
  1339             VerificationType::float_type(), CHECK_VERIFY(this));
  1340           no_control_flow = false; break;
  1341         case Bytecodes::_i2d :
  1342           current_frame.pop_stack(
  1343             VerificationType::integer_type(), CHECK_VERIFY(this));
  1344           current_frame.push_stack_2(
  1345             VerificationType::double_type(),
  1346             VerificationType::double2_type(), CHECK_VERIFY(this));
  1347           no_control_flow = false; break;
  1348         case Bytecodes::_l2f :
  1349           current_frame.pop_stack_2(
  1350             VerificationType::long2_type(),
  1351             VerificationType::long_type(), CHECK_VERIFY(this));
  1352           current_frame.push_stack(
  1353             VerificationType::float_type(), CHECK_VERIFY(this));
  1354           no_control_flow = false; break;
  1355         case Bytecodes::_l2d :
  1356           current_frame.pop_stack_2(
  1357             VerificationType::long2_type(),
  1358             VerificationType::long_type(), CHECK_VERIFY(this));
  1359           current_frame.push_stack_2(
  1360             VerificationType::double_type(),
  1361             VerificationType::double2_type(), CHECK_VERIFY(this));
  1362           no_control_flow = false; break;
  1363         case Bytecodes::_f2i :
  1364           current_frame.pop_stack(
  1365             VerificationType::float_type(), CHECK_VERIFY(this));
  1366           current_frame.push_stack(
  1367             VerificationType::integer_type(), CHECK_VERIFY(this));
  1368           no_control_flow = false; break;
  1369         case Bytecodes::_f2l :
  1370           current_frame.pop_stack(
  1371             VerificationType::float_type(), CHECK_VERIFY(this));
  1372           current_frame.push_stack_2(
  1373             VerificationType::long_type(),
  1374             VerificationType::long2_type(), CHECK_VERIFY(this));
  1375           no_control_flow = false; break;
  1376         case Bytecodes::_f2d :
  1377           current_frame.pop_stack(
  1378             VerificationType::float_type(), CHECK_VERIFY(this));
  1379           current_frame.push_stack_2(
  1380             VerificationType::double_type(),
  1381             VerificationType::double2_type(), CHECK_VERIFY(this));
  1382           no_control_flow = false; break;
  1383         case Bytecodes::_d2i :
  1384           current_frame.pop_stack_2(
  1385             VerificationType::double2_type(),
  1386             VerificationType::double_type(), CHECK_VERIFY(this));
  1387           current_frame.push_stack(
  1388             VerificationType::integer_type(), CHECK_VERIFY(this));
  1389           no_control_flow = false; break;
  1390         case Bytecodes::_d2l :
  1391           current_frame.pop_stack_2(
  1392             VerificationType::double2_type(),
  1393             VerificationType::double_type(), CHECK_VERIFY(this));
  1394           current_frame.push_stack_2(
  1395             VerificationType::long_type(),
  1396             VerificationType::long2_type(), CHECK_VERIFY(this));
  1397           no_control_flow = false; break;
  1398         case Bytecodes::_d2f :
  1399           current_frame.pop_stack_2(
  1400             VerificationType::double2_type(),
  1401             VerificationType::double_type(), CHECK_VERIFY(this));
  1402           current_frame.push_stack(
  1403             VerificationType::float_type(), CHECK_VERIFY(this));
  1404           no_control_flow = false; break;
  1405         case Bytecodes::_i2b :
  1406         case Bytecodes::_i2c :
  1407         case Bytecodes::_i2s :
  1408           current_frame.pop_stack(
  1409             VerificationType::integer_type(), CHECK_VERIFY(this));
  1410           current_frame.push_stack(
  1411             VerificationType::integer_type(), CHECK_VERIFY(this));
  1412           no_control_flow = false; break;
  1413         case Bytecodes::_lcmp :
  1414           current_frame.pop_stack_2(
  1415             VerificationType::long2_type(),
  1416             VerificationType::long_type(), CHECK_VERIFY(this));
  1417           current_frame.pop_stack_2(
  1418             VerificationType::long2_type(),
  1419             VerificationType::long_type(), CHECK_VERIFY(this));
  1420           current_frame.push_stack(
  1421             VerificationType::integer_type(), CHECK_VERIFY(this));
  1422           no_control_flow = false; break;
  1423         case Bytecodes::_fcmpl :
  1424         case Bytecodes::_fcmpg :
  1425           current_frame.pop_stack(
  1426             VerificationType::float_type(), CHECK_VERIFY(this));
  1427           current_frame.pop_stack(
  1428             VerificationType::float_type(), CHECK_VERIFY(this));
  1429           current_frame.push_stack(
  1430             VerificationType::integer_type(), CHECK_VERIFY(this));
  1431           no_control_flow = false; break;
  1432         case Bytecodes::_dcmpl :
  1433         case Bytecodes::_dcmpg :
  1434           current_frame.pop_stack_2(
  1435             VerificationType::double2_type(),
  1436             VerificationType::double_type(), CHECK_VERIFY(this));
  1437           current_frame.pop_stack_2(
  1438             VerificationType::double2_type(),
  1439             VerificationType::double_type(), CHECK_VERIFY(this));
  1440           current_frame.push_stack(
  1441             VerificationType::integer_type(), CHECK_VERIFY(this));
  1442           no_control_flow = false; break;
  1443         case Bytecodes::_if_icmpeq:
  1444         case Bytecodes::_if_icmpne:
  1445         case Bytecodes::_if_icmplt:
  1446         case Bytecodes::_if_icmpge:
  1447         case Bytecodes::_if_icmpgt:
  1448         case Bytecodes::_if_icmple:
  1449           current_frame.pop_stack(
  1450             VerificationType::integer_type(), CHECK_VERIFY(this));
  1451           // fall through
  1452         case Bytecodes::_ifeq:
  1453         case Bytecodes::_ifne:
  1454         case Bytecodes::_iflt:
  1455         case Bytecodes::_ifge:
  1456         case Bytecodes::_ifgt:
  1457         case Bytecodes::_ifle:
  1458           current_frame.pop_stack(
  1459             VerificationType::integer_type(), CHECK_VERIFY(this));
  1460           target = bcs.dest();
  1461           stackmap_table.check_jump_target(
  1462             &current_frame, target, CHECK_VERIFY(this));
  1463           no_control_flow = false; break;
  1464         case Bytecodes::_if_acmpeq :
  1465         case Bytecodes::_if_acmpne :
  1466           current_frame.pop_stack(
  1467             VerificationType::reference_check(), CHECK_VERIFY(this));
  1468           // fall through
  1469         case Bytecodes::_ifnull :
  1470         case Bytecodes::_ifnonnull :
  1471           current_frame.pop_stack(
  1472             VerificationType::reference_check(), CHECK_VERIFY(this));
  1473           target = bcs.dest();
  1474           stackmap_table.check_jump_target
  1475             (&current_frame, target, CHECK_VERIFY(this));
  1476           no_control_flow = false; break;
  1477         case Bytecodes::_goto :
  1478           target = bcs.dest();
  1479           stackmap_table.check_jump_target(
  1480             &current_frame, target, CHECK_VERIFY(this));
  1481           no_control_flow = true; break;
  1482         case Bytecodes::_goto_w :
  1483           target = bcs.dest_w();
  1484           stackmap_table.check_jump_target(
  1485             &current_frame, target, CHECK_VERIFY(this));
  1486           no_control_flow = true; break;
  1487         case Bytecodes::_tableswitch :
  1488         case Bytecodes::_lookupswitch :
  1489           verify_switch(
  1490             &bcs, code_length, code_data, &current_frame,
  1491             &stackmap_table, CHECK_VERIFY(this));
  1492           no_control_flow = true; break;
  1493         case Bytecodes::_ireturn :
  1494           type = current_frame.pop_stack(
  1495             VerificationType::integer_type(), CHECK_VERIFY(this));
  1496           verify_return_value(return_type, type, bci,
  1497                               &current_frame, CHECK_VERIFY(this));
  1498           no_control_flow = true; break;
  1499         case Bytecodes::_lreturn :
  1500           type2 = current_frame.pop_stack(
  1501             VerificationType::long2_type(), CHECK_VERIFY(this));
  1502           type = current_frame.pop_stack(
  1503             VerificationType::long_type(), CHECK_VERIFY(this));
  1504           verify_return_value(return_type, type, bci,
  1505                               &current_frame, CHECK_VERIFY(this));
  1506           no_control_flow = true; break;
  1507         case Bytecodes::_freturn :
  1508           type = current_frame.pop_stack(
  1509             VerificationType::float_type(), CHECK_VERIFY(this));
  1510           verify_return_value(return_type, type, bci,
  1511                               &current_frame, CHECK_VERIFY(this));
  1512           no_control_flow = true; break;
  1513         case Bytecodes::_dreturn :
  1514           type2 = current_frame.pop_stack(
  1515             VerificationType::double2_type(),  CHECK_VERIFY(this));
  1516           type = current_frame.pop_stack(
  1517             VerificationType::double_type(), CHECK_VERIFY(this));
  1518           verify_return_value(return_type, type, bci,
  1519                               &current_frame, CHECK_VERIFY(this));
  1520           no_control_flow = true; break;
  1521         case Bytecodes::_areturn :
  1522           type = current_frame.pop_stack(
  1523             VerificationType::reference_check(), CHECK_VERIFY(this));
  1524           verify_return_value(return_type, type, bci,
  1525                               &current_frame, CHECK_VERIFY(this));
  1526           no_control_flow = true; break;
  1527         case Bytecodes::_return :
  1528           if (return_type != VerificationType::bogus_type()) {
  1529             verify_error(ErrorContext::bad_code(bci),
  1530                          "Method expects a return value");
  1531             return;
  1533           // Make sure "this" has been initialized if current method is an
  1534           // <init>
  1535           if (_method->name() == vmSymbols::object_initializer_name() &&
  1536               current_frame.flag_this_uninit()) {
  1537             verify_error(ErrorContext::bad_code(bci),
  1538                          "Constructor must call super() or this() "
  1539                          "before return");
  1540             return;
  1542           no_control_flow = true; break;
  1543         case Bytecodes::_getstatic :
  1544         case Bytecodes::_putstatic :
  1545         case Bytecodes::_getfield :
  1546         case Bytecodes::_putfield :
  1547           verify_field_instructions(
  1548             &bcs, &current_frame, cp, CHECK_VERIFY(this));
  1549           no_control_flow = false; break;
  1550         case Bytecodes::_invokevirtual :
  1551         case Bytecodes::_invokespecial :
  1552         case Bytecodes::_invokestatic :
  1553           verify_invoke_instructions(
  1554             &bcs, code_length, &current_frame,
  1555             &this_uninit, return_type, cp, CHECK_VERIFY(this));
  1556           no_control_flow = false; break;
  1557         case Bytecodes::_invokeinterface :
  1558         case Bytecodes::_invokedynamic :
  1559           verify_invoke_instructions(
  1560             &bcs, code_length, &current_frame,
  1561             &this_uninit, return_type, cp, CHECK_VERIFY(this));
  1562           no_control_flow = false; break;
  1563         case Bytecodes::_new :
  1565           index = bcs.get_index_u2();
  1566           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
  1567           VerificationType new_class_type =
  1568             cp_index_to_type(index, cp, CHECK_VERIFY(this));
  1569           if (!new_class_type.is_object()) {
  1570             verify_error(ErrorContext::bad_type(bci,
  1571                 TypeOrigin::cp(index, new_class_type)),
  1572                 "Illegal new instruction");
  1573             return;
  1575           type = VerificationType::uninitialized_type(bci);
  1576           current_frame.push_stack(type, CHECK_VERIFY(this));
  1577           no_control_flow = false; break;
  1579         case Bytecodes::_newarray :
  1580           type = get_newarray_type(bcs.get_index(), bci, CHECK_VERIFY(this));
  1581           current_frame.pop_stack(
  1582             VerificationType::integer_type(),  CHECK_VERIFY(this));
  1583           current_frame.push_stack(type, CHECK_VERIFY(this));
  1584           no_control_flow = false; break;
  1585         case Bytecodes::_anewarray :
  1586           verify_anewarray(
  1587             bci, bcs.get_index_u2(), cp, &current_frame, CHECK_VERIFY(this));
  1588           no_control_flow = false; break;
  1589         case Bytecodes::_arraylength :
  1590           type = current_frame.pop_stack(
  1591             VerificationType::reference_check(), CHECK_VERIFY(this));
  1592           if (!(type.is_null() || type.is_array())) {
  1593             verify_error(ErrorContext::bad_type(
  1594                 bci, current_frame.stack_top_ctx()),
  1595                 bad_type_msg, "arraylength");
  1597           current_frame.push_stack(
  1598             VerificationType::integer_type(), CHECK_VERIFY(this));
  1599           no_control_flow = false; break;
  1600         case Bytecodes::_checkcast :
  1602           index = bcs.get_index_u2();
  1603           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
  1604           current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
  1605           VerificationType klass_type = cp_index_to_type(
  1606             index, cp, CHECK_VERIFY(this));
  1607           current_frame.push_stack(klass_type, CHECK_VERIFY(this));
  1608           no_control_flow = false; break;
  1610         case Bytecodes::_instanceof : {
  1611           index = bcs.get_index_u2();
  1612           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
  1613           current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
  1614           current_frame.push_stack(
  1615             VerificationType::integer_type(), CHECK_VERIFY(this));
  1616           no_control_flow = false; break;
  1618         case Bytecodes::_monitorenter :
  1619         case Bytecodes::_monitorexit :
  1620           current_frame.pop_stack(
  1621             VerificationType::reference_check(), CHECK_VERIFY(this));
  1622           no_control_flow = false; break;
  1623         case Bytecodes::_multianewarray :
  1625           index = bcs.get_index_u2();
  1626           u2 dim = *(bcs.bcp()+3);
  1627           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
  1628           VerificationType new_array_type =
  1629             cp_index_to_type(index, cp, CHECK_VERIFY(this));
  1630           if (!new_array_type.is_array()) {
  1631             verify_error(ErrorContext::bad_type(bci,
  1632                 TypeOrigin::cp(index, new_array_type)),
  1633                 "Illegal constant pool index in multianewarray instruction");
  1634             return;
  1636           if (dim < 1 || new_array_type.dimensions() < dim) {
  1637             verify_error(ErrorContext::bad_code(bci),
  1638                 "Illegal dimension in multianewarray instruction: %d", dim);
  1639             return;
  1641           for (int i = 0; i < dim; i++) {
  1642             current_frame.pop_stack(
  1643               VerificationType::integer_type(), CHECK_VERIFY(this));
  1645           current_frame.push_stack(new_array_type, CHECK_VERIFY(this));
  1646           no_control_flow = false; break;
  1648         case Bytecodes::_athrow :
  1649           type = VerificationType::reference_type(
  1650             vmSymbols::java_lang_Throwable());
  1651           current_frame.pop_stack(type, CHECK_VERIFY(this));
  1652           no_control_flow = true; break;
  1653         default:
  1654           // We only need to check the valid bytecodes in class file.
  1655           // And jsr and ret are not in the new class file format in JDK1.5.
  1656           verify_error(ErrorContext::bad_code(bci),
  1657               "Bad instruction: %02x", opcode);
  1658           no_control_flow = false;
  1659           return;
  1660       }  // end switch
  1661     }  // end Merge with the next instruction
  1663     // Look for possible jump target in exception handlers and see if it
  1664     // matches current_frame
  1665     if (bci >= ex_min && bci < ex_max) {
  1666       verify_exception_handler_targets(
  1667         bci, this_uninit, &current_frame, &stackmap_table, CHECK_VERIFY(this));
  1669   } // end while
  1671   // Make sure that control flow does not fall through end of the method
  1672   if (!no_control_flow) {
  1673     verify_error(ErrorContext::bad_code(code_length),
  1674         "Control flow falls through code end");
  1675     return;
  1679 char* ClassVerifier::generate_code_data(methodHandle m, u4 code_length, TRAPS) {
  1680   char* code_data = NEW_RESOURCE_ARRAY(char, code_length);
  1681   memset(code_data, 0, sizeof(char) * code_length);
  1682   RawBytecodeStream bcs(m);
  1684   while (!bcs.is_last_bytecode()) {
  1685     if (bcs.raw_next() != Bytecodes::_illegal) {
  1686       int bci = bcs.bci();
  1687       if (bcs.raw_code() == Bytecodes::_new) {
  1688         code_data[bci] = NEW_OFFSET;
  1689       } else {
  1690         code_data[bci] = BYTECODE_OFFSET;
  1692     } else {
  1693       verify_error(ErrorContext::bad_code(bcs.bci()), "Bad instruction");
  1694       return NULL;
  1698   return code_data;
  1701 void ClassVerifier::verify_exception_handler_table(u4 code_length, char* code_data, int& min, int& max, TRAPS) {
  1702   ExceptionTable exhandlers(_method());
  1703   int exlength = exhandlers.length();
  1704   constantPoolHandle cp (THREAD, _method->constants());
  1706   for(int i = 0; i < exlength; i++) {
  1707     //reacquire the table in case a GC happened
  1708     ExceptionTable exhandlers(_method());
  1709     u2 start_pc = exhandlers.start_pc(i);
  1710     u2 end_pc = exhandlers.end_pc(i);
  1711     u2 handler_pc = exhandlers.handler_pc(i);
  1712     if (start_pc >= code_length || code_data[start_pc] == 0) {
  1713       class_format_error("Illegal exception table start_pc %d", start_pc);
  1714       return;
  1716     if (end_pc != code_length) {   // special case: end_pc == code_length
  1717       if (end_pc > code_length || code_data[end_pc] == 0) {
  1718         class_format_error("Illegal exception table end_pc %d", end_pc);
  1719         return;
  1722     if (handler_pc >= code_length || code_data[handler_pc] == 0) {
  1723       class_format_error("Illegal exception table handler_pc %d", handler_pc);
  1724       return;
  1726     int catch_type_index = exhandlers.catch_type_index(i);
  1727     if (catch_type_index != 0) {
  1728       VerificationType catch_type = cp_index_to_type(
  1729         catch_type_index, cp, CHECK_VERIFY(this));
  1730       VerificationType throwable =
  1731         VerificationType::reference_type(vmSymbols::java_lang_Throwable());
  1732       bool is_subclass = throwable.is_assignable_from(
  1733         catch_type, this, CHECK_VERIFY(this));
  1734       if (!is_subclass) {
  1735         // 4286534: should throw VerifyError according to recent spec change
  1736         verify_error(ErrorContext::bad_type(handler_pc,
  1737             TypeOrigin::cp(catch_type_index, catch_type),
  1738             TypeOrigin::implicit(throwable)),
  1739             "Catch type is not a subclass "
  1740             "of Throwable in exception handler %d", handler_pc);
  1741         return;
  1744     if (start_pc < min) min = start_pc;
  1745     if (end_pc > max) max = end_pc;
  1749 void ClassVerifier::verify_local_variable_table(u4 code_length, char* code_data, TRAPS) {
  1750   int localvariable_table_length = _method()->localvariable_table_length();
  1751   if (localvariable_table_length > 0) {
  1752     LocalVariableTableElement* table = _method()->localvariable_table_start();
  1753     for (int i = 0; i < localvariable_table_length; i++) {
  1754       u2 start_bci = table[i].start_bci;
  1755       u2 length = table[i].length;
  1757       if (start_bci >= code_length || code_data[start_bci] == 0) {
  1758         class_format_error(
  1759           "Illegal local variable table start_pc %d", start_bci);
  1760         return;
  1762       u4 end_bci = (u4)(start_bci + length);
  1763       if (end_bci != code_length) {
  1764         if (end_bci >= code_length || code_data[end_bci] == 0) {
  1765           class_format_error( "Illegal local variable table length %d", length);
  1766           return;
  1773 u2 ClassVerifier::verify_stackmap_table(u2 stackmap_index, u2 bci,
  1774                                         StackMapFrame* current_frame,
  1775                                         StackMapTable* stackmap_table,
  1776                                         bool no_control_flow, TRAPS) {
  1777   if (stackmap_index < stackmap_table->get_frame_count()) {
  1778     u2 this_offset = stackmap_table->get_offset(stackmap_index);
  1779     if (no_control_flow && this_offset > bci) {
  1780       verify_error(ErrorContext::missing_stackmap(bci),
  1781                    "Expecting a stack map frame");
  1782       return 0;
  1784     if (this_offset == bci) {
  1785       ErrorContext ctx;
  1786       // See if current stack map can be assigned to the frame in table.
  1787       // current_frame is the stackmap frame got from the last instruction.
  1788       // If matched, current_frame will be updated by this method.
  1789       bool matches = stackmap_table->match_stackmap(
  1790         current_frame, this_offset, stackmap_index,
  1791         !no_control_flow, true, &ctx, CHECK_VERIFY_(this, 0));
  1792       if (!matches) {
  1793         // report type error
  1794         verify_error(ctx, "Instruction type does not match stack map");
  1795         return 0;
  1797       stackmap_index++;
  1798     } else if (this_offset < bci) {
  1799       // current_offset should have met this_offset.
  1800       class_format_error("Bad stack map offset %d", this_offset);
  1801       return 0;
  1803   } else if (no_control_flow) {
  1804     verify_error(ErrorContext::bad_code(bci), "Expecting a stack map frame");
  1805     return 0;
  1807   return stackmap_index;
  1810 void ClassVerifier::verify_exception_handler_targets(u2 bci, bool this_uninit, StackMapFrame* current_frame,
  1811                                                      StackMapTable* stackmap_table, TRAPS) {
  1812   constantPoolHandle cp (THREAD, _method->constants());
  1813   ExceptionTable exhandlers(_method());
  1814   int exlength = exhandlers.length();
  1815   for(int i = 0; i < exlength; i++) {
  1816     //reacquire the table in case a GC happened
  1817     ExceptionTable exhandlers(_method());
  1818     u2 start_pc = exhandlers.start_pc(i);
  1819     u2 end_pc = exhandlers.end_pc(i);
  1820     u2 handler_pc = exhandlers.handler_pc(i);
  1821     int catch_type_index = exhandlers.catch_type_index(i);
  1822     if(bci >= start_pc && bci < end_pc) {
  1823       u1 flags = current_frame->flags();
  1824       if (this_uninit) {  flags |= FLAG_THIS_UNINIT; }
  1825       StackMapFrame* new_frame = current_frame->frame_in_exception_handler(flags);
  1826       if (catch_type_index != 0) {
  1827         // We know that this index refers to a subclass of Throwable
  1828         VerificationType catch_type = cp_index_to_type(
  1829           catch_type_index, cp, CHECK_VERIFY(this));
  1830         new_frame->push_stack(catch_type, CHECK_VERIFY(this));
  1831       } else {
  1832         VerificationType throwable =
  1833           VerificationType::reference_type(vmSymbols::java_lang_Throwable());
  1834         new_frame->push_stack(throwable, CHECK_VERIFY(this));
  1836       ErrorContext ctx;
  1837       bool matches = stackmap_table->match_stackmap(
  1838         new_frame, handler_pc, true, false, &ctx, CHECK_VERIFY(this));
  1839       if (!matches) {
  1840         verify_error(ctx, "Stack map does not match the one at "
  1841             "exception handler %d", handler_pc);
  1842         return;
  1848 void ClassVerifier::verify_cp_index(
  1849     u2 bci, constantPoolHandle cp, int index, TRAPS) {
  1850   int nconstants = cp->length();
  1851   if ((index <= 0) || (index >= nconstants)) {
  1852     verify_error(ErrorContext::bad_cp_index(bci, index),
  1853         "Illegal constant pool index %d in class %s",
  1854         index, cp->pool_holder()->external_name());
  1855     return;
  1859 void ClassVerifier::verify_cp_type(
  1860     u2 bci, int index, constantPoolHandle cp, unsigned int types, TRAPS) {
  1862   // In some situations, bytecode rewriting may occur while we're verifying.
  1863   // In this case, a constant pool cache exists and some indices refer to that
  1864   // instead.  Be sure we don't pick up such indices by accident.
  1865   // We must check was_recursively_verified() before we get here.
  1866   guarantee(cp->cache() == NULL, "not rewritten yet");
  1868   verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
  1869   unsigned int tag = cp->tag_at(index).value();
  1870   if ((types & (1 << tag)) == 0) {
  1871     verify_error(ErrorContext::bad_cp_index(bci, index),
  1872       "Illegal type at constant pool entry %d in class %s",
  1873       index, cp->pool_holder()->external_name());
  1874     return;
  1878 void ClassVerifier::verify_cp_class_type(
  1879     u2 bci, int index, constantPoolHandle cp, TRAPS) {
  1880   verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
  1881   constantTag tag = cp->tag_at(index);
  1882   if (!tag.is_klass() && !tag.is_unresolved_klass()) {
  1883     verify_error(ErrorContext::bad_cp_index(bci, index),
  1884         "Illegal type at constant pool entry %d in class %s",
  1885         index, cp->pool_holder()->external_name());
  1886     return;
  1890 void ClassVerifier::verify_error(ErrorContext ctx, const char* msg, ...) {
  1891   stringStream ss;
  1893   ctx.reset_frames();
  1894   _exception_type = vmSymbols::java_lang_VerifyError();
  1895   _error_context = ctx;
  1896   va_list va;
  1897   va_start(va, msg);
  1898   ss.vprint(msg, va);
  1899   va_end(va);
  1900   _message = ss.as_string();
  1901 #ifdef ASSERT
  1902   ResourceMark rm;
  1903   const char* exception_name = _exception_type->as_C_string();
  1904   Exceptions::debug_check_abort(exception_name, NULL);
  1905 #endif // ndef ASSERT
  1908 void ClassVerifier::class_format_error(const char* msg, ...) {
  1909   stringStream ss;
  1910   _exception_type = vmSymbols::java_lang_ClassFormatError();
  1911   va_list va;
  1912   va_start(va, msg);
  1913   ss.vprint(msg, va);
  1914   va_end(va);
  1915   if (!_method.is_null()) {
  1916     ss.print(" in method %s", _method->name_and_sig_as_C_string());
  1918   _message = ss.as_string();
  1921 Klass* ClassVerifier::load_class(Symbol* name, TRAPS) {
  1922   // Get current loader and protection domain first.
  1923   oop loader = current_class()->class_loader();
  1924   oop protection_domain = current_class()->protection_domain();
  1926   return SystemDictionary::resolve_or_fail(
  1927     name, Handle(THREAD, loader), Handle(THREAD, protection_domain),
  1928     true, CHECK_NULL);
  1931 bool ClassVerifier::is_protected_access(instanceKlassHandle this_class,
  1932                                         Klass* target_class,
  1933                                         Symbol* field_name,
  1934                                         Symbol* field_sig,
  1935                                         bool is_method) {
  1936   No_Safepoint_Verifier nosafepoint;
  1938   // If target class isn't a super class of this class, we don't worry about this case
  1939   if (!this_class->is_subclass_of(target_class)) {
  1940     return false;
  1942   // Check if the specified method or field is protected
  1943   InstanceKlass* target_instance = InstanceKlass::cast(target_class);
  1944   fieldDescriptor fd;
  1945   if (is_method) {
  1946     Method* m = target_instance->uncached_lookup_method(field_name, field_sig);
  1947     if (m != NULL && m->is_protected()) {
  1948       if (!this_class->is_same_class_package(m->method_holder())) {
  1949         return true;
  1952   } else {
  1953     Klass* member_klass = target_instance->find_field(field_name, field_sig, &fd);
  1954     if (member_klass != NULL && fd.is_protected()) {
  1955       if (!this_class->is_same_class_package(member_klass)) {
  1956         return true;
  1960   return false;
  1963 void ClassVerifier::verify_ldc(
  1964     int opcode, u2 index, StackMapFrame* current_frame,
  1965     constantPoolHandle cp, u2 bci, TRAPS) {
  1966   verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
  1967   constantTag tag = cp->tag_at(index);
  1968   unsigned int types;
  1969   if (opcode == Bytecodes::_ldc || opcode == Bytecodes::_ldc_w) {
  1970     if (!tag.is_unresolved_klass()) {
  1971       types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float)
  1972             | (1 << JVM_CONSTANT_String)  | (1 << JVM_CONSTANT_Class)
  1973             | (1 << JVM_CONSTANT_MethodHandle) | (1 << JVM_CONSTANT_MethodType);
  1974       // Note:  The class file parser already verified the legality of
  1975       // MethodHandle and MethodType constants.
  1976       verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
  1978   } else {
  1979     assert(opcode == Bytecodes::_ldc2_w, "must be ldc2_w");
  1980     types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long);
  1981     verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
  1983   if (tag.is_string() && cp->is_pseudo_string_at(index)) {
  1984     current_frame->push_stack(object_type(), CHECK_VERIFY(this));
  1985   } else if (tag.is_string()) {
  1986     current_frame->push_stack(
  1987       VerificationType::reference_type(
  1988         vmSymbols::java_lang_String()), CHECK_VERIFY(this));
  1989   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
  1990     current_frame->push_stack(
  1991       VerificationType::reference_type(
  1992         vmSymbols::java_lang_Class()), CHECK_VERIFY(this));
  1993   } else if (tag.is_int()) {
  1994     current_frame->push_stack(
  1995       VerificationType::integer_type(), CHECK_VERIFY(this));
  1996   } else if (tag.is_float()) {
  1997     current_frame->push_stack(
  1998       VerificationType::float_type(), CHECK_VERIFY(this));
  1999   } else if (tag.is_double()) {
  2000     current_frame->push_stack_2(
  2001       VerificationType::double_type(),
  2002       VerificationType::double2_type(), CHECK_VERIFY(this));
  2003   } else if (tag.is_long()) {
  2004     current_frame->push_stack_2(
  2005       VerificationType::long_type(),
  2006       VerificationType::long2_type(), CHECK_VERIFY(this));
  2007   } else if (tag.is_method_handle()) {
  2008     current_frame->push_stack(
  2009       VerificationType::reference_type(
  2010         vmSymbols::java_lang_invoke_MethodHandle()), CHECK_VERIFY(this));
  2011   } else if (tag.is_method_type()) {
  2012     current_frame->push_stack(
  2013       VerificationType::reference_type(
  2014         vmSymbols::java_lang_invoke_MethodType()), CHECK_VERIFY(this));
  2015   } else {
  2016     /* Unreachable? verify_cp_type has already validated the cp type. */
  2017     verify_error(
  2018         ErrorContext::bad_cp_index(bci, index), "Invalid index in ldc");
  2019     return;
  2023 void ClassVerifier::verify_switch(
  2024     RawBytecodeStream* bcs, u4 code_length, char* code_data,
  2025     StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS) {
  2026   int bci = bcs->bci();
  2027   address bcp = bcs->bcp();
  2028   address aligned_bcp = (address) round_to((intptr_t)(bcp + 1), jintSize);
  2030   // 4639449 & 4647081: padding bytes must be 0
  2031   u2 padding_offset = 1;
  2032   while ((bcp + padding_offset) < aligned_bcp) {
  2033     if(*(bcp + padding_offset) != 0) {
  2034       verify_error(ErrorContext::bad_code(bci),
  2035                    "Nonzero padding byte in lookswitch or tableswitch");
  2036       return;
  2038     padding_offset++;
  2040   int default_offset = (int) Bytes::get_Java_u4(aligned_bcp);
  2041   int keys, delta;
  2042   current_frame->pop_stack(
  2043     VerificationType::integer_type(), CHECK_VERIFY(this));
  2044   if (bcs->raw_code() == Bytecodes::_tableswitch) {
  2045     jint low = (jint)Bytes::get_Java_u4(aligned_bcp + jintSize);
  2046     jint high = (jint)Bytes::get_Java_u4(aligned_bcp + 2*jintSize);
  2047     if (low > high) {
  2048       verify_error(ErrorContext::bad_code(bci),
  2049           "low must be less than or equal to high in tableswitch");
  2050       return;
  2052     keys = high - low + 1;
  2053     if (keys < 0) {
  2054       verify_error(ErrorContext::bad_code(bci), "too many keys in tableswitch");
  2055       return;
  2057     delta = 1;
  2058   } else {
  2059     keys = (int)Bytes::get_Java_u4(aligned_bcp + jintSize);
  2060     if (keys < 0) {
  2061       verify_error(ErrorContext::bad_code(bci),
  2062                    "number of keys in lookupswitch less than 0");
  2063       return;
  2065     delta = 2;
  2066     // Make sure that the lookupswitch items are sorted
  2067     for (int i = 0; i < (keys - 1); i++) {
  2068       jint this_key = Bytes::get_Java_u4(aligned_bcp + (2+2*i)*jintSize);
  2069       jint next_key = Bytes::get_Java_u4(aligned_bcp + (2+2*i+2)*jintSize);
  2070       if (this_key >= next_key) {
  2071         verify_error(ErrorContext::bad_code(bci),
  2072                      "Bad lookupswitch instruction");
  2073         return;
  2077   int target = bci + default_offset;
  2078   stackmap_table->check_jump_target(current_frame, target, CHECK_VERIFY(this));
  2079   for (int i = 0; i < keys; i++) {
  2080     // Because check_jump_target() may safepoint, the bytecode could have
  2081     // moved, which means 'aligned_bcp' is no good and needs to be recalculated.
  2082     aligned_bcp = (address)round_to((intptr_t)(bcs->bcp() + 1), jintSize);
  2083     target = bci + (jint)Bytes::get_Java_u4(aligned_bcp+(3+i*delta)*jintSize);
  2084     stackmap_table->check_jump_target(
  2085       current_frame, target, CHECK_VERIFY(this));
  2087   NOT_PRODUCT(aligned_bcp = NULL);  // no longer valid at this point
  2090 bool ClassVerifier::name_in_supers(
  2091     Symbol* ref_name, instanceKlassHandle current) {
  2092   Klass* super = current->super();
  2093   while (super != NULL) {
  2094     if (super->name() == ref_name) {
  2095       return true;
  2097     super = super->super();
  2099   return false;
  2102 void ClassVerifier::verify_field_instructions(RawBytecodeStream* bcs,
  2103                                               StackMapFrame* current_frame,
  2104                                               constantPoolHandle cp,
  2105                                               TRAPS) {
  2106   u2 index = bcs->get_index_u2();
  2107   verify_cp_type(bcs->bci(), index, cp,
  2108       1 << JVM_CONSTANT_Fieldref, CHECK_VERIFY(this));
  2110   // Get field name and signature
  2111   Symbol* field_name = cp->name_ref_at(index);
  2112   Symbol* field_sig = cp->signature_ref_at(index);
  2114   if (!SignatureVerifier::is_valid_type_signature(field_sig)) {
  2115     class_format_error(
  2116       "Invalid signature for field in class %s referenced "
  2117       "from constant pool index %d", _klass->external_name(), index);
  2118     return;
  2121   // Get referenced class type
  2122   VerificationType ref_class_type = cp_ref_index_to_type(
  2123     index, cp, CHECK_VERIFY(this));
  2124   if (!ref_class_type.is_object()) {
  2125     /* Unreachable?  Class file parser verifies Fieldref contents */
  2126     verify_error(ErrorContext::bad_type(bcs->bci(),
  2127         TypeOrigin::cp(index, ref_class_type)),
  2128         "Expecting reference to class in class %s at constant pool index %d",
  2129         _klass->external_name(), index);
  2130     return;
  2132   VerificationType target_class_type = ref_class_type;
  2134   assert(sizeof(VerificationType) == sizeof(uintptr_t),
  2135         "buffer type must match VerificationType size");
  2136   uintptr_t field_type_buffer[2];
  2137   VerificationType* field_type = (VerificationType*)field_type_buffer;
  2138   // If we make a VerificationType[2] array directly, the compiler calls
  2139   // to the c-runtime library to do the allocation instead of just
  2140   // stack allocating it.  Plus it would run constructors.  This shows up
  2141   // in performance profiles.
  2143   SignatureStream sig_stream(field_sig, false);
  2144   VerificationType stack_object_type;
  2145   int n = change_sig_to_verificationType(
  2146     &sig_stream, field_type, CHECK_VERIFY(this));
  2147   u2 bci = bcs->bci();
  2148   bool is_assignable;
  2149   switch (bcs->raw_code()) {
  2150     case Bytecodes::_getstatic: {
  2151       for (int i = 0; i < n; i++) {
  2152         current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
  2154       break;
  2156     case Bytecodes::_putstatic: {
  2157       for (int i = n - 1; i >= 0; i--) {
  2158         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
  2160       break;
  2162     case Bytecodes::_getfield: {
  2163       stack_object_type = current_frame->pop_stack(
  2164         target_class_type, CHECK_VERIFY(this));
  2165       for (int i = 0; i < n; i++) {
  2166         current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
  2168       goto check_protected;
  2170     case Bytecodes::_putfield: {
  2171       for (int i = n - 1; i >= 0; i--) {
  2172         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
  2174       stack_object_type = current_frame->pop_stack(CHECK_VERIFY(this));
  2176       // The JVMS 2nd edition allows field initialization before the superclass
  2177       // initializer, if the field is defined within the current class.
  2178       fieldDescriptor fd;
  2179       if (stack_object_type == VerificationType::uninitialized_this_type() &&
  2180           target_class_type.equals(current_type()) &&
  2181           _klass->find_local_field(field_name, field_sig, &fd)) {
  2182         stack_object_type = current_type();
  2184       is_assignable = target_class_type.is_assignable_from(
  2185         stack_object_type, this, CHECK_VERIFY(this));
  2186       if (!is_assignable) {
  2187         verify_error(ErrorContext::bad_type(bci,
  2188             current_frame->stack_top_ctx(),
  2189             TypeOrigin::cp(index, target_class_type)),
  2190             "Bad type on operand stack in putfield");
  2191         return;
  2194     check_protected: {
  2195       if (_this_type == stack_object_type)
  2196         break; // stack_object_type must be assignable to _current_class_type
  2197       Symbol* ref_class_name =
  2198         cp->klass_name_at(cp->klass_ref_index_at(index));
  2199       if (!name_in_supers(ref_class_name, current_class()))
  2200         // stack_object_type must be assignable to _current_class_type since:
  2201         // 1. stack_object_type must be assignable to ref_class.
  2202         // 2. ref_class must be _current_class or a subclass of it. It can't
  2203         //    be a superclass of it. See revised JVMS 5.4.4.
  2204         break;
  2206       Klass* ref_class_oop = load_class(ref_class_name, CHECK);
  2207       if (is_protected_access(current_class(), ref_class_oop, field_name,
  2208                               field_sig, false)) {
  2209         // It's protected access, check if stack object is assignable to
  2210         // current class.
  2211         is_assignable = current_type().is_assignable_from(
  2212           stack_object_type, this, CHECK_VERIFY(this));
  2213         if (!is_assignable) {
  2214           verify_error(ErrorContext::bad_type(bci,
  2215               current_frame->stack_top_ctx(),
  2216               TypeOrigin::implicit(current_type())),
  2217               "Bad access to protected data in getfield");
  2218           return;
  2221       break;
  2223     default: ShouldNotReachHere();
  2227 void ClassVerifier::verify_invoke_init(
  2228     RawBytecodeStream* bcs, u2 ref_class_index, VerificationType ref_class_type,
  2229     StackMapFrame* current_frame, u4 code_length, bool *this_uninit,
  2230     constantPoolHandle cp, TRAPS) {
  2231   u2 bci = bcs->bci();
  2232   VerificationType type = current_frame->pop_stack(
  2233     VerificationType::reference_check(), CHECK_VERIFY(this));
  2234   if (type == VerificationType::uninitialized_this_type()) {
  2235     // The method must be an <init> method of this class or its superclass
  2236     Klass* superk = current_class()->super();
  2237     if (ref_class_type.name() != current_class()->name() &&
  2238         ref_class_type.name() != superk->name()) {
  2239       verify_error(ErrorContext::bad_type(bci,
  2240           TypeOrigin::implicit(ref_class_type),
  2241           TypeOrigin::implicit(current_type())),
  2242           "Bad <init> method call");
  2243       return;
  2245     current_frame->initialize_object(type, current_type());
  2246     *this_uninit = true;
  2247   } else if (type.is_uninitialized()) {
  2248     u2 new_offset = type.bci();
  2249     address new_bcp = bcs->bcp() - bci + new_offset;
  2250     if (new_offset > (code_length - 3) || (*new_bcp) != Bytecodes::_new) {
  2251       /* Unreachable?  Stack map parsing ensures valid type and new
  2252        * instructions have a valid BCI. */
  2253       verify_error(ErrorContext::bad_code(new_offset),
  2254                    "Expecting new instruction");
  2255       return;
  2257     u2 new_class_index = Bytes::get_Java_u2(new_bcp + 1);
  2258     verify_cp_class_type(bci, new_class_index, cp, CHECK_VERIFY(this));
  2260     // The method must be an <init> method of the indicated class
  2261     VerificationType new_class_type = cp_index_to_type(
  2262       new_class_index, cp, CHECK_VERIFY(this));
  2263     if (!new_class_type.equals(ref_class_type)) {
  2264       verify_error(ErrorContext::bad_type(bci,
  2265           TypeOrigin::cp(new_class_index, new_class_type),
  2266           TypeOrigin::cp(ref_class_index, ref_class_type)),
  2267           "Call to wrong <init> method");
  2268       return;
  2270     // According to the VM spec, if the referent class is a superclass of the
  2271     // current class, and is in a different runtime package, and the method is
  2272     // protected, then the objectref must be the current class or a subclass
  2273     // of the current class.
  2274     VerificationType objectref_type = new_class_type;
  2275     if (name_in_supers(ref_class_type.name(), current_class())) {
  2276       Klass* ref_klass = load_class(
  2277         ref_class_type.name(), CHECK_VERIFY(this));
  2278       Method* m = InstanceKlass::cast(ref_klass)->uncached_lookup_method(
  2279         vmSymbols::object_initializer_name(),
  2280         cp->signature_ref_at(bcs->get_index_u2()));
  2281       instanceKlassHandle mh(THREAD, m->method_holder());
  2282       if (m->is_protected() && !mh->is_same_class_package(_klass())) {
  2283         bool assignable = current_type().is_assignable_from(
  2284           objectref_type, this, CHECK_VERIFY(this));
  2285         if (!assignable) {
  2286           verify_error(ErrorContext::bad_type(bci,
  2287               TypeOrigin::cp(new_class_index, objectref_type),
  2288               TypeOrigin::implicit(current_type())),
  2289               "Bad access to protected <init> method");
  2290           return;
  2294     current_frame->initialize_object(type, new_class_type);
  2295   } else {
  2296     verify_error(ErrorContext::bad_type(bci, current_frame->stack_top_ctx()),
  2297         "Bad operand type when invoking <init>");
  2298     return;
  2302 void ClassVerifier::verify_invoke_instructions(
  2303     RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
  2304     bool *this_uninit, VerificationType return_type,
  2305     constantPoolHandle cp, TRAPS) {
  2306   // Make sure the constant pool item is the right type
  2307   u2 index = bcs->get_index_u2();
  2308   Bytecodes::Code opcode = bcs->raw_code();
  2309   unsigned int types;
  2310   switch (opcode) {
  2311     case Bytecodes::_invokeinterface:
  2312       types = 1 << JVM_CONSTANT_InterfaceMethodref;
  2313       break;
  2314     case Bytecodes::_invokedynamic:
  2315       types = 1 << JVM_CONSTANT_InvokeDynamic;
  2316       break;
  2317     case Bytecodes::_invokespecial:
  2318       types = (1 << JVM_CONSTANT_InterfaceMethodref) |
  2319               (1 << JVM_CONSTANT_Methodref);
  2320       break;
  2321     case Bytecodes::_invokestatic:
  2322       types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ?
  2323         (1 << JVM_CONSTANT_Methodref) :
  2324         ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref));
  2325       break;
  2326     default:
  2327       types = 1 << JVM_CONSTANT_Methodref;
  2329   verify_cp_type(bcs->bci(), index, cp, types, CHECK_VERIFY(this));
  2331   // Get method name and signature
  2332   Symbol* method_name = cp->name_ref_at(index);
  2333   Symbol* method_sig = cp->signature_ref_at(index);
  2335   if (!SignatureVerifier::is_valid_method_signature(method_sig)) {
  2336     class_format_error(
  2337       "Invalid method signature in class %s referenced "
  2338       "from constant pool index %d", _klass->external_name(), index);
  2339     return;
  2342   // Get referenced class type
  2343   VerificationType ref_class_type;
  2344   if (opcode == Bytecodes::_invokedynamic) {
  2345     if (!EnableInvokeDynamic ||
  2346         _klass->major_version() < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
  2347       class_format_error(
  2348         (!EnableInvokeDynamic ?
  2349          "invokedynamic instructions not enabled in this JVM" :
  2350          "invokedynamic instructions not supported by this class file version"),
  2351         _klass->external_name());
  2352       return;
  2354   } else {
  2355     ref_class_type = cp_ref_index_to_type(index, cp, CHECK_VERIFY(this));
  2358   // For a small signature length, we just allocate 128 bytes instead
  2359   // of parsing the signature once to find its size.
  2360   // -3 is for '(', ')' and return descriptor; multiply by 2 is for
  2361   // longs/doubles to be consertive.
  2362   assert(sizeof(VerificationType) == sizeof(uintptr_t),
  2363         "buffer type must match VerificationType size");
  2364   uintptr_t on_stack_sig_types_buffer[128];
  2365   // If we make a VerificationType[128] array directly, the compiler calls
  2366   // to the c-runtime library to do the allocation instead of just
  2367   // stack allocating it.  Plus it would run constructors.  This shows up
  2368   // in performance profiles.
  2370   VerificationType* sig_types;
  2371   int size = (method_sig->utf8_length() - 3) * 2;
  2372   if (size > 128) {
  2373     // Long and double occupies two slots here.
  2374     ArgumentSizeComputer size_it(method_sig);
  2375     size = size_it.size();
  2376     sig_types = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, size);
  2377   } else{
  2378     sig_types = (VerificationType*)on_stack_sig_types_buffer;
  2380   SignatureStream sig_stream(method_sig);
  2381   int sig_i = 0;
  2382   while (!sig_stream.at_return_type()) {
  2383     sig_i += change_sig_to_verificationType(
  2384       &sig_stream, &sig_types[sig_i], CHECK_VERIFY(this));
  2385     sig_stream.next();
  2387   int nargs = sig_i;
  2389 #ifdef ASSERT
  2391     ArgumentSizeComputer size_it(method_sig);
  2392     assert(nargs == size_it.size(), "Argument sizes do not match");
  2393     assert(nargs <= (method_sig->utf8_length() - 3) * 2, "estimate of max size isn't conservative enough");
  2395 #endif
  2397   // Check instruction operands
  2398   u2 bci = bcs->bci();
  2399   if (opcode == Bytecodes::_invokeinterface) {
  2400     address bcp = bcs->bcp();
  2401     // 4905268: count operand in invokeinterface should be nargs+1, not nargs.
  2402     // JSR202 spec: The count operand of an invokeinterface instruction is valid if it is
  2403     // the difference between the size of the operand stack before and after the instruction
  2404     // executes.
  2405     if (*(bcp+3) != (nargs+1)) {
  2406       verify_error(ErrorContext::bad_code(bci),
  2407           "Inconsistent args count operand in invokeinterface");
  2408       return;
  2410     if (*(bcp+4) != 0) {
  2411       verify_error(ErrorContext::bad_code(bci),
  2412           "Fourth operand byte of invokeinterface must be zero");
  2413       return;
  2417   if (opcode == Bytecodes::_invokedynamic) {
  2418     address bcp = bcs->bcp();
  2419     if (*(bcp+3) != 0 || *(bcp+4) != 0) {
  2420       verify_error(ErrorContext::bad_code(bci),
  2421           "Third and fourth operand bytes of invokedynamic must be zero");
  2422       return;
  2426   if (method_name->byte_at(0) == '<') {
  2427     // Make sure <init> can only be invoked by invokespecial
  2428     if (opcode != Bytecodes::_invokespecial ||
  2429         method_name != vmSymbols::object_initializer_name()) {
  2430       verify_error(ErrorContext::bad_code(bci),
  2431           "Illegal call to internal method");
  2432       return;
  2434   } else if (opcode == Bytecodes::_invokespecial
  2435              && !ref_class_type.equals(current_type())
  2436              && !ref_class_type.equals(VerificationType::reference_type(
  2437                   current_class()->super()->name()))) {
  2438     bool subtype = ref_class_type.is_assignable_from(
  2439       current_type(), this, CHECK_VERIFY(this));
  2440     if (!subtype) {
  2441       verify_error(ErrorContext::bad_code(bci),
  2442           "Bad invokespecial instruction: "
  2443           "current class isn't assignable to reference class.");
  2444        return;
  2447   // Match method descriptor with operand stack
  2448   for (int i = nargs - 1; i >= 0; i--) {  // Run backwards
  2449     current_frame->pop_stack(sig_types[i], CHECK_VERIFY(this));
  2451   // Check objectref on operand stack
  2452   if (opcode != Bytecodes::_invokestatic &&
  2453       opcode != Bytecodes::_invokedynamic) {
  2454     if (method_name == vmSymbols::object_initializer_name()) {  // <init> method
  2455       verify_invoke_init(bcs, index, ref_class_type, current_frame,
  2456         code_length, this_uninit, cp, CHECK_VERIFY(this));
  2457     } else {   // other methods
  2458       // Ensures that target class is assignable to method class.
  2459       if (opcode == Bytecodes::_invokespecial) {
  2460         current_frame->pop_stack(current_type(), CHECK_VERIFY(this));
  2461       } else if (opcode == Bytecodes::_invokevirtual) {
  2462         VerificationType stack_object_type =
  2463           current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this));
  2464         if (current_type() != stack_object_type) {
  2465           assert(cp->cache() == NULL, "not rewritten yet");
  2466           Symbol* ref_class_name =
  2467             cp->klass_name_at(cp->klass_ref_index_at(index));
  2468           // See the comments in verify_field_instructions() for
  2469           // the rationale behind this.
  2470           if (name_in_supers(ref_class_name, current_class())) {
  2471             Klass* ref_class = load_class(ref_class_name, CHECK);
  2472             if (is_protected_access(
  2473                   _klass, ref_class, method_name, method_sig, true)) {
  2474               // It's protected access, check if stack object is
  2475               // assignable to current class.
  2476               bool is_assignable = current_type().is_assignable_from(
  2477                 stack_object_type, this, CHECK_VERIFY(this));
  2478               if (!is_assignable) {
  2479                 if (ref_class_type.name() == vmSymbols::java_lang_Object()
  2480                     && stack_object_type.is_array()
  2481                     && method_name == vmSymbols::clone_name()) {
  2482                   // Special case: arrays pretend to implement public Object
  2483                   // clone().
  2484                 } else {
  2485                   verify_error(ErrorContext::bad_type(bci,
  2486                       current_frame->stack_top_ctx(),
  2487                       TypeOrigin::implicit(current_type())),
  2488                       "Bad access to protected data in invokevirtual");
  2489                   return;
  2495       } else {
  2496         assert(opcode == Bytecodes::_invokeinterface, "Unexpected opcode encountered");
  2497         current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this));
  2501   // Push the result type.
  2502   if (sig_stream.type() != T_VOID) {
  2503     if (method_name == vmSymbols::object_initializer_name()) {
  2504       // <init> method must have a void return type
  2505       /* Unreachable?  Class file parser verifies that methods with '<' have
  2506        * void return */
  2507       verify_error(ErrorContext::bad_code(bci),
  2508           "Return type must be void in <init> method");
  2509       return;
  2511     VerificationType return_type[2];
  2512     int n = change_sig_to_verificationType(
  2513       &sig_stream, return_type, CHECK_VERIFY(this));
  2514     for (int i = 0; i < n; i++) {
  2515       current_frame->push_stack(return_type[i], CHECK_VERIFY(this)); // push types backwards
  2520 VerificationType ClassVerifier::get_newarray_type(
  2521     u2 index, u2 bci, TRAPS) {
  2522   const char* from_bt[] = {
  2523     NULL, NULL, NULL, NULL, "[Z", "[C", "[F", "[D", "[B", "[S", "[I", "[J",
  2524   };
  2525   if (index < T_BOOLEAN || index > T_LONG) {
  2526     verify_error(ErrorContext::bad_code(bci), "Illegal newarray instruction");
  2527     return VerificationType::bogus_type();
  2530   // from_bt[index] contains the array signature which has a length of 2
  2531   Symbol* sig = create_temporary_symbol(
  2532     from_bt[index], 2, CHECK_(VerificationType::bogus_type()));
  2533   return VerificationType::reference_type(sig);
  2536 void ClassVerifier::verify_anewarray(
  2537     u2 bci, u2 index, constantPoolHandle cp,
  2538     StackMapFrame* current_frame, TRAPS) {
  2539   verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
  2540   current_frame->pop_stack(
  2541     VerificationType::integer_type(), CHECK_VERIFY(this));
  2543   VerificationType component_type =
  2544     cp_index_to_type(index, cp, CHECK_VERIFY(this));
  2545   int length;
  2546   char* arr_sig_str;
  2547   if (component_type.is_array()) {     // it's an array
  2548     const char* component_name = component_type.name()->as_utf8();
  2549     // add one dimension to component
  2550     length = (int)strlen(component_name) + 1;
  2551     arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length);
  2552     arr_sig_str[0] = '[';
  2553     strncpy(&arr_sig_str[1], component_name, length - 1);
  2554   } else {         // it's an object or interface
  2555     const char* component_name = component_type.name()->as_utf8();
  2556     // add one dimension to component with 'L' prepended and ';' postpended.
  2557     length = (int)strlen(component_name) + 3;
  2558     arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length);
  2559     arr_sig_str[0] = '[';
  2560     arr_sig_str[1] = 'L';
  2561     strncpy(&arr_sig_str[2], component_name, length - 2);
  2562     arr_sig_str[length - 1] = ';';
  2564   Symbol* arr_sig = create_temporary_symbol(
  2565     arr_sig_str, length, CHECK_VERIFY(this));
  2566   VerificationType new_array_type = VerificationType::reference_type(arr_sig);
  2567   current_frame->push_stack(new_array_type, CHECK_VERIFY(this));
  2570 void ClassVerifier::verify_iload(u2 index, StackMapFrame* current_frame, TRAPS) {
  2571   current_frame->get_local(
  2572     index, VerificationType::integer_type(), CHECK_VERIFY(this));
  2573   current_frame->push_stack(
  2574     VerificationType::integer_type(), CHECK_VERIFY(this));
  2577 void ClassVerifier::verify_lload(u2 index, StackMapFrame* current_frame, TRAPS) {
  2578   current_frame->get_local_2(
  2579     index, VerificationType::long_type(),
  2580     VerificationType::long2_type(), CHECK_VERIFY(this));
  2581   current_frame->push_stack_2(
  2582     VerificationType::long_type(),
  2583     VerificationType::long2_type(), CHECK_VERIFY(this));
  2586 void ClassVerifier::verify_fload(u2 index, StackMapFrame* current_frame, TRAPS) {
  2587   current_frame->get_local(
  2588     index, VerificationType::float_type(), CHECK_VERIFY(this));
  2589   current_frame->push_stack(
  2590     VerificationType::float_type(), CHECK_VERIFY(this));
  2593 void ClassVerifier::verify_dload(u2 index, StackMapFrame* current_frame, TRAPS) {
  2594   current_frame->get_local_2(
  2595     index, VerificationType::double_type(),
  2596     VerificationType::double2_type(), CHECK_VERIFY(this));
  2597   current_frame->push_stack_2(
  2598     VerificationType::double_type(),
  2599     VerificationType::double2_type(), CHECK_VERIFY(this));
  2602 void ClassVerifier::verify_aload(u2 index, StackMapFrame* current_frame, TRAPS) {
  2603   VerificationType type = current_frame->get_local(
  2604     index, VerificationType::reference_check(), CHECK_VERIFY(this));
  2605   current_frame->push_stack(type, CHECK_VERIFY(this));
  2608 void ClassVerifier::verify_istore(u2 index, StackMapFrame* current_frame, TRAPS) {
  2609   current_frame->pop_stack(
  2610     VerificationType::integer_type(), CHECK_VERIFY(this));
  2611   current_frame->set_local(
  2612     index, VerificationType::integer_type(), CHECK_VERIFY(this));
  2615 void ClassVerifier::verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS) {
  2616   current_frame->pop_stack_2(
  2617     VerificationType::long2_type(),
  2618     VerificationType::long_type(), CHECK_VERIFY(this));
  2619   current_frame->set_local_2(
  2620     index, VerificationType::long_type(),
  2621     VerificationType::long2_type(), CHECK_VERIFY(this));
  2624 void ClassVerifier::verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS) {
  2625   current_frame->pop_stack(VerificationType::float_type(), CHECK_VERIFY(this));
  2626   current_frame->set_local(
  2627     index, VerificationType::float_type(), CHECK_VERIFY(this));
  2630 void ClassVerifier::verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS) {
  2631   current_frame->pop_stack_2(
  2632     VerificationType::double2_type(),
  2633     VerificationType::double_type(), CHECK_VERIFY(this));
  2634   current_frame->set_local_2(
  2635     index, VerificationType::double_type(),
  2636     VerificationType::double2_type(), CHECK_VERIFY(this));
  2639 void ClassVerifier::verify_astore(u2 index, StackMapFrame* current_frame, TRAPS) {
  2640   VerificationType type = current_frame->pop_stack(
  2641     VerificationType::reference_check(), CHECK_VERIFY(this));
  2642   current_frame->set_local(index, type, CHECK_VERIFY(this));
  2645 void ClassVerifier::verify_iinc(u2 index, StackMapFrame* current_frame, TRAPS) {
  2646   VerificationType type = current_frame->get_local(
  2647     index, VerificationType::integer_type(), CHECK_VERIFY(this));
  2648   current_frame->set_local(index, type, CHECK_VERIFY(this));
  2651 void ClassVerifier::verify_return_value(
  2652     VerificationType return_type, VerificationType type, u2 bci,
  2653     StackMapFrame* current_frame, TRAPS) {
  2654   if (return_type == VerificationType::bogus_type()) {
  2655     verify_error(ErrorContext::bad_type(bci,
  2656         current_frame->stack_top_ctx(), TypeOrigin::signature(return_type)),
  2657         "Method expects a return value");
  2658     return;
  2660   bool match = return_type.is_assignable_from(type, this, CHECK_VERIFY(this));
  2661   if (!match) {
  2662     verify_error(ErrorContext::bad_type(bci,
  2663         current_frame->stack_top_ctx(), TypeOrigin::signature(return_type)),
  2664         "Bad return type");
  2665     return;
  2669 // The verifier creates symbols which are substrings of Symbols.
  2670 // These are stored in the verifier until the end of verification so that
  2671 // they can be reference counted.
  2672 Symbol* ClassVerifier::create_temporary_symbol(const Symbol *s, int begin,
  2673                                                int end, TRAPS) {
  2674   Symbol* sym = SymbolTable::new_symbol(s, begin, end, CHECK_NULL);
  2675   _symbols->push(sym);
  2676   return sym;
  2679 Symbol* ClassVerifier::create_temporary_symbol(const char *s, int length, TRAPS) {
  2680   Symbol* sym = SymbolTable::new_symbol(s, length, CHECK_NULL);
  2681   _symbols->push(sym);
  2682   return sym;

mercurial