< prev index next >

src/hotspot/share/classfile/verifier.cpp

Print this page




 615 
 616   if (was_recursively_verified()){
 617     log_info(verification)("Recursive verification detected for: %s", _klass->external_name());
 618     log_info(class, init)("Recursive verification detected for: %s",
 619                         _klass->external_name());
 620   }
 621 }
 622 
 623 void ClassVerifier::verify_method(const methodHandle& m, TRAPS) {
 624   HandleMark hm(THREAD);
 625   _method = m;   // initialize _method
 626   log_info(verification)("Verifying method %s", m->name_and_sig_as_C_string());
 627 
 628 // For clang, the only good constant format string is a literal constant format string.
 629 #define bad_type_msg "Bad type on operand stack in %s"
 630 
 631   int32_t max_stack = m->verifier_max_stack();
 632   int32_t max_locals = m->max_locals();
 633   constantPoolHandle cp(THREAD, m->constants());
 634 
 635   if (!SignatureVerifier::is_valid_method_signature(m->signature())) {
 636     class_format_error("Invalid method signature");
 637     return;
 638   }
 639 
 640   // Initial stack map frame: offset is 0, stack is initially empty.
 641   StackMapFrame current_frame(max_locals, max_stack, this);
 642   // Set initial locals
 643   VerificationType return_type = current_frame.set_locals_from_arg(
 644     m, current_type(), CHECK_VERIFY(this));
 645 
 646   int32_t stackmap_index = 0; // index to the stackmap array
 647 
 648   u4 code_length = m->code_size();
 649 
 650   // Scan the bytecode and map each instruction's start offset to a number.
 651   char* code_data = generate_code_data(m, code_length, CHECK_VERIFY(this));
 652 
 653   int ex_min = code_length;
 654   int ex_max = -1;
 655   // Look through each item on the exception table. Each of the fields must refer
 656   // to a legal instruction.
 657   if (was_recursively_verified()) return;
 658   verify_exception_handler_table(


2093     current_frame->push_stack(
2094       VerificationType::float_type(), CHECK_VERIFY(this));
2095   } else if (tag.is_double()) {
2096     current_frame->push_stack_2(
2097       VerificationType::double_type(),
2098       VerificationType::double2_type(), CHECK_VERIFY(this));
2099   } else if (tag.is_long()) {
2100     current_frame->push_stack_2(
2101       VerificationType::long_type(),
2102       VerificationType::long2_type(), CHECK_VERIFY(this));
2103   } else if (tag.is_method_handle()) {
2104     current_frame->push_stack(
2105       VerificationType::reference_type(
2106         vmSymbols::java_lang_invoke_MethodHandle()), CHECK_VERIFY(this));
2107   } else if (tag.is_method_type()) {
2108     current_frame->push_stack(
2109       VerificationType::reference_type(
2110         vmSymbols::java_lang_invoke_MethodType()), CHECK_VERIFY(this));
2111   } else if (tag.is_dynamic_constant()) {
2112     Symbol* constant_type = cp->uncached_signature_ref_at(index);
2113     if (!SignatureVerifier::is_valid_type_signature(constant_type)) {
2114       class_format_error(
2115         "Invalid type for dynamic constant in class %s referenced "
2116         "from constant pool index %d", _klass->external_name(), index);
2117       return;
2118     }
2119     assert(sizeof(VerificationType) == sizeof(uintptr_t),
2120           "buffer type must match VerificationType size");
2121     uintptr_t constant_type_buffer[2];
2122     VerificationType* v_constant_type = (VerificationType*)constant_type_buffer;
2123     SignatureStream sig_stream(constant_type, false);
2124     int n = change_sig_to_verificationType(
2125       &sig_stream, v_constant_type, CHECK_VERIFY(this));
2126     int opcode_n = (opcode == Bytecodes::_ldc2_w ? 2 : 1);
2127     if (n != opcode_n) {
2128       // wrong kind of ldc; reverify against updated type mask
2129       types &= ~(1 << JVM_CONSTANT_Dynamic);
2130       verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2131     }
2132     for (int i = 0; i < n; i++) {
2133       current_frame->push_stack(v_constant_type[i], CHECK_VERIFY(this));
2134     }
2135   } else {
2136     /* Unreachable? verify_cp_type has already validated the cp type. */
2137     verify_error(
2138         ErrorContext::bad_cp_index(bci, index), "Invalid index in ldc");


2218       return true;
2219     }
2220     super = super->super();
2221   }
2222   return false;
2223 }
2224 
2225 void ClassVerifier::verify_field_instructions(RawBytecodeStream* bcs,
2226                                               StackMapFrame* current_frame,
2227                                               const constantPoolHandle& cp,
2228                                               bool allow_arrays,
2229                                               TRAPS) {
2230   u2 index = bcs->get_index_u2();
2231   verify_cp_type(bcs->bci(), index, cp,
2232       1 << JVM_CONSTANT_Fieldref, CHECK_VERIFY(this));
2233 
2234   // Get field name and signature
2235   Symbol* field_name = cp->name_ref_at(index);
2236   Symbol* field_sig = cp->signature_ref_at(index);
2237 
2238   if (!SignatureVerifier::is_valid_type_signature(field_sig)) {
2239     class_format_error(
2240       "Invalid signature for field in class %s referenced "
2241       "from constant pool index %d", _klass->external_name(), index);
2242     return;
2243   }
2244 
2245   // Get referenced class type
2246   VerificationType ref_class_type = cp_ref_index_to_type(
2247     index, cp, CHECK_VERIFY(this));
2248   if (!ref_class_type.is_object() &&
2249     (!allow_arrays || !ref_class_type.is_array())) {
2250     verify_error(ErrorContext::bad_type(bcs->bci(),
2251         TypeOrigin::cp(index, ref_class_type)),
2252         "Expecting reference to class in class %s at constant pool index %d",
2253         _klass->external_name(), index);
2254     return;
2255   }
2256   VerificationType target_class_type = ref_class_type;
2257 
2258   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2259         "buffer type must match VerificationType size");
2260   uintptr_t field_type_buffer[2];
2261   VerificationType* field_type = (VerificationType*)field_type_buffer;
2262   // If we make a VerificationType[2] array directly, the compiler calls
2263   // to the c-runtime library to do the allocation instead of just


