src/share/vm/classfile/verifier.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File bug_jdk8026065 Sdiff src/share/vm/classfile

src/share/vm/classfile/verifier.cpp

Print this page




2285       if (m->is_protected() && !mh->is_same_class_package(_klass())) {
2286         bool assignable = current_type().is_assignable_from(
2287           objectref_type, this, CHECK_VERIFY(this));
2288         if (!assignable) {
2289           verify_error(ErrorContext::bad_type(bci,
2290               TypeOrigin::cp(new_class_index, objectref_type),
2291               TypeOrigin::implicit(current_type())),
2292               "Bad access to protected <init> method");
2293           return;
2294         }
2295       }
2296     }
2297     current_frame->initialize_object(type, new_class_type);
2298   } else {
2299     verify_error(ErrorContext::bad_type(bci, current_frame->stack_top_ctx()),
2300         "Bad operand type when invoking <init>");
2301     return;
2302   }
2303 }
2304 


















2305 void ClassVerifier::verify_invoke_instructions(
2306     RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
2307     bool *this_uninit, VerificationType return_type,
2308     constantPoolHandle cp, TRAPS) {
2309   // Make sure the constant pool item is the right type
2310   u2 index = bcs->get_index_u2();
2311   Bytecodes::Code opcode = bcs->raw_code();
2312   unsigned int types;
2313   switch (opcode) {
2314     case Bytecodes::_invokeinterface:
2315       types = 1 << JVM_CONSTANT_InterfaceMethodref;
2316       break;
2317     case Bytecodes::_invokedynamic:
2318       types = 1 << JVM_CONSTANT_InvokeDynamic;
2319       break;
2320     case Bytecodes::_invokespecial:
2321     case Bytecodes::_invokestatic:
2322       types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ?
2323         (1 << JVM_CONSTANT_Methodref) :
2324         ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref));


