< prev index next >

src/hotspot/share/classfile/verifier.cpp

Print this page

  26 #include "classfile/classFileStream.hpp"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/stackMapTable.hpp"
  30 #include "classfile/stackMapFrame.hpp"
  31 #include "classfile/stackMapTableFormat.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/verifier.hpp"
  35 #include "classfile/vmClasses.hpp"
  36 #include "classfile/vmSymbols.hpp"
  37 #include "interpreter/bytecodes.hpp"
  38 #include "interpreter/bytecodeStream.hpp"
  39 #include "jvm.h"
  40 #include "logging/log.hpp"
  41 #include "logging/logStream.hpp"
  42 #include "memory/oopFactory.hpp"
  43 #include "memory/resourceArea.hpp"
  44 #include "memory/universe.hpp"
  45 #include "oops/constantPool.inline.hpp"

  46 #include "oops/instanceKlass.inline.hpp"
  47 #include "oops/klass.inline.hpp"
  48 #include "oops/oop.inline.hpp"
  49 #include "oops/typeArrayOop.hpp"
  50 #include "runtime/arguments.hpp"
  51 #include "runtime/fieldDescriptor.hpp"
  52 #include "runtime/handles.inline.hpp"
  53 #include "runtime/interfaceSupport.inline.hpp"
  54 #include "runtime/javaCalls.hpp"
  55 #include "runtime/javaThread.hpp"
  56 #include "runtime/jniHandles.inline.hpp"
  57 #include "runtime/os.hpp"
  58 #include "runtime/safepointVerifiers.hpp"
  59 #include "services/threadService.hpp"
  60 #include "utilities/align.hpp"
  61 #include "utilities/bytes.hpp"
  62 #if INCLUDE_CDS
  63 #include "classfile/systemDictionaryShared.hpp"
  64 #endif
  65 
  66 #define NOFAILOVER_MAJOR_VERSION                       51
  67 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION  51
  68 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION       52

  69 #define MAX_ARRAY_DIMENSIONS 255
  70 
  71 // Access to external entry for VerifyClassForMajorVersion - old byte code verifier
  72 
  73 extern "C" {
  74   typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint, jint);
  75 }
  76 
  77 static verify_byte_codes_fn_t volatile _verify_byte_codes_fn = nullptr;
  78 
  79 static verify_byte_codes_fn_t verify_byte_codes_fn() {
  80 
  81   if (_verify_byte_codes_fn != nullptr)
  82     return _verify_byte_codes_fn;
  83 
  84   MutexLocker locker(Verify_lock);
  85 
  86   if (_verify_byte_codes_fn != nullptr)
  87     return _verify_byte_codes_fn;
  88 

 462         _expected.details(ss);
 463       } else {
 464         ss->print("Invalid type: ");
 465         _type.details(ss);
 466       }
 467       break;
 468     case FLAGS_MISMATCH:
 469       if (_expected.is_valid()) {
 470         ss->print("Current frame's flags are not assignable "
 471                   "to stack map frame's.");
 472       } else {
 473         ss->print("Current frame's flags are invalid in this context.");
 474       }
 475       break;
 476     case BAD_CP_INDEX:
 477       ss->print("Constant pool index %d is invalid", _type.index());
 478       break;
 479     case BAD_LOCAL_INDEX:
 480       ss->print("Local index %d is invalid", _type.index());
 481       break;



 482     case LOCALS_SIZE_MISMATCH:
 483       ss->print("Current frame's local size doesn't match stackmap.");
 484       break;
 485     case STACK_SIZE_MISMATCH:
 486       ss->print("Current frame's stack size doesn't match stackmap.");
 487       break;
 488     case STACK_OVERFLOW:
 489       ss->print("Exceeded max stack size.");
 490       break;
 491     case STACK_UNDERFLOW:
 492       ss->print("Attempt to pop empty stack.");
 493       break;
 494     case MISSING_STACKMAP:
 495       ss->print("Expected stackmap frame at this location.");
 496       break;
 497     case BAD_STACKMAP:
 498       ss->print("Invalid stackmap specification.");
 499       break;







 500     case UNKNOWN:
 501     default:
 502       ShouldNotReachHere();
 503       ss->print_cr("Unknown");
 504   }
 505   ss->cr();
 506 }
 507 
 508 void ErrorContext::location_details(outputStream* ss, const Method* method) const {
 509   if (_bci != -1 && method != nullptr) {
 510     streamIndentor si(ss);
 511     const char* bytecode_name = "<invalid>";
 512     if (method->validate_bci(_bci) != -1) {
 513       Bytecodes::Code code = Bytecodes::code_or_bp_at(method->bcp_from(_bci));
 514       if (Bytecodes::is_defined(code)) {
 515           bytecode_name = Bytecodes::name(code);
 516       } else {
 517           bytecode_name = "<illegal>";
 518       }
 519     }

 600 ClassVerifier::~ClassVerifier() {
 601   // Decrement the reference count for any symbols created.
 602   if (_symbols != nullptr) {
 603     for (int i = 0; i < _symbols->length(); i++) {
 604       Symbol* s = _symbols->at(i);
 605       s->decrement_refcount();
 606     }
 607   }
 608 }
 609 
 610 VerificationType ClassVerifier::object_type() const {
 611   return VerificationType::reference_type(vmSymbols::java_lang_Object());
 612 }
 613 
 614 TypeOrigin ClassVerifier::ref_ctx(const char* sig) {
 615   VerificationType vt = VerificationType::reference_type(
 616                          create_temporary_symbol(sig, (int)strlen(sig)));
 617   return TypeOrigin::implicit(vt);
 618 }
 619 





 620 
 621 void ClassVerifier::verify_class(TRAPS) {
 622   log_info(verification)("Verifying class %s with new format", _klass->external_name());
 623 
 624   // Either verifying both local and remote classes or just remote classes.
 625   assert(BytecodeVerificationRemote, "Should not be here");
 626 
 627   Array<Method*>* methods = _klass->methods();
 628   int num_methods = methods->length();
 629 
 630   for (int index = 0; index < num_methods; index++) {
 631     // Check for recursive re-verification before each method.
 632     if (was_recursively_verified()) return;
 633 
 634     Method* m = methods->at(index);
 635     if (m->is_native() || m->is_abstract() || m->is_overpass()) {
 636       // If m is native or abstract, skip it.  It is checked in class file
 637       // parser that methods do not override a final method.  Overpass methods
 638       // are trusted since the VM generates them.
 639       continue;

 695   bool is_unique = method_signatures_table()->put(sig_index, sig_verif_types);
 696   assert(is_unique, "Duplicate entries in method_signature_table");
 697 }
 698 
 699 void ClassVerifier::verify_method(const methodHandle& m, TRAPS) {
 700   HandleMark hm(THREAD);
 701   _method = m;   // initialize _method
 702   log_info(verification)("Verifying method %s", m->name_and_sig_as_C_string());
 703 
 704 // For clang, the only good constant format string is a literal constant format string.
 705 #define bad_type_msg "Bad type on operand stack in %s"
 706 
 707   u2 max_stack = m->verifier_max_stack();
 708   u2 max_locals = m->max_locals();
 709   constantPoolHandle cp(THREAD, m->constants());
 710 
 711   // Method signature was checked in ClassFileParser.
 712   assert(SignatureVerifier::is_valid_method_signature(m->signature()),
 713          "Invalid method signature");
 714 















 715   // Initial stack map frame: offset is 0, stack is initially empty.
 716   StackMapFrame current_frame(max_locals, max_stack, this);
 717   // Set initial locals
 718   VerificationType return_type = current_frame.set_locals_from_arg( m, current_type());
 719 
 720   u2 stackmap_index = 0; // index to the stackmap array
 721 
 722   u4 code_length = m->code_size();
 723 
 724   // Scan the bytecode and map each instruction's start offset to a number.
 725   char* code_data = generate_code_data(m, code_length, CHECK_VERIFY(this));
 726 
 727   int ex_min = code_length;
 728   int ex_max = -1;
 729   // Look through each item on the exception table. Each of the fields must refer
 730   // to a legal instruction.
 731   if (was_recursively_verified()) return;
 732   verify_exception_handler_table(
 733     code_length, code_data, ex_min, ex_max, CHECK_VERIFY(this));
 734 
 735   // Look through each entry on the local variable table and make sure
 736   // its range of code array offsets is valid. (4169817)
 737   if (m->has_localvariable_table()) {
 738     verify_local_variable_table(code_length, code_data, CHECK_VERIFY(this));
 739   }
 740 
 741   Array<u1>* stackmap_data = m->stackmap_data();
 742   StackMapStream stream(stackmap_data);
 743   StackMapReader reader(this, &stream, code_data, code_length, &current_frame, max_locals, max_stack, THREAD);
 744   StackMapTable stackmap_table(&reader, CHECK_VERIFY(this));
 745 
 746   LogTarget(Debug, verification) lt;
 747   if (lt.is_enabled()) {
 748     LogStream ls(lt);
 749     stackmap_table.print_on(&ls);
 750   }
 751 
 752   RawBytecodeStream bcs(m);
 753 
 754   // Scan the byte code linearly from the start to the end
 755   bool no_control_flow = false; // Set to true when there is no direct control
 756                                 // flow from current instruction to the next
 757                                 // instruction in sequence
 758 
 759   Bytecodes::Code opcode;
 760   while (!bcs.is_last_bytecode()) {
 761     // Check for recursive re-verification before each bytecode.
 762     if (was_recursively_verified())  return;
 763 

1664             VerificationType::double2_type(),  CHECK_VERIFY(this));
1665           type = current_frame.pop_stack(
1666             VerificationType::double_type(), CHECK_VERIFY(this));
1667           verify_return_value(return_type, type, bci,
1668                               &current_frame, CHECK_VERIFY(this));
1669           no_control_flow = true; break;
1670         case Bytecodes::_areturn :
1671           type = current_frame.pop_stack(
1672             VerificationType::reference_check(), CHECK_VERIFY(this));
1673           verify_return_value(return_type, type, bci,
1674                               &current_frame, CHECK_VERIFY(this));
1675           no_control_flow = true; break;
1676         case Bytecodes::_return :
1677           if (return_type != VerificationType::bogus_type()) {
1678             verify_error(ErrorContext::bad_code(bci),
1679                          "Method expects a return value");
1680             return;
1681           }
1682           // Make sure "this" has been initialized if current method is an
1683           // <init>.
1684           if (_method->name() == vmSymbols::object_initializer_name() &&
1685               current_frame.flag_this_uninit()) {
1686             verify_error(ErrorContext::bad_code(bci),
1687                          "Constructor must call super() or this() "
1688                          "before return");
1689             return;
1690           }
1691           no_control_flow = true; break;
1692         case Bytecodes::_getstatic :
1693         case Bytecodes::_putstatic :
1694           // pass TRUE, operand can be an array type for getstatic/putstatic.
1695           verify_field_instructions(
1696             &bcs, &current_frame, cp, true, CHECK_VERIFY(this));
1697           no_control_flow = false; break;
1698         case Bytecodes::_getfield :
1699         case Bytecodes::_putfield :
1700           // pass FALSE, operand can't be an array type for getfield/putfield.
1701           verify_field_instructions(
1702             &bcs, &current_frame, cp, false, CHECK_VERIFY(this));
1703           no_control_flow = false; break;
1704         case Bytecodes::_invokevirtual :
1705         case Bytecodes::_invokespecial :
1706         case Bytecodes::_invokestatic :
1707           verify_invoke_instructions(
1708             &bcs, code_length, &current_frame, (bci >= ex_min && bci < ex_max),
1709             &this_uninit, return_type, cp, &stackmap_table, CHECK_VERIFY(this));
1710           no_control_flow = false; break;
1711         case Bytecodes::_invokeinterface :
1712         case Bytecodes::_invokedynamic :
1713           verify_invoke_instructions(
1714             &bcs, code_length, &current_frame, (bci >= ex_min && bci < ex_max),
1715             &this_uninit, return_type, cp, &stackmap_table, CHECK_VERIFY(this));
1716           no_control_flow = false; break;
1717         case Bytecodes::_new :
1718         {
1719           u2 index = bcs.get_index_u2();
1720           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1721           VerificationType new_class_type =
1722             cp_index_to_type(index, cp, CHECK_VERIFY(this));
1723           if (!new_class_type.is_object()) {
1724             verify_error(ErrorContext::bad_type(bci,
1725                 TypeOrigin::cp(index, new_class_type)),
1726                 "Illegal new instruction");
1727             return;
1728           }
1729           type = VerificationType::uninitialized_type(checked_cast<u2>(bci));
1730           current_frame.push_stack(type, CHECK_VERIFY(this));
1731           no_control_flow = false; break;
1732         }
1733         case Bytecodes::_newarray :
1734           type = get_newarray_type(bcs.get_index(), bci, CHECK_VERIFY(this));
1735           current_frame.pop_stack(

1753           no_control_flow = false; break;
1754         case Bytecodes::_checkcast :
1755         {
1756           u2 index = bcs.get_index_u2();
1757           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1758           current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1759           VerificationType klass_type = cp_index_to_type(
1760             index, cp, CHECK_VERIFY(this));
1761           current_frame.push_stack(klass_type, CHECK_VERIFY(this));
1762           no_control_flow = false; break;
1763         }
1764         case Bytecodes::_instanceof : {
1765           u2 index = bcs.get_index_u2();
1766           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1767           current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1768           current_frame.push_stack(
1769             VerificationType::integer_type(), CHECK_VERIFY(this));
1770           no_control_flow = false; break;
1771         }
1772         case Bytecodes::_monitorenter :
1773         case Bytecodes::_monitorexit :
1774           current_frame.pop_stack(
1775             VerificationType::reference_check(), CHECK_VERIFY(this));
1776           no_control_flow = false; break;

1777         case Bytecodes::_multianewarray :
1778         {
1779           u2 index = bcs.get_index_u2();
1780           u2 dim = *(bcs.bcp()+3);
1781           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1782           VerificationType new_array_type =
1783             cp_index_to_type(index, cp, CHECK_VERIFY(this));
1784           if (!new_array_type.is_array()) {
1785             verify_error(ErrorContext::bad_type(bci,
1786                 TypeOrigin::cp(index, new_array_type)),
1787                 "Illegal constant pool index in multianewarray instruction");
1788             return;
1789           }
1790           if (dim < 1 || new_array_type.dimensions() < dim) {
1791             verify_error(ErrorContext::bad_code(bci),
1792                 "Illegal dimension in multianewarray instruction: %d", dim);
1793             return;
1794           }
1795           for (int i = 0; i < dim; i++) {
1796             current_frame.pop_stack(

2134   } else {
2135     Klass* member_klass = target_instance->find_field(field_name, field_sig, &fd);
2136     if (member_klass != nullptr && fd.is_protected()) {
2137       if (!this_class->is_same_class_package(member_klass)) {
2138         return true;
2139       }
2140     }
2141   }
2142   return false;
2143 }
2144 
2145 void ClassVerifier::verify_ldc(
2146     int opcode, u2 index, StackMapFrame* current_frame,
2147     const constantPoolHandle& cp, int bci, TRAPS) {
2148   verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
2149   constantTag tag = cp->tag_at(index);
2150   unsigned int types = 0;
2151   if (opcode == Bytecodes::_ldc || opcode == Bytecodes::_ldc_w) {
2152     if (!tag.is_unresolved_klass()) {
2153       types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float)
2154             | (1 << JVM_CONSTANT_String)  | (1 << JVM_CONSTANT_Class)
2155             | (1 << JVM_CONSTANT_MethodHandle) | (1 << JVM_CONSTANT_MethodType)
2156             | (1 << JVM_CONSTANT_Dynamic);
2157       // Note:  The class file parser already verified the legality of
2158       // MethodHandle and MethodType constants.
2159       verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2160     }
2161   } else {
2162     assert(opcode == Bytecodes::_ldc2_w, "must be ldc2_w");
2163     types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long)
2164           | (1 << JVM_CONSTANT_Dynamic);
2165     verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2166   }
2167   if (tag.is_string()) {
2168     current_frame->push_stack(
2169       VerificationType::reference_type(
2170         vmSymbols::java_lang_String()), CHECK_VERIFY(this));
2171   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
2172     current_frame->push_stack(
2173       VerificationType::reference_type(
2174         vmSymbols::java_lang_Class()), CHECK_VERIFY(this));

2310                                               const constantPoolHandle& cp,
2311                                               bool allow_arrays,
2312                                               TRAPS) {
2313   u2 index = bcs->get_index_u2();
2314   verify_cp_type(bcs->bci(), index, cp,
2315       1 << JVM_CONSTANT_Fieldref, CHECK_VERIFY(this));
2316 
2317   // Get field name and signature
2318   Symbol* field_name = cp->uncached_name_ref_at(index);
2319   Symbol* field_sig = cp->uncached_signature_ref_at(index);
2320   bool is_getfield = false;
2321 
2322   // Field signature was checked in ClassFileParser.
2323   assert(SignatureVerifier::is_valid_type_signature(field_sig),
2324          "Invalid field signature");
2325 
2326   // Get referenced class type
2327   VerificationType ref_class_type = cp_ref_index_to_type(
2328     index, cp, CHECK_VERIFY(this));
2329   if (!ref_class_type.is_object() &&
2330     (!allow_arrays || !ref_class_type.is_array())) {
2331     verify_error(ErrorContext::bad_type(bcs->bci(),
2332         TypeOrigin::cp(index, ref_class_type)),
2333         "Expecting reference to class in class %s at constant pool index %d",
2334         _klass->external_name(), index);
2335     return;
2336   }

2337   VerificationType target_class_type = ref_class_type;
2338 
2339   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2340         "buffer type must match VerificationType size");
2341   uintptr_t field_type_buffer[2];
2342   VerificationType* field_type = (VerificationType*)field_type_buffer;
2343   // If we make a VerificationType[2] array directly, the compiler calls
2344   // to the c-runtime library to do the allocation instead of just
2345   // stack allocating it.  Plus it would run constructors.  This shows up
2346   // in performance profiles.
2347 
2348   SignatureStream sig_stream(field_sig, false);
2349   VerificationType stack_object_type;
2350   int n = change_sig_to_verificationType(&sig_stream, field_type);
2351   int bci = bcs->bci();
2352   bool is_assignable;
2353   switch (bcs->raw_code()) {
2354     case Bytecodes::_getstatic: {
2355       for (int i = 0; i < n; i++) {
2356         current_frame->push_stack(field_type[i], CHECK_VERIFY(this));

2358       break;
2359     }
2360     case Bytecodes::_putstatic: {
2361       for (int i = n - 1; i >= 0; i--) {
2362         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2363       }
2364       break;
2365     }
2366     case Bytecodes::_getfield: {
2367       is_getfield = true;
2368       stack_object_type = current_frame->pop_stack(
2369         target_class_type, CHECK_VERIFY(this));
2370       goto check_protected;
2371     }
2372     case Bytecodes::_putfield: {
2373       for (int i = n - 1; i >= 0; i--) {
2374         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2375       }
2376       stack_object_type = current_frame->pop_stack(CHECK_VERIFY(this));
2377 
2378       // The JVMS 2nd edition allows field initialization before the superclass
2379       // initializer, if the field is defined within the current class.
2380       fieldDescriptor fd;
2381       if (stack_object_type == VerificationType::uninitialized_this_type() &&
2382           target_class_type.equals(current_type()) &&
2383           _klass->find_local_field(field_name, field_sig, &fd)) {
2384         stack_object_type = current_type();




















2385       }
2386       is_assignable = target_class_type.is_assignable_from(
2387         stack_object_type, this, false, CHECK_VERIFY(this));
2388       if (!is_assignable) {
2389         verify_error(ErrorContext::bad_type(bci,
2390             current_frame->stack_top_ctx(),
2391             TypeOrigin::cp(index, target_class_type)),
2392             "Bad type on operand stack in putfield");
2393         return;
2394       }
2395     }
2396     check_protected: {
2397       if (_this_type == stack_object_type)
2398         break; // stack_object_type must be assignable to _current_class_type
2399       if (was_recursively_verified()) {
2400         if (is_getfield) {
2401           // Push field type for getfield.
2402           for (int i = 0; i < n; i++) {
2403             current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
2404           }

2646 }
2647 
2648 void ClassVerifier::verify_invoke_init(
2649     RawBytecodeStream* bcs, u2 ref_class_index, VerificationType ref_class_type,
2650     StackMapFrame* current_frame, u4 code_length, bool in_try_block,
2651     bool *this_uninit, const constantPoolHandle& cp, StackMapTable* stackmap_table,
2652     TRAPS) {
2653   int bci = bcs->bci();
2654   VerificationType type = current_frame->pop_stack(
2655     VerificationType::reference_check(), CHECK_VERIFY(this));
2656   if (type == VerificationType::uninitialized_this_type()) {
2657     // The method must be an <init> method of this class or its superclass
2658     Klass* superk = current_class()->super();
2659     if (ref_class_type.name() != current_class()->name() &&
2660         ref_class_type.name() != superk->name()) {
2661       verify_error(ErrorContext::bad_type(bci,
2662           TypeOrigin::implicit(ref_class_type),
2663           TypeOrigin::implicit(current_type())),
2664           "Bad <init> method call");
2665       return;







2666     }
2667 
2668     // If this invokespecial call is done from inside of a TRY block then make
2669     // sure that all catch clause paths end in a throw.  Otherwise, this can
2670     // result in returning an incomplete object.
2671     if (in_try_block) {
2672       ExceptionTable exhandlers(_method());
2673       int exlength = exhandlers.length();
2674       for(int i = 0; i < exlength; i++) {
2675         u2 start_pc = exhandlers.start_pc(i);
2676         u2 end_pc = exhandlers.end_pc(i);
2677 
2678         if (bci >= start_pc && bci < end_pc) {
2679           if (!ends_in_athrow(exhandlers.handler_pc(i))) {
2680             verify_error(ErrorContext::bad_code(bci),
2681               "Bad <init> method call from after the start of a try block");
2682             return;
2683           } else if (log_is_enabled(Debug, verification)) {
2684             ResourceMark rm(THREAD);
2685             log_debug(verification)("Survived call to ends_in_athrow(): %s",

2769 bool ClassVerifier::is_same_or_direct_interface(
2770     InstanceKlass* klass,
2771     VerificationType klass_type,
2772     VerificationType ref_class_type) {
2773   if (ref_class_type.equals(klass_type)) return true;
2774   Array<InstanceKlass*>* local_interfaces = klass->local_interfaces();
2775   if (local_interfaces != nullptr) {
2776     for (int x = 0; x < local_interfaces->length(); x++) {
2777       InstanceKlass* k = local_interfaces->at(x);
2778       assert (k != nullptr && k->is_interface(), "invalid interface");
2779       if (ref_class_type.equals(VerificationType::reference_type(k->name()))) {
2780         return true;
2781       }
2782     }
2783   }
2784   return false;
2785 }
2786 
2787 void ClassVerifier::verify_invoke_instructions(
2788     RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
2789     bool in_try_block, bool *this_uninit, VerificationType return_type,
2790     const constantPoolHandle& cp, StackMapTable* stackmap_table, TRAPS) {
2791   // Make sure the constant pool item is the right type
2792   u2 index = bcs->get_index_u2();
2793   Bytecodes::Code opcode = bcs->raw_code();
2794   unsigned int types = 0;
2795   switch (opcode) {
2796     case Bytecodes::_invokeinterface:
2797       types = 1 << JVM_CONSTANT_InterfaceMethodref;
2798       break;
2799     case Bytecodes::_invokedynamic:
2800       types = 1 << JVM_CONSTANT_InvokeDynamic;
2801       break;
2802     case Bytecodes::_invokespecial:
2803     case Bytecodes::_invokestatic:
2804       types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ?
2805         (1 << JVM_CONSTANT_Methodref) :
2806         ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref));
2807       break;
2808     default:
2809       types = 1 << JVM_CONSTANT_Methodref;
2810   }
2811   verify_cp_type(bcs->bci(), index, cp, types, CHECK_VERIFY(this));
2812 
2813   // Get method name and signature
2814   Symbol* method_name = cp->uncached_name_ref_at(index);
2815   Symbol* method_sig = cp->uncached_signature_ref_at(index);
2816 
2817   // Method signature was checked in ClassFileParser.
2818   assert(SignatureVerifier::is_valid_method_signature(method_sig),
2819          "Invalid method signature");
2820 
2821   // Get referenced class type
2822   VerificationType ref_class_type;
2823   if (opcode == Bytecodes::_invokedynamic) {
2824     if (_klass->major_version() < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2825       class_format_error(
2826         "invokedynamic instructions not supported by this class file version (%d), class %s",
2827         _klass->major_version(), _klass->external_name());
2828       return;
2829     }
2830   } else {
2831     ref_class_type = cp_ref_index_to_type(index, cp, CHECK_VERIFY(this));
2832   }
2833 
2834   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2835         "buffer type must match VerificationType size");
2836 
2837   // Get the UTF8 index for this signature.
2838   int sig_index = cp->signature_ref_index_at(cp->uncached_name_and_type_ref_index_at(index));
2839 
2840   // Get the signature's verification types.
2841   sig_as_verification_types* mth_sig_verif_types;

2867           "Inconsistent args count operand in invokeinterface");
2868       return;
2869     }
2870     if (*(bcp+4) != 0) {
2871       verify_error(ErrorContext::bad_code(bci),
2872           "Fourth operand byte of invokeinterface must be zero");
2873       return;
2874     }
2875   }
2876 
2877   if (opcode == Bytecodes::_invokedynamic) {
2878     address bcp = bcs->bcp();
2879     if (*(bcp+3) != 0 || *(bcp+4) != 0) {
2880       verify_error(ErrorContext::bad_code(bci),
2881           "Third and fourth operand bytes of invokedynamic must be zero");
2882       return;
2883     }
2884   }
2885 
2886   if (method_name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
2887     // Make sure <init> can only be invoked by invokespecial

2888     if (opcode != Bytecodes::_invokespecial ||
2889         method_name != vmSymbols::object_initializer_name()) {
2890       verify_error(ErrorContext::bad_code(bci),
2891           "Illegal call to internal method");
2892       return;
2893     }
2894   } else if (opcode == Bytecodes::_invokespecial
2895              && !is_same_or_direct_interface(current_class(), current_type(), ref_class_type)
2896              && !ref_class_type.equals(VerificationType::reference_type(
2897                   current_class()->super()->name()))) {
2898     bool subtype = false;
2899     bool have_imr_indirect = cp->tag_at(index).value() == JVM_CONSTANT_InterfaceMethodref;
2900     subtype = ref_class_type.is_assignable_from(
2901                current_type(), this, false, CHECK_VERIFY(this));
2902     if (!subtype) {
2903       verify_error(ErrorContext::bad_code(bci),
2904           "Bad invokespecial instruction: "
2905           "current class isn't assignable to reference class.");
2906        return;
2907     } else if (have_imr_indirect) {
2908       verify_error(ErrorContext::bad_code(bci),
2909           "Bad invokespecial instruction: "
2910           "interface method reference is in an indirect superinterface.");
2911       return;
2912     }
2913 
2914   }
2915 
2916   // Get the verification types for the method's arguments.
2917   GrowableArray<VerificationType>* sig_verif_types = mth_sig_verif_types->sig_verif_types();

2962                   verify_error(ErrorContext::bad_type(bci,
2963                       current_frame->stack_top_ctx(),
2964                       TypeOrigin::implicit(current_type())),
2965                       "Bad access to protected data in invokevirtual");
2966                   return;
2967                 }
2968               }
2969             }
2970           }
2971         }
2972       } else {
2973         assert(opcode == Bytecodes::_invokeinterface, "Unexpected opcode encountered");
2974         current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this));
2975       }
2976     }
2977   }
2978   // Push the result type.
2979   int sig_verif_types_len = sig_verif_types->length();
2980   if (sig_verif_types_len > nargs) {  // There's a return type
2981     if (method_name == vmSymbols::object_initializer_name()) {
2982       // <init> method must have a void return type
2983       /* Unreachable?  Class file parser verifies that methods with '<' have
2984        * void return */
2985       verify_error(ErrorContext::bad_code(bci),
2986           "Return type must be void in <init> method");
2987       return;
2988     }
2989 
2990     assert(sig_verif_types_len <= nargs + 2,
2991            "Signature verification types array return type is bogus");
2992     for (int i = nargs; i < sig_verif_types_len; i++) {
2993       assert(i == nargs || sig_verif_types->at(i).is_long2() ||
2994              sig_verif_types->at(i).is_double2(), "Unexpected return verificationType");
2995       current_frame->push_stack(sig_verif_types->at(i), CHECK_VERIFY(this));
2996     }
2997   }
2998 }
2999 
3000 VerificationType ClassVerifier::get_newarray_type(
3001     u2 index, int bci, TRAPS) {
3002   const char* from_bt[] = {
3003     nullptr, nullptr, nullptr, nullptr, "[Z", "[C", "[F", "[D", "[B", "[S", "[I", "[J",
3004   };

  26 #include "classfile/classFileStream.hpp"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/stackMapTable.hpp"
  30 #include "classfile/stackMapFrame.hpp"
  31 #include "classfile/stackMapTableFormat.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/verifier.hpp"
  35 #include "classfile/vmClasses.hpp"
  36 #include "classfile/vmSymbols.hpp"
  37 #include "interpreter/bytecodes.hpp"
  38 #include "interpreter/bytecodeStream.hpp"
  39 #include "jvm.h"
  40 #include "logging/log.hpp"
  41 #include "logging/logStream.hpp"
  42 #include "memory/oopFactory.hpp"
  43 #include "memory/resourceArea.hpp"
  44 #include "memory/universe.hpp"
  45 #include "oops/constantPool.inline.hpp"
  46 #include "oops/fieldStreams.inline.hpp"
  47 #include "oops/instanceKlass.inline.hpp"
  48 #include "oops/klass.inline.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "oops/typeArrayOop.hpp"
  51 #include "runtime/arguments.hpp"
  52 #include "runtime/fieldDescriptor.inline.hpp"
  53 #include "runtime/handles.inline.hpp"
  54 #include "runtime/interfaceSupport.inline.hpp"
  55 #include "runtime/javaCalls.hpp"
  56 #include "runtime/javaThread.hpp"
  57 #include "runtime/jniHandles.inline.hpp"
  58 #include "runtime/os.hpp"
  59 #include "runtime/safepointVerifiers.hpp"
  60 #include "services/threadService.hpp"
  61 #include "utilities/align.hpp"
  62 #include "utilities/bytes.hpp"
  63 #if INCLUDE_CDS
  64 #include "classfile/systemDictionaryShared.hpp"
  65 #endif
  66 
  67 #define NOFAILOVER_MAJOR_VERSION                       51
  68 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION  51
  69 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION       52
  70 #define INLINE_TYPE_MAJOR_VERSION                       56
  71 #define MAX_ARRAY_DIMENSIONS 255
  72 
  73 // Access to external entry for VerifyClassForMajorVersion - old byte code verifier
  74 
  75 extern "C" {
  76   typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint, jint);
  77 }
  78 
  79 static verify_byte_codes_fn_t volatile _verify_byte_codes_fn = nullptr;
  80 
  81 static verify_byte_codes_fn_t verify_byte_codes_fn() {
  82 
  83   if (_verify_byte_codes_fn != nullptr)
  84     return _verify_byte_codes_fn;
  85 
  86   MutexLocker locker(Verify_lock);
  87 
  88   if (_verify_byte_codes_fn != nullptr)
  89     return _verify_byte_codes_fn;
  90 

 464         _expected.details(ss);
 465       } else {
 466         ss->print("Invalid type: ");
 467         _type.details(ss);
 468       }
 469       break;
 470     case FLAGS_MISMATCH:
 471       if (_expected.is_valid()) {
 472         ss->print("Current frame's flags are not assignable "
 473                   "to stack map frame's.");
 474       } else {
 475         ss->print("Current frame's flags are invalid in this context.");
 476       }
 477       break;
 478     case BAD_CP_INDEX:
 479       ss->print("Constant pool index %d is invalid", _type.index());
 480       break;
 481     case BAD_LOCAL_INDEX:
 482       ss->print("Local index %d is invalid", _type.index());
 483       break;
 484     case BAD_STRICT_FIELDS:
 485       ss->print("Invalid use of strict instance fields");
 486       break;
 487     case LOCALS_SIZE_MISMATCH:
 488       ss->print("Current frame's local size doesn't match stackmap.");
 489       break;
 490     case STACK_SIZE_MISMATCH:
 491       ss->print("Current frame's stack size doesn't match stackmap.");
 492       break;
 493     case STACK_OVERFLOW:
 494       ss->print("Exceeded max stack size.");
 495       break;
 496     case STACK_UNDERFLOW:
 497       ss->print("Attempt to pop empty stack.");
 498       break;
 499     case MISSING_STACKMAP:
 500       ss->print("Expected stackmap frame at this location.");
 501       break;
 502     case BAD_STACKMAP:
 503       ss->print("Invalid stackmap specification.");
 504       break;
 505     case WRONG_INLINE_TYPE:
 506       ss->print("Type ");
 507       _type.details(ss);
 508       ss->print(" and type ");
 509       _expected.details(ss);
 510       ss->print(" must be identical inline types.");
 511       break;
 512     case UNKNOWN:
 513     default:
 514       ShouldNotReachHere();
 515       ss->print_cr("Unknown");
 516   }
 517   ss->cr();
 518 }
 519 
 520 void ErrorContext::location_details(outputStream* ss, const Method* method) const {
 521   if (_bci != -1 && method != nullptr) {
 522     streamIndentor si(ss);
 523     const char* bytecode_name = "<invalid>";
 524     if (method->validate_bci(_bci) != -1) {
 525       Bytecodes::Code code = Bytecodes::code_or_bp_at(method->bcp_from(_bci));
 526       if (Bytecodes::is_defined(code)) {
 527           bytecode_name = Bytecodes::name(code);
 528       } else {
 529           bytecode_name = "<illegal>";
 530       }
 531     }

 612 ClassVerifier::~ClassVerifier() {
 613   // Decrement the reference count for any symbols created.
 614   if (_symbols != nullptr) {
 615     for (int i = 0; i < _symbols->length(); i++) {
 616       Symbol* s = _symbols->at(i);
 617       s->decrement_refcount();
 618     }
 619   }
 620 }
 621 
 622 VerificationType ClassVerifier::object_type() const {
 623   return VerificationType::reference_type(vmSymbols::java_lang_Object());
 624 }
 625 
 626 TypeOrigin ClassVerifier::ref_ctx(const char* sig) {
 627   VerificationType vt = VerificationType::reference_type(
 628                          create_temporary_symbol(sig, (int)strlen(sig)));
 629   return TypeOrigin::implicit(vt);
 630 }
 631 
 632 static bool supports_strict_fields(InstanceKlass* klass) {
 633   int ver = klass->major_version();
 634   return ver > Verifier::VALUE_TYPES_MAJOR_VERSION ||
 635          (ver == Verifier::VALUE_TYPES_MAJOR_VERSION && klass->minor_version() == Verifier::JAVA_PREVIEW_MINOR_VERSION);
 636 }
 637 
 638 void ClassVerifier::verify_class(TRAPS) {
 639   log_info(verification)("Verifying class %s with new format", _klass->external_name());
 640 
 641   // Either verifying both local and remote classes or just remote classes.
 642   assert(BytecodeVerificationRemote, "Should not be here");
 643 
 644   Array<Method*>* methods = _klass->methods();
 645   int num_methods = methods->length();
 646 
 647   for (int index = 0; index < num_methods; index++) {
 648     // Check for recursive re-verification before each method.
 649     if (was_recursively_verified()) return;
 650 
 651     Method* m = methods->at(index);
 652     if (m->is_native() || m->is_abstract() || m->is_overpass()) {
 653       // If m is native or abstract, skip it.  It is checked in class file
 654       // parser that methods do not override a final method.  Overpass methods
 655       // are trusted since the VM generates them.
 656       continue;

 712   bool is_unique = method_signatures_table()->put(sig_index, sig_verif_types);
 713   assert(is_unique, "Duplicate entries in method_signature_table");
 714 }
 715 
 716 void ClassVerifier::verify_method(const methodHandle& m, TRAPS) {
 717   HandleMark hm(THREAD);
 718   _method = m;   // initialize _method
 719   log_info(verification)("Verifying method %s", m->name_and_sig_as_C_string());
 720 
 721 // For clang, the only good constant format string is a literal constant format string.
 722 #define bad_type_msg "Bad type on operand stack in %s"
 723 
 724   u2 max_stack = m->verifier_max_stack();
 725   u2 max_locals = m->max_locals();
 726   constantPoolHandle cp(THREAD, m->constants());
 727 
 728   // Method signature was checked in ClassFileParser.
 729   assert(SignatureVerifier::is_valid_method_signature(m->signature()),
 730          "Invalid method signature");
 731 
 732   // Collect the initial strict instance fields
 733   StackMapFrame::AssertUnsetFieldTable* strict_fields = new StackMapFrame::AssertUnsetFieldTable();
 734   if (m->is_object_constructor()) {
 735     for (AllFieldStream fs(m->method_holder()); !fs.done(); fs.next()) {
 736       if (fs.access_flags().is_strict() && !fs.access_flags().is_static()) {
 737         NameAndSig new_field(fs.name(), fs.signature());
 738         if (IgnoreAssertUnsetFields) {
 739           strict_fields->put(new_field, true);
 740         } else {
 741           strict_fields->put(new_field, false);
 742         }
 743       }
 744     }
 745   }
 746 
 747   // Initial stack map frame: offset is 0, stack is initially empty.
 748   StackMapFrame current_frame(max_locals, max_stack, strict_fields, this);
 749   // Set initial locals
 750   VerificationType return_type = current_frame.set_locals_from_arg( m, current_type());
 751 
 752   u2 stackmap_index = 0; // index to the stackmap array
 753 
 754   u4 code_length = m->code_size();
 755 
 756   // Scan the bytecode and map each instruction's start offset to a number.
 757   char* code_data = generate_code_data(m, code_length, CHECK_VERIFY(this));
 758 
 759   int ex_min = code_length;
 760   int ex_max = -1;
 761   // Look through each item on the exception table. Each of the fields must refer
 762   // to a legal instruction.
 763   if (was_recursively_verified()) return;
 764   verify_exception_handler_table(
 765     code_length, code_data, ex_min, ex_max, CHECK_VERIFY(this));
 766 
 767   // Look through each entry on the local variable table and make sure
 768   // its range of code array offsets is valid. (4169817)
 769   if (m->has_localvariable_table()) {
 770     verify_local_variable_table(code_length, code_data, CHECK_VERIFY(this));
 771   }
 772 
 773   Array<u1>* stackmap_data = m->stackmap_data();
 774   StackMapStream stream(stackmap_data);
 775   StackMapReader reader(this, &stream, code_data, code_length, &current_frame, max_locals, max_stack, strict_fields, THREAD);
 776   StackMapTable stackmap_table(&reader, CHECK_VERIFY(this));
 777 
 778   LogTarget(Debug, verification) lt;
 779   if (lt.is_enabled()) {
 780     LogStream ls(lt);
 781     stackmap_table.print_on(&ls);
 782   }
 783 
 784   RawBytecodeStream bcs(m);
 785 
 786   // Scan the byte code linearly from the start to the end
 787   bool no_control_flow = false; // Set to true when there is no direct control
 788                                 // flow from current instruction to the next
 789                                 // instruction in sequence
 790 
 791   Bytecodes::Code opcode;
 792   while (!bcs.is_last_bytecode()) {
 793     // Check for recursive re-verification before each bytecode.
 794     if (was_recursively_verified())  return;
 795 

1696             VerificationType::double2_type(),  CHECK_VERIFY(this));
1697           type = current_frame.pop_stack(
1698             VerificationType::double_type(), CHECK_VERIFY(this));
1699           verify_return_value(return_type, type, bci,
1700                               &current_frame, CHECK_VERIFY(this));
1701           no_control_flow = true; break;
1702         case Bytecodes::_areturn :
1703           type = current_frame.pop_stack(
1704             VerificationType::reference_check(), CHECK_VERIFY(this));
1705           verify_return_value(return_type, type, bci,
1706                               &current_frame, CHECK_VERIFY(this));
1707           no_control_flow = true; break;
1708         case Bytecodes::_return :
1709           if (return_type != VerificationType::bogus_type()) {
1710             verify_error(ErrorContext::bad_code(bci),
1711                          "Method expects a return value");
1712             return;
1713           }
1714           // Make sure "this" has been initialized if current method is an
1715           // <init>.
1716           if (_method->is_object_constructor() &&
1717               current_frame.flag_this_uninit()) {
1718             verify_error(ErrorContext::bad_code(bci),
1719                          "Constructor must call super() or this() "
1720                          "before return");
1721             return;
1722           }
1723           no_control_flow = true; break;
1724         case Bytecodes::_getstatic :
1725         case Bytecodes::_putstatic :
1726           // pass TRUE, operand can be an array type for getstatic/putstatic.
1727           verify_field_instructions(
1728             &bcs, &current_frame, cp, true, CHECK_VERIFY(this));
1729           no_control_flow = false; break;
1730         case Bytecodes::_getfield :
1731         case Bytecodes::_putfield :
1732           // pass FALSE, operand can't be an array type for getfield/putfield.
1733           verify_field_instructions(
1734             &bcs, &current_frame, cp, false, CHECK_VERIFY(this));
1735           no_control_flow = false; break;
1736         case Bytecodes::_invokevirtual :
1737         case Bytecodes::_invokespecial :
1738         case Bytecodes::_invokestatic :




1739         case Bytecodes::_invokeinterface :
1740         case Bytecodes::_invokedynamic :
1741           verify_invoke_instructions(
1742             &bcs, code_length, &current_frame, (bci >= ex_min && bci < ex_max),
1743             &this_uninit, cp, &stackmap_table, CHECK_VERIFY(this));
1744           no_control_flow = false; break;
1745         case Bytecodes::_new :
1746         {
1747           u2 index = bcs.get_index_u2();
1748           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1749           VerificationType new_class_type =
1750             cp_index_to_type(index, cp, CHECK_VERIFY(this));
1751           if (!new_class_type.is_object()) {
1752             verify_error(ErrorContext::bad_type(bci,
1753                 TypeOrigin::cp(index, new_class_type)),
1754                 "Illegal new instruction");
1755             return;
1756           }
1757           type = VerificationType::uninitialized_type(checked_cast<u2>(bci));
1758           current_frame.push_stack(type, CHECK_VERIFY(this));
1759           no_control_flow = false; break;
1760         }
1761         case Bytecodes::_newarray :
1762           type = get_newarray_type(bcs.get_index(), bci, CHECK_VERIFY(this));
1763           current_frame.pop_stack(

1781           no_control_flow = false; break;
1782         case Bytecodes::_checkcast :
1783         {
1784           u2 index = bcs.get_index_u2();
1785           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1786           current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1787           VerificationType klass_type = cp_index_to_type(
1788             index, cp, CHECK_VERIFY(this));
1789           current_frame.push_stack(klass_type, CHECK_VERIFY(this));
1790           no_control_flow = false; break;
1791         }
1792         case Bytecodes::_instanceof : {
1793           u2 index = bcs.get_index_u2();
1794           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1795           current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1796           current_frame.push_stack(
1797             VerificationType::integer_type(), CHECK_VERIFY(this));
1798           no_control_flow = false; break;
1799         }
1800         case Bytecodes::_monitorenter :
1801         case Bytecodes::_monitorexit : {
1802           VerificationType ref = current_frame.pop_stack(
1803             VerificationType::reference_check(), CHECK_VERIFY(this));
1804           no_control_flow = false; break;
1805         }
1806         case Bytecodes::_multianewarray :
1807         {
1808           u2 index = bcs.get_index_u2();
1809           u2 dim = *(bcs.bcp()+3);
1810           verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1811           VerificationType new_array_type =
1812             cp_index_to_type(index, cp, CHECK_VERIFY(this));
1813           if (!new_array_type.is_array()) {
1814             verify_error(ErrorContext::bad_type(bci,
1815                 TypeOrigin::cp(index, new_array_type)),
1816                 "Illegal constant pool index in multianewarray instruction");
1817             return;
1818           }
1819           if (dim < 1 || new_array_type.dimensions() < dim) {
1820             verify_error(ErrorContext::bad_code(bci),
1821                 "Illegal dimension in multianewarray instruction: %d", dim);
1822             return;
1823           }
1824           for (int i = 0; i < dim; i++) {
1825             current_frame.pop_stack(

2163   } else {
2164     Klass* member_klass = target_instance->find_field(field_name, field_sig, &fd);
2165     if (member_klass != nullptr && fd.is_protected()) {
2166       if (!this_class->is_same_class_package(member_klass)) {
2167         return true;
2168       }
2169     }
2170   }
2171   return false;
2172 }
2173 
2174 void ClassVerifier::verify_ldc(
2175     int opcode, u2 index, StackMapFrame* current_frame,
2176     const constantPoolHandle& cp, int bci, TRAPS) {
2177   verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
2178   constantTag tag = cp->tag_at(index);
2179   unsigned int types = 0;
2180   if (opcode == Bytecodes::_ldc || opcode == Bytecodes::_ldc_w) {
2181     if (!tag.is_unresolved_klass()) {
2182       types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float)
2183             | (1 << JVM_CONSTANT_String) | (1 << JVM_CONSTANT_Class)
2184             | (1 << JVM_CONSTANT_MethodHandle) | (1 << JVM_CONSTANT_MethodType)
2185             | (1 << JVM_CONSTANT_Dynamic);
2186       // Note:  The class file parser already verified the legality of
2187       // MethodHandle and MethodType constants.
2188       verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2189     }
2190   } else {
2191     assert(opcode == Bytecodes::_ldc2_w, "must be ldc2_w");
2192     types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long)
2193           | (1 << JVM_CONSTANT_Dynamic);
2194     verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2195   }
2196   if (tag.is_string()) {
2197     current_frame->push_stack(
2198       VerificationType::reference_type(
2199         vmSymbols::java_lang_String()), CHECK_VERIFY(this));
2200   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
2201     current_frame->push_stack(
2202       VerificationType::reference_type(
2203         vmSymbols::java_lang_Class()), CHECK_VERIFY(this));

2339                                               const constantPoolHandle& cp,
2340                                               bool allow_arrays,
2341                                               TRAPS) {
2342   u2 index = bcs->get_index_u2();
2343   verify_cp_type(bcs->bci(), index, cp,
2344       1 << JVM_CONSTANT_Fieldref, CHECK_VERIFY(this));
2345 
2346   // Get field name and signature
2347   Symbol* field_name = cp->uncached_name_ref_at(index);
2348   Symbol* field_sig = cp->uncached_signature_ref_at(index);
2349   bool is_getfield = false;
2350 
2351   // Field signature was checked in ClassFileParser.
2352   assert(SignatureVerifier::is_valid_type_signature(field_sig),
2353          "Invalid field signature");
2354 
2355   // Get referenced class type
2356   VerificationType ref_class_type = cp_ref_index_to_type(
2357     index, cp, CHECK_VERIFY(this));
2358   if (!ref_class_type.is_object() &&
2359       (!allow_arrays || !ref_class_type.is_array())) {
2360     verify_error(ErrorContext::bad_type(bcs->bci(),
2361         TypeOrigin::cp(index, ref_class_type)),
2362         "Expecting reference to class in class %s at constant pool index %d",
2363         _klass->external_name(), index);
2364     return;
2365   }
2366 
2367   VerificationType target_class_type = ref_class_type;
2368 
2369   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2370         "buffer type must match VerificationType size");
2371   uintptr_t field_type_buffer[2];
2372   VerificationType* field_type = (VerificationType*)field_type_buffer;
2373   // If we make a VerificationType[2] array directly, the compiler calls
2374   // to the c-runtime library to do the allocation instead of just
2375   // stack allocating it.  Plus it would run constructors.  This shows up
2376   // in performance profiles.
2377 
2378   SignatureStream sig_stream(field_sig, false);
2379   VerificationType stack_object_type;
2380   int n = change_sig_to_verificationType(&sig_stream, field_type);
2381   int bci = bcs->bci();
2382   bool is_assignable;
2383   switch (bcs->raw_code()) {
2384     case Bytecodes::_getstatic: {
2385       for (int i = 0; i < n; i++) {
2386         current_frame->push_stack(field_type[i], CHECK_VERIFY(this));

2388       break;
2389     }
2390     case Bytecodes::_putstatic: {
2391       for (int i = n - 1; i >= 0; i--) {
2392         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2393       }
2394       break;
2395     }
2396     case Bytecodes::_getfield: {
2397       is_getfield = true;
2398       stack_object_type = current_frame->pop_stack(
2399         target_class_type, CHECK_VERIFY(this));
2400       goto check_protected;
2401     }
2402     case Bytecodes::_putfield: {
2403       for (int i = n - 1; i >= 0; i--) {
2404         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2405       }
2406       stack_object_type = current_frame->pop_stack(CHECK_VERIFY(this));
2407 
2408       // Field initialization is allowed before the superclass
2409       // initializer, if the field is defined within the current class.
2410       fieldDescriptor fd;
2411       bool is_local_field = _klass->find_local_field(field_name, field_sig, &fd) &&
2412                             target_class_type.equals(current_type());
2413       if (stack_object_type == VerificationType::uninitialized_this_type()) {
2414         if (is_local_field) {
2415           // Set the type to the current type so the is_assignable check passes.
2416           stack_object_type = current_type();
2417 
2418           if (fd.access_flags().is_strict()) {
2419             ResourceMark rm(THREAD);
2420             if (!current_frame->satisfy_unset_field(fd.name(), fd.signature())) {
2421               log_info(verification)("Attempting to initialize field not found in initial strict instance fields: %s%s",
2422                                      fd.name()->as_C_string(), fd.signature()->as_C_string());
2423               verify_error(ErrorContext::bad_strict_fields(bci, current_frame),
2424                            "Initializing unknown strict field: %s:%s", fd.name()->as_C_string(), fd.signature()->as_C_string());
2425             }
2426           }
2427         }
2428       } else if (supports_strict_fields(_klass)) {
2429         // `strict` fields are not writable, but only local fields produce verification errors
2430         if (is_local_field && fd.access_flags().is_strict() && fd.access_flags().is_final()) {
2431           verify_error(ErrorContext::bad_code(bci),
2432                        "Illegal use of putfield on a strict field");
2433           return;
2434         }
2435       }
2436       is_assignable = target_class_type.is_assignable_from(
2437         stack_object_type, this, false, CHECK_VERIFY(this));
2438       if (!is_assignable) {
2439         verify_error(ErrorContext::bad_type(bci,
2440             current_frame->stack_top_ctx(),
2441             TypeOrigin::cp(index, target_class_type)),
2442             "Bad type on operand stack in putfield");
2443         return;
2444       }
2445     }
2446     check_protected: {
2447       if (_this_type == stack_object_type)
2448         break; // stack_object_type must be assignable to _current_class_type
2449       if (was_recursively_verified()) {
2450         if (is_getfield) {
2451           // Push field type for getfield.
2452           for (int i = 0; i < n; i++) {
2453             current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
2454           }

2696 }
2697 
2698 void ClassVerifier::verify_invoke_init(
2699     RawBytecodeStream* bcs, u2 ref_class_index, VerificationType ref_class_type,
2700     StackMapFrame* current_frame, u4 code_length, bool in_try_block,
2701     bool *this_uninit, const constantPoolHandle& cp, StackMapTable* stackmap_table,
2702     TRAPS) {
2703   int bci = bcs->bci();
2704   VerificationType type = current_frame->pop_stack(
2705     VerificationType::reference_check(), CHECK_VERIFY(this));
2706   if (type == VerificationType::uninitialized_this_type()) {
2707     // The method must be an <init> method of this class or its superclass
2708     Klass* superk = current_class()->super();
2709     if (ref_class_type.name() != current_class()->name() &&
2710         ref_class_type.name() != superk->name()) {
2711       verify_error(ErrorContext::bad_type(bci,
2712           TypeOrigin::implicit(ref_class_type),
2713           TypeOrigin::implicit(current_type())),
2714           "Bad <init> method call");
2715       return;
2716     } else if (ref_class_type.name() == superk->name()) {
2717       // Strict final fields must be satisfied by this point
2718       if (!current_frame->verify_unset_fields_satisfied()) {
2719         log_info(verification)("Strict instance fields not initialized");
2720         StackMapFrame::print_strict_fields(current_frame->assert_unset_fields());
2721         current_frame->unsatisfied_strict_fields_error(current_class(), bci);
2722       }
2723     }
2724 
2725     // If this invokespecial call is done from inside of a TRY block then make
2726     // sure that all catch clause paths end in a throw.  Otherwise, this can
2727     // result in returning an incomplete object.
2728     if (in_try_block) {
2729       ExceptionTable exhandlers(_method());
2730       int exlength = exhandlers.length();
2731       for(int i = 0; i < exlength; i++) {
2732         u2 start_pc = exhandlers.start_pc(i);
2733         u2 end_pc = exhandlers.end_pc(i);
2734 
2735         if (bci >= start_pc && bci < end_pc) {
2736           if (!ends_in_athrow(exhandlers.handler_pc(i))) {
2737             verify_error(ErrorContext::bad_code(bci),
2738               "Bad <init> method call from after the start of a try block");
2739             return;
2740           } else if (log_is_enabled(Debug, verification)) {
2741             ResourceMark rm(THREAD);
2742             log_debug(verification)("Survived call to ends_in_athrow(): %s",

2826 bool ClassVerifier::is_same_or_direct_interface(
2827     InstanceKlass* klass,
2828     VerificationType klass_type,
2829     VerificationType ref_class_type) {
2830   if (ref_class_type.equals(klass_type)) return true;
2831   Array<InstanceKlass*>* local_interfaces = klass->local_interfaces();
2832   if (local_interfaces != nullptr) {
2833     for (int x = 0; x < local_interfaces->length(); x++) {
2834       InstanceKlass* k = local_interfaces->at(x);
2835       assert (k != nullptr && k->is_interface(), "invalid interface");
2836       if (ref_class_type.equals(VerificationType::reference_type(k->name()))) {
2837         return true;
2838       }
2839     }
2840   }
2841   return false;
2842 }
2843 
2844 void ClassVerifier::verify_invoke_instructions(
2845     RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
2846     bool in_try_block, bool *this_uninit,
2847     const constantPoolHandle& cp, StackMapTable* stackmap_table, TRAPS) {
2848   // Make sure the constant pool item is the right type
2849   u2 index = bcs->get_index_u2();
2850   Bytecodes::Code opcode = bcs->raw_code();
2851   unsigned int types = 0;
2852   switch (opcode) {
2853     case Bytecodes::_invokeinterface:
2854       types = 1 << JVM_CONSTANT_InterfaceMethodref;
2855       break;
2856     case Bytecodes::_invokedynamic:
2857       types = 1 << JVM_CONSTANT_InvokeDynamic;
2858       break;
2859     case Bytecodes::_invokespecial:
2860     case Bytecodes::_invokestatic:
2861       types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ?
2862         (1 << JVM_CONSTANT_Methodref) :
2863         ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref));
2864       break;
2865     default:
2866       types = 1 << JVM_CONSTANT_Methodref;
2867   }
2868   verify_cp_type(bcs->bci(), index, cp, types, CHECK_VERIFY(this));
2869 
2870   // Get method name and signature
2871   Symbol* method_name = cp->uncached_name_ref_at(index);
2872   Symbol* method_sig = cp->uncached_signature_ref_at(index);
2873 
2874   // Method signature was checked in ClassFileParser.
2875   assert(SignatureVerifier::is_valid_method_signature(method_sig),
2876          "Invalid method signature");
2877 
2878   // Get referenced class
2879   VerificationType ref_class_type;
2880   if (opcode == Bytecodes::_invokedynamic) {
2881     if (_klass->major_version() < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2882       class_format_error(
2883         "invokedynamic instructions not supported by this class file version (%d), class %s",
2884         _klass->major_version(), _klass->external_name());
2885       return;
2886     }
2887   } else {
2888     ref_class_type = cp_ref_index_to_type(index, cp, CHECK_VERIFY(this));
2889   }
2890 
2891   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2892         "buffer type must match VerificationType size");
2893 
2894   // Get the UTF8 index for this signature.
2895   int sig_index = cp->signature_ref_index_at(cp->uncached_name_and_type_ref_index_at(index));
2896 
2897   // Get the signature's verification types.
2898   sig_as_verification_types* mth_sig_verif_types;

2924           "Inconsistent args count operand in invokeinterface");
2925       return;
2926     }
2927     if (*(bcp+4) != 0) {
2928       verify_error(ErrorContext::bad_code(bci),
2929           "Fourth operand byte of invokeinterface must be zero");
2930       return;
2931     }
2932   }
2933 
2934   if (opcode == Bytecodes::_invokedynamic) {
2935     address bcp = bcs->bcp();
2936     if (*(bcp+3) != 0 || *(bcp+4) != 0) {
2937       verify_error(ErrorContext::bad_code(bci),
2938           "Third and fourth operand bytes of invokedynamic must be zero");
2939       return;
2940     }
2941   }
2942 
2943   if (method_name->char_at(0) == JVM_SIGNATURE_SPECIAL) {
2944     // Make sure:
2945     //   <init> can only be invoked by invokespecial.
2946     if (opcode != Bytecodes::_invokespecial ||
2947           method_name != vmSymbols::object_initializer_name()) {
2948       verify_error(ErrorContext::bad_code(bci),
2949           "Illegal call to internal method");
2950       return;
2951     }
2952   } else if (opcode == Bytecodes::_invokespecial
2953              && !is_same_or_direct_interface(current_class(), current_type(), ref_class_type)
2954              && !ref_class_type.equals(VerificationType::reference_type(
2955                   current_class()->super()->name()))) { // super() can never be an inline_type.
2956     bool subtype = false;
2957     bool have_imr_indirect = cp->tag_at(index).value() == JVM_CONSTANT_InterfaceMethodref;
2958     subtype = ref_class_type.is_assignable_from(
2959                current_type(), this, false, CHECK_VERIFY(this));
2960     if (!subtype) {
2961       verify_error(ErrorContext::bad_code(bci),
2962           "Bad invokespecial instruction: "
2963           "current class isn't assignable to reference class.");
2964        return;
2965     } else if (have_imr_indirect) {
2966       verify_error(ErrorContext::bad_code(bci),
2967           "Bad invokespecial instruction: "
2968           "interface method reference is in an indirect superinterface.");
2969       return;
2970     }
2971 
2972   }
2973 
2974   // Get the verification types for the method's arguments.
2975   GrowableArray<VerificationType>* sig_verif_types = mth_sig_verif_types->sig_verif_types();

3020                   verify_error(ErrorContext::bad_type(bci,
3021                       current_frame->stack_top_ctx(),
3022                       TypeOrigin::implicit(current_type())),
3023                       "Bad access to protected data in invokevirtual");
3024                   return;
3025                 }
3026               }
3027             }
3028           }
3029         }
3030       } else {
3031         assert(opcode == Bytecodes::_invokeinterface, "Unexpected opcode encountered");
3032         current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this));
3033       }
3034     }
3035   }
3036   // Push the result type.
3037   int sig_verif_types_len = sig_verif_types->length();
3038   if (sig_verif_types_len > nargs) {  // There's a return type
3039     if (method_name == vmSymbols::object_initializer_name()) {
3040       // an <init> method must have a void return type


3041       verify_error(ErrorContext::bad_code(bci),
3042           "Return type must be void in <init> method");
3043       return;
3044     }
3045 
3046     assert(sig_verif_types_len <= nargs + 2,
3047            "Signature verification types array return type is bogus");
3048     for (int i = nargs; i < sig_verif_types_len; i++) {
3049       assert(i == nargs || sig_verif_types->at(i).is_long2() ||
3050              sig_verif_types->at(i).is_double2(), "Unexpected return verificationType");
3051       current_frame->push_stack(sig_verif_types->at(i), CHECK_VERIFY(this));
3052     }
3053   }
3054 }
3055 
3056 VerificationType ClassVerifier::get_newarray_type(
3057     u2 index, int bci, TRAPS) {
3058   const char* from_bt[] = {
3059     nullptr, nullptr, nullptr, nullptr, "[Z", "[C", "[F", "[D", "[B", "[S", "[I", "[J",
3060   };
< prev index next >