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