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