Mon, 06 Aug 2012 15:54:45 -0400
7116786: RFE: Detailed information on VerifyErrors
Summary: Provide additional detail in VerifyError messages
Reviewed-by: sspitsyn, acorn
1 /*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "classfile/dictionary.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "oops/oop.inline.hpp"
29 #include "prims/jvmtiRedefineClassesTrace.hpp"
30 #include "services/classLoadingService.hpp"
31 #include "utilities/hashtable.inline.hpp"
34 DictionaryEntry* Dictionary::_current_class_entry = NULL;
35 int Dictionary::_current_class_index = 0;
38 Dictionary::Dictionary(int table_size)
39 : TwoOopHashtable<klassOop, mtClass>(table_size, sizeof(DictionaryEntry)) {
40 _current_class_index = 0;
41 _current_class_entry = NULL;
42 };
46 Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t,
47 int number_of_entries)
48 : TwoOopHashtable<klassOop, mtClass>(table_size, sizeof(DictionaryEntry), t, number_of_entries) {
49 _current_class_index = 0;
50 _current_class_entry = NULL;
51 };
54 DictionaryEntry* Dictionary::new_entry(unsigned int hash, klassOop klass,
55 oop loader) {
56 DictionaryEntry* entry;
57 entry = (DictionaryEntry*)Hashtable<klassOop, mtClass>::new_entry(hash, klass);
58 entry->set_loader(loader);
59 entry->set_pd_set(NULL);
60 return entry;
61 }
64 DictionaryEntry* Dictionary::new_entry() {
65 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<klassOop, mtClass>::new_entry(0L, NULL);
66 entry->set_loader(NULL);
67 entry->set_pd_set(NULL);
68 return entry;
69 }
72 void Dictionary::free_entry(DictionaryEntry* entry) {
73 // avoid recursion when deleting linked list
74 while (entry->pd_set() != NULL) {
75 ProtectionDomainEntry* to_delete = entry->pd_set();
76 entry->set_pd_set(to_delete->next());
77 delete to_delete;
78 }
79 Hashtable<klassOop, mtClass>::free_entry(entry);
80 }
83 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
84 #ifdef ASSERT
85 if (protection_domain == instanceKlass::cast(klass())->protection_domain()) {
86 // Ensure this doesn't show up in the pd_set (invariant)
87 bool in_pd_set = false;
88 for (ProtectionDomainEntry* current = _pd_set;
89 current != NULL;
90 current = current->next()) {
91 if (current->protection_domain() == protection_domain) {
92 in_pd_set = true;
93 break;
94 }
95 }
96 if (in_pd_set) {
97 assert(false, "A klass's protection domain should not show up "
98 "in its sys. dict. PD set");
99 }
100 }
101 #endif /* ASSERT */
103 if (protection_domain == instanceKlass::cast(klass())->protection_domain()) {
104 // Succeeds trivially
105 return true;
106 }
108 for (ProtectionDomainEntry* current = _pd_set;
109 current != NULL;
110 current = current->next()) {
111 if (current->protection_domain() == protection_domain) return true;
112 }
113 return false;
114 }
117 void DictionaryEntry::add_protection_domain(oop protection_domain) {
118 assert_locked_or_safepoint(SystemDictionary_lock);
119 if (!contains_protection_domain(protection_domain)) {
120 ProtectionDomainEntry* new_head =
121 new ProtectionDomainEntry(protection_domain, _pd_set);
122 // Warning: Preserve store ordering. The SystemDictionary is read
123 // without locks. The new ProtectionDomainEntry must be
124 // complete before other threads can be allowed to see it
125 // via a store to _pd_set.
126 OrderAccess::release_store_ptr(&_pd_set, new_head);
127 }
128 if (TraceProtectionDomainVerification && WizardMode) {
129 print();
130 }
131 }
134 bool Dictionary::do_unloading(BoolObjectClosure* is_alive) {
135 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
136 bool class_was_unloaded = false;
137 int index = 0; // Defined here for portability! Do not move
139 // Remove unloadable entries and classes from system dictionary
140 // The placeholder array has been handled in always_strong_oops_do.
141 DictionaryEntry* probe = NULL;
142 for (index = 0; index < table_size(); index++) {
143 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
144 probe = *p;
145 klassOop e = probe->klass();
146 oop class_loader = probe->loader();
148 instanceKlass* ik = instanceKlass::cast(e);
149 if (ik->previous_versions() != NULL) {
150 // This klass has previous versions so see what we can cleanup
151 // while it is safe to do so.
153 int gc_count = 0; // leave debugging breadcrumbs
154 int live_count = 0;
156 // RC_TRACE macro has an embedded ResourceMark
157 RC_TRACE(0x00000200, ("unload: %s: previous version length=%d",
158 ik->external_name(), ik->previous_versions()->length()));
160 for (int i = ik->previous_versions()->length() - 1; i >= 0; i--) {
161 // check the previous versions array for GC'ed weak refs
162 PreviousVersionNode * pv_node = ik->previous_versions()->at(i);
163 jobject cp_ref = pv_node->prev_constant_pool();
164 assert(cp_ref != NULL, "cp ref was unexpectedly cleared");
165 if (cp_ref == NULL) {
166 delete pv_node;
167 ik->previous_versions()->remove_at(i);
168 // Since we are traversing the array backwards, we don't have to
169 // do anything special with the index.
170 continue; // robustness
171 }
173 constantPoolOop pvcp = (constantPoolOop)JNIHandles::resolve(cp_ref);
174 if (pvcp == NULL) {
175 // this entry has been GC'ed so remove it
176 delete pv_node;
177 ik->previous_versions()->remove_at(i);
178 // Since we are traversing the array backwards, we don't have to
179 // do anything special with the index.
180 gc_count++;
181 continue;
182 } else {
183 RC_TRACE(0x00000200, ("unload: previous version @%d is alive", i));
184 if (is_alive->do_object_b(pvcp)) {
185 live_count++;
186 } else {
187 guarantee(false, "sanity check");
188 }
189 }
191 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods();
192 if (method_refs != NULL) {
193 RC_TRACE(0x00000200, ("unload: previous methods length=%d",
194 method_refs->length()));
195 for (int j = method_refs->length() - 1; j >= 0; j--) {
196 jweak method_ref = method_refs->at(j);
197 assert(method_ref != NULL, "weak method ref was unexpectedly cleared");
198 if (method_ref == NULL) {
199 method_refs->remove_at(j);
200 // Since we are traversing the array backwards, we don't have to
201 // do anything special with the index.
202 continue; // robustness
203 }
205 methodOop method = (methodOop)JNIHandles::resolve(method_ref);
206 if (method == NULL) {
207 // this method entry has been GC'ed so remove it
208 JNIHandles::destroy_weak_global(method_ref);
209 method_refs->remove_at(j);
210 } else {
211 // RC_TRACE macro has an embedded ResourceMark
212 RC_TRACE(0x00000200,
213 ("unload: %s(%s): prev method @%d in version @%d is alive",
214 method->name()->as_C_string(),
215 method->signature()->as_C_string(), j, i));
216 }
217 }
218 }
219 }
220 assert(ik->previous_versions()->length() == live_count, "sanity check");
221 RC_TRACE(0x00000200,
222 ("unload: previous version stats: live=%d, GC'ed=%d", live_count,
223 gc_count));
224 }
226 // Non-unloadable classes were handled in always_strong_oops_do
227 if (!is_strongly_reachable(class_loader, e)) {
228 // Entry was not visited in phase1 (negated test from phase1)
229 assert(class_loader != NULL, "unloading entry with null class loader");
230 oop k_def_class_loader = ik->class_loader();
232 // Do we need to delete this system dictionary entry?
233 bool purge_entry = false;
235 // Do we need to delete this system dictionary entry?
236 if (!is_alive->do_object_b(class_loader)) {
237 // If the loader is not live this entry should always be
238 // removed (will never be looked up again). Note that this is
239 // not the same as unloading the referred class.
240 if (k_def_class_loader == class_loader) {
241 // This is the defining entry, so the referred class is about
242 // to be unloaded.
243 // Notify the debugger and clean up the class.
244 guarantee(!is_alive->do_object_b(e),
245 "klass should not be live if defining loader is not");
246 class_was_unloaded = true;
247 // notify the debugger
248 if (JvmtiExport::should_post_class_unload()) {
249 JvmtiExport::post_class_unload(ik->as_klassOop());
250 }
252 // notify ClassLoadingService of class unload
253 ClassLoadingService::notify_class_unloaded(ik);
255 // Clean up C heap
256 ik->release_C_heap_structures();
257 }
258 // Also remove this system dictionary entry.
259 purge_entry = true;
261 } else {
262 // The loader in this entry is alive. If the klass is dead,
263 // the loader must be an initiating loader (rather than the
264 // defining loader). Remove this entry.
265 if (!is_alive->do_object_b(e)) {
266 guarantee(!is_alive->do_object_b(k_def_class_loader),
267 "defining loader should not be live if klass is not");
268 // If we get here, the class_loader must not be the defining
269 // loader, it must be an initiating one.
270 assert(k_def_class_loader != class_loader,
271 "cannot have live defining loader and unreachable klass");
273 // Loader is live, but class and its defining loader are dead.
274 // Remove the entry. The class is going away.
275 purge_entry = true;
276 }
277 }
279 if (purge_entry) {
280 *p = probe->next();
281 if (probe == _current_class_entry) {
282 _current_class_entry = NULL;
283 }
284 free_entry(probe);
285 continue;
286 }
287 }
288 p = probe->next_addr();
289 }
290 }
291 return class_was_unloaded;
292 }
295 void Dictionary::always_strong_classes_do(OopClosure* blk) {
296 // Follow all system classes and temporary placeholders in dictionary
297 for (int index = 0; index < table_size(); index++) {
298 for (DictionaryEntry *probe = bucket(index);
299 probe != NULL;
300 probe = probe->next()) {
301 klassOop e = probe->klass();
302 oop class_loader = probe->loader();
303 if (is_strongly_reachable(class_loader, e)) {
304 blk->do_oop((oop*)probe->klass_addr());
305 if (class_loader != NULL) {
306 blk->do_oop(probe->loader_addr());
307 }
308 probe->protection_domain_set_oops_do(blk);
309 }
310 }
311 }
312 }
315 // Just the classes from defining class loaders
316 void Dictionary::classes_do(void f(klassOop)) {
317 for (int index = 0; index < table_size(); index++) {
318 for (DictionaryEntry* probe = bucket(index);
319 probe != NULL;
320 probe = probe->next()) {
321 klassOop k = probe->klass();
322 if (probe->loader() == instanceKlass::cast(k)->class_loader()) {
323 f(k);
324 }
325 }
326 }
327 }
329 // Added for initialize_itable_for_klass to handle exceptions
330 // Just the classes from defining class loaders
331 void Dictionary::classes_do(void f(klassOop, TRAPS), TRAPS) {
332 for (int index = 0; index < table_size(); index++) {
333 for (DictionaryEntry* probe = bucket(index);
334 probe != NULL;
335 probe = probe->next()) {
336 klassOop k = probe->klass();
337 if (probe->loader() == instanceKlass::cast(k)->class_loader()) {
338 f(k, CHECK);
339 }
340 }
341 }
342 }
345 // All classes, and their class loaders
346 // (added for helpers that use HandleMarks and ResourceMarks)
347 // Don't iterate over placeholders
348 void Dictionary::classes_do(void f(klassOop, oop, TRAPS), TRAPS) {
349 for (int index = 0; index < table_size(); index++) {
350 for (DictionaryEntry* probe = bucket(index);
351 probe != NULL;
352 probe = probe->next()) {
353 klassOop k = probe->klass();
354 f(k, probe->loader(), CHECK);
355 }
356 }
357 }
360 // All classes, and their class loaders
361 // Don't iterate over placeholders
362 void Dictionary::classes_do(void f(klassOop, oop)) {
363 for (int index = 0; index < table_size(); index++) {
364 for (DictionaryEntry* probe = bucket(index);
365 probe != NULL;
366 probe = probe->next()) {
367 klassOop k = probe->klass();
368 f(k, probe->loader());
369 }
370 }
371 }
374 void Dictionary::oops_do(OopClosure* f) {
375 for (int index = 0; index < table_size(); index++) {
376 for (DictionaryEntry* probe = bucket(index);
377 probe != NULL;
378 probe = probe->next()) {
379 f->do_oop((oop*)probe->klass_addr());
380 if (probe->loader() != NULL) {
381 f->do_oop(probe->loader_addr());
382 }
383 probe->protection_domain_set_oops_do(f);
384 }
385 }
386 }
389 void Dictionary::methods_do(void f(methodOop)) {
390 for (int index = 0; index < table_size(); index++) {
391 for (DictionaryEntry* probe = bucket(index);
392 probe != NULL;
393 probe = probe->next()) {
394 klassOop k = probe->klass();
395 if (probe->loader() == instanceKlass::cast(k)->class_loader()) {
396 // only take klass is we have the entry with the defining class loader
397 instanceKlass::cast(k)->methods_do(f);
398 }
399 }
400 }
401 }
404 klassOop Dictionary::try_get_next_class() {
405 while (true) {
406 if (_current_class_entry != NULL) {
407 klassOop k = _current_class_entry->klass();
408 _current_class_entry = _current_class_entry->next();
409 return k;
410 }
411 _current_class_index = (_current_class_index + 1) % table_size();
412 _current_class_entry = bucket(_current_class_index);
413 }
414 // never reached
415 }
418 // Add a loaded class to the system dictionary.
419 // Readers of the SystemDictionary aren't always locked, so _buckets
420 // is volatile. The store of the next field in the constructor is
421 // also cast to volatile; we do this to ensure store order is maintained
422 // by the compilers.
424 void Dictionary::add_klass(Symbol* class_name, Handle class_loader,
425 KlassHandle obj) {
426 assert_locked_or_safepoint(SystemDictionary_lock);
427 assert(obj() != NULL, "adding NULL obj");
428 assert(Klass::cast(obj())->name() == class_name, "sanity check on name");
430 unsigned int hash = compute_hash(class_name, class_loader);
431 int index = hash_to_index(hash);
432 DictionaryEntry* entry = new_entry(hash, obj(), class_loader());
433 add_entry(index, entry);
434 }
437 // This routine does not lock the system dictionary.
438 //
439 // Since readers don't hold a lock, we must make sure that system
440 // dictionary entries are only removed at a safepoint (when only one
441 // thread is running), and are added to in a safe way (all links must
442 // be updated in an MT-safe manner).
443 //
444 // Callers should be aware that an entry could be added just after
445 // _buckets[index] is read here, so the caller will not see the new entry.
446 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
447 Symbol* class_name,
448 Handle class_loader) {
449 oop loader = class_loader();
450 debug_only(_lookup_count++);
451 for (DictionaryEntry* entry = bucket(index);
452 entry != NULL;
453 entry = entry->next()) {
454 if (entry->hash() == hash && entry->equals(class_name, loader)) {
455 return entry;
456 }
457 debug_only(_lookup_length++);
458 }
459 return NULL;
460 }
463 klassOop Dictionary::find(int index, unsigned int hash, Symbol* name,
464 Handle loader, Handle protection_domain, TRAPS) {
465 DictionaryEntry* entry = get_entry(index, hash, name, loader);
466 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
467 return entry->klass();
468 } else {
469 return NULL;
470 }
471 }
474 klassOop Dictionary::find_class(int index, unsigned int hash,
475 Symbol* name, Handle loader) {
476 assert_locked_or_safepoint(SystemDictionary_lock);
477 assert (index == index_for(name, loader), "incorrect index?");
479 DictionaryEntry* entry = get_entry(index, hash, name, loader);
480 return (entry != NULL) ? entry->klass() : (klassOop)NULL;
481 }
484 // Variant of find_class for shared classes. No locking required, as
485 // that table is static.
487 klassOop Dictionary::find_shared_class(int index, unsigned int hash,
488 Symbol* name) {
489 assert (index == index_for(name, Handle()), "incorrect index?");
491 DictionaryEntry* entry = get_entry(index, hash, name, Handle());
492 return (entry != NULL) ? entry->klass() : (klassOop)NULL;
493 }
496 void Dictionary::add_protection_domain(int index, unsigned int hash,
497 instanceKlassHandle klass,
498 Handle loader, Handle protection_domain,
499 TRAPS) {
500 Symbol* klass_name = klass->name();
501 DictionaryEntry* entry = get_entry(index, hash, klass_name, loader);
503 assert(entry != NULL,"entry must be present, we just created it");
504 assert(protection_domain() != NULL,
505 "real protection domain should be present");
507 entry->add_protection_domain(protection_domain());
509 assert(entry->contains_protection_domain(protection_domain()),
510 "now protection domain should be present");
511 }
514 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
515 Symbol* name,
516 Handle loader,
517 Handle protection_domain) {
518 DictionaryEntry* entry = get_entry(index, hash, name, loader);
519 return entry->is_valid_protection_domain(protection_domain);
520 }
523 void Dictionary::reorder_dictionary() {
525 // Copy all the dictionary entries into a single master list.
527 DictionaryEntry* master_list = NULL;
528 for (int i = 0; i < table_size(); ++i) {
529 DictionaryEntry* p = bucket(i);
530 while (p != NULL) {
531 DictionaryEntry* tmp;
532 tmp = p->next();
533 p->set_next(master_list);
534 master_list = p;
535 p = tmp;
536 }
537 set_entry(i, NULL);
538 }
540 // Add the dictionary entries back to the list in the correct buckets.
541 Thread *thread = Thread::current();
543 while (master_list != NULL) {
544 DictionaryEntry* p = master_list;
545 master_list = master_list->next();
546 p->set_next(NULL);
547 Symbol* class_name = instanceKlass::cast((klassOop)(p->klass()))->name();
548 unsigned int hash = compute_hash(class_name, Handle(thread, p->loader()));
549 int index = hash_to_index(hash);
550 p->set_hash(hash);
551 p->set_next(bucket(index));
552 set_entry(index, p);
553 }
554 }
556 SymbolPropertyTable::SymbolPropertyTable(int table_size)
557 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
558 {
559 }
560 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
561 int number_of_entries)
562 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
563 {
564 }
567 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
568 Symbol* sym,
569 intptr_t sym_mode) {
570 assert(index == index_for(sym, sym_mode), "incorrect index?");
571 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
572 if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) {
573 return p;
574 }
575 }
576 return NULL;
577 }
580 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash,
581 Symbol* sym, intptr_t sym_mode) {
582 assert_locked_or_safepoint(SystemDictionary_lock);
583 assert(index == index_for(sym, sym_mode), "incorrect index?");
584 assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry");
586 SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode);
587 Hashtable<Symbol*, mtSymbol>::add_entry(index, p);
588 return p;
589 }
592 void SymbolPropertyTable::oops_do(OopClosure* f) {
593 for (int index = 0; index < table_size(); index++) {
594 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
595 if (p->property_oop() != NULL) {
596 f->do_oop(p->property_oop_addr());
597 }
598 }
599 }
600 }
602 void SymbolPropertyTable::methods_do(void f(methodOop)) {
603 for (int index = 0; index < table_size(); index++) {
604 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
605 oop prop = p->property_oop();
606 if (prop != NULL && prop->is_method()) {
607 f((methodOop)prop);
608 }
609 }
610 }
611 }
614 // ----------------------------------------------------------------------------
615 #ifndef PRODUCT
617 void Dictionary::print() {
618 ResourceMark rm;
619 HandleMark hm;
621 tty->print_cr("Java system dictionary (table_size=%d, classes=%d)",
622 table_size(), number_of_entries());
623 tty->print_cr("^ indicates that initiating loader is different from "
624 "defining loader");
626 for (int index = 0; index < table_size(); index++) {
627 for (DictionaryEntry* probe = bucket(index);
628 probe != NULL;
629 probe = probe->next()) {
630 if (Verbose) tty->print("%4d: ", index);
631 klassOop e = probe->klass();
632 oop class_loader = probe->loader();
633 bool is_defining_class =
634 (class_loader == instanceKlass::cast(e)->class_loader());
635 tty->print("%s%s", is_defining_class ? " " : "^",
636 Klass::cast(e)->external_name());
637 if (class_loader != NULL) {
638 tty->print(", loader ");
639 class_loader->print_value();
640 }
641 tty->cr();
642 }
643 }
644 }
646 #endif
648 void Dictionary::verify() {
649 guarantee(number_of_entries() >= 0, "Verify of system dictionary failed");
650 int element_count = 0;
651 for (int index = 0; index < table_size(); index++) {
652 for (DictionaryEntry* probe = bucket(index);
653 probe != NULL;
654 probe = probe->next()) {
655 klassOop e = probe->klass();
656 oop class_loader = probe->loader();
657 guarantee(Klass::cast(e)->oop_is_instance(),
658 "Verify of system dictionary failed");
659 // class loader must be present; a null class loader is the
660 // boostrap loader
661 guarantee(class_loader == NULL || class_loader->is_instance(),
662 "checking type of class_loader");
663 e->verify();
664 probe->verify_protection_domain_set();
665 element_count++;
666 }
667 }
668 guarantee(number_of_entries() == element_count,
669 "Verify of system dictionary failed");
670 debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
671 }