Fri, 08 Nov 2013 01:13:11 -0800
8023037: Race between ciEnv::register_method and nmethod::make_not_entrant_or_zombie
Reviewed-by: kvn, iveresov
1 /*
2 * Copyright (c) 1997, 2013, 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 */
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
42 // -----------------------------------------------------------------------------------------
43 // Implementation of VtableStub
45 address VtableStub::_chunk = NULL;
46 address VtableStub::_chunk_end = NULL;
47 VMReg VtableStub::_receiver_location = VMRegImpl::Bad();
50 void* VtableStub::operator new(size_t size, int code_size) throw() {
51 assert(size == sizeof(VtableStub), "mismatched size");
52 // compute real VtableStub size (rounded to nearest word)
53 const int real_size = round_to(code_size + sizeof(VtableStub), wordSize);
54 // malloc them in chunks to minimize header overhead
55 const int chunk_factor = 32;
56 if (_chunk == NULL || _chunk + real_size > _chunk_end) {
57 const int bytes = chunk_factor * real_size + pd_code_alignment();
58 BufferBlob* blob = BufferBlob::create("vtable chunks", bytes);
59 if (blob == NULL) {
60 return NULL;
61 }
62 _chunk = blob->content_begin();
63 _chunk_end = _chunk + bytes;
64 Forte::register_stub("vtable stub", _chunk, _chunk_end);
65 // Notify JVMTI about this stub. The event will be recorded by the enclosing
66 // JvmtiDynamicCodeEventCollector and posted when this thread has released
67 // all locks.
68 if (JvmtiExport::should_post_dynamic_code_generated()) {
69 JvmtiExport::post_dynamic_code_generated_while_holding_locks("vtable stub", _chunk, _chunk_end);
70 }
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 }
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 }
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.
94 VtableStub* VtableStubs::_table[VtableStubs::N];
95 int VtableStubs::_number_of_vtable_stubs = 0;
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 }
111 address VtableStubs::find_stub(bool is_vtable_stub, int vtable_index) {
112 assert(vtable_index >= 0, "must be positive");
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 }
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 }
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 }
134 return s->entry_point();
135 }
138 inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){
139 // Assumption: receiver_location < 4 in most cases.
140 int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index;
141 return (is_vtable_stub ? ~hash : hash) & mask;
142 }
145 VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) {
146 MutexLocker ml(VtableStubs_lock);
147 unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index);
148 VtableStub* s = _table[hash];
149 while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next();
150 return s;
151 }
154 void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) {
155 MutexLocker ml(VtableStubs_lock);
156 assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub");
157 unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index);
158 // enter s at the beginning of the corresponding list
159 s->set_next(_table[h]);
160 _table[h] = s;
161 _number_of_vtable_stubs++;
162 }
165 bool VtableStubs::is_entry_point(address pc) {
166 MutexLocker ml(VtableStubs_lock);
167 VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset());
168 uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index());
169 VtableStub* s;
170 for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {}
171 return s == stub;
172 }
175 bool VtableStubs::contains(address pc) {
176 // simple solution for now - we may want to use
177 // a faster way if this function is called often
178 return stub_containing(pc) != NULL;
179 }
182 VtableStub* VtableStubs::stub_containing(address pc) {
183 // Note: No locking needed since any change to the data structure
184 // happens with an atomic store into it (we don't care about
185 // consistency with the _number_of_vtable_stubs counter).
186 for (int i = 0; i < N; i++) {
187 for (VtableStub* s = _table[i]; s != NULL; s = s->next()) {
188 if (s->contains(pc)) return s;
189 }
190 }
191 return NULL;
192 }
194 void vtableStubs_init() {
195 VtableStubs::initialize();
196 }
199 //-----------------------------------------------------------------------------------------------------
200 // Non-product code
201 #ifndef PRODUCT
203 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) {
204 ResourceMark rm;
205 HandleMark hm;
206 Klass* klass = receiver->klass();
207 InstanceKlass* ik = InstanceKlass::cast(klass);
208 klassVtable* vt = ik->vtable();
209 ik->print();
210 fatal(err_msg("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", "
211 "index %d (vtable length %d)",
212 (address)receiver, index, vt->length()));
213 }
215 #endif // Product