test/tools/javac/6567415/T6567415.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1847
988aef3a8c3a
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial