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

vromero@2027 1 /*
vromero@2709 2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
vromero@2027 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
vromero@2027 4 *
vromero@2027 5 * This code is free software; you can redistribute it and/or modify it
vromero@2027 6 * under the terms of the GNU General Public License version 2 only, as
vromero@2027 7 * published by the Free Software Foundation.
vromero@2027 8 *
vromero@2027 9 * This code is distributed in the hope that it will be useful, but WITHOUT
vromero@2027 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
vromero@2027 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
vromero@2027 12 * version 2 for more details (a copy is included in the LICENSE file that
vromero@2027 13 * accompanied this code).
vromero@2027 14 *
vromero@2027 15 * You should have received a copy of the GNU General Public License version
vromero@2027 16 * 2 along with this work; if not, write to the Free Software Foundation,
vromero@2027 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
vromero@2027 18 *
vromero@2027 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
vromero@2027 20 * or visit www.oracle.com if you need additional information or have any
vromero@2027 21 * questions.
vromero@2027 22 */
vromero@2027 23
vromero@2027 24 /*
vromero@2027 25 * @test
vromero@2709 26 * @bug 7047734 8027660 8037937 8047719 8058708 8064857
vromero@2406 27 * @summary The LVT is not generated correctly during some try/catch scenarios
vromero@2178 28 * javac crash while creating LVT entry for a local variable defined in
vromero@2178 29 * an inner block
vromero@2027 30 * @library /tools/javac/lib
vromero@2027 31 * @build JavacTestingAbstractProcessor LVTHarness
vromero@2027 32 * @run main LVTHarness
vromero@2027 33 */
vromero@2027 34
vromero@2027 35 import java.io.File;
vromero@2027 36 import java.io.IOException;
vromero@2027 37 import java.lang.annotation.Annotation;
vromero@2027 38 import java.util.Set;
vromero@2027 39 import java.util.Arrays;
vromero@2027 40 import java.util.ArrayList;
vromero@2027 41 import java.util.Collections;
vromero@2027 42 import java.util.HashMap;
vromero@2027 43 import java.util.HashSet;
vromero@2027 44 import java.util.List;
vromero@2027 45 import java.util.Map;
vromero@2027 46
vromero@2027 47 import javax.annotation.processing.RoundEnvironment;
vromero@2027 48 import javax.lang.model.element.Element;
vromero@2027 49 import javax.lang.model.element.TypeElement;
vromero@2027 50 import javax.tools.JavaCompiler;
vromero@2027 51 import javax.tools.JavaFileObject;
vromero@2027 52 import javax.tools.StandardJavaFileManager;
vromero@2027 53 import javax.tools.ToolProvider;
vromero@2027 54
vromero@2027 55 import com.sun.source.util.JavacTask;
vromero@2027 56 import com.sun.tools.classfile.Attribute;
vromero@2027 57 import com.sun.tools.classfile.ClassFile;
vromero@2027 58 import com.sun.tools.classfile.ConstantPool;
vromero@2027 59 import com.sun.tools.classfile.ConstantPoolException;
vromero@2027 60 import com.sun.tools.classfile.Code_attribute;
vromero@2027 61 import com.sun.tools.classfile.ConstantPool.InvalidIndex;
vromero@2027 62 import com.sun.tools.classfile.ConstantPool.UnexpectedEntry;
vromero@2027 63 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
vromero@2027 64 import com.sun.tools.classfile.LocalVariableTable_attribute;
vromero@2027 65 import com.sun.tools.classfile.Method;
vromero@2027 66
vromero@2027 67 import static javax.tools.StandardLocation.*;
vromero@2027 68 import static com.sun.tools.classfile.LocalVariableTable_attribute.Entry;
alundblad@2046 69 import static javax.tools.JavaFileObject.Kind.SOURCE;
vromero@2027 70
vromero@2027 71 public class LVTHarness {
vromero@2027 72
vromero@2027 73 static int nerrors = 0;
vromero@2027 74
vromero@2027 75 static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
vromero@2027 76 static final StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
vromero@2027 77
vromero@2027 78 public static void main(String[] args) throws Exception {
alundblad@2046 79
alundblad@2046 80 String testDir = System.getProperty("test.src");
alundblad@2046 81 fm.setLocation(SOURCE_PATH, Arrays.asList(new File(testDir, "tests")));
alundblad@2046 82
alundblad@2046 83 // Make sure classes are written to scratch dir.
alundblad@2046 84 fm.setLocation(CLASS_OUTPUT, Arrays.asList(new File(".")));
alundblad@2046 85
alundblad@2046 86 for (JavaFileObject jfo : fm.list(SOURCE_PATH, "", Collections.singleton(SOURCE), true)) {
vromero@2027 87 new LVTHarness(jfo).check();
vromero@2027 88 }
vromero@2027 89 if (nerrors > 0) {
vromero@2027 90 throw new AssertionError("Errors were found");
vromero@2027 91 }
vromero@2027 92 }
vromero@2027 93
vromero@2027 94
vromero@2027 95 JavaFileObject jfo;
alundblad@2046 96 Map<ElementKey, AliveRanges> aliveRangeMap = new HashMap<>();
vromero@2027 97 Set<String> declaredKeys = new HashSet<>();
vromero@2027 98 List<ElementKey> seenAliveRanges = new ArrayList<>();
vromero@2027 99
vromero@2027 100 protected LVTHarness(JavaFileObject jfo) {
vromero@2027 101 this.jfo = jfo;
vromero@2027 102 }
vromero@2027 103
vromero@2027 104 protected void check() throws Exception {
alundblad@2046 105
alundblad@2046 106 JavacTask ct = (JavacTask) comp.getTask(null, fm, null, Arrays.asList("-g"),
alundblad@2046 107 null, Arrays.asList(jfo));
alundblad@2046 108 System.err.println("compiling code " + jfo);
vromero@2027 109 ct.setProcessors(Collections.singleton(new AliveRangeFinder()));
vromero@2027 110 if (!ct.call()) {
vromero@2027 111 throw new AssertionError("Error during compilation");
vromero@2027 112 }
vromero@2027 113
alundblad@2046 114
alundblad@2046 115 File javaFile = new File(jfo.getName());
alundblad@2046 116 File classFile = new File(javaFile.getName().replace(".java", ".class"));
alundblad@2046 117 checkClassFile(classFile);
vromero@2027 118
vromero@2027 119 //check all candidates have been used up
vromero@2027 120 for (Map.Entry<ElementKey, AliveRanges> entry : aliveRangeMap.entrySet()) {
vromero@2027 121 if (!seenAliveRanges.contains(entry.getKey())) {
vromero@2027 122 error("Redundant @AliveRanges annotation on method " +
vromero@2406 123 entry.getKey().elem + " with key " + entry.getKey());
vromero@2027 124 }
vromero@2027 125 }
vromero@2027 126 }
vromero@2027 127
vromero@2027 128 void checkClassFile(File file)
vromero@2027 129 throws IOException, ConstantPoolException, InvalidDescriptor {
vromero@2027 130 ClassFile classFile = ClassFile.read(file);
vromero@2027 131 ConstantPool constantPool = classFile.constant_pool;
vromero@2027 132
vromero@2027 133 //lets get all the methods in the class file.
vromero@2027 134 for (Method method : classFile.methods) {
vromero@2027 135 for (ElementKey elementKey: aliveRangeMap.keySet()) {
vromero@2027 136 String methodDesc = method.getName(constantPool) +
vromero@2406 137 method.descriptor.getParameterTypes(constantPool).replace(" ", "");
vromero@2027 138 if (methodDesc.equals(elementKey.elem.toString())) {
vromero@2027 139 checkMethod(constantPool, method, aliveRangeMap.get(elementKey));
vromero@2027 140 seenAliveRanges.add(elementKey);
vromero@2027 141 }
vromero@2027 142 }
vromero@2027 143 }
vromero@2027 144 }
vromero@2027 145
vromero@2027 146 void checkMethod(ConstantPool constantPool, Method method, AliveRanges ranges)
vromero@2709 147 throws InvalidIndex, UnexpectedEntry, ConstantPoolException {
vromero@2027 148 Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
vromero@2027 149 LocalVariableTable_attribute lvt =
vromero@2027 150 (LocalVariableTable_attribute) (code.attributes.get(Attribute.LocalVariableTable));
vromero@2027 151 List<String> infoFromRanges = convertToStringList(ranges);
vromero@2027 152 List<String> infoFromLVT = convertToStringList(constantPool, lvt);
vromero@2027 153
vromero@2027 154 // infoFromRanges most be contained in infoFromLVT
vromero@2027 155 int i = 0;
vromero@2027 156 int j = 0;
vromero@2027 157 while (i < infoFromRanges.size() && j < infoFromLVT.size()) {
vromero@2027 158 int comparison = infoFromRanges.get(i).compareTo(infoFromLVT.get(j));
vromero@2027 159 if (comparison == 0) {
vromero@2027 160 i++; j++;
vromero@2027 161 } else if (comparison > 0) {
vromero@2027 162 j++;
vromero@2027 163 } else {
vromero@2027 164 break;
vromero@2027 165 }
vromero@2027 166 }
vromero@2027 167
vromero@2027 168 if (i < infoFromRanges.size()) {
vromero@2709 169 error(infoFromLVT, infoFromRanges, method.getName(constantPool).toString());
vromero@2027 170 }
vromero@2027 171 }
vromero@2027 172
vromero@2027 173 List<String> convertToStringList(AliveRanges ranges) {
vromero@2027 174 List<String> result = new ArrayList<>();
vromero@2027 175 for (Annotation anno : ranges.value()) {
vromero@2027 176 AliveRange range = (AliveRange)anno;
vromero@2027 177 String str = formatLocalVariableData(range.varName(),
vromero@2027 178 range.bytecodeStart(), range.bytecodeLength());
vromero@2027 179 result.add(str);
vromero@2027 180 }
vromero@2027 181 Collections.sort(result);
vromero@2027 182 return result;
vromero@2027 183 }
vromero@2027 184
vromero@2027 185 List<String> convertToStringList(ConstantPool constantPool,
vromero@2027 186 LocalVariableTable_attribute lvt) throws InvalidIndex, UnexpectedEntry {
vromero@2027 187 List<String> result = new ArrayList<>();
vromero@2027 188 for (Entry entry : lvt.local_variable_table) {
vromero@2027 189 String str = formatLocalVariableData(constantPool.getUTF8Value(entry.name_index),
vromero@2027 190 entry.start_pc, entry.length);
vromero@2027 191 result.add(str);
vromero@2027 192 }
vromero@2027 193 Collections.sort(result);
vromero@2027 194 return result;
vromero@2027 195 }
vromero@2027 196
vromero@2027 197 String formatLocalVariableData(String varName, int start, int length) {
vromero@2027 198 StringBuilder sb = new StringBuilder()
vromero@2027 199 .append("var name: ").append(varName)
vromero@2027 200 .append(" start: ").append(start)
vromero@2027 201 .append(" length: ").append(length);
vromero@2027 202 return sb.toString();
vromero@2027 203 }
vromero@2027 204
vromero@2709 205 protected void error(List<String> infoFromLVT, List<String> infoFromRanges, String methodName) {
vromero@2027 206 nerrors++;
vromero@2027 207 System.err.printf("Error occurred while checking file: %s\n", jfo.getName());
vromero@2709 208 System.err.printf("at method: %s\n", methodName);
vromero@2027 209 System.err.println("The range info from the annotations is");
vromero@2027 210 printStringListToErrOutput(infoFromRanges);
vromero@2027 211 System.err.println();
vromero@2027 212 System.err.println("And the range info from the class file is");
vromero@2027 213 printStringListToErrOutput(infoFromLVT);
vromero@2027 214 System.err.println();
vromero@2027 215 }
vromero@2027 216
vromero@2027 217 void printStringListToErrOutput(List<String> list) {
vromero@2027 218 for (String s : list) {
vromero@2027 219 System.err.println("\t" + s);
vromero@2027 220 }
vromero@2027 221 }
vromero@2027 222
vromero@2027 223 protected void error(String msg) {
vromero@2027 224 nerrors++;
vromero@2027 225 System.err.printf("Error occurred while checking file: %s\nreason: %s\n",
vromero@2027 226 jfo.getName(), msg);
vromero@2027 227 }
vromero@2027 228
vromero@2027 229 class AliveRangeFinder extends JavacTestingAbstractProcessor {
vromero@2027 230
vromero@2027 231 @Override
vromero@2027 232 public boolean process(Set<? extends TypeElement> annotations,
vromero@2027 233 RoundEnvironment roundEnv) {
vromero@2027 234 if (roundEnv.processingOver())
vromero@2027 235 return true;
vromero@2027 236
vromero@2027 237 TypeElement aliveRangeAnno = elements.getTypeElement("AliveRanges");
vromero@2027 238
vromero@2027 239 if (!annotations.contains(aliveRangeAnno)) {
vromero@2027 240 error("no @AliveRanges annotation found in test class");
vromero@2027 241 }
vromero@2027 242
vromero@2027 243 for (Element elem: roundEnv.getElementsAnnotatedWith(aliveRangeAnno)) {
vromero@2027 244 Annotation annotation = elem.getAnnotation(AliveRanges.class);
vromero@2027 245 aliveRangeMap.put(new ElementKey(elem), (AliveRanges)annotation);
vromero@2027 246 }
vromero@2027 247 return true;
vromero@2027 248 }
vromero@2027 249 }
vromero@2027 250
vromero@2027 251 class ElementKey {
vromero@2027 252
vromero@2027 253 String key;
vromero@2027 254 Element elem;
vromero@2027 255
vromero@2027 256 public ElementKey(Element elem) {
vromero@2027 257 this.elem = elem;
vromero@2027 258 this.key = computeKey(elem);
vromero@2027 259 }
vromero@2027 260
vromero@2027 261 @Override
vromero@2027 262 public boolean equals(Object obj) {
vromero@2027 263 if (obj instanceof ElementKey) {
vromero@2027 264 ElementKey other = (ElementKey)obj;
vromero@2027 265 return other.key.equals(key);
vromero@2027 266 }
vromero@2027 267 return false;
vromero@2027 268 }
vromero@2027 269
vromero@2027 270 @Override
vromero@2027 271 public int hashCode() {
vromero@2027 272 return key.hashCode();
vromero@2027 273 }
vromero@2027 274
vromero@2027 275 String computeKey(Element e) {
vromero@2027 276 StringBuilder buf = new StringBuilder();
vromero@2027 277 while (e != null) {
vromero@2027 278 buf.append(e.toString());
vromero@2027 279 e = e.getEnclosingElement();
vromero@2027 280 }
vromero@2027 281 buf.append(jfo.getName());
vromero@2027 282 return buf.toString();
vromero@2027 283 }
vromero@2027 284
vromero@2027 285 @Override
vromero@2027 286 public String toString() {
vromero@2027 287 return "Key{" + key + "}";
vromero@2027 288 }
vromero@2027 289 }
vromero@2027 290
vromero@2027 291 }

mercurial