Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/ConstantPoolTestCase.java
41153 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.jvmci.compilerToVM;2425import compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses;26import jdk.internal.reflect.ConstantPool;27import jdk.internal.reflect.ConstantPool.Tag;28import jdk.vm.ci.hotspot.CompilerToVMHelper;29import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;30import jdk.vm.ci.meta.ResolvedJavaMethod;31import sun.hotspot.WhiteBox;3233import java.util.HashMap;34import java.util.Map;3536import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_CLASS;37import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_DOUBLE;38import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_FIELDREF;39import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_FLOAT;40import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_INTEGER;41import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_INTERFACEMETHODREF;42import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_INVALID;43import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_INVOKEDYNAMIC;44import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_LONG;45import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_METHODHANDLE;46import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_METHODREF;47import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_METHODTYPE;48import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_NAMEANDTYPE;49import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_STRING;50import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_UTF8;5152/**53* Common class for jdk.vm.ci.hotspot.CompilerToVM constant pool tests54*/55public class ConstantPoolTestCase {5657private static final Map<Tag, ConstantTypes> TAG_TO_TYPE_MAP;58static {59TAG_TO_TYPE_MAP = new HashMap<>();60TAG_TO_TYPE_MAP.put(Tag.CLASS, CONSTANT_CLASS);61TAG_TO_TYPE_MAP.put(Tag.FIELDREF, CONSTANT_FIELDREF);62TAG_TO_TYPE_MAP.put(Tag.METHODREF, CONSTANT_METHODREF);63TAG_TO_TYPE_MAP.put(Tag.INTERFACEMETHODREF, CONSTANT_INTERFACEMETHODREF);64TAG_TO_TYPE_MAP.put(Tag.STRING, CONSTANT_STRING);65TAG_TO_TYPE_MAP.put(Tag.INTEGER, CONSTANT_INTEGER);66TAG_TO_TYPE_MAP.put(Tag.FLOAT, CONSTANT_FLOAT);67TAG_TO_TYPE_MAP.put(Tag.LONG, CONSTANT_LONG);68TAG_TO_TYPE_MAP.put(Tag.DOUBLE, CONSTANT_DOUBLE);69TAG_TO_TYPE_MAP.put(Tag.NAMEANDTYPE, CONSTANT_NAMEANDTYPE);70TAG_TO_TYPE_MAP.put(Tag.UTF8, CONSTANT_UTF8);71TAG_TO_TYPE_MAP.put(Tag.METHODHANDLE, CONSTANT_METHODHANDLE);72TAG_TO_TYPE_MAP.put(Tag.METHODTYPE, CONSTANT_METHODTYPE);73TAG_TO_TYPE_MAP.put(Tag.INVOKEDYNAMIC, CONSTANT_INVOKEDYNAMIC);74TAG_TO_TYPE_MAP.put(Tag.INVALID, CONSTANT_INVALID);75}76private static final WhiteBox WB = WhiteBox.getWhiteBox();77private final Map<ConstantTypes, Validator> typeTests;7879public static enum ConstantTypes {80CONSTANT_CLASS {81@Override82public TestedCPEntry getTestedCPEntry(DummyClasses dummyClass, int index) {83ConstantPool constantPoolSS = dummyClass.constantPoolSS;84checkIndex(constantPoolSS, index);85Class<?> klass = constantPoolSS.getClassAt(index);86String klassName = klass.getName();87TestedCPEntry[] testedEntries = dummyClass.testedCP.get(this);88for (TestedCPEntry entry : testedEntries) {89if (entry.klass.replaceAll("/", "\\.").equals(klassName)) {90return entry;91}92}93return null;94}95},96CONSTANT_FIELDREF {97@Override98public TestedCPEntry getTestedCPEntry(DummyClasses dummyClass, int index) {99return this.getTestedCPEntryForMethodAndField(dummyClass, index);100}101},102CONSTANT_METHODREF {103@Override104public TestedCPEntry getTestedCPEntry(DummyClasses dummyClass, int index) {105return this.getTestedCPEntryForMethodAndField(dummyClass, index);106}107},108CONSTANT_INTERFACEMETHODREF {109@Override110public TestedCPEntry getTestedCPEntry(DummyClasses dummyClass, int index) {111return this.getTestedCPEntryForMethodAndField(dummyClass, index);112}113},114CONSTANT_STRING {115@Override116public TestedCPEntry getTestedCPEntry(DummyClasses dummyClass, int index) {117ConstantPool constantPoolSS = dummyClass.constantPoolSS;118checkIndex(constantPoolSS, index);119String value = constantPoolSS.getStringAt(index);120TestedCPEntry[] testedEntries = dummyClass.testedCP.get(this);121for (TestedCPEntry entry : testedEntries) {122if (entry.name.equals(value)) {123return entry;124}125}126return null;127}128},129CONSTANT_INTEGER,130CONSTANT_FLOAT,131CONSTANT_LONG,132CONSTANT_DOUBLE,133CONSTANT_NAMEANDTYPE,134CONSTANT_UTF8,135CONSTANT_METHODHANDLE,136CONSTANT_METHODTYPE,137CONSTANT_INVOKEDYNAMIC {138@Override139public TestedCPEntry getTestedCPEntry(DummyClasses dummyClass, int index) {140ConstantPool constantPoolSS = dummyClass.constantPoolSS;141checkIndex(constantPoolSS, index);142int nameAndTypeIndex = constantPoolSS.getNameAndTypeRefIndexAt(index);143String[] info = constantPoolSS.getNameAndTypeRefInfoAt(nameAndTypeIndex);144TestedCPEntry[] testedEntries = dummyClass.testedCP.get(this);145for (TestedCPEntry entry : testedEntries) {146if (info[0].equals(entry.name) && info[1].equals(entry.type)) {147return entry;148}149}150return null;151}152},153CONSTANT_INVALID;154155public TestedCPEntry getTestedCPEntry(DummyClasses dummyClass, int index) {156return null; // returning null by default157}158159public TestedCPEntry[] getAllCPEntriesForType(DummyClasses dummyClass) {160TestedCPEntry[] toReturn = dummyClass.testedCP.get(this);161if (toReturn == null) {162return new TestedCPEntry[0];163}164return dummyClass.testedCP.get(this);165}166167protected TestedCPEntry getTestedCPEntryForMethodAndField(DummyClasses dummyClass, int index) {168ConstantPool constantPoolSS = dummyClass.constantPoolSS;169checkIndex(constantPoolSS, index);170String[] info = constantPoolSS.getMemberRefInfoAt(index);171TestedCPEntry[] testedEntries = dummyClass.testedCP.get(this);172for (TestedCPEntry entry : testedEntries) {173if (info[0].equals(entry.klass) && info[1].equals(entry.name) && info[2].equals(entry.type)) {174return entry;175}176}177return null;178}179180protected void checkIndex(ConstantPool constantPoolSS, int index) {181ConstantPool.Tag tag = constantPoolSS.getTagAt(index);182ConstantTypes type = mapTagToCPType(tag);183if (!this.equals(type)) {184String msg = String.format("TESTBUG: CP tag should be a %s, but is %s",185this.name(),186type.name());187throw new Error(msg);188}189}190}191192public static interface Validator {193void validate(jdk.vm.ci.meta.ConstantPool constantPoolCTVM,194ConstantTypes cpType,195DummyClasses dummyClass,196int index);197}198199public static class TestedCPEntry {200public final String klass;201public final String name;202public final String type;203public final ResolvedJavaMethod[] methods;204public final byte[] opcodes;205public final int accFlags;206207public TestedCPEntry(String klass, String name, String type, byte[] opcodes, int accFlags) {208this(klass, name, type, null, opcodes, accFlags);209}210211public TestedCPEntry(String klass, String name, String type, ResolvedJavaMethod[] methods, byte[] opcodes, int accFlags) {212this.klass = klass;213this.name = name;214this.type = type;215if (methods != null) {216this.methods = new ResolvedJavaMethod[methods.length];217System.arraycopy(methods, 0, this.methods, 0, methods.length);218} else {219this.methods = null;220}221if (opcodes != null) {222this.opcodes = new byte[opcodes.length];223System.arraycopy(opcodes, 0, this.opcodes, 0, opcodes.length);224} else {225this.opcodes = null;226}227this.accFlags = accFlags;228}229230public TestedCPEntry(String klass, String name, String type, byte[] opcodes) {231this(klass, name, type, opcodes, 0);232}233234public TestedCPEntry(String klass, String name, String type) {235this(klass, name, type, null, 0);236}237}238239public static ConstantTypes mapTagToCPType(Tag tag) {240return TAG_TO_TYPE_MAP.get(tag);241}242243public ConstantPoolTestCase(Map<ConstantTypes, Validator> typeTests) {244this.typeTests = new HashMap<>();245this.typeTests.putAll(typeTests);246}247248public void test() {249for (DummyClasses dummyClass : DummyClasses.values()) {250boolean isCPCached = WB.getConstantPoolCacheLength(dummyClass.klass) > -1;251System.out.printf("Testing dummy %s with constant pool cached = %b%n",252dummyClass.klass,253isCPCached);254HotSpotResolvedObjectType holder = CompilerToVMHelper.fromObjectClass(dummyClass.klass);255jdk.vm.ci.meta.ConstantPool constantPoolCTVM = holder.getConstantPool();256ConstantPool constantPoolSS = dummyClass.constantPoolSS;257for (int i = 0; i < constantPoolSS.getSize(); i++) {258Tag tag = constantPoolSS.getTagAt(i);259ConstantTypes cpType = mapTagToCPType(tag);260if (!typeTests.keySet().contains(cpType)) {261continue;262}263typeTests.get(cpType).validate(constantPoolCTVM, cpType, dummyClass, i);264}265}266}267}268269270