langtools/test/tools/javac/6567415/T6567415.java
author sogoel
Thu, 05 Jun 2014 10:57:10 -0700
changeset 24797 850ebd4d80a7
parent 18416 51ef92228b23
child 30730 d3ce7619db2c
permissions -rw-r--r--
8044072: Group 2: create .out files for OverrideChecks tests in tools/javac dir Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7842
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     1
/*
18416
51ef92228b23 8016684: JDK8 b94 source with GPL header errors
katleman
parents: 18012
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
7842
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     4
 *
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     8
 *
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    13
 * accompanied this code).
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    14
 *
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    18
 *
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    21
 * questions.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    22
 */
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    23
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    24
/*
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    25
 * @test
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    26
 * @bug 6567415
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    27
 * @summary Test to ensure javac does not go into an infinite loop, while
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    28
 *               reading a classfile of a specific length.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    29
 * @compile -XDignore.symbol.file T6567415.java
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    30
 * @run main T6567415
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    31
 * @author ksrini
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    32
 */
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    33
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    34
import java.io.File;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    35
import java.io.FileInputStream;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    36
import java.io.FileOutputStream;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    37
import java.io.IOException;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    38
import java.io.PrintStream;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    39
import java.io.RandomAccessFile;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    40
import java.nio.ByteBuffer;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    41
import java.nio.MappedByteBuffer;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    42
import java.nio.channels.FileChannel;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    43
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    44
/*
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    45
 * this test compiles Bar.java into a classfile and enlarges the file to the
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    46
 * magic file length, then use this mutated file on the classpath to compile
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    47
 * Foo.java which references Bar.java and Ka-boom. QED.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    48
 */
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    49
public class T6567415 {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    50
    final static String TEST_FILE_NAME = "Bar";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    51
    final static String TEST_JAVA = TEST_FILE_NAME + ".java";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    52
    final static String TEST_CLASS = TEST_FILE_NAME + ".class";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    53
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    54
    final static String TEST2_FILE_NAME = "Foo";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    55
    final static String TEST2_JAVA = TEST2_FILE_NAME + ".java";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    56
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    57
    /*
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    58
     * the following is the initial buffer length set in ClassReader.java
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    59
     * thus this value needs to change if ClassReader buf length changes.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    60
     */
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    61
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    62
    final static int BAD_FILE_LENGTH =
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    63
            com.sun.tools.javac.jvm.ClassReader.INITIAL_BUFFER_SIZE;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    64
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    65
    static void createClassFile() throws IOException {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    66
        FileOutputStream fos = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    67
        try {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    68
            fos = new FileOutputStream(TEST_JAVA);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    69
            PrintStream ps = new PrintStream(fos);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    70
            ps.println("public class " + TEST_FILE_NAME + " {}");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    71
        } finally {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    72
            fos.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    73
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    74
        String cmds[] = {TEST_JAVA};
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    75
        com.sun.tools.javac.Main.compile(cmds);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    76
    }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    77
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    78
    static void enlargeClassFile() throws IOException {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    79
        File f = new File(TEST_CLASS);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    80
        if (!f.exists()) {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    81
            System.out.println("file not found: " + TEST_CLASS);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    82
            System.exit(1);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    83
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    84
        File tfile = new File(f.getAbsolutePath() + ".tmp");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    85
        f.renameTo(tfile);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    86
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    87
        RandomAccessFile raf = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    88
        FileChannel wfc = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    89
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    90
        FileInputStream fis = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    91
        FileChannel rfc = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    92
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    93
        try {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    94
            raf =  new RandomAccessFile(f, "rw");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    95
            wfc = raf.getChannel();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    96
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    97
            fis = new FileInputStream(tfile);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    98
            rfc = fis.getChannel();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    99
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   100
            ByteBuffer bb = MappedByteBuffer.allocate(BAD_FILE_LENGTH);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   101
            rfc.read(bb);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   102
            bb.rewind();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   103
            wfc.write(bb);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   104
            wfc.truncate(BAD_FILE_LENGTH);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   105
        } finally {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   106
            wfc.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   107
            raf.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   108
            rfc.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   109
            fis.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   110
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   111
        System.out.println("file length = " + f.length());
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   112
    }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   113
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   114
    static void createJavaFile() throws IOException {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   115
        FileOutputStream fos = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   116
        try {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   117
            fos = new FileOutputStream(TEST2_JAVA);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   118
            PrintStream ps = new PrintStream(fos);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   119
            ps.println("public class " + TEST2_FILE_NAME +
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   120
                    " {" + TEST_FILE_NAME + " b = new " +
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   121
                    TEST_FILE_NAME  + " ();}");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   122
        } finally {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   123
            fos.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   124
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   125
    }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   126
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   127
    public static void main(String... args) throws Exception {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   128
        createClassFile();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   129
        enlargeClassFile();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   130
        createJavaFile();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   131
        Thread t = new Thread () {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   132
            @Override
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   133
            public void run() {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   134
                String cmds[] = {"-verbose", "-cp", ".", TEST2_JAVA};
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   135
                int ret = com.sun.tools.javac.Main.compile(cmds);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   136
                System.out.println("test compilation returns: " + ret);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   137
            }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   138
        };
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   139
        t.start();
18012
03261c9bd428 7186887: Test T6567415.java can fail on a slow machine
kizune
parents: 7842
diff changeset
   140
        t.join(1000*60);
7842
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   141
        System.out.println(t.getState());
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   142
        if (t.isAlive()) {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   143
            throw new RuntimeException("Error: compilation is looping");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   144
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   145
    }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   146
}