1 /*
   2  * Copyright (c) 2014, 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 import java.io.File;
  25 import java.io.FileOutputStream;
  26 
  27 import jdk.internal.org.objectweb.asm.ClassWriter;
  28 import jdk.internal.org.objectweb.asm.MethodVisitor;
  29 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  30 
  31 import com.oracle.java.testlibrary.*;
  32 
  33 /*
  34  * @test
  35  * @summary Test that anewarray bytecode is valid only if it specifies 255 or fewer dimensions.
  36  * @library /testlibrary
  37  * @build com.oracle.java.testlibrary.*
  38  * @compile -XDignore.symbol.file TestANewArray.java
  39  * @run main/othervm TestANewArray 49
  40  * @run main/othervm TestANewArray 50
  41  * @run main/othervm TestANewArray 51
  42  * @run main/othervm TestANewArray 52
  43  */
  44 
  45 /*
  46  * Testing anewarray instruction with 254, 255 & 264 dimensions to verify JVMS 8,
  47  * Section 4.9.1, Static Constraints that states the following:
  48  *
  49  * "No anewarray instruction may be used to create an array of more than 255 dimensions."
  50  *
  51  */
  52 
  53 public class TestANewArray {
  54 
  55     static String classCName = null; // the generated class name
  56 
  57     static final int test_Dimension_254 = 254; // should always pass
  58     static final int test_Dimension_255 = 255; // should always pass, except for cfv 49
  59     static final int test_Dimension_264 = 264; // should always fail
  60 
  61     static final String array_Dimension_254 = genArrayDim(test_Dimension_254);
  62     static final String array_Dimension_255 = genArrayDim(test_Dimension_255);
  63     static final String array_Dimension_264 = genArrayDim(test_Dimension_264);
  64 
  65     public static void main(String... args) throws Exception {
  66         int cfv = Integer.parseInt(args[0]);
  67 
  68         // 254 array dimensions
  69         byte[] classFile_254 = dumpClassFile(cfv, test_Dimension_254, array_Dimension_254);
  70         writeClassFileFromByteArray(classFile_254);
  71         System.err.println("Running with cfv: " + cfv + ", test_Dimension_254");
  72         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-verify", "-cp", ".",  classCName);
  73         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  74         output.shouldNotContain("java.lang.VerifyError");
  75         output.shouldHaveExitValue(0);
  76 
  77         // 255 array dimensions
  78         byte[] classFile_255 = dumpClassFile(cfv, test_Dimension_255, array_Dimension_255);
  79         writeClassFileFromByteArray(classFile_255);
  80         System.err.println("Running with cfv: " + cfv + ", test_Dimension_255");
  81         pb = ProcessTools.createJavaProcessBuilder(true, "-verify", "-cp", ".",  classCName);
  82         output = new OutputAnalyzer(pb.start());
  83         if (cfv == 49) {
  84             // The type-inferencing verifier used for <=49.0 ClassFiles detects an anewarray instruction
  85             // with exactly 255 dimensions and incorrectly issues the "Array with too many dimensions" VerifyError.
  86             output.shouldContain("Array with too many dimensions");
  87             output.shouldHaveExitValue(1);
  88         } else {
  89             // 255 dimensions should always pass, except for cfv 49
  90             output.shouldNotContain("java.lang.VerifyError");
  91             output.shouldNotContain("java.lang.ClassFormatError");
  92             output.shouldHaveExitValue(0);
  93         }
  94 
  95         // 264 array dimensions
  96         byte[] classFile_264 = dumpClassFile(cfv, test_Dimension_264, array_Dimension_264);
  97         writeClassFileFromByteArray(classFile_264);
  98         System.err.println("Running with cfv: " + cfv + ", test_Dimension_264");
  99         pb = ProcessTools.createJavaProcessBuilder(true, "-verify", "-cp", ".",  classCName);
 100         output = new OutputAnalyzer(pb.start());
 101         output.shouldContain("java.lang.ClassFormatError");
 102         output.shouldHaveExitValue(1);
 103     }
 104 
 105     public static byte[] dumpClassFile(int cfv, int testDimension264, String arrayDim) throws Exception {
 106         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
 107         MethodVisitor mv;
 108 
 109         classCName = "classCName_" + cfv + "_" + testDimension264;
 110 
 111         cw.visit(cfv, ACC_PUBLIC + ACC_SUPER, classCName, null, "java/lang/Object", null);
 112         {
 113             mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
 114             mv.visitCode();
 115             mv.visitVarInsn(ALOAD, 0);
 116             mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 117             mv.visitInsn(RETURN);
 118             mv.visitMaxs(1, 1);
 119             mv.visitEnd();
 120         }
 121         {   // classCName main method
 122             mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
 123             mv.visitCode();
 124             mv.visitIntInsn(BIPUSH, 1);
 125             mv.visitTypeInsn(ANEWARRAY, arrayDim); // Test ANEWARRAY bytecode with various dimensions
 126             mv.visitInsn(RETURN);
 127             mv.visitMaxs(2, 2);
 128             mv.visitEnd();
 129         }
 130         cw.visitEnd();
 131         return cw.toByteArray();
 132     }
 133 
 134     public static FileOutputStream writeClassFileFromByteArray(byte[] classFileByteArray) throws Exception {
 135         FileOutputStream fos = new FileOutputStream(new File(classCName + ".class"));
 136         fos.write(classFileByteArray);
 137         fos.close();
 138         return fos;
 139     }
 140 
 141     private static String genArrayDim(int testDim) {
 142         StringBuilder array_Dimension = new StringBuilder();
 143         for (int i = 0; i < testDim; i++)
 144         {
 145             array_Dimension.append("[");
 146         }
 147         return array_Dimension.append("Ljava/lang/Object;").toString();
 148     }
 149 }