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