Path: blob/master/test/langtools/tools/javac/6567415/T6567415.java
41149 views
/*1* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 656741526* @summary Test to ensure javac does not go into an infinite loop, while27* reading a classfile of a specific length.28* @modules jdk.compiler/com.sun.tools.javac.jvm29* @compile -XDignore.symbol.file T6567415.java30* @run main T656741531* @author ksrini32*/3334import java.io.File;35import java.io.FileInputStream;36import java.io.FileOutputStream;37import java.io.IOException;38import java.io.PrintStream;39import java.io.RandomAccessFile;40import java.nio.ByteBuffer;41import java.nio.MappedByteBuffer;42import java.nio.channels.FileChannel;4344/*45* this test compiles Bar.java into a classfile and enlarges the file to the46* magic file length, then use this mutated file on the classpath to compile47* Foo.java which references Bar.java and Ka-boom. QED.48*/49public class T6567415 {50final static String TEST_FILE_NAME = "Bar";51final static String TEST_JAVA = TEST_FILE_NAME + ".java";52final static String TEST_CLASS = TEST_FILE_NAME + ".class";5354final static String TEST2_FILE_NAME = "Foo";55final static String TEST2_JAVA = TEST2_FILE_NAME + ".java";5657/*58* the following is the initial buffer length set in ClassReader.java59* thus this value needs to change if ClassReader buf length changes.60*/6162final static int BAD_FILE_LENGTH =63com.sun.tools.javac.jvm.ClassReader.INITIAL_BUFFER_SIZE;6465static void createClassFile() throws IOException {66FileOutputStream fos = null;67try {68fos = new FileOutputStream(TEST_JAVA);69PrintStream ps = new PrintStream(fos);70ps.println("public class " + TEST_FILE_NAME + " {}");71} finally {72fos.close();73}74String cmds[] = {TEST_JAVA};75com.sun.tools.javac.Main.compile(cmds);76}7778static void enlargeClassFile() throws IOException {79File f = new File(TEST_CLASS);80if (!f.exists()) {81System.out.println("file not found: " + TEST_CLASS);82System.exit(1);83}84File tfile = new File(f.getAbsolutePath() + ".tmp");85f.renameTo(tfile);8687RandomAccessFile raf = null;88FileChannel wfc = null;8990FileInputStream fis = null;91FileChannel rfc = null;9293try {94raf = new RandomAccessFile(f, "rw");95wfc = raf.getChannel();9697fis = new FileInputStream(tfile);98rfc = fis.getChannel();99100ByteBuffer bb = MappedByteBuffer.allocate(BAD_FILE_LENGTH);101rfc.read(bb);102bb.rewind();103wfc.write(bb);104wfc.truncate(BAD_FILE_LENGTH);105} finally {106wfc.close();107raf.close();108rfc.close();109fis.close();110}111System.out.println("file length = " + f.length());112}113114static void createJavaFile() throws IOException {115FileOutputStream fos = null;116try {117fos = new FileOutputStream(TEST2_JAVA);118PrintStream ps = new PrintStream(fos);119ps.println("public class " + TEST2_FILE_NAME +120" {" + TEST_FILE_NAME + " b = new " +121TEST_FILE_NAME + " ();}");122} finally {123fos.close();124}125}126127public static void main(String... args) throws Exception {128createClassFile();129enlargeClassFile();130createJavaFile();131Thread t = new Thread () {132@Override133public void run() {134String cmds[] = {"-verbose", "-cp", ".", TEST2_JAVA};135int ret = com.sun.tools.javac.Main.compile(cmds);136System.out.println("test compilation returns: " + ret);137}138};139t.start();140t.join(1000*60);141System.out.println(t.getState());142if (t.isAlive()) {143throw new RuntimeException("Error: compilation is looping");144}145}146}147148149