1 /* 2 * Copyright (c) 1997, 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 "code/vtableStubs.hpp" 27 #include "compiler/compileBroker.hpp" 28 #include "compiler/disassembler.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "oops/instanceKlass.hpp" 32 #include "oops/klassVtable.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "prims/forte.hpp" 35 #include "prims/jvmtiExport.hpp" 36 #include "runtime/handles.inline.hpp" 37 #include "runtime/mutexLocker.hpp" 38 #include "runtime/sharedRuntime.hpp" 39 #ifdef COMPILER2 40 #include "opto/matcher.hpp" 41 #endif 42 43 // ----------------------------------------------------------------------------------------- 44 // Implementation of VtableStub 45 46 address VtableStub::_chunk = NULL; 47 address VtableStub::_chunk_end = NULL; 48 VMReg VtableStub::_receiver_location = VMRegImpl::Bad(); 49 50 51 void* VtableStub::operator new(size_t size, int code_size) throw() { 52 assert(size == sizeof(VtableStub), "mismatched size"); 53 // compute real VtableStub size (rounded to nearest word) 54 const int real_size = align_up(code_size + (int)sizeof(VtableStub), wordSize); 55 // malloc them in chunks to minimize header overhead 56 const int chunk_factor = 32; 57 if (_chunk == NULL || _chunk + real_size > _chunk_end) { 58 const int bytes = chunk_factor * real_size + pd_code_alignment(); 59 60 // There is a dependency on the name of the blob in src/share/vm/prims/jvmtiCodeBlobEvents.cpp 61 // If changing the name, update the other file accordingly. 62 BufferBlob* blob = BufferBlob::create("vtable chunks", bytes); 63 if (blob == NULL) { 64 return NULL; 65 } 66 _chunk = blob->content_begin(); 67 _chunk_end = _chunk + bytes; 68 Forte::register_stub("vtable stub", _chunk, _chunk_end); 69 align_chunk(); 70 } 71 assert(_chunk + real_size <= _chunk_end, "bad allocation"); 72 void* res = _chunk; 73 _chunk += real_size; 74 align_chunk(); 75 return res; 76 } 77 78 79 void VtableStub::print_on(outputStream* st) const { 80 st->print("vtable stub (index = %d, receiver_location = " INTX_FORMAT ", code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)", 81 index(), p2i(receiver_location()), p2i(code_begin()), p2i(code_end())); 82 } 83 84 85 // ----------------------------------------------------------------------------------------- 86 // Implementation of VtableStubs 87 // 88 // For each hash value there's a linked list of vtable stubs (with that 89 // hash value). Each list is anchored in a little hash _table, indexed 90 // by that hash value. 91 92 VtableStub* VtableStubs::_table[VtableStubs::N]; 93 int VtableStubs::_number_of_vtable_stubs = 0; 94 95 96 void VtableStubs::initialize() { 97 VtableStub::_receiver_location = SharedRuntime::name_for_receiver(); 98 { 99 MutexLocker ml(VtableStubs_lock); 100 assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once"); 101 assert(is_power_of_2(N), "N must be a power of 2"); 102 for (int i = 0; i < N; i++) { 103 _table[i] = NULL; 104 } 105 } 106 } 107 108 109 address VtableStubs::find_stub(bool is_vtable_stub, int vtable_index) { 110 assert(vtable_index >= 0, "must be positive"); 111 112 VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL; 113 if (s == NULL) { 114 if (is_vtable_stub) { 115 s = create_vtable_stub(vtable_index); 116 } else { 117 s = create_itable_stub(vtable_index); 118 } 119 120 // Creation of vtable or itable can fail if there is not enough free space in the code cache. 121 if (s == NULL) { 122 return NULL; 123 } 124 125 enter(is_vtable_stub, vtable_index, s); 126 if (PrintAdapterHandlers) { 127 tty->print_cr("Decoding VtableStub %s[%d]@" INTX_FORMAT, 128 is_vtable_stub? "vtbl": "itbl", vtable_index, p2i(VtableStub::receiver_location())); 129 Disassembler::decode(s->code_begin(), s->code_end()); 130 } 131 // Notify JVMTI about this stub. The event will be recorded by the enclosing 132 // JvmtiDynamicCodeEventCollector and posted when this thread has released 133 // all locks. 134 if (JvmtiExport::should_post_dynamic_code_generated()) { 135 JvmtiExport::post_dynamic_code_generated_while_holding_locks(is_vtable_stub? "vtable stub": "itable stub", 136 s->code_begin(), s->code_end()); 137 } 138 } 139 return s->entry_point(); 140 } 141 142 143 inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){ 144 // Assumption: receiver_location < 4 in most cases. 145 int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index; 146 return (is_vtable_stub ? ~hash : hash) & mask; 147 } 148 149 150 VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) { 151 MutexLocker ml(VtableStubs_lock); 152 unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index); 153 VtableStub* s = _table[hash]; 154 while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next(); 155 return s; 156 } 157 158 159 void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) { 160 MutexLocker ml(VtableStubs_lock); 161 assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub"); 162 unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index); 163 // enter s at the beginning of the corresponding list 164 s->set_next(_table[h]); 165 _table[h] = s; 166 _number_of_vtable_stubs++; 167 } 168 169 170 bool VtableStubs::is_entry_point(address pc) { 171 MutexLocker ml(VtableStubs_lock); 172 VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset()); 173 uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index()); 174 VtableStub* s; 175 for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {} 176 return s == stub; 177 } 178 179 180 bool VtableStubs::contains(address pc) { 181 // simple solution for now - we may want to use 182 // a faster way if this function is called often 183 return stub_containing(pc) != NULL; 184 } 185 186 187 VtableStub* VtableStubs::stub_containing(address pc) { 188 // Note: No locking needed since any change to the data structure 189 // happens with an atomic store into it (we don't care about 190 // consistency with the _number_of_vtable_stubs counter). 191 for (int i = 0; i < N; i++) { 192 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { 193 if (s->contains(pc)) return s; 194 } 195 } 196 return NULL; 197 } 198 199 void vtableStubs_init() { 200 VtableStubs::initialize(); 201 } 202 203 void VtableStubs::vtable_stub_do(void f(VtableStub*)) { 204 for (int i = 0; i < N; i++) { 205 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { 206 f(s); 207 } 208 } 209 } 210 211 212 //----------------------------------------------------------------------------------------------------- 213 // Non-product code 214 #ifndef PRODUCT 215 216 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) { 217 ResourceMark rm; 218 HandleMark hm; 219 Klass* klass = receiver->klass(); 220 InstanceKlass* ik = InstanceKlass::cast(klass); 221 klassVtable vt = ik->vtable(); 222 ik->print(); 223 fatal("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", " 224 "index %d (vtable length %d)", 225 p2i(receiver), index, vt.length()); 226 } 227 228 #endif // PRODUCT