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