src/share/vm/classfile/verifier.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6871
077483254bf6
parent 1
2d8a650513c2
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial