test/tools/javac/flow/LVTHarness.java

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2709
dca7f60e618d
parent 2702
9ca8d8713094
permissions
-rw-r--r--

merge

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

mercurial