@@ -222,13 +222,22 @@ void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
222
222
assert (def_vtable_indices->length () == len, " reinit vtable len?" );
223
223
}
224
224
for (int i = 0 ; i < len; i++) {
225
- methodHandle mh (THREAD, default_methods->at (i));
226
- assert (!mh->is_private (), " private interface method in the default method list" );
227
- bool needs_new_entry = update_inherited_vtable (mh, super_vtable_len, i, checkconstraints, CHECK);
225
+ bool needs_new_entry;
226
+ {
227
+ // Reduce the scope of this handle so that it is fetched again.
228
+ // The methodHandle keeps it from being deleted by RedefineClasses while
229
+ // we're using it.
230
+ methodHandle mh (THREAD, default_methods->at (i));
231
+ assert (!mh->is_private (), " private interface method in the default method list" );
232
+ needs_new_entry = update_inherited_vtable (mh, super_vtable_len, i, checkconstraints, CHECK);
233
+ }
228
234
229
235
// needs new entry
230
236
if (needs_new_entry) {
231
- put_method_at (mh (), initialized);
237
+ // Refetch this default method in case of redefinition that might
238
+ // happen during constraint checking in the update_inherited_vtable call above.
239
+ Method* method = default_methods->at (i);
240
+ put_method_at (method, initialized);
232
241
if (is_preinitialized_vtable ()) {
233
242
// At runtime initialize_vtable is rerun for a shared class
234
243
// (loaded by the non-boot loader) as part of link_class_impl().
@@ -494,6 +503,11 @@ bool klassVtable::update_inherited_vtable(const methodHandle& target_method,
494
503
allocate_new = false ;
495
504
}
496
505
506
+ // Set the vtable index before the constraint check safepoint, which potentially
507
+ // redefines this method if this method is a default method belonging to a
508
+ // super class or interface.
509
+ put_method_at (target_method (), i);
510
+
497
511
// Do not check loader constraints for overpass methods because overpass
498
512
// methods are created by the jvm to throw exceptions.
499
513
if (checkconstraints && !target_method->is_overpass ()) {
@@ -531,7 +545,6 @@ bool klassVtable::update_inherited_vtable(const methodHandle& target_method,
531
545
}
532
546
}
533
547
534
- put_method_at (target_method (), i);
535
548
overrides = true ;
536
549
if (!is_default) {
537
550
target_method->set_vtable_index (i);
0 commit comments