1 /* 2 * Copyright (c) 2021, 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 * @test 26 * @bug 8272586 27 * @requires vm.flagless 28 * @requires vm.compiler2.enabled 29 * @summary Test that abstract machine code is dumped for the top frames in a hs-err log 30 * @library /test/lib 31 * @modules java.base/jdk.internal.misc 32 * java.compiler 33 * java.management 34 * jdk.internal.jvmstat/sun.jvmstat.monitor 35 * @run driver MachCodeFramesInErrorFile 36 */ 37 38 import java.io.File; 39 import java.lang.annotation.Annotation; 40 import java.lang.reflect.Method; 41 import java.nio.file.Files; 42 import java.nio.file.Path; 43 import java.nio.file.Paths; 44 import java.util.ArrayList; 45 import java.util.HashSet; 46 import java.util.List; 47 import java.util.Set; 48 import java.util.stream.Collectors; 49 import java.util.stream.Stream; 50 import java.util.regex.Pattern; 51 import java.util.regex.Matcher; 52 53 import jdk.test.lib.Platform; 54 import jdk.test.lib.process.ProcessTools; 55 import jdk.test.lib.process.OutputAnalyzer; 56 import jdk.test.lib.Asserts; 57 58 import jdk.internal.misc.Unsafe; 59 60 public class MachCodeFramesInErrorFile { 61 private static class Crasher { 62 // Make Crasher.unsafe a compile-time constant so that 63 // C2 intrinsifies calls to Unsafe intrinsics. 64 private static final Unsafe unsafe = Unsafe.getUnsafe(); 65 66 public static void main(String[] args) throws Exception { 67 if (args[0].equals("crashInJava")) { 68 // This test relies on Unsafe.putLong(Object, long, long) being intrinsified 69 if (!Stream.of(Unsafe.class.getDeclaredMethod("putLong", Object.class, long.class, long.class).getAnnotations()). 70 anyMatch(a -> a.annotationType().getName().equals("jdk.internal.vm.annotation.IntrinsicCandidate"))) { 71 throw new RuntimeException("Unsafe.putLong(Object, long, long) is not an intrinsic"); 72 } 73 crashInJava1(10); 74 } else { 75 assert args[0].equals("crashInVM"); 76 // Low address reads are allowed on PPC 77 crashInNative1(Platform.isPPC() ? -1 : 10); 78 } 79 } 80 81 static void crashInJava1(long address) { 82 System.out.println("in crashInJava1"); 83 crashInJava2(address); 84 } 85 static void crashInJava2(long address) { 86 System.out.println("in crashInJava2"); 87 crashInJava3(address); 88 } 89 static void crashInJava3(long address) { 90 unsafe.putLong(null, address, 42); 91 System.out.println("wrote value to 0x" + Long.toHexString(address)); 92 } 93 94 static void crashInNative1(long address) { 95 System.out.println("in crashInNative1"); 96 crashInNative2(address); 97 } 98 static void crashInNative2(long address) { 99 System.out.println("in crashInNative2"); 100 crashInNative3(address); 101 } 102 static void crashInNative3(long address) { 103 System.out.println("read value " + unsafe.getLong(address) + " from 0x" + Long.toHexString(address)); 104 } 105 } 106 107 public static void main(String[] args) throws Exception { 108 run(true); 109 run(false); 110 } 111 112 /** 113 * Runs Crasher in Xcomp mode. The inner 114 * most method crashes the VM with Unsafe. The resulting hs-err log is 115 * expected to have a min number of MachCode sections. 116 */ 117 private static void run(boolean crashInJava) throws Exception { 118 ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder( 119 "-Xmx64m", "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED", 120 "-XX:-CreateCoredumpOnCrash", 121 "-Xcomp", 122 "-XX:-TieredCompilation", 123 "-XX:CompileCommand=compileonly,MachCodeFramesInErrorFile$Crasher.crashIn*", 124 "-XX:CompileCommand=dontinline,MachCodeFramesInErrorFile$Crasher.crashIn*", 125 "-XX:CompileCommand=dontinline,*/Unsafe.getLong", // ensures VM call when crashInJava == false 126 Crasher.class.getName(), 127 crashInJava ? "crashInJava" : "crashInVM"); 128 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 129 130 // Extract hs_err_pid file. 131 File hs_err_file = HsErrFileUtils.openHsErrFileFromOutput(output); 132 Path hsErrPath = hs_err_file.toPath(); 133 if (!Files.exists(hsErrPath)) { 134 throw new RuntimeException("hs_err_pid file missing at " + hsErrPath + ".\n"); 135 } 136 String hsErr = Files.readString(hsErrPath); 137 if (System.getenv("DEBUG") != null) { 138 System.err.println(hsErr); 139 } 140 Set<String> frames = new HashSet<>(); 141 extractFrames(hsErr, frames, true); 142 if (!crashInJava) { 143 // A crash in native will have Java frames in the hs-err log 144 // as there is a Java frame anchor on the stack. 145 extractFrames(hsErr, frames, false); 146 } 147 int compiledJavaFrames = (int) frames.stream().filter(f -> f.startsWith("J ")).count(); 148 149 Matcher matcherDisasm = Pattern.compile("\\[Disassembly\\].*\\[/Disassembly\\]", Pattern.DOTALL).matcher(hsErr); 150 if (matcherDisasm.find()) { 151 // Real disassembly is present, no MachCode is expected. 152 return; 153 } 154 155 String preCodeBlobSectionHeader = "Stack slot to memory mapping:"; 156 if (!hsErr.contains(preCodeBlobSectionHeader) && 157 System.getProperty("os.arch").equals("aarch64") && 158 System.getProperty("os.name").toLowerCase().startsWith("mac")) { 159 // JDK-8282607: hs_err can be truncated. If the section preceding 160 // code blob dumping is missing, exit successfully. 161 System.out.println("Could not find \"" + preCodeBlobSectionHeader + "\" in " + hsErrPath); 162 System.out.println("Looks like hs-err is truncated - exiting with success"); 163 return; 164 } 165 166 Matcher matcher = Pattern.compile("\\[MachCode\\]\\s*\\[Verified Entry Point\\]\\s* # \\{method\\} \\{[^}]*\\} '([^']+)' '([^']+)' in '([^']+)'", Pattern.DOTALL).matcher(hsErr); 167 List<String> machCodeHeaders = matcher.results().map(mr -> String.format("'%s' '%s' in '%s'", mr.group(1), mr.group(2), mr.group(3))).collect(Collectors.toList()); 168 int minExpectedMachCodeSections = Math.max(1, compiledJavaFrames); 169 if (machCodeHeaders.size() < minExpectedMachCodeSections) { 170 Asserts.fail(machCodeHeaders.size() + " < " + minExpectedMachCodeSections); 171 } 172 } 173 174 /** 175 * Extracts the lines in {@code hsErr} below the line starting with 176 * "Native frame" or "Java frame" up to the next blank line 177 * and adds them to {@code frames}. 178 */ 179 private static void extractFrames(String hsErr, Set<String> frames, boolean nativeStack) { 180 String marker = (nativeStack ? "Native" : "Java") + " frame"; 181 182 boolean seenMarker = false; 183 for (String line : hsErr.split(System.lineSeparator())) { 184 if (line.startsWith(marker)) { 185 seenMarker = true; 186 } else if (seenMarker) { 187 if (line.trim().isEmpty()) { 188 return; 189 } 190 frames.add(line); 191 } 192 } 193 System.err.println(hsErr); 194 throw new RuntimeException("\"" + marker + "\" line missing in hs_err_pid file"); 195 } 196 }