src/share/vm/oops/instanceKlass.cpp

Thu, 26 Jul 2018 16:04:06 +0800

author
aoqi
date
Thu, 26 Jul 2018 16:04:06 +0800
changeset 9203
53eec13fbaa5
parent 9184
fbcbfd2753b5
parent 9041
95a08233f46c
child 9448
73d689add964
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
dbuck@8716 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/javaClasses.hpp"
aoqi@0 27 #include "classfile/systemDictionary.hpp"
iklam@8497 28 #include "classfile/systemDictionaryShared.hpp"
aoqi@0 29 #include "classfile/verifier.hpp"
aoqi@0 30 #include "classfile/vmSymbols.hpp"
aoqi@0 31 #include "compiler/compileBroker.hpp"
aoqi@0 32 #include "gc_implementation/shared/markSweep.inline.hpp"
aoqi@0 33 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 34 #include "interpreter/oopMapCache.hpp"
aoqi@0 35 #include "interpreter/rewriter.hpp"
aoqi@0 36 #include "jvmtifiles/jvmti.h"
aoqi@0 37 #include "memory/genOopClosures.inline.hpp"
aoqi@0 38 #include "memory/heapInspection.hpp"
stefank@6982 39 #include "memory/iterator.inline.hpp"
aoqi@0 40 #include "memory/metadataFactory.hpp"
aoqi@0 41 #include "memory/oopFactory.hpp"
aoqi@0 42 #include "oops/fieldStreams.hpp"
aoqi@0 43 #include "oops/instanceClassLoaderKlass.hpp"
aoqi@0 44 #include "oops/instanceKlass.hpp"
aoqi@0 45 #include "oops/instanceMirrorKlass.hpp"
aoqi@0 46 #include "oops/instanceOop.hpp"
aoqi@0 47 #include "oops/klass.inline.hpp"
aoqi@0 48 #include "oops/method.hpp"
aoqi@0 49 #include "oops/oop.inline.hpp"
aoqi@0 50 #include "oops/symbol.hpp"
aoqi@0 51 #include "prims/jvmtiExport.hpp"
aoqi@0 52 #include "prims/jvmtiRedefineClassesTrace.hpp"
aoqi@0 53 #include "prims/jvmtiRedefineClasses.hpp"
jbachorik@7812 54 #include "prims/jvmtiThreadState.hpp"
aoqi@0 55 #include "prims/methodComparator.hpp"
aoqi@0 56 #include "runtime/fieldDescriptor.hpp"
aoqi@0 57 #include "runtime/handles.inline.hpp"
aoqi@0 58 #include "runtime/javaCalls.hpp"
aoqi@0 59 #include "runtime/mutexLocker.hpp"
goetz@6911 60 #include "runtime/orderAccess.inline.hpp"
aoqi@0 61 #include "runtime/thread.inline.hpp"
aoqi@0 62 #include "services/classLoadingService.hpp"
aoqi@0 63 #include "services/threadService.hpp"
aoqi@0 64 #include "utilities/dtrace.hpp"
aoqi@0 65 #include "utilities/macros.hpp"
aoqi@0 66 #if INCLUDE_ALL_GCS
aoqi@0 67 #include "gc_implementation/concurrentMarkSweep/cmsOopClosures.inline.hpp"
aoqi@0 68 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 69 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
aoqi@0 70 #include "gc_implementation/g1/g1RemSet.inline.hpp"
tschatzl@7091 71 #include "gc_implementation/g1/heapRegionManager.inline.hpp"
aoqi@0 72 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
aoqi@0 73 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.inline.hpp"
aoqi@0 74 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
aoqi@0 75 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
aoqi@0 76 #include "oops/oop.pcgc.inline.hpp"
aoqi@0 77 #endif // INCLUDE_ALL_GCS
aoqi@0 78 #ifdef COMPILER1
aoqi@0 79 #include "c1/c1_Compiler.hpp"
aoqi@0 80 #endif
aoqi@0 81
aoqi@0 82 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 83
aoqi@0 84 #ifdef DTRACE_ENABLED
aoqi@0 85
aoqi@0 86 #ifndef USDT2
aoqi@0 87
aoqi@0 88 HS_DTRACE_PROBE_DECL4(hotspot, class__initialization__required,
aoqi@0 89 char*, intptr_t, oop, intptr_t);
aoqi@0 90 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__recursive,
aoqi@0 91 char*, intptr_t, oop, intptr_t, int);
aoqi@0 92 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__concurrent,
aoqi@0 93 char*, intptr_t, oop, intptr_t, int);
aoqi@0 94 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__erroneous,
aoqi@0 95 char*, intptr_t, oop, intptr_t, int);
aoqi@0 96 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__super__failed,
aoqi@0 97 char*, intptr_t, oop, intptr_t, int);
aoqi@0 98 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__clinit,
aoqi@0 99 char*, intptr_t, oop, intptr_t, int);
aoqi@0 100 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__error,
aoqi@0 101 char*, intptr_t, oop, intptr_t, int);
aoqi@0 102 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__end,
aoqi@0 103 char*, intptr_t, oop, intptr_t, int);
aoqi@0 104
aoqi@0 105 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) \
aoqi@0 106 { \
aoqi@0 107 char* data = NULL; \
aoqi@0 108 int len = 0; \
aoqi@0 109 Symbol* name = (clss)->name(); \
aoqi@0 110 if (name != NULL) { \
aoqi@0 111 data = (char*)name->bytes(); \
aoqi@0 112 len = name->utf8_length(); \
aoqi@0 113 } \
aoqi@0 114 HS_DTRACE_PROBE4(hotspot, class__initialization__##type, \
aoqi@423 115 data, len, (void *)(clss)->class_loader(), thread_type); \
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \
aoqi@0 119 { \
aoqi@0 120 char* data = NULL; \
aoqi@0 121 int len = 0; \
aoqi@0 122 Symbol* name = (clss)->name(); \
aoqi@0 123 if (name != NULL) { \
aoqi@0 124 data = (char*)name->bytes(); \
aoqi@0 125 len = name->utf8_length(); \
aoqi@0 126 } \
aoqi@0 127 HS_DTRACE_PROBE5(hotspot, class__initialization__##type, \
aoqi@423 128 data, len, (void *)(clss)->class_loader(), thread_type, wait); \
aoqi@0 129 }
aoqi@0 130 #else /* USDT2 */
aoqi@0 131
aoqi@0 132 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
aoqi@0 133 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
aoqi@0 134 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
aoqi@0 135 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
aoqi@0 136 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
aoqi@0 137 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
aoqi@0 138 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
aoqi@0 139 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
aoqi@0 140 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) \
aoqi@0 141 { \
aoqi@0 142 char* data = NULL; \
aoqi@0 143 int len = 0; \
aoqi@0 144 Symbol* name = (clss)->name(); \
aoqi@0 145 if (name != NULL) { \
aoqi@0 146 data = (char*)name->bytes(); \
aoqi@0 147 len = name->utf8_length(); \
aoqi@0 148 } \
aoqi@0 149 HOTSPOT_CLASS_INITIALIZATION_##type( \
aoqi@0 150 data, len, (clss)->class_loader(), thread_type); \
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \
aoqi@0 154 { \
aoqi@0 155 char* data = NULL; \
aoqi@0 156 int len = 0; \
aoqi@0 157 Symbol* name = (clss)->name(); \
aoqi@0 158 if (name != NULL) { \
aoqi@0 159 data = (char*)name->bytes(); \
aoqi@0 160 len = name->utf8_length(); \
aoqi@0 161 } \
aoqi@0 162 HOTSPOT_CLASS_INITIALIZATION_##type( \
aoqi@0 163 data, len, (clss)->class_loader(), thread_type, wait); \
aoqi@0 164 }
aoqi@0 165 #endif /* USDT2 */
aoqi@0 166
aoqi@0 167 #else // ndef DTRACE_ENABLED
aoqi@0 168
aoqi@0 169 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type)
aoqi@0 170 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait)
aoqi@0 171
aoqi@0 172 #endif // ndef DTRACE_ENABLED
aoqi@0 173
aoqi@0 174 volatile int InstanceKlass::_total_instanceKlass_count = 0;
aoqi@0 175
aoqi@0 176 InstanceKlass* InstanceKlass::allocate_instance_klass(
aoqi@0 177 ClassLoaderData* loader_data,
aoqi@0 178 int vtable_len,
aoqi@0 179 int itable_len,
aoqi@0 180 int static_field_size,
aoqi@0 181 int nonstatic_oop_map_size,
aoqi@0 182 ReferenceType rt,
aoqi@0 183 AccessFlags access_flags,
aoqi@0 184 Symbol* name,
aoqi@0 185 Klass* super_klass,
aoqi@0 186 bool is_anonymous,
aoqi@0 187 TRAPS) {
aoqi@0 188
aoqi@0 189 int size = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
aoqi@0 190 access_flags.is_interface(), is_anonymous);
aoqi@0 191
aoqi@0 192 // Allocation
aoqi@0 193 InstanceKlass* ik;
aoqi@0 194 if (rt == REF_NONE) {
aoqi@0 195 if (name == vmSymbols::java_lang_Class()) {
aoqi@0 196 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(
aoqi@0 197 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
aoqi@0 198 access_flags, is_anonymous);
aoqi@0 199 } else if (name == vmSymbols::java_lang_ClassLoader() ||
aoqi@0 200 (SystemDictionary::ClassLoader_klass_loaded() &&
aoqi@0 201 super_klass != NULL &&
aoqi@0 202 super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass()))) {
aoqi@0 203 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(
aoqi@0 204 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
aoqi@0 205 access_flags, is_anonymous);
aoqi@0 206 } else {
aoqi@0 207 // normal class
aoqi@0 208 ik = new (loader_data, size, THREAD) InstanceKlass(
aoqi@0 209 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
aoqi@0 210 access_flags, is_anonymous);
aoqi@0 211 }
aoqi@0 212 } else {
aoqi@0 213 // reference klass
aoqi@0 214 ik = new (loader_data, size, THREAD) InstanceRefKlass(
aoqi@0 215 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
aoqi@0 216 access_flags, is_anonymous);
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 // Check for pending exception before adding to the loader data and incrementing
aoqi@0 220 // class count. Can get OOM here.
aoqi@0 221 if (HAS_PENDING_EXCEPTION) {
aoqi@0 222 return NULL;
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 // Add all classes to our internal class loader list here,
aoqi@0 226 // including classes in the bootstrap (NULL) class loader.
aoqi@0 227 loader_data->add_class(ik);
aoqi@0 228
aoqi@0 229 Atomic::inc(&_total_instanceKlass_count);
aoqi@0 230 return ik;
aoqi@0 231 }
aoqi@0 232
aoqi@0 233
aoqi@0 234 // copy method ordering from resource area to Metaspace
aoqi@0 235 void InstanceKlass::copy_method_ordering(intArray* m, TRAPS) {
aoqi@0 236 if (m != NULL) {
aoqi@0 237 // allocate a new array and copy contents (memcpy?)
aoqi@0 238 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
aoqi@0 239 for (int i = 0; i < m->length(); i++) {
aoqi@0 240 _method_ordering->at_put(i, m->at(i));
aoqi@0 241 }
aoqi@0 242 } else {
aoqi@0 243 _method_ordering = Universe::the_empty_int_array();
aoqi@0 244 }
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 // create a new array of vtable_indices for default methods
aoqi@0 248 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
aoqi@0 249 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
aoqi@0 250 assert(default_vtable_indices() == NULL, "only create once");
aoqi@0 251 set_default_vtable_indices(vtable_indices);
aoqi@0 252 return vtable_indices;
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 InstanceKlass::InstanceKlass(int vtable_len,
aoqi@0 256 int itable_len,
aoqi@0 257 int static_field_size,
aoqi@0 258 int nonstatic_oop_map_size,
aoqi@0 259 ReferenceType rt,
aoqi@0 260 AccessFlags access_flags,
aoqi@0 261 bool is_anonymous) {
aoqi@0 262 No_Safepoint_Verifier no_safepoint; // until k becomes parsable
aoqi@0 263
aoqi@0 264 int iksize = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
aoqi@0 265 access_flags.is_interface(), is_anonymous);
aoqi@0 266
aoqi@0 267 set_vtable_length(vtable_len);
aoqi@0 268 set_itable_length(itable_len);
aoqi@0 269 set_static_field_size(static_field_size);
aoqi@0 270 set_nonstatic_oop_map_size(nonstatic_oop_map_size);
aoqi@0 271 set_access_flags(access_flags);
aoqi@0 272 _misc_flags = 0; // initialize to zero
aoqi@0 273 set_is_anonymous(is_anonymous);
aoqi@0 274 assert(size() == iksize, "wrong size for object");
aoqi@0 275
aoqi@0 276 set_array_klasses(NULL);
aoqi@0 277 set_methods(NULL);
aoqi@0 278 set_method_ordering(NULL);
aoqi@0 279 set_default_methods(NULL);
aoqi@0 280 set_default_vtable_indices(NULL);
aoqi@0 281 set_local_interfaces(NULL);
aoqi@0 282 set_transitive_interfaces(NULL);
aoqi@0 283 init_implementor();
aoqi@0 284 set_fields(NULL, 0);
aoqi@0 285 set_constants(NULL);
aoqi@0 286 set_class_loader_data(NULL);
aoqi@0 287 set_source_file_name_index(0);
aoqi@0 288 set_source_debug_extension(NULL, 0);
aoqi@0 289 set_array_name(NULL);
aoqi@0 290 set_inner_classes(NULL);
aoqi@0 291 set_static_oop_field_count(0);
aoqi@0 292 set_nonstatic_field_size(0);
aoqi@0 293 set_is_marked_dependent(false);
stefank@6992 294 set_has_unloaded_dependent(false);
aoqi@0 295 set_init_state(InstanceKlass::allocated);
aoqi@0 296 set_init_thread(NULL);
aoqi@0 297 set_reference_type(rt);
aoqi@0 298 set_oop_map_cache(NULL);
aoqi@0 299 set_jni_ids(NULL);
aoqi@0 300 set_osr_nmethods_head(NULL);
aoqi@0 301 set_breakpoints(NULL);
aoqi@0 302 init_previous_versions();
aoqi@0 303 set_generic_signature_index(0);
aoqi@0 304 release_set_methods_jmethod_ids(NULL);
aoqi@0 305 set_annotations(NULL);
aoqi@0 306 set_jvmti_cached_class_field_map(NULL);
aoqi@0 307 set_initial_method_idnum(0);
aoqi@0 308 _dependencies = NULL;
aoqi@0 309 set_jvmti_cached_class_field_map(NULL);
aoqi@0 310 set_cached_class_file(NULL);
aoqi@0 311 set_initial_method_idnum(0);
aoqi@0 312 set_minor_version(0);
aoqi@0 313 set_major_version(0);
aoqi@0 314 NOT_PRODUCT(_verify_count = 0;)
aoqi@0 315
aoqi@0 316 // initialize the non-header words to zero
aoqi@0 317 intptr_t* p = (intptr_t*)this;
aoqi@0 318 for (int index = InstanceKlass::header_size(); index < iksize; index++) {
aoqi@0 319 p[index] = NULL_WORD;
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 // Set temporary value until parseClassFile updates it with the real instance
aoqi@0 323 // size.
aoqi@0 324 set_layout_helper(Klass::instance_layout_helper(0, true));
aoqi@0 325 }
aoqi@0 326
aoqi@0 327
aoqi@0 328 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
aoqi@0 329 Array<Method*>* methods) {
aoqi@0 330 if (methods != NULL && methods != Universe::the_empty_method_array() &&
aoqi@0 331 !methods->is_shared()) {
aoqi@0 332 for (int i = 0; i < methods->length(); i++) {
aoqi@0 333 Method* method = methods->at(i);
aoqi@0 334 if (method == NULL) continue; // maybe null if error processing
aoqi@0 335 // Only want to delete methods that are not executing for RedefineClasses.
aoqi@0 336 // The previous version will point to them so they're not totally dangling
aoqi@0 337 assert (!method->on_stack(), "shouldn't be called with methods on stack");
aoqi@0 338 MetadataFactory::free_metadata(loader_data, method);
aoqi@0 339 }
aoqi@0 340 MetadataFactory::free_array<Method*>(loader_data, methods);
aoqi@0 341 }
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
aoqi@0 345 Klass* super_klass,
aoqi@0 346 Array<Klass*>* local_interfaces,
aoqi@0 347 Array<Klass*>* transitive_interfaces) {
aoqi@0 348 // Only deallocate transitive interfaces if not empty, same as super class
aoqi@0 349 // or same as local interfaces. See code in parseClassFile.
aoqi@0 350 Array<Klass*>* ti = transitive_interfaces;
aoqi@0 351 if (ti != Universe::the_empty_klass_array() && ti != local_interfaces) {
aoqi@0 352 // check that the interfaces don't come from super class
aoqi@0 353 Array<Klass*>* sti = (super_klass == NULL) ? NULL :
aoqi@0 354 InstanceKlass::cast(super_klass)->transitive_interfaces();
aoqi@0 355 if (ti != sti && ti != NULL && !ti->is_shared()) {
aoqi@0 356 MetadataFactory::free_array<Klass*>(loader_data, ti);
aoqi@0 357 }
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 // local interfaces can be empty
aoqi@0 361 if (local_interfaces != Universe::the_empty_klass_array() &&
aoqi@0 362 local_interfaces != NULL && !local_interfaces->is_shared()) {
aoqi@0 363 MetadataFactory::free_array<Klass*>(loader_data, local_interfaces);
aoqi@0 364 }
aoqi@0 365 }
aoqi@0 366
aoqi@0 367 // This function deallocates the metadata and C heap pointers that the
aoqi@0 368 // InstanceKlass points to.
aoqi@0 369 void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
aoqi@0 370
aoqi@0 371 // Orphan the mirror first, CMS thinks it's still live.
aoqi@0 372 if (java_mirror() != NULL) {
aoqi@0 373 java_lang_Class::set_klass(java_mirror(), NULL);
aoqi@0 374 }
aoqi@0 375
aoqi@0 376 // Need to take this class off the class loader data list.
aoqi@0 377 loader_data->remove_class(this);
aoqi@0 378
aoqi@0 379 // The array_klass for this class is created later, after error handling.
aoqi@0 380 // For class redefinition, we keep the original class so this scratch class
aoqi@0 381 // doesn't have an array class. Either way, assert that there is nothing
aoqi@0 382 // to deallocate.
aoqi@0 383 assert(array_klasses() == NULL, "array classes shouldn't be created for this class yet");
aoqi@0 384
aoqi@0 385 // Release C heap allocated data that this might point to, which includes
aoqi@0 386 // reference counting symbol names.
aoqi@0 387 release_C_heap_structures();
aoqi@0 388
aoqi@0 389 deallocate_methods(loader_data, methods());
aoqi@0 390 set_methods(NULL);
aoqi@0 391
aoqi@0 392 if (method_ordering() != NULL &&
aoqi@0 393 method_ordering() != Universe::the_empty_int_array() &&
aoqi@0 394 !method_ordering()->is_shared()) {
aoqi@0 395 MetadataFactory::free_array<int>(loader_data, method_ordering());
aoqi@0 396 }
aoqi@0 397 set_method_ordering(NULL);
aoqi@0 398
aoqi@0 399 // default methods can be empty
aoqi@0 400 if (default_methods() != NULL &&
aoqi@0 401 default_methods() != Universe::the_empty_method_array() &&
aoqi@0 402 !default_methods()->is_shared()) {
aoqi@0 403 MetadataFactory::free_array<Method*>(loader_data, default_methods());
aoqi@0 404 }
aoqi@0 405 // Do NOT deallocate the default methods, they are owned by superinterfaces.
aoqi@0 406 set_default_methods(NULL);
aoqi@0 407
aoqi@0 408 // default methods vtable indices can be empty
aoqi@0 409 if (default_vtable_indices() != NULL &&
aoqi@0 410 !default_vtable_indices()->is_shared()) {
aoqi@0 411 MetadataFactory::free_array<int>(loader_data, default_vtable_indices());
aoqi@0 412 }
aoqi@0 413 set_default_vtable_indices(NULL);
aoqi@0 414
aoqi@0 415
aoqi@0 416 // This array is in Klass, but remove it with the InstanceKlass since
aoqi@0 417 // this place would be the only caller and it can share memory with transitive
aoqi@0 418 // interfaces.
aoqi@0 419 if (secondary_supers() != NULL &&
aoqi@0 420 secondary_supers() != Universe::the_empty_klass_array() &&
aoqi@0 421 secondary_supers() != transitive_interfaces() &&
aoqi@0 422 !secondary_supers()->is_shared()) {
aoqi@0 423 MetadataFactory::free_array<Klass*>(loader_data, secondary_supers());
aoqi@0 424 }
aoqi@0 425 set_secondary_supers(NULL);
aoqi@0 426
aoqi@0 427 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
aoqi@0 428 set_transitive_interfaces(NULL);
aoqi@0 429 set_local_interfaces(NULL);
aoqi@0 430
aoqi@0 431 if (fields() != NULL && !fields()->is_shared()) {
aoqi@0 432 MetadataFactory::free_array<jushort>(loader_data, fields());
aoqi@0 433 }
aoqi@0 434 set_fields(NULL, 0);
aoqi@0 435
aoqi@0 436 // If a method from a redefined class is using this constant pool, don't
aoqi@0 437 // delete it, yet. The new class's previous version will point to this.
aoqi@0 438 if (constants() != NULL) {
aoqi@0 439 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
aoqi@0 440 if (!constants()->is_shared()) {
aoqi@0 441 MetadataFactory::free_metadata(loader_data, constants());
aoqi@0 442 }
aeriksso@8042 443 // Delete any cached resolution errors for the constant pool
aeriksso@8042 444 SystemDictionary::delete_resolution_error(constants());
aeriksso@8042 445
aoqi@0 446 set_constants(NULL);
aoqi@0 447 }
aoqi@0 448
aoqi@0 449 if (inner_classes() != NULL &&
aoqi@0 450 inner_classes() != Universe::the_empty_short_array() &&
aoqi@0 451 !inner_classes()->is_shared()) {
aoqi@0 452 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
aoqi@0 453 }
aoqi@0 454 set_inner_classes(NULL);
aoqi@0 455
aoqi@0 456 // We should deallocate the Annotations instance if it's not in shared spaces.
aoqi@0 457 if (annotations() != NULL && !annotations()->is_shared()) {
aoqi@0 458 MetadataFactory::free_metadata(loader_data, annotations());
aoqi@0 459 }
aoqi@0 460 set_annotations(NULL);
aoqi@0 461 }
aoqi@0 462
aoqi@0 463 bool InstanceKlass::should_be_initialized() const {
aoqi@0 464 return !is_initialized();
aoqi@0 465 }
aoqi@0 466
aoqi@0 467 klassVtable* InstanceKlass::vtable() const {
aoqi@0 468 return new klassVtable(this, start_of_vtable(), vtable_length() / vtableEntry::size());
aoqi@0 469 }
aoqi@0 470
aoqi@0 471 klassItable* InstanceKlass::itable() const {
aoqi@0 472 return new klassItable(instanceKlassHandle(this));
aoqi@0 473 }
aoqi@0 474
aoqi@0 475 void InstanceKlass::eager_initialize(Thread *thread) {
aoqi@0 476 if (!EagerInitialization) return;
aoqi@0 477
aoqi@0 478 if (this->is_not_initialized()) {
aoqi@0 479 // abort if the the class has a class initializer
aoqi@0 480 if (this->class_initializer() != NULL) return;
aoqi@0 481
aoqi@0 482 // abort if it is java.lang.Object (initialization is handled in genesis)
aoqi@0 483 Klass* super = this->super();
aoqi@0 484 if (super == NULL) return;
aoqi@0 485
aoqi@0 486 // abort if the super class should be initialized
aoqi@0 487 if (!InstanceKlass::cast(super)->is_initialized()) return;
aoqi@0 488
aoqi@0 489 // call body to expose the this pointer
aoqi@0 490 instanceKlassHandle this_oop(thread, this);
aoqi@0 491 eager_initialize_impl(this_oop);
aoqi@0 492 }
aoqi@0 493 }
aoqi@0 494
aoqi@0 495 // JVMTI spec thinks there are signers and protection domain in the
aoqi@0 496 // instanceKlass. These accessors pretend these fields are there.
aoqi@0 497 // The hprof specification also thinks these fields are in InstanceKlass.
aoqi@0 498 oop InstanceKlass::protection_domain() const {
aoqi@0 499 // return the protection_domain from the mirror
aoqi@0 500 return java_lang_Class::protection_domain(java_mirror());
aoqi@0 501 }
aoqi@0 502
aoqi@0 503 // To remove these from requires an incompatible change and CCC request.
aoqi@0 504 objArrayOop InstanceKlass::signers() const {
aoqi@0 505 // return the signers from the mirror
aoqi@0 506 return java_lang_Class::signers(java_mirror());
aoqi@0 507 }
aoqi@0 508
aoqi@0 509 oop InstanceKlass::init_lock() const {
aoqi@0 510 // return the init lock from the mirror
aoqi@0 511 oop lock = java_lang_Class::init_lock(java_mirror());
dholmes@7191 512 // Prevent reordering with any access of initialization state
dholmes@7191 513 OrderAccess::loadload();
aoqi@0 514 assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state
aoqi@0 515 "only fully initialized state can have a null lock");
aoqi@0 516 return lock;
aoqi@0 517 }
aoqi@0 518
aoqi@0 519 // Set the initialization lock to null so the object can be GC'ed. Any racing
aoqi@0 520 // threads to get this lock will see a null lock and will not lock.
aoqi@0 521 // That's okay because they all check for initialized state after getting
aoqi@0 522 // the lock and return.
aoqi@0 523 void InstanceKlass::fence_and_clear_init_lock() {
aoqi@0 524 // make sure previous stores are all done, notably the init_state.
aoqi@0 525 OrderAccess::storestore();
aoqi@0 526 java_lang_Class::set_init_lock(java_mirror(), NULL);
aoqi@0 527 assert(!is_not_initialized(), "class must be initialized now");
aoqi@0 528 }
aoqi@0 529
aoqi@0 530 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_oop) {
aoqi@0 531 EXCEPTION_MARK;
aoqi@0 532 oop init_lock = this_oop->init_lock();
aoqi@0 533 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
aoqi@0 534
aoqi@0 535 // abort if someone beat us to the initialization
aoqi@0 536 if (!this_oop->is_not_initialized()) return; // note: not equivalent to is_initialized()
aoqi@0 537
aoqi@0 538 ClassState old_state = this_oop->init_state();
aoqi@0 539 link_class_impl(this_oop, true, THREAD);
aoqi@0 540 if (HAS_PENDING_EXCEPTION) {
aoqi@0 541 CLEAR_PENDING_EXCEPTION;
aoqi@0 542 // Abort if linking the class throws an exception.
aoqi@0 543
aoqi@0 544 // Use a test to avoid redundantly resetting the state if there's
aoqi@0 545 // no change. Set_init_state() asserts that state changes make
aoqi@0 546 // progress, whereas here we might just be spinning in place.
aoqi@0 547 if( old_state != this_oop->_init_state )
aoqi@0 548 this_oop->set_init_state (old_state);
aoqi@0 549 } else {
aoqi@0 550 // linking successfull, mark class as initialized
aoqi@0 551 this_oop->set_init_state (fully_initialized);
aoqi@0 552 this_oop->fence_and_clear_init_lock();
aoqi@0 553 // trace
aoqi@0 554 if (TraceClassInitialization) {
aoqi@0 555 ResourceMark rm(THREAD);
aoqi@0 556 tty->print_cr("[Initialized %s without side effects]", this_oop->external_name());
aoqi@0 557 }
aoqi@0 558 }
aoqi@0 559 }
aoqi@0 560
aoqi@0 561
aoqi@0 562 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
aoqi@0 563 // process. The step comments refers to the procedure described in that section.
aoqi@0 564 // Note: implementation moved to static method to expose the this pointer.
aoqi@0 565 void InstanceKlass::initialize(TRAPS) {
aoqi@0 566 if (this->should_be_initialized()) {
aoqi@0 567 HandleMark hm(THREAD);
aoqi@0 568 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 569 initialize_impl(this_oop, CHECK);
aoqi@0 570 // Note: at this point the class may be initialized
aoqi@0 571 // OR it may be in the state of being initialized
aoqi@0 572 // in case of recursive initialization!
aoqi@0 573 } else {
aoqi@0 574 assert(is_initialized(), "sanity check");
aoqi@0 575 }
aoqi@0 576 }
aoqi@0 577
aoqi@0 578
aoqi@0 579 bool InstanceKlass::verify_code(
aoqi@0 580 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
aoqi@0 581 // 1) Verify the bytecodes
aoqi@0 582 Verifier::Mode mode =
aoqi@0 583 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException;
aoqi@0 584 return Verifier::verify(this_oop, mode, this_oop->should_verify_class(), CHECK_false);
aoqi@0 585 }
aoqi@0 586
aoqi@0 587
aoqi@0 588 // Used exclusively by the shared spaces dump mechanism to prevent
aoqi@0 589 // classes mapped into the shared regions in new VMs from appearing linked.
aoqi@0 590
aoqi@0 591 void InstanceKlass::unlink_class() {
aoqi@0 592 assert(is_linked(), "must be linked");
aoqi@0 593 _init_state = loaded;
aoqi@0 594 }
aoqi@0 595
aoqi@0 596 void InstanceKlass::link_class(TRAPS) {
aoqi@0 597 assert(is_loaded(), "must be loaded");
aoqi@0 598 if (!is_linked()) {
aoqi@0 599 HandleMark hm(THREAD);
aoqi@0 600 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 601 link_class_impl(this_oop, true, CHECK);
aoqi@0 602 }
aoqi@0 603 }
aoqi@0 604
aoqi@0 605 // Called to verify that a class can link during initialization, without
aoqi@0 606 // throwing a VerifyError.
aoqi@0 607 bool InstanceKlass::link_class_or_fail(TRAPS) {
aoqi@0 608 assert(is_loaded(), "must be loaded");
aoqi@0 609 if (!is_linked()) {
aoqi@0 610 HandleMark hm(THREAD);
aoqi@0 611 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 612 link_class_impl(this_oop, false, CHECK_false);
aoqi@0 613 }
aoqi@0 614 return is_linked();
aoqi@0 615 }
aoqi@0 616
aoqi@0 617 bool InstanceKlass::link_class_impl(
aoqi@0 618 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
coleenp@8640 619 // check for error state.
coleenp@8640 620 // This is checking for the wrong state. If the state is initialization_error,
coleenp@8640 621 // then this class *was* linked. The CDS code does a try_link_class and uses
coleenp@8640 622 // initialization_error to mark classes to not include in the archive during
coleenp@8640 623 // DumpSharedSpaces. This should be removed when the CDS bug is fixed.
aoqi@0 624 if (this_oop->is_in_error_state()) {
aoqi@0 625 ResourceMark rm(THREAD);
aoqi@0 626 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(),
aoqi@0 627 this_oop->external_name(), false);
aoqi@0 628 }
aoqi@0 629 // return if already verified
aoqi@0 630 if (this_oop->is_linked()) {
aoqi@0 631 return true;
aoqi@0 632 }
aoqi@0 633
aoqi@0 634 // Timing
aoqi@0 635 // timer handles recursion
aoqi@0 636 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl");
aoqi@0 637 JavaThread* jt = (JavaThread*)THREAD;
aoqi@0 638
aoqi@0 639 // link super class before linking this class
aoqi@0 640 instanceKlassHandle super(THREAD, this_oop->super());
aoqi@0 641 if (super.not_null()) {
aoqi@0 642 if (super->is_interface()) { // check if super class is an interface
aoqi@0 643 ResourceMark rm(THREAD);
aoqi@0 644 Exceptions::fthrow(
aoqi@0 645 THREAD_AND_LOCATION,
aoqi@0 646 vmSymbols::java_lang_IncompatibleClassChangeError(),
aoqi@0 647 "class %s has interface %s as super class",
aoqi@0 648 this_oop->external_name(),
aoqi@0 649 super->external_name()
aoqi@0 650 );
aoqi@0 651 return false;
aoqi@0 652 }
aoqi@0 653
aoqi@0 654 link_class_impl(super, throw_verifyerror, CHECK_false);
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 // link all interfaces implemented by this class before linking this class
aoqi@0 658 Array<Klass*>* interfaces = this_oop->local_interfaces();
aoqi@0 659 int num_interfaces = interfaces->length();
aoqi@0 660 for (int index = 0; index < num_interfaces; index++) {
aoqi@0 661 HandleMark hm(THREAD);
aoqi@0 662 instanceKlassHandle ih(THREAD, interfaces->at(index));
aoqi@0 663 link_class_impl(ih, throw_verifyerror, CHECK_false);
aoqi@0 664 }
aoqi@0 665
aoqi@0 666 // in case the class is linked in the process of linking its superclasses
aoqi@0 667 if (this_oop->is_linked()) {
aoqi@0 668 return true;
aoqi@0 669 }
aoqi@0 670
aoqi@0 671 // trace only the link time for this klass that includes
aoqi@0 672 // the verification time
aoqi@0 673 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
aoqi@0 674 ClassLoader::perf_class_link_selftime(),
aoqi@0 675 ClassLoader::perf_classes_linked(),
aoqi@0 676 jt->get_thread_stat()->perf_recursion_counts_addr(),
aoqi@0 677 jt->get_thread_stat()->perf_timers_addr(),
aoqi@0 678 PerfClassTraceTime::CLASS_LINK);
aoqi@0 679
aoqi@0 680 // verification & rewriting
aoqi@0 681 {
aoqi@0 682 oop init_lock = this_oop->init_lock();
aoqi@0 683 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
aoqi@0 684 // rewritten will have been set if loader constraint error found
aoqi@0 685 // on an earlier link attempt
aoqi@0 686 // don't verify or rewrite if already rewritten
aoqi@0 687
aoqi@0 688 if (!this_oop->is_linked()) {
aoqi@0 689 if (!this_oop->is_rewritten()) {
aoqi@0 690 {
aoqi@0 691 // Timer includes any side effects of class verification (resolution,
aoqi@0 692 // etc), but not recursive entry into verify_code().
aoqi@0 693 PerfClassTraceTime timer(ClassLoader::perf_class_verify_time(),
aoqi@0 694 ClassLoader::perf_class_verify_selftime(),
aoqi@0 695 ClassLoader::perf_classes_verified(),
aoqi@0 696 jt->get_thread_stat()->perf_recursion_counts_addr(),
aoqi@0 697 jt->get_thread_stat()->perf_timers_addr(),
aoqi@0 698 PerfClassTraceTime::CLASS_VERIFY);
aoqi@0 699 bool verify_ok = verify_code(this_oop, throw_verifyerror, THREAD);
aoqi@0 700 if (!verify_ok) {
aoqi@0 701 return false;
aoqi@0 702 }
aoqi@0 703 }
aoqi@0 704
aoqi@0 705 // Just in case a side-effect of verify linked this class already
aoqi@0 706 // (which can sometimes happen since the verifier loads classes
aoqi@0 707 // using custom class loaders, which are free to initialize things)
aoqi@0 708 if (this_oop->is_linked()) {
aoqi@0 709 return true;
aoqi@0 710 }
aoqi@0 711
aoqi@0 712 // also sets rewritten
aoqi@0 713 this_oop->rewrite_class(CHECK_false);
iklam@8497 714 } else if (this_oop()->is_shared()) {
iklam@8497 715 ResourceMark rm(THREAD);
iklam@8497 716 char* message_buffer; // res-allocated by check_verification_dependencies
iklam@8497 717 Handle loader = this_oop()->class_loader();
iklam@8497 718 Handle pd = this_oop()->protection_domain();
iklam@8497 719 bool verified = SystemDictionaryShared::check_verification_dependencies(this_oop(),
iklam@8497 720 loader, pd, &message_buffer, THREAD);
iklam@8497 721 if (!verified) {
iklam@8497 722 THROW_MSG_(vmSymbols::java_lang_VerifyError(), message_buffer, false);
iklam@8497 723 }
aoqi@0 724 }
aoqi@0 725
aoqi@0 726 // relocate jsrs and link methods after they are all rewritten
aoqi@0 727 this_oop->link_methods(CHECK_false);
aoqi@0 728
aoqi@0 729 // Initialize the vtable and interface table after
aoqi@0 730 // methods have been rewritten since rewrite may
aoqi@0 731 // fabricate new Method*s.
aoqi@0 732 // also does loader constraint checking
jiangli@8509 733 //
jiangli@8509 734 // Initialize_vtable and initialize_itable need to be rerun for
jiangli@8509 735 // a shared class if the class is not loaded by the NULL classloader.
jiangli@8509 736 ClassLoaderData * loader_data = this_oop->class_loader_data();
jiangli@8509 737 if (!(this_oop()->is_shared() &&
jiangli@8509 738 loader_data->is_the_null_class_loader_data())) {
aoqi@0 739 ResourceMark rm(THREAD);
aoqi@0 740 this_oop->vtable()->initialize_vtable(true, CHECK_false);
aoqi@0 741 this_oop->itable()->initialize_itable(true, CHECK_false);
aoqi@0 742 }
aoqi@0 743 #ifdef ASSERT
aoqi@0 744 else {
aoqi@0 745 ResourceMark rm(THREAD);
aoqi@0 746 this_oop->vtable()->verify(tty, true);
aoqi@0 747 // In case itable verification is ever added.
aoqi@0 748 // this_oop->itable()->verify(tty, true);
aoqi@0 749 }
aoqi@0 750 #endif
aoqi@0 751 this_oop->set_init_state(linked);
aoqi@0 752 if (JvmtiExport::should_post_class_prepare()) {
aoqi@0 753 Thread *thread = THREAD;
aoqi@0 754 assert(thread->is_Java_thread(), "thread->is_Java_thread()");
aoqi@0 755 JvmtiExport::post_class_prepare((JavaThread *) thread, this_oop());
aoqi@0 756 }
aoqi@0 757 }
aoqi@0 758 }
aoqi@0 759 return true;
aoqi@0 760 }
aoqi@0 761
aoqi@0 762
aoqi@0 763 // Rewrite the byte codes of all of the methods of a class.
aoqi@0 764 // The rewriter must be called exactly once. Rewriting must happen after
aoqi@0 765 // verification but before the first method of the class is executed.
aoqi@0 766 void InstanceKlass::rewrite_class(TRAPS) {
aoqi@0 767 assert(is_loaded(), "must be loaded");
aoqi@0 768 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 769 if (this_oop->is_rewritten()) {
aoqi@0 770 assert(this_oop()->is_shared(), "rewriting an unshared class?");
aoqi@0 771 return;
aoqi@0 772 }
aoqi@0 773 Rewriter::rewrite(this_oop, CHECK);
aoqi@0 774 this_oop->set_rewritten();
aoqi@0 775 }
aoqi@0 776
aoqi@0 777 // Now relocate and link method entry points after class is rewritten.
aoqi@0 778 // This is outside is_rewritten flag. In case of an exception, it can be
aoqi@0 779 // executed more than once.
aoqi@0 780 void InstanceKlass::link_methods(TRAPS) {
aoqi@0 781 int len = methods()->length();
aoqi@0 782 for (int i = len-1; i >= 0; i--) {
aoqi@0 783 methodHandle m(THREAD, methods()->at(i));
aoqi@0 784
aoqi@0 785 // Set up method entry points for compiler and interpreter .
aoqi@0 786 m->link_method(m, CHECK);
aoqi@0 787
aoqi@0 788 // This is for JVMTI and unrelated to relocator but the last thing we do
aoqi@0 789 #ifdef ASSERT
aoqi@0 790 if (StressMethodComparator) {
aoqi@0 791 ResourceMark rm(THREAD);
aoqi@0 792 static int nmc = 0;
aoqi@0 793 for (int j = i; j >= 0 && j >= i-4; j--) {
aoqi@0 794 if ((++nmc % 1000) == 0) tty->print_cr("Have run MethodComparator %d times...", nmc);
aoqi@0 795 bool z = MethodComparator::methods_EMCP(m(),
aoqi@0 796 methods()->at(j));
aoqi@0 797 if (j == i && !z) {
aoqi@0 798 tty->print("MethodComparator FAIL: "); m->print(); m->print_codes();
aoqi@0 799 assert(z, "method must compare equal to itself");
aoqi@0 800 }
aoqi@0 801 }
aoqi@0 802 }
aoqi@0 803 #endif //ASSERT
aoqi@0 804 }
aoqi@0 805 }
aoqi@0 806
acorn@7290 807 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
coleenp@8640 808 void InstanceKlass::initialize_super_interfaces(instanceKlassHandle this_k, TRAPS) {
coleenp@8640 809 assert (this_k->has_default_methods(), "caller should have checked this");
coleenp@8640 810 for (int i = 0; i < this_k->local_interfaces()->length(); ++i) {
coleenp@8640 811 Klass* iface = this_k->local_interfaces()->at(i);
coleenp@8640 812 InstanceKlass* ik = InstanceKlass::cast(iface);
coleenp@8640 813
coleenp@8640 814 // Initialization is depth first search ie. we start with top of the inheritance tree
coleenp@8640 815 // has_default_methods drives searching superinterfaces since it
coleenp@8640 816 // means has_default_methods in its superinterface hierarchy
coleenp@8640 817 if (ik->has_default_methods()) {
coleenp@8640 818 ik->initialize_super_interfaces(ik, CHECK);
coleenp@8640 819 }
coleenp@8640 820
coleenp@8640 821 // Only initialize() interfaces that "declare" concrete methods.
coleenp@8640 822 if (ik->should_be_initialized() && ik->declares_default_methods()) {
coleenp@8640 823 ik->initialize(CHECK);
acorn@7290 824 }
acorn@7290 825 }
acorn@7290 826 }
aoqi@0 827
aoqi@0 828 void InstanceKlass::initialize_impl(instanceKlassHandle this_oop, TRAPS) {
aoqi@0 829 // Make sure klass is linked (verified) before initialization
aoqi@0 830 // A class could already be verified, since it has been reflected upon.
aoqi@0 831 this_oop->link_class(CHECK);
aoqi@0 832
aoqi@0 833 DTRACE_CLASSINIT_PROBE(required, InstanceKlass::cast(this_oop()), -1);
aoqi@0 834
aoqi@0 835 bool wait = false;
aoqi@0 836
aoqi@0 837 // refer to the JVM book page 47 for description of steps
aoqi@0 838 // Step 1
aoqi@0 839 {
aoqi@0 840 oop init_lock = this_oop->init_lock();
aoqi@0 841 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
aoqi@0 842
aoqi@0 843 Thread *self = THREAD; // it's passed the current thread
aoqi@0 844
aoqi@0 845 // Step 2
aoqi@0 846 // If we were to use wait() instead of waitInterruptibly() then
aoqi@0 847 // we might end up throwing IE from link/symbol resolution sites
aoqi@0 848 // that aren't expected to throw. This would wreak havoc. See 6320309.
aoqi@0 849 while(this_oop->is_being_initialized() && !this_oop->is_reentrant_initialization(self)) {
aoqi@0 850 wait = true;
aoqi@0 851 ol.waitUninterruptibly(CHECK);
aoqi@0 852 }
aoqi@0 853
aoqi@0 854 // Step 3
aoqi@0 855 if (this_oop->is_being_initialized() && this_oop->is_reentrant_initialization(self)) {
aoqi@0 856 DTRACE_CLASSINIT_PROBE_WAIT(recursive, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 857 return;
aoqi@0 858 }
aoqi@0 859
aoqi@0 860 // Step 4
aoqi@0 861 if (this_oop->is_initialized()) {
aoqi@0 862 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 863 return;
aoqi@0 864 }
aoqi@0 865
aoqi@0 866 // Step 5
aoqi@0 867 if (this_oop->is_in_error_state()) {
aoqi@0 868 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 869 ResourceMark rm(THREAD);
aoqi@0 870 const char* desc = "Could not initialize class ";
aoqi@0 871 const char* className = this_oop->external_name();
aoqi@0 872 size_t msglen = strlen(desc) + strlen(className) + 1;
aoqi@0 873 char* message = NEW_RESOURCE_ARRAY(char, msglen);
aoqi@0 874 if (NULL == message) {
aoqi@0 875 // Out of memory: can't create detailed error message
aoqi@0 876 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
aoqi@0 877 } else {
aoqi@0 878 jio_snprintf(message, msglen, "%s%s", desc, className);
aoqi@0 879 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
aoqi@0 880 }
aoqi@0 881 }
aoqi@0 882
aoqi@0 883 // Step 6
aoqi@0 884 this_oop->set_init_state(being_initialized);
aoqi@0 885 this_oop->set_init_thread(self);
aoqi@0 886 }
aoqi@0 887
aoqi@0 888 // Step 7
coleenp@8640 889 // Next, if C is a class rather than an interface, initialize its super class and super
coleenp@8640 890 // interfaces.
coleenp@8640 891 if (!this_oop->is_interface()) {
coleenp@8640 892 Klass* super_klass = this_oop->super();
coleenp@8640 893 if (super_klass != NULL && super_klass->should_be_initialized()) {
coleenp@8640 894 super_klass->initialize(THREAD);
coleenp@8640 895 }
coleenp@8640 896 // If C implements any interfaces that declares a non-abstract, non-static method,
coleenp@8640 897 // the initialization of C triggers initialization of its super interfaces.
coleenp@8640 898 // Only need to recurse if has_default_methods which includes declaring and
coleenp@8640 899 // inheriting default methods
coleenp@8640 900 if (!HAS_PENDING_EXCEPTION && this_oop->has_default_methods()) {
coleenp@8640 901 this_oop->initialize_super_interfaces(this_oop, THREAD);
coleenp@8640 902 }
coleenp@8640 903
coleenp@8640 904 // If any exceptions, complete abruptly, throwing the same exception as above.
aoqi@0 905 if (HAS_PENDING_EXCEPTION) {
aoqi@0 906 Handle e(THREAD, PENDING_EXCEPTION);
aoqi@0 907 CLEAR_PENDING_EXCEPTION;
aoqi@0 908 {
aoqi@0 909 EXCEPTION_MARK;
coleenp@8640 910 // Locks object, set state, and notify all waiting threads
coleenp@8640 911 this_oop->set_initialization_state_and_notify(initialization_error, THREAD);
coleenp@8640 912 CLEAR_PENDING_EXCEPTION;
aoqi@0 913 }
aoqi@0 914 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 915 THROW_OOP(e());
aoqi@0 916 }
aoqi@0 917 }
aoqi@0 918
aoqi@0 919 // Step 8
aoqi@0 920 {
aoqi@0 921 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
aoqi@0 922 JavaThread* jt = (JavaThread*)THREAD;
aoqi@0 923 DTRACE_CLASSINIT_PROBE_WAIT(clinit, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 924 // Timer includes any side effects of class initialization (resolution,
aoqi@0 925 // etc), but not recursive entry into call_class_initializer().
aoqi@0 926 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
aoqi@0 927 ClassLoader::perf_class_init_selftime(),
aoqi@0 928 ClassLoader::perf_classes_inited(),
aoqi@0 929 jt->get_thread_stat()->perf_recursion_counts_addr(),
aoqi@0 930 jt->get_thread_stat()->perf_timers_addr(),
aoqi@0 931 PerfClassTraceTime::CLASS_CLINIT);
aoqi@0 932 this_oop->call_class_initializer(THREAD);
aoqi@0 933 }
aoqi@0 934
aoqi@0 935 // Step 9
aoqi@0 936 if (!HAS_PENDING_EXCEPTION) {
aoqi@0 937 this_oop->set_initialization_state_and_notify(fully_initialized, CHECK);
aoqi@0 938 { ResourceMark rm(THREAD);
aoqi@0 939 debug_only(this_oop->vtable()->verify(tty, true);)
aoqi@0 940 }
aoqi@0 941 }
aoqi@0 942 else {
aoqi@0 943 // Step 10 and 11
aoqi@0 944 Handle e(THREAD, PENDING_EXCEPTION);
aoqi@0 945 CLEAR_PENDING_EXCEPTION;
jbachorik@7812 946 // JVMTI has already reported the pending exception
jbachorik@7812 947 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
jbachorik@7812 948 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
aoqi@0 949 {
aoqi@0 950 EXCEPTION_MARK;
aoqi@0 951 this_oop->set_initialization_state_and_notify(initialization_error, THREAD);
aoqi@0 952 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
jbachorik@7812 953 // JVMTI has already reported the pending exception
jbachorik@7812 954 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
jbachorik@7812 955 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
aoqi@0 956 }
aoqi@0 957 DTRACE_CLASSINIT_PROBE_WAIT(error, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 958 if (e->is_a(SystemDictionary::Error_klass())) {
aoqi@0 959 THROW_OOP(e());
aoqi@0 960 } else {
aoqi@0 961 JavaCallArguments args(e);
aoqi@0 962 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
aoqi@0 963 vmSymbols::throwable_void_signature(),
aoqi@0 964 &args);
aoqi@0 965 }
aoqi@0 966 }
aoqi@0 967 DTRACE_CLASSINIT_PROBE_WAIT(end, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 968 }
aoqi@0 969
aoqi@0 970
aoqi@0 971 // Note: implementation moved to static method to expose the this pointer.
aoqi@0 972 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
aoqi@0 973 instanceKlassHandle kh(THREAD, this);
aoqi@0 974 set_initialization_state_and_notify_impl(kh, state, CHECK);
aoqi@0 975 }
aoqi@0 976
aoqi@0 977 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) {
aoqi@0 978 oop init_lock = this_oop->init_lock();
coleenp@8640 979 if (init_lock != NULL) {
coleenp@8640 980 ObjectLocker ol(init_lock, THREAD);
coleenp@8640 981 this_oop->set_init_state(state);
coleenp@8640 982 this_oop->fence_and_clear_init_lock();
coleenp@8640 983 ol.notify_all(CHECK);
coleenp@8640 984 } else {
coleenp@8640 985 assert(init_lock != NULL, "The initialization state should never be set twice");
coleenp@8640 986 this_oop->set_init_state(state);
coleenp@8640 987 }
aoqi@0 988 }
aoqi@0 989
aoqi@0 990 // The embedded _implementor field can only record one implementor.
aoqi@0 991 // When there are more than one implementors, the _implementor field
aoqi@0 992 // is set to the interface Klass* itself. Following are the possible
aoqi@0 993 // values for the _implementor field:
aoqi@0 994 // NULL - no implementor
aoqi@0 995 // implementor Klass* - one implementor
aoqi@0 996 // self - more than one implementor
aoqi@0 997 //
aoqi@0 998 // The _implementor field only exists for interfaces.
aoqi@0 999 void InstanceKlass::add_implementor(Klass* k) {
aoqi@0 1000 assert(Compile_lock->owned_by_self(), "");
aoqi@0 1001 assert(is_interface(), "not interface");
aoqi@0 1002 // Filter out my subinterfaces.
aoqi@0 1003 // (Note: Interfaces are never on the subklass list.)
aoqi@0 1004 if (InstanceKlass::cast(k)->is_interface()) return;
aoqi@0 1005
aoqi@0 1006 // Filter out subclasses whose supers already implement me.
aoqi@0 1007 // (Note: CHA must walk subclasses of direct implementors
aoqi@0 1008 // in order to locate indirect implementors.)
aoqi@0 1009 Klass* sk = InstanceKlass::cast(k)->super();
aoqi@0 1010 if (sk != NULL && InstanceKlass::cast(sk)->implements_interface(this))
aoqi@0 1011 // We only need to check one immediate superclass, since the
aoqi@0 1012 // implements_interface query looks at transitive_interfaces.
aoqi@0 1013 // Any supers of the super have the same (or fewer) transitive_interfaces.
aoqi@0 1014 return;
aoqi@0 1015
aoqi@0 1016 Klass* ik = implementor();
aoqi@0 1017 if (ik == NULL) {
aoqi@0 1018 set_implementor(k);
aoqi@0 1019 } else if (ik != this) {
aoqi@0 1020 // There is already an implementor. Use itself as an indicator of
aoqi@0 1021 // more than one implementors.
aoqi@0 1022 set_implementor(this);
aoqi@0 1023 }
aoqi@0 1024
aoqi@0 1025 // The implementor also implements the transitive_interfaces
aoqi@0 1026 for (int index = 0; index < local_interfaces()->length(); index++) {
aoqi@0 1027 InstanceKlass::cast(local_interfaces()->at(index))->add_implementor(k);
aoqi@0 1028 }
aoqi@0 1029 }
aoqi@0 1030
aoqi@0 1031 void InstanceKlass::init_implementor() {
aoqi@0 1032 if (is_interface()) {
aoqi@0 1033 set_implementor(NULL);
aoqi@0 1034 }
aoqi@0 1035 }
aoqi@0 1036
aoqi@0 1037
aoqi@0 1038 void InstanceKlass::process_interfaces(Thread *thread) {
aoqi@0 1039 // link this class into the implementors list of every interface it implements
aoqi@0 1040 Klass* this_as_klass_oop = this;
aoqi@0 1041 for (int i = local_interfaces()->length() - 1; i >= 0; i--) {
aoqi@0 1042 assert(local_interfaces()->at(i)->is_klass(), "must be a klass");
aoqi@0 1043 InstanceKlass* interf = InstanceKlass::cast(local_interfaces()->at(i));
aoqi@0 1044 assert(interf->is_interface(), "expected interface");
aoqi@0 1045 interf->add_implementor(this_as_klass_oop);
aoqi@0 1046 }
aoqi@0 1047 }
aoqi@0 1048
aoqi@0 1049 bool InstanceKlass::can_be_primary_super_slow() const {
aoqi@0 1050 if (is_interface())
aoqi@0 1051 return false;
aoqi@0 1052 else
aoqi@0 1053 return Klass::can_be_primary_super_slow();
aoqi@0 1054 }
aoqi@0 1055
aoqi@0 1056 GrowableArray<Klass*>* InstanceKlass::compute_secondary_supers(int num_extra_slots) {
aoqi@0 1057 // The secondaries are the implemented interfaces.
aoqi@0 1058 InstanceKlass* ik = InstanceKlass::cast(this);
aoqi@0 1059 Array<Klass*>* interfaces = ik->transitive_interfaces();
aoqi@0 1060 int num_secondaries = num_extra_slots + interfaces->length();
aoqi@0 1061 if (num_secondaries == 0) {
aoqi@0 1062 // Must share this for correct bootstrapping!
aoqi@0 1063 set_secondary_supers(Universe::the_empty_klass_array());
aoqi@0 1064 return NULL;
aoqi@0 1065 } else if (num_extra_slots == 0) {
aoqi@0 1066 // The secondary super list is exactly the same as the transitive interfaces.
aoqi@0 1067 // Redefine classes has to be careful not to delete this!
aoqi@0 1068 set_secondary_supers(interfaces);
aoqi@0 1069 return NULL;
aoqi@0 1070 } else {
aoqi@0 1071 // Copy transitive interfaces to a temporary growable array to be constructed
aoqi@0 1072 // into the secondary super list with extra slots.
aoqi@0 1073 GrowableArray<Klass*>* secondaries = new GrowableArray<Klass*>(interfaces->length());
aoqi@0 1074 for (int i = 0; i < interfaces->length(); i++) {
aoqi@0 1075 secondaries->push(interfaces->at(i));
aoqi@0 1076 }
aoqi@0 1077 return secondaries;
aoqi@0 1078 }
aoqi@0 1079 }
aoqi@0 1080
aoqi@0 1081 bool InstanceKlass::compute_is_subtype_of(Klass* k) {
aoqi@0 1082 if (k->is_interface()) {
aoqi@0 1083 return implements_interface(k);
aoqi@0 1084 } else {
aoqi@0 1085 return Klass::compute_is_subtype_of(k);
aoqi@0 1086 }
aoqi@0 1087 }
aoqi@0 1088
aoqi@0 1089 bool InstanceKlass::implements_interface(Klass* k) const {
aoqi@0 1090 if (this == k) return true;
aoqi@0 1091 assert(k->is_interface(), "should be an interface class");
aoqi@0 1092 for (int i = 0; i < transitive_interfaces()->length(); i++) {
aoqi@0 1093 if (transitive_interfaces()->at(i) == k) {
aoqi@0 1094 return true;
aoqi@0 1095 }
aoqi@0 1096 }
aoqi@0 1097 return false;
aoqi@0 1098 }
aoqi@0 1099
aoqi@0 1100 bool InstanceKlass::is_same_or_direct_interface(Klass *k) const {
aoqi@0 1101 // Verify direct super interface
aoqi@0 1102 if (this == k) return true;
aoqi@0 1103 assert(k->is_interface(), "should be an interface class");
aoqi@0 1104 for (int i = 0; i < local_interfaces()->length(); i++) {
aoqi@0 1105 if (local_interfaces()->at(i) == k) {
aoqi@0 1106 return true;
aoqi@0 1107 }
aoqi@0 1108 }
aoqi@0 1109 return false;
aoqi@0 1110 }
aoqi@0 1111
aoqi@0 1112 objArrayOop InstanceKlass::allocate_objArray(int n, int length, TRAPS) {
aoqi@0 1113 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
aoqi@0 1114 if (length > arrayOopDesc::max_array_length(T_OBJECT)) {
aoqi@0 1115 report_java_out_of_memory("Requested array size exceeds VM limit");
aoqi@0 1116 JvmtiExport::post_array_size_exhausted();
aoqi@0 1117 THROW_OOP_0(Universe::out_of_memory_error_array_size());
aoqi@0 1118 }
aoqi@0 1119 int size = objArrayOopDesc::object_size(length);
aoqi@0 1120 Klass* ak = array_klass(n, CHECK_NULL);
aoqi@0 1121 KlassHandle h_ak (THREAD, ak);
aoqi@0 1122 objArrayOop o =
aoqi@0 1123 (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL);
aoqi@0 1124 return o;
aoqi@0 1125 }
aoqi@0 1126
aoqi@0 1127 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
aoqi@0 1128 if (TraceFinalizerRegistration) {
aoqi@0 1129 tty->print("Registered ");
aoqi@0 1130 i->print_value_on(tty);
aoqi@0 1131 tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", (address)i);
aoqi@0 1132 }
aoqi@0 1133 instanceHandle h_i(THREAD, i);
aoqi@0 1134 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
aoqi@0 1135 JavaValue result(T_VOID);
aoqi@0 1136 JavaCallArguments args(h_i);
aoqi@0 1137 methodHandle mh (THREAD, Universe::finalizer_register_method());
aoqi@0 1138 JavaCalls::call(&result, mh, &args, CHECK_NULL);
aoqi@0 1139 return h_i();
aoqi@0 1140 }
aoqi@0 1141
aoqi@0 1142 instanceOop InstanceKlass::allocate_instance(TRAPS) {
aoqi@0 1143 bool has_finalizer_flag = has_finalizer(); // Query before possible GC
aoqi@0 1144 int size = size_helper(); // Query before forming handle.
aoqi@0 1145
aoqi@0 1146 KlassHandle h_k(THREAD, this);
aoqi@0 1147
aoqi@0 1148 instanceOop i;
aoqi@0 1149
aoqi@0 1150 i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL);
aoqi@0 1151 if (has_finalizer_flag && !RegisterFinalizersAtInit) {
aoqi@0 1152 i = register_finalizer(i, CHECK_NULL);
aoqi@0 1153 }
aoqi@0 1154 return i;
aoqi@0 1155 }
aoqi@0 1156
aoqi@0 1157 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
aoqi@0 1158 if (is_interface() || is_abstract()) {
aoqi@0 1159 ResourceMark rm(THREAD);
aoqi@0 1160 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
aoqi@0 1161 : vmSymbols::java_lang_InstantiationException(), external_name());
aoqi@0 1162 }
aoqi@0 1163 if (this == SystemDictionary::Class_klass()) {
aoqi@0 1164 ResourceMark rm(THREAD);
aoqi@0 1165 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
aoqi@0 1166 : vmSymbols::java_lang_IllegalAccessException(), external_name());
aoqi@0 1167 }
aoqi@0 1168 }
aoqi@0 1169
aoqi@0 1170 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) {
aoqi@0 1171 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 1172 return array_klass_impl(this_oop, or_null, n, THREAD);
aoqi@0 1173 }
aoqi@0 1174
aoqi@0 1175 Klass* InstanceKlass::array_klass_impl(instanceKlassHandle this_oop, bool or_null, int n, TRAPS) {
aoqi@0 1176 if (this_oop->array_klasses() == NULL) {
aoqi@0 1177 if (or_null) return NULL;
aoqi@0 1178
aoqi@0 1179 ResourceMark rm;
aoqi@0 1180 JavaThread *jt = (JavaThread *)THREAD;
aoqi@0 1181 {
aoqi@0 1182 // Atomic creation of array_klasses
aoqi@0 1183 MutexLocker mc(Compile_lock, THREAD); // for vtables
aoqi@0 1184 MutexLocker ma(MultiArray_lock, THREAD);
aoqi@0 1185
aoqi@0 1186 // Check if update has already taken place
aoqi@0 1187 if (this_oop->array_klasses() == NULL) {
aoqi@0 1188 Klass* k = ObjArrayKlass::allocate_objArray_klass(this_oop->class_loader_data(), 1, this_oop, CHECK_NULL);
aoqi@0 1189 this_oop->set_array_klasses(k);
aoqi@0 1190 }
aoqi@0 1191 }
aoqi@0 1192 }
aoqi@0 1193 // _this will always be set at this point
aoqi@0 1194 ObjArrayKlass* oak = (ObjArrayKlass*)this_oop->array_klasses();
aoqi@0 1195 if (or_null) {
aoqi@0 1196 return oak->array_klass_or_null(n);
aoqi@0 1197 }
aoqi@0 1198 return oak->array_klass(n, CHECK_NULL);
aoqi@0 1199 }
aoqi@0 1200
aoqi@0 1201 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) {
aoqi@0 1202 return array_klass_impl(or_null, 1, THREAD);
aoqi@0 1203 }
aoqi@0 1204
aoqi@0 1205 void InstanceKlass::call_class_initializer(TRAPS) {
aoqi@0 1206 instanceKlassHandle ik (THREAD, this);
aoqi@0 1207 call_class_initializer_impl(ik, THREAD);
aoqi@0 1208 }
aoqi@0 1209
aoqi@0 1210 static int call_class_initializer_impl_counter = 0; // for debugging
aoqi@0 1211
aoqi@0 1212 Method* InstanceKlass::class_initializer() {
aoqi@0 1213 Method* clinit = find_method(
aoqi@0 1214 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
aoqi@0 1215 if (clinit != NULL && clinit->has_valid_initializer_flags()) {
aoqi@0 1216 return clinit;
aoqi@0 1217 }
aoqi@0 1218 return NULL;
aoqi@0 1219 }
aoqi@0 1220
aoqi@0 1221 void InstanceKlass::call_class_initializer_impl(instanceKlassHandle this_oop, TRAPS) {
aoqi@0 1222 if (ReplayCompiles &&
aoqi@0 1223 (ReplaySuppressInitializers == 1 ||
aoqi@0 1224 ReplaySuppressInitializers >= 2 && this_oop->class_loader() != NULL)) {
aoqi@0 1225 // Hide the existence of the initializer for the purpose of replaying the compile
aoqi@0 1226 return;
aoqi@0 1227 }
aoqi@0 1228
aoqi@0 1229 methodHandle h_method(THREAD, this_oop->class_initializer());
aoqi@0 1230 assert(!this_oop->is_initialized(), "we cannot initialize twice");
aoqi@0 1231 if (TraceClassInitialization) {
aoqi@0 1232 tty->print("%d Initializing ", call_class_initializer_impl_counter++);
aoqi@0 1233 this_oop->name()->print_value();
aoqi@0 1234 tty->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", (address)this_oop());
aoqi@0 1235 }
aoqi@0 1236 if (h_method() != NULL) {
aoqi@0 1237 JavaCallArguments args; // No arguments
aoqi@0 1238 JavaValue result(T_VOID);
aoqi@0 1239 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
aoqi@0 1240 }
aoqi@0 1241 }
aoqi@0 1242
aoqi@0 1243
aoqi@0 1244 void InstanceKlass::mask_for(methodHandle method, int bci,
aoqi@0 1245 InterpreterOopMap* entry_for) {
aoqi@0 1246 // Dirty read, then double-check under a lock.
aoqi@0 1247 if (_oop_map_cache == NULL) {
aoqi@0 1248 // Otherwise, allocate a new one.
aoqi@0 1249 MutexLocker x(OopMapCacheAlloc_lock);
aoqi@0 1250 // First time use. Allocate a cache in C heap
aoqi@0 1251 if (_oop_map_cache == NULL) {
aoqi@0 1252 // Release stores from OopMapCache constructor before assignment
aoqi@0 1253 // to _oop_map_cache. C++ compilers on ppc do not emit the
aoqi@0 1254 // required memory barrier only because of the volatile
aoqi@0 1255 // qualifier of _oop_map_cache.
aoqi@0 1256 OrderAccess::release_store_ptr(&_oop_map_cache, new OopMapCache());
aoqi@0 1257 }
aoqi@0 1258 }
aoqi@0 1259 // _oop_map_cache is constant after init; lookup below does is own locking.
aoqi@0 1260 _oop_map_cache->lookup(method, bci, entry_for);
aoqi@0 1261 }
aoqi@0 1262
aoqi@0 1263
aoqi@0 1264 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
aoqi@0 1265 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
aoqi@0 1266 Symbol* f_name = fs.name();
aoqi@0 1267 Symbol* f_sig = fs.signature();
aoqi@0 1268 if (f_name == name && f_sig == sig) {
aoqi@0 1269 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
aoqi@0 1270 return true;
aoqi@0 1271 }
aoqi@0 1272 }
aoqi@0 1273 return false;
aoqi@0 1274 }
aoqi@0 1275
aoqi@0 1276
aoqi@0 1277 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
aoqi@0 1278 const int n = local_interfaces()->length();
aoqi@0 1279 for (int i = 0; i < n; i++) {
aoqi@0 1280 Klass* intf1 = local_interfaces()->at(i);
aoqi@0 1281 assert(intf1->is_interface(), "just checking type");
aoqi@0 1282 // search for field in current interface
aoqi@0 1283 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
aoqi@0 1284 assert(fd->is_static(), "interface field must be static");
aoqi@0 1285 return intf1;
aoqi@0 1286 }
aoqi@0 1287 // search for field in direct superinterfaces
aoqi@0 1288 Klass* intf2 = InstanceKlass::cast(intf1)->find_interface_field(name, sig, fd);
aoqi@0 1289 if (intf2 != NULL) return intf2;
aoqi@0 1290 }
aoqi@0 1291 // otherwise field lookup fails
aoqi@0 1292 return NULL;
aoqi@0 1293 }
aoqi@0 1294
aoqi@0 1295
aoqi@0 1296 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
aoqi@0 1297 // search order according to newest JVM spec (5.4.3.2, p.167).
aoqi@0 1298 // 1) search for field in current klass
aoqi@0 1299 if (find_local_field(name, sig, fd)) {
aoqi@0 1300 return const_cast<InstanceKlass*>(this);
aoqi@0 1301 }
aoqi@0 1302 // 2) search for field recursively in direct superinterfaces
aoqi@0 1303 { Klass* intf = find_interface_field(name, sig, fd);
aoqi@0 1304 if (intf != NULL) return intf;
aoqi@0 1305 }
aoqi@0 1306 // 3) apply field lookup recursively if superclass exists
aoqi@0 1307 { Klass* supr = super();
aoqi@0 1308 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, fd);
aoqi@0 1309 }
aoqi@0 1310 // 4) otherwise field lookup fails
aoqi@0 1311 return NULL;
aoqi@0 1312 }
aoqi@0 1313
aoqi@0 1314
aoqi@0 1315 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
aoqi@0 1316 // search order according to newest JVM spec (5.4.3.2, p.167).
aoqi@0 1317 // 1) search for field in current klass
aoqi@0 1318 if (find_local_field(name, sig, fd)) {
aoqi@0 1319 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
aoqi@0 1320 }
aoqi@0 1321 // 2) search for field recursively in direct superinterfaces
aoqi@0 1322 if (is_static) {
aoqi@0 1323 Klass* intf = find_interface_field(name, sig, fd);
aoqi@0 1324 if (intf != NULL) return intf;
aoqi@0 1325 }
aoqi@0 1326 // 3) apply field lookup recursively if superclass exists
aoqi@0 1327 { Klass* supr = super();
aoqi@0 1328 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
aoqi@0 1329 }
aoqi@0 1330 // 4) otherwise field lookup fails
aoqi@0 1331 return NULL;
aoqi@0 1332 }
aoqi@0 1333
aoqi@0 1334
aoqi@0 1335 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
aoqi@0 1336 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
aoqi@0 1337 if (fs.offset() == offset) {
aoqi@0 1338 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
aoqi@0 1339 if (fd->is_static() == is_static) return true;
aoqi@0 1340 }
aoqi@0 1341 }
aoqi@0 1342 return false;
aoqi@0 1343 }
aoqi@0 1344
aoqi@0 1345
aoqi@0 1346 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
aoqi@0 1347 Klass* klass = const_cast<InstanceKlass*>(this);
aoqi@0 1348 while (klass != NULL) {
aoqi@0 1349 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
aoqi@0 1350 return true;
aoqi@0 1351 }
aoqi@0 1352 klass = klass->super();
aoqi@0 1353 }
aoqi@0 1354 return false;
aoqi@0 1355 }
aoqi@0 1356
aoqi@0 1357
aoqi@0 1358 void InstanceKlass::methods_do(void f(Method* method)) {
aoqi@0 1359 int len = methods()->length();
aoqi@0 1360 for (int index = 0; index < len; index++) {
aoqi@0 1361 Method* m = methods()->at(index);
aoqi@0 1362 assert(m->is_method(), "must be method");
aoqi@0 1363 f(m);
aoqi@0 1364 }
aoqi@0 1365 }
aoqi@0 1366
aoqi@0 1367
aoqi@0 1368 void InstanceKlass::do_local_static_fields(FieldClosure* cl) {
aoqi@0 1369 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
aoqi@0 1370 if (fs.access_flags().is_static()) {
aoqi@0 1371 fieldDescriptor& fd = fs.field_descriptor();
aoqi@0 1372 cl->do_field(&fd);
aoqi@0 1373 }
aoqi@0 1374 }
aoqi@0 1375 }
aoqi@0 1376
aoqi@0 1377
aoqi@0 1378 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) {
aoqi@0 1379 instanceKlassHandle h_this(THREAD, this);
aoqi@0 1380 do_local_static_fields_impl(h_this, f, mirror, CHECK);
aoqi@0 1381 }
aoqi@0 1382
aoqi@0 1383
aoqi@0 1384 void InstanceKlass::do_local_static_fields_impl(instanceKlassHandle this_k,
aoqi@0 1385 void f(fieldDescriptor* fd, Handle mirror, TRAPS), Handle mirror, TRAPS) {
aoqi@0 1386 for (JavaFieldStream fs(this_k()); !fs.done(); fs.next()) {
aoqi@0 1387 if (fs.access_flags().is_static()) {
aoqi@0 1388 fieldDescriptor& fd = fs.field_descriptor();
aoqi@0 1389 f(&fd, mirror, CHECK);
aoqi@0 1390 }
aoqi@0 1391 }
aoqi@0 1392 }
aoqi@0 1393
aoqi@0 1394
aoqi@0 1395 static int compare_fields_by_offset(int* a, int* b) {
aoqi@0 1396 return a[0] - b[0];
aoqi@0 1397 }
aoqi@0 1398
aoqi@0 1399 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) {
aoqi@0 1400 InstanceKlass* super = superklass();
aoqi@0 1401 if (super != NULL) {
aoqi@0 1402 super->do_nonstatic_fields(cl);
aoqi@0 1403 }
aoqi@0 1404 fieldDescriptor fd;
aoqi@0 1405 int length = java_fields_count();
aoqi@0 1406 // In DebugInfo nonstatic fields are sorted by offset.
aoqi@0 1407 int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1), mtClass);
aoqi@0 1408 int j = 0;
aoqi@0 1409 for (int i = 0; i < length; i += 1) {
aoqi@0 1410 fd.reinitialize(this, i);
aoqi@0 1411 if (!fd.is_static()) {
aoqi@0 1412 fields_sorted[j + 0] = fd.offset();
aoqi@0 1413 fields_sorted[j + 1] = i;
aoqi@0 1414 j += 2;
aoqi@0 1415 }
aoqi@0 1416 }
aoqi@0 1417 if (j > 0) {
aoqi@0 1418 length = j;
aoqi@0 1419 // _sort_Fn is defined in growableArray.hpp.
aoqi@0 1420 qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset);
aoqi@0 1421 for (int i = 0; i < length; i += 2) {
aoqi@0 1422 fd.reinitialize(this, fields_sorted[i + 1]);
aoqi@0 1423 assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields");
aoqi@0 1424 cl->do_field(&fd);
aoqi@0 1425 }
aoqi@0 1426 }
aoqi@0 1427 FREE_C_HEAP_ARRAY(int, fields_sorted, mtClass);
aoqi@0 1428 }
aoqi@0 1429
aoqi@0 1430
aoqi@0 1431 void InstanceKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
aoqi@0 1432 if (array_klasses() != NULL)
aoqi@0 1433 ArrayKlass::cast(array_klasses())->array_klasses_do(f, THREAD);
aoqi@0 1434 }
aoqi@0 1435
aoqi@0 1436 void InstanceKlass::array_klasses_do(void f(Klass* k)) {
aoqi@0 1437 if (array_klasses() != NULL)
aoqi@0 1438 ArrayKlass::cast(array_klasses())->array_klasses_do(f);
aoqi@0 1439 }
aoqi@0 1440
aoqi@0 1441 #ifdef ASSERT
aoqi@0 1442 static int linear_search(Array<Method*>* methods, Symbol* name, Symbol* signature) {
aoqi@0 1443 int len = methods->length();
aoqi@0 1444 for (int index = 0; index < len; index++) {
aoqi@0 1445 Method* m = methods->at(index);
aoqi@0 1446 assert(m->is_method(), "must be method");
aoqi@0 1447 if (m->signature() == signature && m->name() == name) {
aoqi@0 1448 return index;
aoqi@0 1449 }
aoqi@0 1450 }
aoqi@0 1451 return -1;
aoqi@0 1452 }
aoqi@0 1453 #endif
aoqi@0 1454
aoqi@0 1455 static int binary_search(Array<Method*>* methods, Symbol* name) {
aoqi@0 1456 int len = methods->length();
aoqi@0 1457 // methods are sorted, so do binary search
aoqi@0 1458 int l = 0;
aoqi@0 1459 int h = len - 1;
aoqi@0 1460 while (l <= h) {
aoqi@0 1461 int mid = (l + h) >> 1;
aoqi@0 1462 Method* m = methods->at(mid);
aoqi@0 1463 assert(m->is_method(), "must be method");
aoqi@0 1464 int res = m->name()->fast_compare(name);
aoqi@0 1465 if (res == 0) {
aoqi@0 1466 return mid;
aoqi@0 1467 } else if (res < 0) {
aoqi@0 1468 l = mid + 1;
aoqi@0 1469 } else {
aoqi@0 1470 h = mid - 1;
aoqi@0 1471 }
aoqi@0 1472 }
aoqi@0 1473 return -1;
aoqi@0 1474 }
aoqi@0 1475
aoqi@0 1476 // find_method looks up the name/signature in the local methods array
aoqi@0 1477 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const {
dbuck@8716 1478 return find_method_impl(name, signature, find_overpass, find_static, find_private);
aoqi@0 1479 }
aoqi@0 1480
dbuck@8716 1481 Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature,
dbuck@8716 1482 OverpassLookupMode overpass_mode,
dbuck@8716 1483 StaticLookupMode static_mode,
dbuck@8716 1484 PrivateLookupMode private_mode) const {
dbuck@8716 1485 return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode);
aoqi@0 1486 }
aoqi@0 1487
aoqi@0 1488 // find_instance_method looks up the name/signature in the local methods array
aoqi@0 1489 // and skips over static methods
aoqi@0 1490 Method* InstanceKlass::find_instance_method(
aoqi@0 1491 Array<Method*>* methods, Symbol* name, Symbol* signature) {
dbuck@8716 1492 Method* meth = InstanceKlass::find_method_impl(methods, name, signature,
dbuck@8716 1493 find_overpass, skip_static, find_private);
dbuck@8716 1494 assert(((meth == NULL) || !meth->is_static()), "find_instance_method should have skipped statics");
aoqi@0 1495 return meth;
aoqi@0 1496 }
aoqi@0 1497
drchase@7499 1498 // find_instance_method looks up the name/signature in the local methods array
drchase@7499 1499 // and skips over static methods
drchase@7499 1500 Method* InstanceKlass::find_instance_method(Symbol* name, Symbol* signature) {
drchase@7499 1501 return InstanceKlass::find_instance_method(methods(), name, signature);
drchase@7499 1502 }
drchase@7499 1503
dbuck@8716 1504 // Find looks up the name/signature in the local methods array
dbuck@8716 1505 // and filters on the overpass, static and private flags
dbuck@8716 1506 // This returns the first one found
dbuck@8716 1507 // note that the local methods array can have up to one overpass, one static
dbuck@8716 1508 // and one instance (private or not) with the same name/signature
dbuck@8716 1509 Method* InstanceKlass::find_local_method(Symbol* name, Symbol* signature,
dbuck@8716 1510 OverpassLookupMode overpass_mode,
dbuck@8716 1511 StaticLookupMode static_mode,
dbuck@8716 1512 PrivateLookupMode private_mode) const {
dbuck@8716 1513 return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode);
dbuck@8716 1514 }
dbuck@8716 1515
dbuck@8716 1516 // Find looks up the name/signature in the local methods array
dbuck@8716 1517 // and filters on the overpass, static and private flags
dbuck@8716 1518 // This returns the first one found
dbuck@8716 1519 // note that the local methods array can have up to one overpass, one static
dbuck@8716 1520 // and one instance (private or not) with the same name/signature
dbuck@8716 1521 Method* InstanceKlass::find_local_method(Array<Method*>* methods,
dbuck@8716 1522 Symbol* name, Symbol* signature,
dbuck@8716 1523 OverpassLookupMode overpass_mode,
dbuck@8716 1524 StaticLookupMode static_mode,
dbuck@8716 1525 PrivateLookupMode private_mode) {
dbuck@8716 1526 return InstanceKlass::find_method_impl(methods, name, signature, overpass_mode, static_mode, private_mode);
dbuck@8716 1527 }
dbuck@8716 1528
dbuck@8716 1529
aoqi@0 1530 // find_method looks up the name/signature in the local methods array
aoqi@0 1531 Method* InstanceKlass::find_method(
aoqi@0 1532 Array<Method*>* methods, Symbol* name, Symbol* signature) {
dbuck@8716 1533 return InstanceKlass::find_method_impl(methods, name, signature, find_overpass, find_static, find_private);
aoqi@0 1534 }
aoqi@0 1535
aoqi@0 1536 Method* InstanceKlass::find_method_impl(
dbuck@8716 1537 Array<Method*>* methods, Symbol* name, Symbol* signature,
dbuck@8716 1538 OverpassLookupMode overpass_mode, StaticLookupMode static_mode,
dbuck@8716 1539 PrivateLookupMode private_mode) {
dbuck@8716 1540 int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode);
aoqi@0 1541 return hit >= 0 ? methods->at(hit): NULL;
aoqi@0 1542 }
aoqi@0 1543
dbuck@8716 1544 bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static, bool skipping_private) {
dbuck@8716 1545 return ((m->signature() == signature) &&
drchase@7499 1546 (!skipping_overpass || !m->is_overpass()) &&
dbuck@8716 1547 (!skipping_static || !m->is_static()) &&
dbuck@8716 1548 (!skipping_private || !m->is_private()));
drchase@7499 1549 }
drchase@7499 1550
aoqi@0 1551 // Used directly for default_methods to find the index into the
aoqi@0 1552 // default_vtable_indices, and indirectly by find_method
aoqi@0 1553 // find_method_index looks in the local methods array to return the index
aoqi@0 1554 // of the matching name/signature. If, overpass methods are being ignored,
aoqi@0 1555 // the search continues to find a potential non-overpass match. This capability
aoqi@0 1556 // is important during method resolution to prefer a static method, for example,
aoqi@0 1557 // over an overpass method.
dbuck@8716 1558 // There is the possibility in any _method's array to have the same name/signature
dbuck@8716 1559 // for a static method, an overpass method and a local instance method
dbuck@8716 1560 // To correctly catch a given method, the search criteria may need
dbuck@8716 1561 // to explicitly skip the other two. For local instance methods, it
dbuck@8716 1562 // is often necessary to skip private methods
aoqi@0 1563 int InstanceKlass::find_method_index(
dbuck@8716 1564 Array<Method*>* methods, Symbol* name, Symbol* signature,
dbuck@8716 1565 OverpassLookupMode overpass_mode, StaticLookupMode static_mode,
dbuck@8716 1566 PrivateLookupMode private_mode) {
dbuck@8716 1567 bool skipping_overpass = (overpass_mode == skip_overpass);
dbuck@8716 1568 bool skipping_static = (static_mode == skip_static);
dbuck@8716 1569 bool skipping_private = (private_mode == skip_private);
aoqi@0 1570 int hit = binary_search(methods, name);
aoqi@0 1571 if (hit != -1) {
aoqi@0 1572 Method* m = methods->at(hit);
drchase@7499 1573
aoqi@0 1574 // Do linear search to find matching signature. First, quick check
aoqi@0 1575 // for common case, ignoring overpasses if requested.
dbuck@8716 1576 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return hit;
aoqi@0 1577
aoqi@0 1578 // search downwards through overloaded methods
aoqi@0 1579 int i;
aoqi@0 1580 for (i = hit - 1; i >= 0; --i) {
aoqi@0 1581 Method* m = methods->at(i);
aoqi@0 1582 assert(m->is_method(), "must be method");
aoqi@0 1583 if (m->name() != name) break;
dbuck@8716 1584 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i;
aoqi@0 1585 }
aoqi@0 1586 // search upwards
aoqi@0 1587 for (i = hit + 1; i < methods->length(); ++i) {
aoqi@0 1588 Method* m = methods->at(i);
aoqi@0 1589 assert(m->is_method(), "must be method");
aoqi@0 1590 if (m->name() != name) break;
dbuck@8716 1591 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i;
aoqi@0 1592 }
aoqi@0 1593 // not found
aoqi@0 1594 #ifdef ASSERT
dbuck@8716 1595 int index = (skipping_overpass || skipping_static || skipping_private) ? -1 : linear_search(methods, name, signature);
aoqi@0 1596 assert(index == -1, err_msg("binary search should have found entry %d", index));
aoqi@0 1597 #endif
aoqi@0 1598 }
aoqi@0 1599 return -1;
aoqi@0 1600 }
aoqi@0 1601 int InstanceKlass::find_method_by_name(Symbol* name, int* end) {
aoqi@0 1602 return find_method_by_name(methods(), name, end);
aoqi@0 1603 }
aoqi@0 1604
aoqi@0 1605 int InstanceKlass::find_method_by_name(
aoqi@0 1606 Array<Method*>* methods, Symbol* name, int* end_ptr) {
aoqi@0 1607 assert(end_ptr != NULL, "just checking");
aoqi@0 1608 int start = binary_search(methods, name);
aoqi@0 1609 int end = start + 1;
aoqi@0 1610 if (start != -1) {
aoqi@0 1611 while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
aoqi@0 1612 while (end < methods->length() && (methods->at(end))->name() == name) ++end;
aoqi@0 1613 *end_ptr = end;
aoqi@0 1614 return start;
aoqi@0 1615 }
aoqi@0 1616 return -1;
aoqi@0 1617 }
aoqi@0 1618
aoqi@0 1619 // uncached_lookup_method searches both the local class methods array and all
aoqi@0 1620 // superclasses methods arrays, skipping any overpass methods in superclasses.
dbuck@8716 1621 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
dbuck@8716 1622 OverpassLookupMode overpass_local_mode = overpass_mode;
aoqi@0 1623 Klass* klass = const_cast<InstanceKlass*>(this);
aoqi@0 1624 while (klass != NULL) {
dbuck@8716 1625 Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, overpass_local_mode, find_static, find_private);
aoqi@0 1626 if (method != NULL) {
aoqi@0 1627 return method;
aoqi@0 1628 }
aoqi@0 1629 klass = InstanceKlass::cast(klass)->super();
dbuck@8716 1630 overpass_local_mode = skip_overpass; // Always ignore overpass methods in superclasses
aoqi@0 1631 }
aoqi@0 1632 return NULL;
aoqi@0 1633 }
aoqi@0 1634
dsamersoff@8049 1635 #ifdef ASSERT
dsamersoff@8049 1636 // search through class hierarchy and return true if this class or
dsamersoff@8049 1637 // one of the superclasses was redefined
dsamersoff@8049 1638 bool InstanceKlass::has_redefined_this_or_super() const {
dsamersoff@8049 1639 const InstanceKlass* klass = this;
dsamersoff@8049 1640 while (klass != NULL) {
dsamersoff@8049 1641 if (klass->has_been_redefined()) {
dsamersoff@8049 1642 return true;
dsamersoff@8049 1643 }
dsamersoff@8049 1644 klass = InstanceKlass::cast(klass->super());
dsamersoff@8049 1645 }
dsamersoff@8049 1646 return false;
dsamersoff@8049 1647 }
dsamersoff@8049 1648 #endif
dsamersoff@8049 1649
aoqi@0 1650 // lookup a method in the default methods list then in all transitive interfaces
aoqi@0 1651 // Do NOT return private or static methods
aoqi@0 1652 Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name,
aoqi@0 1653 Symbol* signature) const {
aoqi@0 1654 Method* m = NULL;
aoqi@0 1655 if (default_methods() != NULL) {
aoqi@0 1656 m = find_method(default_methods(), name, signature);
aoqi@0 1657 }
aoqi@0 1658 // Look up interfaces
aoqi@0 1659 if (m == NULL) {
dbuck@8716 1660 m = lookup_method_in_all_interfaces(name, signature, find_defaults);
aoqi@0 1661 }
aoqi@0 1662 return m;
aoqi@0 1663 }
aoqi@0 1664
aoqi@0 1665 // lookup a method in all the interfaces that this class implements
aoqi@0 1666 // Do NOT return private or static methods, new in JDK8 which are not externally visible
aoqi@0 1667 // They should only be found in the initial InterfaceMethodRef
aoqi@0 1668 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name,
aoqi@0 1669 Symbol* signature,
dbuck@8716 1670 DefaultsLookupMode defaults_mode) const {
aoqi@0 1671 Array<Klass*>* all_ifs = transitive_interfaces();
aoqi@0 1672 int num_ifs = all_ifs->length();
aoqi@0 1673 InstanceKlass *ik = NULL;
aoqi@0 1674 for (int i = 0; i < num_ifs; i++) {
aoqi@0 1675 ik = InstanceKlass::cast(all_ifs->at(i));
aoqi@0 1676 Method* m = ik->lookup_method(name, signature);
aoqi@0 1677 if (m != NULL && m->is_public() && !m->is_static() &&
dbuck@8716 1678 ((defaults_mode != skip_defaults) || !m->is_default_method())) {
aoqi@0 1679 return m;
aoqi@0 1680 }
aoqi@0 1681 }
aoqi@0 1682 return NULL;
aoqi@0 1683 }
aoqi@0 1684
aoqi@0 1685 /* jni_id_for_impl for jfieldIds only */
aoqi@0 1686 JNIid* InstanceKlass::jni_id_for_impl(instanceKlassHandle this_oop, int offset) {
aoqi@0 1687 MutexLocker ml(JfieldIdCreation_lock);
aoqi@0 1688 // Retry lookup after we got the lock
aoqi@0 1689 JNIid* probe = this_oop->jni_ids() == NULL ? NULL : this_oop->jni_ids()->find(offset);
aoqi@0 1690 if (probe == NULL) {
aoqi@0 1691 // Slow case, allocate new static field identifier
aoqi@0 1692 probe = new JNIid(this_oop(), offset, this_oop->jni_ids());
aoqi@0 1693 this_oop->set_jni_ids(probe);
aoqi@0 1694 }
aoqi@0 1695 return probe;
aoqi@0 1696 }
aoqi@0 1697
aoqi@0 1698
aoqi@0 1699 /* jni_id_for for jfieldIds only */
aoqi@0 1700 JNIid* InstanceKlass::jni_id_for(int offset) {
aoqi@0 1701 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
aoqi@0 1702 if (probe == NULL) {
aoqi@0 1703 probe = jni_id_for_impl(this, offset);
aoqi@0 1704 }
aoqi@0 1705 return probe;
aoqi@0 1706 }
aoqi@0 1707
aoqi@0 1708 u2 InstanceKlass::enclosing_method_data(int offset) {
aoqi@0 1709 Array<jushort>* inner_class_list = inner_classes();
aoqi@0 1710 if (inner_class_list == NULL) {
aoqi@0 1711 return 0;
aoqi@0 1712 }
aoqi@0 1713 int length = inner_class_list->length();
aoqi@0 1714 if (length % inner_class_next_offset == 0) {
aoqi@0 1715 return 0;
aoqi@0 1716 } else {
aoqi@0 1717 int index = length - enclosing_method_attribute_size;
aoqi@0 1718 assert(offset < enclosing_method_attribute_size, "invalid offset");
aoqi@0 1719 return inner_class_list->at(index + offset);
aoqi@0 1720 }
aoqi@0 1721 }
aoqi@0 1722
aoqi@0 1723 void InstanceKlass::set_enclosing_method_indices(u2 class_index,
aoqi@0 1724 u2 method_index) {
aoqi@0 1725 Array<jushort>* inner_class_list = inner_classes();
aoqi@0 1726 assert (inner_class_list != NULL, "_inner_classes list is not set up");
aoqi@0 1727 int length = inner_class_list->length();
aoqi@0 1728 if (length % inner_class_next_offset == enclosing_method_attribute_size) {
aoqi@0 1729 int index = length - enclosing_method_attribute_size;
aoqi@0 1730 inner_class_list->at_put(
aoqi@0 1731 index + enclosing_method_class_index_offset, class_index);
aoqi@0 1732 inner_class_list->at_put(
aoqi@0 1733 index + enclosing_method_method_index_offset, method_index);
aoqi@0 1734 }
aoqi@0 1735 }
aoqi@0 1736
aoqi@0 1737 // Lookup or create a jmethodID.
aoqi@0 1738 // This code is called by the VMThread and JavaThreads so the
aoqi@0 1739 // locking has to be done very carefully to avoid deadlocks
aoqi@0 1740 // and/or other cache consistency problems.
aoqi@0 1741 //
aoqi@0 1742 jmethodID InstanceKlass::get_jmethod_id(instanceKlassHandle ik_h, methodHandle method_h) {
aoqi@0 1743 size_t idnum = (size_t)method_h->method_idnum();
aoqi@0 1744 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
aoqi@0 1745 size_t length = 0;
aoqi@0 1746 jmethodID id = NULL;
aoqi@0 1747
aoqi@0 1748 // We use a double-check locking idiom here because this cache is
aoqi@0 1749 // performance sensitive. In the normal system, this cache only
aoqi@0 1750 // transitions from NULL to non-NULL which is safe because we use
aoqi@0 1751 // release_set_methods_jmethod_ids() to advertise the new cache.
aoqi@0 1752 // A partially constructed cache should never be seen by a racing
aoqi@0 1753 // thread. We also use release_store_ptr() to save a new jmethodID
aoqi@0 1754 // in the cache so a partially constructed jmethodID should never be
aoqi@0 1755 // seen either. Cache reads of existing jmethodIDs proceed without a
aoqi@0 1756 // lock, but cache writes of a new jmethodID requires uniqueness and
aoqi@0 1757 // creation of the cache itself requires no leaks so a lock is
aoqi@0 1758 // generally acquired in those two cases.
aoqi@0 1759 //
aoqi@0 1760 // If the RedefineClasses() API has been used, then this cache can
aoqi@0 1761 // grow and we'll have transitions from non-NULL to bigger non-NULL.
aoqi@0 1762 // Cache creation requires no leaks and we require safety between all
aoqi@0 1763 // cache accesses and freeing of the old cache so a lock is generally
aoqi@0 1764 // acquired when the RedefineClasses() API has been used.
aoqi@0 1765
aoqi@0 1766 if (jmeths != NULL) {
aoqi@0 1767 // the cache already exists
aoqi@0 1768 if (!ik_h->idnum_can_increment()) {
aoqi@0 1769 // the cache can't grow so we can just get the current values
aoqi@0 1770 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
aoqi@0 1771 } else {
aoqi@0 1772 // cache can grow so we have to be more careful
aoqi@0 1773 if (Threads::number_of_threads() == 0 ||
aoqi@0 1774 SafepointSynchronize::is_at_safepoint()) {
aoqi@0 1775 // we're single threaded or at a safepoint - no locking needed
aoqi@0 1776 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
aoqi@0 1777 } else {
aoqi@0 1778 MutexLocker ml(JmethodIdCreation_lock);
aoqi@0 1779 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
aoqi@0 1780 }
aoqi@0 1781 }
aoqi@0 1782 }
aoqi@0 1783 // implied else:
aoqi@0 1784 // we need to allocate a cache so default length and id values are good
aoqi@0 1785
aoqi@0 1786 if (jmeths == NULL || // no cache yet
aoqi@0 1787 length <= idnum || // cache is too short
aoqi@0 1788 id == NULL) { // cache doesn't contain entry
aoqi@0 1789
aoqi@0 1790 // This function can be called by the VMThread so we have to do all
aoqi@0 1791 // things that might block on a safepoint before grabbing the lock.
aoqi@0 1792 // Otherwise, we can deadlock with the VMThread or have a cache
aoqi@0 1793 // consistency issue. These vars keep track of what we might have
aoqi@0 1794 // to free after the lock is dropped.
aoqi@0 1795 jmethodID to_dealloc_id = NULL;
aoqi@0 1796 jmethodID* to_dealloc_jmeths = NULL;
aoqi@0 1797
aoqi@0 1798 // may not allocate new_jmeths or use it if we allocate it
aoqi@0 1799 jmethodID* new_jmeths = NULL;
aoqi@0 1800 if (length <= idnum) {
aoqi@0 1801 // allocate a new cache that might be used
aoqi@0 1802 size_t size = MAX2(idnum+1, (size_t)ik_h->idnum_allocated_count());
aoqi@0 1803 new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1, mtClass);
aoqi@0 1804 memset(new_jmeths, 0, (size+1)*sizeof(jmethodID));
aoqi@0 1805 // cache size is stored in element[0], other elements offset by one
aoqi@0 1806 new_jmeths[0] = (jmethodID)size;
aoqi@0 1807 }
aoqi@0 1808
aoqi@0 1809 // allocate a new jmethodID that might be used
aoqi@0 1810 jmethodID new_id = NULL;
aoqi@0 1811 if (method_h->is_old() && !method_h->is_obsolete()) {
aoqi@0 1812 // The method passed in is old (but not obsolete), we need to use the current version
aoqi@0 1813 Method* current_method = ik_h->method_with_idnum((int)idnum);
aoqi@0 1814 assert(current_method != NULL, "old and but not obsolete, so should exist");
aoqi@0 1815 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), current_method);
aoqi@0 1816 } else {
aoqi@0 1817 // It is the current version of the method or an obsolete method,
aoqi@0 1818 // use the version passed in
aoqi@0 1819 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), method_h());
aoqi@0 1820 }
aoqi@0 1821
aoqi@0 1822 if (Threads::number_of_threads() == 0 ||
aoqi@0 1823 SafepointSynchronize::is_at_safepoint()) {
aoqi@0 1824 // we're single threaded or at a safepoint - no locking needed
aoqi@0 1825 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
aoqi@0 1826 &to_dealloc_id, &to_dealloc_jmeths);
aoqi@0 1827 } else {
aoqi@0 1828 MutexLocker ml(JmethodIdCreation_lock);
aoqi@0 1829 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
aoqi@0 1830 &to_dealloc_id, &to_dealloc_jmeths);
aoqi@0 1831 }
aoqi@0 1832
aoqi@0 1833 // The lock has been dropped so we can free resources.
aoqi@0 1834 // Free up either the old cache or the new cache if we allocated one.
aoqi@0 1835 if (to_dealloc_jmeths != NULL) {
aoqi@0 1836 FreeHeap(to_dealloc_jmeths);
aoqi@0 1837 }
aoqi@0 1838 // free up the new ID since it wasn't needed
aoqi@0 1839 if (to_dealloc_id != NULL) {
aoqi@0 1840 Method::destroy_jmethod_id(ik_h->class_loader_data(), to_dealloc_id);
aoqi@0 1841 }
aoqi@0 1842 }
aoqi@0 1843 return id;
aoqi@0 1844 }
aoqi@0 1845
aoqi@0 1846
aoqi@0 1847 // Common code to fetch the jmethodID from the cache or update the
aoqi@0 1848 // cache with the new jmethodID. This function should never do anything
aoqi@0 1849 // that causes the caller to go to a safepoint or we can deadlock with
aoqi@0 1850 // the VMThread or have cache consistency issues.
aoqi@0 1851 //
aoqi@0 1852 jmethodID InstanceKlass::get_jmethod_id_fetch_or_update(
aoqi@0 1853 instanceKlassHandle ik_h, size_t idnum, jmethodID new_id,
aoqi@0 1854 jmethodID* new_jmeths, jmethodID* to_dealloc_id_p,
aoqi@0 1855 jmethodID** to_dealloc_jmeths_p) {
aoqi@0 1856 assert(new_id != NULL, "sanity check");
aoqi@0 1857 assert(to_dealloc_id_p != NULL, "sanity check");
aoqi@0 1858 assert(to_dealloc_jmeths_p != NULL, "sanity check");
aoqi@0 1859 assert(Threads::number_of_threads() == 0 ||
aoqi@0 1860 SafepointSynchronize::is_at_safepoint() ||
aoqi@0 1861 JmethodIdCreation_lock->owned_by_self(), "sanity check");
aoqi@0 1862
aoqi@0 1863 // reacquire the cache - we are locked, single threaded or at a safepoint
aoqi@0 1864 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
aoqi@0 1865 jmethodID id = NULL;
aoqi@0 1866 size_t length = 0;
aoqi@0 1867
aoqi@0 1868 if (jmeths == NULL || // no cache yet
aoqi@0 1869 (length = (size_t)jmeths[0]) <= idnum) { // cache is too short
aoqi@0 1870 if (jmeths != NULL) {
aoqi@0 1871 // copy any existing entries from the old cache
aoqi@0 1872 for (size_t index = 0; index < length; index++) {
aoqi@0 1873 new_jmeths[index+1] = jmeths[index+1];
aoqi@0 1874 }
aoqi@0 1875 *to_dealloc_jmeths_p = jmeths; // save old cache for later delete
aoqi@0 1876 }
aoqi@0 1877 ik_h->release_set_methods_jmethod_ids(jmeths = new_jmeths);
aoqi@0 1878 } else {
aoqi@0 1879 // fetch jmethodID (if any) from the existing cache
aoqi@0 1880 id = jmeths[idnum+1];
aoqi@0 1881 *to_dealloc_jmeths_p = new_jmeths; // save new cache for later delete
aoqi@0 1882 }
aoqi@0 1883 if (id == NULL) {
aoqi@0 1884 // No matching jmethodID in the existing cache or we have a new
aoqi@0 1885 // cache or we just grew the cache. This cache write is done here
aoqi@0 1886 // by the first thread to win the foot race because a jmethodID
aoqi@0 1887 // needs to be unique once it is generally available.
aoqi@0 1888 id = new_id;
aoqi@0 1889
aoqi@0 1890 // The jmethodID cache can be read while unlocked so we have to
aoqi@0 1891 // make sure the new jmethodID is complete before installing it
aoqi@0 1892 // in the cache.
aoqi@0 1893 OrderAccess::release_store_ptr(&jmeths[idnum+1], id);
aoqi@0 1894 } else {
aoqi@0 1895 *to_dealloc_id_p = new_id; // save new id for later delete
aoqi@0 1896 }
aoqi@0 1897 return id;
aoqi@0 1898 }
aoqi@0 1899
aoqi@0 1900
aoqi@0 1901 // Common code to get the jmethodID cache length and the jmethodID
aoqi@0 1902 // value at index idnum if there is one.
aoqi@0 1903 //
aoqi@0 1904 void InstanceKlass::get_jmethod_id_length_value(jmethodID* cache,
aoqi@0 1905 size_t idnum, size_t *length_p, jmethodID* id_p) {
aoqi@0 1906 assert(cache != NULL, "sanity check");
aoqi@0 1907 assert(length_p != NULL, "sanity check");
aoqi@0 1908 assert(id_p != NULL, "sanity check");
aoqi@0 1909
aoqi@0 1910 // cache size is stored in element[0], other elements offset by one
aoqi@0 1911 *length_p = (size_t)cache[0];
aoqi@0 1912 if (*length_p <= idnum) { // cache is too short
aoqi@0 1913 *id_p = NULL;
aoqi@0 1914 } else {
aoqi@0 1915 *id_p = cache[idnum+1]; // fetch jmethodID (if any)
aoqi@0 1916 }
aoqi@0 1917 }
aoqi@0 1918
aoqi@0 1919
aoqi@0 1920 // Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles
aoqi@0 1921 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
aoqi@0 1922 size_t idnum = (size_t)method->method_idnum();
aoqi@0 1923 jmethodID* jmeths = methods_jmethod_ids_acquire();
aoqi@0 1924 size_t length; // length assigned as debugging crumb
aoqi@0 1925 jmethodID id = NULL;
aoqi@0 1926 if (jmeths != NULL && // If there is a cache
aoqi@0 1927 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough,
aoqi@0 1928 id = jmeths[idnum+1]; // Look up the id (may be NULL)
aoqi@0 1929 }
aoqi@0 1930 return id;
aoqi@0 1931 }
aoqi@0 1932
stefank@6992 1933 int nmethodBucket::decrement() {
stefank@6992 1934 return Atomic::add(-1, (volatile int *)&_count);
stefank@6992 1935 }
aoqi@0 1936
aoqi@0 1937 //
aoqi@0 1938 // Walk the list of dependent nmethods searching for nmethods which
aoqi@0 1939 // are dependent on the changes that were passed in and mark them for
aoqi@0 1940 // deoptimization. Returns the number of nmethods found.
aoqi@0 1941 //
aoqi@0 1942 int InstanceKlass::mark_dependent_nmethods(DepChange& changes) {
aoqi@0 1943 assert_locked_or_safepoint(CodeCache_lock);
aoqi@0 1944 int found = 0;
aoqi@0 1945 nmethodBucket* b = _dependencies;
aoqi@0 1946 while (b != NULL) {
aoqi@0 1947 nmethod* nm = b->get_nmethod();
aoqi@0 1948 // since dependencies aren't removed until an nmethod becomes a zombie,
aoqi@0 1949 // the dependency list may contain nmethods which aren't alive.
stefank@6992 1950 if (b->count() > 0 && nm->is_alive() && !nm->is_marked_for_deoptimization() && nm->check_dependency_on(changes)) {
aoqi@0 1951 if (TraceDependencies) {
aoqi@0 1952 ResourceMark rm;
aoqi@0 1953 tty->print_cr("Marked for deoptimization");
aoqi@0 1954 tty->print_cr(" context = %s", this->external_name());
aoqi@0 1955 changes.print();
aoqi@0 1956 nm->print();
aoqi@0 1957 nm->print_dependencies();
aoqi@0 1958 }
aoqi@0 1959 nm->mark_for_deoptimization();
aoqi@0 1960 found++;
aoqi@0 1961 }
aoqi@0 1962 b = b->next();
aoqi@0 1963 }
aoqi@0 1964 return found;
aoqi@0 1965 }
aoqi@0 1966
stefank@6992 1967 void InstanceKlass::clean_dependent_nmethods() {
stefank@6992 1968 assert_locked_or_safepoint(CodeCache_lock);
stefank@6992 1969
stefank@6992 1970 if (has_unloaded_dependent()) {
stefank@6992 1971 nmethodBucket* b = _dependencies;
stefank@6992 1972 nmethodBucket* last = NULL;
stefank@6992 1973 while (b != NULL) {
stefank@6992 1974 assert(b->count() >= 0, err_msg("bucket count: %d", b->count()));
stefank@6992 1975
stefank@6992 1976 nmethodBucket* next = b->next();
stefank@6992 1977
stefank@6992 1978 if (b->count() == 0) {
stefank@6992 1979 if (last == NULL) {
stefank@6992 1980 _dependencies = next;
stefank@6992 1981 } else {
stefank@6992 1982 last->set_next(next);
stefank@6992 1983 }
stefank@6992 1984 delete b;
stefank@6992 1985 // last stays the same.
stefank@6992 1986 } else {
stefank@6992 1987 last = b;
stefank@6992 1988 }
stefank@6992 1989
stefank@6992 1990 b = next;
stefank@6992 1991 }
stefank@6992 1992 set_has_unloaded_dependent(false);
stefank@6992 1993 }
stefank@6992 1994 #ifdef ASSERT
stefank@6992 1995 else {
stefank@6992 1996 // Verification
stefank@6992 1997 for (nmethodBucket* b = _dependencies; b != NULL; b = b->next()) {
stefank@6992 1998 assert(b->count() >= 0, err_msg("bucket count: %d", b->count()));
stefank@6992 1999 assert(b->count() != 0, "empty buckets need to be cleaned");
stefank@6992 2000 }
stefank@6992 2001 }
stefank@6992 2002 #endif
stefank@6992 2003 }
aoqi@0 2004
aoqi@0 2005 //
aoqi@0 2006 // Add an nmethodBucket to the list of dependencies for this nmethod.
aoqi@0 2007 // It's possible that an nmethod has multiple dependencies on this klass
aoqi@0 2008 // so a count is kept for each bucket to guarantee that creation and
aoqi@0 2009 // deletion of dependencies is consistent.
aoqi@0 2010 //
aoqi@0 2011 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
aoqi@0 2012 assert_locked_or_safepoint(CodeCache_lock);
aoqi@0 2013 nmethodBucket* b = _dependencies;
aoqi@0 2014 nmethodBucket* last = NULL;
aoqi@0 2015 while (b != NULL) {
aoqi@0 2016 if (nm == b->get_nmethod()) {
aoqi@0 2017 b->increment();
aoqi@0 2018 return;
aoqi@0 2019 }
aoqi@0 2020 b = b->next();
aoqi@0 2021 }
aoqi@0 2022 _dependencies = new nmethodBucket(nm, _dependencies);
aoqi@0 2023 }
aoqi@0 2024
aoqi@0 2025
aoqi@0 2026 //
aoqi@0 2027 // Decrement count of the nmethod in the dependency list and remove
aoqi@0 2028 // the bucket competely when the count goes to 0. This method must
aoqi@0 2029 // find a corresponding bucket otherwise there's a bug in the
aoqi@0 2030 // recording of dependecies.
aoqi@0 2031 //
stefank@8185 2032 void InstanceKlass::remove_dependent_nmethod(nmethod* nm, bool delete_immediately) {
aoqi@0 2033 assert_locked_or_safepoint(CodeCache_lock);
aoqi@0 2034 nmethodBucket* b = _dependencies;
aoqi@0 2035 nmethodBucket* last = NULL;
aoqi@0 2036 while (b != NULL) {
aoqi@0 2037 if (nm == b->get_nmethod()) {
stefank@6992 2038 int val = b->decrement();
stefank@6992 2039 guarantee(val >= 0, err_msg("Underflow: %d", val));
stefank@6992 2040 if (val == 0) {
stefank@8185 2041 if (delete_immediately) {
stefank@8185 2042 if (last == NULL) {
stefank@8185 2043 _dependencies = b->next();
stefank@8185 2044 } else {
stefank@8185 2045 last->set_next(b->next());
stefank@8185 2046 }
stefank@8185 2047 delete b;
stefank@8185 2048 } else {
stefank@8185 2049 // The deletion of this entry is deferred until a later, potentially parallel GC phase.
stefank@8185 2050 set_has_unloaded_dependent(true);
stefank@8185 2051 }
aoqi@0 2052 }
aoqi@0 2053 return;
aoqi@0 2054 }
aoqi@0 2055 last = b;
aoqi@0 2056 b = b->next();
aoqi@0 2057 }
aoqi@0 2058 #ifdef ASSERT
aoqi@0 2059 tty->print_cr("### %s can't find dependent nmethod:", this->external_name());
aoqi@0 2060 nm->print();
aoqi@0 2061 #endif // ASSERT
aoqi@0 2062 ShouldNotReachHere();
aoqi@0 2063 }
aoqi@0 2064
aoqi@0 2065
aoqi@0 2066 #ifndef PRODUCT
aoqi@0 2067 void InstanceKlass::print_dependent_nmethods(bool verbose) {
aoqi@0 2068 nmethodBucket* b = _dependencies;
aoqi@0 2069 int idx = 0;
aoqi@0 2070 while (b != NULL) {
aoqi@0 2071 nmethod* nm = b->get_nmethod();
aoqi@0 2072 tty->print("[%d] count=%d { ", idx++, b->count());
aoqi@0 2073 if (!verbose) {
aoqi@0 2074 nm->print_on(tty, "nmethod");
aoqi@0 2075 tty->print_cr(" } ");
aoqi@0 2076 } else {
aoqi@0 2077 nm->print();
aoqi@0 2078 nm->print_dependencies();
aoqi@0 2079 tty->print_cr("--- } ");
aoqi@0 2080 }
aoqi@0 2081 b = b->next();
aoqi@0 2082 }
aoqi@0 2083 }
aoqi@0 2084
aoqi@0 2085
aoqi@0 2086 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
aoqi@0 2087 nmethodBucket* b = _dependencies;
aoqi@0 2088 while (b != NULL) {
aoqi@0 2089 if (nm == b->get_nmethod()) {
stefank@6992 2090 #ifdef ASSERT
stefank@6992 2091 int count = b->count();
ehelin@6993 2092 assert(count >= 0, err_msg("count shouldn't be negative: %d", count));
stefank@6992 2093 #endif
aoqi@0 2094 return true;
aoqi@0 2095 }
aoqi@0 2096 b = b->next();
aoqi@0 2097 }
aoqi@0 2098 return false;
aoqi@0 2099 }
aoqi@0 2100 #endif //PRODUCT
aoqi@0 2101
aoqi@0 2102
aoqi@0 2103 // Garbage collection
aoqi@0 2104
aoqi@0 2105 #ifdef ASSERT
aoqi@0 2106 template <class T> void assert_is_in(T *p) {
aoqi@0 2107 T heap_oop = oopDesc::load_heap_oop(p);
aoqi@0 2108 if (!oopDesc::is_null(heap_oop)) {
aoqi@0 2109 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
aoqi@0 2110 assert(Universe::heap()->is_in(o), "should be in heap");
aoqi@0 2111 }
aoqi@0 2112 }
aoqi@0 2113 template <class T> void assert_is_in_closed_subset(T *p) {
aoqi@0 2114 T heap_oop = oopDesc::load_heap_oop(p);
aoqi@0 2115 if (!oopDesc::is_null(heap_oop)) {
aoqi@0 2116 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
aoqi@0 2117 assert(Universe::heap()->is_in_closed_subset(o),
aoqi@0 2118 err_msg("should be in closed *p " INTPTR_FORMAT " " INTPTR_FORMAT, (address)p, (address)o));
aoqi@0 2119 }
aoqi@0 2120 }
aoqi@0 2121 template <class T> void assert_is_in_reserved(T *p) {
aoqi@0 2122 T heap_oop = oopDesc::load_heap_oop(p);
aoqi@0 2123 if (!oopDesc::is_null(heap_oop)) {
aoqi@0 2124 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
aoqi@0 2125 assert(Universe::heap()->is_in_reserved(o), "should be in reserved");
aoqi@0 2126 }
aoqi@0 2127 }
aoqi@0 2128 template <class T> void assert_nothing(T *p) {}
aoqi@0 2129
aoqi@0 2130 #else
aoqi@0 2131 template <class T> void assert_is_in(T *p) {}
aoqi@0 2132 template <class T> void assert_is_in_closed_subset(T *p) {}
aoqi@0 2133 template <class T> void assert_is_in_reserved(T *p) {}
aoqi@0 2134 template <class T> void assert_nothing(T *p) {}
aoqi@0 2135 #endif // ASSERT
aoqi@0 2136
aoqi@0 2137 //
aoqi@0 2138 // Macros that iterate over areas of oops which are specialized on type of
aoqi@0 2139 // oop pointer either narrow or wide, depending on UseCompressedOops
aoqi@0 2140 //
aoqi@0 2141 // Parameters are:
aoqi@0 2142 // T - type of oop to point to (either oop or narrowOop)
aoqi@0 2143 // start_p - starting pointer for region to iterate over
aoqi@0 2144 // count - number of oops or narrowOops to iterate over
aoqi@0 2145 // do_oop - action to perform on each oop (it's arbitrary C code which
aoqi@0 2146 // makes it more efficient to put in a macro rather than making
aoqi@0 2147 // it a template function)
aoqi@0 2148 // assert_fn - assert function which is template function because performance
aoqi@0 2149 // doesn't matter when enabled.
aoqi@0 2150 #define InstanceKlass_SPECIALIZED_OOP_ITERATE( \
aoqi@0 2151 T, start_p, count, do_oop, \
aoqi@0 2152 assert_fn) \
aoqi@0 2153 { \
aoqi@0 2154 T* p = (T*)(start_p); \
aoqi@0 2155 T* const end = p + (count); \
aoqi@0 2156 while (p < end) { \
aoqi@0 2157 (assert_fn)(p); \
aoqi@0 2158 do_oop; \
aoqi@0 2159 ++p; \
aoqi@0 2160 } \
aoqi@0 2161 }
aoqi@0 2162
aoqi@0 2163 #define InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE( \
aoqi@0 2164 T, start_p, count, do_oop, \
aoqi@0 2165 assert_fn) \
aoqi@0 2166 { \
aoqi@0 2167 T* const start = (T*)(start_p); \
aoqi@0 2168 T* p = start + (count); \
aoqi@0 2169 while (start < p) { \
aoqi@0 2170 --p; \
aoqi@0 2171 (assert_fn)(p); \
aoqi@0 2172 do_oop; \
aoqi@0 2173 } \
aoqi@0 2174 }
aoqi@0 2175
aoqi@0 2176 #define InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE( \
aoqi@0 2177 T, start_p, count, low, high, \
aoqi@0 2178 do_oop, assert_fn) \
aoqi@0 2179 { \
aoqi@0 2180 T* const l = (T*)(low); \
aoqi@0 2181 T* const h = (T*)(high); \
aoqi@0 2182 assert(mask_bits((intptr_t)l, sizeof(T)-1) == 0 && \
aoqi@0 2183 mask_bits((intptr_t)h, sizeof(T)-1) == 0, \
aoqi@0 2184 "bounded region must be properly aligned"); \
aoqi@0 2185 T* p = (T*)(start_p); \
aoqi@0 2186 T* end = p + (count); \
aoqi@0 2187 if (p < l) p = l; \
aoqi@0 2188 if (end > h) end = h; \
aoqi@0 2189 while (p < end) { \
aoqi@0 2190 (assert_fn)(p); \
aoqi@0 2191 do_oop; \
aoqi@0 2192 ++p; \
aoqi@0 2193 } \
aoqi@0 2194 }
aoqi@0 2195
aoqi@0 2196
aoqi@0 2197 // The following macros call specialized macros, passing either oop or
aoqi@0 2198 // narrowOop as the specialization type. These test the UseCompressedOops
aoqi@0 2199 // flag.
aoqi@0 2200 #define InstanceKlass_OOP_MAP_ITERATE(obj, do_oop, assert_fn) \
aoqi@0 2201 { \
aoqi@0 2202 /* Compute oopmap block range. The common case \
aoqi@0 2203 is nonstatic_oop_map_size == 1. */ \
aoqi@0 2204 OopMapBlock* map = start_of_nonstatic_oop_maps(); \
aoqi@0 2205 OopMapBlock* const end_map = map + nonstatic_oop_map_count(); \
aoqi@0 2206 if (UseCompressedOops) { \
aoqi@0 2207 while (map < end_map) { \
aoqi@0 2208 InstanceKlass_SPECIALIZED_OOP_ITERATE(narrowOop, \
aoqi@0 2209 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
aoqi@0 2210 do_oop, assert_fn) \
aoqi@0 2211 ++map; \
aoqi@0 2212 } \
aoqi@0 2213 } else { \
aoqi@0 2214 while (map < end_map) { \
aoqi@0 2215 InstanceKlass_SPECIALIZED_OOP_ITERATE(oop, \
aoqi@0 2216 obj->obj_field_addr<oop>(map->offset()), map->count(), \
aoqi@0 2217 do_oop, assert_fn) \
aoqi@0 2218 ++map; \
aoqi@0 2219 } \
aoqi@0 2220 } \
aoqi@0 2221 }
aoqi@0 2222
aoqi@0 2223 #define InstanceKlass_OOP_MAP_REVERSE_ITERATE(obj, do_oop, assert_fn) \
aoqi@0 2224 { \
aoqi@0 2225 OopMapBlock* const start_map = start_of_nonstatic_oop_maps(); \
aoqi@0 2226 OopMapBlock* map = start_map + nonstatic_oop_map_count(); \
aoqi@0 2227 if (UseCompressedOops) { \
aoqi@0 2228 while (start_map < map) { \
aoqi@0 2229 --map; \
aoqi@0 2230 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(narrowOop, \
aoqi@0 2231 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
aoqi@0 2232 do_oop, assert_fn) \
aoqi@0 2233 } \
aoqi@0 2234 } else { \
aoqi@0 2235 while (start_map < map) { \
aoqi@0 2236 --map; \
aoqi@0 2237 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(oop, \
aoqi@0 2238 obj->obj_field_addr<oop>(map->offset()), map->count(), \
aoqi@0 2239 do_oop, assert_fn) \
aoqi@0 2240 } \
aoqi@0 2241 } \
aoqi@0 2242 }
aoqi@0 2243
aoqi@0 2244 #define InstanceKlass_BOUNDED_OOP_MAP_ITERATE(obj, low, high, do_oop, \
aoqi@0 2245 assert_fn) \
aoqi@0 2246 { \
aoqi@0 2247 /* Compute oopmap block range. The common case is \
aoqi@0 2248 nonstatic_oop_map_size == 1, so we accept the \
aoqi@0 2249 usually non-existent extra overhead of examining \
aoqi@0 2250 all the maps. */ \
aoqi@0 2251 OopMapBlock* map = start_of_nonstatic_oop_maps(); \
aoqi@0 2252 OopMapBlock* const end_map = map + nonstatic_oop_map_count(); \
aoqi@0 2253 if (UseCompressedOops) { \
aoqi@0 2254 while (map < end_map) { \
aoqi@0 2255 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(narrowOop, \
aoqi@0 2256 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
aoqi@0 2257 low, high, \
aoqi@0 2258 do_oop, assert_fn) \
aoqi@0 2259 ++map; \
aoqi@0 2260 } \
aoqi@0 2261 } else { \
aoqi@0 2262 while (map < end_map) { \
aoqi@0 2263 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(oop, \
aoqi@0 2264 obj->obj_field_addr<oop>(map->offset()), map->count(), \
aoqi@0 2265 low, high, \
aoqi@0 2266 do_oop, assert_fn) \
aoqi@0 2267 ++map; \
aoqi@0 2268 } \
aoqi@0 2269 } \
aoqi@0 2270 }
aoqi@0 2271
aoqi@0 2272 void InstanceKlass::oop_follow_contents(oop obj) {
aoqi@0 2273 assert(obj != NULL, "can't follow the content of NULL object");
aoqi@0 2274 MarkSweep::follow_klass(obj->klass());
aoqi@0 2275 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2276 obj, \
aoqi@0 2277 MarkSweep::mark_and_push(p), \
aoqi@0 2278 assert_is_in_closed_subset)
aoqi@0 2279 }
aoqi@0 2280
aoqi@0 2281 #if INCLUDE_ALL_GCS
aoqi@0 2282 void InstanceKlass::oop_follow_contents(ParCompactionManager* cm,
aoqi@0 2283 oop obj) {
aoqi@0 2284 assert(obj != NULL, "can't follow the content of NULL object");
aoqi@0 2285 PSParallelCompact::follow_klass(cm, obj->klass());
aoqi@0 2286 // Only mark the header and let the scan of the meta-data mark
aoqi@0 2287 // everything else.
aoqi@0 2288 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2289 obj, \
aoqi@0 2290 PSParallelCompact::mark_and_push(cm, p), \
aoqi@0 2291 assert_is_in)
aoqi@0 2292 }
aoqi@0 2293 #endif // INCLUDE_ALL_GCS
aoqi@0 2294
aoqi@0 2295 // closure's do_metadata() method dictates whether the given closure should be
aoqi@0 2296 // applied to the klass ptr in the object header.
aoqi@0 2297
aoqi@0 2298 #define InstanceKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \
aoqi@0 2299 \
aoqi@0 2300 int InstanceKlass::oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) { \
aoqi@0 2301 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\
aoqi@0 2302 /* header */ \
aoqi@0 2303 if_do_metadata_checked(closure, nv_suffix) { \
aoqi@0 2304 closure->do_klass##nv_suffix(obj->klass()); \
aoqi@0 2305 } \
aoqi@0 2306 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2307 obj, \
aoqi@0 2308 SpecializationStats:: \
aoqi@0 2309 record_do_oop_call##nv_suffix(SpecializationStats::ik); \
aoqi@0 2310 (closure)->do_oop##nv_suffix(p), \
aoqi@0 2311 assert_is_in_closed_subset) \
aoqi@0 2312 return size_helper(); \
aoqi@0 2313 }
aoqi@0 2314
aoqi@0 2315 #if INCLUDE_ALL_GCS
aoqi@0 2316 #define InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \
aoqi@0 2317 \
aoqi@0 2318 int InstanceKlass::oop_oop_iterate_backwards##nv_suffix(oop obj, \
aoqi@0 2319 OopClosureType* closure) { \
aoqi@0 2320 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik); \
stefank@6982 2321 \
stefank@6982 2322 assert_should_ignore_metadata(closure, nv_suffix); \
stefank@6982 2323 \
aoqi@0 2324 /* instance variables */ \
aoqi@0 2325 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \
aoqi@0 2326 obj, \
aoqi@0 2327 SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::ik);\
aoqi@0 2328 (closure)->do_oop##nv_suffix(p), \
aoqi@0 2329 assert_is_in_closed_subset) \
aoqi@0 2330 return size_helper(); \
aoqi@0 2331 }
aoqi@0 2332 #endif // INCLUDE_ALL_GCS
aoqi@0 2333
aoqi@0 2334 #define InstanceKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \
aoqi@0 2335 \
aoqi@0 2336 int InstanceKlass::oop_oop_iterate##nv_suffix##_m(oop obj, \
aoqi@0 2337 OopClosureType* closure, \
aoqi@0 2338 MemRegion mr) { \
aoqi@0 2339 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\
aoqi@0 2340 if_do_metadata_checked(closure, nv_suffix) { \
aoqi@0 2341 if (mr.contains(obj)) { \
aoqi@0 2342 closure->do_klass##nv_suffix(obj->klass()); \
aoqi@0 2343 } \
aoqi@0 2344 } \
aoqi@0 2345 InstanceKlass_BOUNDED_OOP_MAP_ITERATE( \
aoqi@0 2346 obj, mr.start(), mr.end(), \
aoqi@0 2347 (closure)->do_oop##nv_suffix(p), \
aoqi@0 2348 assert_is_in_closed_subset) \
aoqi@0 2349 return size_helper(); \
aoqi@0 2350 }
aoqi@0 2351
aoqi@0 2352 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN)
aoqi@0 2353 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DEFN)
aoqi@0 2354 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN_m)
aoqi@0 2355 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DEFN_m)
aoqi@0 2356 #if INCLUDE_ALL_GCS
aoqi@0 2357 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
aoqi@0 2358 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
aoqi@0 2359 #endif // INCLUDE_ALL_GCS
aoqi@0 2360
aoqi@0 2361 int InstanceKlass::oop_adjust_pointers(oop obj) {
aoqi@0 2362 int size = size_helper();
aoqi@0 2363 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2364 obj, \
aoqi@0 2365 MarkSweep::adjust_pointer(p), \
aoqi@0 2366 assert_is_in)
aoqi@0 2367 return size;
aoqi@0 2368 }
aoqi@0 2369
aoqi@0 2370 #if INCLUDE_ALL_GCS
aoqi@0 2371 void InstanceKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
aoqi@0 2372 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \
aoqi@0 2373 obj, \
aoqi@0 2374 if (PSScavenge::should_scavenge(p)) { \
aoqi@0 2375 pm->claim_or_forward_depth(p); \
aoqi@0 2376 }, \
aoqi@0 2377 assert_nothing )
aoqi@0 2378 }
aoqi@0 2379
aoqi@0 2380 int InstanceKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
aoqi@0 2381 int size = size_helper();
aoqi@0 2382 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2383 obj, \
aoqi@0 2384 PSParallelCompact::adjust_pointer(p), \
aoqi@0 2385 assert_is_in)
aoqi@0 2386 return size;
aoqi@0 2387 }
aoqi@0 2388
aoqi@0 2389 #endif // INCLUDE_ALL_GCS
aoqi@0 2390
stefank@8185 2391 void InstanceKlass::clean_weak_instanceklass_links(BoolObjectClosure* is_alive) {
stefank@8185 2392 clean_implementors_list(is_alive);
stefank@8185 2393 clean_method_data(is_alive);
stefank@8185 2394
stefank@8185 2395 clean_dependent_nmethods();
stefank@8185 2396 }
stefank@8185 2397
aoqi@0 2398 void InstanceKlass::clean_implementors_list(BoolObjectClosure* is_alive) {
stefank@6992 2399 assert(class_loader_data()->is_alive(is_alive), "this klass should be live");
aoqi@0 2400 if (is_interface()) {
aoqi@0 2401 if (ClassUnloading) {
aoqi@0 2402 Klass* impl = implementor();
aoqi@0 2403 if (impl != NULL) {
aoqi@0 2404 if (!impl->is_loader_alive(is_alive)) {
aoqi@0 2405 // remove this guy
aoqi@0 2406 Klass** klass = adr_implementor();
aoqi@0 2407 assert(klass != NULL, "null klass");
aoqi@0 2408 if (klass != NULL) {
aoqi@0 2409 *klass = NULL;
aoqi@0 2410 }
aoqi@0 2411 }
aoqi@0 2412 }
aoqi@0 2413 }
aoqi@0 2414 }
aoqi@0 2415 }
aoqi@0 2416
aoqi@0 2417 void InstanceKlass::clean_method_data(BoolObjectClosure* is_alive) {
aoqi@0 2418 for (int m = 0; m < methods()->length(); m++) {
aoqi@0 2419 MethodData* mdo = methods()->at(m)->method_data();
aoqi@0 2420 if (mdo != NULL) {
aoqi@0 2421 mdo->clean_method_data(is_alive);
aoqi@0 2422 }
aoqi@0 2423 }
aoqi@0 2424 }
aoqi@0 2425
aoqi@0 2426
aoqi@0 2427 static void remove_unshareable_in_class(Klass* k) {
aoqi@0 2428 // remove klass's unshareable info
aoqi@0 2429 k->remove_unshareable_info();
aoqi@0 2430 }
aoqi@0 2431
aoqi@0 2432 void InstanceKlass::remove_unshareable_info() {
aoqi@0 2433 Klass::remove_unshareable_info();
aoqi@0 2434 // Unlink the class
aoqi@0 2435 if (is_linked()) {
aoqi@0 2436 unlink_class();
aoqi@0 2437 }
aoqi@0 2438 init_implementor();
aoqi@0 2439
aoqi@0 2440 constants()->remove_unshareable_info();
aoqi@0 2441
aoqi@0 2442 for (int i = 0; i < methods()->length(); i++) {
aoqi@0 2443 Method* m = methods()->at(i);
aoqi@0 2444 m->remove_unshareable_info();
aoqi@0 2445 }
aoqi@0 2446
aoqi@0 2447 // do array classes also.
aoqi@0 2448 array_klasses_do(remove_unshareable_in_class);
aoqi@0 2449 }
aoqi@0 2450
iklam@7089 2451 static void restore_unshareable_in_class(Klass* k, TRAPS) {
iklam@7089 2452 // Array classes have null protection domain.
iklam@7089 2453 // --> see ArrayKlass::complete_create_array_klass()
iklam@7089 2454 k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
aoqi@0 2455 }
aoqi@0 2456
iklam@7089 2457 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
iklam@7089 2458 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
aoqi@0 2459 instanceKlassHandle ik(THREAD, this);
aoqi@0 2460
aoqi@0 2461 Array<Method*>* methods = ik->methods();
aoqi@0 2462 int num_methods = methods->length();
aoqi@0 2463 for (int index2 = 0; index2 < num_methods; ++index2) {
aoqi@0 2464 methodHandle m(THREAD, methods->at(index2));
aoqi@0 2465 m->restore_unshareable_info(CHECK);
aoqi@0 2466 }
aoqi@0 2467 if (JvmtiExport::has_redefined_a_class()) {
aoqi@0 2468 // Reinitialize vtable because RedefineClasses may have changed some
aoqi@0 2469 // entries in this vtable for super classes so the CDS vtable might
aoqi@0 2470 // point to old or obsolete entries. RedefineClasses doesn't fix up
aoqi@0 2471 // vtables in the shared system dictionary, only the main one.
aoqi@0 2472 // It also redefines the itable too so fix that too.
aoqi@0 2473 ResourceMark rm(THREAD);
aoqi@0 2474 ik->vtable()->initialize_vtable(false, CHECK);
aoqi@0 2475 ik->itable()->initialize_itable(false, CHECK);
aoqi@0 2476 }
aoqi@0 2477
aoqi@0 2478 // restore constant pool resolved references
aoqi@0 2479 ik->constants()->restore_unshareable_info(CHECK);
aoqi@0 2480
aoqi@0 2481 ik->array_klasses_do(restore_unshareable_in_class, CHECK);
aoqi@0 2482 }
aoqi@0 2483
iklam@7089 2484 // returns true IFF is_in_error_state() has been changed as a result of this call.
iklam@7089 2485 bool InstanceKlass::check_sharing_error_state() {
iklam@7089 2486 assert(DumpSharedSpaces, "should only be called during dumping");
iklam@7089 2487 bool old_state = is_in_error_state();
iklam@7089 2488
iklam@7089 2489 if (!is_in_error_state()) {
iklam@7089 2490 bool bad = false;
iklam@7089 2491 for (InstanceKlass* sup = java_super(); sup; sup = sup->java_super()) {
iklam@7089 2492 if (sup->is_in_error_state()) {
iklam@7089 2493 bad = true;
iklam@7089 2494 break;
iklam@7089 2495 }
iklam@7089 2496 }
iklam@7089 2497 if (!bad) {
iklam@7089 2498 Array<Klass*>* interfaces = transitive_interfaces();
iklam@7089 2499 for (int i = 0; i < interfaces->length(); i++) {
iklam@7089 2500 Klass* iface = interfaces->at(i);
iklam@7089 2501 if (InstanceKlass::cast(iface)->is_in_error_state()) {
iklam@7089 2502 bad = true;
iklam@7089 2503 break;
iklam@7089 2504 }
iklam@7089 2505 }
iklam@7089 2506 }
iklam@7089 2507
iklam@7089 2508 if (bad) {
iklam@7089 2509 set_in_error_state();
iklam@7089 2510 }
iklam@7089 2511 }
iklam@7089 2512
iklam@7089 2513 return (old_state != is_in_error_state());
iklam@7089 2514 }
iklam@7089 2515
aoqi@0 2516 static void clear_all_breakpoints(Method* m) {
aoqi@0 2517 m->clear_all_breakpoints();
aoqi@0 2518 }
aoqi@0 2519
aoqi@0 2520
aoqi@0 2521 void InstanceKlass::notify_unload_class(InstanceKlass* ik) {
aoqi@0 2522 // notify the debugger
aoqi@0 2523 if (JvmtiExport::should_post_class_unload()) {
aoqi@0 2524 JvmtiExport::post_class_unload(ik);
aoqi@0 2525 }
aoqi@0 2526
aoqi@0 2527 // notify ClassLoadingService of class unload
aoqi@0 2528 ClassLoadingService::notify_class_unloaded(ik);
aoqi@0 2529 }
aoqi@0 2530
aoqi@0 2531 void InstanceKlass::release_C_heap_structures(InstanceKlass* ik) {
aoqi@0 2532 // Clean up C heap
aoqi@0 2533 ik->release_C_heap_structures();
aoqi@0 2534 ik->constants()->release_C_heap_structures();
aoqi@0 2535 }
aoqi@0 2536
aoqi@0 2537 void InstanceKlass::release_C_heap_structures() {
aoqi@0 2538
aoqi@0 2539 // Can't release the constant pool here because the constant pool can be
aoqi@0 2540 // deallocated separately from the InstanceKlass for default methods and
aoqi@0 2541 // redefine classes.
aoqi@0 2542
aoqi@0 2543 // Deallocate oop map cache
aoqi@0 2544 if (_oop_map_cache != NULL) {
aoqi@0 2545 delete _oop_map_cache;
aoqi@0 2546 _oop_map_cache = NULL;
aoqi@0 2547 }
aoqi@0 2548
aoqi@0 2549 // Deallocate JNI identifiers for jfieldIDs
aoqi@0 2550 JNIid::deallocate(jni_ids());
aoqi@0 2551 set_jni_ids(NULL);
aoqi@0 2552
aoqi@0 2553 jmethodID* jmeths = methods_jmethod_ids_acquire();
aoqi@0 2554 if (jmeths != (jmethodID*)NULL) {
aoqi@0 2555 release_set_methods_jmethod_ids(NULL);
aoqi@0 2556 FreeHeap(jmeths);
aoqi@0 2557 }
aoqi@0 2558
aoqi@0 2559 // Deallocate MemberNameTable
aoqi@0 2560 {
aoqi@0 2561 Mutex* lock_or_null = SafepointSynchronize::is_at_safepoint() ? NULL : MemberNameTable_lock;
aoqi@0 2562 MutexLockerEx ml(lock_or_null, Mutex::_no_safepoint_check_flag);
aoqi@0 2563 MemberNameTable* mnt = member_names();
aoqi@0 2564 if (mnt != NULL) {
aoqi@0 2565 delete mnt;
aoqi@0 2566 set_member_names(NULL);
aoqi@0 2567 }
aoqi@0 2568 }
aoqi@0 2569
aoqi@0 2570 // release dependencies
aoqi@0 2571 nmethodBucket* b = _dependencies;
aoqi@0 2572 _dependencies = NULL;
aoqi@0 2573 while (b != NULL) {
aoqi@0 2574 nmethodBucket* next = b->next();
aoqi@0 2575 delete b;
aoqi@0 2576 b = next;
aoqi@0 2577 }
aoqi@0 2578
aoqi@0 2579 // Deallocate breakpoint records
aoqi@0 2580 if (breakpoints() != 0x0) {
aoqi@0 2581 methods_do(clear_all_breakpoints);
aoqi@0 2582 assert(breakpoints() == 0x0, "should have cleared breakpoints");
aoqi@0 2583 }
aoqi@0 2584
aoqi@0 2585 // deallocate the cached class file
aoqi@0 2586 if (_cached_class_file != NULL) {
aoqi@0 2587 os::free(_cached_class_file, mtClass);
aoqi@0 2588 _cached_class_file = NULL;
aoqi@0 2589 }
aoqi@0 2590
aoqi@0 2591 // Decrement symbol reference counts associated with the unloaded class.
aoqi@0 2592 if (_name != NULL) _name->decrement_refcount();
aoqi@0 2593 // unreference array name derived from this class name (arrays of an unloaded
aoqi@0 2594 // class can't be referenced anymore).
aoqi@0 2595 if (_array_name != NULL) _array_name->decrement_refcount();
aoqi@0 2596 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension, mtClass);
aoqi@0 2597
aoqi@0 2598 assert(_total_instanceKlass_count >= 1, "Sanity check");
aoqi@0 2599 Atomic::dec(&_total_instanceKlass_count);
aoqi@0 2600 }
aoqi@0 2601
aoqi@0 2602 void InstanceKlass::set_source_debug_extension(char* array, int length) {
aoqi@0 2603 if (array == NULL) {
aoqi@0 2604 _source_debug_extension = NULL;
aoqi@0 2605 } else {
aoqi@0 2606 // Adding one to the attribute length in order to store a null terminator
aoqi@0 2607 // character could cause an overflow because the attribute length is
aoqi@0 2608 // already coded with an u4 in the classfile, but in practice, it's
aoqi@0 2609 // unlikely to happen.
aoqi@0 2610 assert((length+1) > length, "Overflow checking");
aoqi@0 2611 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
aoqi@0 2612 for (int i = 0; i < length; i++) {
aoqi@0 2613 sde[i] = array[i];
aoqi@0 2614 }
aoqi@0 2615 sde[length] = '\0';
aoqi@0 2616 _source_debug_extension = sde;
aoqi@0 2617 }
aoqi@0 2618 }
aoqi@0 2619
aoqi@0 2620 address InstanceKlass::static_field_addr(int offset) {
aoqi@0 2621 return (address)(offset + InstanceMirrorKlass::offset_of_static_fields() + cast_from_oop<intptr_t>(java_mirror()));
aoqi@0 2622 }
aoqi@0 2623
aoqi@0 2624
aoqi@0 2625 const char* InstanceKlass::signature_name() const {
aoqi@0 2626 int hash_len = 0;
aoqi@0 2627 char hash_buf[40];
aoqi@0 2628
aoqi@0 2629 // If this is an anonymous class, append a hash to make the name unique
aoqi@0 2630 if (is_anonymous()) {
aoqi@0 2631 assert(EnableInvokeDynamic, "EnableInvokeDynamic was not set.");
aoqi@0 2632 intptr_t hash = (java_mirror() != NULL) ? java_mirror()->identity_hash() : 0;
aoqi@0 2633 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
aoqi@0 2634 hash_len = (int)strlen(hash_buf);
aoqi@0 2635 }
aoqi@0 2636
aoqi@0 2637 // Get the internal name as a c string
aoqi@0 2638 const char* src = (const char*) (name()->as_C_string());
aoqi@0 2639 const int src_length = (int)strlen(src);
aoqi@0 2640
aoqi@0 2641 char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
aoqi@0 2642
aoqi@0 2643 // Add L as type indicator
aoqi@0 2644 int dest_index = 0;
aoqi@0 2645 dest[dest_index++] = 'L';
aoqi@0 2646
aoqi@0 2647 // Add the actual class name
aoqi@0 2648 for (int src_index = 0; src_index < src_length; ) {
aoqi@0 2649 dest[dest_index++] = src[src_index++];
aoqi@0 2650 }
aoqi@0 2651
aoqi@0 2652 // If we have a hash, append it
aoqi@0 2653 for (int hash_index = 0; hash_index < hash_len; ) {
aoqi@0 2654 dest[dest_index++] = hash_buf[hash_index++];
aoqi@0 2655 }
aoqi@0 2656
aoqi@0 2657 // Add the semicolon and the NULL
aoqi@0 2658 dest[dest_index++] = ';';
aoqi@0 2659 dest[dest_index] = '\0';
aoqi@0 2660 return dest;
aoqi@0 2661 }
aoqi@0 2662
aoqi@0 2663 // different verisons of is_same_class_package
aoqi@0 2664 bool InstanceKlass::is_same_class_package(Klass* class2) {
aoqi@0 2665 Klass* class1 = this;
aoqi@0 2666 oop classloader1 = InstanceKlass::cast(class1)->class_loader();
aoqi@0 2667 Symbol* classname1 = class1->name();
aoqi@0 2668
aoqi@0 2669 if (class2->oop_is_objArray()) {
aoqi@0 2670 class2 = ObjArrayKlass::cast(class2)->bottom_klass();
aoqi@0 2671 }
aoqi@0 2672 oop classloader2;
aoqi@0 2673 if (class2->oop_is_instance()) {
aoqi@0 2674 classloader2 = InstanceKlass::cast(class2)->class_loader();
aoqi@0 2675 } else {
aoqi@0 2676 assert(class2->oop_is_typeArray(), "should be type array");
aoqi@0 2677 classloader2 = NULL;
aoqi@0 2678 }
aoqi@0 2679 Symbol* classname2 = class2->name();
aoqi@0 2680
aoqi@0 2681 return InstanceKlass::is_same_class_package(classloader1, classname1,
aoqi@0 2682 classloader2, classname2);
aoqi@0 2683 }
aoqi@0 2684
aoqi@0 2685 bool InstanceKlass::is_same_class_package(oop classloader2, Symbol* classname2) {
aoqi@0 2686 Klass* class1 = this;
aoqi@0 2687 oop classloader1 = InstanceKlass::cast(class1)->class_loader();
aoqi@0 2688 Symbol* classname1 = class1->name();
aoqi@0 2689
aoqi@0 2690 return InstanceKlass::is_same_class_package(classloader1, classname1,
aoqi@0 2691 classloader2, classname2);
aoqi@0 2692 }
aoqi@0 2693
aoqi@0 2694 // return true if two classes are in the same package, classloader
aoqi@0 2695 // and classname information is enough to determine a class's package
aoqi@0 2696 bool InstanceKlass::is_same_class_package(oop class_loader1, Symbol* class_name1,
aoqi@0 2697 oop class_loader2, Symbol* class_name2) {
aoqi@0 2698 if (class_loader1 != class_loader2) {
aoqi@0 2699 return false;
aoqi@0 2700 } else if (class_name1 == class_name2) {
aoqi@0 2701 return true; // skip painful bytewise comparison
aoqi@0 2702 } else {
aoqi@0 2703 ResourceMark rm;
aoqi@0 2704
aoqi@0 2705 // The Symbol*'s are in UTF8 encoding. Since we only need to check explicitly
aoqi@0 2706 // for ASCII characters ('/', 'L', '['), we can keep them in UTF8 encoding.
aoqi@0 2707 // Otherwise, we just compare jbyte values between the strings.
aoqi@0 2708 const jbyte *name1 = class_name1->base();
aoqi@0 2709 const jbyte *name2 = class_name2->base();
aoqi@0 2710
aoqi@0 2711 const jbyte *last_slash1 = UTF8::strrchr(name1, class_name1->utf8_length(), '/');
aoqi@0 2712 const jbyte *last_slash2 = UTF8::strrchr(name2, class_name2->utf8_length(), '/');
aoqi@0 2713
aoqi@0 2714 if ((last_slash1 == NULL) || (last_slash2 == NULL)) {
aoqi@0 2715 // One of the two doesn't have a package. Only return true
aoqi@0 2716 // if the other one also doesn't have a package.
aoqi@0 2717 return last_slash1 == last_slash2;
aoqi@0 2718 } else {
aoqi@0 2719 // Skip over '['s
aoqi@0 2720 if (*name1 == '[') {
aoqi@0 2721 do {
aoqi@0 2722 name1++;
aoqi@0 2723 } while (*name1 == '[');
aoqi@0 2724 if (*name1 != 'L') {
aoqi@0 2725 // Something is terribly wrong. Shouldn't be here.
aoqi@0 2726 return false;
aoqi@0 2727 }
aoqi@0 2728 }
aoqi@0 2729 if (*name2 == '[') {
aoqi@0 2730 do {
aoqi@0 2731 name2++;
aoqi@0 2732 } while (*name2 == '[');
aoqi@0 2733 if (*name2 != 'L') {
aoqi@0 2734 // Something is terribly wrong. Shouldn't be here.
aoqi@0 2735 return false;
aoqi@0 2736 }
aoqi@0 2737 }
aoqi@0 2738
aoqi@0 2739 // Check that package part is identical
aoqi@0 2740 int length1 = last_slash1 - name1;
aoqi@0 2741 int length2 = last_slash2 - name2;
aoqi@0 2742
aoqi@0 2743 return UTF8::equal(name1, length1, name2, length2);
aoqi@0 2744 }
aoqi@0 2745 }
aoqi@0 2746 }
aoqi@0 2747
aoqi@0 2748 // Returns true iff super_method can be overridden by a method in targetclassname
aoqi@0 2749 // See JSL 3rd edition 8.4.6.1
aoqi@0 2750 // Assumes name-signature match
aoqi@0 2751 // "this" is InstanceKlass of super_method which must exist
aoqi@0 2752 // note that the InstanceKlass of the method in the targetclassname has not always been created yet
aoqi@0 2753 bool InstanceKlass::is_override(methodHandle super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) {
aoqi@0 2754 // Private methods can not be overridden
aoqi@0 2755 if (super_method->is_private()) {
aoqi@0 2756 return false;
aoqi@0 2757 }
aoqi@0 2758 // If super method is accessible, then override
aoqi@0 2759 if ((super_method->is_protected()) ||
aoqi@0 2760 (super_method->is_public())) {
aoqi@0 2761 return true;
aoqi@0 2762 }
aoqi@0 2763 // Package-private methods are not inherited outside of package
aoqi@0 2764 assert(super_method->is_package_private(), "must be package private");
aoqi@0 2765 return(is_same_class_package(targetclassloader(), targetclassname));
aoqi@0 2766 }
aoqi@0 2767
aoqi@0 2768 /* defined for now in jvm.cpp, for historical reasons *--
aoqi@0 2769 Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle self,
aoqi@0 2770 Symbol*& simple_name_result, TRAPS) {
aoqi@0 2771 ...
aoqi@0 2772 }
aoqi@0 2773 */
aoqi@0 2774
aoqi@0 2775 // tell if two classes have the same enclosing class (at package level)
aoqi@0 2776 bool InstanceKlass::is_same_package_member_impl(instanceKlassHandle class1,
aoqi@0 2777 Klass* class2_oop, TRAPS) {
aoqi@0 2778 if (class2_oop == class1()) return true;
aoqi@0 2779 if (!class2_oop->oop_is_instance()) return false;
aoqi@0 2780 instanceKlassHandle class2(THREAD, class2_oop);
aoqi@0 2781
aoqi@0 2782 // must be in same package before we try anything else
aoqi@0 2783 if (!class1->is_same_class_package(class2->class_loader(), class2->name()))
aoqi@0 2784 return false;
aoqi@0 2785
aoqi@0 2786 // As long as there is an outer1.getEnclosingClass,
aoqi@0 2787 // shift the search outward.
aoqi@0 2788 instanceKlassHandle outer1 = class1;
aoqi@0 2789 for (;;) {
aoqi@0 2790 // As we walk along, look for equalities between outer1 and class2.
aoqi@0 2791 // Eventually, the walks will terminate as outer1 stops
aoqi@0 2792 // at the top-level class around the original class.
aoqi@0 2793 bool ignore_inner_is_member;
aoqi@0 2794 Klass* next = outer1->compute_enclosing_class(&ignore_inner_is_member,
aoqi@0 2795 CHECK_false);
aoqi@0 2796 if (next == NULL) break;
aoqi@0 2797 if (next == class2()) return true;
aoqi@0 2798 outer1 = instanceKlassHandle(THREAD, next);
aoqi@0 2799 }
aoqi@0 2800
aoqi@0 2801 // Now do the same for class2.
aoqi@0 2802 instanceKlassHandle outer2 = class2;
aoqi@0 2803 for (;;) {
aoqi@0 2804 bool ignore_inner_is_member;
aoqi@0 2805 Klass* next = outer2->compute_enclosing_class(&ignore_inner_is_member,
aoqi@0 2806 CHECK_false);
aoqi@0 2807 if (next == NULL) break;
aoqi@0 2808 // Might as well check the new outer against all available values.
aoqi@0 2809 if (next == class1()) return true;
aoqi@0 2810 if (next == outer1()) return true;
aoqi@0 2811 outer2 = instanceKlassHandle(THREAD, next);
aoqi@0 2812 }
aoqi@0 2813
aoqi@0 2814 // If by this point we have not found an equality between the
aoqi@0 2815 // two classes, we know they are in separate package members.
aoqi@0 2816 return false;
aoqi@0 2817 }
aoqi@0 2818
aoqi@0 2819
aoqi@0 2820 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
aoqi@0 2821 jint access = access_flags().as_int();
aoqi@0 2822
aoqi@0 2823 // But check if it happens to be member class.
aoqi@0 2824 instanceKlassHandle ik(THREAD, this);
aoqi@0 2825 InnerClassesIterator iter(ik);
aoqi@0 2826 for (; !iter.done(); iter.next()) {
aoqi@0 2827 int ioff = iter.inner_class_info_index();
aoqi@0 2828 // Inner class attribute can be zero, skip it.
aoqi@0 2829 // Strange but true: JVM spec. allows null inner class refs.
aoqi@0 2830 if (ioff == 0) continue;
aoqi@0 2831
aoqi@0 2832 // only look at classes that are already loaded
aoqi@0 2833 // since we are looking for the flags for our self.
aoqi@0 2834 Symbol* inner_name = ik->constants()->klass_name_at(ioff);
aoqi@0 2835 if ((ik->name() == inner_name)) {
aoqi@0 2836 // This is really a member class.
aoqi@0 2837 access = iter.inner_access_flags();
aoqi@0 2838 break;
aoqi@0 2839 }
aoqi@0 2840 }
aoqi@0 2841 // Remember to strip ACC_SUPER bit
aoqi@0 2842 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
aoqi@0 2843 }
aoqi@0 2844
aoqi@0 2845 jint InstanceKlass::jvmti_class_status() const {
aoqi@0 2846 jint result = 0;
aoqi@0 2847
aoqi@0 2848 if (is_linked()) {
aoqi@0 2849 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
aoqi@0 2850 }
aoqi@0 2851
aoqi@0 2852 if (is_initialized()) {
aoqi@0 2853 assert(is_linked(), "Class status is not consistent");
aoqi@0 2854 result |= JVMTI_CLASS_STATUS_INITIALIZED;
aoqi@0 2855 }
aoqi@0 2856 if (is_in_error_state()) {
aoqi@0 2857 result |= JVMTI_CLASS_STATUS_ERROR;
aoqi@0 2858 }
aoqi@0 2859 return result;
aoqi@0 2860 }
aoqi@0 2861
aoqi@0 2862 Method* InstanceKlass::method_at_itable(Klass* holder, int index, TRAPS) {
aoqi@0 2863 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
aoqi@0 2864 int method_table_offset_in_words = ioe->offset()/wordSize;
aoqi@0 2865 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
aoqi@0 2866 / itableOffsetEntry::size();
aoqi@0 2867
aoqi@0 2868 for (int cnt = 0 ; ; cnt ++, ioe ++) {
aoqi@0 2869 // If the interface isn't implemented by the receiver class,
aoqi@0 2870 // the VM should throw IncompatibleClassChangeError.
aoqi@0 2871 if (cnt >= nof_interfaces) {
aoqi@0 2872 THROW_NULL(vmSymbols::java_lang_IncompatibleClassChangeError());
aoqi@0 2873 }
aoqi@0 2874
aoqi@0 2875 Klass* ik = ioe->interface_klass();
aoqi@0 2876 if (ik == holder) break;
aoqi@0 2877 }
aoqi@0 2878
aoqi@0 2879 itableMethodEntry* ime = ioe->first_method_entry(this);
aoqi@0 2880 Method* m = ime[index].method();
aoqi@0 2881 if (m == NULL) {
aoqi@0 2882 THROW_NULL(vmSymbols::java_lang_AbstractMethodError());
aoqi@0 2883 }
aoqi@0 2884 return m;
aoqi@0 2885 }
aoqi@0 2886
aoqi@0 2887
aoqi@0 2888 #if INCLUDE_JVMTI
aoqi@0 2889 // update default_methods for redefineclasses for methods that are
aoqi@0 2890 // not yet in the vtable due to concurrent subclass define and superinterface
aoqi@0 2891 // redefinition
aoqi@0 2892 // Note: those in the vtable, should have been updated via adjust_method_entries
sspitsyn@7636 2893 void InstanceKlass::adjust_default_methods(InstanceKlass* holder, bool* trace_name_printed) {
aoqi@0 2894 // search the default_methods for uses of either obsolete or EMCP methods
aoqi@0 2895 if (default_methods() != NULL) {
sspitsyn@7636 2896 for (int index = 0; index < default_methods()->length(); index ++) {
sspitsyn@7636 2897 Method* old_method = default_methods()->at(index);
sspitsyn@7636 2898 if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {
sspitsyn@7636 2899 continue; // skip uninteresting entries
sspitsyn@7636 2900 }
sspitsyn@7636 2901 assert(!old_method->is_deleted(), "default methods may not be deleted");
sspitsyn@7636 2902
sspitsyn@7636 2903 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
sspitsyn@7636 2904
sspitsyn@7636 2905 assert(new_method != NULL, "method_with_idnum() should not be NULL");
sspitsyn@7636 2906 assert(old_method != new_method, "sanity check");
sspitsyn@7636 2907
sspitsyn@7636 2908 default_methods()->at_put(index, new_method);
sspitsyn@7636 2909 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
sspitsyn@7636 2910 if (!(*trace_name_printed)) {
sspitsyn@7636 2911 // RC_TRACE_MESG macro has an embedded ResourceMark
sspitsyn@7636 2912 RC_TRACE_MESG(("adjust: klassname=%s default methods from name=%s",
sspitsyn@7636 2913 external_name(),
sspitsyn@7636 2914 old_method->method_holder()->external_name()));
sspitsyn@7636 2915 *trace_name_printed = true;
aoqi@0 2916 }
sspitsyn@7636 2917 RC_TRACE(0x00100000, ("default method update: %s(%s) ",
sspitsyn@7636 2918 new_method->name()->as_C_string(),
sspitsyn@7636 2919 new_method->signature()->as_C_string()));
aoqi@0 2920 }
aoqi@0 2921 }
aoqi@0 2922 }
aoqi@0 2923 }
aoqi@0 2924 #endif // INCLUDE_JVMTI
aoqi@0 2925
aoqi@0 2926 // On-stack replacement stuff
aoqi@0 2927 void InstanceKlass::add_osr_nmethod(nmethod* n) {
aoqi@0 2928 // only one compilation can be active
aoqi@0 2929 NEEDS_CLEANUP
aoqi@0 2930 // This is a short non-blocking critical region, so the no safepoint check is ok.
aoqi@0 2931 OsrList_lock->lock_without_safepoint_check();
aoqi@0 2932 assert(n->is_osr_method(), "wrong kind of nmethod");
aoqi@0 2933 n->set_osr_link(osr_nmethods_head());
aoqi@0 2934 set_osr_nmethods_head(n);
aoqi@0 2935 // Raise the highest osr level if necessary
aoqi@0 2936 if (TieredCompilation) {
aoqi@0 2937 Method* m = n->method();
aoqi@0 2938 m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level()));
aoqi@0 2939 }
aoqi@0 2940 // Remember to unlock again
aoqi@0 2941 OsrList_lock->unlock();
aoqi@0 2942
aoqi@0 2943 // Get rid of the osr methods for the same bci that have lower levels.
aoqi@0 2944 if (TieredCompilation) {
aoqi@0 2945 for (int l = CompLevel_limited_profile; l < n->comp_level(); l++) {
aoqi@0 2946 nmethod *inv = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), l, true);
aoqi@0 2947 if (inv != NULL && inv->is_in_use()) {
aoqi@0 2948 inv->make_not_entrant();
aoqi@0 2949 }
aoqi@0 2950 }
aoqi@0 2951 }
aoqi@0 2952 }
aoqi@0 2953
aoqi@0 2954
aoqi@0 2955 void InstanceKlass::remove_osr_nmethod(nmethod* n) {
aoqi@0 2956 // This is a short non-blocking critical region, so the no safepoint check is ok.
aoqi@0 2957 OsrList_lock->lock_without_safepoint_check();
aoqi@0 2958 assert(n->is_osr_method(), "wrong kind of nmethod");
aoqi@0 2959 nmethod* last = NULL;
aoqi@0 2960 nmethod* cur = osr_nmethods_head();
aoqi@0 2961 int max_level = CompLevel_none; // Find the max comp level excluding n
aoqi@0 2962 Method* m = n->method();
aoqi@0 2963 // Search for match
aoqi@0 2964 while(cur != NULL && cur != n) {
aoqi@0 2965 if (TieredCompilation && m == cur->method()) {
aoqi@0 2966 // Find max level before n
aoqi@0 2967 max_level = MAX2(max_level, cur->comp_level());
aoqi@0 2968 }
aoqi@0 2969 last = cur;
aoqi@0 2970 cur = cur->osr_link();
aoqi@0 2971 }
aoqi@0 2972 nmethod* next = NULL;
aoqi@0 2973 if (cur == n) {
aoqi@0 2974 next = cur->osr_link();
aoqi@0 2975 if (last == NULL) {
aoqi@0 2976 // Remove first element
aoqi@0 2977 set_osr_nmethods_head(next);
aoqi@0 2978 } else {
aoqi@0 2979 last->set_osr_link(next);
aoqi@0 2980 }
aoqi@0 2981 }
aoqi@0 2982 n->set_osr_link(NULL);
aoqi@0 2983 if (TieredCompilation) {
aoqi@0 2984 cur = next;
aoqi@0 2985 while (cur != NULL) {
aoqi@0 2986 // Find max level after n
aoqi@0 2987 if (m == cur->method()) {
aoqi@0 2988 max_level = MAX2(max_level, cur->comp_level());
aoqi@0 2989 }
aoqi@0 2990 cur = cur->osr_link();
aoqi@0 2991 }
aoqi@0 2992 m->set_highest_osr_comp_level(max_level);
aoqi@0 2993 }
aoqi@0 2994 // Remember to unlock again
aoqi@0 2995 OsrList_lock->unlock();
aoqi@0 2996 }
aoqi@0 2997
thartmann@7325 2998 int InstanceKlass::mark_osr_nmethods(const Method* m) {
thartmann@7325 2999 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@7325 3000 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
thartmann@7325 3001 nmethod* osr = osr_nmethods_head();
thartmann@7325 3002 int found = 0;
thartmann@7325 3003 while (osr != NULL) {
thartmann@7325 3004 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
thartmann@7325 3005 if (osr->method() == m) {
thartmann@7325 3006 osr->mark_for_deoptimization();
thartmann@7325 3007 found++;
thartmann@7325 3008 }
thartmann@7325 3009 osr = osr->osr_link();
thartmann@7325 3010 }
thartmann@7325 3011 return found;
thartmann@7325 3012 }
thartmann@7325 3013
aoqi@0 3014 nmethod* InstanceKlass::lookup_osr_nmethod(const Method* m, int bci, int comp_level, bool match_level) const {
aoqi@0 3015 // This is a short non-blocking critical region, so the no safepoint check is ok.
aoqi@0 3016 OsrList_lock->lock_without_safepoint_check();
aoqi@0 3017 nmethod* osr = osr_nmethods_head();
aoqi@0 3018 nmethod* best = NULL;
aoqi@0 3019 while (osr != NULL) {
aoqi@0 3020 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
aoqi@0 3021 // There can be a time when a c1 osr method exists but we are waiting
aoqi@0 3022 // for a c2 version. When c2 completes its osr nmethod we will trash
aoqi@0 3023 // the c1 version and only be able to find the c2 version. However
aoqi@0 3024 // while we overflow in the c1 code at back branches we don't want to
aoqi@0 3025 // try and switch to the same code as we are already running
aoqi@0 3026
aoqi@0 3027 if (osr->method() == m &&
aoqi@0 3028 (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) {
aoqi@0 3029 if (match_level) {
aoqi@0 3030 if (osr->comp_level() == comp_level) {
aoqi@0 3031 // Found a match - return it.
aoqi@0 3032 OsrList_lock->unlock();
aoqi@0 3033 return osr;
aoqi@0 3034 }
aoqi@0 3035 } else {
aoqi@0 3036 if (best == NULL || (osr->comp_level() > best->comp_level())) {
aoqi@0 3037 if (osr->comp_level() == CompLevel_highest_tier) {
aoqi@0 3038 // Found the best possible - return it.
aoqi@0 3039 OsrList_lock->unlock();
aoqi@0 3040 return osr;
aoqi@0 3041 }
aoqi@0 3042 best = osr;
aoqi@0 3043 }
aoqi@0 3044 }
aoqi@0 3045 }
aoqi@0 3046 osr = osr->osr_link();
aoqi@0 3047 }
aoqi@0 3048 OsrList_lock->unlock();
aoqi@0 3049 if (best != NULL && best->comp_level() >= comp_level && match_level == false) {
aoqi@0 3050 return best;
aoqi@0 3051 }
aoqi@0 3052 return NULL;
aoqi@0 3053 }
aoqi@0 3054
kevinw@8721 3055 oop InstanceKlass::add_member_name(Handle mem_name, bool intern) {
aoqi@0 3056 jweak mem_name_wref = JNIHandles::make_weak_global(mem_name);
aoqi@0 3057 MutexLocker ml(MemberNameTable_lock);
aoqi@0 3058 DEBUG_ONLY(No_Safepoint_Verifier nsv);
aoqi@0 3059
coleenp@7391 3060 // Check if method has been redefined while taking out MemberNameTable_lock, if so
coleenp@7391 3061 // return false. We cannot cache obsolete methods. They will crash when the function
coleenp@7391 3062 // is called!
coleenp@7391 3063 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mem_name());
coleenp@7391 3064 if (method->is_obsolete()) {
kevinw@8721 3065 return NULL;
coleenp@7391 3066 } else if (method->is_old()) {
coleenp@7391 3067 // Replace method with redefined version
coleenp@7391 3068 java_lang_invoke_MemberName::set_vmtarget(mem_name(), method_with_idnum(method->method_idnum()));
coleenp@7391 3069 }
coleenp@7391 3070
aoqi@0 3071 if (_member_names == NULL) {
aoqi@0 3072 _member_names = new (ResourceObj::C_HEAP, mtClass) MemberNameTable(idnum_allocated_count());
aoqi@0 3073 }
kevinw@8721 3074 if (intern) {
kevinw@8721 3075 return _member_names->find_or_add_member_name(mem_name_wref);
kevinw@8721 3076 } else {
kevinw@8721 3077 return _member_names->add_member_name(mem_name_wref);
kevinw@8721 3078 }
aoqi@0 3079 }
aoqi@0 3080
aoqi@0 3081 // -----------------------------------------------------------------------------------------------------
aoqi@0 3082 // Printing
aoqi@0 3083
aoqi@0 3084 #ifndef PRODUCT
aoqi@0 3085
aoqi@0 3086 #define BULLET " - "
aoqi@0 3087
aoqi@0 3088 static const char* state_names[] = {
aoqi@0 3089 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
aoqi@0 3090 };
aoqi@0 3091
aoqi@0 3092 static void print_vtable(intptr_t* start, int len, outputStream* st) {
aoqi@0 3093 for (int i = 0; i < len; i++) {
aoqi@0 3094 intptr_t e = start[i];
aoqi@0 3095 st->print("%d : " INTPTR_FORMAT, i, e);
aoqi@0 3096 if (e != 0 && ((Metadata*)e)->is_metaspace_object()) {
aoqi@0 3097 st->print(" ");
aoqi@0 3098 ((Metadata*)e)->print_value_on(st);
aoqi@0 3099 }
aoqi@0 3100 st->cr();
aoqi@0 3101 }
aoqi@0 3102 }
aoqi@0 3103
aoqi@0 3104 void InstanceKlass::print_on(outputStream* st) const {
aoqi@0 3105 assert(is_klass(), "must be klass");
aoqi@0 3106 Klass::print_on(st);
aoqi@0 3107
aoqi@0 3108 st->print(BULLET"instance size: %d", size_helper()); st->cr();
aoqi@0 3109 st->print(BULLET"klass size: %d", size()); st->cr();
aoqi@0 3110 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
aoqi@0 3111 st->print(BULLET"state: "); st->print_cr("%s", state_names[_init_state]);
aoqi@0 3112 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
aoqi@0 3113 st->print(BULLET"super: "); super()->print_value_on_maybe_null(st); st->cr();
aoqi@0 3114 st->print(BULLET"sub: ");
aoqi@0 3115 Klass* sub = subklass();
aoqi@0 3116 int n;
aoqi@0 3117 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
aoqi@0 3118 if (n < MaxSubklassPrintSize) {
aoqi@0 3119 sub->print_value_on(st);
aoqi@0 3120 st->print(" ");
aoqi@0 3121 }
aoqi@0 3122 }
aoqi@0 3123 if (n >= MaxSubklassPrintSize) st->print("(%d more klasses...)", n - MaxSubklassPrintSize);
aoqi@0 3124 st->cr();
aoqi@0 3125
aoqi@0 3126 if (is_interface()) {
aoqi@0 3127 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
aoqi@0 3128 if (nof_implementors() == 1) {
aoqi@0 3129 st->print_cr(BULLET"implementor: ");
aoqi@0 3130 st->print(" ");
aoqi@0 3131 implementor()->print_value_on(st);
aoqi@0 3132 st->cr();
aoqi@0 3133 }
aoqi@0 3134 }
aoqi@0 3135
aoqi@0 3136 st->print(BULLET"arrays: "); array_klasses()->print_value_on_maybe_null(st); st->cr();
aoqi@0 3137 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
aoqi@0 3138 if (Verbose || WizardMode) {
aoqi@0 3139 Array<Method*>* method_array = methods();
aoqi@0 3140 for (int i = 0; i < method_array->length(); i++) {
aoqi@0 3141 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
aoqi@0 3142 }
aoqi@0 3143 }
aoqi@0 3144 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
aoqi@0 3145 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
aoqi@0 3146 if (Verbose && default_methods() != NULL) {
aoqi@0 3147 Array<Method*>* method_array = default_methods();
aoqi@0 3148 for (int i = 0; i < method_array->length(); i++) {
aoqi@0 3149 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
aoqi@0 3150 }
aoqi@0 3151 }
aoqi@0 3152 if (default_vtable_indices() != NULL) {
aoqi@0 3153 st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr();
aoqi@0 3154 }
aoqi@0 3155 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
aoqi@0 3156 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
aoqi@0 3157 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
aoqi@0 3158 if (class_loader_data() != NULL) {
aoqi@0 3159 st->print(BULLET"class loader data: ");
aoqi@0 3160 class_loader_data()->print_value_on(st);
aoqi@0 3161 st->cr();
aoqi@0 3162 }
aoqi@0 3163 st->print(BULLET"host class: "); host_klass()->print_value_on_maybe_null(st); st->cr();
aoqi@0 3164 if (source_file_name() != NULL) {
aoqi@0 3165 st->print(BULLET"source file: ");
aoqi@0 3166 source_file_name()->print_value_on(st);
aoqi@0 3167 st->cr();
aoqi@0 3168 }
aoqi@0 3169 if (source_debug_extension() != NULL) {
aoqi@0 3170 st->print(BULLET"source debug extension: ");
aoqi@0 3171 st->print("%s", source_debug_extension());
aoqi@0 3172 st->cr();
aoqi@0 3173 }
aoqi@0 3174 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
aoqi@0 3175 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
aoqi@0 3176 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
aoqi@0 3177 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
aoqi@0 3178 {
aoqi@0 3179 bool have_pv = false;
kevinw@9184 3180 // previous versions are linked together through the InstanceKlass
kevinw@9184 3181 for (InstanceKlass* pv_node = _previous_versions;
kevinw@9184 3182 pv_node != NULL;
kevinw@9184 3183 pv_node = pv_node->previous_versions()) {
aoqi@0 3184 if (!have_pv)
aoqi@0 3185 st->print(BULLET"previous version: ");
aoqi@0 3186 have_pv = true;
kevinw@9184 3187 pv_node->constants()->print_value_on(st);
aoqi@0 3188 }
aoqi@0 3189 if (have_pv) st->cr();
kevinw@9184 3190 }
aoqi@0 3191
aoqi@0 3192 if (generic_signature() != NULL) {
aoqi@0 3193 st->print(BULLET"generic signature: ");
aoqi@0 3194 generic_signature()->print_value_on(st);
aoqi@0 3195 st->cr();
aoqi@0 3196 }
aoqi@0 3197 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
aoqi@0 3198 st->print(BULLET"java mirror: "); java_mirror()->print_value_on(st); st->cr();
aoqi@0 3199 st->print(BULLET"vtable length %d (start addr: " INTPTR_FORMAT ")", vtable_length(), start_of_vtable()); st->cr();
aoqi@0 3200 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
aoqi@0 3201 st->print(BULLET"itable length %d (start addr: " INTPTR_FORMAT ")", itable_length(), start_of_itable()); st->cr();
aoqi@0 3202 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
aoqi@0 3203 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
aoqi@0 3204 FieldPrinter print_static_field(st);
aoqi@0 3205 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
aoqi@0 3206 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
aoqi@0 3207 FieldPrinter print_nonstatic_field(st);
aoqi@0 3208 ((InstanceKlass*)this)->do_nonstatic_fields(&print_nonstatic_field);
aoqi@0 3209
aoqi@0 3210 st->print(BULLET"non-static oop maps: ");
aoqi@0 3211 OopMapBlock* map = start_of_nonstatic_oop_maps();
aoqi@0 3212 OopMapBlock* end_map = map + nonstatic_oop_map_count();
aoqi@0 3213 while (map < end_map) {
aoqi@0 3214 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
aoqi@0 3215 map++;
aoqi@0 3216 }
aoqi@0 3217 st->cr();
aoqi@0 3218 }
aoqi@0 3219
aoqi@0 3220 #endif //PRODUCT
aoqi@0 3221
aoqi@0 3222 void InstanceKlass::print_value_on(outputStream* st) const {
aoqi@0 3223 assert(is_klass(), "must be klass");
aoqi@0 3224 if (Verbose || WizardMode) access_flags().print_on(st);
aoqi@0 3225 name()->print_value_on(st);
aoqi@0 3226 }
aoqi@0 3227
aoqi@0 3228 #ifndef PRODUCT
aoqi@0 3229
aoqi@0 3230 void FieldPrinter::do_field(fieldDescriptor* fd) {
aoqi@0 3231 _st->print(BULLET);
aoqi@0 3232 if (_obj == NULL) {
aoqi@0 3233 fd->print_on(_st);
aoqi@0 3234 _st->cr();
aoqi@0 3235 } else {
aoqi@0 3236 fd->print_on_for(_st, _obj);
aoqi@0 3237 _st->cr();
aoqi@0 3238 }
aoqi@0 3239 }
aoqi@0 3240
aoqi@0 3241
aoqi@0 3242 void InstanceKlass::oop_print_on(oop obj, outputStream* st) {
aoqi@0 3243 Klass::oop_print_on(obj, st);
aoqi@0 3244
aoqi@0 3245 if (this == SystemDictionary::String_klass()) {
aoqi@0 3246 typeArrayOop value = java_lang_String::value(obj);
aoqi@0 3247 juint offset = java_lang_String::offset(obj);
aoqi@0 3248 juint length = java_lang_String::length(obj);
aoqi@0 3249 if (value != NULL &&
aoqi@0 3250 value->is_typeArray() &&
aoqi@0 3251 offset <= (juint) value->length() &&
aoqi@0 3252 offset + length <= (juint) value->length()) {
aoqi@0 3253 st->print(BULLET"string: ");
stefank@6975 3254 java_lang_String::print(obj, st);
aoqi@0 3255 st->cr();
aoqi@0 3256 if (!WizardMode) return; // that is enough
aoqi@0 3257 }
aoqi@0 3258 }
aoqi@0 3259
aoqi@0 3260 st->print_cr(BULLET"---- fields (total size %d words):", oop_size(obj));
aoqi@0 3261 FieldPrinter print_field(st, obj);
aoqi@0 3262 do_nonstatic_fields(&print_field);
aoqi@0 3263
aoqi@0 3264 if (this == SystemDictionary::Class_klass()) {
aoqi@0 3265 st->print(BULLET"signature: ");
aoqi@0 3266 java_lang_Class::print_signature(obj, st);
aoqi@0 3267 st->cr();
aoqi@0 3268 Klass* mirrored_klass = java_lang_Class::as_Klass(obj);
aoqi@0 3269 st->print(BULLET"fake entry for mirror: ");
aoqi@0 3270 mirrored_klass->print_value_on_maybe_null(st);
aoqi@0 3271 st->cr();
aoqi@0 3272 Klass* array_klass = java_lang_Class::array_klass(obj);
aoqi@0 3273 st->print(BULLET"fake entry for array: ");
aoqi@0 3274 array_klass->print_value_on_maybe_null(st);
aoqi@0 3275 st->cr();
aoqi@0 3276 st->print_cr(BULLET"fake entry for oop_size: %d", java_lang_Class::oop_size(obj));
aoqi@0 3277 st->print_cr(BULLET"fake entry for static_oop_field_count: %d", java_lang_Class::static_oop_field_count(obj));
aoqi@0 3278 Klass* real_klass = java_lang_Class::as_Klass(obj);
aoqi@0 3279 if (real_klass != NULL && real_klass->oop_is_instance()) {
aoqi@0 3280 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
aoqi@0 3281 }
aoqi@0 3282 } else if (this == SystemDictionary::MethodType_klass()) {
aoqi@0 3283 st->print(BULLET"signature: ");
aoqi@0 3284 java_lang_invoke_MethodType::print_signature(obj, st);
aoqi@0 3285 st->cr();
aoqi@0 3286 }
aoqi@0 3287 }
aoqi@0 3288
aoqi@0 3289 #endif //PRODUCT
aoqi@0 3290
aoqi@0 3291 void InstanceKlass::oop_print_value_on(oop obj, outputStream* st) {
aoqi@0 3292 st->print("a ");
aoqi@0 3293 name()->print_value_on(st);
aoqi@0 3294 obj->print_address_on(st);
aoqi@0 3295 if (this == SystemDictionary::String_klass()
aoqi@0 3296 && java_lang_String::value(obj) != NULL) {
aoqi@0 3297 ResourceMark rm;
aoqi@0 3298 int len = java_lang_String::length(obj);
aoqi@0 3299 int plen = (len < 24 ? len : 12);
aoqi@0 3300 char* str = java_lang_String::as_utf8_string(obj, 0, plen);
aoqi@0 3301 st->print(" = \"%s\"", str);
aoqi@0 3302 if (len > plen)
aoqi@0 3303 st->print("...[%d]", len);
aoqi@0 3304 } else if (this == SystemDictionary::Class_klass()) {
aoqi@0 3305 Klass* k = java_lang_Class::as_Klass(obj);
aoqi@0 3306 st->print(" = ");
aoqi@0 3307 if (k != NULL) {
aoqi@0 3308 k->print_value_on(st);
aoqi@0 3309 } else {
aoqi@0 3310 const char* tname = type2name(java_lang_Class::primitive_type(obj));
aoqi@0 3311 st->print("%s", tname ? tname : "type?");
aoqi@0 3312 }
aoqi@0 3313 } else if (this == SystemDictionary::MethodType_klass()) {
aoqi@0 3314 st->print(" = ");
aoqi@0 3315 java_lang_invoke_MethodType::print_signature(obj, st);
aoqi@0 3316 } else if (java_lang_boxing_object::is_instance(obj)) {
aoqi@0 3317 st->print(" = ");
aoqi@0 3318 java_lang_boxing_object::print(obj, st);
aoqi@0 3319 } else if (this == SystemDictionary::LambdaForm_klass()) {
aoqi@0 3320 oop vmentry = java_lang_invoke_LambdaForm::vmentry(obj);
aoqi@0 3321 if (vmentry != NULL) {
aoqi@0 3322 st->print(" => ");
aoqi@0 3323 vmentry->print_value_on(st);
aoqi@0 3324 }
aoqi@0 3325 } else if (this == SystemDictionary::MemberName_klass()) {
aoqi@0 3326 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(obj);
aoqi@0 3327 if (vmtarget != NULL) {
aoqi@0 3328 st->print(" = ");
aoqi@0 3329 vmtarget->print_value_on(st);
aoqi@0 3330 } else {
aoqi@0 3331 java_lang_invoke_MemberName::clazz(obj)->print_value_on(st);
aoqi@0 3332 st->print(".");
aoqi@0 3333 java_lang_invoke_MemberName::name(obj)->print_value_on(st);
aoqi@0 3334 }
aoqi@0 3335 }
aoqi@0 3336 }
aoqi@0 3337
aoqi@0 3338 const char* InstanceKlass::internal_name() const {
aoqi@0 3339 return external_name();
aoqi@0 3340 }
aoqi@0 3341
aoqi@0 3342 #if INCLUDE_SERVICES
aoqi@0 3343 // Size Statistics
aoqi@0 3344 void InstanceKlass::collect_statistics(KlassSizeStats *sz) const {
aoqi@0 3345 Klass::collect_statistics(sz);
aoqi@0 3346
aoqi@0 3347 sz->_inst_size = HeapWordSize * size_helper();
aoqi@0 3348 sz->_vtab_bytes = HeapWordSize * align_object_offset(vtable_length());
aoqi@0 3349 sz->_itab_bytes = HeapWordSize * align_object_offset(itable_length());
aoqi@0 3350 sz->_nonstatic_oopmap_bytes = HeapWordSize *
aoqi@0 3351 ((is_interface() || is_anonymous()) ?
aoqi@0 3352 align_object_offset(nonstatic_oop_map_size()) :
aoqi@0 3353 nonstatic_oop_map_size());
aoqi@0 3354
aoqi@0 3355 int n = 0;
aoqi@0 3356 n += (sz->_methods_array_bytes = sz->count_array(methods()));
aoqi@0 3357 n += (sz->_method_ordering_bytes = sz->count_array(method_ordering()));
aoqi@0 3358 n += (sz->_local_interfaces_bytes = sz->count_array(local_interfaces()));
aoqi@0 3359 n += (sz->_transitive_interfaces_bytes = sz->count_array(transitive_interfaces()));
aoqi@0 3360 n += (sz->_fields_bytes = sz->count_array(fields()));
aoqi@0 3361 n += (sz->_inner_classes_bytes = sz->count_array(inner_classes()));
aoqi@0 3362 sz->_ro_bytes += n;
aoqi@0 3363
aoqi@0 3364 const ConstantPool* cp = constants();
aoqi@0 3365 if (cp) {
aoqi@0 3366 cp->collect_statistics(sz);
aoqi@0 3367 }
aoqi@0 3368
aoqi@0 3369 const Annotations* anno = annotations();
aoqi@0 3370 if (anno) {
aoqi@0 3371 anno->collect_statistics(sz);
aoqi@0 3372 }
aoqi@0 3373
aoqi@0 3374 const Array<Method*>* methods_array = methods();
aoqi@0 3375 if (methods()) {
aoqi@0 3376 for (int i = 0; i < methods_array->length(); i++) {
aoqi@0 3377 Method* method = methods_array->at(i);
aoqi@0 3378 if (method) {
aoqi@0 3379 sz->_method_count ++;
aoqi@0 3380 method->collect_statistics(sz);
aoqi@0 3381 }
aoqi@0 3382 }
aoqi@0 3383 }
aoqi@0 3384 }
aoqi@0 3385 #endif // INCLUDE_SERVICES
aoqi@0 3386
aoqi@0 3387 // Verification
aoqi@0 3388
aoqi@0 3389 class VerifyFieldClosure: public OopClosure {
aoqi@0 3390 protected:
aoqi@0 3391 template <class T> void do_oop_work(T* p) {
aoqi@0 3392 oop obj = oopDesc::load_decode_heap_oop(p);
aoqi@0 3393 if (!obj->is_oop_or_null()) {
aoqi@0 3394 tty->print_cr("Failed: " PTR_FORMAT " -> " PTR_FORMAT, p, (address)obj);
aoqi@0 3395 Universe::print();
aoqi@0 3396 guarantee(false, "boom");
aoqi@0 3397 }
aoqi@0 3398 }
aoqi@0 3399 public:
aoqi@0 3400 virtual void do_oop(oop* p) { VerifyFieldClosure::do_oop_work(p); }
aoqi@0 3401 virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); }
aoqi@0 3402 };
aoqi@0 3403
aoqi@0 3404 void InstanceKlass::verify_on(outputStream* st) {
aoqi@0 3405 #ifndef PRODUCT
aoqi@0 3406 // Avoid redundant verifies, this really should be in product.
aoqi@0 3407 if (_verify_count == Universe::verify_count()) return;
aoqi@0 3408 _verify_count = Universe::verify_count();
aoqi@0 3409 #endif
aoqi@0 3410
aoqi@0 3411 // Verify Klass
aoqi@0 3412 Klass::verify_on(st);
aoqi@0 3413
aoqi@0 3414 // Verify that klass is present in ClassLoaderData
aoqi@0 3415 guarantee(class_loader_data()->contains_klass(this),
aoqi@0 3416 "this class isn't found in class loader data");
aoqi@0 3417
aoqi@0 3418 // Verify vtables
aoqi@0 3419 if (is_linked()) {
aoqi@0 3420 ResourceMark rm;
aoqi@0 3421 // $$$ This used to be done only for m/s collections. Doing it
aoqi@0 3422 // always seemed a valid generalization. (DLD -- 6/00)
aoqi@0 3423 vtable()->verify(st);
aoqi@0 3424 }
aoqi@0 3425
aoqi@0 3426 // Verify first subklass
aoqi@0 3427 if (subklass_oop() != NULL) {
aoqi@0 3428 guarantee(subklass_oop()->is_klass(), "should be klass");
aoqi@0 3429 }
aoqi@0 3430
aoqi@0 3431 // Verify siblings
aoqi@0 3432 Klass* super = this->super();
aoqi@0 3433 Klass* sib = next_sibling();
aoqi@0 3434 if (sib != NULL) {
aoqi@0 3435 if (sib == this) {
aoqi@0 3436 fatal(err_msg("subclass points to itself " PTR_FORMAT, sib));
aoqi@0 3437 }
aoqi@0 3438
aoqi@0 3439 guarantee(sib->is_klass(), "should be klass");
aoqi@0 3440 guarantee(sib->super() == super, "siblings should have same superklass");
aoqi@0 3441 }
aoqi@0 3442
aoqi@0 3443 // Verify implementor fields
aoqi@0 3444 Klass* im = implementor();
aoqi@0 3445 if (im != NULL) {
aoqi@0 3446 guarantee(is_interface(), "only interfaces should have implementor set");
aoqi@0 3447 guarantee(im->is_klass(), "should be klass");
aoqi@0 3448 guarantee(!im->is_interface() || im == this,
aoqi@0 3449 "implementors cannot be interfaces");
aoqi@0 3450 }
aoqi@0 3451
aoqi@0 3452 // Verify local interfaces
aoqi@0 3453 if (local_interfaces()) {
aoqi@0 3454 Array<Klass*>* local_interfaces = this->local_interfaces();
aoqi@0 3455 for (int j = 0; j < local_interfaces->length(); j++) {
aoqi@0 3456 Klass* e = local_interfaces->at(j);
aoqi@0 3457 guarantee(e->is_klass() && e->is_interface(), "invalid local interface");
aoqi@0 3458 }
aoqi@0 3459 }
aoqi@0 3460
aoqi@0 3461 // Verify transitive interfaces
aoqi@0 3462 if (transitive_interfaces() != NULL) {
aoqi@0 3463 Array<Klass*>* transitive_interfaces = this->transitive_interfaces();
aoqi@0 3464 for (int j = 0; j < transitive_interfaces->length(); j++) {
aoqi@0 3465 Klass* e = transitive_interfaces->at(j);
aoqi@0 3466 guarantee(e->is_klass() && e->is_interface(), "invalid transitive interface");
aoqi@0 3467 }
aoqi@0 3468 }
aoqi@0 3469
aoqi@0 3470 // Verify methods
aoqi@0 3471 if (methods() != NULL) {
aoqi@0 3472 Array<Method*>* methods = this->methods();
aoqi@0 3473 for (int j = 0; j < methods->length(); j++) {
aoqi@0 3474 guarantee(methods->at(j)->is_method(), "non-method in methods array");
aoqi@0 3475 }
aoqi@0 3476 for (int j = 0; j < methods->length() - 1; j++) {
aoqi@0 3477 Method* m1 = methods->at(j);
aoqi@0 3478 Method* m2 = methods->at(j + 1);
aoqi@0 3479 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly");
aoqi@0 3480 }
aoqi@0 3481 }
aoqi@0 3482
aoqi@0 3483 // Verify method ordering
aoqi@0 3484 if (method_ordering() != NULL) {
aoqi@0 3485 Array<int>* method_ordering = this->method_ordering();
aoqi@0 3486 int length = method_ordering->length();
aoqi@0 3487 if (JvmtiExport::can_maintain_original_method_order() ||
aoqi@0 3488 ((UseSharedSpaces || DumpSharedSpaces) && length != 0)) {
aoqi@0 3489 guarantee(length == methods()->length(), "invalid method ordering length");
aoqi@0 3490 jlong sum = 0;
aoqi@0 3491 for (int j = 0; j < length; j++) {
aoqi@0 3492 int original_index = method_ordering->at(j);
aoqi@0 3493 guarantee(original_index >= 0, "invalid method ordering index");
aoqi@0 3494 guarantee(original_index < length, "invalid method ordering index");
aoqi@0 3495 sum += original_index;
aoqi@0 3496 }
aoqi@0 3497 // Verify sum of indices 0,1,...,length-1
aoqi@0 3498 guarantee(sum == ((jlong)length*(length-1))/2, "invalid method ordering sum");
aoqi@0 3499 } else {
aoqi@0 3500 guarantee(length == 0, "invalid method ordering length");
aoqi@0 3501 }
aoqi@0 3502 }
aoqi@0 3503
aoqi@0 3504 // Verify default methods
aoqi@0 3505 if (default_methods() != NULL) {
aoqi@0 3506 Array<Method*>* methods = this->default_methods();
aoqi@0 3507 for (int j = 0; j < methods->length(); j++) {
aoqi@0 3508 guarantee(methods->at(j)->is_method(), "non-method in methods array");
aoqi@0 3509 }
aoqi@0 3510 for (int j = 0; j < methods->length() - 1; j++) {
aoqi@0 3511 Method* m1 = methods->at(j);
aoqi@0 3512 Method* m2 = methods->at(j + 1);
aoqi@0 3513 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly");
aoqi@0 3514 }
aoqi@0 3515 }
aoqi@0 3516
aoqi@0 3517 // Verify JNI static field identifiers
aoqi@0 3518 if (jni_ids() != NULL) {
aoqi@0 3519 jni_ids()->verify(this);
aoqi@0 3520 }
aoqi@0 3521
aoqi@0 3522 // Verify other fields
aoqi@0 3523 if (array_klasses() != NULL) {
aoqi@0 3524 guarantee(array_klasses()->is_klass(), "should be klass");
aoqi@0 3525 }
aoqi@0 3526 if (constants() != NULL) {
aoqi@0 3527 guarantee(constants()->is_constantPool(), "should be constant pool");
aoqi@0 3528 }
aoqi@0 3529 const Klass* host = host_klass();
aoqi@0 3530 if (host != NULL) {
aoqi@0 3531 guarantee(host->is_klass(), "should be klass");
aoqi@0 3532 }
aoqi@0 3533 }
aoqi@0 3534
aoqi@0 3535 void InstanceKlass::oop_verify_on(oop obj, outputStream* st) {
aoqi@0 3536 Klass::oop_verify_on(obj, st);
aoqi@0 3537 VerifyFieldClosure blk;
aoqi@0 3538 obj->oop_iterate_no_header(&blk);
aoqi@0 3539 }
aoqi@0 3540
aoqi@0 3541
aoqi@0 3542 // JNIid class for jfieldIDs only
aoqi@0 3543 // Note to reviewers:
aoqi@0 3544 // These JNI functions are just moved over to column 1 and not changed
aoqi@0 3545 // in the compressed oops workspace.
aoqi@0 3546 JNIid::JNIid(Klass* holder, int offset, JNIid* next) {
aoqi@0 3547 _holder = holder;
aoqi@0 3548 _offset = offset;
aoqi@0 3549 _next = next;
aoqi@0 3550 debug_only(_is_static_field_id = false;)
aoqi@0 3551 }
aoqi@0 3552
aoqi@0 3553
aoqi@0 3554 JNIid* JNIid::find(int offset) {
aoqi@0 3555 JNIid* current = this;
aoqi@0 3556 while (current != NULL) {
aoqi@0 3557 if (current->offset() == offset) return current;
aoqi@0 3558 current = current->next();
aoqi@0 3559 }
aoqi@0 3560 return NULL;
aoqi@0 3561 }
aoqi@0 3562
aoqi@0 3563 void JNIid::deallocate(JNIid* current) {
aoqi@0 3564 while (current != NULL) {
aoqi@0 3565 JNIid* next = current->next();
aoqi@0 3566 delete current;
aoqi@0 3567 current = next;
aoqi@0 3568 }
aoqi@0 3569 }
aoqi@0 3570
aoqi@0 3571
aoqi@0 3572 void JNIid::verify(Klass* holder) {
aoqi@0 3573 int first_field_offset = InstanceMirrorKlass::offset_of_static_fields();
aoqi@0 3574 int end_field_offset;
aoqi@0 3575 end_field_offset = first_field_offset + (InstanceKlass::cast(holder)->static_field_size() * wordSize);
aoqi@0 3576
aoqi@0 3577 JNIid* current = this;
aoqi@0 3578 while (current != NULL) {
aoqi@0 3579 guarantee(current->holder() == holder, "Invalid klass in JNIid");
aoqi@0 3580 #ifdef ASSERT
aoqi@0 3581 int o = current->offset();
aoqi@0 3582 if (current->is_static_field_id()) {
aoqi@0 3583 guarantee(o >= first_field_offset && o < end_field_offset, "Invalid static field offset in JNIid");
aoqi@0 3584 }
aoqi@0 3585 #endif
aoqi@0 3586 current = current->next();
aoqi@0 3587 }
aoqi@0 3588 }
aoqi@0 3589
aoqi@0 3590
aoqi@0 3591 #ifdef ASSERT
aoqi@0 3592 void InstanceKlass::set_init_state(ClassState state) {
aoqi@0 3593 bool good_state = is_shared() ? (_init_state <= state)
aoqi@0 3594 : (_init_state < state);
aoqi@0 3595 assert(good_state || state == allocated, "illegal state transition");
aoqi@0 3596 _init_state = (u1)state;
aoqi@0 3597 }
aoqi@0 3598 #endif
aoqi@0 3599
aoqi@0 3600
aoqi@0 3601 // RedefineClasses() support for previous versions:
aoqi@0 3602
aoqi@0 3603 // Purge previous versions
kevinw@9184 3604 void InstanceKlass::purge_previous_versions(InstanceKlass* ik) {
aoqi@0 3605 if (ik->previous_versions() != NULL) {
aoqi@0 3606 // This klass has previous versions so see what we can cleanup
aoqi@0 3607 // while it is safe to do so.
aoqi@0 3608
aoqi@0 3609 int deleted_count = 0; // leave debugging breadcrumbs
aoqi@0 3610 int live_count = 0;
kevinw@9184 3611 ClassLoaderData* loader_data = ik->class_loader_data();
kevinw@9184 3612 assert(loader_data != NULL, "should never be null");
aoqi@0 3613
aoqi@0 3614 // RC_TRACE macro has an embedded ResourceMark
kevinw@9184 3615 RC_TRACE(0x00000200, ("purge: %s: previous versions", ik->external_name()));
kevinw@9184 3616
kevinw@9184 3617 // previous versions are linked together through the InstanceKlass
kevinw@9184 3618 InstanceKlass* pv_node = ik->previous_versions();
kevinw@9184 3619 InstanceKlass* last = ik;
kevinw@9184 3620 int version = 0;
kevinw@9184 3621
kevinw@9184 3622 // check the previous versions list
kevinw@9184 3623 for (; pv_node != NULL; ) {
kevinw@9184 3624
kevinw@9184 3625 ConstantPool* pvcp = pv_node->constants();
kevinw@9184 3626 assert(pvcp != NULL, "cp ref was unexpectedly cleared");
kevinw@9184 3627
kevinw@9184 3628
aoqi@0 3629 if (!pvcp->on_stack()) {
aoqi@0 3630 // If the constant pool isn't on stack, none of the methods
kevinw@9184 3631 // are executing. Unlink this previous_version.
kevinw@9184 3632 // The previous version InstanceKlass is on the ClassLoaderData deallocate list
kevinw@9184 3633 // so will be deallocated during the next phase of class unloading.
kevinw@9184 3634 pv_node = pv_node->previous_versions();
kevinw@9184 3635 last->link_previous_versions(pv_node);
aoqi@0 3636 deleted_count++;
kevinw@9184 3637 version++;
aoqi@0 3638 continue;
aoqi@0 3639 } else {
kevinw@9184 3640 RC_TRACE(0x00000200, ("purge: previous version " INTPTR_FORMAT " is alive",
kevinw@9184 3641 pv_node));
aoqi@0 3642 assert(pvcp->pool_holder() != NULL, "Constant pool with no holder");
aoqi@0 3643 guarantee (!loader_data->is_unloading(), "unloaded classes can't be on the stack");
aoqi@0 3644 live_count++;
aoqi@0 3645 }
aoqi@0 3646
kevinw@9184 3647 // At least one method is live in this previous version so clean its MethodData.
kevinw@9184 3648 // Reset dead EMCP methods not to get breakpoints.
kevinw@9184 3649 // All methods are deallocated when all of the methods for this class are no
kevinw@9184 3650 // longer running.
kevinw@9184 3651 Array<Method*>* method_refs = pv_node->methods();
aoqi@0 3652 if (method_refs != NULL) {
aoqi@0 3653 RC_TRACE(0x00000200, ("purge: previous methods length=%d",
aoqi@0 3654 method_refs->length()));
kevinw@9184 3655 for (int j = 0; j < method_refs->length(); j++) {
aoqi@0 3656 Method* method = method_refs->at(j);
kevinw@9184 3657
aoqi@0 3658 if (!method->on_stack()) {
kevinw@9184 3659 // no breakpoints for non-running methods
kevinw@9184 3660 if (method->is_running_emcp()) {
kevinw@9184 3661 method->set_running_emcp(false);
kevinw@9184 3662 }
aoqi@0 3663 } else {
kevinw@9184 3664 assert (method->is_obsolete() || method->is_running_emcp(),
kevinw@9184 3665 "emcp method cannot run after emcp bit is cleared");
aoqi@0 3666 // RC_TRACE macro has an embedded ResourceMark
aoqi@0 3667 RC_TRACE(0x00000200,
aoqi@0 3668 ("purge: %s(%s): prev method @%d in version @%d is alive",
aoqi@0 3669 method->name()->as_C_string(),
kevinw@9184 3670 method->signature()->as_C_string(), j, version));
roland@9183 3671 if (method->method_data() != NULL) {
kevinw@9184 3672 // Clean out any weak method links for running methods
kevinw@9184 3673 // (also should include not EMCP methods)
roland@9183 3674 method->method_data()->clean_weak_method_links();
roland@9183 3675 }
aoqi@0 3676 }
aoqi@0 3677 }
aoqi@0 3678 }
kevinw@9184 3679 // next previous version
kevinw@9184 3680 last = pv_node;
kevinw@9184 3681 pv_node = pv_node->previous_versions();
kevinw@9184 3682 version++;
aoqi@0 3683 }
aoqi@0 3684 RC_TRACE(0x00000200,
aoqi@0 3685 ("purge: previous version stats: live=%d, deleted=%d", live_count,
aoqi@0 3686 deleted_count));
aoqi@0 3687 }
roland@9183 3688
kevinw@9184 3689 // Clean MethodData of this class's methods so they don't refer to
kevinw@9184 3690 // old methods that are no longer running.
roland@9183 3691 Array<Method*>* methods = ik->methods();
roland@9183 3692 int num_methods = methods->length();
roland@9183 3693 for (int index2 = 0; index2 < num_methods; ++index2) {
roland@9183 3694 if (methods->at(index2)->method_data() != NULL) {
roland@9183 3695 methods->at(index2)->method_data()->clean_weak_method_links();
roland@9183 3696 }
roland@9183 3697 }
aoqi@0 3698 }
aoqi@0 3699
kevinw@9184 3700 void InstanceKlass::mark_newly_obsolete_methods(Array<Method*>* old_methods,
kevinw@9184 3701 int emcp_method_count) {
aoqi@0 3702 int obsolete_method_count = old_methods->length() - emcp_method_count;
aoqi@0 3703
aoqi@0 3704 if (emcp_method_count != 0 && obsolete_method_count != 0 &&
kevinw@9184 3705 _previous_versions != NULL) {
aoqi@0 3706 // We have a mix of obsolete and EMCP methods so we have to
aoqi@0 3707 // clear out any matching EMCP method entries the hard way.
aoqi@0 3708 int local_count = 0;
aoqi@0 3709 for (int i = 0; i < old_methods->length(); i++) {
kevinw@9184 3710 Method* old_method = old_methods->at(i);
kevinw@9184 3711 if (old_method->is_obsolete()) {
aoqi@0 3712 // only obsolete methods are interesting
aoqi@0 3713 Symbol* m_name = old_method->name();
aoqi@0 3714 Symbol* m_signature = old_method->signature();
aoqi@0 3715
kevinw@9184 3716 // previous versions are linked together through the InstanceKlass
kevinw@9184 3717 int j = 0;
kevinw@9184 3718 for (InstanceKlass* prev_version = _previous_versions;
kevinw@9184 3719 prev_version != NULL;
kevinw@9184 3720 prev_version = prev_version->previous_versions(), j++) {
kevinw@9184 3721
kevinw@9184 3722 Array<Method*>* method_refs = prev_version->methods();
kevinw@9184 3723 for (int k = 0; k < method_refs->length(); k++) {
aoqi@0 3724 Method* method = method_refs->at(k);
aoqi@0 3725
aoqi@0 3726 if (!method->is_obsolete() &&
aoqi@0 3727 method->name() == m_name &&
aoqi@0 3728 method->signature() == m_signature) {
aoqi@0 3729 // The current RedefineClasses() call has made all EMCP
aoqi@0 3730 // versions of this method obsolete so mark it as obsolete
aoqi@0 3731 RC_TRACE(0x00000400,
aoqi@0 3732 ("add: %s(%s): flush obsolete method @%d in version @%d",
aoqi@0 3733 m_name->as_C_string(), m_signature->as_C_string(), k, j));
aoqi@0 3734
aoqi@0 3735 method->set_is_obsolete();
aoqi@0 3736 break;
aoqi@0 3737 }
aoqi@0 3738 }
aoqi@0 3739
aoqi@0 3740 // The previous loop may not find a matching EMCP method, but
aoqi@0 3741 // that doesn't mean that we can optimize and not go any
aoqi@0 3742 // further back in the PreviousVersion generations. The EMCP
kevinw@9184 3743 // method for this generation could have already been made obsolete,
aoqi@0 3744 // but there still may be an older EMCP method that has not
kevinw@9184 3745 // been made obsolete.
aoqi@0 3746 }
aoqi@0 3747
aoqi@0 3748 if (++local_count >= obsolete_method_count) {
aoqi@0 3749 // no more obsolete methods so bail out now
aoqi@0 3750 break;
aoqi@0 3751 }
aoqi@0 3752 }
aoqi@0 3753 }
aoqi@0 3754 }
kevinw@9184 3755 }
kevinw@9184 3756
kevinw@9184 3757 // Save the scratch_class as the previous version if any of the methods are running.
kevinw@9184 3758 // The previous_versions are used to set breakpoints in EMCP methods and they are
kevinw@9184 3759 // also used to clean MethodData links to redefined methods that are no longer running.
kevinw@9184 3760 void InstanceKlass::add_previous_version(instanceKlassHandle scratch_class,
kevinw@9184 3761 int emcp_method_count) {
kevinw@9184 3762 assert(Thread::current()->is_VM_thread(),
kevinw@9184 3763 "only VMThread can add previous versions");
kevinw@9184 3764
kevinw@9184 3765 // RC_TRACE macro has an embedded ResourceMark
kevinw@9184 3766 RC_TRACE(0x00000400, ("adding previous version ref for %s, EMCP_cnt=%d",
kevinw@9184 3767 scratch_class->external_name(), emcp_method_count));
kevinw@9184 3768
kevinw@9184 3769 // Clean out old previous versions
kevinw@9184 3770 purge_previous_versions(this);
kevinw@9184 3771
kevinw@9184 3772 // Mark newly obsolete methods in remaining previous versions. An EMCP method from
kevinw@9184 3773 // a previous redefinition may be made obsolete by this redefinition.
kevinw@9184 3774 Array<Method*>* old_methods = scratch_class->methods();
kevinw@9184 3775 mark_newly_obsolete_methods(old_methods, emcp_method_count);
kevinw@9184 3776
kevinw@9184 3777 // If the constant pool for this previous version of the class
kevinw@9184 3778 // is not marked as being on the stack, then none of the methods
kevinw@9184 3779 // in this previous version of the class are on the stack so
kevinw@9184 3780 // we don't need to add this as a previous version.
kevinw@9184 3781 ConstantPool* cp_ref = scratch_class->constants();
kevinw@9184 3782 if (!cp_ref->on_stack()) {
kevinw@9184 3783 RC_TRACE(0x00000400, ("add: scratch class not added; no methods are running"));
kevinw@9184 3784 return;
sspitsyn@7683 3785 }
kevinw@9184 3786
kevinw@9184 3787 if (emcp_method_count != 0) {
kevinw@9184 3788 // At least one method is still running, check for EMCP methods
kevinw@9184 3789 for (int i = 0; i < old_methods->length(); i++) {
kevinw@9184 3790 Method* old_method = old_methods->at(i);
kevinw@9184 3791 if (!old_method->is_obsolete() && old_method->on_stack()) {
kevinw@9184 3792 // if EMCP method (not obsolete) is on the stack, mark as EMCP so that
kevinw@9184 3793 // we can add breakpoints for it.
kevinw@9184 3794
kevinw@9184 3795 // We set the method->on_stack bit during safepoints for class redefinition and
kevinw@9184 3796 // class unloading and use this bit to set the is_running_emcp bit.
kevinw@9184 3797 // After the safepoint, the on_stack bit is cleared and the running emcp
kevinw@9184 3798 // method may exit. If so, we would set a breakpoint in a method that
kevinw@9184 3799 // is never reached, but this won't be noticeable to the programmer.
kevinw@9184 3800 old_method->set_running_emcp(true);
kevinw@9184 3801 RC_TRACE(0x00000400, ("add: EMCP method %s is on_stack " INTPTR_FORMAT,
kevinw@9184 3802 old_method->name_and_sig_as_C_string(), old_method));
kevinw@9184 3803 } else if (!old_method->is_obsolete()) {
kevinw@9184 3804 RC_TRACE(0x00000400, ("add: EMCP method %s is NOT on_stack " INTPTR_FORMAT,
kevinw@9184 3805 old_method->name_and_sig_as_C_string(), old_method));
kevinw@9184 3806 }
sspitsyn@7683 3807 }
sspitsyn@7683 3808 }
kevinw@9184 3809
kevinw@9184 3810 // Add previous version if any methods are still running.
kevinw@9184 3811 RC_TRACE(0x00000400, ("add: scratch class added; one of its methods is on_stack"));
kevinw@9184 3812 assert(scratch_class->previous_versions() == NULL, "shouldn't have a previous version");
kevinw@9184 3813 scratch_class->link_previous_versions(previous_versions());
kevinw@9184 3814 link_previous_versions(scratch_class());
kevinw@9184 3815 } // end add_previous_version()
sspitsyn@7683 3816
aoqi@0 3817 Method* InstanceKlass::method_with_idnum(int idnum) {
aoqi@0 3818 Method* m = NULL;
aoqi@0 3819 if (idnum < methods()->length()) {
aoqi@0 3820 m = methods()->at(idnum);
aoqi@0 3821 }
aoqi@0 3822 if (m == NULL || m->method_idnum() != idnum) {
aoqi@0 3823 for (int index = 0; index < methods()->length(); ++index) {
aoqi@0 3824 m = methods()->at(index);
aoqi@0 3825 if (m->method_idnum() == idnum) {
aoqi@0 3826 return m;
aoqi@0 3827 }
aoqi@0 3828 }
aoqi@0 3829 // None found, return null for the caller to handle.
aoqi@0 3830 return NULL;
aoqi@0 3831 }
aoqi@0 3832 return m;
aoqi@0 3833 }
aoqi@0 3834
sspitsyn@7683 3835
sspitsyn@7683 3836 Method* InstanceKlass::method_with_orig_idnum(int idnum) {
sspitsyn@7683 3837 if (idnum >= methods()->length()) {
sspitsyn@7683 3838 return NULL;
sspitsyn@7683 3839 }
sspitsyn@7683 3840 Method* m = methods()->at(idnum);
sspitsyn@7683 3841 if (m != NULL && m->orig_method_idnum() == idnum) {
sspitsyn@7683 3842 return m;
sspitsyn@7683 3843 }
sspitsyn@7683 3844 // Obsolete method idnum does not match the original idnum
sspitsyn@7683 3845 for (int index = 0; index < methods()->length(); ++index) {
sspitsyn@7683 3846 m = methods()->at(index);
sspitsyn@7683 3847 if (m->orig_method_idnum() == idnum) {
sspitsyn@7683 3848 return m;
sspitsyn@7683 3849 }
sspitsyn@7683 3850 }
sspitsyn@7683 3851 // None found, return null for the caller to handle.
sspitsyn@7683 3852 return NULL;
sspitsyn@7683 3853 }
sspitsyn@7683 3854
sspitsyn@7683 3855
sspitsyn@7683 3856 Method* InstanceKlass::method_with_orig_idnum(int idnum, int version) {
sspitsyn@7683 3857 InstanceKlass* holder = get_klass_version(version);
sspitsyn@7683 3858 if (holder == NULL) {
sspitsyn@7683 3859 return NULL; // The version of klass is gone, no method is found
sspitsyn@7683 3860 }
sspitsyn@7683 3861 Method* method = holder->method_with_orig_idnum(idnum);
sspitsyn@7683 3862 return method;
sspitsyn@7683 3863 }
sspitsyn@7683 3864
sspitsyn@7683 3865
aoqi@0 3866 jint InstanceKlass::get_cached_class_file_len() {
aoqi@0 3867 return VM_RedefineClasses::get_cached_class_file_len(_cached_class_file);
aoqi@0 3868 }
aoqi@0 3869
aoqi@0 3870 unsigned char * InstanceKlass::get_cached_class_file_bytes() {
aoqi@0 3871 return VM_RedefineClasses::get_cached_class_file_bytes(_cached_class_file);
aoqi@0 3872 }

mercurial