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