src/share/vm/code/vtableStubs.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 6876
710a3c8b516e
child 9203
53eec13fbaa5
permissions
-rw-r--r--

Merge

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

mercurial