9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "gc/shared/gcLocker.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "memory/universe.inline.hpp"
31 #include "oops/instanceKlass.hpp"
32 #include "oops/klassVtable.hpp"
33 #include "oops/method.hpp"
34 #include "oops/objArrayOop.hpp"
35 #include "oops/oop.inline.hpp"
36 #include "prims/jvmtiRedefineClassesTrace.hpp"
37 #include "runtime/arguments.hpp"
38 #include "runtime/handles.inline.hpp"
39 #include "utilities/copy.hpp"
40
41 inline InstanceKlass* klassVtable::ik() const {
42 return InstanceKlass::cast(_klass());
43 }
44
45
46 // this function computes the vtable size (including the size needed for miranda
47 // methods) and the number of miranda methods in this class.
48 // Note on Miranda methods: Let's say there is a class C that implements
117
118 int klassVtable::index_of(Method* m, int len) const {
119 assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
120 return m->vtable_index();
121 }
122
123 // Copy super class's vtable to the first part (prefix) of this class's vtable,
124 // and return the number of entries copied. Expects that 'super' is the Java
125 // super class (arrays can have "array" super classes that must be skipped).
126 int klassVtable::initialize_from_super(KlassHandle super) {
127 if (super.is_null()) {
128 return 0;
129 } else {
130 // copy methods from superKlass
131 klassVtable* superVtable = super->vtable();
132 assert(superVtable->length() <= _length, "vtable too short");
133 #ifdef ASSERT
134 superVtable->verify(tty, true);
135 #endif
136 superVtable->copy_vtable_to(table());
137 #ifndef PRODUCT
138 if (PrintVtables && Verbose) {
139 ResourceMark rm;
140 tty->print_cr("copy vtable from %s to %s size %d", super->internal_name(), klass()->internal_name(), _length);
141 }
142 #endif
143 return superVtable->length();
144 }
145 }
146
147 //
148 // Revised lookup semantics introduced 1.3 (Kestrel beta)
149 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
150
151 // Note: Arrays can have intermediate array supers. Use java_super to skip them.
152 KlassHandle super (THREAD, klass()->java_super());
153 int nofNewEntries = 0;
154
155 if (PrintVtables && !klass()->is_array_klass()) {
156 ResourceMark rm(THREAD);
157 tty->print_cr("Initializing: %s", _klass->name()->as_C_string());
158 }
159
160 #ifdef ASSERT
161 oop* end_of_obj = (oop*)_klass() + _klass()->size();
162 oop* end_of_vtable = (oop*)&table()[_length];
163 assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
164 #endif
165
166 if (Universe::is_bootstrapping()) {
167 // just clear everything
168 for (int i = 0; i < _length; i++) table()[i].clear();
169 return;
170 }
171
172 int super_vtable_len = initialize_from_super(super);
173 if (klass()->is_array_klass()) {
174 assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
175 } else {
176 assert(_klass->is_instance_klass(), "must be InstanceKlass");
177
254 // P2.B extends A, package private m
255 // P1.C extends B, public m
256 // P1.C.m needs to override P1.A.m and can not override P2.B.m
257 // Therefore: all package private methods need their own vtable entries for
258 // them to be the root of an inheritance overriding decision
259 // Package private methods may also override other vtable entries
260 InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method,
261 int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) {
262 InstanceKlass* superk = initialsuper;
263 while (superk != NULL && superk->super() != NULL) {
264 InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super());
265 klassVtable* ssVtable = supersuperklass->vtable();
266 if (vtable_index < ssVtable->length()) {
267 Method* super_method = ssVtable->method_at(vtable_index);
268 #ifndef PRODUCT
269 Symbol* name= target_method()->name();
270 Symbol* signature = target_method()->signature();
271 assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
272 #endif
273 if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) {
274 #ifndef PRODUCT
275 if (PrintVtables && Verbose) {
276 ResourceMark rm(THREAD);
277 char* sig = target_method()->name_and_sig_as_C_string();
278 tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
279 supersuperklass->internal_name(),
280 _klass->internal_name(), sig, vtable_index);
281 super_method->access_flags().print_on(tty);
282 if (super_method->is_default_method()) {
283 tty->print("default ");
284 }
285 tty->print("overriders flags: ");
286 target_method->access_flags().print_on(tty);
287 if (target_method->is_default_method()) {
288 tty->print("default ");
289 }
290 }
291 #endif /*PRODUCT*/
292 break; // return found superk
293 }
294 } else {
295 // super class has no vtable entry here, stop transitive search
296 superk = (InstanceKlass*)NULL;
297 break;
298 }
299 // if no override found yet, continue to search up
300 superk = superk->super() == NULL ? NULL : InstanceKlass::cast(superk->super());
301 }
302
303 return superk;
304 }
305
306 // Update child's copy of super vtable for overrides
307 // OR return true if a new vtable entry is required.
308 // Only called for InstanceKlass's, i.e. not for arrays
309 // If that changed, could not use _klass as handle for klass
310 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
311 int super_vtable_len, int default_index,
441 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
442 strlen(current) + strlen(loader2) + strlen(failed_type_name);
443 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
444 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
445 failed_type_name);
446 THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
447 }
448 }
449 }
450
451 put_method_at(target_method(), i);
452 if (!is_default) {
453 target_method()->set_vtable_index(i);
454 } else {
455 if (def_vtable_indices != NULL) {
456 def_vtable_indices->at_put(default_index, i);
457 }
458 assert(super_method->is_default_method() || super_method->is_overpass()
459 || super_method->is_abstract(), "default override error");
460 }
461
462
463 #ifndef PRODUCT
464 if (PrintVtables && Verbose) {
465 ResourceMark rm(THREAD);
466 char* sig = target_method()->name_and_sig_as_C_string();
467 tty->print("overriding with %s::%s index %d, original flags: ",
468 target_klass->internal_name(), sig, i);
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 }
476 tty->print("overriders flags: ");
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 }
484 tty->cr();
485 }
486 #endif /*PRODUCT*/
487 } else {
488 // allocate_new = true; default. We might override one entry,
489 // but not override another. Once we override one, not need new
490 #ifndef PRODUCT
491 if (PrintVtables && Verbose) {
492 ResourceMark rm(THREAD);
493 char* sig = target_method()->name_and_sig_as_C_string();
494 tty->print("NOT overriding with %s::%s index %d, original flags: ",
495 target_klass->internal_name(), sig,i);
496 super_method->access_flags().print_on(tty);
497 if (super_method->is_default_method()) {
498 tty->print("default ");
499 }
500 if (super_method->is_overpass()) {
501 tty->print("overpass");
502 }
503 tty->print("overriders flags: ");
504 target_method->access_flags().print_on(tty);
505 if (target_method->is_default_method()) {
506 tty->print("default ");
507 }
508 if (target_method->is_overpass()) {
509 tty->print("overpass");
510 }
511 tty->cr();
512 }
513 #endif /*PRODUCT*/
514 }
515 }
516 }
517 return allocate_new;
518 }
519
520 void klassVtable::put_method_at(Method* m, int index) {
521 #ifndef PRODUCT
522 if (PrintVtables && Verbose) {
523 ResourceMark rm;
524 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
525 tty->print("adding %s at index %d, flags: ", sig, index);
526 if (m != NULL) {
527 m->access_flags().print_on(tty);
528 if (m->is_default_method()) {
529 tty->print("default ");
530 }
531 if (m->is_overpass()) {
532 tty->print("overpass");
533 }
534 }
535 tty->cr();
536 }
537 #endif
538 table()[index].set(m);
539 }
540
541 // Find out if a method "m" with superclass "super", loader "classloader" and
542 // name "classname" needs a new vtable entry. Let P be a class package defined
543 // by "classloader" and "classname".
544 // NOTE: The logic used here is very similar to the one used for computing
545 // the vtables indices for a method. We cannot directly use that function because,
546 // we allocate the InstanceKlass at load time, and that requires that the
547 // superclass has been loaded.
548 // However, the vtable entries are filled in at link time, and therefore
549 // the superclass' vtable may not yet have been filled in.
550 bool klassVtable::needs_new_vtable_entry(methodHandle target_method,
551 Klass* super,
552 Handle classloader,
553 Symbol* classname,
554 AccessFlags class_flags,
555 TRAPS) {
556 if (class_flags.is_interface()) {
557 // Interfaces do not use vtables, except for java.lang.Object methods,
834 int num_super_ifs = super_ifs->length();
835 for (int j = 0; j < num_super_ifs; j++) {
836 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
837 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
838 sik->methods(), class_methods,
839 default_methods, super);
840 }
841 }
842 }
843
844 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
845 // and append them into the vtable starting at index initialized,
846 // return the new value of initialized.
847 // Miranda methods use vtable entries, but do not get assigned a vtable_index
848 // The vtable_index is discovered by searching from the end of the vtable
849 int klassVtable::fill_in_mirandas(int initialized) {
850 GrowableArray<Method*> mirandas(20);
851 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
852 ik()->default_methods(), ik()->local_interfaces());
853 for (int i = 0; i < mirandas.length(); i++) {
854 if (PrintVtables && Verbose) {
855 Method* meth = mirandas.at(i);
856 ResourceMark rm(Thread::current());
857 if (meth != NULL) {
858 char* sig = meth->name_and_sig_as_C_string();
859 tty->print("fill in mirandas with %s index %d, flags: ",
860 sig, initialized);
861 meth->access_flags().print_on(tty);
862 if (meth->is_default_method()) {
863 tty->print("default ");
864 }
865 tty->cr();
866 }
867 }
868 put_method_at(mirandas.at(i), initialized);
869 ++initialized;
870 }
871 return initialized;
872 }
873
874 // Copy this class's vtable to the vtable beginning at start.
875 // Used to copy superclass vtable to prefix of subclass's vtable.
876 void klassVtable::copy_vtable_to(vtableEntry* start) {
877 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
878 }
879
880 #if INCLUDE_JVMTI
881 bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
882 // If old_method is default, find this vtable index in default_vtable_indices
883 // and replace that method in the _default_methods list
884 bool updated = false;
885
1018 static int initialize_count = 0;
1019
1020 // Initialization
1021 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1022 if (_klass->is_interface()) {
1023 // This needs to go after vtable indices are assigned but
1024 // before implementors need to know the number of itable indices.
1025 assign_itable_indices_for_interface(_klass());
1026 }
1027
1028 // Cannot be setup doing bootstrapping, interfaces don't have
1029 // itables, and klass with only ones entry have empty itables
1030 if (Universe::is_bootstrapping() ||
1031 _klass->is_interface() ||
1032 _klass->itable_length() == itableOffsetEntry::size()) return;
1033
1034 // There's alway an extra itable entry so we can null-terminate it.
1035 guarantee(size_offset_table() >= 1, "too small");
1036 int num_interfaces = size_offset_table() - 1;
1037 if (num_interfaces > 0) {
1038 if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count,
1039 _klass->name()->as_C_string());
1040
1041
1042 // Iterate through all interfaces
1043 int i;
1044 for(i = 0; i < num_interfaces; i++) {
1045 itableOffsetEntry* ioe = offset_entry(i);
1046 HandleMark hm(THREAD);
1047 KlassHandle interf_h (THREAD, ioe->interface_klass());
1048 assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1049 initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1050 }
1051
1052 }
1053 // Check that the last entry is empty
1054 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1055 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1056 }
1057
1058
1059 inline bool interface_method_needs_itable_index(Method* m) {
1060 if (m->is_static()) return false; // e.g., Stream.empty
1061 if (m->is_initializer()) return false; // <init> or <clinit>
1062 // If an interface redeclares a method from java.lang.Object,
1063 // it should already have a vtable index, don't touch it.
1064 // e.g., CharSequence.toString (from initialize_vtable)
1065 // if (m->has_vtable_index()) return false; // NO!
1066 return true;
1067 }
1068
1069 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1070 // an interface does not have an itable, but its methods need to be numbered
1071 if (TraceItables) tty->print_cr("%3d: Initializing itable indices for interface %s", ++initialize_count,
1072 klass->name()->as_C_string());
1073 Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1074 int nof_methods = methods->length();
1075 int ime_num = 0;
1076 for (int i = 0; i < nof_methods; i++) {
1077 Method* m = methods->at(i);
1078 if (interface_method_needs_itable_index(m)) {
1079 assert(!m->is_final_method(), "no final interface methods");
1080 // If m is already assigned a vtable index, do not disturb it.
1081 if (TraceItables && Verbose) {
1082 ResourceMark rm;
1083 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
1084 if (m->has_vtable_index()) {
1085 tty->print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1086 } else {
1087 tty->print("itable index %d for method: %s, flags: ", ime_num, sig);
1088 }
1089 if (m != NULL) {
1090 m->access_flags().print_on(tty);
1091 if (m->is_default_method()) {
1092 tty->print("default ");
1093 }
1094 if (m->is_overpass()) {
1095 tty->print("overpass");
1096 }
1097 }
1098 tty->cr();
1099 }
1100 if (!m->has_vtable_index()) {
1101 assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable");
1102 m->set_itable_index(ime_num);
1103 // Progress to next itable entry
1104 ime_num++;
1105 }
1106 }
1107 }
1108 assert(ime_num == method_count_for_interface(klass), "proper sizing");
1109 return ime_num;
1110 }
1111
1112 int klassItable::method_count_for_interface(Klass* interf) {
1113 assert(interf->is_instance_klass(), "must be");
1114 assert(interf->is_interface(), "must be");
1115 Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1116 int nof_methods = methods->length();
1117 int length = 0;
1118 while (nof_methods > 0) {
1182 const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
1183 char* current = _klass->name()->as_C_string();
1184 const char* loader2 = SystemDictionary::loader_name(interface_loader());
1185 char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
1186 char* failed_type_name = failed_type_symbol->as_C_string();
1187 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
1188 strlen(current) + strlen(loader2) + strlen(iface) +
1189 strlen(failed_type_name);
1190 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
1191 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
1192 iface, failed_type_name);
1193 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
1194 }
1195 }
1196 }
1197
1198 // ime may have moved during GC so recalculate address
1199 int ime_num = m->itable_index();
1200 assert(ime_num < ime_count, "oob");
1201 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
1202 if (TraceItables && Verbose) {
1203 ResourceMark rm(THREAD);
1204 if (target() != NULL) {
1205 char* sig = target()->name_and_sig_as_C_string();
1206 tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1207 interf_h()->internal_name(), ime_num, sig,
1208 target()->method_holder()->internal_name());
1209 tty->print("target_method flags: ");
1210 target()->access_flags().print_on(tty);
1211 if (target()->is_default_method()) {
1212 tty->print("default ");
1213 }
1214 tty->cr();
1215 }
1216 }
1217 }
1218 }
1219 }
1220
1221 // Update entry for specific Method*
1222 void klassItable::initialize_with_method(Method* m) {
1223 itableMethodEntry* ime = method_entry(0);
1224 for(int i = 0; i < _size_method_table; i++) {
1225 if (ime->method() == m) {
1226 ime->initialize(m);
1227 }
1228 ime++;
1229 }
1230 }
1231
1232 #if INCLUDE_JVMTI
1233 // search the itable for uses of either obsolete or EMCP methods
1234 void klassItable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "gc/shared/gcLocker.hpp"
29 #include "logging/log.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "memory/universe.inline.hpp"
32 #include "oops/instanceKlass.hpp"
33 #include "oops/klassVtable.hpp"
34 #include "oops/method.hpp"
35 #include "oops/objArrayOop.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "prims/jvmtiRedefineClassesTrace.hpp"
38 #include "runtime/arguments.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "utilities/copy.hpp"
41
42 inline InstanceKlass* klassVtable::ik() const {
43 return InstanceKlass::cast(_klass());
44 }
45
46
47 // this function computes the vtable size (including the size needed for miranda
48 // methods) and the number of miranda methods in this class.
49 // Note on Miranda methods: Let's say there is a class C that implements
118
119 int klassVtable::index_of(Method* m, int len) const {
120 assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
121 return m->vtable_index();
122 }
123
124 // Copy super class's vtable to the first part (prefix) of this class's vtable,
125 // and return the number of entries copied. Expects that 'super' is the Java
126 // super class (arrays can have "array" super classes that must be skipped).
127 int klassVtable::initialize_from_super(KlassHandle super) {
128 if (super.is_null()) {
129 return 0;
130 } else {
131 // copy methods from superKlass
132 klassVtable* superVtable = super->vtable();
133 assert(superVtable->length() <= _length, "vtable too short");
134 #ifdef ASSERT
135 superVtable->verify(tty, true);
136 #endif
137 superVtable->copy_vtable_to(table());
138 ResourceMark rm;
139 log_develop_trace(vtables)("copy vtable from %s to %s size %d",
140 super->internal_name(), klass()->internal_name(),
141 _length);
142 return superVtable->length();
143 }
144 }
145
146 //
147 // Revised lookup semantics introduced 1.3 (Kestrel beta)
148 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
149
150 // Note: Arrays can have intermediate array supers. Use java_super to skip them.
151 KlassHandle super (THREAD, klass()->java_super());
152 int nofNewEntries = 0;
153
154 if (develop_log_is_enabled(Debug, vtables) && !klass()->is_array_klass()) {
155 ResourceMark rm(THREAD);
156 log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
157 }
158
159 #ifdef ASSERT
160 oop* end_of_obj = (oop*)_klass() + _klass()->size();
161 oop* end_of_vtable = (oop*)&table()[_length];
162 assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
163 #endif
164
165 if (Universe::is_bootstrapping()) {
166 // just clear everything
167 for (int i = 0; i < _length; i++) table()[i].clear();
168 return;
169 }
170
171 int super_vtable_len = initialize_from_super(super);
172 if (klass()->is_array_klass()) {
173 assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
174 } else {
175 assert(_klass->is_instance_klass(), "must be InstanceKlass");
176
253 // P2.B extends A, package private m
254 // P1.C extends B, public m
255 // P1.C.m needs to override P1.A.m and can not override P2.B.m
256 // Therefore: all package private methods need their own vtable entries for
257 // them to be the root of an inheritance overriding decision
258 // Package private methods may also override other vtable entries
259 InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method,
260 int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) {
261 InstanceKlass* superk = initialsuper;
262 while (superk != NULL && superk->super() != NULL) {
263 InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super());
264 klassVtable* ssVtable = supersuperklass->vtable();
265 if (vtable_index < ssVtable->length()) {
266 Method* super_method = ssVtable->method_at(vtable_index);
267 #ifndef PRODUCT
268 Symbol* name= target_method()->name();
269 Symbol* signature = target_method()->signature();
270 assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
271 #endif
272 if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) {
273 if (develop_log_is_enabled(Trace, vtables)) {
274 ResourceMark rm(THREAD);
275 outputStream* logst = LogHandle(vtables)::trace_stream();
276 char* sig = target_method()->name_and_sig_as_C_string();
277 logst->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
278 supersuperklass->internal_name(),
279 _klass->internal_name(), sig, vtable_index);
280 super_method->access_flags().print_on(logst);
281 if (super_method->is_default_method()) {
282 logst->print("default ");
283 }
284 logst->print("overriders flags: ");
285 target_method->access_flags().print_on(logst);
286 if (target_method->is_default_method()) {
287 logst->print("default ");
288 }
289 logst->cr();
290 }
291
292 break; // return found superk
293 }
294 } else {
295 // super class has no vtable entry here, stop transitive search
296 superk = (InstanceKlass*)NULL;
297 break;
298 }
299 // if no override found yet, continue to search up
300 superk = superk->super() == NULL ? NULL : InstanceKlass::cast(superk->super());
301 }
302
303 return superk;
304 }
305
306 // Update child's copy of super vtable for overrides
307 // OR return true if a new vtable entry is required.
308 // Only called for InstanceKlass's, i.e. not for arrays
309 // If that changed, could not use _klass as handle for klass
310 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
311 int super_vtable_len, int default_index,
441 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
442 strlen(current) + strlen(loader2) + strlen(failed_type_name);
443 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
444 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
445 failed_type_name);
446 THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
447 }
448 }
449 }
450
451 put_method_at(target_method(), i);
452 if (!is_default) {
453 target_method()->set_vtable_index(i);
454 } else {
455 if (def_vtable_indices != NULL) {
456 def_vtable_indices->at_put(default_index, i);
457 }
458 assert(super_method->is_default_method() || super_method->is_overpass()
459 || super_method->is_abstract(), "default override error");
460 }
461 }
462 log_vtables(i, allocate_new, target_method, target_klass, super_method, THREAD);
463 }
464 }
465 return allocate_new;
466 }
467 #ifndef PRODUCT
468 void klassVtable::log_vtables(int i, bool allocate_new, methodHandle target_method,
469 KlassHandle target_klass, Method* super_method,
470 Thread* thread) {
471 if (develop_log_is_enabled(Trace, vtables)) {
472 ResourceMark rm(thread);
473 outputStream* logst = LogHandle(vtables)::trace_stream();
474 char* sig = target_method()->name_and_sig_as_C_string();
475 if (allocate_new) {
476 // allocate_new = true; default. We might override one entry,
477 logst->print("NOT overriding with %s::%s index %d, original flags: ",
478 target_klass->internal_name(), sig, i);
479 } else {
480 logst->print("overriding with %s::%s index %d, original flags: ",
481 target_klass->internal_name(), sig, i);
482 }
483 super_method->access_flags().print_on(logst);
484 if (super_method->is_default_method()) {
485 logst->print("default ");
486 }
487 if (super_method->is_overpass()) {
488 logst->print("overpass");
489 }
490 logst->print("overriders flags: ");
491 target_method->access_flags().print_on(logst);
492 if (target_method->is_default_method()) {
493 logst->print("default ");
494 }
495 if (target_method->is_overpass()) {
496 logst->print("overpass");
497 }
498 logst->cr();
499 }
500 }
501 #endif
502
503 void klassVtable::put_method_at(Method* m, int index) {
504 if (develop_log_is_enabled(Trace, vtables)) {
505 ResourceMark rm;
506 outputStream* logst = LogHandle(vtables)::trace_stream();
507 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
508 logst->print("adding %s at index %d, flags: ", sig, index);
509 if (m != NULL) {
510 m->access_flags().print_on(logst);
511 if (m->is_default_method()) {
512 logst->print("default ");
513 }
514 if (m->is_overpass()) {
515 logst->print("overpass");
516 }
517 }
518 logst->cr();
519 }
520 table()[index].set(m);
521 }
522
523 // Find out if a method "m" with superclass "super", loader "classloader" and
524 // name "classname" needs a new vtable entry. Let P be a class package defined
525 // by "classloader" and "classname".
526 // NOTE: The logic used here is very similar to the one used for computing
527 // the vtables indices for a method. We cannot directly use that function because,
528 // we allocate the InstanceKlass at load time, and that requires that the
529 // superclass has been loaded.
530 // However, the vtable entries are filled in at link time, and therefore
531 // the superclass' vtable may not yet have been filled in.
532 bool klassVtable::needs_new_vtable_entry(methodHandle target_method,
533 Klass* super,
534 Handle classloader,
535 Symbol* classname,
536 AccessFlags class_flags,
537 TRAPS) {
538 if (class_flags.is_interface()) {
539 // Interfaces do not use vtables, except for java.lang.Object methods,
816 int num_super_ifs = super_ifs->length();
817 for (int j = 0; j < num_super_ifs; j++) {
818 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
819 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
820 sik->methods(), class_methods,
821 default_methods, super);
822 }
823 }
824 }
825
826 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
827 // and append them into the vtable starting at index initialized,
828 // return the new value of initialized.
829 // Miranda methods use vtable entries, but do not get assigned a vtable_index
830 // The vtable_index is discovered by searching from the end of the vtable
831 int klassVtable::fill_in_mirandas(int initialized) {
832 GrowableArray<Method*> mirandas(20);
833 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
834 ik()->default_methods(), ik()->local_interfaces());
835 for (int i = 0; i < mirandas.length(); i++) {
836 if (develop_log_is_enabled(Trace, vtables)) {
837 Method* meth = mirandas.at(i);
838 ResourceMark rm(Thread::current());
839 outputStream* logst = LogHandle(vtables)::trace_stream();
840 if (meth != NULL) {
841 char* sig = meth->name_and_sig_as_C_string();
842 logst->print("fill in mirandas with %s index %d, flags: ",
843 sig, initialized);
844 meth->access_flags().print_on(logst);
845 if (meth->is_default_method()) {
846 logst->print("default ");
847 }
848 logst->cr();
849 }
850 }
851 put_method_at(mirandas.at(i), initialized);
852 ++initialized;
853 }
854 return initialized;
855 }
856
857 // Copy this class's vtable to the vtable beginning at start.
858 // Used to copy superclass vtable to prefix of subclass's vtable.
859 void klassVtable::copy_vtable_to(vtableEntry* start) {
860 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
861 }
862
863 #if INCLUDE_JVMTI
864 bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
865 // If old_method is default, find this vtable index in default_vtable_indices
866 // and replace that method in the _default_methods list
867 bool updated = false;
868
1001 static int initialize_count = 0;
1002
1003 // Initialization
1004 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1005 if (_klass->is_interface()) {
1006 // This needs to go after vtable indices are assigned but
1007 // before implementors need to know the number of itable indices.
1008 assign_itable_indices_for_interface(_klass());
1009 }
1010
1011 // Cannot be setup doing bootstrapping, interfaces don't have
1012 // itables, and klass with only ones entry have empty itables
1013 if (Universe::is_bootstrapping() ||
1014 _klass->is_interface() ||
1015 _klass->itable_length() == itableOffsetEntry::size()) return;
1016
1017 // There's alway an extra itable entry so we can null-terminate it.
1018 guarantee(size_offset_table() >= 1, "too small");
1019 int num_interfaces = size_offset_table() - 1;
1020 if (num_interfaces > 0) {
1021 log_develop_debug(itables)("%3d: Initializing itables for %s", ++initialize_count,
1022 _klass->name()->as_C_string());
1023
1024
1025 // Iterate through all interfaces
1026 int i;
1027 for(i = 0; i < num_interfaces; i++) {
1028 itableOffsetEntry* ioe = offset_entry(i);
1029 HandleMark hm(THREAD);
1030 KlassHandle interf_h (THREAD, ioe->interface_klass());
1031 assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1032 initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1033 }
1034
1035 }
1036 // Check that the last entry is empty
1037 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1038 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1039 }
1040
1041
1042 inline bool interface_method_needs_itable_index(Method* m) {
1043 if (m->is_static()) return false; // e.g., Stream.empty
1044 if (m->is_initializer()) return false; // <init> or <clinit>
1045 // If an interface redeclares a method from java.lang.Object,
1046 // it should already have a vtable index, don't touch it.
1047 // e.g., CharSequence.toString (from initialize_vtable)
1048 // if (m->has_vtable_index()) return false; // NO!
1049 return true;
1050 }
1051
1052 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1053 // an interface does not have an itable, but its methods need to be numbered
1054 log_develop_debug(itables)("%3d: Initializing itable indices for interface %s", ++initialize_count,
1055 klass->name()->as_C_string());
1056 Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1057 int nof_methods = methods->length();
1058 int ime_num = 0;
1059 for (int i = 0; i < nof_methods; i++) {
1060 Method* m = methods->at(i);
1061 if (interface_method_needs_itable_index(m)) {
1062 assert(!m->is_final_method(), "no final interface methods");
1063 // If m is already assigned a vtable index, do not disturb it.
1064 if (develop_log_is_enabled(Trace, itables)) {
1065 ResourceMark rm;
1066 outputStream* logst = LogHandle(itables)::trace_stream();
1067 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
1068 if (m->has_vtable_index()) {
1069 logst->print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1070 } else {
1071 logst->print("itable index %d for method: %s, flags: ", ime_num, sig);
1072 }
1073 if (m != NULL) {
1074 m->access_flags().print_on(logst);
1075 if (m->is_default_method()) {
1076 logst->print("default ");
1077 }
1078 if (m->is_overpass()) {
1079 logst->print("overpass");
1080 }
1081 }
1082 logst->cr();
1083 }
1084 if (!m->has_vtable_index()) {
1085 assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable");
1086 m->set_itable_index(ime_num);
1087 // Progress to next itable entry
1088 ime_num++;
1089 }
1090 }
1091 }
1092 assert(ime_num == method_count_for_interface(klass), "proper sizing");
1093 return ime_num;
1094 }
1095
1096 int klassItable::method_count_for_interface(Klass* interf) {
1097 assert(interf->is_instance_klass(), "must be");
1098 assert(interf->is_interface(), "must be");
1099 Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1100 int nof_methods = methods->length();
1101 int length = 0;
1102 while (nof_methods > 0) {
1166 const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
1167 char* current = _klass->name()->as_C_string();
1168 const char* loader2 = SystemDictionary::loader_name(interface_loader());
1169 char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
1170 char* failed_type_name = failed_type_symbol->as_C_string();
1171 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
1172 strlen(current) + strlen(loader2) + strlen(iface) +
1173 strlen(failed_type_name);
1174 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
1175 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
1176 iface, failed_type_name);
1177 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
1178 }
1179 }
1180 }
1181
1182 // ime may have moved during GC so recalculate address
1183 int ime_num = m->itable_index();
1184 assert(ime_num < ime_count, "oob");
1185 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
1186 if (develop_log_is_enabled(Trace, itables)) {
1187 ResourceMark rm(THREAD);
1188 if (target() != NULL) {
1189 outputStream* logst = LogHandle(itables)::trace_stream();
1190 char* sig = target()->name_and_sig_as_C_string();
1191 logst->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1192 interf_h()->internal_name(), ime_num, sig,
1193 target()->method_holder()->internal_name());
1194 logst->print("target_method flags: ");
1195 target()->access_flags().print_on(logst);
1196 if (target()->is_default_method()) {
1197 logst->print("default ");
1198 }
1199 logst->cr();
1200 }
1201 }
1202 }
1203 }
1204 }
1205
1206 // Update entry for specific Method*
1207 void klassItable::initialize_with_method(Method* m) {
1208 itableMethodEntry* ime = method_entry(0);
1209 for(int i = 0; i < _size_method_table; i++) {
1210 if (ime->method() == m) {
1211 ime->initialize(m);
1212 }
1213 ime++;
1214 }
1215 }
1216
1217 #if INCLUDE_JVMTI
1218 // search the itable for uses of either obsolete or EMCP methods
1219 void klassItable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {
|