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