Skip to content

Commit a607f49

Browse files
committed
8316309: AArch64: VMError::print_native_stack() crashes on Java native method frame
Backport-of: 2d38495b61ec4a8144fe187b5b11883add3dfd49
1 parent 98c94fd commit a607f49

File tree

6 files changed

+155
-6
lines changed

6 files changed

+155
-6
lines changed

src/hotspot/cpu/aarch64/frame_aarch64.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165

166166
frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc);
167167

168-
frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb);
168+
frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, bool allow_cb_null = false);
169169
// used for fast frame construction by continuations
170170
frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map, bool on_heap);
171171

src/hotspot/cpu/aarch64/frame_aarch64.inline.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ inline frame::frame(intptr_t* sp, intptr_t* fp, address pc) {
9292
init(sp, fp, pc);
9393
}
9494

95-
inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb) {
95+
inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, bool allow_cb_null) {
9696
assert(pauth_ptr_is_raw(pc), "cannot be signed");
9797
intptr_t a = intptr_t(sp);
9898
intptr_t b = intptr_t(fp);
@@ -103,7 +103,7 @@ inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address
103103
assert(pc != nullptr, "no pc?");
104104
_cb = cb;
105105
_oop_map = nullptr;
106-
assert(_cb != nullptr, "pc: " INTPTR_FORMAT, p2i(pc));
106+
assert(_cb != nullptr || allow_cb_null, "pc: " INTPTR_FORMAT, p2i(pc));
107107
_on_heap = false;
108108
DEBUG_ONLY(_frame_index = -1;)
109109

src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ frame os::fetch_compiled_frame_from_context(const void* ucVoid) {
177177

178178
// JVM compiled with -fno-omit-frame-pointer, so RFP is saved on the stack.
179179
frame os::get_sender_for_C_frame(frame* fr) {
180-
return frame(fr->link(), fr->link(), fr->sender_pc());
180+
return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
181181
}
182182

183183
NOINLINE frame os::current_frame() {

src/hotspot/os_cpu/linux_aarch64/os_linux_aarch64.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,23 @@ frame os::fetch_compiled_frame_from_context(const void* ucVoid) {
152152

153153
// By default, gcc always saves frame pointer rfp on this stack. This
154154
// may get turned off by -fomit-frame-pointer.
155+
// The "Procedure Call Standard for the Arm 64-bit Architecture" doesn't
156+
// specify a location for the frame record within a stack frame (6.4.6).
157+
// GCC currently chooses to save it at the top of the frame (lowest address).
158+
// This means that using fr->sender_sp() to set the caller's frame _unextended_sp,
159+
// as we do in x86, is wrong. Using fr->link() instead only makes sense for
160+
// native frames. Setting a correct value for _unextended_sp is important
161+
// if this value is later used to get that frame's caller. This will happen
162+
// if we end up calling frame::sender_for_compiled_frame(), which will be the
163+
// case if the _pc is associated with a CodeBlob that has a _frame_size > 0
164+
// (nmethod, runtime stub, safepoint stub, etc).
155165
frame os::get_sender_for_C_frame(frame* fr) {
156-
return frame(fr->link(), fr->link(), fr->sender_pc());
166+
address pc = fr->sender_pc();
167+
CodeBlob* cb = CodeCache::find_blob(pc);
168+
bool use_codeblob = cb != nullptr && cb->frame_size() > 0;
169+
assert(!use_codeblob || !Interpreter::contains(pc), "should not be an interpreter frame");
170+
intptr_t* sender_sp = use_codeblob ? (fr->link() + frame::metadata_words - cb->frame_size()) : fr->link();
171+
return frame(sender_sp, sender_sp, fr->link(), pc, cb, true /* allow_cb_null */);
157172
}
158173

159174
NOINLINE frame os::current_frame() {

src/hotspot/share/utilities/vmError.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ static frame next_frame(frame fr, Thread* t) {
426426
if (!t->is_in_full_stack((address)(fr.real_fp() + 1))) {
427427
return invalid;
428428
}
429-
if (fr.is_java_frame() || fr.is_native_frame() || fr.is_runtime_frame()) {
429+
if (fr.is_interpreted_frame() || (fr.cb() != nullptr && fr.cb()->frame_size() > 0)) {
430430
RegisterMap map(JavaThread::cast(t),
431431
RegisterMap::UpdateMap::skip,
432432
RegisterMap::ProcessFrames::include,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
import jdk.test.lib.process.OutputAnalyzer;
26+
import jdk.test.lib.process.ProcessTools;
27+
import jdk.test.lib.Utils;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.List;
31+
32+
/*
33+
* @test StackWalkNativeToJava
34+
* @bug 8316309
35+
* @summary Check that walking the stack works fine when going from C++ frame to Java frame.
36+
* @requires os.arch=="amd64" | os.arch=="x86_64" | os.arch=="aarch64"
37+
* @requires os.family != "windows"
38+
* @requires vm.flagless
39+
* @library /test/lib
40+
* @run driver StackWalkNativeToJava
41+
*/
42+
43+
public class StackWalkNativeToJava {
44+
45+
public static void main(String[] args) throws Exception {
46+
// Check stack walking works fine when sender of C++ frame
47+
// is a Java native method.
48+
testStackWalkNativeToJavaNative("-Xint");
49+
testStackWalkNativeToJavaNative("-Xcomp", "-XX:CompileCommand=dontinline,StackWalkNativeToJava$TestNativeToJavaNative::*");
50+
51+
// Check stack walking works fine when sender of C++ frame
52+
// is a runtime stub or interpreted Java method (VM call from Java).
53+
testStackWalkNativeToJava("-Xint");
54+
testStackWalkNativeToJava("-Xcomp", "-XX:TieredStopAtLevel=3",
55+
"-XX:CompileCommand=dontinline,StackWalkNativeToJava$TestNativeToJava::*");
56+
}
57+
58+
public static void testStackWalkNativeToJavaNative(String... extraFlags) throws Exception {
59+
List<String> commands = new ArrayList<>();
60+
commands.add("-Xbootclasspath/a:.");
61+
commands.add("-XX:-CreateCoredumpOnCrash");
62+
commands.add("-XX:+UnlockDiagnosticVMOptions");
63+
commands.add("-XX:AbortVMOnException=java.lang.IllegalMonitorStateException");
64+
commands.add("-XX:+ErrorFileToStdout");
65+
commands.addAll(Arrays.asList(extraFlags));
66+
commands.add("StackWalkNativeToJava$TestNativeToJavaNative");
67+
68+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(commands);
69+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
70+
output.shouldNotContain("java.lang.RuntimeException: Reached statement after obj.wait()");
71+
output.shouldNotContain("[error occurred during error reporting (printing native stack");
72+
String[] res = output.getOutput().split("StackWalkNativeToJava\\$TestNativeToJavaNative\\.callNativeMethod\\(\\)V");
73+
assertTrue(res.length - 1 == 2, res.length - 1);
74+
output.shouldNotHaveExitValue(0);
75+
}
76+
77+
public static class TestNativeToJavaNative {
78+
public static void main(String[] args) throws Exception {
79+
TestNativeToJavaNative test = new TestNativeToJavaNative();
80+
test.callNativeMethod();
81+
}
82+
83+
public void callNativeMethod() throws Exception {
84+
Object obj = new Object();
85+
// Trigger a fatal exit due to IllegalMonitorStateException during
86+
// a call to the VM from a Java native method.
87+
obj.wait();
88+
throw new RuntimeException("Reached statement after obj.wait()");
89+
}
90+
}
91+
92+
public static void testStackWalkNativeToJava(String... extraFlags) throws Exception {
93+
List<String> commands = new ArrayList<>();
94+
commands.add("-Xbootclasspath/a:.");
95+
commands.add("-XX:-CreateCoredumpOnCrash");
96+
commands.add("-XX:+UnlockDiagnosticVMOptions");
97+
commands.add("-XX:DiagnoseSyncOnValueBasedClasses=1");
98+
commands.add("-XX:+ErrorFileToStdout");
99+
commands.addAll(Arrays.asList(extraFlags));
100+
commands.add("StackWalkNativeToJava$TestNativeToJava");
101+
102+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(commands);
103+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
104+
output.shouldNotContain("java.lang.RuntimeException: Reached statement after synchronized");
105+
output.shouldNotContain("[error occurred during error reporting (printing native stack");
106+
String[] res = output.getOutput().split("StackWalkNativeToJava\\$TestNativeToJava\\.callVMMethod\\(\\)V");
107+
assertTrue(res.length - 1 == 2, res.length - 1);
108+
output.shouldNotHaveExitValue(0);
109+
}
110+
111+
public static class TestNativeToJava {
112+
static Integer counter = 0;
113+
114+
public static void main(String[] args) throws Exception {
115+
TestNativeToJava test = new TestNativeToJava();
116+
test.callVMMethod();
117+
}
118+
119+
public void callVMMethod() throws Exception {
120+
// Trigger a fatal exit for trying to synchronize on a value based class
121+
// during a call to the VM from a Java method.
122+
synchronized (counter) {
123+
counter++;
124+
}
125+
throw new RuntimeException("Reached statement after synchronized");
126+
}
127+
}
128+
129+
private static void assertTrue(boolean condition, int count) {
130+
if (!condition) {
131+
throw new RuntimeException("Count error: count was " + count);
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)