aoqi@0: /* poonam@9185: * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "code/vtableStubs.hpp" aoqi@0: #include "compiler/disassembler.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/instanceKlass.hpp" aoqi@0: #include "oops/klassVtable.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "prims/forte.hpp" aoqi@0: #include "prims/jvmtiExport.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "runtime/sharedRuntime.hpp" aoqi@0: #ifdef COMPILER2 aoqi@0: #include "opto/matcher.hpp" aoqi@0: #endif aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: // ----------------------------------------------------------------------------------------- aoqi@0: // Implementation of VtableStub aoqi@0: aoqi@0: address VtableStub::_chunk = NULL; aoqi@0: address VtableStub::_chunk_end = NULL; aoqi@0: VMReg VtableStub::_receiver_location = VMRegImpl::Bad(); aoqi@0: aoqi@0: aoqi@0: void* VtableStub::operator new(size_t size, int code_size) throw() { aoqi@0: assert(size == sizeof(VtableStub), "mismatched size"); aoqi@0: // compute real VtableStub size (rounded to nearest word) aoqi@0: const int real_size = round_to(code_size + sizeof(VtableStub), wordSize); aoqi@0: // malloc them in chunks to minimize header overhead aoqi@0: const int chunk_factor = 32; aoqi@0: if (_chunk == NULL || _chunk + real_size > _chunk_end) { aoqi@0: const int bytes = chunk_factor * real_size + pd_code_alignment(); aoqi@0: aoqi@0: // There is a dependency on the name of the blob in src/share/vm/prims/jvmtiCodeBlobEvents.cpp aoqi@0: // If changing the name, update the other file accordingly. poonam@9185: VtableBlob* blob = VtableBlob::create("vtable chunks", bytes); aoqi@0: if (blob == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: _chunk = blob->content_begin(); aoqi@0: _chunk_end = _chunk + bytes; aoqi@0: Forte::register_stub("vtable stub", _chunk, _chunk_end); aoqi@0: align_chunk(); aoqi@0: } aoqi@0: assert(_chunk + real_size <= _chunk_end, "bad allocation"); aoqi@0: void* res = _chunk; aoqi@0: _chunk += real_size; aoqi@0: align_chunk(); aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void VtableStub::print_on(outputStream* st) const { aoqi@0: st->print("vtable stub (index = %d, receiver_location = %d, code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)", aoqi@0: index(), receiver_location(), code_begin(), code_end()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ----------------------------------------------------------------------------------------- aoqi@0: // Implementation of VtableStubs aoqi@0: // aoqi@0: // For each hash value there's a linked list of vtable stubs (with that aoqi@0: // hash value). Each list is anchored in a little hash _table, indexed aoqi@0: // by that hash value. aoqi@0: aoqi@0: VtableStub* VtableStubs::_table[VtableStubs::N]; aoqi@0: int VtableStubs::_number_of_vtable_stubs = 0; aoqi@0: aoqi@0: aoqi@0: void VtableStubs::initialize() { aoqi@0: VtableStub::_receiver_location = SharedRuntime::name_for_receiver(); aoqi@0: { aoqi@0: MutexLocker ml(VtableStubs_lock); aoqi@0: assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once"); aoqi@0: assert(is_power_of_2(N), "N must be a power of 2"); aoqi@0: for (int i = 0; i < N; i++) { aoqi@0: _table[i] = NULL; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address VtableStubs::find_stub(bool is_vtable_stub, int vtable_index) { aoqi@0: assert(vtable_index >= 0, "must be positive"); aoqi@0: aoqi@0: VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL; aoqi@0: if (s == NULL) { aoqi@0: if (is_vtable_stub) { aoqi@0: s = create_vtable_stub(vtable_index); aoqi@0: } else { aoqi@0: s = create_itable_stub(vtable_index); aoqi@0: } aoqi@0: aoqi@0: // Creation of vtable or itable can fail if there is not enough free space in the code cache. aoqi@0: if (s == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: enter(is_vtable_stub, vtable_index, s); aoqi@0: if (PrintAdapterHandlers) { aoqi@0: tty->print_cr("Decoding VtableStub %s[%d]@%d", aoqi@0: is_vtable_stub? "vtbl": "itbl", vtable_index, VtableStub::receiver_location()); aoqi@0: Disassembler::decode(s->code_begin(), s->code_end()); aoqi@0: } aoqi@0: // Notify JVMTI about this stub. The event will be recorded by the enclosing aoqi@0: // JvmtiDynamicCodeEventCollector and posted when this thread has released aoqi@0: // all locks. aoqi@0: if (JvmtiExport::should_post_dynamic_code_generated()) { aoqi@0: JvmtiExport::post_dynamic_code_generated_while_holding_locks(is_vtable_stub? "vtable stub": "itable stub", aoqi@0: s->code_begin(), s->code_end()); aoqi@0: } aoqi@0: } aoqi@0: return s->entry_point(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){ aoqi@0: // Assumption: receiver_location < 4 in most cases. aoqi@0: int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index; aoqi@0: return (is_vtable_stub ? ~hash : hash) & mask; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) { aoqi@0: MutexLocker ml(VtableStubs_lock); aoqi@0: unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index); aoqi@0: VtableStub* s = _table[hash]; aoqi@0: while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next(); aoqi@0: return s; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) { aoqi@0: MutexLocker ml(VtableStubs_lock); aoqi@0: assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub"); aoqi@0: unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index); aoqi@0: // enter s at the beginning of the corresponding list aoqi@0: s->set_next(_table[h]); aoqi@0: _table[h] = s; aoqi@0: _number_of_vtable_stubs++; aoqi@0: } aoqi@0: poonam@9185: VtableStub* VtableStubs::entry_point(address pc) { aoqi@0: MutexLocker ml(VtableStubs_lock); aoqi@0: VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset()); aoqi@0: uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index()); aoqi@0: VtableStub* s; aoqi@0: for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {} poonam@9185: if (s == stub) { poonam@9185: return s; poonam@9185: } poonam@9185: return NULL; aoqi@0: } aoqi@0: aoqi@0: bool VtableStubs::contains(address pc) { aoqi@0: // simple solution for now - we may want to use aoqi@0: // a faster way if this function is called often aoqi@0: return stub_containing(pc) != NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: VtableStub* VtableStubs::stub_containing(address pc) { aoqi@0: // Note: No locking needed since any change to the data structure aoqi@0: // happens with an atomic store into it (we don't care about aoqi@0: // consistency with the _number_of_vtable_stubs counter). aoqi@0: for (int i = 0; i < N; i++) { aoqi@0: for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { aoqi@0: if (s->contains(pc)) return s; aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: void vtableStubs_init() { aoqi@0: VtableStubs::initialize(); aoqi@0: } aoqi@0: aoqi@0: void VtableStubs::vtable_stub_do(void f(VtableStub*)) { aoqi@0: for (int i = 0; i < N; i++) { aoqi@0: for (VtableStub* s = _table[i]; s != NULL; s = s->next()) { aoqi@0: f(s); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //----------------------------------------------------------------------------------------------------- aoqi@0: // Non-product code aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) { aoqi@0: ResourceMark rm; aoqi@0: HandleMark hm; aoqi@0: Klass* klass = receiver->klass(); aoqi@0: InstanceKlass* ik = InstanceKlass::cast(klass); aoqi@0: klassVtable* vt = ik->vtable(); aoqi@0: ik->print(); aoqi@0: fatal(err_msg("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", " aoqi@0: "index %d (vtable length %d)", aoqi@0: (address)receiver, index, vt->length())); aoqi@0: } aoqi@0: aoqi@0: #endif // Product