1 /* 2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 7093325 8006694 8129962 27 * @summary Redundant entry in bytecode exception table 28 * temporarily workaround combo tests are causing time out in several platforms 29 * @library /tools/javac/lib 30 * @modules jdk.jdeps/com.sun.tools.classfile 31 * jdk.compiler/com.sun.tools.javac.api 32 * jdk.compiler/com.sun.tools.javac.file 33 * jdk.compiler/com.sun.tools.javac.util 34 * @build combo.ComboTestHelper 35 * @run main T7093325 36 */ 37 38 import java.io.IOException; 39 import java.io.InputStream; 40 41 import com.sun.tools.classfile.Attribute; 42 import com.sun.tools.classfile.ClassFile; 43 import com.sun.tools.classfile.Code_attribute; 44 import com.sun.tools.classfile.ConstantPoolException; 45 import com.sun.tools.classfile.Method; 46 47 import javax.tools.JavaFileObject; 48 49 import combo.ComboInstance; 50 import combo.ComboParameter; 51 import combo.ComboTask.Result; 52 import combo.ComboTestHelper; 53 54 public class T7093325 extends ComboInstance<T7093325> { 55 56 enum StatementKind implements ComboParameter { 57 NONE(null, false, false), 58 THROW("throw new RuntimeException();", false, false), 59 RETURN_NONEMPTY("System.out.println(); return;", true, false), 60 RETURN_EMPTY("return;", true, true), 61 APPLY("System.out.println();", true, false); 62 63 String stmt; 64 boolean canInline; 65 boolean empty; 66 StatementKind(String stmt, boolean canInline, boolean empty)67 StatementKind(String stmt, boolean canInline, boolean empty) { 68 this.stmt = stmt; 69 this.canInline = canInline; 70 this.empty = empty; 71 } 72 73 @Override expand(String optParameter)74 public String expand(String optParameter) { 75 return stmt; 76 } 77 } 78 79 enum CatchArity implements ComboParameter { 80 NONE(""), 81 ONE("catch (A a) { #{STMT[1]} }"), 82 TWO("catch (B b) { #{STMT[2]} }"), 83 THREE("catch (C c) { #{STMT[3]} }"), 84 FOUR("catch (D d) { #{STMT[4]} }"); 85 86 String catchStr; 87 CatchArity(String catchStr)88 CatchArity(String catchStr) { 89 this.catchStr = catchStr; 90 } 91 92 @Override expand(String optParameter)93 public String expand(String optParameter) { 94 if (this.ordinal() == 0) { 95 return catchStr; 96 } else { 97 return CatchArity.values()[this.ordinal() - 1].expand(optParameter) + 98 catchStr; 99 } 100 } 101 } 102 main(String... args)103 public static void main(String... args) throws Exception { 104 new ComboTestHelper<T7093325>() 105 .withFilter(T7093325::testFilter) 106 .withDimension("CATCH", (x, ca) -> x.ca = ca, CatchArity.values()) 107 .withArrayDimension("STMT", (x, stmt, idx) -> x.stmts[idx] = stmt, 5, StatementKind.values()) 108 .run(T7093325::new); 109 } 110 111 /** instance decls **/ 112 113 CatchArity ca; 114 StatementKind[] stmts = new StatementKind[5]; 115 testFilter()116 boolean testFilter() { 117 int lastPos = ca.ordinal() + 1; 118 for (int i = 0; i < stmts.length ; i++) { 119 boolean shouldBeSet = i < lastPos; 120 boolean isSet = stmts[i] != StatementKind.NONE; 121 if (shouldBeSet != isSet) { 122 return false; 123 } 124 } 125 return true; 126 } 127 128 @Override 129 public void doWork() throws IOException { 130 newCompilationTask() 131 .withSourceFromTemplate(source_template) 132 .generate(this::verifyBytecode); 133 } 134 135 void verifyBytecode(Result<Iterable<? extends JavaFileObject>> result) { 136 boolean lastInlined = false; 137 boolean hasCode = false; 138 int gapsCount = 0; 139 for (int i = 0; i < ca.ordinal() + 1 ; i++) { 140 lastInlined = stmts[i].canInline; 141 hasCode = hasCode || !stmts[i].empty; 142 if (lastInlined && hasCode) { 143 hasCode = false; 144 gapsCount++; 145 } 146 } 147 if (!lastInlined) { 148 gapsCount++; 149 } 150 151 try (InputStream is = result.get().iterator().next().openInputStream()) { 152 ClassFile cf = ClassFile.read(is); 153 if (cf == null) { 154 fail("Classfile not found: " + result.compilationInfo()); 155 return; 156 } 157 158 Method test_method = null; 159 for (Method m : cf.methods) { 160 if (m.getName(cf.constant_pool).equals("test")) { 161 test_method = m; 162 break; 163 } 164 } 165 166 if (test_method == null) { 167 fail("Method test() not found in class Test" + result.compilationInfo()); 168 return; 169 } 170 171 Code_attribute code = null; 172 for (Attribute a : test_method.attributes) { 173 if (a.getName(cf.constant_pool).equals(Attribute.Code)) { 174 code = (Code_attribute)a; 175 break; 176 } 177 } 178 179 if (code == null) { 180 fail("Code attribute not found in method test()"); 181 return; 182 } 183 184 int actualGapsCount = 0; 185 for (int i = 0; i < code.exception_table_length ; i++) { 186 int catchType = code.exception_table[i].catch_type; 187 if (catchType == 0) { //any 188 actualGapsCount++; 189 } 190 } 191 192 if (actualGapsCount != gapsCount) { 193 fail("Bad exception table for test()\n" + 194 "expected gaps: " + gapsCount + "\n" + 195 "found gaps: " + actualGapsCount + "\n" + 196 result.compilationInfo()); 197 return; 198 } 199 } catch (IOException | ConstantPoolException e) { 200 e.printStackTrace(); 201 fail("error reading classfile: " + e); 202 } 203 204 } 205 206 static final String source_template = 207 "class Test {\n" + 208 " void test() {\n" + 209 " try { #{STMT[0]} } #{CATCH} finally { System.out.println(); }\n" + 210 " }\n" + 211 "}\n" + 212 "class A extends RuntimeException {} \n" + 213 "class B extends RuntimeException {} \n" + 214 "class C extends RuntimeException {} \n" + 215 "class D extends RuntimeException {} \n" + 216 "class E extends RuntimeException {}"; 217 } 218