src/share/vm/code/icBuffer.cpp

Thu, 08 Oct 2015 09:37:23 +0200

author
thartmann
date
Thu, 08 Oct 2015 09:37:23 +0200
changeset 8073
682119c4c32e
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
permissions
-rw-r--r--

8058737: CodeCache::find_blob fails with 'unsafe access to zombie method'
Summary: Remove active ICStubs from zombie nmethods
Reviewed-by: kvn, iveresov

duke@435 1 /*
drchase@6680 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
twisti@4318 26 #include "code/codeCache.hpp"
stefank@2314 27 #include "code/compiledIC.hpp"
stefank@2314 28 #include "code/icBuffer.hpp"
stefank@2314 29 #include "code/nmethod.hpp"
stefank@2314 30 #include "code/scopeDesc.hpp"
stefank@2314 31 #include "gc_interface/collectedHeap.inline.hpp"
stefank@2314 32 #include "interpreter/interpreter.hpp"
stefank@2314 33 #include "interpreter/linkResolver.hpp"
stefank@2314 34 #include "memory/resourceArea.hpp"
stefank@2314 35 #include "memory/universe.inline.hpp"
coleenp@4037 36 #include "oops/method.hpp"
stefank@2314 37 #include "oops/oop.inline.hpp"
stefank@2314 38 #include "oops/oop.inline2.hpp"
stefank@2314 39 #include "runtime/mutexLocker.hpp"
stefank@2314 40 #include "runtime/stubRoutines.hpp"
duke@435 41
drchase@6680 42 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
duke@435 43
duke@435 44 DEF_STUB_INTERFACE(ICStub);
duke@435 45
duke@435 46 StubQueue* InlineCacheBuffer::_buffer = NULL;
duke@435 47 ICStub* InlineCacheBuffer::_next_stub = NULL;
duke@435 48
coleenp@4037 49 CompiledICHolder* InlineCacheBuffer::_pending_released = NULL;
coleenp@4037 50 int InlineCacheBuffer::_pending_count = 0;
duke@435 51
duke@435 52 void ICStub::finalize() {
duke@435 53 if (!is_empty()) {
duke@435 54 ResourceMark rm;
coleenp@4037 55 CompiledIC *ic = CompiledIC_at(CodeCache::find_nmethod(ic_site()), ic_site());
duke@435 56 assert(CodeCache::find_nmethod(ic->instruction_address()) != NULL, "inline cache in non-nmethod?");
duke@435 57
duke@435 58 assert(this == ICStub_from_destination_address(ic->stub_address()), "wrong owner of ic buffer");
coleenp@4037 59 ic->set_ic_destination_and_value(destination(), cached_value());
duke@435 60 }
duke@435 61 }
duke@435 62
duke@435 63
duke@435 64 address ICStub::destination() const {
duke@435 65 return InlineCacheBuffer::ic_buffer_entry_point(code_begin());
duke@435 66 }
duke@435 67
coleenp@4037 68 void* ICStub::cached_value() const {
coleenp@4037 69 return InlineCacheBuffer::ic_buffer_cached_value(code_begin());
duke@435 70 }
duke@435 71
duke@435 72
coleenp@4037 73 void ICStub::set_stub(CompiledIC *ic, void* cached_val, address dest_addr) {
duke@435 74 // We cannot store a pointer to the 'ic' object, since it is resource allocated. Instead we
duke@435 75 // store the location of the inline cache. Then we have enough information recreate the CompiledIC
duke@435 76 // object when we need to remove the stub.
duke@435 77 _ic_site = ic->instruction_address();
duke@435 78
duke@435 79 // Assemble new stub
coleenp@4037 80 InlineCacheBuffer::assemble_ic_buffer_code(code_begin(), cached_val, dest_addr);
duke@435 81 assert(destination() == dest_addr, "can recover destination");
coleenp@4037 82 assert(cached_value() == cached_val, "can recover destination");
duke@435 83 }
duke@435 84
duke@435 85
duke@435 86 void ICStub::clear() {
coleenp@4037 87 if (CompiledIC::is_icholder_entry(destination())) {
coleenp@4037 88 InlineCacheBuffer::queue_for_release((CompiledICHolder*)cached_value());
coleenp@4037 89 }
duke@435 90 _ic_site = NULL;
duke@435 91 }
duke@435 92
duke@435 93
duke@435 94 #ifndef PRODUCT
duke@435 95 // anybody calling to this stub will trap
duke@435 96
duke@435 97 void ICStub::verify() {
duke@435 98 }
duke@435 99
duke@435 100 void ICStub::print() {
duke@435 101 tty->print_cr("ICStub: site: " INTPTR_FORMAT, _ic_site);
duke@435 102 }
duke@435 103 #endif
duke@435 104
duke@435 105 //-----------------------------------------------------------------------------------------------
duke@435 106 // Implementation of InlineCacheBuffer
duke@435 107
duke@435 108 void InlineCacheBuffer::init_next_stub() {
duke@435 109 ICStub* ic_stub = (ICStub*)buffer()->request_committed (ic_stub_code_size());
duke@435 110 assert (ic_stub != NULL, "no room for a single stub");
duke@435 111 set_next_stub(ic_stub);
duke@435 112 }
duke@435 113
duke@435 114 void InlineCacheBuffer::initialize() {
duke@435 115 if (_buffer != NULL) return; // already initialized
duke@435 116 _buffer = new StubQueue(new ICStubInterface, 10*K, InlineCacheBuffer_lock, "InlineCacheBuffer");
duke@435 117 assert (_buffer != NULL, "cannot allocate InlineCacheBuffer");
duke@435 118 init_next_stub();
duke@435 119 }
duke@435 120
duke@435 121
duke@435 122 ICStub* InlineCacheBuffer::new_ic_stub() {
duke@435 123 while (true) {
duke@435 124 ICStub* ic_stub = (ICStub*)buffer()->request_committed(ic_stub_code_size());
duke@435 125 if (ic_stub != NULL) {
duke@435 126 return ic_stub;
duke@435 127 }
duke@435 128 // we ran out of inline cache buffer space; must enter safepoint.
duke@435 129 // We do this by forcing a safepoint
duke@435 130 EXCEPTION_MARK;
duke@435 131
duke@435 132 VM_ForceSafepoint vfs;
duke@435 133 VMThread::execute(&vfs);
duke@435 134 // We could potential get an async. exception at this point.
duke@435 135 // In that case we will rethrow it to ourselvs.
duke@435 136 if (HAS_PENDING_EXCEPTION) {
duke@435 137 oop exception = PENDING_EXCEPTION;
duke@435 138 CLEAR_PENDING_EXCEPTION;
duke@435 139 Thread::send_async_exception(JavaThread::current()->threadObj(), exception);
duke@435 140 }
duke@435 141 }
duke@435 142 ShouldNotReachHere();
duke@435 143 return NULL;
duke@435 144 }
duke@435 145
duke@435 146
duke@435 147 void InlineCacheBuffer::update_inline_caches() {
duke@435 148 if (buffer()->number_of_stubs() > 1) {
duke@435 149 if (TraceICBuffer) {
duke@435 150 tty->print_cr("[updating inline caches with %d stubs]", buffer()->number_of_stubs());
duke@435 151 }
duke@435 152 buffer()->remove_all();
duke@435 153 init_next_stub();
duke@435 154 }
coleenp@4037 155 release_pending_icholders();
duke@435 156 }
duke@435 157
duke@435 158
duke@435 159 bool InlineCacheBuffer::contains(address instruction_address) {
duke@435 160 return buffer()->contains(instruction_address);
duke@435 161 }
duke@435 162
duke@435 163
duke@435 164 bool InlineCacheBuffer::is_empty() {
duke@435 165 return buffer()->number_of_stubs() == 1; // always has sentinel
duke@435 166 }
duke@435 167
duke@435 168
duke@435 169 void InlineCacheBuffer_init() {
duke@435 170 InlineCacheBuffer::initialize();
duke@435 171 }
duke@435 172
duke@435 173
coleenp@4037 174 void InlineCacheBuffer::create_transition_stub(CompiledIC *ic, void* cached_value, address entry) {
duke@435 175 assert(!SafepointSynchronize::is_at_safepoint(), "should not be called during a safepoint");
duke@435 176 assert (CompiledIC_lock->is_locked(), "");
coleenp@4037 177 if (TraceICBuffer) {
coleenp@4037 178 tty->print_cr(" create transition stub for " INTPTR_FORMAT " destination " INTPTR_FORMAT " cached value " INTPTR_FORMAT,
coleenp@4037 179 ic->instruction_address(), entry, cached_value);
coleenp@4037 180 }
duke@435 181
duke@435 182 // If an transition stub is already associate with the inline cache, then we remove the association.
duke@435 183 if (ic->is_in_transition_state()) {
duke@435 184 ICStub* old_stub = ICStub_from_destination_address(ic->stub_address());
duke@435 185 old_stub->clear();
duke@435 186 }
duke@435 187
duke@435 188 // allocate and initialize new "out-of-line" inline-cache
duke@435 189 ICStub* ic_stub = get_next_stub();
coleenp@4037 190 ic_stub->set_stub(ic, cached_value, entry);
duke@435 191
duke@435 192 // Update inline cache in nmethod to point to new "out-of-line" allocated inline cache
coleenp@4037 193 ic->set_ic_destination(ic_stub);
duke@435 194
duke@435 195 set_next_stub(new_ic_stub()); // can cause safepoint synchronization
duke@435 196 }
duke@435 197
duke@435 198
duke@435 199 address InlineCacheBuffer::ic_destination_for(CompiledIC *ic) {
duke@435 200 ICStub* stub = ICStub_from_destination_address(ic->stub_address());
duke@435 201 return stub->destination();
duke@435 202 }
duke@435 203
duke@435 204
coleenp@4037 205 void* InlineCacheBuffer::cached_value_for(CompiledIC *ic) {
duke@435 206 ICStub* stub = ICStub_from_destination_address(ic->stub_address());
coleenp@4037 207 return stub->cached_value();
duke@435 208 }
coleenp@4037 209
coleenp@4037 210
coleenp@4037 211 // Free CompiledICHolder*s that are no longer in use
coleenp@4037 212 void InlineCacheBuffer::release_pending_icholders() {
coleenp@4037 213 assert(SafepointSynchronize::is_at_safepoint(), "should only be called during a safepoint");
coleenp@4037 214 CompiledICHolder* holder = _pending_released;
coleenp@4037 215 _pending_released = NULL;
coleenp@4037 216 while (holder != NULL) {
coleenp@4037 217 CompiledICHolder* next = holder->next();
coleenp@4037 218 delete holder;
coleenp@4037 219 holder = next;
coleenp@4037 220 _pending_count--;
coleenp@4037 221 }
coleenp@4037 222 assert(_pending_count == 0, "wrong count");
coleenp@4037 223 }
coleenp@4037 224
coleenp@4037 225 // Enqueue this icholder for release during the next safepoint. It's
coleenp@4037 226 // not safe to free them until them since they might be visible to
coleenp@4037 227 // another thread.
coleenp@4037 228 void InlineCacheBuffer::queue_for_release(CompiledICHolder* icholder) {
coleenp@4037 229 MutexLockerEx mex(InlineCacheBuffer_lock);
coleenp@4037 230 icholder->set_next(_pending_released);
coleenp@4037 231 _pending_released = icholder;
coleenp@4037 232 _pending_count++;
coleenp@4037 233 if (TraceICBuffer) {
coleenp@4037 234 tty->print_cr("enqueueing icholder " INTPTR_FORMAT " to be freed", icholder);
coleenp@4037 235 }
coleenp@4037 236 }

mercurial