test/tools/javac/flow/LVTHarness.java

Mon, 26 Oct 2015 13:23:30 -0700

author
asaha
date
Mon, 26 Oct 2015 13:23:30 -0700
changeset 2999
683b3e7e05a7
parent 2709
dca7f60e618d
child 2893
ca5783d9a597
permissions
-rw-r--r--

Added tag jdk8u76-b00 for changeset 10ffafaf5340

     1 /*
     2  * Copyright (c) 2013, 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  */
    24 /*
    25  * @test
    26  * @bug 7047734 8027660 8037937 8047719 8058708 8064857
    27  * @summary The LVT is not generated correctly during some try/catch scenarios
    28  *          javac crash while creating LVT entry for a local variable defined in
    29  *          an inner block
    30  * @library /tools/javac/lib
    31  * @build JavacTestingAbstractProcessor LVTHarness
    32  * @run main LVTHarness
    33  */
    35 import java.io.File;
    36 import java.io.IOException;
    37 import java.lang.annotation.Annotation;
    38 import java.util.Set;
    39 import java.util.Arrays;
    40 import java.util.ArrayList;
    41 import java.util.Collections;
    42 import java.util.HashMap;
    43 import java.util.HashSet;
    44 import java.util.List;
    45 import java.util.Map;
    47 import javax.annotation.processing.RoundEnvironment;
    48 import javax.lang.model.element.Element;
    49 import javax.lang.model.element.TypeElement;
    50 import javax.tools.JavaCompiler;
    51 import javax.tools.JavaFileObject;
    52 import javax.tools.StandardJavaFileManager;
    53 import javax.tools.ToolProvider;
    55 import com.sun.source.util.JavacTask;
    56 import com.sun.tools.classfile.Attribute;
    57 import com.sun.tools.classfile.ClassFile;
    58 import com.sun.tools.classfile.ConstantPool;
    59 import com.sun.tools.classfile.ConstantPoolException;
    60 import com.sun.tools.classfile.Code_attribute;
    61 import com.sun.tools.classfile.ConstantPool.InvalidIndex;
    62 import com.sun.tools.classfile.ConstantPool.UnexpectedEntry;
    63 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
    64 import com.sun.tools.classfile.LocalVariableTable_attribute;
    65 import com.sun.tools.classfile.Method;
    67 import static javax.tools.StandardLocation.*;
    68 import static com.sun.tools.classfile.LocalVariableTable_attribute.Entry;
    69 import static javax.tools.JavaFileObject.Kind.SOURCE;
    71 public class LVTHarness {
    73     static int nerrors = 0;
    75     static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    76     static final StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    78     public static void main(String[] args) throws Exception {
    80         String testDir = System.getProperty("test.src");
    81         fm.setLocation(SOURCE_PATH, Arrays.asList(new File(testDir, "tests")));
    83         // Make sure classes are written to scratch dir.
    84         fm.setLocation(CLASS_OUTPUT, Arrays.asList(new File(".")));
    86         for (JavaFileObject jfo : fm.list(SOURCE_PATH, "", Collections.singleton(SOURCE), true)) {
    87             new LVTHarness(jfo).check();
    88         }
    89         if (nerrors > 0) {
    90             throw new AssertionError("Errors were found");
    91         }
    92     }
    95     JavaFileObject jfo;
    96     Map<ElementKey, AliveRanges> aliveRangeMap = new HashMap<>();
    97     Set<String> declaredKeys = new HashSet<>();
    98     List<ElementKey> seenAliveRanges = new ArrayList<>();
   100     protected LVTHarness(JavaFileObject jfo) {
   101         this.jfo = jfo;
   102     }
   104     protected void check() throws Exception {
   106         JavacTask ct = (JavacTask) comp.getTask(null, fm, null, Arrays.asList("-g"),
   107                                                 null, Arrays.asList(jfo));
   108         System.err.println("compiling code " + jfo);
   109         ct.setProcessors(Collections.singleton(new AliveRangeFinder()));
   110         if (!ct.call()) {
   111             throw new AssertionError("Error during compilation");
   112         }
   115         File javaFile = new File(jfo.getName());
   116         File classFile = new File(javaFile.getName().replace(".java", ".class"));
   117         checkClassFile(classFile);
   119         //check all candidates have been used up
   120         for (Map.Entry<ElementKey, AliveRanges> entry : aliveRangeMap.entrySet()) {
   121             if (!seenAliveRanges.contains(entry.getKey())) {
   122                 error("Redundant @AliveRanges annotation on method " +
   123                         entry.getKey().elem + " with key " + entry.getKey());
   124             }
   125         }
   126     }
   128     void checkClassFile(File file)
   129             throws IOException, ConstantPoolException, InvalidDescriptor {
   130         ClassFile classFile = ClassFile.read(file);
   131         ConstantPool constantPool = classFile.constant_pool;
   133         //lets get all the methods in the class file.
   134         for (Method method : classFile.methods) {
   135             for (ElementKey elementKey: aliveRangeMap.keySet()) {
   136                 String methodDesc = method.getName(constantPool) +
   137                         method.descriptor.getParameterTypes(constantPool).replace(" ", "");
   138                 if (methodDesc.equals(elementKey.elem.toString())) {
   139                     checkMethod(constantPool, method, aliveRangeMap.get(elementKey));
   140                     seenAliveRanges.add(elementKey);
   141                 }
   142             }
   143         }
   144     }
   146     void checkMethod(ConstantPool constantPool, Method method, AliveRanges ranges)
   147             throws InvalidIndex, UnexpectedEntry, ConstantPoolException {
   148         Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
   149         LocalVariableTable_attribute lvt =
   150             (LocalVariableTable_attribute) (code.attributes.get(Attribute.LocalVariableTable));
   151         List<String> infoFromRanges = convertToStringList(ranges);
   152         List<String> infoFromLVT = convertToStringList(constantPool, lvt);
   154         // infoFromRanges most be contained in infoFromLVT
   155         int i = 0;
   156         int j = 0;
   157         while (i < infoFromRanges.size() && j < infoFromLVT.size()) {
   158             int comparison = infoFromRanges.get(i).compareTo(infoFromLVT.get(j));
   159             if (comparison == 0) {
   160                 i++; j++;
   161             } else if (comparison > 0) {
   162                 j++;
   163             } else {
   164                 break;
   165             }
   166         }
   168         if (i < infoFromRanges.size()) {
   169             error(infoFromLVT, infoFromRanges, method.getName(constantPool).toString());
   170         }
   171     }
   173     List<String> convertToStringList(AliveRanges ranges) {
   174         List<String> result = new ArrayList<>();
   175         for (Annotation anno : ranges.value()) {
   176             AliveRange range = (AliveRange)anno;
   177             String str = formatLocalVariableData(range.varName(),
   178                     range.bytecodeStart(), range.bytecodeLength());
   179             result.add(str);
   180         }
   181         Collections.sort(result);
   182         return result;
   183     }
   185     List<String> convertToStringList(ConstantPool constantPool,
   186             LocalVariableTable_attribute lvt) throws InvalidIndex, UnexpectedEntry {
   187         List<String> result = new ArrayList<>();
   188         for (Entry entry : lvt.local_variable_table) {
   189             String str = formatLocalVariableData(constantPool.getUTF8Value(entry.name_index),
   190                     entry.start_pc, entry.length);
   191             result.add(str);
   192         }
   193         Collections.sort(result);
   194         return result;
   195     }
   197     String formatLocalVariableData(String varName, int start, int length) {
   198         StringBuilder sb = new StringBuilder()
   199                     .append("var name: ").append(varName)
   200                     .append(" start: ").append(start)
   201                     .append(" length: ").append(length);
   202         return sb.toString();
   203     }
   205     protected void error(List<String> infoFromLVT, List<String> infoFromRanges, String methodName) {
   206         nerrors++;
   207         System.err.printf("Error occurred while checking file: %s\n", jfo.getName());
   208         System.err.printf("at method: %s\n", methodName);
   209         System.err.println("The range info from the annotations is");
   210         printStringListToErrOutput(infoFromRanges);
   211         System.err.println();
   212         System.err.println("And the range info from the class file is");
   213         printStringListToErrOutput(infoFromLVT);
   214         System.err.println();
   215     }
   217     void printStringListToErrOutput(List<String> list) {
   218         for (String s : list) {
   219             System.err.println("\t" + s);
   220         }
   221     }
   223     protected void error(String msg) {
   224         nerrors++;
   225         System.err.printf("Error occurred while checking file: %s\nreason: %s\n",
   226                 jfo.getName(), msg);
   227     }
   229     class AliveRangeFinder extends JavacTestingAbstractProcessor {
   231         @Override
   232         public boolean process(Set<? extends TypeElement> annotations,
   233             RoundEnvironment roundEnv) {
   234             if (roundEnv.processingOver())
   235                 return true;
   237             TypeElement aliveRangeAnno = elements.getTypeElement("AliveRanges");
   239             if (!annotations.contains(aliveRangeAnno)) {
   240                 error("no @AliveRanges annotation found in test class");
   241             }
   243             for (Element elem: roundEnv.getElementsAnnotatedWith(aliveRangeAnno)) {
   244                 Annotation annotation = elem.getAnnotation(AliveRanges.class);
   245                 aliveRangeMap.put(new ElementKey(elem), (AliveRanges)annotation);
   246             }
   247             return true;
   248         }
   249     }
   251     class ElementKey {
   253         String key;
   254         Element elem;
   256         public ElementKey(Element elem) {
   257             this.elem = elem;
   258             this.key = computeKey(elem);
   259         }
   261         @Override
   262         public boolean equals(Object obj) {
   263             if (obj instanceof ElementKey) {
   264                 ElementKey other = (ElementKey)obj;
   265                 return other.key.equals(key);
   266             }
   267             return false;
   268         }
   270         @Override
   271         public int hashCode() {
   272             return key.hashCode();
   273         }
   275         String computeKey(Element e) {
   276             StringBuilder buf = new StringBuilder();
   277             while (e != null) {
   278                 buf.append(e.toString());
   279                 e = e.getEnclosingElement();
   280             }
   281             buf.append(jfo.getName());
   282             return buf.toString();
   283         }
   285         @Override
   286         public String toString() {
   287             return "Key{" + key + "}";
   288         }
   289     }
   291 }

mercurial