T6567415.java revision 803
803N/A/*
803N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
803N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
803N/A *
803N/A * This code is free software; you can redistribute it and/or modify it
803N/A * under the terms of the GNU General Public License version 2 only, as
803N/A * published by the Free Software Foundation.
803N/A *
803N/A * This code is distributed in the hope that it will be useful, but WITHOUT
803N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
803N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
803N/A * version 2 for more details (a copy is included in the LICENSE file that
803N/A * accompanied this code).
803N/A *
803N/A * You should have received a copy of the GNU General Public License version
803N/A * 2 along with this work; if not, write to the Free Software Foundation,
803N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
803N/A *
803N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
803N/A * or visit www.oracle.com if you need additional information or have any
803N/A * questions.
803N/A */
803N/A
803N/A/*
803N/A * @test
803N/A * @bug 6567415
803N/A * @summary Test to ensure javac does not go into an infinite loop, while
803N/A * reading a classfile of a specific length.
803N/A * @compile -XDignore.symbol.file T6567415.java
803N/A * @run main T6567415
803N/A * @author ksrini
803N/A */
803N/A
803N/Aimport java.io.File;
803N/Aimport java.io.FileInputStream;
803N/Aimport java.io.FileOutputStream;
803N/Aimport java.io.IOException;
803N/Aimport java.io.PrintStream;
803N/Aimport java.io.RandomAccessFile;
803N/Aimport java.nio.ByteBuffer;
803N/Aimport java.nio.MappedByteBuffer;
803N/Aimport java.nio.channels.FileChannel;
803N/A
803N/A/*
803N/A * this test compiles Bar.java into a classfile and enlarges the file to the
803N/A * magic file length, then use this mutated file on the classpath to compile
803N/A * Foo.java which references Bar.java and Ka-boom. QED.
803N/A */
803N/Apublic class T6567415 {
803N/A final static String TEST_FILE_NAME = "Bar";
803N/A final static String TEST_JAVA = TEST_FILE_NAME + ".java";
803N/A final static String TEST_CLASS = TEST_FILE_NAME + ".class";
803N/A
803N/A final static String TEST2_FILE_NAME = "Foo";
803N/A final static String TEST2_JAVA = TEST2_FILE_NAME + ".java";
803N/A
803N/A /*
803N/A * the following is the initial buffer length set in ClassReader.java
803N/A * thus this value needs to change if ClassReader buf length changes.
803N/A */
803N/A
803N/A final static int BAD_FILE_LENGTH =
803N/A com.sun.tools.javac.jvm.ClassReader.INITIAL_BUFFER_SIZE;
803N/A
803N/A static void createClassFile() throws IOException {
803N/A FileOutputStream fos = null;
803N/A try {
803N/A fos = new FileOutputStream(TEST_JAVA);
803N/A PrintStream ps = new PrintStream(fos);
803N/A ps.println("public class " + TEST_FILE_NAME + " {}");
803N/A } finally {
803N/A fos.close();
803N/A }
803N/A String cmds[] = {TEST_JAVA};
803N/A com.sun.tools.javac.Main.compile(cmds);
803N/A }
803N/A
803N/A static void enlargeClassFile() throws IOException {
803N/A File f = new File(TEST_CLASS);
803N/A if (!f.exists()) {
803N/A System.out.println("file not found: " + TEST_CLASS);
803N/A System.exit(1);
803N/A }
803N/A File tfile = new File(f.getAbsolutePath() + ".tmp");
803N/A f.renameTo(tfile);
803N/A
803N/A RandomAccessFile raf = null;
803N/A FileChannel wfc = null;
803N/A
803N/A FileInputStream fis = null;
803N/A FileChannel rfc = null;
803N/A
803N/A try {
803N/A raf = new RandomAccessFile(f, "rw");
803N/A wfc = raf.getChannel();
803N/A
803N/A fis = new FileInputStream(tfile);
803N/A rfc = fis.getChannel();
803N/A
803N/A ByteBuffer bb = MappedByteBuffer.allocate(BAD_FILE_LENGTH);
803N/A rfc.read(bb);
803N/A bb.rewind();
803N/A wfc.write(bb);
803N/A wfc.truncate(BAD_FILE_LENGTH);
803N/A } finally {
803N/A wfc.close();
803N/A raf.close();
803N/A rfc.close();
803N/A fis.close();
803N/A }
803N/A System.out.println("file length = " + f.length());
803N/A }
803N/A
803N/A static void createJavaFile() throws IOException {
803N/A FileOutputStream fos = null;
803N/A try {
803N/A fos = new FileOutputStream(TEST2_JAVA);
803N/A PrintStream ps = new PrintStream(fos);
803N/A ps.println("public class " + TEST2_FILE_NAME +
803N/A " {" + TEST_FILE_NAME + " b = new " +
803N/A TEST_FILE_NAME + " ();}");
803N/A } finally {
803N/A fos.close();
803N/A }
803N/A }
803N/A
803N/A public static void main(String... args) throws Exception {
803N/A createClassFile();
803N/A enlargeClassFile();
803N/A createJavaFile();
803N/A Thread t = new Thread () {
803N/A @Override
803N/A public void run() {
803N/A String cmds[] = {"-verbose", "-cp", ".", TEST2_JAVA};
803N/A int ret = com.sun.tools.javac.Main.compile(cmds);
803N/A System.out.println("test compilation returns: " + ret);
803N/A }
803N/A };
803N/A t.start();
803N/A t.join(1000*10);
803N/A System.out.println(t.getState());
803N/A if (t.isAlive()) {
803N/A throw new RuntimeException("Error: compilation is looping");
803N/A }
803N/A }
803N/A}