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