1 /*
   2  * Copyright (c) 2013, 2016, 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.File;
  29 import java.io.IOException;
  30 import java.util.ArrayList;
  31 import java.util.Arrays;
  32 import java.util.Enumeration;
  33 import java.util.List;
  34 import java.util.zip.ZipEntry;
  35 import java.util.zip.ZipFile;
  36 
  37 import org.graalvm.compiler.core.test.GraalCompilerTest;
  38 import org.graalvm.compiler.test.SubprocessUtil;
  39 import org.graalvm.compiler.test.SubprocessUtil.Subprocess;
  40 import org.junit.Assert;
  41 import org.junit.Test;
  42 
  43 /**
  44  * Tests support for dumping graphs and other info useful for debugging a compiler crash.
  45  */
  46 public class RetryableCompilationTest extends GraalCompilerTest {
  47 
  48     /**
  49      * Tests compilation requested by the VM.
  50      */
  51     @Test
  52     public void testVMCompilation() throws IOException, InterruptedException {
  53         testHelper(Arrays.asList("-XX:+BootstrapJVMCI", "-XX:+UseJVMCICompiler", "-Dgraal.CrashAt=Object.*,String.*", "-version"));
  54     }
  55 
  56     /**
  57      * Tests compilation requested by Truffle.
  58      */
  59     @Test
  60     public void testTruffleCompilation() throws IOException, InterruptedException {
  61         testHelper(Arrays.asList("-Dgraal.CrashAt=root test1"), "org.graalvm.compiler.truffle.test.SLTruffleGraalTestSuite", "test");
  62     }
  63 
  64     private static void testHelper(List<String> extraVmArgs, String... mainClassAndArgs) throws IOException, InterruptedException {
  65         List<String> vmArgs = withoutDebuggerArguments(getVMCommandLine());
  66         vmArgs.removeIf(a -> a.startsWith("-Dgraal."));
  67         // Force output to a file even if there's a running IGV instance available.
  68         vmArgs.add("-Dgraal.PrintGraphFile=true");
  69         vmArgs.addAll(extraVmArgs);
  70 
  71         Subprocess proc = SubprocessUtil.java(vmArgs, mainClassAndArgs);
  72 
  73         String forcedCrashString = "Forced crash after compiling";
  74         String diagnosticOutputFilePrefix = "Graal diagnostic output saved in ";
  75 
  76         boolean seenForcedCrashString = false;
  77         String diagnosticOutputZip = null;
  78 
  79         for (String line : proc.output) {
  80             if (line.contains(forcedCrashString)) {
  81                 seenForcedCrashString = true;
  82             } else if (diagnosticOutputZip == null) {
  83                 int index = line.indexOf(diagnosticOutputFilePrefix);
  84                 if (index != -1) {
  85                     diagnosticOutputZip = line.substring(diagnosticOutputFilePrefix.length()).trim();
  86                 }
  87             }
  88         }
  89         if (!seenForcedCrashString) {
  90             Assert.fail(String.format("Did not find '%s' in output of command:%n%s", forcedCrashString, proc));
  91         }
  92         if (diagnosticOutputZip == null) {
  93             Assert.fail(String.format("Did not find '%s' in output of command:%n%s", diagnosticOutputFilePrefix, proc));
  94         }
  95 
  96         File zip = new File(diagnosticOutputZip).getAbsoluteFile();
  97         Assert.assertTrue(zip.toString(), zip.exists());
  98         try {
  99             int bgv = 0;
 100             int cfg = 0;
 101             ZipFile dd = new ZipFile(diagnosticOutputZip);
 102             List<String> entries = new ArrayList<>();
 103             for (Enumeration<? extends ZipEntry> e = dd.entries(); e.hasMoreElements();) {
 104                 ZipEntry ze = e.nextElement();
 105                 String name = ze.getName();
 106                 entries.add(name);
 107                 if (name.endsWith(".bgv")) {
 108                     bgv++;
 109                 } else if (name.endsWith(".cfg")) {
 110                     cfg++;
 111                 }
 112             }
 113             if (bgv == 0) {
 114                 Assert.fail(String.format("Expected at least one .bgv file in %s: %s%n%s", diagnosticOutputZip, entries, proc));
 115             }
 116             if (cfg == 0) {
 117                 Assert.fail(String.format("Expected at least one .cfg file in %s: %s", diagnosticOutputZip, entries));
 118             }
 119         } finally {
 120             zip.delete();
 121         }
 122     }
 123 }