hotspot/src/share/vm/oops/klassVtable.cpp
changeset 46329 53ccc37bda19
parent 41669 2091069b6851
child 46408 70aab0c2ea8b
equal deleted inserted replaced
46328:6061df52d610 46329:53ccc37bda19
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    38 #include "runtime/arguments.hpp"
    38 #include "runtime/arguments.hpp"
    39 #include "runtime/handles.inline.hpp"
    39 #include "runtime/handles.inline.hpp"
    40 #include "utilities/copy.hpp"
    40 #include "utilities/copy.hpp"
    41 
    41 
    42 inline InstanceKlass* klassVtable::ik() const {
    42 inline InstanceKlass* klassVtable::ik() const {
    43   return InstanceKlass::cast(_klass());
    43   return InstanceKlass::cast(_klass);
    44 }
    44 }
    45 
    45 
    46 bool klassVtable::is_preinitialized_vtable() {
    46 bool klassVtable::is_preinitialized_vtable() {
    47   return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
    47   return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
    48 }
    48 }
   126 }
   126 }
   127 
   127 
   128 // Copy super class's vtable to the first part (prefix) of this class's vtable,
   128 // Copy super class's vtable to the first part (prefix) of this class's vtable,
   129 // and return the number of entries copied.  Expects that 'super' is the Java
   129 // and return the number of entries copied.  Expects that 'super' is the Java
   130 // super class (arrays can have "array" super classes that must be skipped).
   130 // super class (arrays can have "array" super classes that must be skipped).
   131 int klassVtable::initialize_from_super(KlassHandle super) {
   131 int klassVtable::initialize_from_super(Klass* super) {
   132   if (super.is_null()) {
   132   if (super == NULL) {
   133     return 0;
   133     return 0;
   134   } else if (is_preinitialized_vtable()) {
   134   } else if (is_preinitialized_vtable()) {
   135     // A shared class' vtable is preinitialized at dump time. No need to copy
   135     // A shared class' vtable is preinitialized at dump time. No need to copy
   136     // methods from super class for shared class, as that was already done
   136     // methods from super class for shared class, as that was already done
   137     // during archiving time. However, if Jvmti has redefined a class,
   137     // during archiving time. However, if Jvmti has redefined a class,
   158 //
   158 //
   159 // Revised lookup semantics   introduced 1.3 (Kestrel beta)
   159 // Revised lookup semantics   introduced 1.3 (Kestrel beta)
   160 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
   160 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
   161 
   161 
   162   // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
   162   // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
   163   KlassHandle super (THREAD, klass()->java_super());
   163   Klass* super = _klass->java_super();
   164   int nofNewEntries = 0;
   164   int nofNewEntries = 0;
   165 
   165 
   166   bool is_shared = _klass->is_shared();
   166   bool is_shared = _klass->is_shared();
   167 
   167 
   168   if (!klass()->is_array_klass()) {
   168   if (!_klass->is_array_klass()) {
   169     ResourceMark rm(THREAD);
   169     ResourceMark rm(THREAD);
   170     log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
   170     log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
   171   }
   171   }
   172 
   172 
   173 #ifdef ASSERT
   173 #ifdef ASSERT
   174   oop* end_of_obj = (oop*)_klass() + _klass()->size();
   174   oop* end_of_obj = (oop*)_klass + _klass->size();
   175   oop* end_of_vtable = (oop*)&table()[_length];
   175   oop* end_of_vtable = (oop*)&table()[_length];
   176   assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
   176   assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
   177 #endif
   177 #endif
   178 
   178 
   179   if (Universe::is_bootstrapping()) {
   179   if (Universe::is_bootstrapping()) {
   182     for (int i = 0; i < _length; i++) table()[i].clear();
   182     for (int i = 0; i < _length; i++) table()[i].clear();
   183     return;
   183     return;
   184   }
   184   }
   185 
   185 
   186   int super_vtable_len = initialize_from_super(super);
   186   int super_vtable_len = initialize_from_super(super);
   187   if (klass()->is_array_klass()) {
   187   if (_klass->is_array_klass()) {
   188     assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
   188     assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
   189   } else {
   189   } else {
   190     assert(_klass->is_instance_klass(), "must be InstanceKlass");
   190     assert(_klass->is_instance_klass(), "must be InstanceKlass");
   191 
   191 
   192     Array<Method*>* methods = ik()->methods();
   192     Array<Method*>* methods = ik()->methods();
   325 
   325 
   326   return superk;
   326   return superk;
   327 }
   327 }
   328 
   328 
   329 static void log_vtables(int i, bool overrides, methodHandle target_method,
   329 static void log_vtables(int i, bool overrides, methodHandle target_method,
   330                         KlassHandle target_klass, Method* super_method,
   330                         Klass* target_klass, Method* super_method,
   331                         Thread* thread) {
   331                         Thread* thread) {
   332 #ifndef PRODUCT
   332 #ifndef PRODUCT
   333   if (log_develop_is_enabled(Trace, vtables)) {
   333   if (log_develop_is_enabled(Trace, vtables)) {
   334     ResourceMark rm(thread);
   334     ResourceMark rm(thread);
   335     outputStream* logst = Log(vtables)::trace_stream();
   335     outputStream* logst = Log(vtables)::trace_stream();
   430   // For classfiles built with >= jdk7, we now look for transitive overrides
   430   // For classfiles built with >= jdk7, we now look for transitive overrides
   431 
   431 
   432   Symbol* name = target_method()->name();
   432   Symbol* name = target_method()->name();
   433   Symbol* signature = target_method()->signature();
   433   Symbol* signature = target_method()->signature();
   434 
   434 
   435   KlassHandle target_klass(THREAD, target_method()->method_holder());
   435   Klass* target_klass = target_method()->method_holder();
   436   if (target_klass == NULL) {
   436   if (target_klass == NULL) {
   437     target_klass = _klass;
   437     target_klass = _klass;
   438   }
   438   }
   439 
   439 
   440   Handle target_loader(THREAD, target_klass->class_loader());
   440   Handle target_loader(THREAD, target_klass->class_loader());
   953     if (log_is_enabled(Info, redefine, class, update)) {
   953     if (log_is_enabled(Info, redefine, class, update)) {
   954       ResourceMark rm;
   954       ResourceMark rm;
   955       if (!(*trace_name_printed)) {
   955       if (!(*trace_name_printed)) {
   956         log_info(redefine, class, update)
   956         log_info(redefine, class, update)
   957           ("adjust: klassname=%s for methods from name=%s",
   957           ("adjust: klassname=%s for methods from name=%s",
   958            klass()->external_name(), old_method->method_holder()->external_name());
   958            _klass->external_name(), old_method->method_holder()->external_name());
   959         *trace_name_printed = true;
   959         *trace_name_printed = true;
   960       }
   960       }
   961       log_debug(redefine, class, update, vtables)
   961       log_debug(redefine, class, update, vtables)
   962         ("vtable method update: %s(%s), updated default = %s",
   962         ("vtable method update: %s(%s), updated default = %s",
   963          new_method->name()->as_C_string(), new_method->signature()->as_C_string(), updated_default ? "true" : "false");
   963          new_method->name()->as_C_string(), new_method->signature()->as_C_string(), updated_default ? "true" : "false");
  1023   } else {
  1023   } else {
  1024     _method = m;
  1024     _method = m;
  1025   }
  1025   }
  1026 }
  1026 }
  1027 
  1027 
  1028 klassItable::klassItable(instanceKlassHandle klass) {
  1028 klassItable::klassItable(InstanceKlass* klass) {
  1029   _klass = klass;
  1029   _klass = klass;
  1030 
  1030 
  1031   if (klass->itable_length() > 0) {
  1031   if (klass->itable_length() > 0) {
  1032     itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
  1032     itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
  1033     if (offset_entry  != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
  1033     if (offset_entry  != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
  1034       // First offset entry points to the first method_entry
  1034       // First offset entry points to the first method_entry
  1035       intptr_t* method_entry  = (intptr_t *)(((address)klass()) + offset_entry->offset());
  1035       intptr_t* method_entry  = (intptr_t *)(((address)klass) + offset_entry->offset());
  1036       intptr_t* end         = klass->end_of_itable();
  1036       intptr_t* end         = klass->end_of_itable();
  1037 
  1037 
  1038       _table_offset      = (intptr_t*)offset_entry - (intptr_t*)klass();
  1038       _table_offset      = (intptr_t*)offset_entry - (intptr_t*)klass;
  1039       _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
  1039       _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
  1040       _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
  1040       _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
  1041       assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
  1041       assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
  1042       return;
  1042       return;
  1043     }
  1043     }
  1054 // Initialization
  1054 // Initialization
  1055 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
  1055 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
  1056   if (_klass->is_interface()) {
  1056   if (_klass->is_interface()) {
  1057     // This needs to go after vtable indices are assigned but
  1057     // This needs to go after vtable indices are assigned but
  1058     // before implementors need to know the number of itable indices.
  1058     // before implementors need to know the number of itable indices.
  1059     assign_itable_indices_for_interface(_klass());
  1059     assign_itable_indices_for_interface(_klass);
  1060   }
  1060   }
  1061 
  1061 
  1062   // Cannot be setup doing bootstrapping, interfaces don't have
  1062   // Cannot be setup doing bootstrapping, interfaces don't have
  1063   // itables, and klass with only ones entry have empty itables
  1063   // itables, and klass with only ones entry have empty itables
  1064   if (Universe::is_bootstrapping() ||
  1064   if (Universe::is_bootstrapping() ||
  1076     // Iterate through all interfaces
  1076     // Iterate through all interfaces
  1077     int i;
  1077     int i;
  1078     for(i = 0; i < num_interfaces; i++) {
  1078     for(i = 0; i < num_interfaces; i++) {
  1079       itableOffsetEntry* ioe = offset_entry(i);
  1079       itableOffsetEntry* ioe = offset_entry(i);
  1080       HandleMark hm(THREAD);
  1080       HandleMark hm(THREAD);
  1081       KlassHandle interf_h (THREAD, ioe->interface_klass());
  1081       Klass* interf = ioe->interface_klass();
  1082       assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
  1082       assert(interf != NULL && ioe->offset() != 0, "bad offset entry in itable");
  1083       initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
  1083       initialize_itable_for_interface(ioe->offset(), interf, checkconstraints, CHECK);
  1084     }
  1084     }
  1085 
  1085 
  1086   }
  1086   }
  1087   // Check that the last entry is empty
  1087   // Check that the last entry is empty
  1088   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
  1088   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
  1167   // itable indices
  1167   // itable indices
  1168   return length;
  1168   return length;
  1169 }
  1169 }
  1170 
  1170 
  1171 
  1171 
  1172 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
  1172 void klassItable::initialize_itable_for_interface(int method_table_offset, Klass* interf, bool checkconstraints, TRAPS) {
  1173   Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods();
  1173   Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
  1174   int nof_methods = methods->length();
  1174   int nof_methods = methods->length();
  1175   HandleMark hm;
  1175   HandleMark hm;
  1176   assert(nof_methods > 0, "at least one method must exist for interface to be in vtable");
  1176   assert(nof_methods > 0, "at least one method must exist for interface to be in vtable");
  1177   Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader());
  1177   Handle interface_loader (THREAD, InstanceKlass::cast(interf)->class_loader());
  1178 
  1178 
  1179   int ime_count = method_count_for_interface(interf_h());
  1179   int ime_count = method_count_for_interface(interf);
  1180   for (int i = 0; i < nof_methods; i++) {
  1180   for (int i = 0; i < nof_methods; i++) {
  1181     Method* m = methods->at(i);
  1181     Method* m = methods->at(i);
  1182     methodHandle target;
  1182     methodHandle target;
  1183     if (m->has_itable_index()) {
  1183     if (m->has_itable_index()) {
  1184       // This search must match the runtime resolution, i.e. selection search for invokeinterface
  1184       // This search must match the runtime resolution, i.e. selection search for invokeinterface
  1187     }
  1187     }
  1188     if (target == NULL || !target->is_public() || target->is_abstract()) {
  1188     if (target == NULL || !target->is_public() || target->is_abstract()) {
  1189       // Entry does not resolve. Leave it empty for AbstractMethodError.
  1189       // Entry does not resolve. Leave it empty for AbstractMethodError.
  1190         if (!(target == NULL) && !target->is_public()) {
  1190         if (!(target == NULL) && !target->is_public()) {
  1191           // Stuff an IllegalAccessError throwing method in there instead.
  1191           // Stuff an IllegalAccessError throwing method in there instead.
  1192           itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()].
  1192           itableOffsetEntry::method_entry(_klass, method_table_offset)[m->itable_index()].
  1193               initialize(Universe::throw_illegal_access_error());
  1193               initialize(Universe::throw_illegal_access_error());
  1194         }
  1194         }
  1195     } else {
  1195     } else {
  1196       // Entry did resolve, check loader constraints before initializing
  1196       // Entry did resolve, check loader constraints before initializing
  1197       // if checkconstraints requested
  1197       // if checkconstraints requested
  1213               "used in the signature";
  1213               "used in the signature";
  1214             char* sig = target()->name_and_sig_as_C_string();
  1214             char* sig = target()->name_and_sig_as_C_string();
  1215             const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
  1215             const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
  1216             char* current = _klass->name()->as_C_string();
  1216             char* current = _klass->name()->as_C_string();
  1217             const char* loader2 = SystemDictionary::loader_name(interface_loader());
  1217             const char* loader2 = SystemDictionary::loader_name(interface_loader());
  1218             char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
  1218             char* iface = InstanceKlass::cast(interf)->name()->as_C_string();
  1219             char* failed_type_name = failed_type_symbol->as_C_string();
  1219             char* failed_type_name = failed_type_symbol->as_C_string();
  1220             size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
  1220             size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
  1221               strlen(current) + strlen(loader2) + strlen(iface) +
  1221               strlen(current) + strlen(loader2) + strlen(iface) +
  1222               strlen(failed_type_name);
  1222               strlen(failed_type_name);
  1223             char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
  1223             char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
  1229       }
  1229       }
  1230 
  1230 
  1231       // ime may have moved during GC so recalculate address
  1231       // ime may have moved during GC so recalculate address
  1232       int ime_num = m->itable_index();
  1232       int ime_num = m->itable_index();
  1233       assert(ime_num < ime_count, "oob");
  1233       assert(ime_num < ime_count, "oob");
  1234       itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
  1234       itableOffsetEntry::method_entry(_klass, method_table_offset)[ime_num].initialize(target());
  1235       if (log_develop_is_enabled(Trace, itables)) {
  1235       if (log_develop_is_enabled(Trace, itables)) {
  1236         ResourceMark rm(THREAD);
  1236         ResourceMark rm(THREAD);
  1237         if (target() != NULL) {
  1237         if (target() != NULL) {
  1238           outputStream* logst = Log(itables)::trace_stream();
  1238           outputStream* logst = Log(itables)::trace_stream();
  1239           char* sig = target()->name_and_sig_as_C_string();
  1239           char* sig = target()->name_and_sig_as_C_string();
  1240           logst->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
  1240           logst->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
  1241                        interf_h()->internal_name(), ime_num, sig,
  1241                        interf->internal_name(), ime_num, sig,
  1242                        target()->method_holder()->internal_name());
  1242                        target()->method_holder()->internal_name());
  1243           logst->print("target_method flags: ");
  1243           logst->print("target_method flags: ");
  1244           target()->print_linkage_flags(logst);
  1244           target()->print_linkage_flags(logst);
  1245           logst->cr();
  1245           logst->cr();
  1246         }
  1246         }
  1406   return itable_size;
  1406   return itable_size;
  1407 }
  1407 }
  1408 
  1408 
  1409 
  1409 
  1410 // Fill out offset table and interface klasses into the itable space
  1410 // Fill out offset table and interface klasses into the itable space
  1411 void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {
  1411 void klassItable::setup_itable_offset_table(InstanceKlass* klass) {
  1412   if (klass->itable_length() == 0) return;
  1412   if (klass->itable_length() == 0) return;
  1413   assert(!klass->is_interface(), "Should have zero length itable");
  1413   assert(!klass->is_interface(), "Should have zero length itable");
  1414 
  1414 
  1415   // Count no of interfaces and total number of interface methods
  1415   // Count no of interfaces and total number of interface methods
  1416   CountInterfacesClosure cic;
  1416   CountInterfacesClosure cic;
  1431   intptr_t* end               = klass->end_of_itable();
  1431   intptr_t* end               = klass->end_of_itable();
  1432   assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
  1432   assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
  1433   assert((oop*)(end) == (oop*)(ime + nof_methods),                      "wrong offset calculation (2)");
  1433   assert((oop*)(end) == (oop*)(ime + nof_methods),                      "wrong offset calculation (2)");
  1434 
  1434 
  1435   // Visit all interfaces and initialize itable offset table
  1435   // Visit all interfaces and initialize itable offset table
  1436   SetupItableClosure sic((address)klass(), ioe, ime);
  1436   SetupItableClosure sic((address)klass, ioe, ime);
  1437   visit_all_interfaces(klass->transitive_interfaces(), &sic);
  1437   visit_all_interfaces(klass->transitive_interfaces(), &sic);
  1438 
  1438 
  1439 #ifdef ASSERT
  1439 #ifdef ASSERT
  1440   ime  = sic.method_entry();
  1440   ime  = sic.method_entry();
  1441   oop* v = (oop*) klass->end_of_itable();
  1441   oop* v = (oop*) klass->end_of_itable();
  1474 #ifndef PRODUCT
  1474 #ifndef PRODUCT
  1475   // avoid redundant verifies
  1475   // avoid redundant verifies
  1476   if (!forced && _verify_count == Universe::verify_count()) return;
  1476   if (!forced && _verify_count == Universe::verify_count()) return;
  1477   _verify_count = Universe::verify_count();
  1477   _verify_count = Universe::verify_count();
  1478 #endif
  1478 #endif
  1479   oop* end_of_obj = (oop*)_klass() + _klass()->size();
  1479   oop* end_of_obj = (oop*)_klass + _klass->size();
  1480   oop* end_of_vtable = (oop *)&table()[_length];
  1480   oop* end_of_vtable = (oop *)&table()[_length];
  1481   if (end_of_vtable > end_of_obj) {
  1481   if (end_of_vtable > end_of_obj) {
  1482     fatal("klass %s: klass object too short (vtable extends beyond end)",
  1482     fatal("klass %s: klass object too short (vtable extends beyond end)",
  1483           _klass->internal_name());
  1483           _klass->internal_name());
  1484   }
  1484   }
  1514 }
  1514 }
  1515 #endif
  1515 #endif
  1516 
  1516 
  1517 void vtableEntry::verify(klassVtable* vt, outputStream* st) {
  1517 void vtableEntry::verify(klassVtable* vt, outputStream* st) {
  1518   NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
  1518   NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
  1519   KlassHandle vtklass_h = vt->klass();
  1519   Klass* vtklass = vt->klass();
  1520   Klass* vtklass = vtklass_h();
       
  1521   if (vtklass->is_instance_klass() &&
  1520   if (vtklass->is_instance_klass() &&
  1522      (InstanceKlass::cast(vtklass)->major_version() >= klassVtable::VTABLE_TRANSITIVE_OVERRIDE_VERSION)) {
  1521      (InstanceKlass::cast(vtklass)->major_version() >= klassVtable::VTABLE_TRANSITIVE_OVERRIDE_VERSION)) {
  1523     assert(method() != NULL, "must have set method");
  1522     assert(method() != NULL, "must have set method");
  1524   }
  1523   }
  1525   if (method() != NULL) {
  1524   if (method() != NULL) {
  1526     method()->verify();
  1525     method()->verify();
  1527     // we sub_type, because it could be a miranda method
  1526     // we sub_type, because it could be a miranda method
  1528     if (!vtklass_h->is_subtype_of(method()->method_holder())) {
  1527     if (!vtklass->is_subtype_of(method()->method_holder())) {
  1529 #ifndef PRODUCT
  1528 #ifndef PRODUCT
  1530       print();
  1529       print();
  1531 #endif
  1530 #endif
  1532       fatal("vtableEntry " PTR_FORMAT ": method is from subclass", p2i(this));
  1531       fatal("vtableEntry " PTR_FORMAT ": method is from subclass", p2i(this));
  1533     }
  1532     }