1 /*
   2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/javaClasses.hpp"
  30 #include "classfile/stackMapTable.hpp"
  31 #include "classfile/stackMapFrame.hpp"
  32 #include "classfile/stackMapTableFormat.hpp"

  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/verifier.hpp"
  35 #include "classfile/vmSymbols.hpp"
  36 #include "interpreter/bytecodes.hpp"
  37 #include "interpreter/bytecodeStream.hpp"
  38 #include "logging/log.hpp"
  39 #include "logging/logStream.hpp"
  40 #include "memory/oopFactory.hpp"
  41 #include "memory/resourceArea.hpp"

  42 #include "oops/constantPool.inline.hpp"
  43 #include "oops/instanceKlass.hpp"
  44 #include "oops/oop.inline.hpp"
  45 #include "oops/typeArrayOop.hpp"
  46 #include "runtime/fieldDescriptor.hpp"
  47 #include "runtime/handles.inline.hpp"
  48 #include "runtime/interfaceSupport.inline.hpp"
  49 #include "runtime/javaCalls.hpp"
  50 #include "runtime/jniHandles.inline.hpp"
  51 #include "runtime/orderAccess.hpp"
  52 #include "runtime/os.hpp"
  53 #include "runtime/safepointVerifiers.hpp"
  54 #include "runtime/thread.hpp"
  55 #include "services/threadService.hpp"
  56 #include "utilities/align.hpp"
  57 #include "utilities/bytes.hpp"
  58 
  59 #define NOFAILOVER_MAJOR_VERSION                       51
  60 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION  51
  61 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION       52
  62 #define MAX_ARRAY_DIMENSIONS 255
  63 
  64 // Access to external entry for VerifyClassCodes - old byte code verifier
  65 
  66 extern "C" {
  67   typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint);
  68   typedef jboolean (*verify_byte_codes_fn_new_t)(JNIEnv *, jclass, char *, jint, jint);
  69 }
  70 
  71 static void* volatile _verify_byte_codes_fn = NULL;
  72 
  73 static volatile jint _is_new_verify_byte_codes_fn = (jint) true;
  74 
  75 static void* verify_byte_codes_fn() {
  76   if (OrderAccess::load_acquire(&_verify_byte_codes_fn) == NULL) {
  77     void *lib_handle = os::native_java_library();
  78     void *func = os::dll_lookup(lib_handle, "VerifyClassCodesForMajorVersion");
  79     OrderAccess::release_store(&_verify_byte_codes_fn, func);
  80     if (func == NULL) {
  81       _is_new_verify_byte_codes_fn = false;
  82       func = os::dll_lookup(lib_handle, "VerifyClassCodes");
  83       OrderAccess::release_store(&_verify_byte_codes_fn, func);
  84     }
  85   }
  86   return (void*)_verify_byte_codes_fn;
  87 }
  88 
  89 
  90 // Methods in Verifier
  91 
  92 bool Verifier::should_verify_for(oop class_loader, bool should_verify_class) {
  93   return (class_loader == NULL || !should_verify_class) ?
  94     BytecodeVerificationLocal : BytecodeVerificationRemote;
  95 }
  96 
  97 bool Verifier::relax_access_for(oop loader) {
  98   bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
  99   bool need_verify =
 100     // verifyAll
 101     (BytecodeVerificationLocal && BytecodeVerificationRemote) ||
 102     // verifyRemote
 103     (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);
 104   return !need_verify;
 105 }
 106 
 107 void Verifier::trace_class_resolution(Klass* resolve_class, InstanceKlass* verify_class) {
 108   assert(verify_class != NULL, "Unexpected null verify_class");
 109   ResourceMark rm;
 110   Symbol* s = verify_class->source_file_name();
 111   const char* source_file = (s != NULL ? s->as_C_string() : NULL);
 112   const char* verify = verify_class->external_name();
 113   const char* resolve = resolve_class->external_name();
 114   // print in a single call to reduce interleaving between threads
 115   if (source_file != NULL) {
 116     log_debug(class, resolve)("%s %s %s (verification)", verify, resolve, source_file);
 117   } else {
 118     log_debug(class, resolve)("%s %s (verification)", verify, resolve);
 119   }
 120 }
 121 
 122 // Prints the end-verification message to the appropriate output.
 123 void Verifier::log_end_verification(outputStream* st, const char* klassName, Symbol* exception_name, TRAPS) {
 124   if (HAS_PENDING_EXCEPTION) {
 125     st->print("Verification for %s has", klassName);
 126     st->print_cr(" exception pending %s ",
 127                  PENDING_EXCEPTION->klass()->external_name());
 128   } else if (exception_name != NULL) {
 129     st->print_cr("Verification for %s failed", klassName);
 130   }
 131   st->print_cr("End class verification for: %s", klassName);
 132 }
 133 
 134 bool Verifier::verify(InstanceKlass* klass, bool should_verify_class, TRAPS) {
 135   HandleMark hm(THREAD);
 136   ResourceMark rm(THREAD);
 137 
 138   // Eagerly allocate the identity hash code for a klass. This is a fallout
 139   // from 6320749 and 8059924: hash code generator is not supposed to be called
 140   // during the safepoint, but it allows to sneak the hashcode in during
 141   // verification. Without this eager hashcode generation, we may end up
 142   // installing the hashcode during some other operation, which may be at
 143   // safepoint -- blowing up the checks. It was previously done as the side
 144   // effect (sic!) for external_name(), but instead of doing that, we opt to
 145   // explicitly push the hashcode in here. This is signify the following block
 146   // is IMPORTANT:
 147   if (klass->java_mirror() != NULL) {
 148     klass->java_mirror()->identity_hash();
 149   }
 150 
 151   if (!is_eligible_for_verification(klass, should_verify_class)) {
 152     return true;
 153   }
 154 
 155   // Timer includes any side effects of class verification (resolution,
 156   // etc), but not recursive calls to Verifier::verify().
 157   JavaThread* jt = (JavaThread*)THREAD;
 158   PerfClassTraceTime timer(ClassLoader::perf_class_verify_time(),
 159                            ClassLoader::perf_class_verify_selftime(),
 160                            ClassLoader::perf_classes_verified(),
 161                            jt->get_thread_stat()->perf_recursion_counts_addr(),
 162                            jt->get_thread_stat()->perf_timers_addr(),
 163                            PerfClassTraceTime::CLASS_VERIFY);
 164 
 165   // If the class should be verified, first see if we can use the split
 166   // verifier.  If not, or if verification fails and FailOverToOldVerifier
 167   // is set, then call the inference verifier.
 168   Symbol* exception_name = NULL;
 169   const size_t message_buffer_len = klass->name()->utf8_length() + 1024;
 170   char* message_buffer = NULL;
 171   char* exception_message = NULL;
 172 
 173   bool can_failover = FailOverToOldVerifier &&
 174      klass->major_version() < NOFAILOVER_MAJOR_VERSION;
 175 
 176   log_info(class, init)("Start class verification for: %s", klass->external_name());
 177   if (klass->major_version() >= STACKMAP_ATTRIBUTE_MAJOR_VERSION) {
 178     ClassVerifier split_verifier(klass, THREAD);
 179     split_verifier.verify_class(THREAD);
 180     exception_name = split_verifier.result();
 181     if (can_failover && !HAS_PENDING_EXCEPTION &&
 182         (exception_name == vmSymbols::java_lang_VerifyError() ||
 183          exception_name == vmSymbols::java_lang_ClassFormatError())) {
 184       log_info(verification)("Fail over class verification to old verifier for: %s", klass->external_name());
 185       log_info(class, init)("Fail over class verification to old verifier for: %s", klass->external_name());
 186       message_buffer = NEW_RESOURCE_ARRAY(char, message_buffer_len);
 187       exception_message = message_buffer;
 188       exception_name = inference_verify(
 189         klass, message_buffer, message_buffer_len, THREAD);
 190     }
 191     if (exception_name != NULL) {
 192       exception_message = split_verifier.exception_message();
 193     }
 194   } else {
 195     message_buffer = NEW_RESOURCE_ARRAY(char, message_buffer_len);
 196     exception_message = message_buffer;
 197     exception_name = inference_verify(
 198         klass, message_buffer, message_buffer_len, THREAD);
 199   }
 200 
 201   LogTarget(Info, class, init) lt1;
 202   if (lt1.is_enabled()) {
 203     LogStream ls(lt1);
 204     log_end_verification(&ls, klass->external_name(), exception_name, THREAD);
 205   }
 206   LogTarget(Info, verification) lt2;
 207   if (lt2.is_enabled()) {
 208     LogStream ls(lt2);
 209     log_end_verification(&ls, klass->external_name(), exception_name, THREAD);
 210   }
 211 
 212   if (HAS_PENDING_EXCEPTION) {
 213     return false; // use the existing exception
 214   } else if (exception_name == NULL) {
 215     return true; // verification succeeded
 216   } else { // VerifyError or ClassFormatError to be created and thrown
 217     Klass* kls =
 218       SystemDictionary::resolve_or_fail(exception_name, true, CHECK_false);
 219     if (log_is_enabled(Debug, class, resolve)) {
 220       Verifier::trace_class_resolution(kls, klass);
 221     }
 222 
 223     while (kls != NULL) {
 224       if (kls == klass) {
 225         // If the class being verified is the exception we're creating
 226         // or one of it's superclasses, we're in trouble and are going
 227         // to infinitely recurse when we try to initialize the exception.
 228         // So bail out here by throwing the preallocated VM error.
 229         THROW_OOP_(Universe::virtual_machine_error_instance(), false);
 230       }
 231       kls = kls->super();
 232     }
 233     if (message_buffer != NULL) {
 234       message_buffer[message_buffer_len - 1] = '\0'; // just to be sure
 235     }
 236     assert(exception_message != NULL, "");
 237     THROW_MSG_(exception_name, exception_message, false);
 238   }
 239 }
 240 
 241 bool Verifier::is_eligible_for_verification(InstanceKlass* klass, bool should_verify_class) {
 242   Symbol* name = klass->name();
 243   Klass* refl_magic_klass = SystemDictionary::reflect_MagicAccessorImpl_klass();
 244 
 245   bool is_reflect = refl_magic_klass != NULL && klass->is_subtype_of(refl_magic_klass);
 246 
 247   return (should_verify_for(klass->class_loader(), should_verify_class) &&
 248     // return if the class is a bootstrapping class
 249     // or defineClass specified not to verify by default (flags override passed arg)
 250     // We need to skip the following four for bootstraping
 251     name != vmSymbols::java_lang_Object() &&
 252     name != vmSymbols::java_lang_Class() &&
 253     name != vmSymbols::java_lang_String() &&
 254     name != vmSymbols::java_lang_Throwable() &&
 255 
 256     // Can not verify the bytecodes for shared classes because they have
 257     // already been rewritten to contain constant pool cache indices,
 258     // which the verifier can't understand.
 259     // Shared classes shouldn't have stackmaps either.
 260     !klass->is_shared() &&
 261 
 262     // As of the fix for 4486457 we disable verification for all of the
 263     // dynamically-generated bytecodes associated with the 1.4
 264     // reflection implementation, not just those associated with
 265     // jdk/internal/reflect/SerializationConstructorAccessor.
 266     // NOTE: this is called too early in the bootstrapping process to be
 267     // guarded by Universe::is_gte_jdk14x_version().
 268     // Also for lambda generated code, gte jdk8
 269     (!is_reflect));
 270 }
 271 
 272 Symbol* Verifier::inference_verify(
 273     InstanceKlass* klass, char* message, size_t message_len, TRAPS) {
 274   JavaThread* thread = (JavaThread*)THREAD;
 275   JNIEnv *env = thread->jni_environment();
 276 
 277   void* verify_func = verify_byte_codes_fn();
 278 
 279   if (verify_func == NULL) {
 280     jio_snprintf(message, message_len, "Could not link verifier");
 281     return vmSymbols::java_lang_VerifyError();
 282   }
 283 
 284   ResourceMark rm(THREAD);
 285   log_info(verification)("Verifying class %s with old format", klass->external_name());
 286 
 287   jclass cls = (jclass) JNIHandles::make_local(env, klass->java_mirror());
 288   jint result;
 289 
 290   {
 291     HandleMark hm(thread);
 292     ThreadToNativeFromVM ttn(thread);
 293     // ThreadToNativeFromVM takes care of changing thread_state, so safepoint
 294     // code knows that we have left the VM
 295 
 296     if (_is_new_verify_byte_codes_fn) {
 297       verify_byte_codes_fn_new_t func =
 298         CAST_TO_FN_PTR(verify_byte_codes_fn_new_t, verify_func);
 299       result = (*func)(env, cls, message, (int)message_len,
 300           klass->major_version());
 301     } else {
 302       verify_byte_codes_fn_t func =
 303         CAST_TO_FN_PTR(verify_byte_codes_fn_t, verify_func);
 304       result = (*func)(env, cls, message, (int)message_len);
 305     }
 306   }
 307 
 308   JNIHandles::destroy_local(cls);
 309 
 310   // These numbers are chosen so that VerifyClassCodes interface doesn't need
 311   // to be changed (still return jboolean (unsigned char)), and result is
 312   // 1 when verification is passed.
 313   if (result == 0) {
 314     return vmSymbols::java_lang_VerifyError();
 315   } else if (result == 1) {
 316     return NULL; // verified.
 317   } else if (result == 2) {
 318     THROW_MSG_(vmSymbols::java_lang_OutOfMemoryError(), message, NULL);
 319   } else if (result == 3) {
 320     return vmSymbols::java_lang_ClassFormatError();
 321   } else {
 322     ShouldNotReachHere();
 323     return NULL;
 324   }
 325 }
 326 
 327 TypeOrigin TypeOrigin::null() {
 328   return TypeOrigin();
 329 }
 330 TypeOrigin TypeOrigin::local(u2 index, StackMapFrame* frame) {
 331   assert(frame != NULL, "Must have a frame");
 332   return TypeOrigin(CF_LOCALS, index, StackMapFrame::copy(frame),
 333      frame->local_at(index));
 334 }
 335 TypeOrigin TypeOrigin::stack(u2 index, StackMapFrame* frame) {
 336   assert(frame != NULL, "Must have a frame");
 337   return TypeOrigin(CF_STACK, index, StackMapFrame::copy(frame),
 338       frame->stack_at(index));
 339 }
 340 TypeOrigin TypeOrigin::sm_local(u2 index, StackMapFrame* frame) {
 341   assert(frame != NULL, "Must have a frame");
 342   return TypeOrigin(SM_LOCALS, index, StackMapFrame::copy(frame),
 343       frame->local_at(index));
 344 }
 345 TypeOrigin TypeOrigin::sm_stack(u2 index, StackMapFrame* frame) {
 346   assert(frame != NULL, "Must have a frame");
 347   return TypeOrigin(SM_STACK, index, StackMapFrame::copy(frame),
 348       frame->stack_at(index));
 349 }
 350 TypeOrigin TypeOrigin::bad_index(u2 index) {
 351   return TypeOrigin(BAD_INDEX, index, NULL, VerificationType::bogus_type());
 352 }
 353 TypeOrigin TypeOrigin::cp(u2 index, VerificationType vt) {
 354   return TypeOrigin(CONST_POOL, index, NULL, vt);
 355 }
 356 TypeOrigin TypeOrigin::signature(VerificationType vt) {
 357   return TypeOrigin(SIG, 0, NULL, vt);
 358 }
 359 TypeOrigin TypeOrigin::implicit(VerificationType t) {
 360   return TypeOrigin(IMPLICIT, 0, NULL, t);
 361 }
 362 TypeOrigin TypeOrigin::frame(StackMapFrame* frame) {
 363   return TypeOrigin(FRAME_ONLY, 0, StackMapFrame::copy(frame),
 364                     VerificationType::bogus_type());
 365 }
 366 
 367 void TypeOrigin::reset_frame() {
 368   if (_frame != NULL) {
 369     _frame->restore();
 370   }
 371 }
 372 
 373 void TypeOrigin::details(outputStream* ss) const {
 374   _type.print_on(ss);
 375   switch (_origin) {
 376     case CF_LOCALS:
 377       ss->print(" (current frame, locals[%d])", _index);
 378       break;
 379     case CF_STACK:
 380       ss->print(" (current frame, stack[%d])", _index);
 381       break;
 382     case SM_LOCALS:
 383       ss->print(" (stack map, locals[%d])", _index);
 384       break;
 385     case SM_STACK:
 386       ss->print(" (stack map, stack[%d])", _index);
 387       break;
 388     case CONST_POOL:
 389       ss->print(" (constant pool %d)", _index);
 390       break;
 391     case SIG:
 392       ss->print(" (from method signature)");
 393       break;
 394     case IMPLICIT:
 395     case FRAME_ONLY:
 396     case NONE:
 397     default:
 398       ;
 399   }
 400 }
 401 
 402 #ifdef ASSERT
 403 void TypeOrigin::print_on(outputStream* str) const {
 404   str->print("{%d,%d,%p:", _origin, _index, _frame);
 405   if (_frame != NULL) {
 406     _frame->print_on(str);
 407   } else {
 408     str->print("null");
 409   }
 410   str->print(",");
 411   _type.print_on(str);
 412   str->print("}");
 413 }
 414 #endif
 415 
 416 void ErrorContext::details(outputStream* ss, const Method* method) const {
 417   if (is_valid()) {
 418     ss->cr();
 419     ss->print_cr("Exception Details:");
 420     location_details(ss, method);
 421     reason_details(ss);
 422     frame_details(ss);
 423     bytecode_details(ss, method);
 424     handler_details(ss, method);
 425     stackmap_details(ss, method);
 426   }
 427 }
 428 
 429 void ErrorContext::reason_details(outputStream* ss) const {
 430   streamIndentor si(ss);
 431   ss->indent().print_cr("Reason:");
 432   streamIndentor si2(ss);
 433   ss->indent().print("%s", "");
 434   switch (_fault) {
 435     case INVALID_BYTECODE:
 436       ss->print("Error exists in the bytecode");
 437       break;
 438     case WRONG_TYPE:
 439       if (_expected.is_valid()) {
 440         ss->print("Type ");
 441         _type.details(ss);
 442         ss->print(" is not assignable to ");
 443         _expected.details(ss);
 444       } else {
 445         ss->print("Invalid type: ");
 446         _type.details(ss);
 447       }
 448       break;
 449     case FLAGS_MISMATCH:
 450       if (_expected.is_valid()) {
 451         ss->print("Current frame's flags are not assignable "
 452                   "to stack map frame's.");
 453       } else {
 454         ss->print("Current frame's flags are invalid in this context.");
 455       }
 456       break;
 457     case BAD_CP_INDEX:
 458       ss->print("Constant pool index %d is invalid", _type.index());
 459       break;
 460     case BAD_LOCAL_INDEX:
 461       ss->print("Local index %d is invalid", _type.index());
 462       break;
 463     case LOCALS_SIZE_MISMATCH:
 464       ss->print("Current frame's local size doesn't match stackmap.");
 465       break;
 466     case STACK_SIZE_MISMATCH:
 467       ss->print("Current frame's stack size doesn't match stackmap.");
 468       break;
 469     case STACK_OVERFLOW:
 470       ss->print("Exceeded max stack size.");
 471       break;
 472     case STACK_UNDERFLOW:
 473       ss->print("Attempt to pop empty stack.");
 474       break;
 475     case MISSING_STACKMAP:
 476       ss->print("Expected stackmap frame at this location.");
 477       break;
 478     case BAD_STACKMAP:
 479       ss->print("Invalid stackmap specification.");
 480       break;
 481     case UNKNOWN:
 482     default:
 483       ShouldNotReachHere();
 484       ss->print_cr("Unknown");
 485   }
 486   ss->cr();
 487 }
 488 
 489 void ErrorContext::location_details(outputStream* ss, const Method* method) const {
 490   if (_bci != -1 && method != NULL) {
 491     streamIndentor si(ss);
 492     const char* bytecode_name = "<invalid>";
 493     if (method->validate_bci(_bci) != -1) {
 494       Bytecodes::Code code = Bytecodes::code_or_bp_at(method->bcp_from(_bci));
 495       if (Bytecodes::is_defined(code)) {
 496           bytecode_name = Bytecodes::name(code);
 497       } else {
 498           bytecode_name = "<illegal>";
 499       }
 500     }
 501     InstanceKlass* ik = method->method_holder();
 502     ss->indent().print_cr("Location:");
 503     streamIndentor si2(ss);
 504     ss->indent().print_cr("%s.%s%s @%d: %s",
 505         ik->name()->as_C_string(), method->name()->as_C_string(),
 506         method->signature()->as_C_string(), _bci, bytecode_name);
 507   }
 508 }
 509 
 510 void ErrorContext::frame_details(outputStream* ss) const {
 511   streamIndentor si(ss);
 512   if (_type.is_valid() && _type.frame() != NULL) {
 513     ss->indent().print_cr("Current Frame:");
 514     streamIndentor si2(ss);
 515     _type.frame()->print_on(ss);
 516   }
 517   if (_expected.is_valid() && _expected.frame() != NULL) {
 518     ss->indent().print_cr("Stackmap Frame:");
 519     streamIndentor si2(ss);
 520     _expected.frame()->print_on(ss);
 521   }
 522 }
 523 
 524 void ErrorContext::bytecode_details(outputStream* ss, const Method* method) const {
 525   if (method != NULL) {
 526     streamIndentor si(ss);
 527     ss->indent().print_cr("Bytecode:");
 528     streamIndentor si2(ss);
 529     ss->print_data(method->code_base(), method->code_size(), false);
 530   }
 531 }
 532 
 533 void ErrorContext::handler_details(outputStream* ss, const Method* method) const {
 534   if (method != NULL) {
 535     streamIndentor si(ss);
 536     ExceptionTable table(method);
 537     if (table.length() > 0) {
 538       ss->indent().print_cr("Exception Handler Table:");
 539       streamIndentor si2(ss);
 540       for (int i = 0; i < table.length(); ++i) {
 541         ss->indent().print_cr("bci [%d, %d] => handler: %d", table.start_pc(i),
 542             table.end_pc(i), table.handler_pc(i));
 543       }
 544     }
 545   }
 546 }
 547 
 548 void ErrorContext::stackmap_details(outputStream* ss, const Method* method) const {
 549   if (method != NULL && method->has_stackmap_table()) {
 550     streamIndentor si(ss);
 551     ss->indent().print_cr("Stackmap Table:");
 552     Array<u1>* data = method->stackmap_data();
 553     stack_map_table* sm_table =
 554         stack_map_table::at((address)data->adr_at(0));
 555     stack_map_frame* sm_frame = sm_table->entries();
 556     streamIndentor si2(ss);
 557     int current_offset = -1;
 558     address end_of_sm_table = (address)sm_table + method->stackmap_data()->length();
 559     for (u2 i = 0; i < sm_table->number_of_entries(); ++i) {
 560       ss->indent();
 561       if (!sm_frame->verify((address)sm_frame, end_of_sm_table)) {
 562         sm_frame->print_truncated(ss, current_offset);
 563         return;
 564       }
 565       sm_frame->print_on(ss, current_offset);
 566       ss->cr();
 567       current_offset += sm_frame->offset_delta();
 568       sm_frame = sm_frame->next();
 569     }
 570   }
 571 }
 572 
 573 // Methods in ClassVerifier
 574 
 575 ClassVerifier::ClassVerifier(
 576     InstanceKlass* klass, TRAPS)
 577     : _thread(THREAD), _previous_symbol(NULL), _symbols(NULL), _exception_type(NULL),
 578       _message(NULL), _method_signatures_table(NULL), _klass(klass) {
 579   _this_type = VerificationType::reference_type(klass->name());
 580 }
 581 
 582 ClassVerifier::~ClassVerifier() {
 583   // Decrement the reference count for any symbols created.
 584   if (_symbols != NULL) {
 585     for (int i = 0; i < _symbols->length(); i++) {
 586       Symbol* s = _symbols->at(i);
 587       s->decrement_refcount();
 588     }
 589   }
 590 }
 591 
 592 VerificationType ClassVerifier::object_type() const {
 593   return VerificationType::reference_type(vmSymbols::java_lang_Object());
 594 }
 595 
 596 TypeOrigin ClassVerifier::ref_ctx(const char* sig, TRAPS) {
 597   VerificationType vt = VerificationType::reference_type(
 598       create_temporary_symbol(sig, (int)strlen(sig), THREAD));
 599   return TypeOrigin::implicit(vt);
 600 }
 601 
 602 void ClassVerifier::verify_class(TRAPS) {
 603   log_info(verification)("Verifying class %s with new format", _klass->external_name());
 604 
 605   // Either verifying both local and remote classes or just remote classes.
 606   assert(BytecodeVerificationRemote, "Should not be here");
 607 
 608   // Create hash table containing method signatures.
 609   method_signatures_table_type method_signatures_table;
 610   set_method_signatures_table(&method_signatures_table);
 611 
 612   Array<Method*>* methods = _klass->methods();
 613   int num_methods = methods->length();
 614 
 615   for (int index = 0; index < num_methods; index++) {
 616     // Check for recursive re-verification before each method.
 617     if (was_recursively_verified())  return;
 618 
 619     Method* m = methods->at(index);
 620     if (m->is_native() || m->is_abstract() || m->is_overpass()) {
 621       // If m is native or abstract, skip it.  It is checked in class file
 622       // parser that methods do not override a final method.  Overpass methods
 623       // are trusted since the VM generates them.
 624       continue;
 625     }
 626     verify_method(methodHandle(THREAD, m), CHECK_VERIFY(this));
 627   }
 628 
 629   if (was_recursively_verified()){
 630     log_info(verification)("Recursive verification detected for: %s", _klass->external_name());
 631     log_info(class, init)("Recursive verification detected for: %s",
 632                         _klass->external_name());
 633   }
 634 }
 635 
 636 // Translate the signature entries into verification types and save them in
 637 // the growable array.  Also, save the count of arguments.
 638 void ClassVerifier::translate_signature(Symbol* const method_sig,
 639                                         sig_as_verification_types* sig_verif_types,
 640                                         TRAPS) {
 641   SignatureStream sig_stream(method_sig);
 642   VerificationType sig_type[2];
 643   int sig_i = 0;
 644   GrowableArray<VerificationType>* verif_types = sig_verif_types->sig_verif_types();
 645 
 646   // Translate the signature arguments into verification types.
 647   while (!sig_stream.at_return_type()) {
 648     int n = change_sig_to_verificationType(&sig_stream, sig_type, CHECK_VERIFY(this));
 649     assert(n <= 2, "Unexpected signature type");
 650 
 651     // Store verification type(s).  Longs and Doubles each have two verificationTypes.
 652     for (int x = 0; x < n; x++) {
 653       verif_types->push(sig_type[x]);
 654     }
 655     sig_i += n;
 656     sig_stream.next();
 657   }
 658 
 659   // Set final arg count, not including the return type.  The final arg count will
 660   // be compared with sig_verify_types' length to see if there is a return type.
 661   sig_verif_types->set_num_args(sig_i);
 662 
 663   // Store verification type(s) for the return type, if there is one.
 664   if (sig_stream.type() != T_VOID) {
 665     int n = change_sig_to_verificationType(&sig_stream, sig_type, CHECK_VERIFY(this));
 666     assert(n <= 2, "Unexpected signature return type");
 667     for (int y = 0; y < n; y++) {
 668       verif_types->push(sig_type[y]);
 669     }
 670   }
 671 }
 672 
 673 void ClassVerifier::create_method_sig_entry(sig_as_verification_types* sig_verif_types,
 674                                             int sig_index, TRAPS) {
 675   // Translate the signature into verification types.
 676   ConstantPool* cp = _klass->constants();
 677   Symbol* const method_sig = cp->symbol_at(sig_index);
 678   translate_signature(method_sig, sig_verif_types, CHECK_VERIFY(this));
 679 
 680   // Add the list of this signature's verification types to the table.
 681   bool is_unique = method_signatures_table()->put(sig_index, sig_verif_types);
 682   assert(is_unique, "Duplicate entries in method_signature_table");
 683 }
 684 
 685 void ClassVerifier::verify_method(const methodHandle& m, TRAPS) {
 686   HandleMark hm(THREAD);
 687   _method = m;   // initialize _method
 688   log_info(verification)("Verifying method %s", m->name_and_sig_as_C_string());
 689 
 690 // For clang, the only good constant format string is a literal constant format string.
 691 #define bad_type_msg "Bad type on operand stack in %s"
 692 
 693   int32_t max_stack = m->verifier_max_stack();
 694   int32_t max_locals = m->max_locals();
 695   constantPoolHandle cp(THREAD, m->constants());
 696 
 697   // Method signature was checked in ClassFileParser.
 698   assert(SignatureVerifier::is_valid_method_signature(m->signature()),
 699          "Invalid method signature");
 700 
 701   // Initial stack map frame: offset is 0, stack is initially empty.
 702   StackMapFrame current_frame(max_locals, max_stack, this);
 703   // Set initial locals
 704   VerificationType return_type = current_frame.set_locals_from_arg(
 705     m, current_type(), CHECK_VERIFY(this));
 706 
 707   int32_t stackmap_index = 0; // index to the stackmap array
 708 
 709   u4 code_length = m->code_size();
 710 
 711   // Scan the bytecode and map each instruction's start offset to a number.
 712   char* code_data = generate_code_data(m, code_length, CHECK_VERIFY(this));
 713 
 714   int ex_min = code_length;
 715   int ex_max = -1;
 716   // Look through each item on the exception table. Each of the fields must refer
 717   // to a legal instruction.
 718   if (was_recursively_verified()) return;
 719   verify_exception_handler_table(
 720     code_length, code_data, ex_min, ex_max, CHECK_VERIFY(this));
 721 
 722   // Look through each entry on the local variable table and make sure
 723   // its range of code array offsets is valid. (4169817)
 724   if (m->has_localvariable_table()) {
 725     verify_local_variable_table(code_length, code_data, CHECK_VERIFY(this));
 726   }
 727 
 728   Array<u1>* stackmap_data = m->stackmap_data();
 729   StackMapStream stream(stackmap_data);
 730   StackMapReader reader(this, &stream, code_data, code_length, THREAD);
 731   StackMapTable stackmap_table(&reader, &current_frame, max_locals, max_stack,
 732                                code_data, code_length, CHECK_VERIFY(this));
 733 
 734   LogTarget(Info, verification) lt;
 735   if (lt.is_enabled()) {
 736     ResourceMark rm(THREAD);
 737     LogStream ls(lt);
 738     stackmap_table.print_on(&ls);
 739   }
 740 
 741   RawBytecodeStream bcs(m);
 742 
 743   // Scan the byte code linearly from the start to the end
 744   bool no_control_flow = false; // Set to true when there is no direct control
 745                                 // flow from current instruction to the next
 746                                 // instruction in sequence
 747 
 748   Bytecodes::Code opcode;
 749   while (!bcs.is_last_bytecode()) {
 750     // Check for recursive re-verification before each bytecode.
 751     if (was_recursively_verified())  return;
 752 
 753     opcode = bcs.raw_next();
 754     u2 bci = bcs.bci();
 755 
 756     // Set current frame's offset to bci
 757     current_frame.set_offset(bci);
 758     current_frame.set_mark();
 759 
 760     // Make sure every offset in stackmap table point to the beginning to
 761     // an instruction. Match current_frame to stackmap_table entry with
 762     // the same offset if exists.
 763     stackmap_index = verify_stackmap_table(
 764       stackmap_index, bci, &current_frame, &stackmap_table,
 765       no_control_flow, CHECK_VERIFY(this));
 766 
 767 
 768     bool this_uninit = false;  // Set to true when invokespecial <init> initialized 'this'
 769     bool verified_exc_handlers = false;
 770 
 771     // Merge with the next instruction
 772     {
 773       u2 index;
 774       int target;
 775       VerificationType type, type2;
 776       VerificationType atype;
 777 
 778       LogTarget(Info, verification) lt;
 779       if (lt.is_enabled()) {
 780         ResourceMark rm(THREAD);
 781         LogStream ls(lt);
 782         current_frame.print_on(&ls);
 783         lt.print("offset = %d,  opcode = %s", bci,
 784                  opcode == Bytecodes::_illegal ? "illegal" : Bytecodes::name(opcode));
 785       }
 786 
 787       // Make sure wide instruction is in correct format
 788       if (bcs.is_wide()) {
 789         if (opcode != Bytecodes::_iinc   && opcode != Bytecodes::_iload  &&
 790             opcode != Bytecodes::_aload  && opcode != Bytecodes::_lload  &&
 791             opcode != Bytecodes::_istore && opcode != Bytecodes::_astore &&
 792             opcode != Bytecodes::_lstore && opcode != Bytecodes::_fload  &&
 793             opcode != Bytecodes::_dload  && opcode != Bytecodes::_fstore &&
 794             opcode != Bytecodes::_dstore) {
 795           /* Unreachable?  RawBytecodeStream's raw_next() returns 'illegal'
 796            * if we encounter a wide instruction that modifies an invalid
 797            * opcode (not one of the ones listed above) */
 798           verify_error(ErrorContext::bad_code(bci), "Bad wide instruction");
 799           return;
 800         }
 801       }
 802 
 803       // Look for possible jump target in exception handlers and see if it
 804       // matches current_frame.  Do this check here for astore*, dstore*,
 805       // fstore*, istore*, and lstore* opcodes because they can change the type
 806       // state by adding a local.  JVM Spec says that the incoming type state
 807       // should be used for this check.  So, do the check here before a possible
 808       // local is added to the type state.
 809       if (Bytecodes::is_store_into_local(opcode) && bci >= ex_min && bci < ex_max) {
 810         if (was_recursively_verified()) return;
 811         verify_exception_handler_targets(
 812           bci, this_uninit, &current_frame, &stackmap_table, CHECK_VERIFY(this));
 813         verified_exc_handlers = true;
 814       }
 815 
 816       if (was_recursively_verified()) return;
 817 
 818       switch (opcode) {
 819         case Bytecodes::_nop :
 820           no_control_flow = false; break;
 821         case Bytecodes::_aconst_null :
 822           current_frame.push_stack(
 823             VerificationType::null_type(), CHECK_VERIFY(this));
 824           no_control_flow = false; break;
 825         case Bytecodes::_iconst_m1 :
 826         case Bytecodes::_iconst_0 :
 827         case Bytecodes::_iconst_1 :
 828         case Bytecodes::_iconst_2 :
 829         case Bytecodes::_iconst_3 :
 830         case Bytecodes::_iconst_4 :
 831         case Bytecodes::_iconst_5 :
 832           current_frame.push_stack(
 833             VerificationType::integer_type(), CHECK_VERIFY(this));
 834           no_control_flow = false; break;
 835         case Bytecodes::_lconst_0 :
 836         case Bytecodes::_lconst_1 :
 837           current_frame.push_stack_2(
 838             VerificationType::long_type(),
 839             VerificationType::long2_type(), CHECK_VERIFY(this));
 840           no_control_flow = false; break;
 841         case Bytecodes::_fconst_0 :
 842         case Bytecodes::_fconst_1 :
 843         case Bytecodes::_fconst_2 :
 844           current_frame.push_stack(
 845             VerificationType::float_type(), CHECK_VERIFY(this));
 846           no_control_flow = false; break;
 847         case Bytecodes::_dconst_0 :
 848         case Bytecodes::_dconst_1 :
 849           current_frame.push_stack_2(
 850             VerificationType::double_type(),
 851             VerificationType::double2_type(), CHECK_VERIFY(this));
 852           no_control_flow = false; break;
 853         case Bytecodes::_sipush :
 854         case Bytecodes::_bipush :
 855           current_frame.push_stack(
 856             VerificationType::integer_type(), CHECK_VERIFY(this));
 857           no_control_flow = false; break;
 858         case Bytecodes::_ldc :
 859           verify_ldc(
 860             opcode, bcs.get_index_u1(), &current_frame,
 861             cp, bci, CHECK_VERIFY(this));
 862           no_control_flow = false; break;
 863         case Bytecodes::_ldc_w :
 864         case Bytecodes::_ldc2_w :
 865           verify_ldc(
 866             opcode, bcs.get_index_u2(), &current_frame,
 867             cp, bci, CHECK_VERIFY(this));
 868           no_control_flow = false; break;
 869         case Bytecodes::_iload :
 870           verify_iload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
 871           no_control_flow = false; break;
 872         case Bytecodes::_iload_0 :
 873         case Bytecodes::_iload_1 :
 874         case Bytecodes::_iload_2 :
 875         case Bytecodes::_iload_3 :
 876           index = opcode - Bytecodes::_iload_0;
 877           verify_iload(index, &current_frame, CHECK_VERIFY(this));
 878           no_control_flow = false; break;
 879         case Bytecodes::_lload :
 880           verify_lload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
 881           no_control_flow = false; break;
 882         case Bytecodes::_lload_0 :
 883         case Bytecodes::_lload_1 :
 884         case Bytecodes::_lload_2 :
 885         case Bytecodes::_lload_3 :
 886           index = opcode - Bytecodes::_lload_0;
 887           verify_lload(index, &current_frame, CHECK_VERIFY(this));
 888           no_control_flow = false; break;
 889         case Bytecodes::_fload :
 890           verify_fload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
 891           no_control_flow = false; break;
 892         case Bytecodes::_fload_0 :
 893         case Bytecodes::_fload_1 :
 894         case Bytecodes::_fload_2 :
 895         case Bytecodes::_fload_3 :
 896           index = opcode - Bytecodes::_fload_0;
 897           verify_fload(index, &current_frame, CHECK_VERIFY(this));
 898           no_control_flow = false; break;
 899         case Bytecodes::_dload :
 900           verify_dload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
 901           no_control_flow = false; break;
 902         case Bytecodes::_dload_0 :
 903         case Bytecodes::_dload_1 :
 904         case Bytecodes::_dload_2 :
 905         case Bytecodes::_dload_3 :
 906           index = opcode - Bytecodes::_dload_0;
 907           verify_dload(index, &current_frame, CHECK_VERIFY(this));
 908           no_control_flow = false; break;
 909         case Bytecodes::_aload :
 910           verify_aload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
 911           no_control_flow = false; break;
 912         case Bytecodes::_aload_0 :
 913         case Bytecodes::_aload_1 :
 914         case Bytecodes::_aload_2 :
 915         case Bytecodes::_aload_3 :
 916           index = opcode - Bytecodes::_aload_0;
 917           verify_aload(index, &current_frame, CHECK_VERIFY(this));
 918           no_control_flow = false; break;
 919         case Bytecodes::_iaload :
 920           type = current_frame.pop_stack(
 921             VerificationType::integer_type(), CHECK_VERIFY(this));
 922           atype = current_frame.pop_stack(
 923             VerificationType::reference_check(), CHECK_VERIFY(this));
 924           if (!atype.is_int_array()) {
 925             verify_error(ErrorContext::bad_type(bci,
 926                 current_frame.stack_top_ctx(), ref_ctx("[I", THREAD)),
 927                 bad_type_msg, "iaload");
 928             return;
 929           }
 930           current_frame.push_stack(
 931             VerificationType::integer_type(), CHECK_VERIFY(this));
 932           no_control_flow = false; break;
 933         case Bytecodes::_baload :
 934           type = current_frame.pop_stack(
 935             VerificationType::integer_type(), CHECK_VERIFY(this));
 936           atype = current_frame.pop_stack(
 937             VerificationType::reference_check(), CHECK_VERIFY(this));
 938           if (!atype.is_bool_array() && !atype.is_byte_array()) {
 939             verify_error(
 940                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
 941                 bad_type_msg, "baload");
 942             return;
 943           }
 944           current_frame.push_stack(
 945             VerificationType::integer_type(), CHECK_VERIFY(this));
 946           no_control_flow = false; break;
 947         case Bytecodes::_caload :
 948           type = current_frame.pop_stack(
 949             VerificationType::integer_type(), CHECK_VERIFY(this));
 950           atype = current_frame.pop_stack(
 951             VerificationType::reference_check(), CHECK_VERIFY(this));
 952           if (!atype.is_char_array()) {
 953             verify_error(ErrorContext::bad_type(bci,
 954                 current_frame.stack_top_ctx(), ref_ctx("[C", THREAD)),
 955                 bad_type_msg, "caload");
 956             return;
 957           }
 958           current_frame.push_stack(
 959             VerificationType::integer_type(), CHECK_VERIFY(this));
 960           no_control_flow = false; break;
 961         case Bytecodes::_saload :
 962           type = current_frame.pop_stack(
 963             VerificationType::integer_type(), CHECK_VERIFY(this));
 964           atype = current_frame.pop_stack(
 965             VerificationType::reference_check(), CHECK_VERIFY(this));
 966           if (!atype.is_short_array()) {
 967             verify_error(ErrorContext::bad_type(bci,
 968                 current_frame.stack_top_ctx(), ref_ctx("[S", THREAD)),
 969                 bad_type_msg, "saload");
 970             return;
 971           }
 972           current_frame.push_stack(
 973             VerificationType::integer_type(), CHECK_VERIFY(this));
 974           no_control_flow = false; break;
 975         case Bytecodes::_laload :
 976           type = current_frame.pop_stack(
 977             VerificationType::integer_type(), CHECK_VERIFY(this));
 978           atype = current_frame.pop_stack(
 979             VerificationType::reference_check(), CHECK_VERIFY(this));
 980           if (!atype.is_long_array()) {
 981             verify_error(ErrorContext::bad_type(bci,
 982                 current_frame.stack_top_ctx(), ref_ctx("[J", THREAD)),
 983                 bad_type_msg, "laload");
 984             return;
 985           }
 986           current_frame.push_stack_2(
 987             VerificationType::long_type(),
 988             VerificationType::long2_type(), CHECK_VERIFY(this));
 989           no_control_flow = false; break;
 990         case Bytecodes::_faload :
 991           type = current_frame.pop_stack(
 992             VerificationType::integer_type(), CHECK_VERIFY(this));
 993           atype = current_frame.pop_stack(
 994             VerificationType::reference_check(), CHECK_VERIFY(this));
 995           if (!atype.is_float_array()) {
 996             verify_error(ErrorContext::bad_type(bci,
 997                 current_frame.stack_top_ctx(), ref_ctx("[F", THREAD)),
 998                 bad_type_msg, "faload");
 999             return;
1000           }
1001           current_frame.push_stack(
1002             VerificationType::float_type(), CHECK_VERIFY(this));
1003           no_control_flow = false; break;
1004         case Bytecodes::_daload :
1005           type = current_frame.pop_stack(
1006             VerificationType::integer_type(), CHECK_VERIFY(this));
1007           atype = current_frame.pop_stack(
1008             VerificationType::reference_check(), CHECK_VERIFY(this));
1009           if (!atype.is_double_array()) {
1010             verify_error(ErrorContext::bad_type(bci,
1011                 current_frame.stack_top_ctx(), ref_ctx("[D", THREAD)),
1012                 bad_type_msg, "daload");
1013             return;
1014           }
1015           current_frame.push_stack_2(
1016             VerificationType::double_type(),
1017             VerificationType::double2_type(), CHECK_VERIFY(this));
1018           no_control_flow = false; break;
1019         case Bytecodes::_aaload : {
1020           type = current_frame.pop_stack(
1021             VerificationType::integer_type(), CHECK_VERIFY(this));
1022           atype = current_frame.pop_stack(
1023             VerificationType::reference_check(), CHECK_VERIFY(this));
1024           if (!atype.is_reference_array()) {
1025             verify_error(ErrorContext::bad_type(bci,
1026                 current_frame.stack_top_ctx(),
1027                 TypeOrigin::implicit(VerificationType::reference_check())),
1028                 bad_type_msg, "aaload");
1029             return;
1030           }
1031           if (atype.is_null()) {
1032             current_frame.push_stack(
1033               VerificationType::null_type(), CHECK_VERIFY(this));
1034           } else {
1035             VerificationType component =
1036               atype.get_component(this, CHECK_VERIFY(this));
1037             current_frame.push_stack(component, CHECK_VERIFY(this));
1038           }
1039           no_control_flow = false; break;
1040         }
1041         case Bytecodes::_istore :
1042           verify_istore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
1043           no_control_flow = false; break;
1044         case Bytecodes::_istore_0 :
1045         case Bytecodes::_istore_1 :
1046         case Bytecodes::_istore_2 :
1047         case Bytecodes::_istore_3 :
1048           index = opcode - Bytecodes::_istore_0;
1049           verify_istore(index, &current_frame, CHECK_VERIFY(this));
1050           no_control_flow = false; break;
1051         case Bytecodes::_lstore :
1052           verify_lstore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
1053           no_control_flow = false; break;
1054         case Bytecodes::_lstore_0 :
1055         case Bytecodes::_lstore_1 :
1056         case Bytecodes::_lstore_2 :
1057         case Bytecodes::_lstore_3 :
1058           index = opcode - Bytecodes::_lstore_0;
1059           verify_lstore(index, &current_frame, CHECK_VERIFY(this));
1060           no_control_flow = false; break;
1061         case Bytecodes::_fstore :
1062           verify_fstore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
1063           no_control_flow = false; break;
1064         case Bytecodes::_fstore_0 :
1065         case Bytecodes::_fstore_1 :
1066         case Bytecodes::_fstore_2 :
1067         case Bytecodes::_fstore_3 :
1068           index = opcode - Bytecodes::_fstore_0;
1069           verify_fstore(index, &current_frame, CHECK_VERIFY(this));
1070           no_control_flow = false; break;
1071         case Bytecodes::_dstore :
1072           verify_dstore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
1073           no_control_flow = false; break;
1074         case Bytecodes::_dstore_0 :
1075         case Bytecodes::_dstore_1 :
1076         case Bytecodes::_dstore_2 :
1077         case Bytecodes::_dstore_3 :
1078           index = opcode - Bytecodes::_dstore_0;
1079           verify_dstore(index, &current_frame, CHECK_VERIFY(this));
1080           no_control_flow = false; break;
1081         case Bytecodes::_astore :
1082           verify_astore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
1083           no_control_flow = false; break;
1084         case Bytecodes::_astore_0 :
1085         case Bytecodes::_astore_1 :
1086         case Bytecodes::_astore_2 :
1087         case Bytecodes::_astore_3 :
1088           index = opcode - Bytecodes::_astore_0;
1089           verify_astore(index, &current_frame, CHECK_VERIFY(this));
1090           no_control_flow = false; break;
1091         case Bytecodes::_iastore :
1092           type = current_frame.pop_stack(
1093             VerificationType::integer_type(), CHECK_VERIFY(this));
1094           type2 = current_frame.pop_stack(
1095             VerificationType::integer_type(), CHECK_VERIFY(this));
1096           atype = current_frame.pop_stack(
1097             VerificationType::reference_check(), CHECK_VERIFY(this));
1098           if (!atype.is_int_array()) {
1099             verify_error(ErrorContext::bad_type(bci,
1100                 current_frame.stack_top_ctx(), ref_ctx("[I", THREAD)),
1101                 bad_type_msg, "iastore");
1102             return;
1103           }
1104           no_control_flow = false; break;
1105         case Bytecodes::_bastore :
1106           type = current_frame.pop_stack(
1107             VerificationType::integer_type(), CHECK_VERIFY(this));
1108           type2 = current_frame.pop_stack(
1109             VerificationType::integer_type(), CHECK_VERIFY(this));
1110           atype = current_frame.pop_stack(
1111             VerificationType::reference_check(), CHECK_VERIFY(this));
1112           if (!atype.is_bool_array() && !atype.is_byte_array()) {
1113             verify_error(
1114                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1115                 bad_type_msg, "bastore");
1116             return;
1117           }
1118           no_control_flow = false; break;
1119         case Bytecodes::_castore :
1120           current_frame.pop_stack(
1121             VerificationType::integer_type(), CHECK_VERIFY(this));
1122           current_frame.pop_stack(
1123             VerificationType::integer_type(), CHECK_VERIFY(this));
1124           atype = current_frame.pop_stack(
1125             VerificationType::reference_check(), CHECK_VERIFY(this));
1126           if (!atype.is_char_array()) {
1127             verify_error(ErrorContext::bad_type(bci,
1128                 current_frame.stack_top_ctx(), ref_ctx("[C", THREAD)),
1129                 bad_type_msg, "castore");
1130             return;
1131           }
1132           no_control_flow = false; break;
1133         case Bytecodes::_sastore :
1134           current_frame.pop_stack(
1135             VerificationType::integer_type(), CHECK_VERIFY(this));
1136           current_frame.pop_stack(
1137             VerificationType::integer_type(), CHECK_VERIFY(this));
1138           atype = current_frame.pop_stack(
1139             VerificationType::reference_check(), CHECK_VERIFY(this));
1140           if (!atype.is_short_array()) {
1141             verify_error(ErrorContext::bad_type(bci,
1142                 current_frame.stack_top_ctx(), ref_ctx("[S", THREAD)),
1143                 bad_type_msg, "sastore");
1144             return;
1145           }
1146           no_control_flow = false; break;
1147         case Bytecodes::_lastore :
1148           current_frame.pop_stack_2(
1149             VerificationType::long2_type(),
1150             VerificationType::long_type(), CHECK_VERIFY(this));
1151           current_frame.pop_stack(
1152             VerificationType::integer_type(), CHECK_VERIFY(this));
1153           atype = current_frame.pop_stack(
1154             VerificationType::reference_check(), CHECK_VERIFY(this));
1155           if (!atype.is_long_array()) {
1156             verify_error(ErrorContext::bad_type(bci,
1157                 current_frame.stack_top_ctx(), ref_ctx("[J", THREAD)),
1158                 bad_type_msg, "lastore");
1159             return;
1160           }
1161           no_control_flow = false; break;
1162         case Bytecodes::_fastore :
1163           current_frame.pop_stack(
1164             VerificationType::float_type(), CHECK_VERIFY(this));
1165           current_frame.pop_stack
1166             (VerificationType::integer_type(), CHECK_VERIFY(this));
1167           atype = current_frame.pop_stack(
1168             VerificationType::reference_check(), CHECK_VERIFY(this));
1169           if (!atype.is_float_array()) {
1170             verify_error(ErrorContext::bad_type(bci,
1171                 current_frame.stack_top_ctx(), ref_ctx("[F", THREAD)),
1172                 bad_type_msg, "fastore");
1173             return;
1174           }
1175           no_control_flow = false; break;
1176         case Bytecodes::_dastore :
1177           current_frame.pop_stack_2(
1178             VerificationType::double2_type(),
1179             VerificationType::double_type(), CHECK_VERIFY(this));
1180           current_frame.pop_stack(
1181             VerificationType::integer_type(), CHECK_VERIFY(this));
1182           atype = current_frame.pop_stack(
1183             VerificationType::reference_check(), CHECK_VERIFY(this));
1184           if (!atype.is_double_array()) {
1185             verify_error(ErrorContext::bad_type(bci,
1186                 current_frame.stack_top_ctx(), ref_ctx("[D", THREAD)),
1187                 bad_type_msg, "dastore");
1188             return;
1189           }
1190           no_control_flow = false; break;
1191         case Bytecodes::_aastore :
1192           type = current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1193           type2 = current_frame.pop_stack(
1194             VerificationType::integer_type(), CHECK_VERIFY(this));
1195           atype = current_frame.pop_stack(
1196             VerificationType::reference_check(), CHECK_VERIFY(this));
1197           // more type-checking is done at runtime
1198           if (!atype.is_reference_array()) {
1199             verify_error(ErrorContext::bad_type(bci,
1200                 current_frame.stack_top_ctx(),
1201                 TypeOrigin::implicit(VerificationType::reference_check())),
1202                 bad_type_msg, "aastore");
1203             return;
1204           }
1205           // 4938384: relaxed constraint in JVMS 3nd edition.
1206           no_control_flow = false; break;
1207         case Bytecodes::_pop :
1208           current_frame.pop_stack(
1209             VerificationType::category1_check(), CHECK_VERIFY(this));
1210           no_control_flow = false; break;
1211         case Bytecodes::_pop2 :
1212           type = current_frame.pop_stack(CHECK_VERIFY(this));
1213           if (type.is_category1()) {
1214             current_frame.pop_stack(
1215               VerificationType::category1_check(), CHECK_VERIFY(this));
1216           } else if (type.is_category2_2nd()) {
1217             current_frame.pop_stack(
1218               VerificationType::category2_check(), CHECK_VERIFY(this));
1219           } else {
1220             /* Unreachable? Would need a category2_1st on TOS
1221              * which does not appear possible. */
1222             verify_error(
1223                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1224                 bad_type_msg, "pop2");
1225             return;
1226           }
1227           no_control_flow = false; break;
1228         case Bytecodes::_dup :
1229           type = current_frame.pop_stack(
1230             VerificationType::category1_check(), CHECK_VERIFY(this));
1231           current_frame.push_stack(type, CHECK_VERIFY(this));
1232           current_frame.push_stack(type, CHECK_VERIFY(this));
1233           no_control_flow = false; break;
1234         case Bytecodes::_dup_x1 :
1235           type = current_frame.pop_stack(
1236             VerificationType::category1_check(), CHECK_VERIFY(this));
1237           type2 = current_frame.pop_stack(
1238             VerificationType::category1_check(), CHECK_VERIFY(this));
1239           current_frame.push_stack(type, CHECK_VERIFY(this));
1240           current_frame.push_stack(type2, CHECK_VERIFY(this));
1241           current_frame.push_stack(type, CHECK_VERIFY(this));
1242           no_control_flow = false; break;
1243         case Bytecodes::_dup_x2 :
1244         {
1245           VerificationType type3;
1246           type = current_frame.pop_stack(
1247             VerificationType::category1_check(), CHECK_VERIFY(this));
1248           type2 = current_frame.pop_stack(CHECK_VERIFY(this));
1249           if (type2.is_category1()) {
1250             type3 = current_frame.pop_stack(
1251               VerificationType::category1_check(), CHECK_VERIFY(this));
1252           } else if (type2.is_category2_2nd()) {
1253             type3 = current_frame.pop_stack(
1254               VerificationType::category2_check(), CHECK_VERIFY(this));
1255           } else {
1256             /* Unreachable? Would need a category2_1st at stack depth 2 with
1257              * a category1 on TOS which does not appear possible. */
1258             verify_error(ErrorContext::bad_type(
1259                 bci, current_frame.stack_top_ctx()), bad_type_msg, "dup_x2");
1260             return;
1261           }
1262           current_frame.push_stack(type, CHECK_VERIFY(this));
1263           current_frame.push_stack(type3, CHECK_VERIFY(this));
1264           current_frame.push_stack(type2, CHECK_VERIFY(this));
1265           current_frame.push_stack(type, CHECK_VERIFY(this));
1266           no_control_flow = false; break;
1267         }
1268         case Bytecodes::_dup2 :
1269           type = current_frame.pop_stack(CHECK_VERIFY(this));
1270           if (type.is_category1()) {
1271             type2 = current_frame.pop_stack(
1272               VerificationType::category1_check(), CHECK_VERIFY(this));
1273           } else if (type.is_category2_2nd()) {
1274             type2 = current_frame.pop_stack(
1275               VerificationType::category2_check(), CHECK_VERIFY(this));
1276           } else {
1277             /* Unreachable?  Would need a category2_1st on TOS which does not
1278              * appear possible. */
1279             verify_error(
1280                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1281                 bad_type_msg, "dup2");
1282             return;
1283           }
1284           current_frame.push_stack(type2, CHECK_VERIFY(this));
1285           current_frame.push_stack(type, CHECK_VERIFY(this));
1286           current_frame.push_stack(type2, CHECK_VERIFY(this));
1287           current_frame.push_stack(type, CHECK_VERIFY(this));
1288           no_control_flow = false; break;
1289         case Bytecodes::_dup2_x1 :
1290         {
1291           VerificationType type3;
1292           type = current_frame.pop_stack(CHECK_VERIFY(this));
1293           if (type.is_category1()) {
1294             type2 = current_frame.pop_stack(
1295               VerificationType::category1_check(), CHECK_VERIFY(this));
1296           } else if (type.is_category2_2nd()) {
1297             type2 = current_frame.pop_stack(
1298               VerificationType::category2_check(), CHECK_VERIFY(this));
1299           } else {
1300             /* Unreachable?  Would need a category2_1st on TOS which does
1301              * not appear possible. */
1302             verify_error(
1303                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1304                 bad_type_msg, "dup2_x1");
1305             return;
1306           }
1307           type3 = current_frame.pop_stack(
1308             VerificationType::category1_check(), CHECK_VERIFY(this));
1309           current_frame.push_stack(type2, CHECK_VERIFY(this));
1310           current_frame.push_stack(type, CHECK_VERIFY(this));
1311           current_frame.push_stack(type3, CHECK_VERIFY(this));
1312           current_frame.push_stack(type2, CHECK_VERIFY(this));
1313           current_frame.push_stack(type, CHECK_VERIFY(this));
1314           no_control_flow = false; break;
1315         }
1316         case Bytecodes::_dup2_x2 :
1317         {
1318           VerificationType type3, type4;
1319           type = current_frame.pop_stack(CHECK_VERIFY(this));
1320           if (type.is_category1()) {
1321             type2 = current_frame.pop_stack(
1322               VerificationType::category1_check(), CHECK_VERIFY(this));
1323           } else if (type.is_category2_2nd()) {
1324             type2 = current_frame.pop_stack(
1325               VerificationType::category2_check(), CHECK_VERIFY(this));
1326           } else {
1327             /* Unreachable?  Would need a category2_1st on TOS which does
1328              * not appear possible. */
1329             verify_error(
1330                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1331                 bad_type_msg, "dup2_x2");
1332             return;
1333           }
1334           type3 = current_frame.pop_stack(CHECK_VERIFY(this));
1335           if (type3.is_category1()) {
1336             type4 = current_frame.pop_stack(
1337               VerificationType::category1_check(), CHECK_VERIFY(this));
1338           } else if (type3.is_category2_2nd()) {
1339             type4 = current_frame.pop_stack(
1340               VerificationType::category2_check(), CHECK_VERIFY(this));
1341           } else {
1342             /* Unreachable?  Would need a category2_1st on TOS after popping
1343              * a long/double or two category 1's, which does not
1344              * appear possible. */
1345             verify_error(
1346                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1347                 bad_type_msg, "dup2_x2");
1348             return;
1349           }
1350           current_frame.push_stack(type2, CHECK_VERIFY(this));
1351           current_frame.push_stack(type, CHECK_VERIFY(this));
1352           current_frame.push_stack(type4, CHECK_VERIFY(this));
1353           current_frame.push_stack(type3, CHECK_VERIFY(this));
1354           current_frame.push_stack(type2, CHECK_VERIFY(this));
1355           current_frame.push_stack(type, CHECK_VERIFY(this));
1356           no_control_flow = false; break;
1357         }
1358         case Bytecodes::_swap :
1359           type = current_frame.pop_stack(
1360             VerificationType::category1_check(), CHECK_VERIFY(this));
1361           type2 = current_frame.pop_stack(
1362             VerificationType::category1_check(), CHECK_VERIFY(this));
1363           current_frame.push_stack(type, CHECK_VERIFY(this));
1364           current_frame.push_stack(type2, CHECK_VERIFY(this));
1365           no_control_flow = false; break;
1366         case Bytecodes::_iadd :
1367         case Bytecodes::_isub :
1368         case Bytecodes::_imul :
1369         case Bytecodes::_idiv :
1370         case Bytecodes::_irem :
1371         case Bytecodes::_ishl :
1372         case Bytecodes::_ishr :
1373         case Bytecodes::_iushr :
1374         case Bytecodes::_ior :
1375         case Bytecodes::_ixor :
1376         case Bytecodes::_iand :
1377           current_frame.pop_stack(
1378             VerificationType::integer_type(), CHECK_VERIFY(this));
1379           // fall through
1380         case Bytecodes::_ineg :
1381           current_frame.pop_stack(
1382             VerificationType::integer_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::_ladd :
1387         case Bytecodes::_lsub :
1388         case Bytecodes::_lmul :
1389         case Bytecodes::_ldiv :
1390         case Bytecodes::_lrem :
1391         case Bytecodes::_land :
1392         case Bytecodes::_lor :
1393         case Bytecodes::_lxor :
1394           current_frame.pop_stack_2(
1395             VerificationType::long2_type(),
1396             VerificationType::long_type(), CHECK_VERIFY(this));
1397           // fall through
1398         case Bytecodes::_lneg :
1399           current_frame.pop_stack_2(
1400             VerificationType::long2_type(),
1401             VerificationType::long_type(), CHECK_VERIFY(this));
1402           current_frame.push_stack_2(
1403             VerificationType::long_type(),
1404             VerificationType::long2_type(), CHECK_VERIFY(this));
1405           no_control_flow = false; break;
1406         case Bytecodes::_lshl :
1407         case Bytecodes::_lshr :
1408         case Bytecodes::_lushr :
1409           current_frame.pop_stack(
1410             VerificationType::integer_type(), CHECK_VERIFY(this));
1411           current_frame.pop_stack_2(
1412             VerificationType::long2_type(),
1413             VerificationType::long_type(), CHECK_VERIFY(this));
1414           current_frame.push_stack_2(
1415             VerificationType::long_type(),
1416             VerificationType::long2_type(), CHECK_VERIFY(this));
1417           no_control_flow = false; break;
1418         case Bytecodes::_fadd :
1419         case Bytecodes::_fsub :
1420         case Bytecodes::_fmul :
1421         case Bytecodes::_fdiv :
1422         case Bytecodes::_frem :
1423           current_frame.pop_stack(
1424             VerificationType::float_type(), CHECK_VERIFY(this));
1425           // fall through
1426         case Bytecodes::_fneg :
1427           current_frame.pop_stack(
1428             VerificationType::float_type(), CHECK_VERIFY(this));
1429           current_frame.push_stack(
1430             VerificationType::float_type(), CHECK_VERIFY(this));
1431           no_control_flow = false; break;
1432         case Bytecodes::_dadd :
1433         case Bytecodes::_dsub :
1434         case Bytecodes::_dmul :
1435         case Bytecodes::_ddiv :
1436         case Bytecodes::_drem :
1437           current_frame.pop_stack_2(
1438             VerificationType::double2_type(),
1439             VerificationType::double_type(), CHECK_VERIFY(this));
1440           // fall through
1441         case Bytecodes::_dneg :
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::double_type(),
1447             VerificationType::double2_type(), CHECK_VERIFY(this));
1448           no_control_flow = false; break;
1449         case Bytecodes::_iinc :
1450           verify_iinc(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
1451           no_control_flow = false; break;
1452         case Bytecodes::_i2l :
1453           type = current_frame.pop_stack(
1454             VerificationType::integer_type(), CHECK_VERIFY(this));
1455           current_frame.push_stack_2(
1456             VerificationType::long_type(),
1457             VerificationType::long2_type(), CHECK_VERIFY(this));
1458           no_control_flow = false; break;
1459        case Bytecodes::_l2i :
1460           current_frame.pop_stack_2(
1461             VerificationType::long2_type(),
1462             VerificationType::long_type(), CHECK_VERIFY(this));
1463           current_frame.push_stack(
1464             VerificationType::integer_type(), CHECK_VERIFY(this));
1465           no_control_flow = false; break;
1466         case Bytecodes::_i2f :
1467           current_frame.pop_stack(
1468             VerificationType::integer_type(), CHECK_VERIFY(this));
1469           current_frame.push_stack(
1470             VerificationType::float_type(), CHECK_VERIFY(this));
1471           no_control_flow = false; break;
1472         case Bytecodes::_i2d :
1473           current_frame.pop_stack(
1474             VerificationType::integer_type(), CHECK_VERIFY(this));
1475           current_frame.push_stack_2(
1476             VerificationType::double_type(),
1477             VerificationType::double2_type(), CHECK_VERIFY(this));
1478           no_control_flow = false; break;
1479         case Bytecodes::_l2f :
1480           current_frame.pop_stack_2(
1481             VerificationType::long2_type(),
1482             VerificationType::long_type(), CHECK_VERIFY(this));
1483           current_frame.push_stack(
1484             VerificationType::float_type(), CHECK_VERIFY(this));
1485           no_control_flow = false; break;
1486         case Bytecodes::_l2d :
1487           current_frame.pop_stack_2(
1488             VerificationType::long2_type(),
1489             VerificationType::long_type(), CHECK_VERIFY(this));
1490           current_frame.push_stack_2(
1491             VerificationType::double_type(),
1492             VerificationType::double2_type(), CHECK_VERIFY(this));
1493           no_control_flow = false; break;
1494         case Bytecodes::_f2i :
1495           current_frame.pop_stack(
1496             VerificationType::float_type(), CHECK_VERIFY(this));
1497           current_frame.push_stack(
1498             VerificationType::integer_type(), CHECK_VERIFY(this));
1499           no_control_flow = false; break;
1500         case Bytecodes::_f2l :
1501           current_frame.pop_stack(
1502             VerificationType::float_type(), CHECK_VERIFY(this));
1503           current_frame.push_stack_2(
1504             VerificationType::long_type(),
1505             VerificationType::long2_type(), CHECK_VERIFY(this));
1506           no_control_flow = false; break;
1507         case Bytecodes::_f2d :
1508           current_frame.pop_stack(
1509             VerificationType::float_type(), CHECK_VERIFY(this));
1510           current_frame.push_stack_2(
1511             VerificationType::double_type(),
1512             VerificationType::double2_type(), CHECK_VERIFY(this));
1513           no_control_flow = false; break;
1514         case Bytecodes::_d2i :
1515           current_frame.pop_stack_2(
1516             VerificationType::double2_type(),
1517             VerificationType::double_type(), CHECK_VERIFY(this));
1518           current_frame.push_stack(
1519             VerificationType::integer_type(), CHECK_VERIFY(this));
1520           no_control_flow = false; break;
1521         case Bytecodes::_d2l :
1522           current_frame.pop_stack_2(
1523             VerificationType::double2_type(),
1524             VerificationType::double_type(), CHECK_VERIFY(this));
1525           current_frame.push_stack_2(
1526             VerificationType::long_type(),
1527             VerificationType::long2_type(), CHECK_VERIFY(this));
1528           no_control_flow = false; break;
1529         case Bytecodes::_d2f :
1530           current_frame.pop_stack_2(
1531             VerificationType::double2_type(),
1532             VerificationType::double_type(), CHECK_VERIFY(this));
1533           current_frame.push_stack(
1534             VerificationType::float_type(), CHECK_VERIFY(this));
1535           no_control_flow = false; break;
1536         case Bytecodes::_i2b :
1537         case Bytecodes::_i2c :
1538         case Bytecodes::_i2s :
1539           current_frame.pop_stack(
1540             VerificationType::integer_type(), CHECK_VERIFY(this));
1541           current_frame.push_stack(
1542             VerificationType::integer_type(), CHECK_VERIFY(this));
1543           no_control_flow = false; break;
1544         case Bytecodes::_lcmp :
1545           current_frame.pop_stack_2(
1546             VerificationType::long2_type(),
1547             VerificationType::long_type(), CHECK_VERIFY(this));
1548           current_frame.pop_stack_2(
1549             VerificationType::long2_type(),
1550             VerificationType::long_type(), CHECK_VERIFY(this));
1551           current_frame.push_stack(
1552             VerificationType::integer_type(), CHECK_VERIFY(this));
1553           no_control_flow = false; break;
1554         case Bytecodes::_fcmpl :
1555         case Bytecodes::_fcmpg :
1556           current_frame.pop_stack(
1557             VerificationType::float_type(), CHECK_VERIFY(this));
1558           current_frame.pop_stack(
1559             VerificationType::float_type(), CHECK_VERIFY(this));
1560           current_frame.push_stack(
1561             VerificationType::integer_type(), CHECK_VERIFY(this));
1562           no_control_flow = false; break;
1563         case Bytecodes::_dcmpl :
1564         case Bytecodes::_dcmpg :
1565           current_frame.pop_stack_2(
1566             VerificationType::double2_type(),
1567             VerificationType::double_type(), CHECK_VERIFY(this));
1568           current_frame.pop_stack_2(
1569             VerificationType::double2_type(),
1570             VerificationType::double_type(), CHECK_VERIFY(this));
1571           current_frame.push_stack(
1572             VerificationType::integer_type(), CHECK_VERIFY(this));
1573           no_control_flow = false; break;
1574         case Bytecodes::_if_icmpeq:
1575         case Bytecodes::_if_icmpne:
1576         case Bytecodes::_if_icmplt:
1577         case Bytecodes::_if_icmpge:
1578         case Bytecodes::_if_icmpgt:
1579         case Bytecodes::_if_icmple:
1580           current_frame.pop_stack(
1581             VerificationType::integer_type(), CHECK_VERIFY(this));
1582           // fall through
1583         case Bytecodes::_ifeq:
1584         case Bytecodes::_ifne:
1585         case Bytecodes::_iflt:
1586         case Bytecodes::_ifge:
1587         case Bytecodes::_ifgt:
1588         case Bytecodes::_ifle:
1589           current_frame.pop_stack(
1590             VerificationType::integer_type(), CHECK_VERIFY(this));
1591           target = bcs.dest();
1592           stackmap_table.check_jump_target(
1593             &current_frame, target, CHECK_VERIFY(this));
1594           no_control_flow = false; break;
1595         case Bytecodes::_if_acmpeq :
1596         case Bytecodes::_if_acmpne :
1597           current_frame.pop_stack(
1598             VerificationType::reference_check(), CHECK_VERIFY(this));
1599           // fall through
1600         case Bytecodes::_ifnull :
1601         case Bytecodes::_ifnonnull :
1602           current_frame.pop_stack(
1603             VerificationType::reference_check(), CHECK_VERIFY(this));
1604           target = bcs.dest();
1605           stackmap_table.check_jump_target
1606             (&current_frame, target, CHECK_VERIFY(this));
1607           no_control_flow = false; break;
1608         case Bytecodes::_goto :
1609           target = bcs.dest();
1610           stackmap_table.check_jump_target(
1611             &current_frame, target, CHECK_VERIFY(this));
1612           no_control_flow = true; break;
1613         case Bytecodes::_goto_w :
1614           target = bcs.dest_w();
1615           stackmap_table.check_jump_target(
1616             &current_frame, target, CHECK_VERIFY(this));
1617           no_control_flow = true; break;
1618         case Bytecodes::_tableswitch :
1619         case Bytecodes::_lookupswitch :
1620           verify_switch(
1621             &bcs, code_length, code_data, &current_frame,
1622             &stackmap_table, CHECK_VERIFY(this));
1623           no_control_flow = true; break;
1624         case Bytecodes::_ireturn :
1625           type = current_frame.pop_stack(
1626             VerificationType::integer_type(), CHECK_VERIFY(this));
1627           verify_return_value(return_type, type, bci,
1628                               &current_frame, CHECK_VERIFY(this));
1629           no_control_flow = true; break;
1630         case Bytecodes::_lreturn :
1631           type2 = current_frame.pop_stack(
1632             VerificationType::long2_type(), CHECK_VERIFY(this));
1633           type = current_frame.pop_stack(
1634             VerificationType::long_type(), CHECK_VERIFY(this));
1635           verify_return_value(return_type, type, bci,
1636                               &current_frame, CHECK_VERIFY(this));
1637           no_control_flow = true; break;
1638         case Bytecodes::_freturn :
1639           type = current_frame.pop_stack(
1640             VerificationType::float_type(), CHECK_VERIFY(this));
1641           verify_return_value(return_type, type, bci,
1642                               &current_frame, CHECK_VERIFY(this));
1643           no_control_flow = true; break;
1644         case Bytecodes::_dreturn :
1645           type2 = current_frame.pop_stack(
1646             VerificationType::double2_type(),  CHECK_VERIFY(this));
1647           type = current_frame.pop_stack(
1648             VerificationType::double_type(), CHECK_VERIFY(this));
1649           verify_return_value(return_type, type, bci,
1650                               &current_frame, CHECK_VERIFY(this));
1651           no_control_flow = true; break;
1652         case Bytecodes::_areturn :
1653           type = current_frame.pop_stack(
1654             VerificationType::reference_check(), CHECK_VERIFY(this));
1655           verify_return_value(return_type, type, bci,
1656                               &current_frame, CHECK_VERIFY(this));
1657           no_control_flow = true; break;
1658         case Bytecodes::_return :
1659           if (return_type != VerificationType::bogus_type()) {
1660             verify_error(ErrorContext::bad_code(bci),
1661                          "Method expects a return value");
1662             return;
1663           }
1664           // Make sure "this" has been initialized if current method is an
1665           // <init>.
1666           if (_method->name() == vmSymbols::object_initializer_name() &&
1667               current_frame.flag_this_uninit()) {
1668             verify_error(ErrorContext::bad_code(bci),
1669                          "Constructor must call super() or this() "
1670                          "before return");
1671             return;
1672           }
1673           no_control_flow = true; break;
1674         case Bytecodes::_getstatic :
1675         case Bytecodes::_putstatic :
1676           // pass TRUE, operand can be an array type for getstatic/putstatic.
1677           verify_field_instructions(
1678             &bcs, &current_frame, cp, true, CHECK_VERIFY(this));
1679           no_control_flow = false; break;
1680         case Bytecodes::_getfield :
1681         case Bytecodes::_putfield :
1682           // pass FALSE, operand can't be an array type for getfield/putfield.
1683           verify_field_instructions(
1684             &bcs, &current_frame, cp, false, CHECK_VERIFY(this));
1685           no_control_flow = false; break;
1686         case Bytecodes::_invokevirtual :
1687         case Bytecodes::_invokespecial :
1688         case Bytecodes::_invokestatic :
1689           verify_invoke_instructions(
1690             &bcs, code_length, &current_frame, (bci >= ex_min && bci < ex_max),
1691             &this_uninit, return_type, cp, &stackmap_table, CHECK_VERIFY(this));
1692           no_control_flow = false; break;
1693         case Bytecodes::_invokeinterface :
1694         case Bytecodes::_invokedynamic :
1695           verify_invoke_instructions(
1696             &bcs, code_length, &current_frame, (bci >= ex_min && bci < ex_max),
1697             &this_uninit, return_type, cp, &stackmap_table, CHECK_VERIFY(this));
1698           no_control_flow = false; break;
1699         case Bytecodes::_new :
1700         {
1701           index = bcs.get_index_u2();
1702           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1703           VerificationType new_class_type =
1704             cp_index_to_type(index, cp, CHECK_VERIFY(this));
1705           if (!new_class_type.is_object()) {
1706             verify_error(ErrorContext::bad_type(bci,
1707                 TypeOrigin::cp(index, new_class_type)),
1708                 "Illegal new instruction");
1709             return;
1710           }
1711           type = VerificationType::uninitialized_type(bci);
1712           current_frame.push_stack(type, CHECK_VERIFY(this));
1713           no_control_flow = false; break;
1714         }
1715         case Bytecodes::_newarray :
1716           type = get_newarray_type(bcs.get_index(), bci, CHECK_VERIFY(this));
1717           current_frame.pop_stack(
1718             VerificationType::integer_type(),  CHECK_VERIFY(this));
1719           current_frame.push_stack(type, CHECK_VERIFY(this));
1720           no_control_flow = false; break;
1721         case Bytecodes::_anewarray :
1722           verify_anewarray(
1723             bci, bcs.get_index_u2(), cp, &current_frame, CHECK_VERIFY(this));
1724           no_control_flow = false; break;
1725         case Bytecodes::_arraylength :
1726           type = current_frame.pop_stack(
1727             VerificationType::reference_check(), CHECK_VERIFY(this));
1728           if (!(type.is_null() || type.is_array())) {
1729             verify_error(ErrorContext::bad_type(
1730                 bci, current_frame.stack_top_ctx()),
1731                 bad_type_msg, "arraylength");
1732           }
1733           current_frame.push_stack(
1734             VerificationType::integer_type(), CHECK_VERIFY(this));
1735           no_control_flow = false; break;
1736         case Bytecodes::_checkcast :
1737         {
1738           index = bcs.get_index_u2();
1739           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1740           current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1741           VerificationType klass_type = cp_index_to_type(
1742             index, cp, CHECK_VERIFY(this));
1743           current_frame.push_stack(klass_type, CHECK_VERIFY(this));
1744           no_control_flow = false; break;
1745         }
1746         case Bytecodes::_instanceof : {
1747           index = bcs.get_index_u2();
1748           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1749           current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1750           current_frame.push_stack(
1751             VerificationType::integer_type(), CHECK_VERIFY(this));
1752           no_control_flow = false; break;
1753         }
1754         case Bytecodes::_monitorenter :
1755         case Bytecodes::_monitorexit :
1756           current_frame.pop_stack(
1757             VerificationType::reference_check(), CHECK_VERIFY(this));
1758           no_control_flow = false; break;
1759         case Bytecodes::_multianewarray :
1760         {
1761           index = bcs.get_index_u2();
1762           u2 dim = *(bcs.bcp()+3);
1763           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1764           VerificationType new_array_type =
1765             cp_index_to_type(index, cp, CHECK_VERIFY(this));
1766           if (!new_array_type.is_array()) {
1767             verify_error(ErrorContext::bad_type(bci,
1768                 TypeOrigin::cp(index, new_array_type)),
1769                 "Illegal constant pool index in multianewarray instruction");
1770             return;
1771           }
1772           if (dim < 1 || new_array_type.dimensions() < dim) {
1773             verify_error(ErrorContext::bad_code(bci),
1774                 "Illegal dimension in multianewarray instruction: %d", dim);
1775             return;
1776           }
1777           for (int i = 0; i < dim; i++) {
1778             current_frame.pop_stack(
1779               VerificationType::integer_type(), CHECK_VERIFY(this));
1780           }
1781           current_frame.push_stack(new_array_type, CHECK_VERIFY(this));
1782           no_control_flow = false; break;
1783         }
1784         case Bytecodes::_athrow :
1785           type = VerificationType::reference_type(
1786             vmSymbols::java_lang_Throwable());
1787           current_frame.pop_stack(type, CHECK_VERIFY(this));
1788           no_control_flow = true; break;
1789         default:
1790           // We only need to check the valid bytecodes in class file.
1791           // And jsr and ret are not in the new class file format in JDK1.5.
1792           verify_error(ErrorContext::bad_code(bci),
1793               "Bad instruction: %02x", opcode);
1794           no_control_flow = false;
1795           return;
1796       }  // end switch
1797     }  // end Merge with the next instruction
1798 
1799     // Look for possible jump target in exception handlers and see if it matches
1800     // current_frame.  Don't do this check if it has already been done (for
1801     // ([a,d,f,i,l]store* opcodes).  This check cannot be done earlier because
1802     // opcodes, such as invokespecial, may set the this_uninit flag.
1803     assert(!(verified_exc_handlers && this_uninit),
1804       "Exception handler targets got verified before this_uninit got set");
1805     if (!verified_exc_handlers && bci >= ex_min && bci < ex_max) {
1806       if (was_recursively_verified()) return;
1807       verify_exception_handler_targets(
1808         bci, this_uninit, &current_frame, &stackmap_table, CHECK_VERIFY(this));
1809     }
1810   } // end while
1811 
1812   // Make sure that control flow does not fall through end of the method
1813   if (!no_control_flow) {
1814     verify_error(ErrorContext::bad_code(code_length),
1815         "Control flow falls through code end");
1816     return;
1817   }
1818 }
1819 
1820 #undef bad_type_message
1821 
1822 char* ClassVerifier::generate_code_data(const methodHandle& m, u4 code_length, TRAPS) {
1823   char* code_data = NEW_RESOURCE_ARRAY(char, code_length);
1824   memset(code_data, 0, sizeof(char) * code_length);
1825   RawBytecodeStream bcs(m);
1826 
1827   while (!bcs.is_last_bytecode()) {
1828     if (bcs.raw_next() != Bytecodes::_illegal) {
1829       int bci = bcs.bci();
1830       if (bcs.raw_code() == Bytecodes::_new) {
1831         code_data[bci] = NEW_OFFSET;
1832       } else {
1833         code_data[bci] = BYTECODE_OFFSET;
1834       }
1835     } else {
1836       verify_error(ErrorContext::bad_code(bcs.bci()), "Bad instruction");
1837       return NULL;
1838     }
1839   }
1840 
1841   return code_data;
1842 }
1843 
1844 // Since this method references the constant pool, call was_recursively_verified()
1845 // before calling this method to make sure a prior class load did not cause the
1846 // current class to get verified.
1847 void ClassVerifier::verify_exception_handler_table(u4 code_length, char* code_data, int& min, int& max, TRAPS) {
1848   ExceptionTable exhandlers(_method());
1849   int exlength = exhandlers.length();
1850   constantPoolHandle cp (THREAD, _method->constants());
1851 
1852   for(int i = 0; i < exlength; i++) {
1853     u2 start_pc = exhandlers.start_pc(i);
1854     u2 end_pc = exhandlers.end_pc(i);
1855     u2 handler_pc = exhandlers.handler_pc(i);
1856     if (start_pc >= code_length || code_data[start_pc] == 0) {
1857       class_format_error("Illegal exception table start_pc %d", start_pc);
1858       return;
1859     }
1860     if (end_pc != code_length) {   // special case: end_pc == code_length
1861       if (end_pc > code_length || code_data[end_pc] == 0) {
1862         class_format_error("Illegal exception table end_pc %d", end_pc);
1863         return;
1864       }
1865     }
1866     if (handler_pc >= code_length || code_data[handler_pc] == 0) {
1867       class_format_error("Illegal exception table handler_pc %d", handler_pc);
1868       return;
1869     }
1870     int catch_type_index = exhandlers.catch_type_index(i);
1871     if (catch_type_index != 0) {
1872       VerificationType catch_type = cp_index_to_type(
1873         catch_type_index, cp, CHECK_VERIFY(this));
1874       VerificationType throwable =
1875         VerificationType::reference_type(vmSymbols::java_lang_Throwable());
1876       bool is_subclass = throwable.is_assignable_from(
1877         catch_type, this, false, CHECK_VERIFY(this));
1878       if (!is_subclass) {
1879         // 4286534: should throw VerifyError according to recent spec change
1880         verify_error(ErrorContext::bad_type(handler_pc,
1881             TypeOrigin::cp(catch_type_index, catch_type),
1882             TypeOrigin::implicit(throwable)),
1883             "Catch type is not a subclass "
1884             "of Throwable in exception handler %d", handler_pc);
1885         return;
1886       }
1887     }
1888     if (start_pc < min) min = start_pc;
1889     if (end_pc > max) max = end_pc;
1890   }
1891 }
1892 
1893 void ClassVerifier::verify_local_variable_table(u4 code_length, char* code_data, TRAPS) {
1894   int localvariable_table_length = _method->localvariable_table_length();
1895   if (localvariable_table_length > 0) {
1896     LocalVariableTableElement* table = _method->localvariable_table_start();
1897     for (int i = 0; i < localvariable_table_length; i++) {
1898       u2 start_bci = table[i].start_bci;
1899       u2 length = table[i].length;
1900 
1901       if (start_bci >= code_length || code_data[start_bci] == 0) {
1902         class_format_error(
1903           "Illegal local variable table start_pc %d", start_bci);
1904         return;
1905       }
1906       u4 end_bci = (u4)(start_bci + length);
1907       if (end_bci != code_length) {
1908         if (end_bci >= code_length || code_data[end_bci] == 0) {
1909           class_format_error( "Illegal local variable table length %d", length);
1910           return;
1911         }
1912       }
1913     }
1914   }
1915 }
1916 
1917 u2 ClassVerifier::verify_stackmap_table(u2 stackmap_index, u2 bci,
1918                                         StackMapFrame* current_frame,
1919                                         StackMapTable* stackmap_table,
1920                                         bool no_control_flow, TRAPS) {
1921   if (stackmap_index < stackmap_table->get_frame_count()) {
1922     u2 this_offset = stackmap_table->get_offset(stackmap_index);
1923     if (no_control_flow && this_offset > bci) {
1924       verify_error(ErrorContext::missing_stackmap(bci),
1925                    "Expecting a stack map frame");
1926       return 0;
1927     }
1928     if (this_offset == bci) {
1929       ErrorContext ctx;
1930       // See if current stack map can be assigned to the frame in table.
1931       // current_frame is the stackmap frame got from the last instruction.
1932       // If matched, current_frame will be updated by this method.
1933       bool matches = stackmap_table->match_stackmap(
1934         current_frame, this_offset, stackmap_index,
1935         !no_control_flow, true, &ctx, CHECK_VERIFY_(this, 0));
1936       if (!matches) {
1937         // report type error
1938         verify_error(ctx, "Instruction type does not match stack map");
1939         return 0;
1940       }
1941       stackmap_index++;
1942     } else if (this_offset < bci) {
1943       // current_offset should have met this_offset.
1944       class_format_error("Bad stack map offset %d", this_offset);
1945       return 0;
1946     }
1947   } else if (no_control_flow) {
1948     verify_error(ErrorContext::bad_code(bci), "Expecting a stack map frame");
1949     return 0;
1950   }
1951   return stackmap_index;
1952 }
1953 
1954 // Since this method references the constant pool, call was_recursively_verified()
1955 // before calling this method to make sure a prior class load did not cause the
1956 // current class to get verified.
1957 void ClassVerifier::verify_exception_handler_targets(u2 bci, bool this_uninit,
1958                                                      StackMapFrame* current_frame,
1959                                                      StackMapTable* stackmap_table, TRAPS) {
1960   constantPoolHandle cp (THREAD, _method->constants());
1961   ExceptionTable exhandlers(_method());
1962   int exlength = exhandlers.length();
1963   for(int i = 0; i < exlength; i++) {
1964     u2 start_pc = exhandlers.start_pc(i);
1965     u2 end_pc = exhandlers.end_pc(i);
1966     u2 handler_pc = exhandlers.handler_pc(i);
1967     int catch_type_index = exhandlers.catch_type_index(i);
1968     if(bci >= start_pc && bci < end_pc) {
1969       u1 flags = current_frame->flags();
1970       if (this_uninit) {  flags |= FLAG_THIS_UNINIT; }
1971       StackMapFrame* new_frame = current_frame->frame_in_exception_handler(flags);
1972       if (catch_type_index != 0) {
1973         if (was_recursively_verified()) return;
1974         // We know that this index refers to a subclass of Throwable
1975         VerificationType catch_type = cp_index_to_type(
1976           catch_type_index, cp, CHECK_VERIFY(this));
1977         new_frame->push_stack(catch_type, CHECK_VERIFY(this));
1978       } else {
1979         VerificationType throwable =
1980           VerificationType::reference_type(vmSymbols::java_lang_Throwable());
1981         new_frame->push_stack(throwable, CHECK_VERIFY(this));
1982       }
1983       ErrorContext ctx;
1984       bool matches = stackmap_table->match_stackmap(
1985         new_frame, handler_pc, true, false, &ctx, CHECK_VERIFY(this));
1986       if (!matches) {
1987         verify_error(ctx, "Stack map does not match the one at "
1988             "exception handler %d", handler_pc);
1989         return;
1990       }
1991     }
1992   }
1993 }
1994 
1995 void ClassVerifier::verify_cp_index(
1996     u2 bci, const constantPoolHandle& cp, int index, TRAPS) {
1997   int nconstants = cp->length();
1998   if ((index <= 0) || (index >= nconstants)) {
1999     verify_error(ErrorContext::bad_cp_index(bci, index),
2000         "Illegal constant pool index %d in class %s",
2001         index, cp->pool_holder()->external_name());
2002     return;
2003   }
2004 }
2005 
2006 void ClassVerifier::verify_cp_type(
2007     u2 bci, int index, const constantPoolHandle& cp, unsigned int types, TRAPS) {
2008 
2009   // In some situations, bytecode rewriting may occur while we're verifying.
2010   // In this case, a constant pool cache exists and some indices refer to that
2011   // instead.  Be sure we don't pick up such indices by accident.
2012   // We must check was_recursively_verified() before we get here.
2013   guarantee(cp->cache() == NULL, "not rewritten yet");
2014 
2015   verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
2016   unsigned int tag = cp->tag_at(index).value();
2017   if ((types & (1 << tag)) == 0) {
2018     verify_error(ErrorContext::bad_cp_index(bci, index),
2019       "Illegal type at constant pool entry %d in class %s",
2020       index, cp->pool_holder()->external_name());
2021     return;
2022   }
2023 }
2024 
2025 void ClassVerifier::verify_cp_class_type(
2026     u2 bci, int index, const constantPoolHandle& cp, TRAPS) {
2027   verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
2028   constantTag tag = cp->tag_at(index);
2029   if (!tag.is_klass() && !tag.is_unresolved_klass()) {
2030     verify_error(ErrorContext::bad_cp_index(bci, index),
2031         "Illegal type at constant pool entry %d in class %s",
2032         index, cp->pool_holder()->external_name());
2033     return;
2034   }
2035 }
2036 
2037 void ClassVerifier::verify_error(ErrorContext ctx, const char* msg, ...) {
2038   stringStream ss;
2039 
2040   ctx.reset_frames();
2041   _exception_type = vmSymbols::java_lang_VerifyError();
2042   _error_context = ctx;
2043   va_list va;
2044   va_start(va, msg);
2045   ss.vprint(msg, va);
2046   va_end(va);
2047   _message = ss.as_string();
2048 #ifdef ASSERT
2049   ResourceMark rm;
2050   const char* exception_name = _exception_type->as_C_string();
2051   Exceptions::debug_check_abort(exception_name, NULL);
2052 #endif // ndef ASSERT
2053 }
2054 
2055 void ClassVerifier::class_format_error(const char* msg, ...) {
2056   stringStream ss;
2057   _exception_type = vmSymbols::java_lang_ClassFormatError();
2058   va_list va;
2059   va_start(va, msg);
2060   ss.vprint(msg, va);
2061   va_end(va);
2062   if (!_method.is_null()) {
2063     ss.print(" in method %s", _method->name_and_sig_as_C_string());


2064   }
2065   _message = ss.as_string();
2066 }
2067 
2068 Klass* ClassVerifier::load_class(Symbol* name, TRAPS) {
2069   HandleMark hm(THREAD);
2070   // Get current loader and protection domain first.
2071   oop loader = current_class()->class_loader();
2072   oop protection_domain = current_class()->protection_domain();
2073 
2074   Klass* kls = SystemDictionary::resolve_or_fail(
2075     name, Handle(THREAD, loader), Handle(THREAD, protection_domain),
2076     true, THREAD);
2077 
2078   if (kls != NULL) {
2079     if (log_is_enabled(Debug, class, resolve)) {
2080       Verifier::trace_class_resolution(kls, current_class());
2081     }
2082   }
2083   return kls;
2084 }
2085 
2086 bool ClassVerifier::is_protected_access(InstanceKlass* this_class,
2087                                         Klass* target_class,
2088                                         Symbol* field_name,
2089                                         Symbol* field_sig,
2090                                         bool is_method) {
2091   NoSafepointVerifier nosafepoint;
2092 
2093   // If target class isn't a super class of this class, we don't worry about this case
2094   if (!this_class->is_subclass_of(target_class)) {
2095     return false;
2096   }
2097   // Check if the specified method or field is protected
2098   InstanceKlass* target_instance = InstanceKlass::cast(target_class);
2099   fieldDescriptor fd;
2100   if (is_method) {
2101     Method* m = target_instance->uncached_lookup_method(field_name, field_sig, Klass::find_overpass);
2102     if (m != NULL && m->is_protected()) {
2103       if (!this_class->is_same_class_package(m->method_holder())) {
2104         return true;
2105       }
2106     }
2107   } else {
2108     Klass* member_klass = target_instance->find_field(field_name, field_sig, &fd);
2109     if (member_klass != NULL && fd.is_protected()) {
2110       if (!this_class->is_same_class_package(member_klass)) {
2111         return true;
2112       }
2113     }
2114   }
2115   return false;
2116 }
2117 
2118 void ClassVerifier::verify_ldc(
2119     int opcode, u2 index, StackMapFrame* current_frame,
2120     const constantPoolHandle& cp, u2 bci, TRAPS) {
2121   verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
2122   constantTag tag = cp->tag_at(index);
2123   unsigned int types = 0;
2124   if (opcode == Bytecodes::_ldc || opcode == Bytecodes::_ldc_w) {
2125     if (!tag.is_unresolved_klass()) {
2126       types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float)
2127             | (1 << JVM_CONSTANT_String)  | (1 << JVM_CONSTANT_Class)
2128             | (1 << JVM_CONSTANT_MethodHandle) | (1 << JVM_CONSTANT_MethodType)
2129             | (1 << JVM_CONSTANT_Dynamic);
2130       // Note:  The class file parser already verified the legality of
2131       // MethodHandle and MethodType constants.
2132       verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2133     }
2134   } else {
2135     assert(opcode == Bytecodes::_ldc2_w, "must be ldc2_w");
2136     types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long)
2137           | (1 << JVM_CONSTANT_Dynamic);
2138     verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2139   }
2140   if (tag.is_string() && cp->is_pseudo_string_at(index)) {
2141     current_frame->push_stack(object_type(), CHECK_VERIFY(this));
2142   } else if (tag.is_string()) {
2143     current_frame->push_stack(
2144       VerificationType::reference_type(
2145         vmSymbols::java_lang_String()), CHECK_VERIFY(this));
2146   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
2147     current_frame->push_stack(
2148       VerificationType::reference_type(
2149         vmSymbols::java_lang_Class()), CHECK_VERIFY(this));
2150   } else if (tag.is_int()) {
2151     current_frame->push_stack(
2152       VerificationType::integer_type(), CHECK_VERIFY(this));
2153   } else if (tag.is_float()) {
2154     current_frame->push_stack(
2155       VerificationType::float_type(), CHECK_VERIFY(this));
2156   } else if (tag.is_double()) {
2157     current_frame->push_stack_2(
2158       VerificationType::double_type(),
2159       VerificationType::double2_type(), CHECK_VERIFY(this));
2160   } else if (tag.is_long()) {
2161     current_frame->push_stack_2(
2162       VerificationType::long_type(),
2163       VerificationType::long2_type(), CHECK_VERIFY(this));
2164   } else if (tag.is_method_handle()) {
2165     current_frame->push_stack(
2166       VerificationType::reference_type(
2167         vmSymbols::java_lang_invoke_MethodHandle()), CHECK_VERIFY(this));
2168   } else if (tag.is_method_type()) {
2169     current_frame->push_stack(
2170       VerificationType::reference_type(
2171         vmSymbols::java_lang_invoke_MethodType()), CHECK_VERIFY(this));
2172   } else if (tag.is_dynamic_constant()) {
2173     Symbol* constant_type = cp->uncached_signature_ref_at(index);
2174     // Field signature was checked in ClassFileParser.
2175     assert(SignatureVerifier::is_valid_type_signature(constant_type),
2176            "Invalid type for dynamic constant");
2177     assert(sizeof(VerificationType) == sizeof(uintptr_t),
2178           "buffer type must match VerificationType size");
2179     uintptr_t constant_type_buffer[2];
2180     VerificationType* v_constant_type = (VerificationType*)constant_type_buffer;
2181     SignatureStream sig_stream(constant_type, false);
2182     int n = change_sig_to_verificationType(
2183       &sig_stream, v_constant_type, CHECK_VERIFY(this));
2184     int opcode_n = (opcode == Bytecodes::_ldc2_w ? 2 : 1);
2185     if (n != opcode_n) {
2186       // wrong kind of ldc; reverify against updated type mask
2187       types &= ~(1 << JVM_CONSTANT_Dynamic);
2188       verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2189     }
2190     for (int i = 0; i < n; i++) {
2191       current_frame->push_stack(v_constant_type[i], CHECK_VERIFY(this));
2192     }
2193   } else {
2194     /* Unreachable? verify_cp_type has already validated the cp type. */
2195     verify_error(
2196         ErrorContext::bad_cp_index(bci, index), "Invalid index in ldc");
2197     return;
2198   }
2199 }
2200 
2201 void ClassVerifier::verify_switch(
2202     RawBytecodeStream* bcs, u4 code_length, char* code_data,
2203     StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS) {
2204   int bci = bcs->bci();
2205   address bcp = bcs->bcp();
2206   address aligned_bcp = align_up(bcp + 1, jintSize);
2207 
2208   if (_klass->major_version() < NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION) {
2209     // 4639449 & 4647081: padding bytes must be 0
2210     u2 padding_offset = 1;
2211     while ((bcp + padding_offset) < aligned_bcp) {
2212       if(*(bcp + padding_offset) != 0) {
2213         verify_error(ErrorContext::bad_code(bci),
2214                      "Nonzero padding byte in lookupswitch or tableswitch");
2215         return;
2216       }
2217       padding_offset++;
2218     }
2219   }
2220 
2221   int default_offset = (int) Bytes::get_Java_u4(aligned_bcp);
2222   int keys, delta;
2223   current_frame->pop_stack(
2224     VerificationType::integer_type(), CHECK_VERIFY(this));
2225   if (bcs->raw_code() == Bytecodes::_tableswitch) {
2226     jint low = (jint)Bytes::get_Java_u4(aligned_bcp + jintSize);
2227     jint high = (jint)Bytes::get_Java_u4(aligned_bcp + 2*jintSize);
2228     if (low > high) {
2229       verify_error(ErrorContext::bad_code(bci),
2230           "low must be less than or equal to high in tableswitch");
2231       return;
2232     }
2233     keys = high - low + 1;
2234     if (keys < 0) {
2235       verify_error(ErrorContext::bad_code(bci), "too many keys in tableswitch");
2236       return;
2237     }
2238     delta = 1;
2239   } else {
2240     keys = (int)Bytes::get_Java_u4(aligned_bcp + jintSize);
2241     if (keys < 0) {
2242       verify_error(ErrorContext::bad_code(bci),
2243                    "number of keys in lookupswitch less than 0");
2244       return;
2245     }
2246     delta = 2;
2247     // Make sure that the lookupswitch items are sorted
2248     for (int i = 0; i < (keys - 1); i++) {
2249       jint this_key = Bytes::get_Java_u4(aligned_bcp + (2+2*i)*jintSize);
2250       jint next_key = Bytes::get_Java_u4(aligned_bcp + (2+2*i+2)*jintSize);
2251       if (this_key >= next_key) {
2252         verify_error(ErrorContext::bad_code(bci),
2253                      "Bad lookupswitch instruction");
2254         return;
2255       }
2256     }
2257   }
2258   int target = bci + default_offset;
2259   stackmap_table->check_jump_target(current_frame, target, CHECK_VERIFY(this));
2260   for (int i = 0; i < keys; i++) {
2261     // Because check_jump_target() may safepoint, the bytecode could have
2262     // moved, which means 'aligned_bcp' is no good and needs to be recalculated.
2263     aligned_bcp = align_up(bcs->bcp() + 1, jintSize);
2264     target = bci + (jint)Bytes::get_Java_u4(aligned_bcp+(3+i*delta)*jintSize);
2265     stackmap_table->check_jump_target(
2266       current_frame, target, CHECK_VERIFY(this));
2267   }
2268   NOT_PRODUCT(aligned_bcp = NULL);  // no longer valid at this point
2269 }
2270 
2271 bool ClassVerifier::name_in_supers(
2272     Symbol* ref_name, InstanceKlass* current) {
2273   Klass* super = current->super();
2274   while (super != NULL) {
2275     if (super->name() == ref_name) {
2276       return true;
2277     }
2278     super = super->super();
2279   }
2280   return false;
2281 }
2282 
2283 void ClassVerifier::verify_field_instructions(RawBytecodeStream* bcs,
2284                                               StackMapFrame* current_frame,
2285                                               const constantPoolHandle& cp,
2286                                               bool allow_arrays,
2287                                               TRAPS) {
2288   u2 index = bcs->get_index_u2();
2289   verify_cp_type(bcs->bci(), index, cp,
2290       1 << JVM_CONSTANT_Fieldref, CHECK_VERIFY(this));
2291 
2292   // Get field name and signature
2293   Symbol* field_name = cp->name_ref_at(index);
2294   Symbol* field_sig = cp->signature_ref_at(index);
2295 
2296   // Field signature was checked in ClassFileParser.
2297   assert(SignatureVerifier::is_valid_type_signature(field_sig),
2298          "Invalid field signature");
2299 
2300   // Get referenced class type
2301   VerificationType ref_class_type = cp_ref_index_to_type(
2302     index, cp, CHECK_VERIFY(this));
2303   if (!ref_class_type.is_object() &&
2304     (!allow_arrays || !ref_class_type.is_array())) {
2305     verify_error(ErrorContext::bad_type(bcs->bci(),
2306         TypeOrigin::cp(index, ref_class_type)),
2307         "Expecting reference to class in class %s at constant pool index %d",
2308         _klass->external_name(), index);
2309     return;
2310   }
2311   VerificationType target_class_type = ref_class_type;
2312 
2313   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2314         "buffer type must match VerificationType size");
2315   uintptr_t field_type_buffer[2];
2316   VerificationType* field_type = (VerificationType*)field_type_buffer;
2317   // If we make a VerificationType[2] array directly, the compiler calls
2318   // to the c-runtime library to do the allocation instead of just
2319   // stack allocating it.  Plus it would run constructors.  This shows up
2320   // in performance profiles.
2321 
2322   SignatureStream sig_stream(field_sig, false);
2323   VerificationType stack_object_type;
2324   int n = change_sig_to_verificationType(
2325     &sig_stream, field_type, CHECK_VERIFY(this));
2326   u2 bci = bcs->bci();
2327   bool is_assignable;
2328   switch (bcs->raw_code()) {
2329     case Bytecodes::_getstatic: {
2330       for (int i = 0; i < n; i++) {
2331         current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
2332       }
2333       break;
2334     }
2335     case Bytecodes::_putstatic: {
2336       for (int i = n - 1; i >= 0; i--) {
2337         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2338       }
2339       break;
2340     }
2341     case Bytecodes::_getfield: {
2342       stack_object_type = current_frame->pop_stack(
2343         target_class_type, CHECK_VERIFY(this));
2344       for (int i = 0; i < n; i++) {
2345         current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
2346       }
2347       goto check_protected;
2348     }
2349     case Bytecodes::_putfield: {
2350       for (int i = n - 1; i >= 0; i--) {
2351         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2352       }
2353       stack_object_type = current_frame->pop_stack(CHECK_VERIFY(this));
2354 
2355       // The JVMS 2nd edition allows field initialization before the superclass
2356       // initializer, if the field is defined within the current class.
2357       fieldDescriptor fd;
2358       if (stack_object_type == VerificationType::uninitialized_this_type() &&
2359           target_class_type.equals(current_type()) &&
2360           _klass->find_local_field(field_name, field_sig, &fd)) {
2361         stack_object_type = current_type();
2362       }
2363       is_assignable = target_class_type.is_assignable_from(
2364         stack_object_type, this, false, CHECK_VERIFY(this));
2365       if (!is_assignable) {
2366         verify_error(ErrorContext::bad_type(bci,
2367             current_frame->stack_top_ctx(),
2368             TypeOrigin::cp(index, target_class_type)),
2369             "Bad type on operand stack in putfield");
2370         return;
2371       }
2372     }
2373     check_protected: {
2374       if (_this_type == stack_object_type)
2375         break; // stack_object_type must be assignable to _current_class_type
2376       if (was_recursively_verified()) return;
2377       Symbol* ref_class_name =
2378         cp->klass_name_at(cp->klass_ref_index_at(index));
2379       if (!name_in_supers(ref_class_name, current_class()))
2380         // stack_object_type must be assignable to _current_class_type since:
2381         // 1. stack_object_type must be assignable to ref_class.
2382         // 2. ref_class must be _current_class or a subclass of it. It can't
2383         //    be a superclass of it. See revised JVMS 5.4.4.
2384         break;
2385 
2386       Klass* ref_class_oop = load_class(ref_class_name, CHECK);
2387       if (is_protected_access(current_class(), ref_class_oop, field_name,
2388                               field_sig, false)) {
2389         // It's protected access, check if stack object is assignable to
2390         // current class.
2391         is_assignable = current_type().is_assignable_from(
2392           stack_object_type, this, true, CHECK_VERIFY(this));
2393         if (!is_assignable) {
2394           verify_error(ErrorContext::bad_type(bci,
2395               current_frame->stack_top_ctx(),
2396               TypeOrigin::implicit(current_type())),
2397               "Bad access to protected data in getfield");
2398           return;
2399         }
2400       }
2401       break;
2402     }
2403     default: ShouldNotReachHere();
2404   }
2405 }
2406 
2407 // Look at the method's handlers.  If the bci is in the handler's try block
2408 // then check if the handler_pc is already on the stack.  If not, push it
2409 // unless the handler has already been scanned.
2410 void ClassVerifier::push_handlers(ExceptionTable* exhandlers,
2411                                   GrowableArray<u4>* handler_list,
2412                                   GrowableArray<u4>* handler_stack,
2413                                   u4 bci) {
2414   int exlength = exhandlers->length();
2415   for(int x = 0; x < exlength; x++) {
2416     if (bci >= exhandlers->start_pc(x) && bci < exhandlers->end_pc(x)) {
2417       u4 exhandler_pc = exhandlers->handler_pc(x);
2418       if (!handler_list->contains(exhandler_pc)) {
2419         handler_stack->append_if_missing(exhandler_pc);
2420         handler_list->append(exhandler_pc);
2421       }
2422     }
2423   }
2424 }
2425 
2426 // Return TRUE if all code paths starting with start_bc_offset end in
2427 // bytecode athrow or loop.
2428 bool ClassVerifier::ends_in_athrow(u4 start_bc_offset) {
2429   ResourceMark rm;
2430   // Create bytecode stream.
2431   RawBytecodeStream bcs(method());
2432   u4 code_length = method()->code_size();
2433   bcs.set_start(start_bc_offset);
2434   u4 target;
2435   // Create stack for storing bytecode start offsets for if* and *switch.
2436   GrowableArray<u4>* bci_stack = new GrowableArray<u4>(30);
2437   // Create stack for handlers for try blocks containing this handler.
2438   GrowableArray<u4>* handler_stack = new GrowableArray<u4>(30);
2439   // Create list of handlers that have been pushed onto the handler_stack
2440   // so that handlers embedded inside of their own TRY blocks only get
2441   // scanned once.
2442   GrowableArray<u4>* handler_list = new GrowableArray<u4>(30);
2443   // Create list of visited branch opcodes (goto* and if*).
2444   GrowableArray<u4>* visited_branches = new GrowableArray<u4>(30);
2445   ExceptionTable exhandlers(_method());
2446 
2447   while (true) {
2448     if (bcs.is_last_bytecode()) {
2449       // if no more starting offsets to parse or if at the end of the
2450       // method then return false.
2451       if ((bci_stack->is_empty()) || ((u4)bcs.end_bci() == code_length))
2452         return false;
2453       // Pop a bytecode starting offset and scan from there.
2454       bcs.set_start(bci_stack->pop());
2455     }
2456     Bytecodes::Code opcode = bcs.raw_next();
2457     u4 bci = bcs.bci();
2458 
2459     // If the bytecode is in a TRY block, push its handlers so they
2460     // will get parsed.
2461     push_handlers(&exhandlers, handler_list, handler_stack, bci);
2462 
2463     switch (opcode) {
2464       case Bytecodes::_if_icmpeq:
2465       case Bytecodes::_if_icmpne:
2466       case Bytecodes::_if_icmplt:
2467       case Bytecodes::_if_icmpge:
2468       case Bytecodes::_if_icmpgt:
2469       case Bytecodes::_if_icmple:
2470       case Bytecodes::_ifeq:
2471       case Bytecodes::_ifne:
2472       case Bytecodes::_iflt:
2473       case Bytecodes::_ifge:
2474       case Bytecodes::_ifgt:
2475       case Bytecodes::_ifle:
2476       case Bytecodes::_if_acmpeq:
2477       case Bytecodes::_if_acmpne:
2478       case Bytecodes::_ifnull:
2479       case Bytecodes::_ifnonnull:
2480         target = bcs.dest();
2481         if (visited_branches->contains(bci)) {
2482           if (bci_stack->is_empty()) {
2483             if (handler_stack->is_empty()) {
2484               return true;
2485             } else {
2486               // Parse the catch handlers for try blocks containing athrow.
2487               bcs.set_start(handler_stack->pop());
2488             }
2489           } else {
2490             // Pop a bytecode starting offset and scan from there.
2491             bcs.set_start(bci_stack->pop());
2492           }
2493         } else {
2494           if (target > bci) { // forward branch
2495             if (target >= code_length) return false;
2496             // Push the branch target onto the stack.
2497             bci_stack->push(target);
2498             // then, scan bytecodes starting with next.
2499             bcs.set_start(bcs.next_bci());
2500           } else { // backward branch
2501             // Push bytecode offset following backward branch onto the stack.
2502             bci_stack->push(bcs.next_bci());
2503             // Check bytecodes starting with branch target.
2504             bcs.set_start(target);
2505           }
2506           // Record target so we don't branch here again.
2507           visited_branches->append(bci);
2508         }
2509         break;
2510 
2511       case Bytecodes::_goto:
2512       case Bytecodes::_goto_w:
2513         target = (opcode == Bytecodes::_goto ? bcs.dest() : bcs.dest_w());
2514         if (visited_branches->contains(bci)) {
2515           if (bci_stack->is_empty()) {
2516             if (handler_stack->is_empty()) {
2517               return true;
2518             } else {
2519               // Parse the catch handlers for try blocks containing athrow.
2520               bcs.set_start(handler_stack->pop());
2521             }
2522           } else {
2523             // Been here before, pop new starting offset from stack.
2524             bcs.set_start(bci_stack->pop());
2525           }
2526         } else {
2527           if (target >= code_length) return false;
2528           // Continue scanning from the target onward.
2529           bcs.set_start(target);
2530           // Record target so we don't branch here again.
2531           visited_branches->append(bci);
2532         }
2533         break;
2534 
2535       // Check that all switch alternatives end in 'athrow' bytecodes. Since it
2536       // is  difficult to determine where each switch alternative ends, parse
2537       // each switch alternative until either hit a 'return', 'athrow', or reach
2538       // the end of the method's bytecodes.  This is gross but should be okay
2539       // because:
2540       // 1. tableswitch and lookupswitch byte codes in handlers for ctor explicit
2541       //    constructor invocations should be rare.
2542       // 2. if each switch alternative ends in an athrow then the parsing should be
2543       //    short.  If there is no athrow then it is bogus code, anyway.
2544       case Bytecodes::_lookupswitch:
2545       case Bytecodes::_tableswitch:
2546         {
2547           address aligned_bcp = align_up(bcs.bcp() + 1, jintSize);
2548           u4 default_offset = Bytes::get_Java_u4(aligned_bcp) + bci;
2549           int keys, delta;
2550           if (opcode == Bytecodes::_tableswitch) {
2551             jint low = (jint)Bytes::get_Java_u4(aligned_bcp + jintSize);
2552             jint high = (jint)Bytes::get_Java_u4(aligned_bcp + 2*jintSize);
2553             // This is invalid, but let the regular bytecode verifier
2554             // report this because the user will get a better error message.
2555             if (low > high) return true;
2556             keys = high - low + 1;
2557             delta = 1;
2558           } else {
2559             keys = (int)Bytes::get_Java_u4(aligned_bcp + jintSize);
2560             delta = 2;
2561           }
2562           // Invalid, let the regular bytecode verifier deal with it.
2563           if (keys < 0) return true;
2564 
2565           // Push the offset of the next bytecode onto the stack.
2566           bci_stack->push(bcs.next_bci());
2567 
2568           // Push the switch alternatives onto the stack.
2569           for (int i = 0; i < keys; i++) {
2570             u4 target = bci + (jint)Bytes::get_Java_u4(aligned_bcp+(3+i*delta)*jintSize);
2571             if (target > code_length) return false;
2572             bci_stack->push(target);
2573           }
2574 
2575           // Start bytecode parsing for the switch at the default alternative.
2576           if (default_offset > code_length) return false;
2577           bcs.set_start(default_offset);
2578           break;
2579         }
2580 
2581       case Bytecodes::_return:
2582         return false;
2583 
2584       case Bytecodes::_athrow:
2585         {
2586           if (bci_stack->is_empty()) {
2587             if (handler_stack->is_empty()) {
2588               return true;
2589             } else {
2590               // Parse the catch handlers for try blocks containing athrow.
2591               bcs.set_start(handler_stack->pop());
2592             }
2593           } else {
2594             // Pop a bytecode offset and starting scanning from there.
2595             bcs.set_start(bci_stack->pop());
2596           }
2597         }
2598         break;
2599 
2600       default:
2601         ;
2602     } // end switch
2603   } // end while loop
2604 
2605   return false;
2606 }
2607 
2608 void ClassVerifier::verify_invoke_init(
2609     RawBytecodeStream* bcs, u2 ref_class_index, VerificationType ref_class_type,
2610     StackMapFrame* current_frame, u4 code_length, bool in_try_block,
2611     bool *this_uninit, const constantPoolHandle& cp, StackMapTable* stackmap_table,
2612     TRAPS) {
2613   u2 bci = bcs->bci();
2614   VerificationType type = current_frame->pop_stack(
2615     VerificationType::reference_check(), CHECK_VERIFY(this));
2616   if (type == VerificationType::uninitialized_this_type()) {
2617     // The method must be an <init> method of this class or its superclass
2618     Klass* superk = current_class()->super();
2619     if (ref_class_type.name() != current_class()->name() &&
2620         ref_class_type.name() != superk->name()) {
2621       verify_error(ErrorContext::bad_type(bci,
2622           TypeOrigin::implicit(ref_class_type),
2623           TypeOrigin::implicit(current_type())),
2624           "Bad <init> method call");
2625       return;
2626     }
2627 
2628     // If this invokespecial call is done from inside of a TRY block then make
2629     // sure that all catch clause paths end in a throw.  Otherwise, this can
2630     // result in returning an incomplete object.
2631     if (in_try_block) {
2632       ExceptionTable exhandlers(_method());
2633       int exlength = exhandlers.length();
2634       for(int i = 0; i < exlength; i++) {
2635         u2 start_pc = exhandlers.start_pc(i);
2636         u2 end_pc = exhandlers.end_pc(i);
2637 
2638         if (bci >= start_pc && bci < end_pc) {
2639           if (!ends_in_athrow(exhandlers.handler_pc(i))) {
2640             verify_error(ErrorContext::bad_code(bci),
2641               "Bad <init> method call from after the start of a try block");
2642             return;
2643           } else if (log_is_enabled(Info, verification)) {
2644             ResourceMark rm(THREAD);
2645             log_info(verification)("Survived call to ends_in_athrow(): %s",
2646                                           current_class()->name()->as_C_string());
2647           }
2648         }
2649       }
2650 
2651       // Check the exception handler target stackmaps with the locals from the
2652       // incoming stackmap (before initialize_object() changes them to outgoing
2653       // state).
2654       if (was_recursively_verified()) return;
2655       verify_exception_handler_targets(bci, true, current_frame,
2656                                        stackmap_table, CHECK_VERIFY(this));
2657     } // in_try_block
2658 
2659     current_frame->initialize_object(type, current_type());
2660     *this_uninit = true;
2661   } else if (type.is_uninitialized()) {
2662     u2 new_offset = type.bci();
2663     address new_bcp = bcs->bcp() - bci + new_offset;
2664     if (new_offset > (code_length - 3) || (*new_bcp) != Bytecodes::_new) {
2665       /* Unreachable?  Stack map parsing ensures valid type and new
2666        * instructions have a valid BCI. */
2667       verify_error(ErrorContext::bad_code(new_offset),
2668                    "Expecting new instruction");
2669       return;
2670     }
2671     u2 new_class_index = Bytes::get_Java_u2(new_bcp + 1);
2672     if (was_recursively_verified()) return;
2673     verify_cp_class_type(bci, new_class_index, cp, CHECK_VERIFY(this));
2674 
2675     // The method must be an <init> method of the indicated class
2676     VerificationType new_class_type = cp_index_to_type(
2677       new_class_index, cp, CHECK_VERIFY(this));
2678     if (!new_class_type.equals(ref_class_type)) {
2679       verify_error(ErrorContext::bad_type(bci,
2680           TypeOrigin::cp(new_class_index, new_class_type),
2681           TypeOrigin::cp(ref_class_index, ref_class_type)),
2682           "Call to wrong <init> method");
2683       return;
2684     }
2685     // According to the VM spec, if the referent class is a superclass of the
2686     // current class, and is in a different runtime package, and the method is
2687     // protected, then the objectref must be the current class or a subclass
2688     // of the current class.
2689     VerificationType objectref_type = new_class_type;
2690     if (name_in_supers(ref_class_type.name(), current_class())) {
2691       Klass* ref_klass = load_class(ref_class_type.name(), CHECK);
2692       if (was_recursively_verified()) return;
2693       Method* m = InstanceKlass::cast(ref_klass)->uncached_lookup_method(
2694         vmSymbols::object_initializer_name(),
2695         cp->signature_ref_at(bcs->get_index_u2()),
2696         Klass::find_overpass);
2697       // Do nothing if method is not found.  Let resolution detect the error.
2698       if (m != NULL) {
2699         InstanceKlass* mh = m->method_holder();
2700         if (m->is_protected() && !mh->is_same_class_package(_klass)) {
2701           bool assignable = current_type().is_assignable_from(
2702             objectref_type, this, true, CHECK_VERIFY(this));
2703           if (!assignable) {
2704             verify_error(ErrorContext::bad_type(bci,
2705                 TypeOrigin::cp(new_class_index, objectref_type),
2706                 TypeOrigin::implicit(current_type())),
2707                 "Bad access to protected <init> method");
2708             return;
2709           }
2710         }
2711       }
2712     }
2713     // Check the exception handler target stackmaps with the locals from the
2714     // incoming stackmap (before initialize_object() changes them to outgoing
2715     // state).
2716     if (in_try_block) {
2717       if (was_recursively_verified()) return;
2718       verify_exception_handler_targets(bci, *this_uninit, current_frame,
2719                                        stackmap_table, CHECK_VERIFY(this));
2720     }
2721     current_frame->initialize_object(type, new_class_type);
2722   } else {
2723     verify_error(ErrorContext::bad_type(bci, current_frame->stack_top_ctx()),
2724         "Bad operand type when invoking <init>");
2725     return;
2726   }
2727 }
2728 
2729 bool ClassVerifier::is_same_or_direct_interface(
2730     InstanceKlass* klass,
2731     VerificationType klass_type,
2732     VerificationType ref_class_type) {
2733   if (ref_class_type.equals(klass_type)) return true;
2734   Array<InstanceKlass*>* local_interfaces = klass->local_interfaces();
2735   if (local_interfaces != NULL) {
2736     for (int x = 0; x < local_interfaces->length(); x++) {
2737       InstanceKlass* k = local_interfaces->at(x);
2738       assert (k != NULL && k->is_interface(), "invalid interface");
2739       if (ref_class_type.equals(VerificationType::reference_type(k->name()))) {
2740         return true;
2741       }
2742     }
2743   }
2744   return false;
2745 }
2746 
2747 void ClassVerifier::verify_invoke_instructions(
2748     RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
2749     bool in_try_block, bool *this_uninit, VerificationType return_type,
2750     const constantPoolHandle& cp, StackMapTable* stackmap_table, TRAPS) {
2751   // Make sure the constant pool item is the right type
2752   u2 index = bcs->get_index_u2();
2753   Bytecodes::Code opcode = bcs->raw_code();
2754   unsigned int types = 0;
2755   switch (opcode) {
2756     case Bytecodes::_invokeinterface:
2757       types = 1 << JVM_CONSTANT_InterfaceMethodref;
2758       break;
2759     case Bytecodes::_invokedynamic:
2760       types = 1 << JVM_CONSTANT_InvokeDynamic;
2761       break;
2762     case Bytecodes::_invokespecial:
2763     case Bytecodes::_invokestatic:
2764       types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ?
2765         (1 << JVM_CONSTANT_Methodref) :
2766         ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref));
2767       break;
2768     default:
2769       types = 1 << JVM_CONSTANT_Methodref;
2770   }
2771   verify_cp_type(bcs->bci(), index, cp, types, CHECK_VERIFY(this));
2772 
2773   // Get method name and signature
2774   Symbol* method_name = cp->name_ref_at(index);
2775   Symbol* method_sig = cp->signature_ref_at(index);
2776 
2777   // Method signature was checked in ClassFileParser.
2778   assert(SignatureVerifier::is_valid_method_signature(method_sig),
2779          "Invalid method signature");
2780 
2781   // Get referenced class type
2782   VerificationType ref_class_type;
2783   if (opcode == Bytecodes::_invokedynamic) {
2784     if (_klass->major_version() < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2785       class_format_error(
2786         "invokedynamic instructions not supported by this class file version (%d), class %s",
2787         _klass->major_version(), _klass->external_name());
2788       return;
2789     }
2790   } else {
2791     ref_class_type = cp_ref_index_to_type(index, cp, CHECK_VERIFY(this));
2792   }
2793 
2794   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2795         "buffer type must match VerificationType size");
2796 
2797   // Get the UTF8 index for this signature.
2798   int sig_index = cp->signature_ref_index_at(cp->name_and_type_ref_index_at(index));
2799 
2800   // Get the signature's verification types.
2801   sig_as_verification_types* mth_sig_verif_types;
2802   sig_as_verification_types** mth_sig_verif_types_ptr = method_signatures_table()->get(sig_index);
2803   if (mth_sig_verif_types_ptr != NULL) {
2804     // Found the entry for the signature's verification types in the hash table.
2805     mth_sig_verif_types = *mth_sig_verif_types_ptr;
2806     assert(mth_sig_verif_types != NULL, "Unexpected NULL sig_as_verification_types value");
2807   } else {
2808     // Not found, add the entry to the table.
2809     GrowableArray<VerificationType>* verif_types = new GrowableArray<VerificationType>(10);
2810     mth_sig_verif_types = new sig_as_verification_types(verif_types);
2811     create_method_sig_entry(mth_sig_verif_types, sig_index, CHECK_VERIFY(this));
2812   }
2813 
2814   // Get the number of arguments for this signature.
2815   int nargs = mth_sig_verif_types->num_args();
2816 
2817   // Check instruction operands
2818   u2 bci = bcs->bci();
2819   if (opcode == Bytecodes::_invokeinterface) {
2820     address bcp = bcs->bcp();
2821     // 4905268: count operand in invokeinterface should be nargs+1, not nargs.
2822     // JSR202 spec: The count operand of an invokeinterface instruction is valid if it is
2823     // the difference between the size of the operand stack before and after the instruction
2824     // executes.
2825     if (*(bcp+3) != (nargs+1)) {
2826       verify_error(ErrorContext::bad_code(bci),
2827           "Inconsistent args count operand in invokeinterface");
2828       return;
2829     }
2830     if (*(bcp+4) != 0) {
2831       verify_error(ErrorContext::bad_code(bci),
2832           "Fourth operand byte of invokeinterface must be zero");
2833       return;
2834     }
2835   }
2836 
2837   if (opcode == Bytecodes::_invokedynamic) {
2838     address bcp = bcs->bcp();
2839     if (*(bcp+3) != 0 || *(bcp+4) != 0) {
2840       verify_error(ErrorContext::bad_code(bci),
2841           "Third and fourth operand bytes of invokedynamic must be zero");
2842       return;
2843     }
2844   }
2845 
2846   if (method_name->char_at(0) == '<') {
2847     // Make sure <init> can only be invoked by invokespecial
2848     if (opcode != Bytecodes::_invokespecial ||
2849         method_name != vmSymbols::object_initializer_name()) {
2850       verify_error(ErrorContext::bad_code(bci),
2851           "Illegal call to internal method");
2852       return;
2853     }
2854   } else if (opcode == Bytecodes::_invokespecial
2855              && !is_same_or_direct_interface(current_class(), current_type(), ref_class_type)
2856              && !ref_class_type.equals(VerificationType::reference_type(
2857                   current_class()->super()->name()))) {
2858     bool subtype = false;
2859     bool have_imr_indirect = cp->tag_at(index).value() == JVM_CONSTANT_InterfaceMethodref;
2860     if (!current_class()->is_unsafe_anonymous()) {
2861       subtype = ref_class_type.is_assignable_from(
2862                  current_type(), this, false, CHECK_VERIFY(this));
2863     } else {
2864       VerificationType unsafe_anonymous_host_type =
2865                         VerificationType::reference_type(current_class()->unsafe_anonymous_host()->name());
2866       subtype = ref_class_type.is_assignable_from(unsafe_anonymous_host_type, this, false, CHECK_VERIFY(this));
2867 
2868       // If invokespecial of IMR, need to recheck for same or
2869       // direct interface relative to the host class
2870       have_imr_indirect = (have_imr_indirect &&
2871                            !is_same_or_direct_interface(
2872                              current_class()->unsafe_anonymous_host(),
2873                              unsafe_anonymous_host_type, ref_class_type));
2874     }
2875     if (!subtype) {
2876       verify_error(ErrorContext::bad_code(bci),
2877           "Bad invokespecial instruction: "
2878           "current class isn't assignable to reference class.");
2879        return;
2880     } else if (have_imr_indirect) {
2881       verify_error(ErrorContext::bad_code(bci),
2882           "Bad invokespecial instruction: "
2883           "interface method reference is in an indirect superinterface.");
2884       return;
2885     }
2886 
2887   }
2888 
2889   // Get the verification types for the method's arguments.
2890   GrowableArray<VerificationType>* sig_verif_types = mth_sig_verif_types->sig_verif_types();
2891   assert(sig_verif_types != NULL, "Missing signature's array of verification types");
2892   // Match method descriptor with operand stack
2893   // The arguments are on the stack in descending order.
2894   for (int i = nargs - 1; i >= 0; i--) { // Run backwards
2895     current_frame->pop_stack(sig_verif_types->at(i), CHECK_VERIFY(this));
2896   }
2897 
2898   // Check objectref on operand stack
2899   if (opcode != Bytecodes::_invokestatic &&
2900       opcode != Bytecodes::_invokedynamic) {
2901     if (method_name == vmSymbols::object_initializer_name()) {  // <init> method
2902       verify_invoke_init(bcs, index, ref_class_type, current_frame,
2903         code_length, in_try_block, this_uninit, cp, stackmap_table,
2904         CHECK_VERIFY(this));
2905       if (was_recursively_verified()) return;
2906     } else {   // other methods
2907       // Ensures that target class is assignable to method class.
2908       if (opcode == Bytecodes::_invokespecial) {
2909         if (!current_class()->is_unsafe_anonymous()) {
2910           current_frame->pop_stack(current_type(), CHECK_VERIFY(this));
2911         } else {
2912           // anonymous class invokespecial calls: check if the
2913           // objectref is a subtype of the unsafe_anonymous_host of the current class
2914           // to allow an anonymous class to reference methods in the unsafe_anonymous_host
2915           VerificationType top = current_frame->pop_stack(CHECK_VERIFY(this));
2916           VerificationType hosttype =
2917             VerificationType::reference_type(current_class()->unsafe_anonymous_host()->name());
2918           bool subtype = hosttype.is_assignable_from(top, this, false, CHECK_VERIFY(this));
2919           if (!subtype) {
2920             verify_error( ErrorContext::bad_type(current_frame->offset(),
2921               current_frame->stack_top_ctx(),
2922               TypeOrigin::implicit(top)),
2923               "Bad type on operand stack");
2924             return;
2925           }
2926         }
2927       } else if (opcode == Bytecodes::_invokevirtual) {
2928         VerificationType stack_object_type =
2929           current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this));
2930         if (current_type() != stack_object_type) {
2931           if (was_recursively_verified()) return;
2932           assert(cp->cache() == NULL, "not rewritten yet");
2933           Symbol* ref_class_name =
2934             cp->klass_name_at(cp->klass_ref_index_at(index));
2935           // See the comments in verify_field_instructions() for
2936           // the rationale behind this.
2937           if (name_in_supers(ref_class_name, current_class())) {
2938             Klass* ref_class = load_class(ref_class_name, CHECK);
2939             if (is_protected_access(
2940                   _klass, ref_class, method_name, method_sig, true)) {
2941               // It's protected access, check if stack object is
2942               // assignable to current class.
2943               bool is_assignable = current_type().is_assignable_from(
2944                 stack_object_type, this, true, CHECK_VERIFY(this));
2945               if (!is_assignable) {
2946                 if (ref_class_type.name() == vmSymbols::java_lang_Object()
2947                     && stack_object_type.is_array()
2948                     && method_name == vmSymbols::clone_name()) {
2949                   // Special case: arrays pretend to implement public Object
2950                   // clone().
2951                 } else {
2952                   verify_error(ErrorContext::bad_type(bci,
2953                       current_frame->stack_top_ctx(),
2954                       TypeOrigin::implicit(current_type())),
2955                       "Bad access to protected data in invokevirtual");
2956                   return;
2957                 }
2958               }
2959             }
2960           }
2961         }
2962       } else {
2963         assert(opcode == Bytecodes::_invokeinterface, "Unexpected opcode encountered");
2964         current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this));
2965       }
2966     }
2967   }
2968   // Push the result type.
2969   int sig_verif_types_len = sig_verif_types->length();
2970   if (sig_verif_types_len > nargs) {  // There's a return type
2971     if (method_name == vmSymbols::object_initializer_name()) {
2972       // <init> method must have a void return type
2973       /* Unreachable?  Class file parser verifies that methods with '<' have
2974        * void return */
2975       verify_error(ErrorContext::bad_code(bci),
2976           "Return type must be void in <init> method");
2977       return;
2978     }
2979 
2980     assert(sig_verif_types_len <= nargs + 2,
2981            "Signature verification types array return type is bogus");
2982     for (int i = nargs; i < sig_verif_types_len; i++) {
2983       assert(i == nargs || sig_verif_types->at(i).is_long2() ||
2984              sig_verif_types->at(i).is_double2(), "Unexpected return verificationType");
2985       current_frame->push_stack(sig_verif_types->at(i), CHECK_VERIFY(this));
2986     }
2987   }
2988 }
2989 
2990 VerificationType ClassVerifier::get_newarray_type(
2991     u2 index, u2 bci, TRAPS) {
2992   const char* from_bt[] = {
2993     NULL, NULL, NULL, NULL, "[Z", "[C", "[F", "[D", "[B", "[S", "[I", "[J",
2994   };
2995   if (index < T_BOOLEAN || index > T_LONG) {
2996     verify_error(ErrorContext::bad_code(bci), "Illegal newarray instruction");
2997     return VerificationType::bogus_type();
2998   }
2999 
3000   // from_bt[index] contains the array signature which has a length of 2
3001   Symbol* sig = create_temporary_symbol(
3002     from_bt[index], 2, CHECK_(VerificationType::bogus_type()));
3003   return VerificationType::reference_type(sig);
3004 }
3005 
3006 void ClassVerifier::verify_anewarray(
3007     u2 bci, u2 index, const constantPoolHandle& cp,
3008     StackMapFrame* current_frame, TRAPS) {
3009   verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
3010   current_frame->pop_stack(
3011     VerificationType::integer_type(), CHECK_VERIFY(this));
3012 
3013   if (was_recursively_verified()) return;
3014   VerificationType component_type =
3015     cp_index_to_type(index, cp, CHECK_VERIFY(this));
3016   int length;
3017   char* arr_sig_str;
3018   if (component_type.is_array()) {     // it's an array
3019     const char* component_name = component_type.name()->as_utf8();
3020     // Check for more than MAX_ARRAY_DIMENSIONS
3021     length = (int)strlen(component_name);
3022     if (length > MAX_ARRAY_DIMENSIONS &&
3023         component_name[MAX_ARRAY_DIMENSIONS - 1] == '[') {
3024       verify_error(ErrorContext::bad_code(bci),
3025         "Illegal anewarray instruction, array has more than 255 dimensions");
3026     }
3027     // add one dimension to component
3028     length++;
3029     arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length + 1);
3030     int n = os::snprintf(arr_sig_str, length + 1, "[%s", component_name);
3031     assert(n == length, "Unexpected number of characters in string");
3032   } else {         // it's an object or interface
3033     const char* component_name = component_type.name()->as_utf8();
3034     // add one dimension to component with 'L' prepended and ';' postpended.
3035     length = (int)strlen(component_name) + 3;
3036     arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length + 1);
3037     int n = os::snprintf(arr_sig_str, length + 1, "[L%s;", component_name);
3038     assert(n == length, "Unexpected number of characters in string");
3039   }
3040   Symbol* arr_sig = create_temporary_symbol(
3041     arr_sig_str, length, CHECK_VERIFY(this));
3042   VerificationType new_array_type = VerificationType::reference_type(arr_sig);
3043   current_frame->push_stack(new_array_type, CHECK_VERIFY(this));
3044 }
3045 
3046 void ClassVerifier::verify_iload(u2 index, StackMapFrame* current_frame, TRAPS) {
3047   current_frame->get_local(
3048     index, VerificationType::integer_type(), CHECK_VERIFY(this));
3049   current_frame->push_stack(
3050     VerificationType::integer_type(), CHECK_VERIFY(this));
3051 }
3052 
3053 void ClassVerifier::verify_lload(u2 index, StackMapFrame* current_frame, TRAPS) {
3054   current_frame->get_local_2(
3055     index, VerificationType::long_type(),
3056     VerificationType::long2_type(), CHECK_VERIFY(this));
3057   current_frame->push_stack_2(
3058     VerificationType::long_type(),
3059     VerificationType::long2_type(), CHECK_VERIFY(this));
3060 }
3061 
3062 void ClassVerifier::verify_fload(u2 index, StackMapFrame* current_frame, TRAPS) {
3063   current_frame->get_local(
3064     index, VerificationType::float_type(), CHECK_VERIFY(this));
3065   current_frame->push_stack(
3066     VerificationType::float_type(), CHECK_VERIFY(this));
3067 }
3068 
3069 void ClassVerifier::verify_dload(u2 index, StackMapFrame* current_frame, TRAPS) {
3070   current_frame->get_local_2(
3071     index, VerificationType::double_type(),
3072     VerificationType::double2_type(), CHECK_VERIFY(this));
3073   current_frame->push_stack_2(
3074     VerificationType::double_type(),
3075     VerificationType::double2_type(), CHECK_VERIFY(this));
3076 }
3077 
3078 void ClassVerifier::verify_aload(u2 index, StackMapFrame* current_frame, TRAPS) {
3079   VerificationType type = current_frame->get_local(
3080     index, VerificationType::reference_check(), CHECK_VERIFY(this));
3081   current_frame->push_stack(type, CHECK_VERIFY(this));
3082 }
3083 
3084 void ClassVerifier::verify_istore(u2 index, StackMapFrame* current_frame, TRAPS) {
3085   current_frame->pop_stack(
3086     VerificationType::integer_type(), CHECK_VERIFY(this));
3087   current_frame->set_local(
3088     index, VerificationType::integer_type(), CHECK_VERIFY(this));
3089 }
3090 
3091 void ClassVerifier::verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS) {
3092   current_frame->pop_stack_2(
3093     VerificationType::long2_type(),
3094     VerificationType::long_type(), CHECK_VERIFY(this));
3095   current_frame->set_local_2(
3096     index, VerificationType::long_type(),
3097     VerificationType::long2_type(), CHECK_VERIFY(this));
3098 }
3099 
3100 void ClassVerifier::verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS) {
3101   current_frame->pop_stack(VerificationType::float_type(), CHECK_VERIFY(this));
3102   current_frame->set_local(
3103     index, VerificationType::float_type(), CHECK_VERIFY(this));
3104 }
3105 
3106 void ClassVerifier::verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS) {
3107   current_frame->pop_stack_2(
3108     VerificationType::double2_type(),
3109     VerificationType::double_type(), CHECK_VERIFY(this));
3110   current_frame->set_local_2(
3111     index, VerificationType::double_type(),
3112     VerificationType::double2_type(), CHECK_VERIFY(this));
3113 }
3114 
3115 void ClassVerifier::verify_astore(u2 index, StackMapFrame* current_frame, TRAPS) {
3116   VerificationType type = current_frame->pop_stack(
3117     VerificationType::reference_check(), CHECK_VERIFY(this));
3118   current_frame->set_local(index, type, CHECK_VERIFY(this));
3119 }
3120 
3121 void ClassVerifier::verify_iinc(u2 index, StackMapFrame* current_frame, TRAPS) {
3122   VerificationType type = current_frame->get_local(
3123     index, VerificationType::integer_type(), CHECK_VERIFY(this));
3124   current_frame->set_local(index, type, CHECK_VERIFY(this));
3125 }
3126 
3127 void ClassVerifier::verify_return_value(
3128     VerificationType return_type, VerificationType type, u2 bci,
3129     StackMapFrame* current_frame, TRAPS) {
3130   if (return_type == VerificationType::bogus_type()) {
3131     verify_error(ErrorContext::bad_type(bci,
3132         current_frame->stack_top_ctx(), TypeOrigin::signature(return_type)),
3133         "Method expects a return value");
3134     return;
3135   }
3136   bool match = return_type.is_assignable_from(type, this, false, CHECK_VERIFY(this));
3137   if (!match) {
3138     verify_error(ErrorContext::bad_type(bci,
3139         current_frame->stack_top_ctx(), TypeOrigin::signature(return_type)),
3140         "Bad return type");
3141     return;
3142   }
3143 }
3144 
3145 // The verifier creates symbols which are substrings of Symbols.
3146 // These are stored in the verifier until the end of verification so that
3147 // they can be reference counted.
3148 Symbol* ClassVerifier::create_temporary_symbol(const Symbol *s, int begin,
3149                                                int end, TRAPS) {
3150   const char* name = (const char*)s->base() + begin;
3151   int length = end - begin;
3152   return create_temporary_symbol(name, length, CHECK_NULL);
3153 }
3154 
3155 Symbol* ClassVerifier::create_temporary_symbol(const char *name, int length, TRAPS) {
3156   // Quick deduplication check
3157   if (_previous_symbol != NULL && _previous_symbol->equals(name, length)) {
3158     return _previous_symbol;
3159   }
3160   Symbol* sym = SymbolTable::new_symbol(name, length, CHECK_NULL);
3161   if (!sym->is_permanent()) {
3162     if (_symbols == NULL) {
3163       _symbols = new GrowableArray<Symbol*>(50, 0, NULL);
3164     }
3165     _symbols->push(sym);
3166   }
3167   _previous_symbol = sym;
3168   return sym;
3169 }
--- EOF ---