test/tools/javac/6567415/T6567415.java

Wed, 26 Jun 2013 11:26:33 -0700

author
katleman
date
Wed, 26 Jun 2013 11:26:33 -0700
changeset 1847
988aef3a8c3a
parent 1804
79fd9cfa55f2
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8016684: JDK8 b94 source with GPL header errors
Reviewed-by: tbell, darcy

     1 /*
     2  * Copyright (c) 2010, 2013, 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  */
    24 /*
    25  * @test
    26  * @bug 6567415
    27  * @summary Test to ensure javac does not go into an infinite loop, while
    28  *               reading a classfile of a specific length.
    29  * @compile -XDignore.symbol.file T6567415.java
    30  * @run main T6567415
    31  * @author ksrini
    32  */
    34 import java.io.File;
    35 import java.io.FileInputStream;
    36 import java.io.FileOutputStream;
    37 import java.io.IOException;
    38 import java.io.PrintStream;
    39 import java.io.RandomAccessFile;
    40 import java.nio.ByteBuffer;
    41 import java.nio.MappedByteBuffer;
    42 import java.nio.channels.FileChannel;
    44 /*
    45  * this test compiles Bar.java into a classfile and enlarges the file to the
    46  * magic file length, then use this mutated file on the classpath to compile
    47  * Foo.java which references Bar.java and Ka-boom. QED.
    48  */
    49 public class T6567415 {
    50     final static String TEST_FILE_NAME = "Bar";
    51     final static String TEST_JAVA = TEST_FILE_NAME + ".java";
    52     final static String TEST_CLASS = TEST_FILE_NAME + ".class";
    54     final static String TEST2_FILE_NAME = "Foo";
    55     final static String TEST2_JAVA = TEST2_FILE_NAME + ".java";
    57     /*
    58      * the following is the initial buffer length set in ClassReader.java
    59      * thus this value needs to change if ClassReader buf length changes.
    60      */
    62     final static int BAD_FILE_LENGTH =
    63             com.sun.tools.javac.jvm.ClassReader.INITIAL_BUFFER_SIZE;
    65     static void createClassFile() throws IOException {
    66         FileOutputStream fos = null;
    67         try {
    68             fos = new FileOutputStream(TEST_JAVA);
    69             PrintStream ps = new PrintStream(fos);
    70             ps.println("public class " + TEST_FILE_NAME + " {}");
    71         } finally {
    72             fos.close();
    73         }
    74         String cmds[] = {TEST_JAVA};
    75         com.sun.tools.javac.Main.compile(cmds);
    76     }
    78     static void enlargeClassFile() throws IOException {
    79         File f = new File(TEST_CLASS);
    80         if (!f.exists()) {
    81             System.out.println("file not found: " + TEST_CLASS);
    82             System.exit(1);
    83         }
    84         File tfile = new File(f.getAbsolutePath() + ".tmp");
    85         f.renameTo(tfile);
    87         RandomAccessFile raf = null;
    88         FileChannel wfc = null;
    90         FileInputStream fis = null;
    91         FileChannel rfc = null;
    93         try {
    94             raf =  new RandomAccessFile(f, "rw");
    95             wfc = raf.getChannel();
    97             fis = new FileInputStream(tfile);
    98             rfc = fis.getChannel();
   100             ByteBuffer bb = MappedByteBuffer.allocate(BAD_FILE_LENGTH);
   101             rfc.read(bb);
   102             bb.rewind();
   103             wfc.write(bb);
   104             wfc.truncate(BAD_FILE_LENGTH);
   105         } finally {
   106             wfc.close();
   107             raf.close();
   108             rfc.close();
   109             fis.close();
   110         }
   111         System.out.println("file length = " + f.length());
   112     }
   114     static void createJavaFile() throws IOException {
   115         FileOutputStream fos = null;
   116         try {
   117             fos = new FileOutputStream(TEST2_JAVA);
   118             PrintStream ps = new PrintStream(fos);
   119             ps.println("public class " + TEST2_FILE_NAME +
   120                     " {" + TEST_FILE_NAME + " b = new " +
   121                     TEST_FILE_NAME  + " ();}");
   122         } finally {
   123             fos.close();
   124         }
   125     }
   127     public static void main(String... args) throws Exception {
   128         createClassFile();
   129         enlargeClassFile();
   130         createJavaFile();
   131         Thread t = new Thread () {
   132             @Override
   133             public void run() {
   134                 String cmds[] = {"-verbose", "-cp", ".", TEST2_JAVA};
   135                 int ret = com.sun.tools.javac.Main.compile(cmds);
   136                 System.out.println("test compilation returns: " + ret);
   137             }
   138         };
   139         t.start();
   140         t.join(1000*60);
   141         System.out.println(t.getState());
   142         if (t.isAlive()) {
   143             throw new RuntimeException("Error: compilation is looping");
   144         }
   145     }
   146 }

mercurial