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