1 /* 2 * Copyright (c) 2020, 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 8249719 27 * @summary ResolvedMethodTable hash function should take method class into account 28 * @run main/othervm/timeout=300 -Xmx256m -XX:MaxMetaspaceSize=256m ResolvedMethodTableHash 200000 29 */ 30 31 import java.lang.invoke.MethodHandle; 32 import java.lang.invoke.MethodHandles; 33 import java.lang.invoke.MethodType; 34 import java.nio.ByteBuffer; 35 import java.util.ArrayList; 36 import java.util.List; 37 38 // The test generates thousands MethodHandles to the methods of the same name 39 // and the same signature. This should not take too long, unless Method hash 40 // function takes only the name and the signature as an input. 41 public class ResolvedMethodTableHash extends ClassLoader { 42 43 // Generate a MethodHandle for ClassName.m() 44 private MethodHandle generate(String className) throws ReflectiveOperationException { 45 byte[] buf = new byte[100]; 46 int size = writeClass(buf, className); 47 Class<?> cls = defineClass(null, buf, 0, size); 48 return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class)); 49 } 50 51 // Produce a class file with the given name and a single method: 52 // public static native void m(); 53 private int writeClass(byte[] buf, String className) { 54 return ByteBuffer.wrap(buf) 55 .putInt(0xCAFEBABE) // magic 56 .putInt(50) // version: 50 57 .putShort((short) 7) // constant_pool_count: 7 58 .put((byte) 7).putShort((short) 2) 59 .put((byte) 1).putShort((short) className.length()).put(className.getBytes()) 60 .put((byte) 7).putShort((short) 4) 61 .put((byte) 1).putShort((short) 16).put("java/lang/Object".getBytes()) 62 .put((byte) 1).putShort((short) 1).put("m".getBytes()) 63 .put((byte) 1).putShort((short) 3).put("()V".getBytes()) 64 .putShort((short) 0x21) // access_flags: public super 65 .putShort((short) 1) // this_class: #1 66 .putShort((short) 3) // super_class: #3 67 .putShort((short) 0) // interfaces_count: 0 68 .putShort((short) 0) // fields_count: 0 69 .putShort((short) 1) // methods_count: 1 70 .putShort((short) 0x109) // access_flags: public static native 71 .putShort((short) 5) // name_index: #5 72 .putShort((short) 6) // descriptor_index: #6 73 .putShort((short) 0) // attributes_count: 0 74 .putShort((short) 0) // attributes_count: 0 75 .position(); 76 } 77 78 public static void main(String[] args) throws Exception { 79 ResolvedMethodTableHash generator = new ResolvedMethodTableHash(); 80 List<MethodHandle> handles = new ArrayList<>(); 81 82 int count = args.length > 0 ? Integer.parseInt(args[0]) : 200000; 83 84 for (int i = 0; i < count; i++) { 85 handles.add(generator.generate("MH$" + i)); 86 if (i % 1000 == 0) { 87 System.out.println("Generated " + i + " handles"); 88 } 89 } 90 91 System.out.println("Test passed"); 92 } 93 }