< prev index next >

src/hotspot/share/classfile/verifier.cpp

Print this page




  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/symbolTable.hpp"
  34 #include "classfile/systemDictionary.hpp"
  35 #include "classfile/verifier.hpp"
  36 #include "classfile/vmSymbols.hpp"
  37 #include "interpreter/bytecodes.hpp"
  38 #include "interpreter/bytecodeStream.hpp"
  39 #include "logging/log.hpp"
  40 #include "logging/logStream.hpp"
  41 #include "memory/oopFactory.hpp"
  42 #include "memory/resourceArea.hpp"
  43 #include "memory/universe.hpp"
  44 #include "oops/constantPool.inline.hpp"
  45 #include "oops/instanceKlass.hpp"
  46 #include "oops/oop.inline.hpp"
  47 #include "oops/typeArrayOop.hpp"
  48 #include "runtime/fieldDescriptor.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/interfaceSupport.inline.hpp"
  51 #include "runtime/javaCalls.hpp"
  52 #include "runtime/jniHandles.inline.hpp"
  53 #include "runtime/orderAccess.hpp"
  54 #include "runtime/os.hpp"
  55 #include "runtime/safepointVerifiers.hpp"
  56 #include "runtime/thread.hpp"
  57 #include "services/threadService.hpp"
  58 #include "utilities/align.hpp"
  59 #include "utilities/bytes.hpp"
  60 
  61 #define NOFAILOVER_MAJOR_VERSION                       51
  62 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION  51
  63 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION       52


 578     InstanceKlass* klass, TRAPS)
 579     : _thread(THREAD), _previous_symbol(NULL), _symbols(NULL), _exception_type(NULL),
 580       _message(NULL), _method_signatures_table(NULL), _klass(klass) {
 581   _this_type = VerificationType::reference_type(klass->name());
 582 }
 583 
 584 ClassVerifier::~ClassVerifier() {
 585   // Decrement the reference count for any symbols created.
 586   if (_symbols != NULL) {
 587     for (int i = 0; i < _symbols->length(); i++) {
 588       Symbol* s = _symbols->at(i);
 589       s->decrement_refcount();
 590     }
 591   }
 592 }
 593 
 594 VerificationType ClassVerifier::object_type() const {
 595   return VerificationType::reference_type(vmSymbols::java_lang_Object());
 596 }
 597 
 598 TypeOrigin ClassVerifier::ref_ctx(const char* sig) {
 599   VerificationType vt = VerificationType::reference_type(
 600                          create_temporary_symbol(sig, (int)strlen(sig)));
 601   return TypeOrigin::implicit(vt);
 602 }
 603 
 604 void ClassVerifier::verify_class(TRAPS) {
 605   log_info(verification)("Verifying class %s with new format", _klass->external_name());
 606 
 607   // Either verifying both local and remote classes or just remote classes.
 608   assert(BytecodeVerificationRemote, "Should not be here");
 609 
 610   // Create hash table containing method signatures.
 611   method_signatures_table_type method_signatures_table;
 612   set_method_signatures_table(&method_signatures_table);
 613 
 614   Array<Method*>* methods = _klass->methods();
 615   int num_methods = methods->length();
 616 
 617   for (int index = 0; index < num_methods; index++) {
 618     // Check for recursive re-verification before each method.
 619     if (was_recursively_verified())  return;
 620 


 630 
 631   if (was_recursively_verified()){
 632     log_info(verification)("Recursive verification detected for: %s", _klass->external_name());
 633     log_info(class, init)("Recursive verification detected for: %s",
 634                         _klass->external_name());
 635   }
 636 }
 637 
 638 // Translate the signature entries into verification types and save them in
 639 // the growable array.  Also, save the count of arguments.
 640 void ClassVerifier::translate_signature(Symbol* const method_sig,
 641                                         sig_as_verification_types* sig_verif_types,
 642                                         TRAPS) {
 643   SignatureStream sig_stream(method_sig);
 644   VerificationType sig_type[2];
 645   int sig_i = 0;
 646   GrowableArray<VerificationType>* verif_types = sig_verif_types->sig_verif_types();
 647 
 648   // Translate the signature arguments into verification types.
 649   while (!sig_stream.at_return_type()) {
 650     int n = change_sig_to_verificationType(&sig_stream, sig_type);
 651     assert(n <= 2, "Unexpected signature type");
 652 
 653     // Store verification type(s).  Longs and Doubles each have two verificationTypes.
 654     for (int x = 0; x < n; x++) {
 655       verif_types->push(sig_type[x]);
 656     }
 657     sig_i += n;
 658     sig_stream.next();
 659   }
 660 
 661   // Set final arg count, not including the return type.  The final arg count will
 662   // be compared with sig_verify_types' length to see if there is a return type.
 663   sig_verif_types->set_num_args(sig_i);
 664 
 665   // Store verification type(s) for the return type, if there is one.
 666   if (sig_stream.type() != T_VOID) {
 667     int n = change_sig_to_verificationType(&sig_stream, sig_type);
 668     assert(n <= 2, "Unexpected signature return type");
 669     for (int y = 0; y < n; y++) {
 670       verif_types->push(sig_type[y]);
 671     }
 672   }
 673 }
 674 
 675 void ClassVerifier::create_method_sig_entry(sig_as_verification_types* sig_verif_types,
 676                                             int sig_index, TRAPS) {
 677   // Translate the signature into verification types.
 678   ConstantPool* cp = _klass->constants();
 679   Symbol* const method_sig = cp->symbol_at(sig_index);
 680   translate_signature(method_sig, sig_verif_types, CHECK_VERIFY(this));
 681 
 682   // Add the list of this signature's verification types to the table.
 683   bool is_unique = method_signatures_table()->put(sig_index, sig_verif_types);
 684   assert(is_unique, "Duplicate entries in method_signature_table");
 685 }
 686 
 687 void ClassVerifier::verify_method(const methodHandle& m, TRAPS) {


 908           index = opcode - Bytecodes::_dload_0;
 909           verify_dload(index, &current_frame, CHECK_VERIFY(this));
 910           no_control_flow = false; break;
 911         case Bytecodes::_aload :
 912           verify_aload(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
 913           no_control_flow = false; break;
 914         case Bytecodes::_aload_0 :
 915         case Bytecodes::_aload_1 :
 916         case Bytecodes::_aload_2 :
 917         case Bytecodes::_aload_3 :
 918           index = opcode - Bytecodes::_aload_0;
 919           verify_aload(index, &current_frame, CHECK_VERIFY(this));
 920           no_control_flow = false; break;
 921         case Bytecodes::_iaload :
 922           type = current_frame.pop_stack(
 923             VerificationType::integer_type(), CHECK_VERIFY(this));
 924           atype = current_frame.pop_stack(
 925             VerificationType::reference_check(), CHECK_VERIFY(this));
 926           if (!atype.is_int_array()) {
 927             verify_error(ErrorContext::bad_type(bci,
 928                 current_frame.stack_top_ctx(), ref_ctx("[I")),
 929                 bad_type_msg, "iaload");
 930             return;
 931           }
 932           current_frame.push_stack(
 933             VerificationType::integer_type(), CHECK_VERIFY(this));
 934           no_control_flow = false; break;
 935         case Bytecodes::_baload :
 936           type = current_frame.pop_stack(
 937             VerificationType::integer_type(), CHECK_VERIFY(this));
 938           atype = current_frame.pop_stack(
 939             VerificationType::reference_check(), CHECK_VERIFY(this));
 940           if (!atype.is_bool_array() && !atype.is_byte_array()) {
 941             verify_error(
 942                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
 943                 bad_type_msg, "baload");
 944             return;
 945           }
 946           current_frame.push_stack(
 947             VerificationType::integer_type(), CHECK_VERIFY(this));
 948           no_control_flow = false; break;
 949         case Bytecodes::_caload :
 950           type = current_frame.pop_stack(
 951             VerificationType::integer_type(), CHECK_VERIFY(this));
 952           atype = current_frame.pop_stack(
 953             VerificationType::reference_check(), CHECK_VERIFY(this));
 954           if (!atype.is_char_array()) {
 955             verify_error(ErrorContext::bad_type(bci,
 956                 current_frame.stack_top_ctx(), ref_ctx("[C")),
 957                 bad_type_msg, "caload");
 958             return;
 959           }
 960           current_frame.push_stack(
 961             VerificationType::integer_type(), CHECK_VERIFY(this));
 962           no_control_flow = false; break;
 963         case Bytecodes::_saload :
 964           type = current_frame.pop_stack(
 965             VerificationType::integer_type(), CHECK_VERIFY(this));
 966           atype = current_frame.pop_stack(
 967             VerificationType::reference_check(), CHECK_VERIFY(this));
 968           if (!atype.is_short_array()) {
 969             verify_error(ErrorContext::bad_type(bci,
 970                 current_frame.stack_top_ctx(), ref_ctx("[S")),
 971                 bad_type_msg, "saload");
 972             return;
 973           }
 974           current_frame.push_stack(
 975             VerificationType::integer_type(), CHECK_VERIFY(this));
 976           no_control_flow = false; break;
 977         case Bytecodes::_laload :
 978           type = current_frame.pop_stack(
 979             VerificationType::integer_type(), CHECK_VERIFY(this));
 980           atype = current_frame.pop_stack(
 981             VerificationType::reference_check(), CHECK_VERIFY(this));
 982           if (!atype.is_long_array()) {
 983             verify_error(ErrorContext::bad_type(bci,
 984                 current_frame.stack_top_ctx(), ref_ctx("[J")),
 985                 bad_type_msg, "laload");
 986             return;
 987           }
 988           current_frame.push_stack_2(
 989             VerificationType::long_type(),
 990             VerificationType::long2_type(), CHECK_VERIFY(this));
 991           no_control_flow = false; break;
 992         case Bytecodes::_faload :
 993           type = current_frame.pop_stack(
 994             VerificationType::integer_type(), CHECK_VERIFY(this));
 995           atype = current_frame.pop_stack(
 996             VerificationType::reference_check(), CHECK_VERIFY(this));
 997           if (!atype.is_float_array()) {
 998             verify_error(ErrorContext::bad_type(bci,
 999                 current_frame.stack_top_ctx(), ref_ctx("[F")),
1000                 bad_type_msg, "faload");
1001             return;
1002           }
1003           current_frame.push_stack(
1004             VerificationType::float_type(), CHECK_VERIFY(this));
1005           no_control_flow = false; break;
1006         case Bytecodes::_daload :
1007           type = current_frame.pop_stack(
1008             VerificationType::integer_type(), CHECK_VERIFY(this));
1009           atype = current_frame.pop_stack(
1010             VerificationType::reference_check(), CHECK_VERIFY(this));
1011           if (!atype.is_double_array()) {
1012             verify_error(ErrorContext::bad_type(bci,
1013                 current_frame.stack_top_ctx(), ref_ctx("[D")),
1014                 bad_type_msg, "daload");
1015             return;
1016           }
1017           current_frame.push_stack_2(
1018             VerificationType::double_type(),
1019             VerificationType::double2_type(), CHECK_VERIFY(this));
1020           no_control_flow = false; break;
1021         case Bytecodes::_aaload : {
1022           type = current_frame.pop_stack(
1023             VerificationType::integer_type(), CHECK_VERIFY(this));
1024           atype = current_frame.pop_stack(
1025             VerificationType::reference_check(), CHECK_VERIFY(this));
1026           if (!atype.is_reference_array()) {
1027             verify_error(ErrorContext::bad_type(bci,
1028                 current_frame.stack_top_ctx(),
1029                 TypeOrigin::implicit(VerificationType::reference_check())),
1030                 bad_type_msg, "aaload");
1031             return;
1032           }
1033           if (atype.is_null()) {


1082           no_control_flow = false; break;
1083         case Bytecodes::_astore :
1084           verify_astore(bcs.get_index(), &current_frame, CHECK_VERIFY(this));
1085           no_control_flow = false; break;
1086         case Bytecodes::_astore_0 :
1087         case Bytecodes::_astore_1 :
1088         case Bytecodes::_astore_2 :
1089         case Bytecodes::_astore_3 :
1090           index = opcode - Bytecodes::_astore_0;
1091           verify_astore(index, &current_frame, CHECK_VERIFY(this));
1092           no_control_flow = false; break;
1093         case Bytecodes::_iastore :
1094           type = current_frame.pop_stack(
1095             VerificationType::integer_type(), CHECK_VERIFY(this));
1096           type2 = current_frame.pop_stack(
1097             VerificationType::integer_type(), CHECK_VERIFY(this));
1098           atype = current_frame.pop_stack(
1099             VerificationType::reference_check(), CHECK_VERIFY(this));
1100           if (!atype.is_int_array()) {
1101             verify_error(ErrorContext::bad_type(bci,
1102                 current_frame.stack_top_ctx(), ref_ctx("[I")),
1103                 bad_type_msg, "iastore");
1104             return;
1105           }
1106           no_control_flow = false; break;
1107         case Bytecodes::_bastore :
1108           type = current_frame.pop_stack(
1109             VerificationType::integer_type(), CHECK_VERIFY(this));
1110           type2 = current_frame.pop_stack(
1111             VerificationType::integer_type(), CHECK_VERIFY(this));
1112           atype = current_frame.pop_stack(
1113             VerificationType::reference_check(), CHECK_VERIFY(this));
1114           if (!atype.is_bool_array() && !atype.is_byte_array()) {
1115             verify_error(
1116                 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1117                 bad_type_msg, "bastore");
1118             return;
1119           }
1120           no_control_flow = false; break;
1121         case Bytecodes::_castore :
1122           current_frame.pop_stack(
1123             VerificationType::integer_type(), CHECK_VERIFY(this));
1124           current_frame.pop_stack(
1125             VerificationType::integer_type(), CHECK_VERIFY(this));
1126           atype = current_frame.pop_stack(
1127             VerificationType::reference_check(), CHECK_VERIFY(this));
1128           if (!atype.is_char_array()) {
1129             verify_error(ErrorContext::bad_type(bci,
1130                 current_frame.stack_top_ctx(), ref_ctx("[C")),
1131                 bad_type_msg, "castore");
1132             return;
1133           }
1134           no_control_flow = false; break;
1135         case Bytecodes::_sastore :
1136           current_frame.pop_stack(
1137             VerificationType::integer_type(), CHECK_VERIFY(this));
1138           current_frame.pop_stack(
1139             VerificationType::integer_type(), CHECK_VERIFY(this));
1140           atype = current_frame.pop_stack(
1141             VerificationType::reference_check(), CHECK_VERIFY(this));
1142           if (!atype.is_short_array()) {
1143             verify_error(ErrorContext::bad_type(bci,
1144                 current_frame.stack_top_ctx(), ref_ctx("[S")),
1145                 bad_type_msg, "sastore");
1146             return;
1147           }
1148           no_control_flow = false; break;
1149         case Bytecodes::_lastore :
1150           current_frame.pop_stack_2(
1151             VerificationType::long2_type(),
1152             VerificationType::long_type(), CHECK_VERIFY(this));
1153           current_frame.pop_stack(
1154             VerificationType::integer_type(), CHECK_VERIFY(this));
1155           atype = current_frame.pop_stack(
1156             VerificationType::reference_check(), CHECK_VERIFY(this));
1157           if (!atype.is_long_array()) {
1158             verify_error(ErrorContext::bad_type(bci,
1159                 current_frame.stack_top_ctx(), ref_ctx("[J")),
1160                 bad_type_msg, "lastore");
1161             return;
1162           }
1163           no_control_flow = false; break;
1164         case Bytecodes::_fastore :
1165           current_frame.pop_stack(
1166             VerificationType::float_type(), CHECK_VERIFY(this));
1167           current_frame.pop_stack
1168             (VerificationType::integer_type(), CHECK_VERIFY(this));
1169           atype = current_frame.pop_stack(
1170             VerificationType::reference_check(), CHECK_VERIFY(this));
1171           if (!atype.is_float_array()) {
1172             verify_error(ErrorContext::bad_type(bci,
1173                 current_frame.stack_top_ctx(), ref_ctx("[F")),
1174                 bad_type_msg, "fastore");
1175             return;
1176           }
1177           no_control_flow = false; break;
1178         case Bytecodes::_dastore :
1179           current_frame.pop_stack_2(
1180             VerificationType::double2_type(),
1181             VerificationType::double_type(), CHECK_VERIFY(this));
1182           current_frame.pop_stack(
1183             VerificationType::integer_type(), CHECK_VERIFY(this));
1184           atype = current_frame.pop_stack(
1185             VerificationType::reference_check(), CHECK_VERIFY(this));
1186           if (!atype.is_double_array()) {
1187             verify_error(ErrorContext::bad_type(bci,
1188                 current_frame.stack_top_ctx(), ref_ctx("[D")),
1189                 bad_type_msg, "dastore");
1190             return;
1191           }
1192           no_control_flow = false; break;
1193         case Bytecodes::_aastore :
1194           type = current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1195           type2 = current_frame.pop_stack(
1196             VerificationType::integer_type(), CHECK_VERIFY(this));
1197           atype = current_frame.pop_stack(
1198             VerificationType::reference_check(), CHECK_VERIFY(this));
1199           // more type-checking is done at runtime
1200           if (!atype.is_reference_array()) {
1201             verify_error(ErrorContext::bad_type(bci,
1202                 current_frame.stack_top_ctx(),
1203                 TypeOrigin::implicit(VerificationType::reference_check())),
1204                 bad_type_msg, "aastore");
1205             return;
1206           }
1207           // 4938384: relaxed constraint in JVMS 3nd edition.
1208           no_control_flow = false; break;


2045   va_list va;
2046   va_start(va, msg);
2047   ss.vprint(msg, va);
2048   va_end(va);
2049   _message = ss.as_string();
2050 #ifdef ASSERT
2051   ResourceMark rm;
2052   const char* exception_name = _exception_type->as_C_string();
2053   Exceptions::debug_check_abort(exception_name, NULL);
2054 #endif // ndef ASSERT
2055 }
2056 
2057 void ClassVerifier::class_format_error(const char* msg, ...) {
2058   stringStream ss;
2059   _exception_type = vmSymbols::java_lang_ClassFormatError();
2060   va_list va;
2061   va_start(va, msg);
2062   ss.vprint(msg, va);
2063   va_end(va);
2064   if (!_method.is_null()) {
2065     ss.print(" in method '");
2066     _method->print_external_name(&ss);
2067     ss.print("'");
2068   }
2069   _message = ss.as_string();
2070 }
2071 
2072 Klass* ClassVerifier::load_class(Symbol* name, TRAPS) {
2073   HandleMark hm(THREAD);
2074   // Get current loader and protection domain first.
2075   oop loader = current_class()->class_loader();
2076   oop protection_domain = current_class()->protection_domain();
2077 
2078   Klass* kls = SystemDictionary::resolve_or_fail(
2079     name, Handle(THREAD, loader), Handle(THREAD, protection_domain),
2080     true, THREAD);
2081 
2082   if (kls != NULL) {
2083     if (log_is_enabled(Debug, class, resolve)) {
2084       Verifier::trace_class_resolution(kls, current_class());
2085     }
2086   }
2087   return kls;


2166       VerificationType::long_type(),
2167       VerificationType::long2_type(), CHECK_VERIFY(this));
2168   } else if (tag.is_method_handle()) {
2169     current_frame->push_stack(
2170       VerificationType::reference_type(
2171         vmSymbols::java_lang_invoke_MethodHandle()), CHECK_VERIFY(this));
2172   } else if (tag.is_method_type()) {
2173     current_frame->push_stack(
2174       VerificationType::reference_type(
2175         vmSymbols::java_lang_invoke_MethodType()), CHECK_VERIFY(this));
2176   } else if (tag.is_dynamic_constant()) {
2177     Symbol* constant_type = cp->uncached_signature_ref_at(index);
2178     // Field signature was checked in ClassFileParser.
2179     assert(SignatureVerifier::is_valid_type_signature(constant_type),
2180            "Invalid type for dynamic constant");
2181     assert(sizeof(VerificationType) == sizeof(uintptr_t),
2182           "buffer type must match VerificationType size");
2183     uintptr_t constant_type_buffer[2];
2184     VerificationType* v_constant_type = (VerificationType*)constant_type_buffer;
2185     SignatureStream sig_stream(constant_type, false);
2186     int n = change_sig_to_verificationType(&sig_stream, v_constant_type);

2187     int opcode_n = (opcode == Bytecodes::_ldc2_w ? 2 : 1);
2188     if (n != opcode_n) {
2189       // wrong kind of ldc; reverify against updated type mask
2190       types &= ~(1 << JVM_CONSTANT_Dynamic);
2191       verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2192     }
2193     for (int i = 0; i < n; i++) {
2194       current_frame->push_stack(v_constant_type[i], CHECK_VERIFY(this));
2195     }
2196   } else {
2197     /* Unreachable? verify_cp_type has already validated the cp type. */
2198     verify_error(
2199         ErrorContext::bad_cp_index(bci, index), "Invalid index in ldc");
2200     return;
2201   }
2202 }
2203 
2204 void ClassVerifier::verify_switch(
2205     RawBytecodeStream* bcs, u4 code_length, char* code_data,
2206     StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS) {


2307     (!allow_arrays || !ref_class_type.is_array())) {
2308     verify_error(ErrorContext::bad_type(bcs->bci(),
2309         TypeOrigin::cp(index, ref_class_type)),
2310         "Expecting reference to class in class %s at constant pool index %d",
2311         _klass->external_name(), index);
2312     return;
2313   }
2314   VerificationType target_class_type = ref_class_type;
2315 
2316   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2317         "buffer type must match VerificationType size");
2318   uintptr_t field_type_buffer[2];
2319   VerificationType* field_type = (VerificationType*)field_type_buffer;
2320   // If we make a VerificationType[2] array directly, the compiler calls
2321   // to the c-runtime library to do the allocation instead of just
2322   // stack allocating it.  Plus it would run constructors.  This shows up
2323   // in performance profiles.
2324 
2325   SignatureStream sig_stream(field_sig, false);
2326   VerificationType stack_object_type;
2327   int n = change_sig_to_verificationType(&sig_stream, field_type);

2328   u2 bci = bcs->bci();
2329   bool is_assignable;
2330   switch (bcs->raw_code()) {
2331     case Bytecodes::_getstatic: {
2332       for (int i = 0; i < n; i++) {
2333         current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
2334       }
2335       break;
2336     }
2337     case Bytecodes::_putstatic: {
2338       for (int i = n - 1; i >= 0; i--) {
2339         current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2340       }
2341       break;
2342     }
2343     case Bytecodes::_getfield: {
2344       stack_object_type = current_frame->pop_stack(
2345         target_class_type, CHECK_VERIFY(this));
2346       for (int i = 0; i < n; i++) {
2347         current_frame->push_stack(field_type[i], CHECK_VERIFY(this));


2983            "Signature verification types array return type is bogus");
2984     for (int i = nargs; i < sig_verif_types_len; i++) {
2985       assert(i == nargs || sig_verif_types->at(i).is_long2() ||
2986              sig_verif_types->at(i).is_double2(), "Unexpected return verificationType");
2987       current_frame->push_stack(sig_verif_types->at(i), CHECK_VERIFY(this));
2988     }
2989   }
2990 }
2991 
2992 VerificationType ClassVerifier::get_newarray_type(
2993     u2 index, u2 bci, TRAPS) {
2994   const char* from_bt[] = {
2995     NULL, NULL, NULL, NULL, "[Z", "[C", "[F", "[D", "[B", "[S", "[I", "[J",
2996   };
2997   if (index < T_BOOLEAN || index > T_LONG) {
2998     verify_error(ErrorContext::bad_code(bci), "Illegal newarray instruction");
2999     return VerificationType::bogus_type();
3000   }
3001 
3002   // from_bt[index] contains the array signature which has a length of 2
3003   Symbol* sig = create_temporary_symbol(from_bt[index], 2);

3004   return VerificationType::reference_type(sig);
3005 }
3006 
3007 void ClassVerifier::verify_anewarray(
3008     u2 bci, u2 index, const constantPoolHandle& cp,
3009     StackMapFrame* current_frame, TRAPS) {
3010   verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
3011   current_frame->pop_stack(
3012     VerificationType::integer_type(), CHECK_VERIFY(this));
3013 
3014   if (was_recursively_verified()) return;
3015   VerificationType component_type =
3016     cp_index_to_type(index, cp, CHECK_VERIFY(this));
3017   int length;
3018   char* arr_sig_str;
3019   if (component_type.is_array()) {     // it's an array
3020     const char* component_name = component_type.name()->as_utf8();
3021     // Check for more than MAX_ARRAY_DIMENSIONS
3022     length = (int)strlen(component_name);
3023     if (length > MAX_ARRAY_DIMENSIONS &&
3024         component_name[MAX_ARRAY_DIMENSIONS - 1] == '[') {
3025       verify_error(ErrorContext::bad_code(bci),
3026         "Illegal anewarray instruction, array has more than 255 dimensions");
3027     }
3028     // add one dimension to component
3029     length++;
3030     arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length + 1);
3031     int n = os::snprintf(arr_sig_str, length + 1, "[%s", component_name);
3032     assert(n == length, "Unexpected number of characters in string");
3033   } else {         // it's an object or interface
3034     const char* component_name = component_type.name()->as_utf8();
3035     // add one dimension to component with 'L' prepended and ';' postpended.
3036     length = (int)strlen(component_name) + 3;
3037     arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length + 1);
3038     int n = os::snprintf(arr_sig_str, length + 1, "[L%s;", component_name);
3039     assert(n == length, "Unexpected number of characters in string");
3040   }
3041   Symbol* arr_sig = create_temporary_symbol(arr_sig_str, length);

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 


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) {
3150   const char* name = (const char*)s->base() + begin;
3151   int length = end - begin;
3152   return create_temporary_symbol(name, length);
3153 }
3154 
3155 Symbol* ClassVerifier::create_temporary_symbol(const char *name, int length) {
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);
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 }


  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


 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 


 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) {


 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()) {


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;


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;


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) {


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));


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 


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 }
< prev index next >