1 /* 2 * Copyright (c) 2017, 2018, 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 package org.graalvm.compiler.hotspot.test; 26 27 import static org.graalvm.compiler.test.SubprocessUtil.getVMCommandLine; 28 import static org.graalvm.compiler.test.SubprocessUtil.withoutDebuggerArguments; 29 30 import java.io.BufferedReader; 31 import java.io.File; 32 import java.io.FileReader; 33 import java.io.IOException; 34 import java.lang.reflect.Field; 35 import java.util.ArrayList; 36 import java.util.List; 37 import java.util.regex.Matcher; 38 import java.util.regex.Pattern; 39 40 import org.graalvm.compiler.api.directives.GraalDirectives; 41 import org.graalvm.compiler.core.test.GraalCompilerTest; 42 import org.graalvm.compiler.serviceprovider.JavaVersionUtil; 43 import org.graalvm.compiler.test.SubprocessUtil; 44 import org.graalvm.compiler.test.SubprocessUtil.Subprocess; 45 import org.junit.Assert; 46 import org.junit.Test; 47 48 import sun.misc.Unsafe; 49 50 /** 51 * Tests that a hs_err crash log contains expected content. 52 */ 53 public class HsErrLogTest extends GraalCompilerTest { 54 55 @Test 56 public void test1() throws IOException, InterruptedException { 57 List<String> args = new ArrayList<>(); 58 if (JavaVersionUtil.JAVA_SPEC <= 8) { 59 args.add("-XX:-UseJVMCIClassLoader"); 60 } 61 args.add("-XX:+UseJVMCICompiler"); 62 args.add("-XX:CompileOnly=" + Crasher.class.getName() + "::tryCrash"); 63 args.add(Crasher.class.getName()); 64 testHelper(args); 65 } 66 67 private static final boolean VERBOSE = Boolean.getBoolean(HsErrLogTest.class.getSimpleName() + ".verbose"); 68 69 private static void testHelper(List<String> extraVmArgs, String... mainClassAndArgs) throws IOException, InterruptedException { 70 List<String> vmArgs = withoutDebuggerArguments(getVMCommandLine()); 71 vmArgs.removeIf(a -> a.startsWith("-Dgraal.")); 72 vmArgs.remove("-esa"); 73 vmArgs.remove("-ea"); 74 vmArgs.addAll(extraVmArgs); 75 76 Subprocess proc = SubprocessUtil.java(vmArgs, mainClassAndArgs); 77 if (VERBOSE) { 78 System.out.println(proc); 79 } 80 81 Pattern re = Pattern.compile("# +(.*hs_err_pid[\\d]+\\.log)"); 82 83 for (String line : proc.output) { 84 Matcher m = re.matcher(line); 85 if (m.matches()) { 86 File path = new File(m.group(1)); 87 Assert.assertTrue(path.toString(), path.exists()); 88 checkHsErr(path); 89 return; 90 } 91 } 92 93 Assert.fail("Could not find " + re.pattern()); 94 } 95 96 private static void checkHsErr(File hsErrPath) { 97 try (BufferedReader br = new BufferedReader(new FileReader(hsErrPath))) { 98 String line = br.readLine(); 99 String sig = Crasher.class.getName() + ".tryCrash(JI)I"; 100 List<String> lines = new ArrayList<>(); 101 while (line != null) { 102 if (line.contains(sig)) { 103 if (!VERBOSE) { 104 hsErrPath.delete(); 105 } 106 return; 107 } 108 lines.add(line); 109 line = br.readLine(); 110 } 111 throw new AssertionError("Could not find line containing \"" + sig + "\" in " + hsErrPath + 112 ":" + System.lineSeparator() + String.join(System.lineSeparator(), lines)); 113 } catch (IOException e) { 114 throw new AssertionError(e); 115 } 116 } 117 } 118 119 class Crasher { 120 public static void main(String[] args) { 121 int iter = 0; 122 long mem = UNSAFE.allocateMemory(1000); 123 while (iter < Integer.MAX_VALUE) { 124 tryCrash(mem, iter); 125 iter++; 126 } 127 } 128 129 protected static int tryCrash(long mem, int iter) { 130 if (GraalDirectives.inCompiledCode()) { 131 UNSAFE.putInt(0, iter); 132 return 0; 133 } else { 134 UNSAFE.putInt(mem, iter); 135 return UNSAFE.getInt(mem); 136 } 137 } 138 139 static final Unsafe UNSAFE = initUnsafe(); 140 141 private static Unsafe initUnsafe() { 142 try { 143 // Fast path when we are trusted. 144 return Unsafe.getUnsafe(); 145 } catch (SecurityException se) { 146 // Slow path when we are not trusted. 147 try { 148 Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); 149 theUnsafe.setAccessible(true); 150 return (Unsafe) theUnsafe.get(Unsafe.class); 151 } catch (Exception e) { 152 throw new RuntimeException("exception while trying to get Unsafe", e); 153 } 154 } 155 } 156 }