2702       types = 1 << JVM_CONSTANT_InterfaceMethodref;
2703       break;
2704     case Bytecodes::_invokedynamic:
2705       types = 1 << JVM_CONSTANT_InvokeDynamic;
2706       break;
2707     case Bytecodes::_invokespecial:
2708     case Bytecodes::_invokestatic:
2709       types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ?
2710         (1 << JVM_CONSTANT_Methodref) :
2711         ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref));
2712       break;
2713     default:
2714       types = 1 << JVM_CONSTANT_Methodref;
2715   }
2716   verify_cp_type(bcs->bci(), index, cp, types, CHECK_VERIFY(this));
2717 
2718   // Get method name and signature
2719   Symbol* method_name = cp->name_ref_at(index);
2720   Symbol* method_sig = cp->signature_ref_at(index);
2721 
2722   if (!SignatureVerifier::is_valid_method_signature(method_sig)) {
2723     class_format_error(
2724       "Invalid method signature in class %s referenced "
2725       "from constant pool index %d", _klass->external_name(), index);
2726     return;
2727   }
2728 
2729   // Get referenced class type
2730   VerificationType ref_class_type;
2731   if (opcode == Bytecodes::_invokedynamic) {
2732     if (_klass->major_version() < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2733       class_format_error(
2734         "invokedynamic instructions not supported by this class file version (%d), class %s",
2735         _klass->major_version(), _klass->external_name());
2736       return;
2737     }
2738   } else {
2739     ref_class_type = cp_ref_index_to_type(index, cp, CHECK_VERIFY(this));
2740   }
2741 
2742   // For a small signature length, we just allocate 128 bytes instead
2743   // of parsing the signature once to find its size.
2744   // -3 is for '(', ')' and return descriptor; multiply by 2 is for
2745   // longs/doubles to be consertive.
2746   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2747         "buffer type must match VerificationType size");




 615 
 616   if (was_recursively_verified()){
 617     log_info(verification)("Recursive verification detected for: %s", _klass->external_name());
 618     log_info(class, init)("Recursive verification detected for: %s",
 619                         _klass->external_name());
 620   }
 621 }
 622 
 623 void ClassVerifier::verify_method(const methodHandle& m, TRAPS) {
 624   HandleMark hm(THREAD);
 625   _method = m;   // initialize _method
 626   log_info(verification)("Verifying method %s", m->name_and_sig_as_C_string());
 627 
 628 // For clang, the only good constant format string is a literal constant format string.
 629 #define bad_type_msg "Bad type on operand stack in %s"
 630 
 631   int32_t max_stack = m->verifier_max_stack();
 632   int32_t max_locals = m->max_locals();
 633   constantPoolHandle cp(THREAD, m->constants());
 634 
 635   // Method signature was checked in ClassFileParser.
 636   assert(SignatureVerifier::is_valid_method_signature(m->signature()),
 637          "Invalid method signature");

 638 
 639   // Initial stack map frame: offset is 0, stack is initially empty.
 640   StackMapFrame current_frame(max_locals, max_stack, this);
 641   // Set initial locals
 642   VerificationType return_type = current_frame.set_locals_from_arg(
 643     m, current_type(), CHECK_VERIFY(this));
 644 
 645   int32_t stackmap_index = 0; // index to the stackmap array
 646 
 647   u4 code_length = m->code_size();
 648 
 649   // Scan the bytecode and map each instruction's start offset to a number.
 650   char* code_data = generate_code_data(m, code_length, CHECK_VERIFY(this));
 651 
 652   int ex_min = code_length;
 653   int ex_max = -1;
 654   // Look through each item on the exception table. Each of the fields must refer
 655   // to a legal instruction.
 656   if (was_recursively_verified()) return;
 657   verify_exception_handler_table(


2092     current_frame->push_stack(
2093       VerificationType::float_type(), CHECK_VERIFY(this));
2094   } else if (tag.is_double()) {
2095     current_frame->push_stack_2(
2096       VerificationType::double_type(),
2097       VerificationType::double2_type(), CHECK_VERIFY(this));
2098   } else if (tag.is_long()) {
2099     current_frame->push_stack_2(
2100       VerificationType::long_type(),
2101       VerificationType::long2_type(), CHECK_VERIFY(this));
2102   } else if (tag.is_method_handle()) {
2103     current_frame->push_stack(
2104       VerificationType::reference_type(
2105         vmSymbols::java_lang_invoke_MethodHandle()), CHECK_VERIFY(this));
2106   } else if (tag.is_method_type()) {
2107     current_frame->push_stack(
2108       VerificationType::reference_type(
2109         vmSymbols::java_lang_invoke_MethodType()), CHECK_VERIFY(this));
2110   } else if (tag.is_dynamic_constant()) {
2111     Symbol* constant_type = cp->uncached_signature_ref_at(index);
2112     // Field signature was checked in ClassFileParser.
2113     assert(SignatureVerifier::is_valid_type_signature(constant_type),
2114            "Bad field signature");



2115     assert(sizeof(VerificationType) == sizeof(uintptr_t),
2116           "buffer type must match VerificationType size");
2117     uintptr_t constant_type_buffer[2];
2118     VerificationType* v_constant_type = (VerificationType*)constant_type_buffer;
2119     SignatureStream sig_stream(constant_type, false);
2120     int n = change_sig_to_verificationType(
2121       &sig_stream, v_constant_type, CHECK_VERIFY(this));
2122     int opcode_n = (opcode == Bytecodes::_ldc2_w ? 2 : 1);
2123     if (n != opcode_n) {
2124       // wrong kind of ldc; reverify against updated type mask
2125       types &= ~(1 << JVM_CONSTANT_Dynamic);
2126       verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
2127     }
2128     for (int i = 0; i < n; i++) {
2129       current_frame->push_stack(v_constant_type[i], CHECK_VERIFY(this));
2130     }
2131   } else {
2132     /* Unreachable? verify_cp_type has already validated the cp type. */
2133     verify_error(
2134         ErrorContext::bad_cp_index(bci, index), "Invalid index in ldc");


2214       return true;
2215     }
2216     super = super->super();
2217   }
2218   return false;
2219 }
2220 
2221 void ClassVerifier::verify_field_instructions(RawBytecodeStream* bcs,
2222                                               StackMapFrame* current_frame,
2223                                               const constantPoolHandle& cp,
2224                                               bool allow_arrays,
2225                                               TRAPS) {
2226   u2 index = bcs->get_index_u2();
2227   verify_cp_type(bcs->bci(), index, cp,
2228       1 << JVM_CONSTANT_Fieldref, CHECK_VERIFY(this));
2229 
2230   // Get field name and signature
2231   Symbol* field_name = cp->name_ref_at(index);
2232   Symbol* field_sig = cp->signature_ref_at(index);
2233 
2234   // Field signature was checked in ClassFileParser.
2235   assert(SignatureVerifier::is_valid_type_signature(field_sig),
2236          "Bad field signature");



2237 
2238   // Get referenced class type
2239   VerificationType ref_class_type = cp_ref_index_to_type(
2240     index, cp, CHECK_VERIFY(this));
2241   if (!ref_class_type.is_object() &&
2242     (!allow_arrays || !ref_class_type.is_array())) {
2243     verify_error(ErrorContext::bad_type(bcs->bci(),
2244         TypeOrigin::cp(index, ref_class_type)),
2245         "Expecting reference to class in class %s at constant pool index %d",
2246         _klass->external_name(), index);
2247     return;
2248   }
2249   VerificationType target_class_type = ref_class_type;
2250 
2251   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2252         "buffer type must match VerificationType size");
2253   uintptr_t field_type_buffer[2];
2254   VerificationType* field_type = (VerificationType*)field_type_buffer;
2255   // If we make a VerificationType[2] array directly, the compiler calls
2256   // to the c-runtime library to do the allocation instead of just


