1 /* 2 * Copyright (c) 2013, 2016, 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 "classfile/metadataOnStackMark.hpp" 27 #include "code/codeCache.hpp" 28 #include "compiler/compileBroker.hpp" 29 #include "oops/metadata.hpp" 30 #include "prims/jvmtiImpl.hpp" 31 #include "runtime/synchronizer.hpp" 32 #include "runtime/thread.hpp" 33 #include "services/threadService.hpp" 34 #include "utilities/chunkedList.hpp" 35 #if INCLUDE_JVMCI 36 #include "jvmci/jvmciRuntime.hpp" 37 #endif 38 39 MetadataOnStackBuffer* MetadataOnStackMark::_used_buffers = NULL; 40 MetadataOnStackBuffer* MetadataOnStackMark::_free_buffers = NULL; 41 42 MetadataOnStackBuffer* MetadataOnStackMark::_current_buffer = NULL; 43 NOT_PRODUCT(bool MetadataOnStackMark::_is_active = false;) 44 45 // Walk metadata on the stack and mark it so that redefinition doesn't delete 46 // it. Class unloading only deletes in-error class files, methods created by 47 // the relocator and dummy constant pools. None of these appear anywhere except 48 // in metadata Handles. 49 MetadataOnStackMark::MetadataOnStackMark(bool redefinition_walk) { 50 assert(_used_buffers == NULL, "sanity check"); 51 assert(!_is_active, "MetadataOnStackMarks do not nest"); 52 NOT_PRODUCT(_is_active = true;) 53 54 if (!UseZGC) { 55 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 56 Threads::metadata_handles_do(Metadata::mark_on_stack); 57 } 58 59 if (redefinition_walk) { 60 Threads::metadata_do(Metadata::mark_on_stack); 61 CodeCache::metadata_do(Metadata::mark_on_stack); 62 CompileBroker::mark_on_stack(); 63 JvmtiCurrentBreakpoints::metadata_do(Metadata::mark_on_stack); 64 ThreadService::metadata_do(Metadata::mark_on_stack); 65 #if INCLUDE_JVMCI 66 JVMCIRuntime::metadata_do(Metadata::mark_on_stack); 67 #endif 68 } 69 } 70 71 MetadataOnStackMark::~MetadataOnStackMark() { 72 assert(SafepointSynchronize::is_at_safepoint() || UseZGC, "sanity check"); 73 // Unmark everything that was marked. Can't do the same walk because 74 // redefine classes messes up the code cache so the set of methods 75 // might not be the same. 76 retire_current_buffer(); 77 78 MetadataOnStackBuffer* buffer = _used_buffers; 79 while (buffer != NULL) { 80 // Clear on stack state for all metadata. 81 size_t size = buffer->size(); 82 for (size_t i = 0; i < size; i++) { 83 Metadata* md = buffer->at(i); 84 md->set_on_stack(false); 85 } 86 87 MetadataOnStackBuffer* next = buffer->next_used(); 88 89 // Move the buffer to the free list. 90 buffer->clear(); 91 buffer->set_next_used(NULL); 92 buffer->set_next_free(_free_buffers); 93 _free_buffers = buffer; 94 95 // Step to next used buffer. 96 buffer = next; 97 } 98 99 _used_buffers = NULL; 100 101 NOT_PRODUCT(_is_active = false;) 102 } 103 104 void MetadataOnStackMark::retire_buffer(MetadataOnStackBuffer* buffer) { 105 if (buffer == NULL) { 106 return; 107 } 108 buffer->set_next_used(_used_buffers); 109 _used_buffers = buffer; 110 } 111 112 // Current buffer is full or we're ready to walk them, add it to the used list. 113 void MetadataOnStackMark::retire_current_buffer() { 114 retire_buffer(_current_buffer); 115 _current_buffer = NULL; 116 } 117 118 // Get buffer off free list. 119 MetadataOnStackBuffer* MetadataOnStackMark::allocate_buffer() { 120 MetadataOnStackBuffer* allocated = _free_buffers; 121 122 if (allocated != NULL) { 123 _free_buffers = allocated->next_free(); 124 } 125 126 if (allocated == NULL) { 127 allocated = new MetadataOnStackBuffer(); 128 } 129 130 assert(!allocated->is_full(), "Should not be full: " PTR_FORMAT, p2i(allocated)); 131 132 return allocated; 133 } 134 135 // Record which objects are marked so we can unmark the same objects. 136 void MetadataOnStackMark::record(Metadata* m) { 137 assert(_is_active, "metadata on stack marking is active"); 138 139 MetadataOnStackBuffer* buffer = _current_buffer; 140 141 if (buffer != NULL && buffer->is_full()) { 142 retire_buffer(buffer); 143 buffer = NULL; 144 } 145 146 if (buffer == NULL) { 147 buffer = allocate_buffer(); 148 _current_buffer = buffer; 149 } 150 151 buffer->push(m); 152 }