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