Path: blob/master/test/hotspot/jtreg/runtime/MemberName/MemberNameLeak.java
40942 views
/*1* Copyright (c) 2017, 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 8174749 821330726* @summary MemberNameTable should reuse entries27* @library /test/lib28* @modules java.base/jdk.internal.misc29* @modules java.compiler30* @build sun.hotspot.WhiteBox31* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox32* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. MemberNameLeak33*/3435import java.io.*;36import java.nio.file.*;37import java.lang.invoke.*;38import java.lang.reflect.*;39import java.text.*;40import java.util.*;41import jdk.test.lib.process.OutputAnalyzer;42import jdk.test.lib.process.ProcessTools;43import jdk.test.lib.Utils;44import sun.hotspot.WhiteBox;45import sun.hotspot.code.Compiler;46import sun.hotspot.gc.GC;47import jdk.test.lib.classloader.ClassWithManyMethodsClassLoader;4849public class MemberNameLeak {50private static String className = "MemberNameLeakTestClass";51private static String methodPrefix = "method";52// The size of the ResolvedMethodTable is 1024. 2000 entries53// is enough to trigger a grow/cleaning of the table after a GC.54private static int methodCount = 2000;55public static ArrayList<MethodHandle> keepAlive;5657static class Leak {58public void callMe() {59}6061public static void main(String[] args) throws Throwable {62Leak leak = new Leak();63WhiteBox wb = WhiteBox.getWhiteBox();6465keepAlive = new ArrayList<>(methodCount);6667ClassWithManyMethodsClassLoader classLoader = new ClassWithManyMethodsClassLoader();68Class<?> clazz = classLoader.create(className, methodPrefix, methodCount);6970long before = wb.resolvedMethodItemsCount();7172Object o = clazz.newInstance();73MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());7475for (int i = 0; i < methodCount; i++) {76MethodType mt = MethodType.fromMethodDescriptorString("()V", classLoader);77String methodName = methodPrefix + i;78// findSpecial leaks some native mem79// Add entry to ResolvedMethodTable.80MethodHandle mh0 = lookup.findSpecial(clazz, methodName, mt, clazz);81// Find entry in ResolvedMethodTable.82MethodHandle mh1 = lookup.findSpecial(clazz, methodName, mt, clazz);8384mh1.invoke(o);8586keepAlive.add(mh1);87}8889long after = wb.resolvedMethodItemsCount();9091System.out.println("wb.resolvedMethodItemsCount() after setup: " + after);9293if (after == before) {94throw new RuntimeException("Too few resolved methods");95}9697keepAlive = null;9899// Wait until ServiceThread cleans ResolvedMethod table100int cnt = 0;101while (true) {102if (cnt++ % 30 == 0) {103System.gc(); // make mh unused104}105106if (after > wb.resolvedMethodItemsCount() + 50) {107// Entries have been removed.108break;109}110111Thread.sleep(100);112}113}114}115116private static Path createGcLogPath(String prefix) throws IOException {117Path gcLog = Utils.createTempFile(prefix, "log");118Files.delete(gcLog);119return gcLog;120}121122private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");123124public static void test(GC gc, boolean doConcurrent) throws Throwable {125Path gcLogPath = createGcLogPath("gc." + gc + "." + doConcurrent);126System.err.println("test(" + gc + ", " + doConcurrent + ")" + " " + dateFormat.format(new Date()));127// Run this Leak class with logging128ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(129"-Xlog:membername+table=trace,gc+verify=debug,gc:" + gcLogPath + ":time,utctime,uptime,pid,level,tags",130"-XX:+UnlockExperimentalVMOptions",131"-XX:+UnlockDiagnosticVMOptions",132"-XX:+WhiteBoxAPI",133"-Xbootclasspath/a:.",134"-XX:+VerifyBeforeGC",135"-XX:+VerifyAfterGC",136doConcurrent ? "-XX:+ExplicitGCInvokesConcurrent" : "-XX:-ExplicitGCInvokesConcurrent",137"-XX:+ClassUnloading",138"-XX:+ClassUnloadingWithConcurrentMark",139"-XX:+Use" + gc + "GC",140Leak.class.getName());141142// Check process143OutputAnalyzer output = new OutputAnalyzer(pb.start());144output.outputTo(System.out);145output.errorTo(System.err);146output.shouldHaveExitValue(0);147148// Check gc log file149OutputAnalyzer gcLogOutput = new OutputAnalyzer(gcLogPath);150151// Hardcoded names for classes generated by GeneratedClassLoader152String descriptor = className + "." + methodPrefix + "0()V";153gcLogOutput.shouldContain("ResolvedMethod entry added for " + descriptor);154gcLogOutput.shouldContain("ResolvedMethod entry found for " + descriptor);155gcLogOutput.shouldContain("ResolvedMethod entry removed");156157System.err.println("test(" + gc + ", " + doConcurrent + ")" + " done " + dateFormat.format(new Date()));158}159160private static boolean supportsSTW(GC gc) {161return !(gc == GC.Epsilon);162}163164private static boolean supportsConcurrent(GC gc) {165return !(gc == GC.Epsilon || gc == GC.Serial || gc == GC.Parallel);166}167168private static void test(GC gc) throws Throwable {169if (supportsSTW(gc)) {170test(gc, false);171}172if (supportsConcurrent(gc)) {173test(gc, true);174}175}176177public static void main(java.lang.String[] unused) throws Throwable {178test(GC.selected());179}180}181182183