2695       types = 1 << JVM_CONSTANT_InterfaceMethodref;
2696       break;
2697     case Bytecodes::_invokedynamic:
2698       types = 1 << JVM_CONSTANT_InvokeDynamic;
2699       break;
2700     case Bytecodes::_invokespecial:
2701     case Bytecodes::_invokestatic:
2702       types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ?
2703         (1 << JVM_CONSTANT_Methodref) :
2704         ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref));
2705       break;
2706     default:
2707       types = 1 << JVM_CONSTANT_Methodref;
2708   }
2709   verify_cp_type(bcs->bci(), index, cp, types, CHECK_VERIFY(this));
2710 
2711   // Get method name and signature
2712   Symbol* method_name = cp->name_ref_at(index);
2713   Symbol* method_sig = cp->signature_ref_at(index);
2714 
2715   // Method signature was checked in ClassFileParser.
2716   assert(SignatureVerifier::is_valid_method_signature(method_sig),
2717          "Bad method signature");



2718 
2719   // Get referenced class type
2720   VerificationType ref_class_type;
2721   if (opcode == Bytecodes::_invokedynamic) {
2722     if (_klass->major_version() < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2723       class_format_error(
2724         "invokedynamic instructions not supported by this class file version (%d), class %s",
2725         _klass->major_version(), _klass->external_name());
2726       return;
2727     }
2728   } else {
2729     ref_class_type = cp_ref_index_to_type(index, cp, CHECK_VERIFY(this));
2730   }
2731 
2732   // For a small signature length, we just allocate 128 bytes instead
2733   // of parsing the signature once to find its size.
2734   // -3 is for '(', ')' and return descriptor; multiply by 2 is for
2735   // longs/doubles to be consertive.
2736   assert(sizeof(VerificationType) == sizeof(uintptr_t),
2737         "buffer type must match VerificationType size");


< prev index next >