1 /* 2 * Copyright (c) 2017, 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/protectionDomainCache.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "logging/log.hpp" 29 #include "logging/logStream.hpp" 30 #include "memory/iterator.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "oops/weakHandle.inline.hpp" 34 #include "utilities/hashtable.inline.hpp" 35 36 unsigned int ProtectionDomainCacheTable::compute_hash(Handle protection_domain) { 37 // Identity hash can safepoint, so keep protection domain in a Handle. 38 return (unsigned int)(protection_domain->identity_hash()); 39 } 40 41 int ProtectionDomainCacheTable::index_for(Handle protection_domain) { 42 return hash_to_index(compute_hash(protection_domain)); 43 } 44 45 ProtectionDomainCacheTable::ProtectionDomainCacheTable(int table_size) 46 : Hashtable<ClassLoaderWeakHandle, mtClass>(table_size, sizeof(ProtectionDomainCacheEntry)) 47 { _dead_entries = false; 48 } 49 50 void ProtectionDomainCacheTable::trigger_cleanup() { 51 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); 52 _dead_entries = true; 53 Service_lock->notify_all(); 54 } 55 56 void ProtectionDomainCacheTable::unlink() { 57 MutexLocker ml(SystemDictionary_lock); 58 for (int i = 0; i < table_size(); ++i) { 59 ProtectionDomainCacheEntry** p = bucket_addr(i); 60 ProtectionDomainCacheEntry* entry = bucket(i); 61 while (entry != NULL) { 62 oop pd = entry->object_no_keepalive(); 63 if (pd != NULL) { 64 p = entry->next_addr(); 65 } else { 66 LogTarget(Debug, protectiondomain, table) lt; 67 if (lt.is_enabled()) { 68 LogStream ls(lt); 69 ls.print_cr("protection domain unlinked at %d", i); 70 } 71 entry->literal().release(); 72 *p = entry->next(); 73 free_entry(entry); 74 } 75 entry = *p; 76 } 77 } 78 _dead_entries = false; 79 } 80 81 void ProtectionDomainCacheTable::print_on(outputStream* st) const { 82 assert_locked_or_safepoint(SystemDictionary_lock); 83 st->print_cr("Protection domain cache table (table_size=%d, classes=%d)", 84 table_size(), number_of_entries()); 85 for (int index = 0; index < table_size(); index++) { 86 for (ProtectionDomainCacheEntry* probe = bucket(index); 87 probe != NULL; 88 probe = probe->next()) { 89 st->print_cr("%4d: protection_domain: " PTR_FORMAT, index, p2i(probe->object_no_keepalive())); 90 } 91 } 92 } 93 94 void ProtectionDomainCacheTable::verify() { 95 verify_table<ProtectionDomainCacheEntry>("Protection Domain Table"); 96 } 97 98 oop ProtectionDomainCacheEntry::object() { 99 return literal().resolve(); 100 } 101 102 oop ProtectionDomainEntry::object() { 103 return _pd_cache->object(); 104 } 105 106 // The object_no_keepalive() call peeks at the phantomly reachable oop without 107 // keeping it alive. This is okay to do in the VM thread state if it is not 108 // leaked out to become strongly reachable. 109 oop ProtectionDomainCacheEntry::object_no_keepalive() { 110 return literal().peek(); 111 } 112 113 oop ProtectionDomainEntry::object_no_keepalive() { 114 return _pd_cache->object_no_keepalive(); 115 } 116 117 void ProtectionDomainCacheEntry::verify() { 118 guarantee(object_no_keepalive() == NULL || oopDesc::is_oop(object_no_keepalive()), "must be an oop"); 119 } 120 121 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::get(Handle protection_domain) { 122 unsigned int hash = compute_hash(protection_domain); 123 int index = hash_to_index(hash); 124 125 ProtectionDomainCacheEntry* entry = find_entry(index, protection_domain); 126 if (entry == NULL) { 127 entry = add_entry(index, hash, protection_domain); 128 } 129 // keep entry alive 130 (void)entry->object(); 131 return entry; 132 } 133 134 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::find_entry(int index, Handle protection_domain) { 135 assert_locked_or_safepoint(SystemDictionary_lock); 136 for (ProtectionDomainCacheEntry* e = bucket(index); e != NULL; e = e->next()) { 137 if (oopDesc::equals(e->object_no_keepalive(), protection_domain())) { 138 return e; 139 } 140 } 141 142 return NULL; 143 } 144 145 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::add_entry(int index, unsigned int hash, Handle protection_domain) { 146 assert_locked_or_safepoint(SystemDictionary_lock); 147 assert(index == index_for(protection_domain), "incorrect index?"); 148 assert(find_entry(index, protection_domain) == NULL, "no double entry"); 149 150 LogTarget(Debug, protectiondomain, table) lt; 151 if (lt.is_enabled()) { 152 LogStream ls(lt); 153 ls.print("protection domain added "); 154 protection_domain->print_value_on(&ls); 155 ls.cr(); 156 } 157 ClassLoaderWeakHandle w = ClassLoaderWeakHandle::create(protection_domain); 158 ProtectionDomainCacheEntry* p = new_entry(hash, w); 159 Hashtable<ClassLoaderWeakHandle, mtClass>::add_entry(index, p); 160 return p; 161 }