Wed, 08 Apr 2009 00:12:59 -0700
6827505: sizing logic for vtable and itable stubs needs self-check
Summary: Asserts and comments to help maintain the correct sizing of certain stubs
Reviewed-by: kvn
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | #include "incls/_precompiled.incl" |
duke@435 | 26 | #include "incls/_vtableStubs.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | // ----------------------------------------------------------------------------------------- |
duke@435 | 29 | // Implementation of VtableStub |
duke@435 | 30 | |
duke@435 | 31 | address VtableStub::_chunk = NULL; |
duke@435 | 32 | address VtableStub::_chunk_end = NULL; |
duke@435 | 33 | VMReg VtableStub::_receiver_location = VMRegImpl::Bad(); |
duke@435 | 34 | |
duke@435 | 35 | static int num_vtable_chunks = 0; |
duke@435 | 36 | |
duke@435 | 37 | |
duke@435 | 38 | void* VtableStub::operator new(size_t size, int code_size) { |
duke@435 | 39 | assert(size == sizeof(VtableStub), "mismatched size"); |
duke@435 | 40 | num_vtable_chunks++; |
duke@435 | 41 | // compute real VtableStub size (rounded to nearest word) |
duke@435 | 42 | const int real_size = round_to(code_size + sizeof(VtableStub), wordSize); |
duke@435 | 43 | // malloc them in chunks to minimize header overhead |
duke@435 | 44 | const int chunk_factor = 32; |
duke@435 | 45 | if (_chunk == NULL || _chunk + real_size > _chunk_end) { |
duke@435 | 46 | const int bytes = chunk_factor * real_size + pd_code_alignment(); |
duke@435 | 47 | BufferBlob* blob = BufferBlob::create("vtable chunks", bytes); |
duke@435 | 48 | if( blob == NULL ) vm_exit_out_of_memory1(bytes, "CodeCache: no room for %s", "vtable chunks"); |
duke@435 | 49 | _chunk = blob->instructions_begin(); |
duke@435 | 50 | _chunk_end = _chunk + bytes; |
duke@435 | 51 | VTune::register_stub("vtable stub", _chunk, _chunk_end); |
duke@435 | 52 | Forte::register_stub("vtable stub", _chunk, _chunk_end); |
duke@435 | 53 | // Notify JVMTI about this stub. The event will be recorded by the enclosing |
duke@435 | 54 | // JvmtiDynamicCodeEventCollector and posted when this thread has released |
duke@435 | 55 | // all locks. |
duke@435 | 56 | if (JvmtiExport::should_post_dynamic_code_generated()) { |
duke@435 | 57 | JvmtiExport::post_dynamic_code_generated_while_holding_locks("vtable stub", _chunk, _chunk_end); |
duke@435 | 58 | } |
duke@435 | 59 | align_chunk(); |
duke@435 | 60 | } |
duke@435 | 61 | assert(_chunk + real_size <= _chunk_end, "bad allocation"); |
duke@435 | 62 | void* res = _chunk; |
duke@435 | 63 | _chunk += real_size; |
duke@435 | 64 | align_chunk(); |
duke@435 | 65 | return res; |
duke@435 | 66 | } |
duke@435 | 67 | |
duke@435 | 68 | |
duke@435 | 69 | void VtableStub::print() { |
duke@435 | 70 | tty->print("vtable stub (index = %d, receiver_location = %d, code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)", |
duke@435 | 71 | index(), receiver_location(), code_begin(), code_end()); |
duke@435 | 72 | } |
duke@435 | 73 | |
duke@435 | 74 | |
duke@435 | 75 | // ----------------------------------------------------------------------------------------- |
duke@435 | 76 | // Implementation of VtableStubs |
duke@435 | 77 | // |
duke@435 | 78 | // For each hash value there's a linked list of vtable stubs (with that |
duke@435 | 79 | // hash value). Each list is anchored in a little hash _table, indexed |
duke@435 | 80 | // by that hash value. |
duke@435 | 81 | |
duke@435 | 82 | VtableStub* VtableStubs::_table[VtableStubs::N]; |
duke@435 | 83 | int VtableStubs::_number_of_vtable_stubs = 0; |
duke@435 | 84 | |
duke@435 | 85 | |
duke@435 | 86 | void VtableStubs::initialize() { |
duke@435 | 87 | VtableStub::_receiver_location = SharedRuntime::name_for_receiver(); |
duke@435 | 88 | { |
duke@435 | 89 | MutexLocker ml(VtableStubs_lock); |
duke@435 | 90 | assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once"); |
duke@435 | 91 | assert(is_power_of_2(N), "N must be a power of 2"); |
duke@435 | 92 | for (int i = 0; i < N; i++) { |
duke@435 | 93 | _table[i] = NULL; |
duke@435 | 94 | } |
duke@435 | 95 | } |
duke@435 | 96 | } |
duke@435 | 97 | |
duke@435 | 98 | |
duke@435 | 99 | address VtableStubs::create_stub(bool is_vtable_stub, int vtable_index, methodOop method) { |
duke@435 | 100 | assert(vtable_index >= 0, "must be positive"); |
duke@435 | 101 | |
duke@435 | 102 | VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL; |
duke@435 | 103 | if (s == NULL) { |
duke@435 | 104 | if (is_vtable_stub) { |
duke@435 | 105 | s = create_vtable_stub(vtable_index); |
duke@435 | 106 | } else { |
duke@435 | 107 | s = create_itable_stub(vtable_index); |
duke@435 | 108 | } |
duke@435 | 109 | enter(is_vtable_stub, vtable_index, s); |
duke@435 | 110 | if (PrintAdapterHandlers) { |
duke@435 | 111 | tty->print_cr("Decoding VtableStub %s[%d]@%d", |
duke@435 | 112 | is_vtable_stub? "vtbl": "itbl", vtable_index, VtableStub::receiver_location()); |
duke@435 | 113 | Disassembler::decode(s->code_begin(), s->code_end()); |
duke@435 | 114 | } |
duke@435 | 115 | } |
duke@435 | 116 | return s->entry_point(); |
duke@435 | 117 | } |
duke@435 | 118 | |
duke@435 | 119 | |
duke@435 | 120 | inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){ |
duke@435 | 121 | // Assumption: receiver_location < 4 in most cases. |
duke@435 | 122 | int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index; |
duke@435 | 123 | return (is_vtable_stub ? ~hash : hash) & mask; |
duke@435 | 124 | } |
duke@435 | 125 | |
duke@435 | 126 | |
duke@435 | 127 | VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) { |
duke@435 | 128 | MutexLocker ml(VtableStubs_lock); |
duke@435 | 129 | unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index); |
duke@435 | 130 | VtableStub* s = _table[hash]; |
duke@435 | 131 | while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next(); |
duke@435 | 132 | return s; |
duke@435 | 133 | } |
duke@435 | 134 | |
duke@435 | 135 | |
duke@435 | 136 | void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) { |
duke@435 | 137 | MutexLocker ml(VtableStubs_lock); |
duke@435 | 138 | assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub"); |
duke@435 | 139 | unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index); |
duke@435 | 140 | // enter s at the beginning of the corresponding list |
duke@435 | 141 | s->set_next(_table[h]); |
duke@435 | 142 | _table[h] = s; |
duke@435 | 143 | _number_of_vtable_stubs++; |
duke@435 | 144 | } |
duke@435 | 145 | |
duke@435 | 146 | |
duke@435 | 147 | bool VtableStubs::is_entry_point(address pc) { |
duke@435 | 148 | MutexLocker ml(VtableStubs_lock); |
duke@435 | 149 | VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset()); |
duke@435 | 150 | uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index()); |
duke@435 | 151 | VtableStub* s; |
duke@435 | 152 | for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {} |
duke@435 | 153 | return s == stub; |
duke@435 | 154 | } |
duke@435 | 155 | |
duke@435 | 156 | |
duke@435 | 157 | bool VtableStubs::contains(address pc) { |
duke@435 | 158 | // simple solution for now - we may want to use |
duke@435 | 159 | // a faster way if this function is called often |
duke@435 | 160 | return stub_containing(pc) != NULL; |
duke@435 | 161 | } |
duke@435 | 162 | |
duke@435 | 163 | |
duke@435 | 164 | VtableStub* VtableStubs::stub_containing(address pc) { |
duke@435 | 165 | // Note: No locking needed since any change to the data structure |
duke@435 | 166 | // happens with an atomic store into it (we don't care about |
duke@435 | 167 | // consistency with the _number_of_vtable_stubs counter). |
duke@435 | 168 | for (int i = 0; i < N; i++) { |
duke@435 | 169 | for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { |
duke@435 | 170 | if (s->contains(pc)) return s; |
duke@435 | 171 | } |
duke@435 | 172 | } |
duke@435 | 173 | return NULL; |
duke@435 | 174 | } |
duke@435 | 175 | |
duke@435 | 176 | void vtableStubs_init() { |
duke@435 | 177 | VtableStubs::initialize(); |
duke@435 | 178 | } |
duke@435 | 179 | |
duke@435 | 180 | |
duke@435 | 181 | //----------------------------------------------------------------------------------------------------- |
duke@435 | 182 | // Non-product code |
duke@435 | 183 | #ifndef PRODUCT |
duke@435 | 184 | |
duke@435 | 185 | extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) { |
duke@435 | 186 | ResourceMark rm; |
duke@435 | 187 | HandleMark hm; |
duke@435 | 188 | klassOop klass = receiver->klass(); |
duke@435 | 189 | instanceKlass* ik = instanceKlass::cast(klass); |
duke@435 | 190 | klassVtable* vt = ik->vtable(); |
duke@435 | 191 | klass->print(); |
duke@435 | 192 | fatal3("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", index %d (vtable length %d)", (address)receiver, index, vt->length()); |
duke@435 | 193 | } |
duke@435 | 194 | |
duke@435 | 195 | #endif // Product |