src/share/vm/code/vtableStubs.cpp

Thu, 06 Feb 2014 20:13:38 -0800

author
sspitsyn
date
Thu, 06 Feb 2014 20:13:38 -0800
changeset 6310
22b3b2f888bc
parent 5762
891687731b59
child 6680
78bbf4d43a14
permissions
-rw-r--r--

8025841: JVMTI: "vtable stub" dynamic code notification is misplaced
Summary: Generate correct "vtable stub" dynamic code notifications
Reviewed-by: sspitsyn, kvn, coleenp
Contributed-by: oleg.mazurov@oracle.com

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

mercurial