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 VtableStub* VtableStubs::_table[VtableStubs::N]; 94 int VtableStubs::_number_of_vtable_stubs = 0; 95 int VtableStubs::_vtab_stub_size = 0; 96 int VtableStubs::_itab_stub_size = 0; 97 98 #if defined(PRODUCT) 99 // These values are good for the PRODUCT case (no tracing). 100 static const int first_vtableStub_size = 64; 101 static const int first_itableStub_size = 256; 102 #else 103 // These values are good for the non-PRODUCT case (when tracing can be switched on). 104 // Here is a list of observed worst-case values: 105 // vtable itable 106 // aarch64: 107 // arm: 108 // ppc (linux, BE): 404 288 109 // ppc (linux, LE): 356 276 110 // ppc (AIX): 416 296 111 // s390x: 408 256 112 // Solaris-sparc: 748 348 113 // x86 (Linux): 670 309 114 // x86 (MacOS): 682 321 115 static const int first_vtableStub_size = 1024; 116 static const int first_itableStub_size = 512; 117 #endif 118 119 120 void VtableStubs::initialize() { 121 VtableStub::_receiver_location = SharedRuntime::name_for_receiver(); 122 { 123 MutexLocker ml(VtableStubs_lock); 124 assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once"); 125 assert(is_power_of_2(N), "N must be a power of 2"); 126 for (int i = 0; i < N; i++) { 127 _table[i] = NULL; 128 } 129 } 130 } 131 132 133 int VtableStubs::code_size_limit(bool is_vtable_stub) { 134 if (is_vtable_stub) { 135 return _vtab_stub_size > 0 ? _vtab_stub_size : first_vtableStub_size; 136 } else { // itable stub 137 return _itab_stub_size > 0 ? _itab_stub_size : first_itableStub_size; 138 } 139 } // code_size_limit 140 141 142 void VtableStubs::check_and_set_size_limit(bool is_vtable_stub, 143 int code_size, 144 int padding) { 145 const char* name = is_vtable_stub ? "vtable" : "itable"; 146 147 guarantee(code_size <= code_size_limit(is_vtable_stub), 148 "buffer overflow in %s stub, code_size is %d, limit is %d", name, code_size, code_size_limit(is_vtable_stub)); 149 150 if (is_vtable_stub) { 151 if (log_is_enabled(Trace, vtablestubs)) { 152 if ( (_vtab_stub_size > 0) && ((code_size + padding) > _vtab_stub_size) ) { 153 log_trace(vtablestubs)("%s size estimate needed adjustment from %d to %d bytes", 154 name, _vtab_stub_size, code_size + padding); 155 } 156 } 157 if ( (code_size + padding) > _vtab_stub_size ) { 158 _vtab_stub_size = code_size + padding; 159 } 160 } else { // itable stub 161 if (log_is_enabled(Trace, vtablestubs)) { 162 if ( (_itab_stub_size > 0) && ((code_size + padding) > _itab_stub_size) ) { 163 log_trace(vtablestubs)("%s size estimate needed adjustment from %d to %d bytes", 164 name, _itab_stub_size, code_size + padding); 165 } 166 } 167 if ( (code_size + padding) > _itab_stub_size ) { 168 _itab_stub_size = code_size + padding; 169 } 170 } 171 return; 172 } // check_and_set_size_limit 173 174 175 void VtableStubs::bookkeeping(MacroAssembler* masm, outputStream* out, VtableStub* s, 176 address npe_addr, address ame_addr, bool is_vtable_stub, 177 int index, int slop_bytes, int slop32) { 178 const char* name = is_vtable_stub ? "vtable" : "itable"; 179 const int stub_length = code_size_limit(is_vtable_stub); 180 181 if (log_is_enabled(Trace, vtablestubs)) { 182 log_trace(vtablestubs)("%s #%d at " PTR_FORMAT ": size: %d, estimate: %d, slop area: %d", 183 name, index, p2i(s->code_begin()), 184 (int)(masm->pc() - s->code_begin()), 185 stub_length, 186 (int)(s->code_end() - masm->pc())); 187 } 188 guarantee(masm->pc() <= s->code_end(), "%s #%d: overflowed buffer, estimated len: %d, actual len: %d, overrun: %d", 189 name, index, stub_length, 190 (int)(masm->pc() - s->code_begin()), 191 (int)(masm->pc() - s->code_end())); 192 assert((masm->pc() + slop32) <= s->code_end(), "%s #%d: spare space for 32-bit offset: required = %d, available = %d", 193 name, index, slop32, 194 (int)(s->code_end() - masm->pc())); 195 196 // After the first vtable/itable stub is generated, we have a much 197 // better estimate for the stub size. Remember/update this 198 // estimate after some sanity checks. 199 check_and_set_size_limit(is_vtable_stub, masm->offset(), slop_bytes); 200 s->set_exception_points(npe_addr, ame_addr); 201 } 202 203 204 address VtableStubs::find_stub(bool is_vtable_stub, int vtable_index) { 205 assert(vtable_index >= 0, "must be positive"); 206 207 VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL; 208 if (s == NULL) { 209 if (is_vtable_stub) { 210 s = create_vtable_stub(vtable_index); 211 } else { 212 s = create_itable_stub(vtable_index); 213 } 214 215 // Creation of vtable or itable can fail if there is not enough free space in the code cache. 216 if (s == NULL) { 217 return NULL; 218 } 219 220 enter(is_vtable_stub, vtable_index, s); 221 if (PrintAdapterHandlers) { 222 tty->print_cr("Decoding VtableStub %s[%d]@" INTX_FORMAT, 223 is_vtable_stub? "vtbl": "itbl", vtable_index, p2i(VtableStub::receiver_location())); 224 Disassembler::decode(s->code_begin(), s->code_end()); 225 } 226 // Notify JVMTI about this stub. The event will be recorded by the enclosing 227 // JvmtiDynamicCodeEventCollector and posted when this thread has released 228 // all locks. 229 if (JvmtiExport::should_post_dynamic_code_generated()) { 230 JvmtiExport::post_dynamic_code_generated_while_holding_locks(is_vtable_stub? "vtable stub": "itable stub", 231 s->code_begin(), s->code_end()); 232 } 233 } 234 return s->entry_point(); 235 } 236 237 238 inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){ 239 // Assumption: receiver_location < 4 in most cases. 240 int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index; 241 return (is_vtable_stub ? ~hash : hash) & mask; 242 } 243 244 245 VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) { 246 MutexLocker ml(VtableStubs_lock); 247 unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index); 248 VtableStub* s = _table[hash]; 249 while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next(); 250 return s; 251 } 252 253 254 void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) { 255 MutexLocker ml(VtableStubs_lock); 256 assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub"); 257 unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index); 258 // enter s at the beginning of the corresponding list 259 s->set_next(_table[h]); 260 _table[h] = s; 261 _number_of_vtable_stubs++; 262 } 263 264 VtableStub* VtableStubs::entry_point(address pc) { 265 MutexLocker ml(VtableStubs_lock); 266 VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset()); 267 uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index()); 268 VtableStub* s; 269 for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {} 270 return (s == stub) ? s : NULL; 271 } 272 273 bool VtableStubs::contains(address pc) { 274 // simple solution for now - we may want to use 275 // a faster way if this function is called often 276 return stub_containing(pc) != NULL; 277 } 278 279 280 VtableStub* VtableStubs::stub_containing(address pc) { 281 // Note: No locking needed since any change to the data structure 282 // happens with an atomic store into it (we don't care about 283 // consistency with the _number_of_vtable_stubs counter). 284 for (int i = 0; i < N; i++) { 285 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { 286 if (s->contains(pc)) return s; 287 } 288 } 289 return NULL; 290 } 291 292 void vtableStubs_init() { 293 VtableStubs::initialize(); 294 } 295 296 void VtableStubs::vtable_stub_do(void f(VtableStub*)) { 297 for (int i = 0; i < N; i++) { 298 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { 299 f(s); 300 } 301 } 302 } 303 304 305 //----------------------------------------------------------------------------------------------------- 306 // Non-product code 307 #ifndef PRODUCT 308 309 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) { 310 ResourceMark rm; 311 HandleMark hm; 312 Klass* klass = receiver->klass(); 313 InstanceKlass* ik = InstanceKlass::cast(klass); 314 klassVtable vt = ik->vtable(); 315 ik->print(); 316 fatal("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", " 317 "index %d (vtable length %d)", 318 p2i(receiver), index, vt.length()); 319 } 320 321 #endif // PRODUCT