Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/ResolvePossiblyCachedConstantInPoolTest.java
41153 views
/*1* Copyright (c) 2016, 2021, 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*/2223/*24* @test25* @bug 813870826* @bug 813642127* @requires vm.jvmci28* @library /test/lib /29* @library ../common/patches30* @modules java.base/jdk.internal.access31* java.base/jdk.internal.reflect32* java.base/jdk.internal.org.objectweb.asm33* java.base/jdk.internal.org.objectweb.asm.tree34* jdk.internal.vm.ci/jdk.vm.ci.hotspot35* jdk.internal.vm.ci/jdk.vm.ci.runtime36* jdk.internal.vm.ci/jdk.vm.ci.meta37*38* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox39* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox40* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions41* -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI42* -XX:-UseJVMCICompiler43* compiler.jvmci.compilerToVM.ResolvePossiblyCachedConstantInPoolTest44*/4546package compiler.jvmci.compilerToVM;4748import compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes;49import compiler.jvmci.compilerToVM.ConstantPoolTestCase.TestedCPEntry;50import compiler.jvmci.compilerToVM.ConstantPoolTestCase.Validator;51import compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses;52import jdk.test.lib.Asserts;53import jdk.vm.ci.hotspot.CompilerToVMHelper;54import jdk.vm.ci.meta.ConstantPool;5556import java.lang.invoke.MethodHandle;57import java.lang.invoke.MethodType;58import java.util.HashMap;59import java.util.Map;6061import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_METHODHANDLE;62import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_METHODTYPE;63import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_STRING;6465/**66* Test for {@code jdk.vm.ci.hotspot.CompilerToVM.resolvePossiblyCachedConstantInPool} method67*/68public class ResolvePossiblyCachedConstantInPoolTest {6970public static void main(String[] args) throws Exception {71Map<ConstantTypes, Validator> typeTests = new HashMap<>();72typeTests.put(CONSTANT_STRING, ResolvePossiblyCachedConstantInPoolTest::validateString);73typeTests.put(CONSTANT_METHODHANDLE, ResolvePossiblyCachedConstantInPoolTest::validateMethodHandle);74typeTests.put(CONSTANT_METHODTYPE, ResolvePossiblyCachedConstantInPoolTest::validateMethodType);75ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);76testCase.test();77// The next "Class.forName" and repeating "testCase.test()"78// are here for the following reason.79// The first test run is without dummy class initialization,80// which means no constant pool cache exists.81// The second run is with initialized class (with constant pool cache available).82// Some CompilerToVM methods require different input83// depending on whether CP cache exists or not.84for (DummyClasses dummy : DummyClasses.values()) {85Class.forName(dummy.klass.getName());86}87testCase.test();88}8990private static void validateString(ConstantPool constantPoolCTVM,91ConstantTypes cpType,92DummyClasses dummyClass,93int cpi) {94TestedCPEntry entry = cpType.getTestedCPEntry(dummyClass, cpi);95if (entry == null) {96return;97}98int index = cpi;99String cached = "";100int cpci = dummyClass.getCPCacheIndex(cpi);101if (cpci != ConstantPoolTestsHelper.NO_CP_CACHE_PRESENT) {102index = cpci;103cached = "cached ";104}105Object constantInPool = CompilerToVMHelper.resolvePossiblyCachedConstantInPool(constantPoolCTVM, index);106String stringToVerify = (String) constantInPool;107String stringToRefer = entry.name;108if (stringToRefer.equals("") && cpci != ConstantPoolTestsHelper.NO_CP_CACHE_PRESENT) {109stringToRefer = null; // tested method returns null for cached empty strings110}111String msg = String.format("Wrong string accessed by %sconstant pool index %d", cached, index);112Asserts.assertEQ(stringToRefer, stringToVerify, msg);113}114115private static final String NOT_NULL_MSG116= "Object returned by resolvePossiblyCachedConstantInPool method should not be null";117118119private static void validateMethodHandle(ConstantPool constantPoolCTVM,120ConstantTypes cpType,121DummyClasses dummyClass,122int index) {123Object constantInPool = CompilerToVMHelper.resolvePossiblyCachedConstantInPool(constantPoolCTVM, index);124String msg = String.format("%s for index %d", NOT_NULL_MSG, index);125Asserts.assertNotNull(constantInPool, msg);126if (!(constantInPool instanceof MethodHandle)) {127msg = String.format("Wrong constant pool entry accessed by index"128+ " %d: %s, but should be subclass of %s",129index,130constantInPool.getClass(),131MethodHandle.class.getName());132throw new AssertionError(msg);133}134}135136private static void validateMethodType(ConstantPool constantPoolCTVM,137ConstantTypes cpType,138DummyClasses dummyClass,139int index) {140Object constantInPool = CompilerToVMHelper.resolvePossiblyCachedConstantInPool(constantPoolCTVM, index);141String msg = String.format("%s for index %d", NOT_NULL_MSG, index);142Asserts.assertNotNull(constantInPool, msg);143Class mtToVerify = constantInPool.getClass();144Class mtToRefer = MethodType.class;145msg = String.format("Wrong method type class accessed by"146+ " constant pool index %d",147index);148Asserts.assertEQ(mtToRefer, mtToVerify, msg);149}150}151152153