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