< prev index next >

test/compiler/jvmci/compilerToVM/ConstantPoolTestCase.java

Print this page


   1 /*
   2  * Copyright (c) 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 package compiler.jvmci.compilerToVM;
  26 
  27 import java.util.HashMap;
  28 import java.util.Map;
  29 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  30 import jdk.internal.misc.SharedSecrets;
  31 import sun.reflect.ConstantPool;
  32 
  33 /**
  34  * Common class for jdk.vm.ci.hotspot.CompilerToVM constant pool tests
  35  */
  36 public class ConstantPoolTestCase {
  37     private final Map<ConstantPoolTestsHelper.ConstantTypes, Validator> typeTests;



































































































































































  38 
  39     public static interface Validator {
  40         void validate(jdk.vm.ci.meta.ConstantPool constantPoolCTVM,
  41                 ConstantPool constantPoolSS,
  42             ConstantPoolTestsHelper.DummyClasses dummyClass, int index);
  43     }
  44 
  45     public ConstantPoolTestCase(Map<ConstantPoolTestsHelper.ConstantTypes,Validator> typeTests) {
  46         this.typeTests = new HashMap<>();
  47         this.typeTests.putAll(typeTests);
















  48     }
  49 
  50     private void messageOnFail(Throwable t,
  51             ConstantPoolTestsHelper.ConstantTypes cpType,
  52             ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
  53         ConstantPool constantPoolSS = SharedSecrets.getJavaLangAccess().
  54                         getConstantPool(dummyClass.klass);
  55         String msg = String.format("Test for %s constant pool entry of"
  56                         + " type %s",
  57                         dummyClass.klass, cpType.name());
  58         switch (cpType) {
  59             case CONSTANT_CLASS:
  60             case CONSTANT_STRING:
  61             case CONSTANT_METHODTYPE:
  62                 String utf8 = constantPoolSS
  63                         .getUTF8At((int) dummyClass.cp.get(index).value);
  64                 msg = String.format("%s (%s) failed with %s", msg, utf8, t);
  65                 break;
  66             case CONSTANT_INTEGER:
  67                 int intValue = constantPoolSS.getIntAt(index);
  68                 msg = String.format("%s (%d) failed with %s", msg, intValue, t);
  69                 break;
  70             case CONSTANT_LONG:
  71                 long longValue = constantPoolSS.getLongAt(index);
  72                 msg = String.format("%s (%d) failed with %s", msg, longValue, t);
  73                 break;
  74             case CONSTANT_FLOAT:
  75                 float floatValue = constantPoolSS.getFloatAt(index);
  76                 msg = String.format("%s (%E) failed with %s", msg, floatValue, t);
  77                 break;
  78             case CONSTANT_DOUBLE:
  79                 double doubleValue = constantPoolSS.getDoubleAt(index);
  80                 msg = String.format("%s (%E) failed with %s", msg, doubleValue, t);
  81                 break;
  82             case CONSTANT_UTF8:
  83                 String utf8Value = constantPoolSS.getUTF8At(index);
  84                 msg = String.format("%s (%s) failed with %s", msg, utf8Value, t);
  85                 break;
  86             case CONSTANT_INVOKEDYNAMIC:
  87                 index = ((int[]) dummyClass.cp.get(index).value)[1];
  88             case CONSTANT_NAMEANDTYPE:
  89                 String name = constantPoolSS
  90                         .getUTF8At(((int[]) dummyClass.cp.get(index).value)[0]);
  91                 String type = constantPoolSS
  92                         .getUTF8At(((int[]) dummyClass.cp.get(index).value)[1]);
  93                 msg = String.format("%s (%s:%s) failed with %s",
  94                         msg, name, type, t);
  95                 break;
  96             case CONSTANT_METHODHANDLE:
  97                 index = ((int[]) dummyClass.cp.get(index).value)[1];
  98             case CONSTANT_METHODREF:
  99             case CONSTANT_INTERFACEMETHODREF:
 100             case CONSTANT_FIELDREF:
 101                 int classIndex = ((int[]) dummyClass.cp.get(index).value)[0];
 102                 int nameAndTypeIndex = ((int[]) dummyClass.cp.get(index).value)[1];
 103                 String cName = constantPoolSS
 104                         .getUTF8At((int) dummyClass.cp.get(classIndex).value);
 105                 String mName = constantPoolSS
 106                         .getUTF8At(((int[]) dummyClass.cp.get(nameAndTypeIndex).value)[0]);
 107                 String mType = constantPoolSS
 108                         .getUTF8At(((int[]) dummyClass.cp.get(nameAndTypeIndex).value)[1]);
 109                 msg = String.format("%s (%s.%s:%s) failed with %s ",
 110                         msg, cName, mName, mType, t);
 111                 break;
 112             default:
 113                 msg = String.format("Test bug: unknown constant type %s ", cpType);
 114         }
 115         throw new Error(msg + t.getMessage(), t);



 116     }
 117 
 118     public void test() {
 119         for (ConstantPoolTestsHelper.DummyClasses dummyClass
 120                 : ConstantPoolTestsHelper.DummyClasses.values()) {
 121             System.out.printf("%nTesting dummy %s%n", dummyClass.klass);


 122             HotSpotResolvedObjectType holder = HotSpotResolvedObjectType
 123                     .fromObjectClass(dummyClass.klass);
 124             jdk.vm.ci.meta.ConstantPool constantPoolCTVM
 125                     = holder.getConstantPool();
 126             ConstantPool constantPoolSS = SharedSecrets.getJavaLangAccess().
 127                         getConstantPool(dummyClass.klass);
 128             for (Integer i : dummyClass.cp.keySet()) {
 129                 ConstantPoolTestsHelper.ConstantTypes cpType
 130                         = dummyClass.cp.get(i).type;
 131                 if (!typeTests.keySet().contains(cpType)) {
 132                     continue;
 133                 }
 134                 try {
 135                     typeTests.get(cpType).validate(constantPoolCTVM,
 136                             constantPoolSS, dummyClass, i);
 137                 } catch (Throwable t) {
 138                     messageOnFail(t, cpType, dummyClass, i);
 139                 }
 140             }
 141         }
 142     }
 143 }
 144 
   1 /*
   2  * Copyright (c) 2015, 2016, 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 package compiler.jvmci.compilerToVM;
  26 
  27 import java.util.HashMap;
  28 import java.util.Map;
  29 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  30 import sun.hotspot.WhiteBox;
  31 import sun.reflect.ConstantPool;
  32 
  33 /**
  34  * Common class for jdk.vm.ci.hotspot.CompilerToVM constant pool tests
  35  */
  36 public class ConstantPoolTestCase {
  37 
  38     private static final Map<ConstantPool.Tag, ConstantTypes> TAG_TO_TYPE_MAP;
  39     static {
  40         TAG_TO_TYPE_MAP = new HashMap<>();
  41         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.CLASS,
  42                 ConstantTypes.CONSTANT_CLASS);
  43         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.FIELDREF,
  44                 ConstantTypes.CONSTANT_FIELDREF);
  45         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.METHODREF,
  46                 ConstantTypes.CONSTANT_METHODREF);
  47         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.INTERFACEMETHODREF,
  48                 ConstantTypes.CONSTANT_INTERFACEMETHODREF);
  49         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.STRING,
  50                 ConstantTypes.CONSTANT_STRING);
  51         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.INTEGER,
  52                 ConstantTypes.CONSTANT_INTEGER);
  53         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.FLOAT,
  54                 ConstantTypes.CONSTANT_FLOAT);
  55         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.LONG,
  56                 ConstantTypes.CONSTANT_LONG);
  57         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.DOUBLE,
  58                 ConstantTypes.CONSTANT_DOUBLE);
  59         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.NAMEANDTYPE,
  60                 ConstantTypes.CONSTANT_NAMEANDTYPE);
  61         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.UTF8,
  62                 ConstantTypes.CONSTANT_UTF8);
  63         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.METHODHANDLE,
  64                 ConstantTypes.CONSTANT_METHODHANDLE);
  65         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.METHODTYPE,
  66                 ConstantTypes.CONSTANT_METHODTYPE);
  67         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.INVOKEDYNAMIC,
  68                 ConstantTypes.CONSTANT_INVOKEDYNAMIC);
  69         TAG_TO_TYPE_MAP.put(ConstantPool.Tag.INVALID,
  70                 ConstantTypes.CONSTANT_INVALID);
  71     }
  72     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  73     private final Map<ConstantTypes, Validator> typeTests;
  74 
  75     public static enum ConstantTypes {
  76         CONSTANT_CLASS {
  77             @Override
  78             public TestedCPEntry getTestedCPEntry(
  79                     ConstantPoolTestsHelper.DummyClasses dummyClass,
  80                     int index) {
  81                 ConstantPool constantPoolSS = dummyClass.constantPoolSS;
  82                 checkIndex(constantPoolSS, index);
  83                 Class<?> klass = constantPoolSS.getClassAt(index);
  84                 String klassName = klass.getName();
  85                 TestedCPEntry[] testedEntries = dummyClass.testedCP.get(this);
  86                 for (TestedCPEntry entry : testedEntries) {
  87                     if (entry.klass.replaceAll("/", "\\.").equals(klassName)) {
  88                         return entry;
  89                     }
  90                 }
  91                 return null;
  92             }
  93         },
  94         CONSTANT_FIELDREF {
  95             @Override
  96             public TestedCPEntry getTestedCPEntry(
  97                     ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
  98                 return this.getTestedCPEntryForMethodAndField(dummyClass, index);
  99             }
 100         },
 101         CONSTANT_METHODREF {
 102             @Override
 103             public TestedCPEntry getTestedCPEntry(
 104                     ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
 105                 return this.getTestedCPEntryForMethodAndField(dummyClass, index);
 106             }
 107         },
 108         CONSTANT_INTERFACEMETHODREF {
 109             @Override
 110             public TestedCPEntry getTestedCPEntry(
 111                     ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
 112                 return this.getTestedCPEntryForMethodAndField(dummyClass, index);
 113             }
 114         },
 115         CONSTANT_STRING {
 116             @Override
 117             public TestedCPEntry getTestedCPEntry(
 118                     ConstantPoolTestsHelper.DummyClasses dummyClass,
 119                     int index) {
 120                 ConstantPool constantPoolSS = dummyClass.constantPoolSS;
 121                 checkIndex(constantPoolSS, index);
 122                 String value = constantPoolSS.getStringAt(index);
 123                 TestedCPEntry[] testedEntries = dummyClass.testedCP.get(this);
 124                 for (TestedCPEntry entry : testedEntries) {
 125                     if (entry.name.equals(value)) {
 126                         return entry;
 127                     }
 128                 }
 129                 return null;
 130             }
 131         },
 132         CONSTANT_INTEGER,
 133         CONSTANT_FLOAT,
 134         CONSTANT_LONG,
 135         CONSTANT_DOUBLE,
 136         CONSTANT_NAMEANDTYPE,
 137         CONSTANT_UTF8,
 138         CONSTANT_METHODHANDLE,
 139         CONSTANT_METHODTYPE,
 140         CONSTANT_INVOKEDYNAMIC {
 141             @Override
 142             public TestedCPEntry getTestedCPEntry(
 143                     ConstantPoolTestsHelper.DummyClasses dummyClass,
 144                     int index) {
 145                 ConstantPool constantPoolSS = dummyClass.constantPoolSS;
 146                 checkIndex(constantPoolSS, index);
 147                 int nameAndTypeIndex = constantPoolSS.getNameAndTypeRefIndexAt(index);
 148                 String[] info = constantPoolSS.getNameAndTypeRefInfoAt(nameAndTypeIndex);
 149                 TestedCPEntry[] testedEntries = dummyClass.testedCP.get(this);
 150                 for (TestedCPEntry entry : testedEntries) {
 151                     if (info[0].equals(entry.name)
 152                             && info[1].equals(entry.type)) {
 153                         return entry;
 154                     }
 155                 }
 156                 return null;
 157             }
 158         },
 159         CONSTANT_INVALID;
 160 
 161         public TestedCPEntry getTestedCPEntry(
 162                 ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
 163             return null; // returning null by default
 164         }
 165 
 166         public TestedCPEntry[] getAllCPEntriesForType(
 167                 ConstantPoolTestsHelper.DummyClasses dummyClass) {
 168             TestedCPEntry[] toReturn = dummyClass.testedCP.get(this);
 169             if (toReturn == null) {
 170                 return new TestedCPEntry[0];
 171             }
 172             return dummyClass.testedCP.get(this);
 173         }
 174 
 175         protected TestedCPEntry getTestedCPEntryForMethodAndField(
 176                 ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
 177             ConstantPool constantPoolSS = dummyClass.constantPoolSS;
 178             checkIndex(constantPoolSS, index);
 179             String[] info = constantPoolSS.getMemberRefInfoAt(index);
 180             TestedCPEntry[] testedEntries = dummyClass.testedCP.get(this);
 181             for (TestedCPEntry entry : testedEntries) {
 182                 if (info[0].equals(entry.klass)
 183                         && info[1].equals(entry.name)
 184                         && info[2].equals(entry.type)) {
 185                     return entry;
 186                 }
 187             }
 188             return null;
 189         }
 190 
 191         protected void checkIndex(ConstantPool constantPoolSS, int index) {
 192             ConstantPool.Tag tag = constantPoolSS.getTagAt(index);
 193             ConstantTypes type = mapTagToCPType(tag);
 194             if (!this.equals(type)) {
 195                 String msg = String.format("TESTBUG: CP tag should be a %s, but is %s",
 196                         this.name(), type.name());
 197                throw new Error(msg);
 198             }
 199         }
 200     }
 201 
 202     public static interface Validator {
 203         void validate(jdk.vm.ci.meta.ConstantPool constantPoolCTVM, ConstantTypes cpType,

 204             ConstantPoolTestsHelper.DummyClasses dummyClass, int index);
 205     }
 206 
 207     public static class TestedCPEntry {
 208         public final String klass;
 209         public final String name;
 210         public final String type;
 211         public final byte[] opcodes;
 212         public final long accFlags;
 213 
 214         public TestedCPEntry(String klass, String name, String type,
 215                 byte[] opcodes, long accFlags) {
 216             this.klass = klass;
 217             this.name = name;
 218             this.type = type;
 219             if (opcodes != null) {
 220                 this.opcodes = new byte[opcodes.length];
 221                 System.arraycopy(opcodes, 0, this.opcodes, 0, opcodes.length);
 222             } else {
 223                 this.opcodes = null;
 224             }
 225             this.accFlags = accFlags;
 226         }
 227 
 228         public TestedCPEntry(String klass, String name, String type,
 229                 byte[] opcodes) {
 230             this(klass, name, type, opcodes, 0);
 231         }
 232 
 233         public TestedCPEntry(String klass, String name, String type) {
 234             this(klass, name, type, null, 0);
 235         }
 236     }
 237 
 238     public static ConstantTypes mapTagToCPType(ConstantPool.Tag tag) {
 239         return TAG_TO_TYPE_MAP.get(tag);




















































 240     }
 241 
 242     public ConstantPoolTestCase(Map<ConstantTypes, Validator> typeTests) {
 243         this.typeTests = new HashMap<>();
 244         this.typeTests.putAll(typeTests);
 245     }
 246 
 247     public void test() {
 248         for (ConstantPoolTestsHelper.DummyClasses dummyClass
 249                 : ConstantPoolTestsHelper.DummyClasses.values()) {
 250             boolean isCPCached = WB.getConstantPoolCacheLength(dummyClass.klass) > -1;
 251             System.out.printf("Testing dummy %s with constant pool cached = %b%n",
 252                     dummyClass.klass, isCPCached);
 253             HotSpotResolvedObjectType holder = HotSpotResolvedObjectType
 254                     .fromObjectClass(dummyClass.klass);
 255             jdk.vm.ci.meta.ConstantPool constantPoolCTVM = holder.getConstantPool();
 256             ConstantPool constantPoolSS = dummyClass.constantPoolSS;
 257             for (int i = 0; i < constantPoolSS.getSize(); i++) {
 258                 ConstantPool.Tag tag = constantPoolSS.getTagAt(i);
 259                 ConstantTypes cpType = mapTagToCPType(tag);


 260                 if (!typeTests.keySet().contains(cpType)) {
 261                     continue;
 262                 }

 263                 typeTests.get(cpType).validate(constantPoolCTVM,
 264                         cpType, dummyClass, i);



 265             }
 266         }
 267     }
 268 }

< prev index next >