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