Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/GetSymbolTest.java
41153 views
/*1* Copyright (c) 2015, 2019, 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 813642126* @requires vm.jvmci27* @library / /test/lib28* @library ../common/patches29* @modules java.base/jdk.internal.misc:+open30* @modules java.base/jdk.internal.org.objectweb.asm31* java.base/jdk.internal.org.objectweb.asm.tree32* @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot:+open33* jdk.internal.vm.ci/jdk.vm.ci.code34* jdk.internal.vm.ci/jdk.vm.ci.meta35* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper36* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI37* -XX:-UseJVMCICompiler38* compiler.jvmci.compilerToVM.GetSymbolTest39*/4041package compiler.jvmci.compilerToVM;4243import compiler.jvmci.common.CTVMUtilities;44import compiler.jvmci.common.testcases.SingleImplementer;45import jdk.test.lib.Utils;46import jdk.vm.ci.hotspot.CompilerToVMHelper;47import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;48import jdk.vm.ci.meta.ConstantPool;4950import java.lang.reflect.Field;51import java.lang.reflect.Member;52import java.lang.reflect.Method;53import java.util.ArrayList;54import java.util.List;55import java.util.function.Function;56import java.util.stream.Collectors;57import java.util.stream.Stream;5859public class GetSymbolTest {60private static final int CONSTANT_POOL_UTF8_TAG = 1; // see jvms, section 4.46162private static final Function<Member[], List<String>> NAMES = members ->63Stream.of(members)64.map(Member::getName)65.collect(Collectors.toList());6667public static void main(String[] args) {68new GetSymbolTest().test(SingleImplementer.class);69}7071private void test(Class<?> aClass) {72Utils.ensureClassIsLoaded(aClass);73Method method;74try {75method = aClass.getDeclaredMethod("nonInterfaceMethod");76} catch (NoSuchMethodException e) {77throw new Error("TEST BUG: can't find test method", e);78}79HotSpotResolvedJavaMethod resolvedMethod80= CTVMUtilities.getResolvedMethod(aClass, method);81List<String> symbols;82try {83symbols = getSymbols(resolvedMethod);84} catch (ReflectiveOperationException e) {85throw new Error("TEST BUG: can't access private members", e);86}87List<String> classSymbols = new ArrayList<>();88classSymbols.addAll(NAMES.apply(aClass.getDeclaredFields()));89classSymbols.addAll(NAMES.apply(aClass.getDeclaredMethods()));90// Check that all members of test class have symbols from constant pool91for (String s : classSymbols) {92if (!symbols.contains(s)) {93// failed. print all symbols found by getSymbol94System.out.println("getSymbol data:");95for (String ctvmValue : symbols) {96System.out.println(ctvmValue);97}98throw new AssertionError("Unable to find symbol " + s99+ " using CompilerToVM.getSymbol");100}101}102}103104private List<String> getSymbols(HotSpotResolvedJavaMethod105metaspaceMethod) throws ReflectiveOperationException {106List<String> symbols = new ArrayList<>();107ConstantPool pool = metaspaceMethod.getConstantPool();108long length = pool.length();109// jvms-4.1: The constant_pool table is indexed from 1 ...110for (int i = 1; i < length; i++) {111if (getTag(pool, i) == CONSTANT_POOL_UTF8_TAG) {112long entryPointer;113Method getEntryAt = pool.getClass()114.getDeclaredMethod("getEntryAt", int.class);115getEntryAt.setAccessible(true);116entryPointer = (Long) getEntryAt.invoke(pool, i);117String symbol = CompilerToVMHelper.getSymbol(entryPointer);118symbols.add(symbol);119}120}121return symbols;122}123124private int getTag(ConstantPool pool, int index)125throws ReflectiveOperationException {126Object jvmConstant;127Method getTag = pool.getClass().getDeclaredMethod("getTagAt",128int.class);129getTag.setAccessible(true);130jvmConstant = getTag.invoke(pool, index);131Field tagCode = jvmConstant.getClass().getDeclaredField("tag");132tagCode.setAccessible(true);133return tagCode.getInt(jvmConstant);134}135}136137138