1 /*
   2  * Copyright (c) 2017, 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 "gc/shared/gcLocker.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "oops/method.hpp"
  30 #include "oops/symbol.hpp"
  31 #include "prims/resolvedMethodTable.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/mutexLocker.hpp"
  34 #include "utilities/hashtable.inline.hpp"
  35 #include "utilities/macros.hpp"
  36 
  37 
  38 ResolvedMethodTable::ResolvedMethodTable()
  39   : Hashtable<oop, mtClass>(_table_size, sizeof(ResolvedMethodEntry)) { }
  40 
  41 oop ResolvedMethodTable::lookup(int index, unsigned int hash, Method* method) {
  42   for (ResolvedMethodEntry* p = bucket(index); p != NULL; p = p->next()) {
  43     if (p->hash() == hash) {
  44       oop target = p->literal();
  45       // The method is in the table as a target already
  46       if (java_lang_invoke_ResolvedMethodName::vmtarget(target) == method) {
  47         ResourceMark rm;
  48         log_debug(membername, table) ("ResolvedMethod entry found for %s index %d",
  49                                        method->name_and_sig_as_C_string(), index);
  50         return target;
  51       }
  52     }
  53   }
  54   return NULL;
  55 }
  56 
  57 unsigned int ResolvedMethodTable::compute_hash(Method* method) {
  58   unsigned int name_hash = method->name()->identity_hash();
  59   unsigned int signature_hash = method->signature()->identity_hash();
  60   return name_hash ^ signature_hash;
  61 }
  62 
  63 
  64 oop ResolvedMethodTable::lookup(Method* method) {
  65   unsigned int hash = compute_hash(method);
  66   int index = hash_to_index(hash);
  67   return lookup(index, hash, method);
  68 }
  69 
  70 // Tell the GC that this oop was looked up in the table
  71 static void ensure_oop_alive(oop mname) {
  72   // A lookup in the ResolvedMethodTable could return an object that was previously
  73   // considered dead. The SATB part of G1 needs to get notified about this
  74   // potential resurrection, otherwise the marking might not find the object.
  75 #if INCLUDE_ALL_GCS
  76   if (mname != NULL) {
  77     oopDesc::bs()->keep_alive_barrier(mname);
  78   }
  79 #endif
  80 }
  81 
  82 oop ResolvedMethodTable::basic_add(Method* method, oop rmethod_name) {
  83   assert_locked_or_safepoint(ResolvedMethodTable_lock);
  84 
  85   unsigned int hash = compute_hash(method);
  86   int index = hash_to_index(hash);
  87 
  88   // One was added while aquiring the lock
  89   oop entry = lookup(index, hash, method);
  90   if (entry != NULL) {
  91     ensure_oop_alive(entry);
  92     return entry;
  93   }
  94 
  95   ResolvedMethodEntry* p = (ResolvedMethodEntry*) Hashtable<oop, mtClass>::new_entry(hash, rmethod_name);
  96   Hashtable<oop, mtClass>::add_entry(index, p);
  97   ResourceMark rm;
  98   log_debug(membername, table) ("ResolvedMethod entry added for %s index %d",
  99                                  method->name_and_sig_as_C_string(), index);
 100   return p->literal();
 101 }
 102 
 103 ResolvedMethodTable* ResolvedMethodTable::_the_table = NULL;
 104 
 105 oop ResolvedMethodTable::find_method(Method* method) {
 106   oop entry = _the_table->lookup(method);
 107   ensure_oop_alive(entry);
 108   return entry;
 109 }
 110 
 111 oop ResolvedMethodTable::add_method(Handle resolved_method_name) {
 112   MutexLocker ml(ResolvedMethodTable_lock);
 113   DEBUG_ONLY(NoSafepointVerifier nsv);
 114 
 115   // Check if method has been redefined while taking out ResolvedMethodTable_lock, if so
 116   // use new method.
 117   Method* method = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(resolved_method_name());
 118   assert(method->is_method(), "must be method");
 119   if (method->is_old()) {
 120     // Replace method with redefined version
 121     InstanceKlass* holder = method->method_holder();
 122     method = holder->method_with_idnum(method->method_idnum());
 123     java_lang_invoke_ResolvedMethodName::set_vmtarget(resolved_method_name(), method);
 124   }
 125   // Set flag in class to indicate this InstanceKlass has entries in the table
 126   // to avoid walking table during redefinition if none of the redefined classes
 127   // have any membernames in the table.
 128   method->method_holder()->set_has_resolved_methods();
 129 
 130   return _the_table->basic_add(method, resolved_method_name());
 131 }
 132 
 133 // Removing entries
 134 int ResolvedMethodTable::_oops_removed = 0;
 135 int ResolvedMethodTable::_oops_counted = 0;
 136 
 137 // Serially invoke removed unused oops from the table.
 138 // This is done late during GC.
 139 void ResolvedMethodTable::unlink(BoolObjectClosure* is_alive) {
 140   _oops_removed = 0;
 141   _oops_counted = 0;
 142   for (int i = 0; i < _the_table->table_size(); ++i) {
 143     ResolvedMethodEntry** p = _the_table->bucket_addr(i);
 144     ResolvedMethodEntry* entry = _the_table->bucket(i);
 145     while (entry != NULL) {
 146       _oops_counted++;
 147       if (is_alive->do_object_b(entry->literal())) {
 148         p = entry->next_addr();
 149       } else {
 150         _oops_removed++;
 151         if (log_is_enabled(Debug, membername, table)) {
 152           Method* m = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(entry->literal());
 153           ResourceMark rm;
 154           log_debug(membername, table) ("ResolvedMethod entry removed for %s index %d",
 155                                            m->name_and_sig_as_C_string(), i);
 156         }
 157         *p = entry->next();
 158         _the_table->free_entry(entry);
 159       }
 160       // get next entry
 161       entry = (ResolvedMethodEntry*)HashtableEntry<oop, mtClass>::make_ptr(*p);
 162     }
 163   }
 164   log_debug(membername, table) ("ResolvedMethod entries counted %d removed %d",
 165                                 _oops_counted, _oops_removed);
 166 }
 167 
 168 // Serially invoke "f->do_oop" on the locations of all oops in the table.
 169 void ResolvedMethodTable::oops_do(OopClosure* f) {
 170   for (int i = 0; i < _the_table->table_size(); ++i) {
 171     ResolvedMethodEntry* entry = _the_table->bucket(i);
 172     while (entry != NULL) {
 173       f->do_oop(entry->literal_addr());
 174       entry = entry->next();
 175     }
 176   }
 177 }
 178 
 179 #ifndef PRODUCT
 180 void ResolvedMethodTable::print() {
 181   for (int i = 0; i < table_size(); ++i) {
 182     ResolvedMethodEntry* entry = bucket(i);
 183     while (entry != NULL) {
 184       tty->print("%d : ", i);
 185       oop rmethod_name = entry->literal();
 186       rmethod_name->print();
 187       Method* m = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(rmethod_name);
 188       m->print();
 189       entry = entry->next();
 190     }
 191   }
 192 }
 193 #endif // PRODUCT
 194 
 195 #if INCLUDE_JVMTI
 196 // It is called at safepoint only for RedefineClasses
 197 void ResolvedMethodTable::adjust_method_entries(bool * trace_name_printed) {
 198   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 199   // For each entry in RMT, change to new method
 200   for (int i = 0; i < _the_table->table_size(); ++i) {
 201     ResolvedMethodEntry* entry = _the_table->bucket(i);
 202     while (entry != NULL) {
 203 
 204       oop mem_name = entry->literal();
 205       Method* old_method = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(mem_name);
 206 
 207       if (old_method->is_old()) {
 208 
 209         if (old_method->is_deleted()) {
 210           // leave deleted method in ResolvedMethod for now (this is a bug that we don't mark
 211           // these on_stack)
 212           continue;
 213         }
 214 
 215         InstanceKlass* holder = old_method->method_holder();
 216         Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
 217         assert(holder == new_method->method_holder(), "call after swapping redefined guts");
 218         assert(new_method != NULL, "method_with_idnum() should not be NULL");
 219         assert(old_method != new_method, "sanity check");
 220 
 221         java_lang_invoke_ResolvedMethodName::set_vmtarget(mem_name, new_method);
 222 
 223         ResourceMark rm;
 224         if (!(*trace_name_printed)) {
 225           log_info(redefine, class, update)("adjust: name=%s", old_method->method_holder()->external_name());
 226            *trace_name_printed = true;
 227         }
 228         log_debug(redefine, class, update, constantpool)
 229           ("ResolvedMethod method update: %s(%s)",
 230            new_method->name()->as_C_string(), new_method->signature()->as_C_string());
 231       }
 232       entry = entry->next();
 233     }
 234   }
 235 }
 236 #endif // INCLUDE_JVMTI