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