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