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 "utilities/hashtable.inline.hpp"
 34 
 35 unsigned int ProtectionDomainCacheTable::compute_hash(Handle protection_domain) {
 36   // Identity hash can safepoint, so keep protection domain in a Handle.
 37   return (unsigned int)(protection_domain->identity_hash());
 38 }
 39 
 40 int ProtectionDomainCacheTable::index_for(Handle protection_domain) {
 41   return hash_to_index(compute_hash(protection_domain));
 42 }
 43 
 44 ProtectionDomainCacheTable::ProtectionDomainCacheTable(int table_size)
 45   : Hashtable<oop, mtClass>(table_size, sizeof(ProtectionDomainCacheEntry))
 46 {
 47 }
 48 
 49 void ProtectionDomainCacheTable::unlink(BoolObjectClosure* is_alive) {
 50   assert(SafepointSynchronize::is_at_safepoint(), "must be");
 51   for (int i = 0; i < table_size(); ++i) {
 52     ProtectionDomainCacheEntry** p = bucket_addr(i);
 53     ProtectionDomainCacheEntry* entry = bucket(i);
 54     while (entry != NULL) {
 55       if (is_alive->do_object_b(entry->object_no_keepalive())) {
 56         p = entry->next_addr();
 57       } else {
 58         LogTarget(Debug, protectiondomain) lt;
 59         if (lt.is_enabled()) {
 60           LogStream ls(lt);
 61           ls.print("protection domain unlinked: ");
 62           entry->object_no_keepalive()->print_value_on(&ls);
 63           ls.cr();
 64         }
 65         *p = entry->next();
 66         free_entry(entry);
 67       }
 68       entry = *p;
 69     }
 70   }
 71 }
 72 
 73 void ProtectionDomainCacheTable::oops_do(OopClosure* f) {
 74   for (int index = 0; index < table_size(); index++) {
 75     for (ProtectionDomainCacheEntry* probe = bucket(index);
 76                                      probe != NULL;
 77                                      probe = probe->next()) {
 78       probe->oops_do(f);
 79     }
 80   }
 81 }
 82 
 83 void ProtectionDomainCacheTable::print_on(outputStream* st) const {
 84   st->print_cr("Protection domain cache table (table_size=%d, classes=%d)",
 85                table_size(), number_of_entries());
 86   for (int index = 0; index < table_size(); index++) {
 87     for (ProtectionDomainCacheEntry* probe = bucket(index);
 88                                      probe != NULL;
 89                                      probe = probe->next()) {
 90       st->print_cr("%4d: protection_domain: " PTR_FORMAT, index, p2i(probe->object_no_keepalive()));
 91     }
 92   }
 93 }
 94 
 95 void ProtectionDomainCacheTable::verify() {
 96   verify_table<ProtectionDomainCacheEntry>("Protection Domain Table");
 97 }
 98 
 99 oop ProtectionDomainCacheEntry::object() {
100   return RootAccess<ON_PHANTOM_OOP_REF>::oop_load(literal_addr());
101 }
102 
103 oop ProtectionDomainEntry::object() {
104   return _pd_cache->object();
105 }
106 
107 // The object_no_keepalive() call peeks at the phantomly reachable oop without
108 // keeping it alive. This is okay to do in the VM thread state if it is not
109 // leaked out to become strongly reachable.
110 oop ProtectionDomainCacheEntry::object_no_keepalive() {
111   return RootAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(literal_addr());
112 }
113 
114 oop ProtectionDomainEntry::object_no_keepalive() {
115   return _pd_cache->object_no_keepalive();
116 }
117 
118 void ProtectionDomainCacheEntry::verify() {
119   guarantee(oopDesc::is_oop(object_no_keepalive()), "must be an oop");
120 }
121 
122 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::get(Handle protection_domain) {
123   unsigned int hash = compute_hash(protection_domain);
124   int index = hash_to_index(hash);
125 
126   ProtectionDomainCacheEntry* entry = find_entry(index, protection_domain);
127   if (entry == NULL) {
128     entry = add_entry(index, hash, protection_domain);
129   }
130   return entry;
131 }
132 
133 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::find_entry(int index, Handle protection_domain) {
134   for (ProtectionDomainCacheEntry* e = bucket(index); e != NULL; e = e->next()) {
135     if (e->object_no_keepalive() == protection_domain()) {
136       return e;
137     }
138   }
139 
140   return NULL;
141 }
142 
143 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::add_entry(int index, unsigned int hash, Handle protection_domain) {
144   assert_locked_or_safepoint(SystemDictionary_lock);
145   assert(index == index_for(protection_domain), "incorrect index?");
146   assert(find_entry(index, protection_domain) == NULL, "no double entry");
147 
148   ProtectionDomainCacheEntry* p = new_entry(hash, protection_domain);
149   Hashtable<oop, mtClass>::add_entry(index, p);
150   return p;
151 }