Skip to content

Commit 4ea1b99

Browse files
David Holmespchilano
andcommitted
8317262: LockStack::contains(oop) fails "assert(t->is_Java_thread()) failed: incorrect cast to JavaThread"
Co-authored-by: Patricio Chilano Mateo <[email protected]> Reviewed-by: stuefe, pchilanomate, rkennke, mli
1 parent 01ea1ef commit 4ea1b99

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

src/hotspot/share/runtime/lockStack.inline.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ inline bool LockStack::can_push() const {
4747
}
4848

4949
inline bool LockStack::is_owning_thread() const {
50-
JavaThread* thread = JavaThread::current();
51-
bool is_owning = &thread->lock_stack() == this;
52-
assert(is_owning == (get_thread() == thread), "is_owning sanity");
53-
return is_owning;
50+
Thread* current = Thread::current();
51+
if (current->is_Java_thread()) {
52+
JavaThread* thread = JavaThread::cast(current);
53+
bool is_owning = &thread->lock_stack() == this;
54+
assert(is_owning == (get_thread() == thread), "is_owning sanity");
55+
return is_owning;
56+
}
57+
return false;
5458
}
5559

5660
inline void LockStack::push(oop o) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2023, 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+
/*
26+
* @test
27+
* @bug 8317262
28+
* @library /testlibrary /test/lib
29+
* @build jdk.test.whitebox.WhiteBox
30+
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
31+
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+HandshakeALot -XX:GuaranteedSafepointInterval=1 TestStackWalk
32+
*/
33+
34+
import jvmti.JVMTIUtils;
35+
import jdk.test.lib.Asserts;
36+
import jdk.test.whitebox.WhiteBox;
37+
import java.util.concurrent.CountDownLatch;
38+
39+
public class TestStackWalk {
40+
static Thread worker1;
41+
static Thread worker2;
42+
static volatile boolean done;
43+
static volatile int counter = 0;
44+
static Object lock = new Object();
45+
46+
public static void main(String... args) throws Exception {
47+
worker1 = new Thread(() -> syncedWorker());
48+
worker1.start();
49+
worker2 = new Thread(() -> syncedWorker());
50+
worker2.start();
51+
Thread worker3 = new Thread(() -> stackWalker());
52+
worker3.start();
53+
54+
worker1.join();
55+
worker2.join();
56+
worker3.join();
57+
}
58+
59+
public static void syncedWorker() {
60+
synchronized (lock) {
61+
while (!done) {
62+
counter++;
63+
}
64+
}
65+
}
66+
67+
public static void stackWalker() {
68+
// Suspend workers so the one looping waiting for "done"
69+
// doesn't execute the handshake below, increasing the
70+
// chances the VMThread will do it.
71+
suspendWorkers();
72+
73+
WhiteBox wb = WhiteBox.getWhiteBox();
74+
long end = System.currentTimeMillis() + 20000;
75+
while (end > System.currentTimeMillis()) {
76+
wb.handshakeWalkStack(worker1, false /* all_threads */);
77+
wb.handshakeWalkStack(worker2, false /* all_threads */);
78+
}
79+
80+
resumeWorkers();
81+
done = true;
82+
}
83+
84+
static void suspendWorkers() {
85+
JVMTIUtils.suspendThread(worker1);
86+
JVMTIUtils.suspendThread(worker2);
87+
}
88+
89+
static void resumeWorkers() {
90+
JVMTIUtils.resumeThread(worker1);
91+
JVMTIUtils.resumeThread(worker2);
92+
}
93+
}

0 commit comments

Comments
 (0)