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