1 /*
  2  * Copyright (c) 2003, 2018, 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  */
 24 
 25 #include "precompiled.hpp"
 26 #include "classfile/classLoaderData.inline.hpp"
 27 #include "classfile/dictionary.inline.hpp"
 28 #include "classfile/protectionDomainCache.hpp"
 29 #include "classfile/systemDictionary.hpp"
 30 #include "classfile/systemDictionaryShared.hpp"
 31 #include "logging/log.hpp"
 32 #include "logging/logStream.hpp"
 33 #include "memory/iterator.hpp"
 34 #include "memory/metaspaceClosure.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "oops/oop.inline.hpp"
 37 #include "runtime/atomic.hpp"
 38 #include "runtime/orderAccess.hpp"
 39 #include "runtime/safepointVerifiers.hpp"
 40 #include "utilities/hashtable.inline.hpp"
 41 
 42 // Optimization: if any dictionary needs resizing, we set this flag,
 43 // so that we dont't have to walk all dictionaries to check if any actually
 44 // needs resizing, which is costly to do at Safepoint.
 45 bool Dictionary::_some_dictionary_needs_resizing = false;
 46 
 47 size_t Dictionary::entry_size() {
 48   if (DumpSharedSpaces) {
 49     return SystemDictionaryShared::dictionary_entry_size();
 50   } else {
 51     return sizeof(DictionaryEntry);
 52   }
 53 }
 54 
 55 Dictionary::Dictionary(ClassLoaderData* loader_data, int table_size, bool resizable)
 56   : Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()),
 57     _resizable(resizable), _needs_resizing(false), _loader_data(loader_data) {
 58 };
 59 
 60 
 61 Dictionary::Dictionary(ClassLoaderData* loader_data,
 62                        int table_size, HashtableBucket<mtClass>* t,
 63                        int number_of_entries, bool resizable)
 64   : Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries),
 65     _resizable(resizable), _needs_resizing(false), _loader_data(loader_data) {
 66 };
 67 
 68 Dictionary::~Dictionary() {
 69   DictionaryEntry* probe = NULL;
 70   for (int index = 0; index < table_size(); index++) {
 71     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 72       probe = *p;
 73       *p = probe->next();
 74       free_entry(probe);
 75     }
 76   }
 77   assert(number_of_entries() == 0, "should have removed all entries");
 78   assert(new_entry_free_list() == NULL, "entry present on Dictionary's free list");
 79   free_buckets();
 80 }
 81 
 82 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass) {
 83   DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::allocate_new_entry(hash, klass);
 84   entry->set_pd_set(NULL);
 85   assert(klass->is_instance_klass(), "Must be");
 86   if (DumpSharedSpaces) {
 87     SystemDictionaryShared::init_shared_dictionary_entry(klass, entry);
 88   }
 89   return entry;
 90 }
 91 
 92 
 93 void Dictionary::free_entry(DictionaryEntry* entry) {
 94   // avoid recursion when deleting linked list
 95   // pd_set is accessed during a safepoint.
 96   while (entry->pd_set() != NULL) {
 97     ProtectionDomainEntry* to_delete = entry->pd_set();
 98     entry->set_pd_set(to_delete->next());
 99     delete to_delete;
100   }
101   // Unlink from the Hashtable prior to freeing
102   unlink_entry(entry);
103   FREE_C_HEAP_ARRAY(char, entry);
104 }
105 
106 const int _resize_load_trigger = 5;       // load factor that will trigger the resize
107 const double _resize_factor    = 2.0;     // by how much we will resize using current number of entries
108 const int _resize_max_size     = 40423;   // the max dictionary size allowed
109 const int _primelist[] = {107, 1009, 2017, 4049, 5051, 10103, 20201, _resize_max_size};
110 const int _prime_array_size = sizeof(_primelist)/sizeof(int);
111 
112 // Calculate next "good" dictionary size based on requested count
113 static int calculate_dictionary_size(int requested) {
114   int newsize = _primelist[0];
115   int index = 0;
116   for (newsize = _primelist[index]; index < (_prime_array_size - 1);
117        newsize = _primelist[++index]) {
118     if (requested <= newsize) {
119       break;
120     }
121   }
122   return newsize;
123 }
124 
125 bool Dictionary::does_any_dictionary_needs_resizing() {
126   return Dictionary::_some_dictionary_needs_resizing;
127 }
128 
129 void Dictionary::check_if_needs_resize() {
130   if (_resizable == true) {
131     if (number_of_entries() > (_resize_load_trigger*table_size())) {
132       _needs_resizing = true;
133       Dictionary::_some_dictionary_needs_resizing = true;
134     }
135   }
136 }
137 
138 bool Dictionary::resize_if_needed() {
139   int desired_size = 0;
140   if (_needs_resizing == true) {
141     desired_size = calculate_dictionary_size((int)(_resize_factor*number_of_entries()));
142     if (desired_size >= _resize_max_size) {
143       desired_size = _resize_max_size;
144       // We have reached the limit, turn resizing off
145       _resizable = false;
146     }
147     if ((desired_size != 0) && (desired_size != table_size())) {
148       if (!resize(desired_size)) {
149         // Something went wrong, turn resizing off
150         _resizable = false;
151       }
152     }
153   }
154 
155   _needs_resizing = false;
156   Dictionary::_some_dictionary_needs_resizing = false;
157 
158   return (desired_size != 0);
159 }
160 
161 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
162 #ifdef ASSERT
163   if (oopDesc::equals(protection_domain, instance_klass()->protection_domain())) {
164     // Ensure this doesn't show up in the pd_set (invariant)
165     bool in_pd_set = false;
166     for (ProtectionDomainEntry* current = pd_set_acquire();
167                                 current != NULL;
168                                 current = current->next()) {
169       if (oopDesc::equals(current->object_no_keepalive(), protection_domain)) {
170         in_pd_set = true;
171         break;
172       }
173     }
174     if (in_pd_set) {
175       assert(false, "A klass's protection domain should not show up "
176                     "in its sys. dict. PD set");
177     }
178   }
179 #endif /* ASSERT */
180 
181   if (oopDesc::equals(protection_domain, instance_klass()->protection_domain())) {
182     // Succeeds trivially
183     return true;
184   }
185 
186   for (ProtectionDomainEntry* current = pd_set_acquire();
187                               current != NULL;
188                               current = current->next()) {
189     if (oopDesc::equals(current->object_no_keepalive(), protection_domain)) return true;
190   }
191   return false;
192 }
193 
194 
195 void DictionaryEntry::add_protection_domain(Dictionary* dict, Handle protection_domain) {
196   assert_locked_or_safepoint(SystemDictionary_lock);
197   if (!contains_protection_domain(protection_domain())) {
198     ProtectionDomainCacheEntry* entry = SystemDictionary::cache_get(protection_domain);
199     ProtectionDomainEntry* new_head =
200                 new ProtectionDomainEntry(entry, pd_set());
201     // Warning: Preserve store ordering.  The SystemDictionary is read
202     //          without locks.  The new ProtectionDomainEntry must be
203     //          complete before other threads can be allowed to see it
204     //          via a store to _pd_set.
205     release_set_pd_set(new_head);
206   }
207   LogTarget(Trace, protectiondomain) lt;
208   if (lt.is_enabled()) {
209     LogStream ls(lt);
210     print_count(&ls);
211   }
212 }
213 
214 // During class loading we may have cached a protection domain that has
215 // since been unreferenced, so this entry should be cleared.
216 void Dictionary::clean_cached_protection_domains(DictionaryEntry* probe) {
217   assert_locked_or_safepoint(SystemDictionary_lock);
218 
219   ProtectionDomainEntry* current = probe->pd_set();
220   ProtectionDomainEntry* prev = NULL;
221   while (current != NULL) {
222     if (current->object_no_keepalive() == NULL) {
223       LogTarget(Debug, protectiondomain) lt;
224       if (lt.is_enabled()) {
225         ResourceMark rm;
226         // Print out trace information
227         LogStream ls(lt);
228         ls.print_cr("PD in set is not alive:");
229         ls.print("class loader: "); loader_data()->class_loader()->print_value_on(&ls);
230         ls.print(" loading: "); probe->instance_klass()->print_value_on(&ls);
231         ls.cr();
232       }
233       if (probe->pd_set() == current) {
234         probe->set_pd_set(current->next());
235       } else {
236         assert(prev != NULL, "should be set by alive entry");
237         prev->set_next(current->next());
238       }
239       ProtectionDomainEntry* to_delete = current;
240       current = current->next();
241       delete to_delete;
242     } else {
243       prev = current;
244       current = current->next();
245     }
246   }
247 }
248 
249 
250 void Dictionary::do_unloading() {
251   MutexLockerEx m(SystemDictionary_lock);
252   assert(UseZGC || SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
253 
254   // The NULL class loader doesn't initiate loading classes from other class loaders
255   if (loader_data() == ClassLoaderData::the_null_class_loader_data()) {
256     return;
257   }
258 
259   // Remove unloaded entries and classes from this dictionary
260   DictionaryEntry* probe = NULL;
261   for (int index = 0; index < table_size(); index++) {
262     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
263       probe = *p;
264       InstanceKlass* ik = probe->instance_klass();
265       ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
266 
267       // If the klass that this loader initiated is dead,
268       // (determined by checking the defining class loader)
269       // remove this entry.
270       if (k_def_class_loader_data->is_unloading()) {
271         assert(k_def_class_loader_data != loader_data(),
272                "cannot have live defining loader and unreachable klass");
273         *p = probe->next();
274         free_entry(probe);
275         continue;
276       }
277       // Clean pd_set
278       clean_cached_protection_domains(probe);
279       p = probe->next_addr();
280     }
281   }
282 }
283 
284 void Dictionary::remove_classes_in_error_state() {
285   assert(DumpSharedSpaces, "supported only when dumping");
286   DictionaryEntry* probe = NULL;
287   for (int index = 0; index < table_size(); index++) {
288     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
289       probe = *p;
290       InstanceKlass* ik = probe->instance_klass();
291       if (ik->is_in_error_state()) { // purge this entry
292         *p = probe->next();
293         free_entry(probe);
294         ResourceMark rm;
295         tty->print_cr("Preload Warning: Removed error class: %s", ik->external_name());
296         continue;
297       }
298 
299       p = probe->next_addr();
300     }
301   }
302 }
303 
304 //   Just the classes from defining class loaders
305 void Dictionary::classes_do(void f(InstanceKlass*)) {
306   for (int index = 0; index < table_size(); index++) {
307     for (DictionaryEntry* probe = bucket(index);
308                           probe != NULL;
309                           probe = probe->next()) {
310       InstanceKlass* k = probe->instance_klass();
311       if (loader_data() == k->class_loader_data()) {
312         f(k);
313       }
314     }
315   }
316 }
317 
318 // Added for initialize_itable_for_klass to handle exceptions
319 //   Just the classes from defining class loaders
320 void Dictionary::classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
321   for (int index = 0; index < table_size(); index++) {
322     for (DictionaryEntry* probe = bucket(index);
323                           probe != NULL;
324                           probe = probe->next()) {
325       InstanceKlass* k = probe->instance_klass();
326       if (loader_data() == k->class_loader_data()) {
327         f(k, CHECK);
328       }
329     }
330   }
331 }
332 
333 // All classes, and their class loaders, including initiating class loaders
334 void Dictionary::all_entries_do(void f(InstanceKlass*, ClassLoaderData*)) {
335   for (int index = 0; index < table_size(); index++) {
336     for (DictionaryEntry* probe = bucket(index);
337                           probe != NULL;
338                           probe = probe->next()) {
339       InstanceKlass* k = probe->instance_klass();
340       f(k, loader_data());
341     }
342   }
343 }
344 
345 // Used to scan and relocate the classes during CDS archive dump.
346 void Dictionary::classes_do(MetaspaceClosure* it) {
347   assert(DumpSharedSpaces, "dump-time only");
348   for (int index = 0; index < table_size(); index++) {
349     for (DictionaryEntry* probe = bucket(index);
350                           probe != NULL;
351                           probe = probe->next()) {
352       it->push(probe->klass_addr());
353       ((SharedDictionaryEntry*)probe)->metaspace_pointers_do(it);
354     }
355   }
356 }
357 
358 
359 
360 // Add a loaded class to the dictionary.
361 // Readers of the SystemDictionary aren't always locked, so _buckets
362 // is volatile. The store of the next field in the constructor is
363 // also cast to volatile;  we do this to ensure store order is maintained
364 // by the compilers.
365 
366 void Dictionary::add_klass(unsigned int hash, Symbol* class_name,
367                            InstanceKlass* obj) {
368   assert_locked_or_safepoint(SystemDictionary_lock);
369   assert(obj != NULL, "adding NULL obj");
370   assert(obj->name() == class_name, "sanity check on name");
371 
372   DictionaryEntry* entry = new_entry(hash, obj);
373   int index = hash_to_index(hash);
374   add_entry(index, entry);
375   check_if_needs_resize();
376 }
377 
378 
379 // This routine does not lock the dictionary.
380 //
381 // Since readers don't hold a lock, we must make sure that system
382 // dictionary entries are only removed at a safepoint (when only one
383 // thread is running), and are added to in a safe way (all links must
384 // be updated in an MT-safe manner).
385 //
386 // Callers should be aware that an entry could be added just after
387 // _buckets[index] is read here, so the caller will not see the new entry.
388 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
389                                        Symbol* class_name) {
390   for (DictionaryEntry* entry = bucket(index);
391                         entry != NULL;
392                         entry = entry->next()) {
393     if (entry->hash() == hash && entry->equals(class_name)) {
394       if (!DumpSharedSpaces || SystemDictionaryShared::is_builtin(entry)) {
395         return entry;
396       }
397     }
398   }
399   return NULL;
400 }
401 
402 
403 InstanceKlass* Dictionary::find(unsigned int hash, Symbol* name,
404                                 Handle protection_domain) {
405   NoSafepointVerifier nsv;
406 
407   int index = hash_to_index(hash);
408   DictionaryEntry* entry = get_entry(index, hash, name);
409   if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
410     return entry->instance_klass();
411   } else {
412     return NULL;
413   }
414 }
415 
416 
417 InstanceKlass* Dictionary::find_class(int index, unsigned int hash,
418                                       Symbol* name) {
419   assert_locked_or_safepoint(SystemDictionary_lock);
420   assert (index == index_for(name), "incorrect index?");
421 
422   DictionaryEntry* entry = get_entry(index, hash, name);
423   return (entry != NULL) ? entry->instance_klass() : NULL;
424 }
425 
426 
427 // Variant of find_class for shared classes.  No locking required, as
428 // that table is static.
429 
430 InstanceKlass* Dictionary::find_shared_class(int index, unsigned int hash,
431                                              Symbol* name) {
432   assert (index == index_for(name), "incorrect index?");
433 
434   DictionaryEntry* entry = get_entry(index, hash, name);
435   return (entry != NULL) ? entry->instance_klass() : NULL;
436 }
437 
438 
439 void Dictionary::add_protection_domain(int index, unsigned int hash,
440                                        InstanceKlass* klass,
441                                        Handle protection_domain,
442                                        TRAPS) {
443   Symbol*  klass_name = klass->name();
444   DictionaryEntry* entry = get_entry(index, hash, klass_name);
445 
446   assert(entry != NULL,"entry must be present, we just created it");
447   assert(protection_domain() != NULL,
448          "real protection domain should be present");
449 
450   entry->add_protection_domain(this, protection_domain);
451 
452 #ifdef ASSERT
453   assert(loader_data() != ClassLoaderData::the_null_class_loader_data(), "doesn't make sense");
454 #endif
455 
456   assert(entry->contains_protection_domain(protection_domain()),
457          "now protection domain should be present");
458 }
459 
460 
461 bool Dictionary::is_valid_protection_domain(unsigned int hash,
462                                             Symbol* name,
463                                             Handle protection_domain) {
464   int index = hash_to_index(hash);
465   DictionaryEntry* entry = get_entry(index, hash, name);
466   return entry->is_valid_protection_domain(protection_domain);
467 }
468 
469 #if INCLUDE_CDS
470 static bool is_jfr_event_class(Klass *k) {
471   while (k) {
472     if (k->name()->equals("jdk/jfr/Event")) {
473       return true;
474     }
475     k = k->super();
476   }
477   return false;
478 }
479 
480 void Dictionary::reorder_dictionary_for_sharing() {
481 
482   // Copy all the dictionary entries into a single master list.
483   assert(DumpSharedSpaces, "Should only be used at dump time");
484 
485   DictionaryEntry* master_list = NULL;
486   for (int i = 0; i < table_size(); ++i) {
487     DictionaryEntry* p = bucket(i);
488     while (p != NULL) {
489       DictionaryEntry* next = p->next();
490       InstanceKlass*ik = p->instance_klass();
491       if (ik->has_signer_and_not_archived()) {
492         // We cannot include signed classes in the archive because the certificates
493         // used during dump time may be different than those used during
494         // runtime (due to expiration, etc).
495         ResourceMark rm;
496         tty->print_cr("Preload Warning: Skipping %s from signed JAR",
497                        ik->name()->as_C_string());
498         free_entry(p);
499       } else if (is_jfr_event_class(ik)) {
500         // We cannot include JFR event classes because they need runtime-specific
501         // instrumentation in order to work with -XX:FlightRecorderOptions=retransform=false.
502         // There are only a small number of these classes, so it's not worthwhile to
503         // support them and make CDS more complicated.
504         ResourceMark rm;
505         tty->print_cr("Skipping JFR event class %s", ik->name()->as_C_string());
506         free_entry(p);
507       } else {
508         p->set_next(master_list);
509         master_list = p;
510       }
511       p = next;
512     }
513     set_entry(i, NULL);
514   }
515 
516   // Add the dictionary entries back to the list in the correct buckets.
517   while (master_list != NULL) {
518     DictionaryEntry* p = master_list;
519     master_list = master_list->next();
520     p->set_next(NULL);
521     Symbol* class_name = p->instance_klass()->name();
522     // Since the null class loader data isn't copied to the CDS archive,
523     // compute the hash with NULL for loader data.
524     unsigned int hash = compute_hash(class_name);
525     int index = hash_to_index(hash);
526     p->set_hash(hash);
527     p->set_next(bucket(index));
528     set_entry(index, p);
529   }
530 }
531 #endif
532 
533 SymbolPropertyTable::SymbolPropertyTable(int table_size)
534   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
535 {
536 }
537 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
538                                          int number_of_entries)
539   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
540 {
541 }
542 
543 
544 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
545                                                      Symbol* sym,
546                                                      intptr_t sym_mode) {
547   assert(index == index_for(sym, sym_mode), "incorrect index?");
548   for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
549     if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) {
550       return p;
551     }
552   }
553   return NULL;
554 }
555 
556 
557 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash,
558                                                     Symbol* sym, intptr_t sym_mode) {
559   assert_locked_or_safepoint(SystemDictionary_lock);
560   assert(index == index_for(sym, sym_mode), "incorrect index?");
561   assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry");
562 
563   SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode);
564   Hashtable<Symbol*, mtSymbol>::add_entry(index, p);
565   return p;
566 }
567 
568 void SymbolPropertyTable::oops_do(OopClosure* f) {
569   for (int index = 0; index < table_size(); index++) {
570     for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
571       if (p->method_type() != NULL) {
572         f->do_oop(p->method_type_addr());
573       }
574     }
575   }
576 }
577 
578 void SymbolPropertyTable::methods_do(void f(Method*)) {
579   for (int index = 0; index < table_size(); index++) {
580     for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
581       Method* prop = p->method();
582       if (prop != NULL) {
583         f((Method*)prop);
584       }
585     }
586   }
587 }
588 
589 
590 // ----------------------------------------------------------------------------
591 
592 void Dictionary::print_on(outputStream* st) const {
593   ResourceMark rm;
594 
595   assert(loader_data() != NULL, "loader data should not be null");
596   st->print_cr("Java dictionary (table_size=%d, classes=%d)",
597                table_size(), number_of_entries());
598   st->print_cr("^ indicates that initiating loader is different from defining loader");
599 
600   for (int index = 0; index < table_size(); index++) {
601     for (DictionaryEntry* probe = bucket(index);
602                           probe != NULL;
603                           probe = probe->next()) {
604       Klass* e = probe->instance_klass();
605       bool is_defining_class =
606          (loader_data() == e->class_loader_data());
607       st->print("%4d: %s%s", index, is_defining_class ? " " : "^", e->external_name());
608       ClassLoaderData* cld = e->class_loader_data();
609       if (cld == NULL) {
610         // Shared class not restored yet in shared dictionary
611         st->print(", loader data <shared, not restored>");
612       } else if (!loader_data()->is_the_null_class_loader_data()) {
613         // Class loader output for the dictionary for the null class loader data is
614         // redundant and obvious.
615         st->print(", ");
616         cld->print_value_on(st);
617       }
618       st->cr();
619     }
620   }
621   tty->cr();
622 }
623 
624 void DictionaryEntry::verify() {
625   Klass* e = instance_klass();
626   guarantee(e->is_instance_klass(),
627                           "Verify of dictionary failed");
628   e->verify();
629   verify_protection_domain_set();
630 }
631 
632 void Dictionary::verify() {
633   guarantee(number_of_entries() >= 0, "Verify of dictionary failed");
634 
635   ClassLoaderData* cld = loader_data();
636   // class loader must be present;  a null class loader is the
637   // boostrap loader
638   guarantee(cld != NULL || DumpSharedSpaces ||
639             cld->class_loader() == NULL ||
640             cld->class_loader()->is_instance(),
641             "checking type of class_loader");
642 
643   ResourceMark rm;
644   stringStream tempst;
645   tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id());
646   verify_table<DictionaryEntry>(tempst.as_string());
647 }