comparison src/share/vm/oops/klassVtable.cpp @ 12823:ac9cb1d5a202

8009130: Lambda: Fix access controls, loader constraints. Summary: New default methods list with inherited superinterface methods Reviewed-by: minqi, sspitsyn, coleenp
author acorn
date Mon, 07 Oct 2013 12:20:28 -0400
parents 36b97be47bde
children fce21ac5968d
comparison
equal deleted inserted replaced
12822:cc4f5f8d885e 12823:ac9cb1d5a202
81 } 81 }
82 } 82 }
83 83
84 GrowableArray<Method*> new_mirandas(20); 84 GrowableArray<Method*> new_mirandas(20);
85 // compute the number of mirandas methods that must be added to the end 85 // compute the number of mirandas methods that must be added to the end
86 get_mirandas(&new_mirandas, all_mirandas, super, methods, local_interfaces); 86 get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces);
87 *num_new_mirandas = new_mirandas.length(); 87 *num_new_mirandas = new_mirandas.length();
88 88
89 vtable_length += *num_new_mirandas * vtableEntry::size(); 89 vtable_length += *num_new_mirandas * vtableEntry::size();
90 90
91 if (Universe::is_bootstrapping() && vtable_length == 0) { 91 if (Universe::is_bootstrapping() && vtable_length == 0) {
184 // update_inherited_vtable can stop for gc - ensure using handles 184 // update_inherited_vtable can stop for gc - ensure using handles
185 HandleMark hm(THREAD); 185 HandleMark hm(THREAD);
186 assert(methods->at(i)->is_method(), "must be a Method*"); 186 assert(methods->at(i)->is_method(), "must be a Method*");
187 methodHandle mh(THREAD, methods->at(i)); 187 methodHandle mh(THREAD, methods->at(i));
188 188
189 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, checkconstraints, CHECK); 189 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK);
190 190
191 if (needs_new_entry) { 191 if (needs_new_entry) {
192 put_method_at(mh(), initialized); 192 put_method_at(mh(), initialized);
193 mh()->set_vtable_index(initialized); // set primary vtable index 193 mh()->set_vtable_index(initialized); // set primary vtable index
194 initialized++; 194 initialized++;
195 } 195 }
196 } 196 }
197 197
198 // add miranda methods to end of vtable. 198 // update vtable with default_methods
199 Array<Method*>* default_methods = ik()->default_methods();
200 if (default_methods != NULL) {
201 len = default_methods->length();
202 if (len > 0) {
203 Array<int>* def_vtable_indices = NULL;
204 if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) {
205 def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK);
206 } else {
207 assert(def_vtable_indices->length() == len, "reinit vtable len?");
208 }
209 for (int i = 0; i < len; i++) {
210 HandleMark hm(THREAD);
211 assert(default_methods->at(i)->is_method(), "must be a Method*");
212 methodHandle mh(THREAD, default_methods->at(i));
213
214 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK);
215
216 // needs new entry
217 if (needs_new_entry) {
218 put_method_at(mh(), initialized);
219 def_vtable_indices->at_put(i, initialized); //set vtable index
220 initialized++;
221 }
222 }
223 }
224 }
225
226 // add miranda methods; it will also return the updated initialized
199 initialized = fill_in_mirandas(initialized); 227 initialized = fill_in_mirandas(initialized);
200 228
201 // In class hierarchies where the accessibility is not increasing (i.e., going from private -> 229 // In class hierarchies where the accessibility is not increasing (i.e., going from private ->
202 // package_private -> public/protected), the vtable might actually be smaller than our initial 230 // package_private -> public/protected), the vtable might actually be smaller than our initial
203 // calculation. 231 // calculation.
228 #endif 256 #endif
229 if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) { 257 if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) {
230 #ifndef PRODUCT 258 #ifndef PRODUCT
231 if (PrintVtables && Verbose) { 259 if (PrintVtables && Verbose) {
232 ResourceMark rm(THREAD); 260 ResourceMark rm(THREAD);
261 char* sig = target_method()->name_and_sig_as_C_string();
233 tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ", 262 tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
234 supersuperklass->internal_name(), 263 supersuperklass->internal_name(),
235 _klass->internal_name(), (target_method() != NULL) ? 264 _klass->internal_name(), sig, vtable_index);
236 target_method()->name()->as_C_string() : "<NULL>", vtable_index);
237 super_method->access_flags().print_on(tty); 265 super_method->access_flags().print_on(tty);
266 if (super_method->is_default_method()) {
267 tty->print("default");
268 }
238 tty->print("overriders flags: "); 269 tty->print("overriders flags: ");
239 target_method->access_flags().print_on(tty); 270 target_method->access_flags().print_on(tty);
240 tty->cr(); 271 if (target_method->is_default_method()) {
272 tty->print("default");
273 }
241 } 274 }
242 #endif /*PRODUCT*/ 275 #endif /*PRODUCT*/
243 break; // return found superk 276 break; // return found superk
244 } 277 }
245 } else { 278 } else {
256 289
257 // Update child's copy of super vtable for overrides 290 // Update child's copy of super vtable for overrides
258 // OR return true if a new vtable entry is required. 291 // OR return true if a new vtable entry is required.
259 // Only called for InstanceKlass's, i.e. not for arrays 292 // Only called for InstanceKlass's, i.e. not for arrays
260 // If that changed, could not use _klass as handle for klass 293 // If that changed, could not use _klass as handle for klass
261 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, 294 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
262 bool checkconstraints, TRAPS) { 295 int super_vtable_len, int default_index,
296 bool checkconstraints, TRAPS) {
263 ResourceMark rm; 297 ResourceMark rm;
264 bool allocate_new = true; 298 bool allocate_new = true;
265 assert(klass->oop_is_instance(), "must be InstanceKlass"); 299 assert(klass->oop_is_instance(), "must be InstanceKlass");
266 assert(klass == target_method()->method_holder(), "caller resp."); 300
267 301 Array<int>* def_vtable_indices = NULL;
268 // Initialize the method's vtable index to "nonvirtual". 302 bool is_default = false;
269 // If we allocate a vtable entry, we will update it to a non-negative number. 303 // default methods are concrete methods in superinterfaces which are added to the vtable
270 target_method()->set_vtable_index(Method::nonvirtual_vtable_index); 304 // with their real method_holder
305 // Since vtable and itable indices share the same storage, don't touch
306 // the default method's real vtable/itable index
307 // default_vtable_indices stores the vtable value relative to this inheritor
308 if (default_index >= 0 ) {
309 is_default = true;
310 def_vtable_indices = klass->default_vtable_indices();
311 assert(def_vtable_indices != NULL, "def vtable alloc?");
312 assert(default_index <= def_vtable_indices->length(), "def vtable len?");
313 } else {
314 assert(klass == target_method()->method_holder(), "caller resp.");
315 // Initialize the method's vtable index to "nonvirtual".
316 // If we allocate a vtable entry, we will update it to a non-negative number.
317 target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
318 }
271 319
272 // Static and <init> methods are never in 320 // Static and <init> methods are never in
273 if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) { 321 if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) {
274 return false; 322 return false;
275 } 323 }
282 } else if (klass->is_interface()) { 330 } else if (klass->is_interface()) {
283 allocate_new = false; // see note below in needs_new_vtable_entry 331 allocate_new = false; // see note below in needs_new_vtable_entry
284 // An interface never allocates new vtable slots, only inherits old ones. 332 // An interface never allocates new vtable slots, only inherits old ones.
285 // This method will either be assigned its own itable index later, 333 // This method will either be assigned its own itable index later,
286 // or be assigned an inherited vtable index in the loop below. 334 // or be assigned an inherited vtable index in the loop below.
335 // default methods store their vtable indices in the inheritors default_vtable_indices
336 assert (default_index == -1, "interfaces don't store resolved default methods");
287 target_method()->set_vtable_index(Method::pending_itable_index); 337 target_method()->set_vtable_index(Method::pending_itable_index);
288 } 338 }
289 339
290 // we need a new entry if there is no superclass 340 // we need a new entry if there is no superclass
291 if (klass->super() == NULL) { 341 if (klass->super() == NULL) {
305 // which can block for gc, once we are in this loop, use handles 355 // which can block for gc, once we are in this loop, use handles
306 // For classfiles built with >= jdk7, we now look for transitive overrides 356 // For classfiles built with >= jdk7, we now look for transitive overrides
307 357
308 Symbol* name = target_method()->name(); 358 Symbol* name = target_method()->name();
309 Symbol* signature = target_method()->signature(); 359 Symbol* signature = target_method()->signature();
310 Handle target_loader(THREAD, _klass()->class_loader()); 360
311 Symbol* target_classname = _klass->name(); 361 KlassHandle target_klass(THREAD, target_method()->method_holder());
362 if (target_klass == NULL) {
363 target_klass = _klass;
364 }
365
366 Handle target_loader(THREAD, target_klass->class_loader());
367
368 Symbol* target_classname = target_klass->name();
312 for(int i = 0; i < super_vtable_len; i++) { 369 for(int i = 0; i < super_vtable_len; i++) {
313 Method* super_method = method_at(i); 370 Method* super_method = method_at(i);
314 // Check if method name matches 371 // Check if method name matches
315 if (super_method->name() == name && super_method->signature() == signature) { 372 if (super_method->name() == name && super_method->signature() == signature) {
316 373
317 // get super_klass for method_holder for the found method 374 // get super_klass for method_holder for the found method
318 InstanceKlass* super_klass = super_method->method_holder(); 375 InstanceKlass* super_klass = super_method->method_holder();
319 376
320 if ((super_klass->is_override(super_method, target_loader, target_classname, THREAD)) || 377 if (is_default
321 ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) 378 || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD))
322 && ((super_klass = find_transitive_override(super_klass, target_method, i, target_loader, 379 || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION)
323 target_classname, THREAD)) != (InstanceKlass*)NULL))) { 380 && ((super_klass = find_transitive_override(super_klass,
381 target_method, i, target_loader,
382 target_classname, THREAD))
383 != (InstanceKlass*)NULL))))
384 {
324 // overriding, so no new entry 385 // overriding, so no new entry
325 allocate_new = false; 386 allocate_new = false;
326 387
327 if (checkconstraints) { 388 if (checkconstraints) {
328 // Override vtable entry if passes loader constraint check 389 // Override vtable entry if passes loader constraint check
345 " of %s) of the current class, %s, and its superclass loader " 406 " of %s) of the current class, %s, and its superclass loader "
346 "(instance of %s), have different Class objects for the type " 407 "(instance of %s), have different Class objects for the type "
347 "%s used in the signature"; 408 "%s used in the signature";
348 char* sig = target_method()->name_and_sig_as_C_string(); 409 char* sig = target_method()->name_and_sig_as_C_string();
349 const char* loader1 = SystemDictionary::loader_name(target_loader()); 410 const char* loader1 = SystemDictionary::loader_name(target_loader());
350 char* current = _klass->name()->as_C_string(); 411 char* current = target_klass->name()->as_C_string();
351 const char* loader2 = SystemDictionary::loader_name(super_loader()); 412 const char* loader2 = SystemDictionary::loader_name(super_loader());
352 char* failed_type_name = failed_type_symbol->as_C_string(); 413 char* failed_type_name = failed_type_symbol->as_C_string();
353 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + 414 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
354 strlen(current) + strlen(loader2) + strlen(failed_type_name); 415 strlen(current) + strlen(loader2) + strlen(failed_type_name);
355 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); 416 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
358 THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false); 419 THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
359 } 420 }
360 } 421 }
361 } 422 }
362 423
363 put_method_at(target_method(), i); 424 put_method_at(target_method(), i);
364 target_method()->set_vtable_index(i); 425 if (!is_default) {
426 target_method()->set_vtable_index(i);
427 } else {
428 if (def_vtable_indices != NULL) {
429 def_vtable_indices->at_put(default_index, i);
430 }
431 assert(super_method->is_default_method() || super_method->is_overpass()
432 || super_method->is_abstract(), "default override error");
433 }
434
435
365 #ifndef PRODUCT 436 #ifndef PRODUCT
366 if (PrintVtables && Verbose) { 437 if (PrintVtables && Verbose) {
438 ResourceMark rm(THREAD);
439 char* sig = target_method()->name_and_sig_as_C_string();
367 tty->print("overriding with %s::%s index %d, original flags: ", 440 tty->print("overriding with %s::%s index %d, original flags: ",
368 _klass->internal_name(), (target_method() != NULL) ? 441 target_klass->internal_name(), sig, i);
369 target_method()->name()->as_C_string() : "<NULL>", i);
370 super_method->access_flags().print_on(tty); 442 super_method->access_flags().print_on(tty);
443 if (super_method->is_default_method()) {
444 tty->print("default");
445 }
446 if (super_method->is_overpass()) {
447 tty->print("overpass");
448 }
371 tty->print("overriders flags: "); 449 tty->print("overriders flags: ");
372 target_method->access_flags().print_on(tty); 450 target_method->access_flags().print_on(tty);
451 if (target_method->is_default_method()) {
452 tty->print("default");
453 }
454 if (target_method->is_overpass()) {
455 tty->print("overpass");
456 }
373 tty->cr(); 457 tty->cr();
374 } 458 }
375 #endif /*PRODUCT*/ 459 #endif /*PRODUCT*/
376 } else { 460 } else {
377 // allocate_new = true; default. We might override one entry, 461 // allocate_new = true; default. We might override one entry,
378 // but not override another. Once we override one, not need new 462 // but not override another. Once we override one, not need new
379 #ifndef PRODUCT 463 #ifndef PRODUCT
380 if (PrintVtables && Verbose) { 464 if (PrintVtables && Verbose) {
465 ResourceMark rm(THREAD);
466 char* sig = target_method()->name_and_sig_as_C_string();
381 tty->print("NOT overriding with %s::%s index %d, original flags: ", 467 tty->print("NOT overriding with %s::%s index %d, original flags: ",
382 _klass->internal_name(), (target_method() != NULL) ? 468 target_klass->internal_name(), sig,i);
383 target_method()->name()->as_C_string() : "<NULL>", i);
384 super_method->access_flags().print_on(tty); 469 super_method->access_flags().print_on(tty);
470 if (super_method->is_default_method()) {
471 tty->print("default");
472 }
473 if (super_method->is_overpass()) {
474 tty->print("overpass");
475 }
385 tty->print("overriders flags: "); 476 tty->print("overriders flags: ");
386 target_method->access_flags().print_on(tty); 477 target_method->access_flags().print_on(tty);
478 if (target_method->is_default_method()) {
479 tty->print("default");
480 }
481 if (target_method->is_overpass()) {
482 tty->print("overpass");
483 }
387 tty->cr(); 484 tty->cr();
388 } 485 }
389 #endif /*PRODUCT*/ 486 #endif /*PRODUCT*/
390 } 487 }
391 } 488 }
436 // <init> is never called dynamically-bound 533 // <init> is never called dynamically-bound
437 ) { 534 ) {
438 return false; 535 return false;
439 } 536 }
440 537
538 // Concrete interface methods do not need new entries, they override
539 // abstract method entries using default inheritance rules
540 if (target_method()->method_holder() != NULL &&
541 target_method()->method_holder()->is_interface() &&
542 !target_method()->is_abstract() ) {
543 return false;
544 }
545
441 // we need a new entry if there is no superclass 546 // we need a new entry if there is no superclass
442 if (super == NULL) { 547 if (super == NULL) {
443 return true; 548 return true;
444 } 549 }
445 550
446 // private methods in classes always have a new entry in the vtable 551 // private methods in classes always have a new entry in the vtable
447 // specification interpretation since classic has 552 // specification interpretation since classic has
448 // private methods not overriding 553 // private methods not overriding
449 // JDK8 adds private methods in interfaces which require invokespecial 554 // JDK8 adds private methods in interfaces which require invokespecial
450 if (target_method()->is_private()) { 555 if (target_method()->is_private()) {
451 return true; 556 return true;
452 } 557 }
453 558
454 // search through the super class hierarchy to see if we need 559 // search through the super class hierarchy to see if we need
524 629
525 // miranda methods are public abstract instance interface methods in a class's vtable 630 // miranda methods are public abstract instance interface methods in a class's vtable
526 if (mhk->is_interface()) { 631 if (mhk->is_interface()) {
527 assert(m->is_public(), "should be public"); 632 assert(m->is_public(), "should be public");
528 assert(ik()->implements_interface(method_holder) , "this class should implement the interface"); 633 assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
529 assert(is_miranda(m, ik()->methods(), ik()->super()), "should be a miranda_method"); 634 assert(is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super()), "should be a miranda_method");
530 return true; 635 return true;
531 } 636 }
532 return false; 637 return false;
533 } 638 }
534 639
535 // check if a method is a miranda method, given a class's methods table and its super 640 // check if a method is a miranda method, given a class's methods table,
536 // "miranda" means not static, not defined by this class, and not defined 641 // its default_method table and its super
537 // in super unless it is private and therefore inaccessible to this class. 642 // "miranda" means not static, not defined by this class.
643 // private methods in interfaces do not belong in the miranda list.
538 // the caller must make sure that the method belongs to an interface implemented by the class 644 // the caller must make sure that the method belongs to an interface implemented by the class
539 // Miranda methods only include public interface instance methods 645 // Miranda methods only include public interface instance methods
540 // Not private methods, not static methods, not default = concrete abstract 646 // Not private methods, not static methods, not default == concrete abstract
541 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods, Klass* super) { 647 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
542 if (m->is_static()) { 648 Array<Method*>* default_methods, Klass* super) {
649 if (m->is_static() || m->is_private()) {
543 return false; 650 return false;
544 } 651 }
545 Symbol* name = m->name(); 652 Symbol* name = m->name();
546 Symbol* signature = m->signature(); 653 Symbol* signature = m->signature();
547 if (InstanceKlass::find_method(class_methods, name, signature) == NULL) { 654 if (InstanceKlass::find_method(class_methods, name, signature) == NULL) {
548 // did not find it in the method table of the current class 655 // did not find it in the method table of the current class
549 if (super == NULL) { 656 if ((default_methods == NULL) ||
550 // super doesn't exist 657 InstanceKlass::find_method(default_methods, name, signature) == NULL) {
551 return true; 658 if (super == NULL) {
552 } 659 // super doesn't exist
553 660 return true;
554 Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature); 661 }
555 if (mo == NULL || mo->access_flags().is_private() ) { 662
556 // super class hierarchy does not implement it or protection is different 663 Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature);
557 return true; 664 if (mo == NULL || mo->access_flags().is_private() ) {
665 // super class hierarchy does not implement it or protection is different
666 return true;
667 }
558 } 668 }
559 } 669 }
560 670
561 return false; 671 return false;
562 } 672 }
563 673
564 // Scans current_interface_methods for miranda methods that do not 674 // Scans current_interface_methods for miranda methods that do not
565 // already appear in new_mirandas and are also not defined-and-non-private 675 // already appear in new_mirandas, or default methods, and are also not defined-and-non-private
566 // in super (superclass). These mirandas are added to all_mirandas if it is 676 // in super (superclass). These mirandas are added to all_mirandas if it is
567 // not null; in addition, those that are not duplicates of miranda methods 677 // not null; in addition, those that are not duplicates of miranda methods
568 // inherited by super from its interfaces are added to new_mirandas. 678 // inherited by super from its interfaces are added to new_mirandas.
569 // Thus, new_mirandas will be the set of mirandas that this class introduces, 679 // Thus, new_mirandas will be the set of mirandas that this class introduces,
570 // all_mirandas will be the set of all mirandas applicable to this class 680 // all_mirandas will be the set of all mirandas applicable to this class
571 // including all defined in superclasses. 681 // including all defined in superclasses.
572 void klassVtable::add_new_mirandas_to_lists( 682 void klassVtable::add_new_mirandas_to_lists(
573 GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas, 683 GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,
574 Array<Method*>* current_interface_methods, Array<Method*>* class_methods, 684 Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
575 Klass* super) { 685 Array<Method*>* default_methods, Klass* super) {
686
576 // iterate thru the current interface's method to see if it a miranda 687 // iterate thru the current interface's method to see if it a miranda
577 int num_methods = current_interface_methods->length(); 688 int num_methods = current_interface_methods->length();
578 for (int i = 0; i < num_methods; i++) { 689 for (int i = 0; i < num_methods; i++) {
579 Method* im = current_interface_methods->at(i); 690 Method* im = current_interface_methods->at(i);
580 bool is_duplicate = false; 691 bool is_duplicate = false;
588 break; 699 break;
589 } 700 }
590 } 701 }
591 702
592 if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable 703 if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
593 if (is_miranda(im, class_methods, super)) { // is it a miranda at all? 704 if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all?
594 InstanceKlass *sk = InstanceKlass::cast(super); 705 InstanceKlass *sk = InstanceKlass::cast(super);
595 // check if it is a duplicate of a super's miranda 706 // check if it is a duplicate of a super's miranda
596 if (sk->lookup_method_in_all_interfaces(im->name(), im->signature()) == NULL) { 707 if (sk->lookup_method_in_all_interfaces(im->name(), im->signature()) == NULL) {
597 new_mirandas->append(im); 708 new_mirandas->append(im);
598 } 709 }
605 } 716 }
606 717
607 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas, 718 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
608 GrowableArray<Method*>* all_mirandas, 719 GrowableArray<Method*>* all_mirandas,
609 Klass* super, Array<Method*>* class_methods, 720 Klass* super, Array<Method*>* class_methods,
721 Array<Method*>* default_methods,
610 Array<Klass*>* local_interfaces) { 722 Array<Klass*>* local_interfaces) {
611 assert((new_mirandas->length() == 0) , "current mirandas must be 0"); 723 assert((new_mirandas->length() == 0) , "current mirandas must be 0");
612 724
613 // iterate thru the local interfaces looking for a miranda 725 // iterate thru the local interfaces looking for a miranda
614 int num_local_ifs = local_interfaces->length(); 726 int num_local_ifs = local_interfaces->length();
615 for (int i = 0; i < num_local_ifs; i++) { 727 for (int i = 0; i < num_local_ifs; i++) {
616 InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i)); 728 InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
617 add_new_mirandas_to_lists(new_mirandas, all_mirandas, 729 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
618 ik->methods(), class_methods, super); 730 ik->methods(), class_methods,
731 default_methods, super);
619 // iterate thru each local's super interfaces 732 // iterate thru each local's super interfaces
620 Array<Klass*>* super_ifs = ik->transitive_interfaces(); 733 Array<Klass*>* super_ifs = ik->transitive_interfaces();
621 int num_super_ifs = super_ifs->length(); 734 int num_super_ifs = super_ifs->length();
622 for (int j = 0; j < num_super_ifs; j++) { 735 for (int j = 0; j < num_super_ifs; j++) {
623 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j)); 736 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
624 add_new_mirandas_to_lists(new_mirandas, all_mirandas, 737 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
625 sik->methods(), class_methods, super); 738 sik->methods(), class_methods,
739 default_methods, super);
626 } 740 }
627 } 741 }
628 } 742 }
629 743
630 // Discover miranda methods ("miranda" = "interface abstract, no binding"), 744 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
631 // and append them into the vtable starting at index initialized, 745 // and append them into the vtable starting at index initialized,
632 // return the new value of initialized. 746 // return the new value of initialized.
633 int klassVtable::fill_in_mirandas(int initialized) { 747 int klassVtable::fill_in_mirandas(int initialized) {
634 GrowableArray<Method*> mirandas(20); 748 GrowableArray<Method*> mirandas(20);
635 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(), 749 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
636 ik()->local_interfaces()); 750 ik()->default_methods(), ik()->local_interfaces());
637 for (int i = 0; i < mirandas.length(); i++) { 751 for (int i = 0; i < mirandas.length(); i++) {
752 if (PrintVtables && Verbose) {
753 Method* meth = mirandas.at(i);
754 ResourceMark rm(Thread::current());
755 if (meth != NULL) {
756 char* sig = meth->name_and_sig_as_C_string();
757 tty->print("fill in mirandas with %s index %d, flags: ",
758 sig, initialized);
759 meth->access_flags().print_on(tty);
760 if (meth->is_default_method()) {
761 tty->print("default");
762 }
763 tty->cr();
764 }
765 }
638 put_method_at(mirandas.at(i), initialized); 766 put_method_at(mirandas.at(i), initialized);
639 ++initialized; 767 ++initialized;
640 } 768 }
641 return initialized; 769 return initialized;
642 } 770 }
646 void klassVtable::copy_vtable_to(vtableEntry* start) { 774 void klassVtable::copy_vtable_to(vtableEntry* start) {
647 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size()); 775 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
648 } 776 }
649 777
650 #if INCLUDE_JVMTI 778 #if INCLUDE_JVMTI
779 bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
780 // If old_method is default, find this vtable index in default_vtable_indices
781 // and replace that method in the _default_methods list
782 bool updated = false;
783
784 Array<Method*>* default_methods = ik()->default_methods();
785 if (default_methods != NULL) {
786 int len = default_methods->length();
787 for (int idx = 0; idx < len; idx++) {
788 if (vtable_index == ik()->default_vtable_indices()->at(idx)) {
789 if (default_methods->at(idx) == old_method) {
790 default_methods->at_put(idx, new_method);
791 updated = true;
792 }
793 break;
794 }
795 }
796 }
797 return updated;
798 }
651 void klassVtable::adjust_method_entries(Method** old_methods, Method** new_methods, 799 void klassVtable::adjust_method_entries(Method** old_methods, Method** new_methods,
652 int methods_length, bool * trace_name_printed) { 800 int methods_length, bool * trace_name_printed) {
653 // search the vtable for uses of either obsolete or EMCP methods 801 // search the vtable for uses of either obsolete or EMCP methods
654 for (int j = 0; j < methods_length; j++) { 802 for (int j = 0; j < methods_length; j++) {
655 Method* old_method = old_methods[j]; 803 Method* old_method = old_methods[j];
661 // in sun.awt.X11.XFramePeer where methods occur more than once in the 809 // in sun.awt.X11.XFramePeer where methods occur more than once in the
662 // vtable, so, alas, we must do an exhaustive search. 810 // vtable, so, alas, we must do an exhaustive search.
663 for (int index = 0; index < length(); index++) { 811 for (int index = 0; index < length(); index++) {
664 if (unchecked_method_at(index) == old_method) { 812 if (unchecked_method_at(index) == old_method) {
665 put_method_at(new_method, index); 813 put_method_at(new_method, index);
814 // For default methods, need to update the _default_methods array
815 // which can only have one method entry for a given signature
816 bool updated_default = false;
817 if (old_method->is_default_method()) {
818 updated_default = adjust_default_method(index, old_method, new_method);
819 }
666 820
667 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { 821 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
668 if (!(*trace_name_printed)) { 822 if (!(*trace_name_printed)) {
669 // RC_TRACE_MESG macro has an embedded ResourceMark 823 // RC_TRACE_MESG macro has an embedded ResourceMark
670 RC_TRACE_MESG(("adjust: name=%s", 824 RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s",
825 klass()->external_name(),
671 old_method->method_holder()->external_name())); 826 old_method->method_holder()->external_name()));
672 *trace_name_printed = true; 827 *trace_name_printed = true;
673 } 828 }
674 // RC_TRACE macro has an embedded ResourceMark 829 // RC_TRACE macro has an embedded ResourceMark
675 RC_TRACE(0x00100000, ("vtable method update: %s(%s)", 830 RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s",
676 new_method->name()->as_C_string(), 831 new_method->name()->as_C_string(),
677 new_method->signature()->as_C_string())); 832 new_method->signature()->as_C_string(),
833 updated_default ? "true" : "false"));
678 } 834 }
679 // cannot 'break' here; see for-loop comment above. 835 // cannot 'break' here; see for-loop comment above.
680 } 836 }
681 } 837 }
682 } 838 }
699 for (int i = 0; i < length(); i++) { 855 for (int i = 0; i < length(); i++) {
700 Method* m = unchecked_method_at(i); 856 Method* m = unchecked_method_at(i);
701 if (m != NULL) { 857 if (m != NULL) {
702 tty->print(" (%5d) ", i); 858 tty->print(" (%5d) ", i);
703 m->access_flags().print_on(tty); 859 m->access_flags().print_on(tty);
860 if (m->is_default_method()) {
861 tty->print("default");
862 }
863 if (m->is_overpass()) {
864 tty->print("overpass");
865 }
704 tty->print(" -- "); 866 tty->print(" -- ");
705 m->print_name(tty); 867 m->print_name(tty);
706 tty->cr(); 868 tty->cr();
707 } 869 }
708 } 870 }
755 static int initialize_count = 0; 917 static int initialize_count = 0;
756 918
757 // Initialization 919 // Initialization
758 void klassItable::initialize_itable(bool checkconstraints, TRAPS) { 920 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
759 if (_klass->is_interface()) { 921 if (_klass->is_interface()) {
760 // This needs to go after vtable indexes are assigned but 922 // This needs to go after vtable indices are assigned but
761 // before implementors need to know the number of itable indexes. 923 // before implementors need to know the number of itable indices.
762 assign_itable_indexes_for_interface(_klass()); 924 assign_itable_indices_for_interface(_klass());
763 } 925 }
764 926
765 // Cannot be setup doing bootstrapping, interfaces don't have 927 // Cannot be setup doing bootstrapping, interfaces don't have
766 // itables, and klass with only ones entry have empty itables 928 // itables, and klass with only ones entry have empty itables
767 if (Universe::is_bootstrapping() || 929 if (Universe::is_bootstrapping() ||
801 // e.g., CharSequence.toString (from initialize_vtable) 963 // e.g., CharSequence.toString (from initialize_vtable)
802 // if (m->has_vtable_index()) return false; // NO! 964 // if (m->has_vtable_index()) return false; // NO!
803 return true; 965 return true;
804 } 966 }
805 967
806 int klassItable::assign_itable_indexes_for_interface(Klass* klass) { 968 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
807 // an interface does not have an itable, but its methods need to be numbered 969 // an interface does not have an itable, but its methods need to be numbered
808 if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count, 970 if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count,
809 klass->name()->as_C_string()); 971 klass->name()->as_C_string());
810 Array<Method*>* methods = InstanceKlass::cast(klass)->methods(); 972 Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
811 int nof_methods = methods->length(); 973 int nof_methods = methods->length();
844 #endif //ASSERT 1006 #endif //ASSERT
845 return length; // return the rightmost itable index, plus one 1007 return length; // return the rightmost itable index, plus one
846 } 1008 }
847 nof_methods -= 1; 1009 nof_methods -= 1;
848 } 1010 }
849 // no methods have itable indexes 1011 // no methods have itable indices
850 return 0; 1012 return 0;
851 } 1013 }
852 1014
853 1015
854 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) { 1016 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
905 1067
906 // ime may have moved during GC so recalculate address 1068 // ime may have moved during GC so recalculate address
907 int ime_num = m->itable_index(); 1069 int ime_num = m->itable_index();
908 assert(ime_num < ime_count, "oob"); 1070 assert(ime_num < ime_count, "oob");
909 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target()); 1071 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
1072 if (TraceItables && Verbose) {
1073 ResourceMark rm(THREAD);
1074 if (target() != NULL) {
1075 char* sig = target()->name_and_sig_as_C_string();
1076 tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1077 interf_h()->internal_name(), ime_num, sig,
1078 target()->method_holder()->internal_name());
1079 tty->print("target_method flags: ");
1080 target()->access_flags().print_on(tty);
1081 if (target()->is_default_method()) {
1082 tty->print("default");
1083 }
1084 tty->cr();
1085 }
1086 }
910 } 1087 }
911 } 1088 }
912 } 1089 }
913 1090
914 // Update entry for specific Method* 1091 // Update entry for specific Method*
978 for (int i = 0; i < _size_method_table; i++) { 1155 for (int i = 0; i < _size_method_table; i++) {
979 Method* m = ime->method(); 1156 Method* m = ime->method();
980 if (m != NULL) { 1157 if (m != NULL) {
981 tty->print(" (%5d) ", i); 1158 tty->print(" (%5d) ", i);
982 m->access_flags().print_on(tty); 1159 m->access_flags().print_on(tty);
1160 if (m->is_default_method()) {
1161 tty->print("default");
1162 }
983 tty->print(" -- "); 1163 tty->print(" -- ");
984 m->print_name(tty); 1164 m->print_name(tty);
985 tty->cr(); 1165 tty->cr();
986 } 1166 }
987 ime++; 1167 ime++;
1114 assert(InstanceKlass::cast(intf)->is_interface(), "sanity check"); 1294 assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");
1115 assert(intf->verify_itable_index(itable_index), ""); 1295 assert(intf->verify_itable_index(itable_index), "");
1116 Array<Method*>* methods = InstanceKlass::cast(intf)->methods(); 1296 Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1117 1297
1118 if (itable_index < 0 || itable_index >= method_count_for_interface(intf)) 1298 if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
1119 return NULL; // help caller defend against bad indexes 1299 return NULL; // help caller defend against bad indices
1120 1300
1121 int index = itable_index; 1301 int index = itable_index;
1122 Method* m = methods->at(index); 1302 Method* m = methods->at(index);
1123 int index2 = -1; 1303 int index2 = -1;
1124 while (!m->has_itable_index() || 1304 while (!m->has_itable_index() ||