< prev index next >

src/share/vm/interpreter/linkResolver.cpp

Print this page




 218 #endif //ASSERT
 219 
 220 #ifndef PRODUCT
 221 void CallInfo::print() {
 222   ResourceMark rm;
 223   const char* kindstr;
 224   switch (_call_kind) {
 225   case direct_call: kindstr = "direct";  break;
 226   case vtable_call: kindstr = "vtable";  break;
 227   case itable_call: kindstr = "itable";  break;
 228   default         : kindstr = "unknown"; break;
 229   }
 230   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
 231                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
 232 }
 233 #endif
 234 
 235 //------------------------------------------------------------------------------------------------------------------------
 236 // Implementation of LinkInfo
 237 
 238 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, methodHandle current_method, TRAPS) {
 239    // resolve klass
 240   _resolved_klass = pool->klass_ref_at(index, CHECK);
 241 
 242   // Get name, signature, and static klass
 243   _name          = pool->name_ref_at(index);
 244   _signature     = pool->signature_ref_at(index);
 245   _tag           = pool->tag_ref_at(index);
 246   _current_klass = pool->pool_holder();
 247   _current_method = current_method;
 248 
 249   // Coming from the constant pool always checks access
 250   _check_access  = true;
 251 }
 252 
 253 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 254    // resolve klass
 255   _resolved_klass = pool->klass_ref_at(index, CHECK);
 256 
 257   // Get name, signature, and static klass
 258   _name          = pool->name_ref_at(index);


 298         "failed to access class %s from class %s",
 299         sel_klass->external_name(),
 300         ref_klass->external_name());
 301     } else {
 302       // Use module specific message returned by verify_class_access_msg().
 303       Exceptions::fthrow(
 304         THREAD_AND_LOCATION,
 305         vmSymbols::java_lang_IllegalAccessError(),
 306         "%s", msg);
 307     }
 308   }
 309 }
 310 
 311 //------------------------------------------------------------------------------------------------------------------------
 312 // Method resolution
 313 //
 314 // According to JVM spec. $5.4.3c & $5.4.3d
 315 
 316 // Look up method in klasses, including static methods
 317 // Then look up local default methods
 318 methodHandle LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 319                                                     bool checkpolymorphism,
 320                                                     bool in_imethod_resolve, TRAPS) {


 321   Klass* klass = link_info.resolved_klass();
 322   Symbol* name = link_info.name();
 323   Symbol* signature = link_info.signature();
 324 
 325   // Ignore overpasses so statics can be found during resolution
 326   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 327 
 328   if (klass->is_array_klass()) {
 329     // Only consider klass and super klass for arrays
 330     return methodHandle(THREAD, result);
 331   }
 332 
 333   InstanceKlass* ik = InstanceKlass::cast(klass);
 334 
 335   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 336   // ignore static and non-public methods of java.lang.Object,
 337   // like clone, finalize, registerNatives.
 338   if (in_imethod_resolve &&
 339       result != NULL &&
 340       ik->is_interface() &&
 341       (result->is_static() || !result->is_public()) &&
 342       result->method_holder() == SystemDictionary::Object_klass()) {
 343     result = NULL;
 344   }
 345 
 346   // Before considering default methods, check for an overpass in the
 347   // current class if a method has not been found.
 348   if (result == NULL) {
 349     result = ik->find_method(name, signature);
 350   }
 351 
 352   if (result == NULL) {
 353     Array<Method*>* default_methods = ik->default_methods();
 354     if (default_methods != NULL) {
 355       result = InstanceKlass::find_method(default_methods, name, signature);
 356     }
 357   }
 358 
 359   if (checkpolymorphism && result != NULL) {
 360     vmIntrinsics::ID iid = result->intrinsic_id();
 361     if (MethodHandles::is_signature_polymorphic(iid)) {
 362       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 363       return NULL;
 364     }
 365   }
 366   return methodHandle(THREAD, result);
 367 }
 368 
 369 // returns first instance method
 370 // Looks up method in classes, then looks up local default methods
 371 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 372                                                              Symbol* name,
 373                                                              Symbol* signature, TRAPS) {
 374   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 375 
 376   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 377     Klass* super_klass = result->method_holder()->super();
 378     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 379   }
 380 
 381   if (klass->is_array_klass()) {
 382     // Only consider klass and super klass for arrays
 383     return methodHandle(THREAD, result);
 384   }
 385 
 386   if (result == NULL) {


 401   Symbol* signature = resolved_method->signature();
 402   InstanceKlass* ik = InstanceKlass::cast(klass);
 403 
 404   // First check in default method array
 405   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 406     int index = InstanceKlass::find_method_index(ik->default_methods(),
 407                                                  name, signature, Klass::find_overpass,
 408                                                  Klass::find_static, Klass::find_private);
 409     if (index >= 0 ) {
 410       vtable_index = ik->default_vtable_indices()->at(index);
 411     }
 412   }
 413   if (vtable_index == Method::invalid_vtable_index) {
 414     // get vtable_index for miranda methods
 415     klassVtable vt = ik->vtable();
 416     vtable_index = vt.index_of_miranda(name, signature);
 417   }
 418   return vtable_index;
 419 }
 420 
 421 methodHandle LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info, TRAPS) {
 422   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
 423 
 424   // Specify 'true' in order to skip default methods when searching the
 425   // interfaces.  Function lookup_method_in_klasses() already looked for
 426   // the method in the default methods table.
 427   return methodHandle(THREAD,
 428     ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(),
 429     Klass::skip_defaults));
 430 }
 431 
 432 methodHandle LinkResolver::lookup_polymorphic_method(
 433                                              const LinkInfo& link_info,
 434                                              Handle *appendix_result_or_null,
 435                                              Handle *method_type_result,
 436                                              TRAPS) {
 437   Klass* klass = link_info.resolved_klass();
 438   Symbol* name = link_info.name();
 439   Symbol* full_signature = link_info.signature();
 440 
 441   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 442   if (TraceMethodHandles) {
 443     ResourceMark rm(THREAD);
 444     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 445                   vmIntrinsics::name_at(iid), klass->external_name(),
 446                   name->as_C_string(), full_signature->as_C_string());
 447   }
 448   if ((klass == SystemDictionary::MethodHandle_klass() ||
 449        klass == SystemDictionary::VarHandle_klass()) &&


 698 
 699   // 1. For invokevirtual, cannot call an interface method
 700   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 701     ResourceMark rm(THREAD);
 702     char buf[200];
 703     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 704         resolved_klass->external_name());
 705     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 706   }
 707 
 708   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 709   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 710     ResourceMark rm(THREAD);
 711     char buf[200];
 712     jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string());
 713     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 714   }
 715 
 716 
 717   // 3. lookup method in resolved klass and its super klasses
 718   methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL);
 719 
 720   // 4. lookup method in all the interfaces implemented by the resolved klass
 721   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 722     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 723 
 724     if (resolved_method.is_null()) {
 725       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 726       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 727       if (HAS_PENDING_EXCEPTION) {
 728         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 729         CLEAR_PENDING_EXCEPTION;
 730       }
 731     }
 732   }
 733 
 734   // 5. method lookup failed
 735   if (resolved_method.is_null()) {
 736     ResourceMark rm(THREAD);
 737     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 738                     Method::name_and_sig_as_C_string(resolved_klass,
 739                                                      link_info.name(),
 740                                                      link_info.signature()),
 741                     nested_exception, NULL);
 742   }
 743 
 744   // 5. access checks, access checking may be turned off when calling from within the VM.


 800   Klass* resolved_klass = link_info.resolved_klass();
 801 
 802   // check if klass is interface
 803   if (!resolved_klass->is_interface()) {
 804     ResourceMark rm(THREAD);
 805     char buf[200];
 806     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 807     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 808   }
 809 
 810   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 811   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 812     ResourceMark rm(THREAD);
 813     char buf[200];
 814     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 815     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 816   }
 817 
 818   // lookup method in this interface or its super, java.lang.Object
 819   // JDK8: also look for static methods
 820   methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL);
 821 
 822   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 823     // lookup method in all the super-interfaces
 824     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 825   }
 826 
 827   if (resolved_method.is_null()) {
 828     // no method found
 829     ResourceMark rm(THREAD);
 830     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 831                    Method::name_and_sig_as_C_string(resolved_klass,
 832                                                     link_info.name(),
 833                                                     link_info.signature()));
 834   }
 835 
 836   if (link_info.check_access()) {
 837     // JDK8 adds non-public interface methods, and accessability check requirement
 838     Klass* current_klass = link_info.current_klass();
 839 
 840     assert(current_klass != NULL , "current_klass should not be null");
 841 
 842     // check if method can be accessed by the referring class
 843     check_method_accessability(current_klass,
 844                                resolved_klass,




 218 #endif //ASSERT
 219 
 220 #ifndef PRODUCT
 221 void CallInfo::print() {
 222   ResourceMark rm;
 223   const char* kindstr;
 224   switch (_call_kind) {
 225   case direct_call: kindstr = "direct";  break;
 226   case vtable_call: kindstr = "vtable";  break;
 227   case itable_call: kindstr = "itable";  break;
 228   default         : kindstr = "unknown"; break;
 229   }
 230   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
 231                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
 232 }
 233 #endif
 234 
 235 //------------------------------------------------------------------------------------------------------------------------
 236 // Implementation of LinkInfo
 237 
 238 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS) {
 239    // resolve klass
 240   _resolved_klass = pool->klass_ref_at(index, CHECK);
 241 
 242   // Get name, signature, and static klass
 243   _name          = pool->name_ref_at(index);
 244   _signature     = pool->signature_ref_at(index);
 245   _tag           = pool->tag_ref_at(index);
 246   _current_klass = pool->pool_holder();
 247   _current_method = current_method;
 248 
 249   // Coming from the constant pool always checks access
 250   _check_access  = true;
 251 }
 252 
 253 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 254    // resolve klass
 255   _resolved_klass = pool->klass_ref_at(index, CHECK);
 256 
 257   // Get name, signature, and static klass
 258   _name          = pool->name_ref_at(index);


 298         "failed to access class %s from class %s",
 299         sel_klass->external_name(),
 300         ref_klass->external_name());
 301     } else {
 302       // Use module specific message returned by verify_class_access_msg().
 303       Exceptions::fthrow(
 304         THREAD_AND_LOCATION,
 305         vmSymbols::java_lang_IllegalAccessError(),
 306         "%s", msg);
 307     }
 308   }
 309 }
 310 
 311 //------------------------------------------------------------------------------------------------------------------------
 312 // Method resolution
 313 //
 314 // According to JVM spec. $5.4.3c & $5.4.3d
 315 
 316 // Look up method in klasses, including static methods
 317 // Then look up local default methods
 318 Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 319                                                bool checkpolymorphism,
 320                                                bool in_imethod_resolve) {
 321   NoSafepointVerifier nsv;  // Method* returned may not be reclaimed
 322 
 323   Klass* klass = link_info.resolved_klass();
 324   Symbol* name = link_info.name();
 325   Symbol* signature = link_info.signature();
 326 
 327   // Ignore overpasses so statics can be found during resolution
 328   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 329 
 330   if (klass->is_array_klass()) {
 331     // Only consider klass and super klass for arrays
 332     return result;
 333   }
 334 
 335   InstanceKlass* ik = InstanceKlass::cast(klass);
 336 
 337   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 338   // ignore static and non-public methods of java.lang.Object,
 339   // like clone, finalize, registerNatives.
 340   if (in_imethod_resolve &&
 341       result != NULL &&
 342       ik->is_interface() &&
 343       (result->is_static() || !result->is_public()) &&
 344       result->method_holder() == SystemDictionary::Object_klass()) {
 345     result = NULL;
 346   }
 347 
 348   // Before considering default methods, check for an overpass in the
 349   // current class if a method has not been found.
 350   if (result == NULL) {
 351     result = ik->find_method(name, signature);
 352   }
 353 
 354   if (result == NULL) {
 355     Array<Method*>* default_methods = ik->default_methods();
 356     if (default_methods != NULL) {
 357       result = InstanceKlass::find_method(default_methods, name, signature);
 358     }
 359   }
 360 
 361   if (checkpolymorphism && result != NULL) {
 362     vmIntrinsics::ID iid = result->intrinsic_id();
 363     if (MethodHandles::is_signature_polymorphic(iid)) {
 364       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 365       return NULL;
 366     }
 367   }
 368   return result;
 369 }
 370 
 371 // returns first instance method
 372 // Looks up method in classes, then looks up local default methods
 373 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 374                                                              Symbol* name,
 375                                                              Symbol* signature, TRAPS) {
 376   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 377 
 378   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 379     Klass* super_klass = result->method_holder()->super();
 380     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 381   }
 382 
 383   if (klass->is_array_klass()) {
 384     // Only consider klass and super klass for arrays
 385     return methodHandle(THREAD, result);
 386   }
 387 
 388   if (result == NULL) {


 403   Symbol* signature = resolved_method->signature();
 404   InstanceKlass* ik = InstanceKlass::cast(klass);
 405 
 406   // First check in default method array
 407   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 408     int index = InstanceKlass::find_method_index(ik->default_methods(),
 409                                                  name, signature, Klass::find_overpass,
 410                                                  Klass::find_static, Klass::find_private);
 411     if (index >= 0 ) {
 412       vtable_index = ik->default_vtable_indices()->at(index);
 413     }
 414   }
 415   if (vtable_index == Method::invalid_vtable_index) {
 416     // get vtable_index for miranda methods
 417     klassVtable vt = ik->vtable();
 418     vtable_index = vt.index_of_miranda(name, signature);
 419   }
 420   return vtable_index;
 421 }
 422 
 423 Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
 424   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
 425 
 426   // Specify 'true' in order to skip default methods when searching the
 427   // interfaces.  Function lookup_method_in_klasses() already looked for
 428   // the method in the default methods table.
 429   return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::skip_defaults);


 430 }
 431 
 432 methodHandle LinkResolver::lookup_polymorphic_method(
 433                                              const LinkInfo& link_info,
 434                                              Handle *appendix_result_or_null,
 435                                              Handle *method_type_result,
 436                                              TRAPS) {
 437   Klass* klass = link_info.resolved_klass();
 438   Symbol* name = link_info.name();
 439   Symbol* full_signature = link_info.signature();
 440 
 441   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 442   if (TraceMethodHandles) {
 443     ResourceMark rm(THREAD);
 444     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 445                   vmIntrinsics::name_at(iid), klass->external_name(),
 446                   name->as_C_string(), full_signature->as_C_string());
 447   }
 448   if ((klass == SystemDictionary::MethodHandle_klass() ||
 449        klass == SystemDictionary::VarHandle_klass()) &&


 698 
 699   // 1. For invokevirtual, cannot call an interface method
 700   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 701     ResourceMark rm(THREAD);
 702     char buf[200];
 703     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 704         resolved_klass->external_name());
 705     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 706   }
 707 
 708   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 709   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 710     ResourceMark rm(THREAD);
 711     char buf[200];
 712     jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string());
 713     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 714   }
 715 
 716 
 717   // 3. lookup method in resolved klass and its super klasses
 718   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 719 
 720   // 4. lookup method in all the interfaces implemented by the resolved klass
 721   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 722     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 723 
 724     if (resolved_method == NULL) {
 725       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 726       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 727       if (HAS_PENDING_EXCEPTION) {
 728         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 729         CLEAR_PENDING_EXCEPTION;
 730       }
 731     }
 732   }
 733 
 734   // 5. method lookup failed
 735   if (resolved_method.is_null()) {
 736     ResourceMark rm(THREAD);
 737     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 738                     Method::name_and_sig_as_C_string(resolved_klass,
 739                                                      link_info.name(),
 740                                                      link_info.signature()),
 741                     nested_exception, NULL);
 742   }
 743 
 744   // 5. access checks, access checking may be turned off when calling from within the VM.


 800   Klass* resolved_klass = link_info.resolved_klass();
 801 
 802   // check if klass is interface
 803   if (!resolved_klass->is_interface()) {
 804     ResourceMark rm(THREAD);
 805     char buf[200];
 806     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 807     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 808   }
 809 
 810   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 811   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 812     ResourceMark rm(THREAD);
 813     char buf[200];
 814     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 815     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 816   }
 817 
 818   // lookup method in this interface or its super, java.lang.Object
 819   // JDK8: also look for static methods
 820   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 821 
 822   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 823     // lookup method in all the super-interfaces
 824     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 825   }
 826 
 827   if (resolved_method.is_null()) {
 828     // no method found
 829     ResourceMark rm(THREAD);
 830     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 831                    Method::name_and_sig_as_C_string(resolved_klass,
 832                                                     link_info.name(),
 833                                                     link_info.signature()));
 834   }
 835 
 836   if (link_info.check_access()) {
 837     // JDK8 adds non-public interface methods, and accessability check requirement
 838     Klass* current_klass = link_info.current_klass();
 839 
 840     assert(current_klass != NULL , "current_klass should not be null");
 841 
 842     // check if method can be accessed by the referring class
 843     check_method_accessability(current_klass,
 844                                resolved_klass,


< prev index next >