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