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