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