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 {






  48 }
  49 
  50 void ProtectionDomainCacheTable::unlink() {
  51   assert(SafepointSynchronize::is_at_safepoint(), "must be");
  52   for (int i = 0; i < table_size(); ++i) {
  53     ProtectionDomainCacheEntry** p = bucket_addr(i);
  54     ProtectionDomainCacheEntry* entry = bucket(i);
  55     while (entry != NULL) {
  56       oop pd = entry->object_no_keepalive();
  57       if (pd != NULL) {
  58         p = entry->next_addr();
  59       } else {
  60         LogTarget(Debug, protectiondomain) lt;
  61         if (lt.is_enabled()) {
  62           LogStream ls(lt);
  63           ls.print_cr("protection domain unlinked at %d", i);
  64         }
  65         entry->literal().release();
  66         *p = entry->next();
  67         free_entry(entry);
  68       }
  69       entry = *p;
  70     }
  71   }

  72 }
  73 
  74 void ProtectionDomainCacheTable::print_on(outputStream* st) const {

  75   st->print_cr("Protection domain cache table (table_size=%d, classes=%d)",
  76                table_size(), number_of_entries());
  77   for (int index = 0; index < table_size(); index++) {
  78     for (ProtectionDomainCacheEntry* probe = bucket(index);
  79                                      probe != NULL;
  80                                      probe = probe->next()) {
  81       st->print_cr("%4d: protection_domain: " PTR_FORMAT, index, p2i(probe->object_no_keepalive()));
  82     }
  83   }
  84 }
  85 
  86 void ProtectionDomainCacheTable::verify() {
  87   verify_table<ProtectionDomainCacheEntry>("Protection Domain Table");
  88 }
  89 
  90 oop ProtectionDomainCacheEntry::object() {
  91   return literal().resolve();
  92 }
  93 
  94 oop ProtectionDomainEntry::object() {
  95   return _pd_cache->object();
  96 }
  97 
  98 // The object_no_keepalive() call peeks at the phantomly reachable oop without
  99 // keeping it alive. This is okay to do in the VM thread state if it is not
 100 // leaked out to become strongly reachable.
 101 oop ProtectionDomainCacheEntry::object_no_keepalive() {
 102   return literal().peek();
 103 }
 104 
 105 oop ProtectionDomainEntry::object_no_keepalive() {
 106   return _pd_cache->object_no_keepalive();
 107 }
 108 
 109 void ProtectionDomainCacheEntry::verify() {
 110   guarantee(object_no_keepalive() == NULL || oopDesc::is_oop(object_no_keepalive()), "must be an oop");
 111 }
 112 
 113 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::get(Handle protection_domain) {
 114   unsigned int hash = compute_hash(protection_domain);
 115   int index = hash_to_index(hash);
 116 
 117   ProtectionDomainCacheEntry* entry = find_entry(index, protection_domain);
 118   if (entry == NULL) {
 119     entry = add_entry(index, hash, protection_domain);
 120   }
 121   // keep entry alive
 122   (void)entry->object();
 123   return entry;
 124 }
 125 
 126 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::find_entry(int index, Handle protection_domain) {

 127   for (ProtectionDomainCacheEntry* e = bucket(index); e != NULL; e = e->next()) {
 128     if (oopDesc::equals(e->object_no_keepalive(), protection_domain())) {
 129       return e;
 130     }
 131   }
 132 
 133   return NULL;
 134 }
 135 
 136 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::add_entry(int index, unsigned int hash, Handle protection_domain) {
 137   assert_locked_or_safepoint(SystemDictionary_lock);
 138   assert(index == index_for(protection_domain), "incorrect index?");
 139   assert(find_entry(index, protection_domain) == NULL, "no double entry");
 140 







 141   ClassLoaderWeakHandle w = ClassLoaderWeakHandle::create(protection_domain);
 142   ProtectionDomainCacheEntry* p = new_entry(hash, w);
 143   Hashtable<ClassLoaderWeakHandle, mtClass>::add_entry(index, p);
 144   return p;
 145 }
--- EOF ---