langtools/test/tools/javac/6567415/T6567415.java
author shade
Tue, 02 Feb 2016 18:15:15 +0300
changeset 35639 f34e7e8b4eac
parent 35359 f04501964016
permissions -rw-r--r--
8148787: StringConcatFactory exactness check produces bad bytecode when a non-arg concat is requested Reviewed-by: vlivanov, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7842
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 18416
diff changeset
     2
 * Copyright (c) 2010, 2015, 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.
35359
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 30730
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.jvm
7842
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    30
 * @compile -XDignore.symbol.file T6567415.java
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    31
 * @run main T6567415
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    32
 * @author ksrini
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    33
 */
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    34
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    35
import java.io.File;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    36
import java.io.FileInputStream;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    37
import java.io.FileOutputStream;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    38
import java.io.IOException;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    39
import java.io.PrintStream;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    40
import java.io.RandomAccessFile;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    41
import java.nio.ByteBuffer;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    42
import java.nio.MappedByteBuffer;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    43
import java.nio.channels.FileChannel;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    44
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    45
/*
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    46
 * this test compiles Bar.java into a classfile and enlarges the file to the
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    47
 * magic file length, then use this mutated file on the classpath to compile
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    48
 * Foo.java which references Bar.java and Ka-boom. QED.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    49
 */
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    50
public class T6567415 {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    51
    final static String TEST_FILE_NAME = "Bar";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    52
    final static String TEST_JAVA = TEST_FILE_NAME + ".java";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    53
    final static String TEST_CLASS = TEST_FILE_NAME + ".class";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    54
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    55
    final static String TEST2_FILE_NAME = "Foo";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    56
    final static String TEST2_JAVA = TEST2_FILE_NAME + ".java";
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    57
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    58
    /*
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    59
     * the following is the initial buffer length set in ClassReader.java
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    60
     * thus this value needs to change if ClassReader buf length changes.
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    61
     */
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    62
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    63
    final static int BAD_FILE_LENGTH =
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    64
            com.sun.tools.javac.jvm.ClassReader.INITIAL_BUFFER_SIZE;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    65
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    66
    static void createClassFile() throws IOException {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    67
        FileOutputStream fos = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    68
        try {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    69
            fos = new FileOutputStream(TEST_JAVA);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    70
            PrintStream ps = new PrintStream(fos);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    71
            ps.println("public class " + TEST_FILE_NAME + " {}");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    72
        } finally {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    73
            fos.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    74
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    75
        String cmds[] = {TEST_JAVA};
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    76
        com.sun.tools.javac.Main.compile(cmds);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    77
    }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    78
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    79
    static void enlargeClassFile() throws IOException {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    80
        File f = new File(TEST_CLASS);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    81
        if (!f.exists()) {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    82
            System.out.println("file not found: " + TEST_CLASS);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    83
            System.exit(1);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    84
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    85
        File tfile = new File(f.getAbsolutePath() + ".tmp");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    86
        f.renameTo(tfile);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    87
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    88
        RandomAccessFile raf = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    89
        FileChannel wfc = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    90
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    91
        FileInputStream fis = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    92
        FileChannel rfc = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    93
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    94
        try {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    95
            raf =  new RandomAccessFile(f, "rw");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    96
            wfc = raf.getChannel();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    97
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    98
            fis = new FileInputStream(tfile);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
    99
            rfc = fis.getChannel();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   100
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   101
            ByteBuffer bb = MappedByteBuffer.allocate(BAD_FILE_LENGTH);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   102
            rfc.read(bb);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   103
            bb.rewind();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   104
            wfc.write(bb);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   105
            wfc.truncate(BAD_FILE_LENGTH);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   106
        } finally {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   107
            wfc.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   108
            raf.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   109
            rfc.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   110
            fis.close();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   111
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   112
        System.out.println("file length = " + f.length());
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   113
    }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   114
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   115
    static void createJavaFile() throws IOException {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   116
        FileOutputStream fos = null;
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   117
        try {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   118
            fos = new FileOutputStream(TEST2_JAVA);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   119
            PrintStream ps = new PrintStream(fos);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   120
            ps.println("public class " + TEST2_FILE_NAME +
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   121
                    " {" + TEST_FILE_NAME + " b = new " +
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   122
                    TEST_FILE_NAME  + " ();}");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   123
        } finally {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   124
            fos.close();
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
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   128
    public static void main(String... args) throws Exception {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   129
        createClassFile();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   130
        enlargeClassFile();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   131
        createJavaFile();
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   132
        Thread t = new Thread () {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   133
            @Override
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   134
            public void run() {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   135
                String cmds[] = {"-verbose", "-cp", ".", TEST2_JAVA};
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   136
                int ret = com.sun.tools.javac.Main.compile(cmds);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   137
                System.out.println("test compilation returns: " + ret);
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   138
            }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   139
        };
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   140
        t.start();
18012
03261c9bd428 7186887: Test T6567415.java can fail on a slow machine
kizune
parents: 7842
diff changeset
   141
        t.join(1000*60);
7842
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   142
        System.out.println(t.getState());
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   143
        if (t.isAlive()) {
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   144
            throw new RuntimeException("Error: compilation is looping");
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   145
        }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   146
    }
5de9ea98089d 6567415: Neverending loop in ClassReader
ksrini
parents:
diff changeset
   147
}