author | jlahoda |
Thu, 09 Oct 2014 10:08:52 +0200 | |
changeset 26993 | 513b2cae81c3 |
parent 23790 | 0697e38017ec |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
23790
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
1 |
/* |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
4 |
* |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
8 |
* |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
13 |
* accompanied this code). |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
14 |
* |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
18 |
* |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
21 |
* questions. |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
22 |
*/ |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
23 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
24 |
/* |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
25 |
* @test |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
26 |
* @bug 6898851 |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
27 |
* @summary Compiling against this corrupt class file causes a stacktrace from javac |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
28 |
*/ |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
29 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
30 |
import java.io.File; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
31 |
import java.io.FileWriter; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
32 |
import java.io.StringWriter; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
33 |
import java.io.PrintWriter; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
34 |
import java.io.IOException; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
35 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
36 |
import com.sun.tools.classfile.ClassFile; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
37 |
import com.sun.tools.classfile.ClassWriter; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
38 |
import com.sun.tools.javac.Main; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
39 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
40 |
public class BadClass { |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
41 |
// Create and compile file containing body; return compiler output |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
42 |
static String makeClass(String dir, String filename, String body) throws IOException { |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
43 |
File file = new File(dir, filename); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
44 |
try (FileWriter fw = new FileWriter(file)) { |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
45 |
fw.write(body); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
46 |
} |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
47 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
48 |
StringWriter sw = new StringWriter(); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
49 |
PrintWriter pw = new PrintWriter(sw); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
50 |
String args[] = { "-cp", dir, "-d", dir, "-XDrawDiagnostics", file.getPath() }; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
51 |
Main.compile(args, pw); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
52 |
pw.close(); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
53 |
return sw.toString(); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
54 |
} |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
55 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
56 |
public static void main(String... args) throws Exception { |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
57 |
new File("d1").mkdir(); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
58 |
new File("d2").mkdir(); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
59 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
60 |
// Step 1. build an empty class with an interface |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
61 |
makeClass("d1", "Empty.java", "abstract class Empty implements Readable {}"); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
62 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
63 |
// Step 2. Modify classfile to have invalid constant pool index |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
64 |
ClassFile cf = ClassFile.read(new File("d1","Empty.class")); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
65 |
cf.interfaces[0] = cf.constant_pool.size() + 10; |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
66 |
ClassWriter cw = new ClassWriter(); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
67 |
cw.write(cf, new File("d2","Empty.class")); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
68 |
|
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
69 |
// Step 3. Compile use of invalid class |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
70 |
String result = makeClass("d2", "EmptyUse.java", "class EmptyUse { Empty e; }"); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
71 |
if (!result.contains("compiler.misc.bad.class.file")) { |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
72 |
System.out.println(result); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
73 |
throw new Exception("test failed"); |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
74 |
} |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
75 |
} |
0697e38017ec
6898851: Compiling against this corrupt class file causes a stacktrace from javac
pgovereau
parents:
diff
changeset
|
76 |
} |