1 /* 2 * Copyright (c) 2022, Red Hat, Inc. All rights reserved. 3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. 4 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 * 25 */ 26 27 #include "precompiled.hpp" 28 #include "memory/allocation.hpp" 29 #include "runtime/globals.hpp" 30 #include "runtime/lockStack.inline.hpp" 31 #include "runtime/safepoint.hpp" 32 #include "runtime/stackWatermark.hpp" 33 #include "runtime/stackWatermarkSet.inline.hpp" 34 #include "runtime/thread.hpp" 35 #include "utilities/copy.hpp" 36 #include "utilities/debug.hpp" 37 #include "utilities/globalDefinitions.hpp" 38 #include "utilities/ostream.hpp" 39 40 #include <type_traits> 41 42 const int LockStack::lock_stack_offset = in_bytes(JavaThread::lock_stack_offset()); 43 const int LockStack::lock_stack_top_offset = in_bytes(JavaThread::lock_stack_top_offset()); 44 const int LockStack::lock_stack_base_offset = in_bytes(JavaThread::lock_stack_base_offset()); 45 46 LockStack::LockStack(JavaThread* jt) : 47 _top(lock_stack_base_offset), _base() { 48 // Make sure the layout of the object is compatible with the emitted code's assumptions. 49 STATIC_ASSERT(sizeof(_bad_oop_sentinel) == oopSize); 50 STATIC_ASSERT(sizeof(_base[0]) == oopSize); 51 STATIC_ASSERT(std::is_standard_layout<LockStack>::value); 52 STATIC_ASSERT(offsetof(LockStack, _bad_oop_sentinel) == offsetof(LockStack, _base) - oopSize); 53 #ifdef ASSERT 54 for (int i = 0; i < CAPACITY; i++) { 55 _base[i] = NULL; 56 } 57 #endif 58 } 59 60 uint32_t LockStack::start_offset() { 61 int offset = lock_stack_base_offset; 62 assert(offset > 0, "must be positive offset"); 63 return static_cast<uint32_t>(offset); 64 } 65 66 uint32_t LockStack::end_offset() { 67 int offset = lock_stack_base_offset + CAPACITY * oopSize; 68 assert(offset > 0, "must be positive offset"); 69 return static_cast<uint32_t>(offset); 70 } 71 72 #ifndef PRODUCT 73 void LockStack::verify(const char* msg) const { 74 assert(LockingMode == LM_LIGHTWEIGHT, "never use lock-stack when light weight locking is disabled"); 75 assert((_top <= end_offset()), "lockstack overflow: _top %d end_offset %d", _top, end_offset()); 76 assert((_top >= start_offset()), "lockstack underflow: _top %d start_offset %d", _top, start_offset()); 77 if (SafepointSynchronize::is_at_safepoint() || (Thread::current()->is_Java_thread() && is_owning_thread())) { 78 int top = to_index(_top); 79 for (int i = 0; i < top; i++) { 80 assert(_base[i] != NULL, "no zapped before top"); 81 if (VM_Version::supports_recursive_lightweight_locking()) { 82 oop o = _base[i]; 83 for (; i < top - 1; i++) { 84 // Consecutive entries may be the same 85 if (_base[i + 1] != o) { 86 break; 87 } 88 } 89 } 90 91 for (int j = i + 1; j < top; j++) { 92 assert(_base[i] != _base[j], "entries must be unique: %s", msg); 93 } 94 } 95 for (int i = top; i < CAPACITY; i++) { 96 assert(_base[i] == NULL, "only zapped entries after top: i: %d, top: %d, entry: " PTR_FORMAT, i, top, p2i(_base[i])); 97 } 98 } 99 } 100 #endif 101 102 void LockStack::print_on(outputStream* st) { 103 for (int i = to_index(_top); (--i) >= 0;) { 104 st->print("LockStack[%d]: ", i); 105 oop o = _base[i]; 106 if (oopDesc::is_oop(o)) { 107 o->print_on(st); 108 } else { 109 st->print_cr("not an oop: " PTR_FORMAT, p2i(o)); 110 } 111 } 112 }