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