Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/vtableStubs.cpp
32285 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "code/vtableStubs.hpp"26#include "compiler/disassembler.hpp"27#include "memory/allocation.inline.hpp"28#include "memory/resourceArea.hpp"29#include "oops/instanceKlass.hpp"30#include "oops/klassVtable.hpp"31#include "oops/oop.inline.hpp"32#include "prims/forte.hpp"33#include "prims/jvmtiExport.hpp"34#include "runtime/handles.inline.hpp"35#include "runtime/mutexLocker.hpp"36#include "runtime/sharedRuntime.hpp"37#ifdef COMPILER238#include "opto/matcher.hpp"39#endif4041PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC4243// -----------------------------------------------------------------------------------------44// Implementation of VtableStub4546address VtableStub::_chunk = NULL;47address VtableStub::_chunk_end = NULL;48VMReg VtableStub::_receiver_location = VMRegImpl::Bad();495051void* VtableStub::operator new(size_t size, int code_size) throw() {52assert(size == sizeof(VtableStub), "mismatched size");53// compute real VtableStub size (rounded to nearest word)54const int real_size = round_to(code_size + sizeof(VtableStub), wordSize);55// malloc them in chunks to minimize header overhead56const int chunk_factor = 32;57if (_chunk == NULL || _chunk + real_size > _chunk_end) {58const int bytes = chunk_factor * real_size + pd_code_alignment();5960// There is a dependency on the name of the blob in src/share/vm/prims/jvmtiCodeBlobEvents.cpp61// If changing the name, update the other file accordingly.62VtableBlob* blob = VtableBlob::create("vtable chunks", bytes);63if (blob == NULL) {64return NULL;65}66_chunk = blob->content_begin();67_chunk_end = _chunk + bytes;68Forte::register_stub("vtable stub", _chunk, _chunk_end);69align_chunk();70}71assert(_chunk + real_size <= _chunk_end, "bad allocation");72void* res = _chunk;73_chunk += real_size;74align_chunk();75return res;76}777879void VtableStub::print_on(outputStream* st) const {80st->print("vtable stub (index = %d, receiver_location = %d, code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)",81index(), receiver_location(), code_begin(), code_end());82}838485// -----------------------------------------------------------------------------------------86// Implementation of VtableStubs87//88// For each hash value there's a linked list of vtable stubs (with that89// hash value). Each list is anchored in a little hash _table, indexed90// by that hash value.9192VtableStub* VtableStubs::_table[VtableStubs::N];93int VtableStubs::_number_of_vtable_stubs = 0;949596void VtableStubs::initialize() {97VtableStub::_receiver_location = SharedRuntime::name_for_receiver();98{99MutexLocker ml(VtableStubs_lock);100assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once");101assert(is_power_of_2(N), "N must be a power of 2");102for (int i = 0; i < N; i++) {103_table[i] = NULL;104}105}106}107108109address VtableStubs::find_stub(bool is_vtable_stub, int vtable_index) {110assert(vtable_index >= 0, "must be positive");111112VtableStub* s = lookup(is_vtable_stub, vtable_index);113if (s == NULL) {114if (is_vtable_stub) {115s = create_vtable_stub(vtable_index);116} else {117s = create_itable_stub(vtable_index);118}119120// Creation of vtable or itable can fail if there is not enough free space in the code cache.121if (s == NULL) {122return NULL;123}124125enter(is_vtable_stub, vtable_index, s);126if (PrintAdapterHandlers) {127tty->print_cr("Decoding VtableStub %s[%d]@%d",128is_vtable_stub? "vtbl": "itbl", vtable_index, VtableStub::receiver_location());129Disassembler::decode(s->code_begin(), s->code_end());130}131// Notify JVMTI about this stub. The event will be recorded by the enclosing132// JvmtiDynamicCodeEventCollector and posted when this thread has released133// all locks.134if (JvmtiExport::should_post_dynamic_code_generated()) {135JvmtiExport::post_dynamic_code_generated_while_holding_locks(is_vtable_stub? "vtable stub": "itable stub",136s->code_begin(), s->code_end());137}138}139return s->entry_point();140}141142143inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){144// Assumption: receiver_location < 4 in most cases.145int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index;146return (is_vtable_stub ? ~hash : hash) & mask;147}148149150VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) {151MutexLocker ml(VtableStubs_lock);152unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index);153VtableStub* s = _table[hash];154while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next();155return s;156}157158159void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) {160MutexLocker ml(VtableStubs_lock);161assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub");162unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index);163// enter s at the beginning of the corresponding list164s->set_next(_table[h]);165_table[h] = s;166_number_of_vtable_stubs++;167}168169VtableStub* VtableStubs::entry_point(address pc) {170MutexLocker ml(VtableStubs_lock);171VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset());172uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index());173VtableStub* s;174for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {}175if (s == stub) {176return s;177}178return NULL;179}180181bool VtableStubs::contains(address pc) {182// simple solution for now - we may want to use183// a faster way if this function is called often184return stub_containing(pc) != NULL;185}186187188VtableStub* VtableStubs::stub_containing(address pc) {189// Note: No locking needed since any change to the data structure190// happens with an atomic store into it (we don't care about191// consistency with the _number_of_vtable_stubs counter).192for (int i = 0; i < N; i++) {193for (VtableStub* s = _table[i]; s != NULL; s = s->next()) {194if (s->contains(pc)) return s;195}196}197return NULL;198}199200void vtableStubs_init() {201VtableStubs::initialize();202}203204void VtableStubs::vtable_stub_do(void f(VtableStub*)) {205for (int i = 0; i < N; i++) {206for (VtableStub* s = _table[i]; s != NULL; s = s->next()) {207f(s);208}209}210}211212213//-----------------------------------------------------------------------------------------------------214// Non-product code215#ifndef PRODUCT216217extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) {218ResourceMark rm;219HandleMark hm;220Klass* klass = receiver->klass();221InstanceKlass* ik = InstanceKlass::cast(klass);222klassVtable* vt = ik->vtable();223ik->print();224fatal(err_msg("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", "225"index %d (vtable length %d)",226(address)receiver, index, vt->length()));227}228229#endif // Product230231232