vtableStubs.cpp revision 1879:f95d63e2154a
1/*
2 * Copyright (c) 1997, 2010, 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/disassembler.hpp"
28#include "memory/allocation.inline.hpp"
29#include "memory/resourceArea.hpp"
30#include "oops/instanceKlass.hpp"
31#include "oops/klassVtable.hpp"
32#include "oops/oop.inline.hpp"
33#include "prims/forte.hpp"
34#include "prims/jvmtiExport.hpp"
35#include "runtime/handles.inline.hpp"
36#include "runtime/mutexLocker.hpp"
37#include "runtime/sharedRuntime.hpp"
38#ifdef COMPILER2
39#include "opto/matcher.hpp"
40#endif
41
42// -----------------------------------------------------------------------------------------
43// Implementation of VtableStub
44
45address VtableStub::_chunk             = NULL;
46address VtableStub::_chunk_end         = NULL;
47VMReg   VtableStub::_receiver_location = VMRegImpl::Bad();
48
49static int num_vtable_chunks = 0;
50
51
52void* VtableStub::operator new(size_t size, int code_size) {
53  assert(size == sizeof(VtableStub), "mismatched size");
54  num_vtable_chunks++;
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    BufferBlob* blob = BufferBlob::create("vtable chunks", bytes);
62    if (blob == NULL) {
63      vm_exit_out_of_memory(bytes, "CodeCache: no room for vtable chunks");
64    }
65    _chunk = blob->content_begin();
66    _chunk_end = _chunk + bytes;
67    Forte::register_stub("vtable stub", _chunk, _chunk_end);
68    // Notify JVMTI about this stub. The event will be recorded by the enclosing
69    // JvmtiDynamicCodeEventCollector and posted when this thread has released
70    // all locks.
71    if (JvmtiExport::should_post_dynamic_code_generated()) {
72      JvmtiExport::post_dynamic_code_generated_while_holding_locks("vtable stub", _chunk, _chunk_end);
73    }
74    align_chunk();
75  }
76  assert(_chunk + real_size <= _chunk_end, "bad allocation");
77  void* res = _chunk;
78  _chunk += real_size;
79  align_chunk();
80 return res;
81}
82
83
84void VtableStub::print_on(outputStream* st) const {
85  st->print("vtable stub (index = %d, receiver_location = %d, code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)",
86             index(), receiver_location(), code_begin(), code_end());
87}
88
89
90// -----------------------------------------------------------------------------------------
91// Implementation of VtableStubs
92//
93// For each hash value there's a linked list of vtable stubs (with that
94// hash value). Each list is anchored in a little hash _table, indexed
95// by that hash value.
96
97VtableStub* VtableStubs::_table[VtableStubs::N];
98int VtableStubs::_number_of_vtable_stubs = 0;
99
100
101void VtableStubs::initialize() {
102  VtableStub::_receiver_location = SharedRuntime::name_for_receiver();
103  {
104    MutexLocker ml(VtableStubs_lock);
105    assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once");
106    assert(is_power_of_2(N), "N must be a power of 2");
107    for (int i = 0; i < N; i++) {
108      _table[i] = NULL;
109    }
110  }
111}
112
113
114address VtableStubs::create_stub(bool is_vtable_stub, int vtable_index, methodOop method) {
115  assert(vtable_index >= 0, "must be positive");
116
117  VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL;
118  if (s == NULL) {
119    if (is_vtable_stub) {
120      s = create_vtable_stub(vtable_index);
121    } else {
122      s = create_itable_stub(vtable_index);
123    }
124    enter(is_vtable_stub, vtable_index, s);
125    if (PrintAdapterHandlers) {
126      tty->print_cr("Decoding VtableStub %s[%d]@%d",
127                    is_vtable_stub? "vtbl": "itbl", vtable_index, VtableStub::receiver_location());
128      Disassembler::decode(s->code_begin(), s->code_end());
129    }
130  }
131  return s->entry_point();
132}
133
134
135inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){
136  // Assumption: receiver_location < 4 in most cases.
137  int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index;
138  return (is_vtable_stub ? ~hash : hash)  & mask;
139}
140
141
142VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) {
143  MutexLocker ml(VtableStubs_lock);
144  unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index);
145  VtableStub* s = _table[hash];
146  while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next();
147  return s;
148}
149
150
151void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) {
152  MutexLocker ml(VtableStubs_lock);
153  assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub");
154  unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index);
155  // enter s at the beginning of the corresponding list
156  s->set_next(_table[h]);
157  _table[h] = s;
158  _number_of_vtable_stubs++;
159}
160
161
162bool VtableStubs::is_entry_point(address pc) {
163  MutexLocker ml(VtableStubs_lock);
164  VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset());
165  uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index());
166  VtableStub* s;
167  for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {}
168  return s == stub;
169}
170
171
172bool VtableStubs::contains(address pc) {
173  // simple solution for now - we may want to use
174  // a faster way if this function is called often
175  return stub_containing(pc) != NULL;
176}
177
178
179VtableStub* VtableStubs::stub_containing(address pc) {
180  // Note: No locking needed since any change to the data structure
181  //       happens with an atomic store into it (we don't care about
182  //       consistency with the _number_of_vtable_stubs counter).
183  for (int i = 0; i < N; i++) {
184    for (VtableStub* s = _table[i]; s != NULL; s = s->next()) {
185      if (s->contains(pc)) return s;
186    }
187  }
188  return NULL;
189}
190
191void vtableStubs_init() {
192  VtableStubs::initialize();
193}
194
195
196//-----------------------------------------------------------------------------------------------------
197// Non-product code
198#ifndef PRODUCT
199
200extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) {
201  ResourceMark rm;
202  HandleMark hm;
203  klassOop klass = receiver->klass();
204  instanceKlass* ik = instanceKlass::cast(klass);
205  klassVtable* vt = ik->vtable();
206  klass->print();
207  fatal(err_msg("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", "
208                "index %d (vtable length %d)",
209                (address)receiver, index, vt->length()));
210}
211
212#endif // Product
213