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
  27  * @summary Redundant entry in bytecode exception table
  28  *  temporarily workaround combo tests are causing time out in several platforms
  29  * @library lib
  30  * @modules jdk.jdeps/com.sun.tools.classfile
  31  * @build JavacTestingAbstractThreadedTest
  32  * @run main/othervm T7093325
  33  */
  34 
  35 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
  36 // see JDK-8006746
  37 
  38 import java.io.File;
  39 import java.net.URI;
  40 import java.util.Arrays;
  41 import javax.tools.JavaCompiler;
  42 import javax.tools.JavaFileObject;
  43 import javax.tools.SimpleJavaFileObject;
  44 import javax.tools.ToolProvider;
  45 
  46 import com.sun.source.util.JavacTask;
  47 import com.sun.tools.classfile.Attribute;
  48 import com.sun.tools.classfile.ClassFile;
  49 import com.sun.tools.classfile.Code_attribute;
  50 import com.sun.tools.classfile.ConstantPool.*;
  51 import com.sun.tools.classfile.Method;
  52 
  53 public class T7093325
  54     extends JavacTestingAbstractThreadedTest
  55     implements Runnable {
  56 
  57     enum StatementKind {
  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 
  67         private StatementKind(String stmt, boolean canInline, boolean empty) {
  68             this.stmt = stmt;
  69             this.canInline = canInline;
  70             this.empty = empty;
  71         }
  72     }
  73 
  74     enum CatchArity {
  75         NONE(""),
  76         ONE("catch (A a) { #S1 }"),
  77         TWO("catch (B b) { #S2 }"),
  78         THREE("catch (C c) { #S3 }"),
  79         FOUR("catch (D d) { #S4 }");
  80 
  81         String catchStr;
  82 
  83         private CatchArity(String catchStr) {
  84             this.catchStr = catchStr;
  85         }
  86 
  87         String catchers() {
  88             if (this.ordinal() == 0) {
  89                 return catchStr;
  90             } else {
  91                 return CatchArity.values()[this.ordinal() - 1].catchers() +
  92                         catchStr;
  93             }
  94         }
  95     }
  96 
  97     public static void main(String... args) throws Exception {
  98         for (CatchArity ca : CatchArity.values()) {
  99             for (StatementKind stmt0 : StatementKind.values()) {
 100                 if (ca.ordinal() == 0) {
 101                     pool.execute(new T7093325(ca, stmt0));
 102                     continue;
 103                 }
 104                 for (StatementKind stmt1 : StatementKind.values()) {
 105                     if (ca.ordinal() == 1) {
 106                         pool.execute(new T7093325(ca, stmt0, stmt1));
 107                         continue;
 108                     }
 109                     for (StatementKind stmt2 : StatementKind.values()) {
 110                         if (ca.ordinal() == 2) {
 111                             pool.execute(new T7093325(ca, stmt0, stmt1, stmt2));
 112                             continue;
 113                         }
 114                         for (StatementKind stmt3 : StatementKind.values()) {
 115                             if (ca.ordinal() == 3) {
 116                                 pool.execute(
 117                                     new T7093325(ca, stmt0, stmt1, stmt2, stmt3));
 118                                 continue;
 119                             }
 120                             for (StatementKind stmt4 : StatementKind.values()) {
 121                                 if (ca.ordinal() == 4) {
 122                                     pool.execute(
 123                                         new T7093325(ca, stmt0, stmt1,
 124                                                      stmt2, stmt3, stmt4));
 125                                     continue;
 126                                 }
 127                                 for (StatementKind stmt5 : StatementKind.values()) {
 128                                     pool.execute(
 129                                         new T7093325(ca, stmt0, stmt1, stmt2,
 130                                                      stmt3, stmt4, stmt5));
 131                                 }
 132                             }
 133                         }
 134                     }
 135                 }
 136             }
 137         }
 138 
 139         checkAfterExec();
 140     }
 141 
 142     /** instance decls **/
 143 
 144     CatchArity ca;
 145     StatementKind[] stmts;
 146 
 147     public T7093325(CatchArity ca, StatementKind... stmts) {
 148         this.ca = ca;
 149         this.stmts = stmts;
 150     }
 151 
 152     @Override
 153     public void run() {
 154         int id = checkCount.incrementAndGet();
 155         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
 156         JavaSource source = new JavaSource(id);
 157         JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), null,
 158                 null, null, Arrays.asList(source));
 159         ct.call();
 160         verifyBytecode(source, id);
 161     }
 162 
 163     void verifyBytecode(JavaSource source, int id) {
 164         boolean lastInlined = false;
 165         boolean hasCode = false;
 166         int gapsCount = 0;
 167         for (int i = 0; i < stmts.length ; i++) {
 168             lastInlined = stmts[i].canInline;
 169             hasCode = hasCode || !stmts[i].empty;
 170             if (lastInlined && hasCode) {
 171                 hasCode = false;
 172                 gapsCount++;
 173             }
 174         }
 175         if (!lastInlined) {
 176             gapsCount++;
 177         }
 178 
 179         File compiledTest = new File(String.format("Test%s.class", id));
 180         try {
 181             ClassFile cf = ClassFile.read(compiledTest);
 182             if (cf == null) {
 183                 throw new Error("Classfile not found: " +
 184                                 compiledTest.getName());
 185             }
 186 
 187             Method test_method = null;
 188             for (Method m : cf.methods) {
 189                 if (m.getName(cf.constant_pool).equals("test")) {
 190                     test_method = m;
 191                     break;
 192                 }
 193             }
 194 
 195             if (test_method == null) {
 196                 throw new Error("Method test() not found in class Test");
 197             }
 198 
 199             Code_attribute code = null;
 200             for (Attribute a : test_method.attributes) {
 201                 if (a.getName(cf.constant_pool).equals(Attribute.Code)) {
 202                     code = (Code_attribute)a;
 203                     break;
 204                 }
 205             }
 206 
 207             if (code == null) {
 208                 throw new Error("Code attribute not found in method test()");
 209             }
 210 
 211             int actualGapsCount = 0;
 212             for (int i = 0; i < code.exception_table_length ; i++) {
 213                 int catchType = code.exception_table[i].catch_type;
 214                 if (catchType == 0) { //any
 215                     actualGapsCount++;
 216                 }
 217             }
 218 
 219             if (actualGapsCount != gapsCount) {
 220                 throw new Error("Bad exception table for test()\n" +
 221                             "expected gaps: " + gapsCount + "\n" +
 222                             "found gaps: " + actualGapsCount + "\n" +
 223                             source);
 224             }
 225         } catch (Exception e) {
 226             e.printStackTrace();
 227             throw new Error("error reading " + compiledTest +": " + e);
 228         }
 229 
 230     }
 231 
 232     class JavaSource extends SimpleJavaFileObject {
 233 
 234         static final String source_template =
 235                 "class A extends RuntimeException {} \n" +
 236                 "class B extends RuntimeException {} \n" +
 237                 "class C extends RuntimeException {} \n" +
 238                 "class D extends RuntimeException {} \n" +
 239                 "class E extends RuntimeException {} \n" +
 240                 "class Test#ID {\n" +
 241                 "   void test() {\n" +
 242                 "   try { #S0 } #C finally { System.out.println(); }\n" +
 243                 "   }\n" +
 244                 "}";
 245 
 246         String source;
 247 
 248         public JavaSource(int id) {
 249             super(URI.create(String.format("myfo:/Test%s.java", id)),
 250                   JavaFileObject.Kind.SOURCE);
 251             source = source_template.replace("#C", ca.catchers());
 252             source = source.replace("#S0", stmts[0].stmt);
 253             source = source.replace("#ID", String.valueOf(id));
 254             for (int i = 1; i < ca.ordinal() + 1; i++) {
 255                 source = source.replace("#S" + i, stmts[i].stmt);
 256             }
 257         }
 258 
 259         @Override
 260         public String toString() {
 261             return source;
 262         }
 263 
 264         @Override
 265         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
 266             return source;
 267         }
 268     }
 269 
 270 }