1 /*
   2  * Copyright (c) 2015, 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 8136421
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  28  * @library / /testlibrary /test/lib
  29  * @compile ../common/CompilerToVMHelper.java
  30  * @run main ClassFileInstaller
  31  *      jdk.vm.ci.hotspot.CompilerToVMHelper
  32  * @run driver compiler.jvmci.compilerToVM.DebugOutputTest
  33  */
  34 
  35 package compiler.jvmci.compilerToVM;
  36 
  37 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  38 import jdk.test.lib.ProcessTools;
  39 import java.util.Arrays;
  40 import jdk.test.lib.OutputAnalyzer;
  41 import jdk.test.lib.Utils;
  42 
  43 public class DebugOutputTest {
  44     public static void main(String[] args) {
  45         new DebugOutputTest().test();
  46     }
  47 
  48     private void test() {
  49         for (TestCaseData testCase : TestCaseData.values()) {
  50             System.out.println(testCase);
  51             OutputAnalyzer oa;
  52             try {
  53                 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  54                         /* use test options = */ true,
  55                         "-XX:+UnlockExperimentalVMOptions",
  56                         "-XX:+EnableJVMCI",
  57                         "-Xbootclasspath/a:.",
  58                         DebugOutputTest.Worker.class.getName(),
  59                         testCase.name());
  60                oa = ProcessTools.executeProcess(pb);
  61                } catch (Exception e) {
  62                 e.printStackTrace();
  63                 throw new Error("Problems running child process", e);
  64             }
  65             if (testCase.expectedException != null) {
  66                 oa.shouldHaveExitValue(1);
  67                 oa.shouldContain(testCase.expectedException.getName());
  68             } else {
  69                 oa.shouldHaveExitValue(0);
  70                 oa.shouldContain(new String(testCase.getExpected()));
  71             }
  72         }
  73     }
  74 
  75     /**
  76      * A list of test cases that are executed in forked VM
  77      */
  78     private enum TestCaseData {
  79         PART_ARRAY(100, 50),
  80         FULL_ARRAY(0, 255),
  81         EMPTY(0, 0),
  82         NEGATIVE_LENGTH(0, Integer.MIN_VALUE,
  83                 ArrayIndexOutOfBoundsException.class),
  84         NEGATIVE_OFFSET(-1, 255,
  85                 ArrayIndexOutOfBoundsException.class),
  86         LEFT_BOUND(Integer.MIN_VALUE, 100,
  87                 ArrayIndexOutOfBoundsException.class),
  88         RIGHT_BOUND(Integer.MAX_VALUE, 100,
  89                 ArrayIndexOutOfBoundsException.class),
  90         BIG_LENGTH(0, Integer.MAX_VALUE,
  91                 ArrayIndexOutOfBoundsException.class),
  92         NULL_POINTER(0, 0,
  93                 NullPointerException.class),
  94         ;
  95 
  96         private static final int SIZE = 255;
  97         private static final byte[] DATA = generate();
  98         public final int offset;
  99         public final int length;
 100         public final Class<? extends Throwable> expectedException;
 101 
 102         private TestCaseData(int offset, int length,
 103                 Class<? extends Throwable> expectedException) {
 104             this.offset = offset;
 105             this.length = length;
 106             this.expectedException = expectedException;
 107         }
 108 
 109         private TestCaseData(int offset, int length) {
 110             this(offset, length, null);
 111         }
 112 
 113         private static byte[] generate() {
 114             byte[] byteArray = new byte[SIZE];
 115             for (int i = 0; i < SIZE; i++) {
 116                 byteArray[i] = (byte) (i + 1);
 117             }
 118             return byteArray;
 119         }
 120 
 121         public byte[] getExpected() {
 122             if (expectedException != null) {
 123                 return new byte[0];
 124             }
 125             return Arrays.copyOfRange(TestCaseData.DATA, offset,
 126                     offset + length);
 127         }
 128 
 129         @Override
 130         public String toString() {
 131             return "CASE: " + this.name();
 132         }
 133 
 134         public byte[] getData() {
 135             if (equals(NULL_POINTER)) {
 136                 return null;
 137             } else {
 138                 return DATA;
 139             }
 140         }
 141     }
 142 
 143     public static class Worker {
 144         public static void main(String[] args) {
 145             for (String arg : args) {
 146                 TestCaseData tcase = TestCaseData.valueOf(arg);
 147                 CompilerToVMHelper.writeDebugOutput(tcase.getData(),
 148                         tcase.offset, tcase.length);
 149                 CompilerToVMHelper.flushDebugOutput();
 150             }
 151         }
 152     }
 153 }