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 * @test 26 * @bug 8136421 27 * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64") 28 * @library / /testlibrary /test/lib 29 * @library ../common/patches 30 * @modules java.base/jdk.internal.org.objectweb.asm 31 * java.base/jdk.internal.org.objectweb.asm.tree 32 * jdk.vm.ci/jdk.vm.ci.code 33 * jdk.vm.ci/jdk.vm.ci.meta 34 * jdk.vm.ci.hotspot/jdk.vm.ci.hotspot 35 * @build jdk.vm.ci.hotspot/jdk.vm.ci.hotspot.CompilerToVMHelper 36 * @build compiler.jvmci.compilerToVM.GetSymbolTest 37 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 38 * compiler.jvmci.compilerToVM.GetSymbolTest 39 */ 40 41 package compiler.jvmci.compilerToVM; 42 43 import jdk.vm.ci.hotspot.CompilerToVMHelper; 44 import compiler.jvmci.common.CTVMUtilities; 45 import compiler.jvmci.common.testcases.SingleImplementer; 46 import java.lang.reflect.Field; 47 import java.lang.reflect.Member; 48 import java.lang.reflect.Method; 49 import java.util.ArrayList; 50 import java.util.List; 51 import java.util.function.Function; 52 import java.util.stream.Collectors; 53 import java.util.stream.Stream; 54 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 55 import jdk.vm.ci.meta.ConstantPool; 56 import jdk.test.lib.Utils; 57 58 public class GetSymbolTest { 59 private static final int CONSTANT_POOL_UTF8_TAG = 1; // see jvms, section 4.4 60 61 private static final Function<Member[], List<String>> NAMES = members -> 62 Stream.of(members) 63 .map(Member::getName) 64 .collect(Collectors.toList()); 65 66 public static void main(String[] args) { 67 new GetSymbolTest().test(SingleImplementer.class); 68 } 69 70 private void test(Class<?> aClass) { 71 Utils.ensureClassIsLoaded(aClass); 72 Method method; 73 try { 74 method = aClass.getDeclaredMethod("nonInterfaceMethod"); 75 } catch (NoSuchMethodException e) { 76 throw new Error("TEST BUG: can't find test method", e); 77 } 78 HotSpotResolvedJavaMethod resolvedMethod 79 = CTVMUtilities.getResolvedMethod(aClass, method); 80 List<String> symbols; 81 try { 82 symbols = getSymbols(resolvedMethod); 83 } catch (ReflectiveOperationException e) { 84 throw new Error("TEST BUG: can't access private members", e); 85 } 86 List<String> classSymbols = new ArrayList<>(); 87 classSymbols.addAll(NAMES.apply(aClass.getDeclaredFields())); 88 classSymbols.addAll(NAMES.apply(aClass.getDeclaredMethods())); 89 // Check that all members of test class have symbols from constant pool 90 for (String s : classSymbols) { 91 if (!symbols.contains(s)) { 92 // failed. print all symbols found by getSymbol 93 System.out.println("getSymbol data:"); 94 for (String ctvmValue : symbols) { 95 System.out.println(ctvmValue); 96 } 97 throw new AssertionError("Unable to find symbol " + s 98 + " using CompilerToVM.getSymbol"); 99 } 100 } 101 } 102 103 private List<String> getSymbols(HotSpotResolvedJavaMethod 104 metaspaceMethod) throws ReflectiveOperationException { 105 List<String> symbols = new ArrayList<>(); 106 ConstantPool pool = metaspaceMethod.getConstantPool(); 107 long length = pool.length(); 108 // jvms-4.1: The constant_pool table is indexed from 1 ... 109 for (int i = 1; i < length; i++) { 110 if (getTag(pool, i) == CONSTANT_POOL_UTF8_TAG) { 111 long entryPointer; 112 Method getEntryAt = pool.getClass() 113 .getDeclaredMethod("getEntryAt", int.class); 114 getEntryAt.setAccessible(true); 115 entryPointer = (Long) getEntryAt.invoke(pool, i); 116 String symbol = CompilerToVMHelper.getSymbol(entryPointer); 117 symbols.add(symbol); 118 } 119 } 120 return symbols; 121 } 122 123 private int getTag(ConstantPool pool, int index) 124 throws ReflectiveOperationException { 125 Object jvmConstant; 126 Method getTag = pool.getClass().getDeclaredMethod("getTagAt", 127 int.class); 128 getTag.setAccessible(true); 129 jvmConstant = getTag.invoke(pool, index); 130 Field tagCode = jvmConstant.getClass().getDeclaredField("tag"); 131 tagCode.setAccessible(true); 132 return tagCode.getInt(jvmConstant); 133 } 134 }