2415   }
2416 
2417   if (opcode == Bytecodes::_invokedynamic) {
2418     address bcp = bcs->bcp();
2419     if (*(bcp+3) != 0 || *(bcp+4) != 0) {
2420       verify_error(ErrorContext::bad_code(bci),
2421           "Third and fourth operand bytes of invokedynamic must be zero");
2422       return;
2423     }
2424   }
2425 
2426   if (method_name->byte_at(0) == '<') {
2427     // Make sure <init> can only be invoked by invokespecial
2428     if (opcode != Bytecodes::_invokespecial ||
2429         method_name != vmSymbols::object_initializer_name()) {
2430       verify_error(ErrorContext::bad_code(bci),
2431           "Illegal call to internal method");
2432       return;
2433     }
2434   } else if (opcode == Bytecodes::_invokespecial
2435              && !ref_class_type.equals(current_type())
2436              && !ref_class_type.equals(VerificationType::reference_type(
2437                   current_class()->super()->name()))) {
2438     bool subtype = false;

2439     if (!current_class()->is_anonymous()) {
2440       subtype = ref_class_type.is_assignable_from(
2441                  current_type(), this, CHECK_VERIFY(this));
2442     } else {
2443       subtype = ref_class_type.is_assignable_from(VerificationType::reference_type(
2444                  current_class()->host_klass()->name()), this, CHECK_VERIFY(this));





2445     }
2446     if (!subtype) {
2447       verify_error(ErrorContext::bad_code(bci),
2448           "Bad invokespecial instruction: "
2449           "current class isn't assignable to reference class.");
2450        return;






2451     }

2452   }
2453   // Match method descriptor with operand stack
2454   for (int i = nargs - 1; i >= 0; i--) {  // Run backwards
2455     current_frame->pop_stack(sig_types[i], CHECK_VERIFY(this));
2456   }
2457   // Check objectref on operand stack
2458   if (opcode != Bytecodes::_invokestatic &&
2459       opcode != Bytecodes::_invokedynamic) {
2460     if (method_name == vmSymbols::object_initializer_name()) {  // <init> method
2461       verify_invoke_init(bcs, index, ref_class_type, current_frame,
2462         code_length, this_uninit, cp, CHECK_VERIFY(this));
2463     } else {   // other methods
2464       // Ensures that target class is assignable to method class.
2465       if (opcode == Bytecodes::_invokespecial) {
2466         if (!current_class()->is_anonymous()) {
2467           current_frame->pop_stack(current_type(), CHECK_VERIFY(this));
2468         } else {
2469           // anonymous class invokespecial calls: check if the
2470           // objectref is a subtype of the host_klass of the current class
2471           // to allow an anonymous class to reference methods in the host_klass




2285       if (m->is_protected() && !mh->is_same_class_package(_klass())) {
2286         bool assignable = current_type().is_assignable_from(
2287           objectref_type, this, CHECK_VERIFY(this));
2288         if (!assignable) {
2289           verify_error(ErrorContext::bad_type(bci,
2290               TypeOrigin::cp(new_class_index, objectref_type),
2291               TypeOrigin::implicit(current_type())),
2292               "Bad access to protected <init> method");
2293           return;
2294         }
2295       }
2296     }
2297     current_frame->initialize_object(type, new_class_type);
2298   } else {
2299     verify_error(ErrorContext::bad_type(bci, current_frame->stack_top_ctx()),
2300         "Bad operand type when invoking <init>");
2301     return;
2302   }
2303 }
2304 
2305 bool ClassVerifier::is_same_or_direct_interface(
2306     instanceKlassHandle klass,
2307     VerificationType klass_type,
2308     VerificationType ref_class_type) {
2309   if (ref_class_type.equals(klass_type)) return true;
2310   Array<Klass*>* local_interfaces = klass->local_interfaces();
2311   if (local_interfaces != NULL) {
2312     for (int x = 0; x < local_interfaces->length(); x++) {
2313       Klass* k = local_interfaces->at(x);
2314       assert (k != NULL && k->is_interface(), "invalid interface");
2315       if (ref_class_type.equals(VerificationType::reference_type(k->name()))) {
2316         return true;
2317       }
2318     }
2319   }
2320   return false;
2321 }
2322 
2323 void ClassVerifier::verify_invoke_instructions(
2324     RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
2325     bool *this_uninit, VerificationType return_type,
2326     constantPoolHandle cp, TRAPS) {
2327   // Make sure the constant pool item is the right type
2328   u2 index = bcs->get_index_u2();
2329   Bytecodes::Code opcode = bcs->raw_code();
2330   unsigned int types;
2331   switch (opcode) {
2332     case Bytecodes::_invokeinterface:
2333       types = 1 << JVM_CONSTANT_InterfaceMethodref;
2334       break;
2335     case Bytecodes::_invokedynamic:
2336       types = 1 << JVM_CONSTANT_InvokeDynamic;
2337       break;
2338     case Bytecodes::_invokespecial:
2339     case Bytecodes::_invokestatic:
2340       types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ?
2341         (1 << JVM_CONSTANT_Methodref) :
2342         ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref));


2433   }
2434 
2435   if (opcode == Bytecodes::_invokedynamic) {
2436     address bcp = bcs->bcp();
2437     if (*(bcp+3) != 0 || *(bcp+4) != 0) {
2438       verify_error(ErrorContext::bad_code(bci),
2439           "Third and fourth operand bytes of invokedynamic must be zero");
2440       return;
2441     }
2442   }
2443 
2444   if (method_name->byte_at(0) == '<') {
2445     // Make sure <init> can only be invoked by invokespecial
2446     if (opcode != Bytecodes::_invokespecial ||
2447         method_name != vmSymbols::object_initializer_name()) {
2448       verify_error(ErrorContext::bad_code(bci),
2449           "Illegal call to internal method");
2450       return;
2451     }
2452   } else if (opcode == Bytecodes::_invokespecial
2453              && !is_same_or_direct_interface(current_class(), current_type(), ref_class_type)
2454              && !ref_class_type.equals(VerificationType::reference_type(
2455                   current_class()->super()->name()))) {
2456     bool subtype = false;
2457     bool same_or_direct = false;
2458     if (!current_class()->is_anonymous()) {
2459       subtype = ref_class_type.is_assignable_from(
2460                  current_type(), this, CHECK_VERIFY(this));
2461     } else {
2462       VerificationType host_klass_type =
2463                         VerificationType::reference_type(current_class()->host_klass()->name());
2464       subtype = ref_class_type.is_assignable_from(host_klass_type, this, CHECK_VERIFY(this));
2465       // Recheck for same or direct interface relevant to the host class
2466       same_or_direct = is_same_or_direct_interface(
2467                         InstanceKlass::cast(current_class()->host_klass()),
2468                         host_klass_type, ref_class_type);
2469     }
2470     if (!subtype) {
2471       verify_error(ErrorContext::bad_code(bci),
2472           "Bad invokespecial instruction: "
2473           "current class isn't assignable to reference class.");
2474        return;
2475     } else if (!same_or_direct &&
2476                cp->tag_at(index).value() == JVM_CONSTANT_InterfaceMethodref) {
2477       verify_error(ErrorContext::bad_code(bci),
2478           "Bad invokespecial instruction: "
2479           "interface method reference is in an indirect superinterface.");
2480       return;
2481     }
2482 
2483   }
2484   // Match method descriptor with operand stack
2485   for (int i = nargs - 1; i >= 0; i--) {  // Run backwards
2486     current_frame->pop_stack(sig_types[i], CHECK_VERIFY(this));
2487   }
2488   // Check objectref on operand stack
2489   if (opcode != Bytecodes::_invokestatic &&
2490       opcode != Bytecodes::_invokedynamic) {
2491     if (method_name == vmSymbols::object_initializer_name()) {  // <init> method
2492       verify_invoke_init(bcs, index, ref_class_type, current_frame,
2493         code_length, this_uninit, cp, CHECK_VERIFY(this));
2494     } else {   // other methods
2495       // Ensures that target class is assignable to method class.
2496       if (opcode == Bytecodes::_invokespecial) {
2497         if (!current_class()->is_anonymous()) {
2498           current_frame->pop_stack(current_type(), CHECK_VERIFY(this));
2499         } else {
2500           // anonymous class invokespecial calls: check if the
2501           // objectref is a subtype of the host_klass of the current class
2502           // to allow an anonymous class to reference methods in the host_klass


src/share/vm/classfile/verifier.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File