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