1 /* 2 * Copyright (c) 1997, 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 "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 #include "utilities/align.hpp" 40 #ifdef COMPILER2 41 #include "opto/matcher.hpp" 42 #endif 43 44 // ----------------------------------------------------------------------------------------- 45 // Implementation of VtableStub 46 47 address VtableStub::_chunk = NULL; 48 address VtableStub::_chunk_end = NULL; 49 VMReg VtableStub::_receiver_location = VMRegImpl::Bad(); 50 51 52 void* VtableStub::operator new(size_t size, int code_size) throw() { 53 assert(size == sizeof(VtableStub), "mismatched size"); 54 // compute real VtableStub size (rounded to nearest word) 55 const int real_size = align_up(code_size + (int)sizeof(VtableStub), wordSize); 56 // malloc them in chunks to minimize header overhead 57 const int chunk_factor = 32; 58 if (_chunk == NULL || _chunk + real_size > _chunk_end) { 59 const int bytes = chunk_factor * real_size + pd_code_alignment(); 60 61 // There is a dependency on the name of the blob in src/share/vm/prims/jvmtiCodeBlobEvents.cpp 62 // If changing the name, update the other file accordingly. 63 VtableBlob* blob = VtableBlob::create("vtable chunks", bytes); 64 if (blob == NULL) { 65 return NULL; 66 } 67 _chunk = blob->content_begin(); 68 _chunk_end = _chunk + bytes; 69 Forte::register_stub("vtable stub", _chunk, _chunk_end); 70 align_chunk(); 71 } 72 assert(_chunk + real_size <= _chunk_end, "bad allocation"); 73 void* res = _chunk; 74 _chunk += real_size; 75 align_chunk(); 76 return res; 77 } 78 79 80 void VtableStub::print_on(outputStream* st) const { 81 st->print("vtable stub (index = %d, receiver_location = " INTX_FORMAT ", code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)", 82 index(), p2i(receiver_location()), p2i(code_begin()), p2i(code_end())); 83 } 84 85 86 // ----------------------------------------------------------------------------------------- 87 // Implementation of VtableStubs 88 // 89 // For each hash value there's a linked list of vtable stubs (with that 90 // hash value). Each list is anchored in a little hash _table, indexed 91 // by that hash value. 92 93 static int const firstStub_size = 1024; 94 95 VtableStub* VtableStubs::_table[VtableStubs::N]; 96 int VtableStubs::_number_of_vtable_stubs = 0; 97 int VtableStubs::_vtab_stub_size = 0; 98 int VtableStubs::_itab_stub_size = 0; 99 100 101 void VtableStubs::initialize() { 102 VtableStub::_receiver_location = SharedRuntime::name_for_receiver(); 103 { 104 MutexLocker ml(VtableStubs_lock); 105 assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once"); 106 assert(is_power_of_2(N), "N must be a power of 2"); 107 for (int i = 0; i < N; i++) { 108 _table[i] = NULL; 109 } 110 } 111 } 112 113 114 int VtableStub::code_size_limit(bool is_vtable_stub) { 115 if (is_vtable_stub) { 116 return VtableStubs::_vtab_stub_size > 0 ? VtableStubs::_vtab_stub_size 117 : firstStub_size; 118 } else { // itable stub 119 return VtableStubs::_itab_stub_size > 0 ? VtableStubs::_itab_stub_size 120 : firstStub_size; 121 } 122 } // code_size_limit 123 124 125 void VtableStub::check_and_set_size_limit(bool is_vtable_stub, 126 int code_size, 127 int padding ) { 128 const char* name = is_vtable_stub ? "vtable" : "itable"; 129 130 guarantee(code_size <= code_size_limit(is_vtable_stub), 131 "buffer overflow in %s stub, code_size is %d, limit is %d", name, code_size, code_size_limit(is_vtable_stub)); 132 133 if (is_vtable_stub) { 134 if ( code_size > VtableStubs::_vtab_stub_size - padding ) { 135 VtableStubs::_vtab_stub_size = code_size + padding; 136 } 137 } else { // itable stub 138 if ( code_size > VtableStubs::_itab_stub_size - padding ) { 139 VtableStubs::_itab_stub_size = code_size + padding; 140 } 141 } 142 return; 143 } // check_and_set_size_limit 144 145 146 void VtableStubs::bookkeeping(MacroAssembler* masm, outputStream* out, VtableStub* s, 147 address npe_addr, address ame_addr, bool is_vtable_stub, 148 int index, int slop_bytes, int slop32) { 149 const char* name = is_vtable_stub ? "vtable" : "itable"; 150 const int stub_length = VtableStub::code_size_limit(is_vtable_stub); 151 152 if (PrintMiscellaneous && (WizardMode || Verbose)) { 153 out->print_cr("%s #%d at " PTR_FORMAT "[%d], estimate %d, left over: %d", 154 name, index, p2i(s->entry_point()), 155 (int)(s->code_end() - s->entry_point()), 156 stub_length, 157 (int)(s->code_end() - masm->pc())); 158 } 159 guarantee(masm->pc() <= s->code_end(), "%s #%d: overflowed buffer, estimated len: %d, actual len: %d, overrun: %d", 160 name, index, stub_length, 161 (int)(masm->pc() - s->code_begin()), 162 (int)(masm->pc() - s->code_end())); 163 assert((masm->pc() + slop32) <= s->code_end(), "%s #%d: spare space for 32-bit offset: required = %d, available = %d", 164 name, index, slop32, 165 (int)(s->code_end() - masm->pc())); 166 167 // After the first vtable/itable stub is generated, we have a much 168 // better estimate for the stub size. Remember/update this 169 // estimate after some sanity checks. 170 s->check_and_set_size_limit(is_vtable_stub, masm->offset(), slop_bytes); 171 s->set_exception_points(npe_addr, ame_addr); 172 } 173 174 175 address VtableStubs::find_stub(bool is_vtable_stub, int vtable_index) { 176 assert(vtable_index >= 0, "must be positive"); 177 178 VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL; 179 if (s == NULL) { 180 if (is_vtable_stub) { 181 s = create_vtable_stub(vtable_index); 182 } else { 183 s = create_itable_stub(vtable_index); 184 } 185 186 // Creation of vtable or itable can fail if there is not enough free space in the code cache. 187 if (s == NULL) { 188 return NULL; 189 } 190 191 enter(is_vtable_stub, vtable_index, s); 192 if (PrintAdapterHandlers) { 193 tty->print_cr("Decoding VtableStub %s[%d]@" INTX_FORMAT, 194 is_vtable_stub? "vtbl": "itbl", vtable_index, p2i(VtableStub::receiver_location())); 195 Disassembler::decode(s->code_begin(), s->code_end()); 196 } 197 // Notify JVMTI about this stub. The event will be recorded by the enclosing 198 // JvmtiDynamicCodeEventCollector and posted when this thread has released 199 // all locks. 200 if (JvmtiExport::should_post_dynamic_code_generated()) { 201 JvmtiExport::post_dynamic_code_generated_while_holding_locks(is_vtable_stub? "vtable stub": "itable stub", 202 s->code_begin(), s->code_end()); 203 } 204 } 205 return s->entry_point(); 206 } 207 208 209 inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){ 210 // Assumption: receiver_location < 4 in most cases. 211 int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index; 212 return (is_vtable_stub ? ~hash : hash) & mask; 213 } 214 215 216 VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) { 217 MutexLocker ml(VtableStubs_lock); 218 unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index); 219 VtableStub* s = _table[hash]; 220 while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next(); 221 return s; 222 } 223 224 225 void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) { 226 MutexLocker ml(VtableStubs_lock); 227 assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub"); 228 unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index); 229 // enter s at the beginning of the corresponding list 230 s->set_next(_table[h]); 231 _table[h] = s; 232 _number_of_vtable_stubs++; 233 } 234 235 VtableStub* VtableStubs::entry_point(address pc) { 236 MutexLocker ml(VtableStubs_lock); 237 VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset()); 238 uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index()); 239 VtableStub* s; 240 for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {} 241 return (s == stub) ? s : NULL; 242 } 243 244 bool VtableStubs::contains(address pc) { 245 // simple solution for now - we may want to use 246 // a faster way if this function is called often 247 return stub_containing(pc) != NULL; 248 } 249 250 251 VtableStub* VtableStubs::stub_containing(address pc) { 252 // Note: No locking needed since any change to the data structure 253 // happens with an atomic store into it (we don't care about 254 // consistency with the _number_of_vtable_stubs counter). 255 for (int i = 0; i < N; i++) { 256 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { 257 if (s->contains(pc)) return s; 258 } 259 } 260 return NULL; 261 } 262 263 void vtableStubs_init() { 264 VtableStubs::initialize(); 265 } 266 267 void VtableStubs::vtable_stub_do(void f(VtableStub*)) { 268 for (int i = 0; i < N; i++) { 269 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { 270 f(s); 271 } 272 } 273 } 274 275 276 //----------------------------------------------------------------------------------------------------- 277 // Non-product code 278 #ifndef PRODUCT 279 280 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) { 281 ResourceMark rm; 282 HandleMark hm; 283 Klass* klass = receiver->klass(); 284 InstanceKlass* ik = InstanceKlass::cast(klass); 285 klassVtable vt = ik->vtable(); 286 ik->print(); 287 fatal("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", " 288 "index %d (vtable length %d)", 289 p2i(receiver), index, vt.length()); 290 } 291 292 #endif // PRODUCT