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