Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6567415/T6567415.java
38813 views
/*1* Copyright (c) 2010, 2013, 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* @compile -XDignore.symbol.file T6567415.java29* @run main T656741530* @author ksrini31*/3233import java.io.File;34import java.io.FileInputStream;35import java.io.FileOutputStream;36import java.io.IOException;37import java.io.PrintStream;38import java.io.RandomAccessFile;39import java.nio.ByteBuffer;40import java.nio.MappedByteBuffer;41import java.nio.channels.FileChannel;4243/*44* this test compiles Bar.java into a classfile and enlarges the file to the45* magic file length, then use this mutated file on the classpath to compile46* Foo.java which references Bar.java and Ka-boom. QED.47*/48public class T6567415 {49final static String TEST_FILE_NAME = "Bar";50final static String TEST_JAVA = TEST_FILE_NAME + ".java";51final static String TEST_CLASS = TEST_FILE_NAME + ".class";5253final static String TEST2_FILE_NAME = "Foo";54final static String TEST2_JAVA = TEST2_FILE_NAME + ".java";5556/*57* the following is the initial buffer length set in ClassReader.java58* thus this value needs to change if ClassReader buf length changes.59*/6061final static int BAD_FILE_LENGTH =62com.sun.tools.javac.jvm.ClassReader.INITIAL_BUFFER_SIZE;6364static void createClassFile() throws IOException {65FileOutputStream fos = null;66try {67fos = new FileOutputStream(TEST_JAVA);68PrintStream ps = new PrintStream(fos);69ps.println("public class " + TEST_FILE_NAME + " {}");70} finally {71fos.close();72}73String cmds[] = {TEST_JAVA};74com.sun.tools.javac.Main.compile(cmds);75}7677static void enlargeClassFile() throws IOException {78File f = new File(TEST_CLASS);79if (!f.exists()) {80System.out.println("file not found: " + TEST_CLASS);81System.exit(1);82}83File tfile = new File(f.getAbsolutePath() + ".tmp");84f.renameTo(tfile);8586RandomAccessFile raf = null;87FileChannel wfc = null;8889FileInputStream fis = null;90FileChannel rfc = null;9192try {93raf = new RandomAccessFile(f, "rw");94wfc = raf.getChannel();9596fis = new FileInputStream(tfile);97rfc = fis.getChannel();9899ByteBuffer bb = MappedByteBuffer.allocate(BAD_FILE_LENGTH);100rfc.read(bb);101bb.rewind();102wfc.write(bb);103wfc.truncate(BAD_FILE_LENGTH);104} finally {105wfc.close();106raf.close();107rfc.close();108fis.close();109}110System.out.println("file length = " + f.length());111}112113static void createJavaFile() throws IOException {114FileOutputStream fos = null;115try {116fos = new FileOutputStream(TEST2_JAVA);117PrintStream ps = new PrintStream(fos);118ps.println("public class " + TEST2_FILE_NAME +119" {" + TEST_FILE_NAME + " b = new " +120TEST_FILE_NAME + " ();}");121} finally {122fos.close();123}124}125126public static void main(String... args) throws Exception {127createClassFile();128enlargeClassFile();129createJavaFile();130Thread t = new Thread () {131@Override132public void run() {133String cmds[] = {"-verbose", "-cp", ".", TEST2_JAVA};134int ret = com.sun.tools.javac.Main.compile(cmds);135System.out.println("test compilation returns: " + ret);136}137};138t.start();139t.join(1000*60);140System.out.println(t.getState());141if (t.isAlive()) {142throw new RuntimeException("Error: compilation is looping");143}144}145}146147148