From 9cc0c530e92ef6b596d2e33b83dd3c431aebcdf6 Mon Sep 17 00:00:00 2001 From: Andrew Lu Date: Thu, 23 May 2024 09:19:18 +0000 Subject: [PATCH 01/10] 8321107: Add more test cases for JDK-8319372 Backport-of: ecd335d8f42757d332f217e220e1a9db8c48c8d6 --- .../TestTopCastIIOnUndetectedDeadPath4.java | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 test/hotspot/jtreg/compiler/c2/TestTopCastIIOnUndetectedDeadPath4.java diff --git a/test/hotspot/jtreg/compiler/c2/TestTopCastIIOnUndetectedDeadPath4.java b/test/hotspot/jtreg/compiler/c2/TestTopCastIIOnUndetectedDeadPath4.java new file mode 100644 index 00000000000..566295b844b --- /dev/null +++ b/test/hotspot/jtreg/compiler/c2/TestTopCastIIOnUndetectedDeadPath4.java @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test id=JDK-8293941 + * @bug 8319372 8293941 + * @summary Tests that CastII are not dying anymore and breaking the graph due to control that is not removed + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:-RangeCheckElimination + * -Xcomp -XX:CompileOnly=compiler.c2.TestTopCastIIOnUndetectedDeadPath4::* + * compiler.c2.TestTopCastIIOnUndetectedDeadPath4 + */ + +/* + * @test id=JDK-8314111 + * @bug 8319372 8314111 + * @summary Tests that CastII are not dying anymore and breaking the graph due to control that is not removed + * @run main/othervm -Xcomp -XX:CompileOnly=compiler.c2.TestTopCastIIOnUndetectedDeadPath4::test* + * compiler.c2.TestTopCastIIOnUndetectedDeadPath4 + */ + +/* + * @test id=NoFlags + * @summary Tests that CastII are not dying anymore and breaking the graph due to control that is not removed + * @run main/othervm compiler.c2.TestTopCastIIOnUndetectedDeadPath4 + */ + +package compiler.c2; + +public class TestTopCastIIOnUndetectedDeadPath4 { + + static boolean bFld; + static int iArrFld[]; + static long lArrFld[]; + static double dArrFld[][]; + + public static void main(String[] strArr) { + for (int i = 0; i < 5000; i++) { + test8293941(); + test8314111_1(); + test8314111_2(); + } + } + + static void test8293941() { + int i16; + boolean b = false; + for (double d1 = 31.2; d1 < 72; d1++) { + for (i16 = (int) d1; i16 < 2; ++i16) { + iArrFld[i16] >>= 5; + dArrFld[i16 - 1][i16] = 3; + if (b) { + break; + } + lArrFld[i16] = 4; + } + switch (0) { + case 5: + b = b; + } + } + } + + static void test8314111_1() { + int i, i1 = 0, i28, i30 = 0, iArr[] = new int[10]; + boolean bArr[] = new boolean[10]; + i = 1; + while (++i < 5) { + try { + i1 = iArr[i - 1]; + i1 = 2 / i; + } catch (ArithmeticException a_e) { + } + if (bFld) { + switch (i) { + case 4: + for (i28 = 3; 100 > i28; i28++) { + i1 -= i28; + } + if ((i30 -= 3) > 0) { + switch (i30) { + case 4: + bArr[i - 1] = bFld; + iArr[i] = 6; + } + } + } + } + } + } + + static void test8314111_2() { + int iArr[] = new int[1000]; + boolean bArr[] = new boolean[1000]; + int x = 0; + int i = 1; + while (++i < 5) { + try { + x = iArr[i - 1]; + x = 2 / i; + } catch (ArithmeticException a_e) { + } + if (bFld) { + x++; + bArr[i - 1] = false; + iArr[i] = 0; + } + } + } +} + +class Foo { + public static void empty() { + } +} From 3fc5ee98bdf06cf797e19f6381a2547000fcf786 Mon Sep 17 00:00:00 2001 From: Andrew Lu Date: Thu, 23 May 2024 09:20:50 +0000 Subject: [PATCH 02/10] 8319338: tools/jpackage/share/RuntimeImageTest.java fails with -XX:+UseZGC Backport-of: e9eb8b98f4dd949c8a0f501189471e11b837d936 --- test/jdk/tools/jpackage/share/RuntimeImageTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jdk/tools/jpackage/share/RuntimeImageTest.java b/test/jdk/tools/jpackage/share/RuntimeImageTest.java index 3d66a163c07..3b57536213b 100644 --- a/test/jdk/tools/jpackage/share/RuntimeImageTest.java +++ b/test/jdk/tools/jpackage/share/RuntimeImageTest.java @@ -47,7 +47,7 @@ * @build jdk.jpackage.test.* * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal * @compile RuntimeImageTest.java - * @run main/othervm/timeout=1400 -Xmx512m jdk.jpackage.test.Main + * @run main/othervm/timeout=1400 jdk.jpackage.test.Main * --jpt-run=RuntimeImageTest */ From bc96dab7d712f0c93328aa33c7b5bc84953005af Mon Sep 17 00:00:00 2001 From: Andrew Lu Date: Thu, 23 May 2024 09:21:13 +0000 Subject: [PATCH 03/10] 8312194: test/hotspot/jtreg/applications/ctw/modules/jdk_crypto_ec.java cannot handle empty modules Backport-of: e7c83ea948f8b2cd7caf7e59d3cf6b087807dba7 --- .../ctw/modules/jdk_crypto_ec.java | 38 ------------------- 1 file changed, 38 deletions(-) delete mode 100644 test/hotspot/jtreg/applications/ctw/modules/jdk_crypto_ec.java diff --git a/test/hotspot/jtreg/applications/ctw/modules/jdk_crypto_ec.java b/test/hotspot/jtreg/applications/ctw/modules/jdk_crypto_ec.java deleted file mode 100644 index 09e173b4801..00000000000 --- a/test/hotspot/jtreg/applications/ctw/modules/jdk_crypto_ec.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @summary run CTW for all classes from jdk.crypto.ec module - * - * @library /test/lib / /testlibrary/ctw/src - * @modules java.base/jdk.internal.access - * java.base/jdk.internal.jimage - * java.base/jdk.internal.misc - * java.base/jdk.internal.reflect - * @modules jdk.crypto.ec - * - * @build jdk.test.whitebox.WhiteBox - * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox - * @run driver/timeout=7200 sun.hotspot.tools.ctw.CtwRunner modules:jdk.crypto.ec - */ From aeb4aa893bf962213b79d5cf588dd32f1f05f578 Mon Sep 17 00:00:00 2001 From: Thomas Fitzsimmons Date: Thu, 23 May 2024 21:17:41 +0000 Subject: [PATCH 04/10] 8286781: Replace the deprecated/obsolete gethostbyname and inet_addr calls Reviewed-by: andrew Backport-of: d7298245d6759f62e253b5cf0df975db17fdbf82 --- make/autoconf/libraries.m4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make/autoconf/libraries.m4 b/make/autoconf/libraries.m4 index 62db5b16c31..865feea36d9 100644 --- a/make/autoconf/libraries.m4 +++ b/make/autoconf/libraries.m4 @@ -154,7 +154,7 @@ AC_DEFUN_ONCE([LIB_SETUP_LIBRARIES], if test "x$OPENJDK_TARGET_OS" = xwindows; then BASIC_JVM_LIBS="$BASIC_JVM_LIBS kernel32.lib user32.lib gdi32.lib winspool.lib \ comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib powrprof.lib uuid.lib \ - wsock32.lib winmm.lib version.lib psapi.lib" + ws2_32.lib winmm.lib version.lib psapi.lib" fi JDKLIB_LIBS="$BASIC_JDKLIB_LIBS" From 095f8183d9d9cbd80a69e481193d1189ed36681c Mon Sep 17 00:00:00 2001 From: Aleksey Shipilev Date: Mon, 27 May 2024 08:30:05 +0000 Subject: [PATCH 05/10] 8177107: Reduce memory footprint of java.lang.reflect.Constructor/Method Backport-of: a385142398eee102ff1a53d848230dc95c4ebd37 --- .../share/classes/java/lang/reflect/Constructor.java | 5 +++-- src/java.base/share/classes/java/lang/reflect/Method.java | 5 +++-- .../reflect/generics/repository/GenericDeclRepository.java | 7 ++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/java.base/share/classes/java/lang/reflect/Constructor.java b/src/java.base/share/classes/java/lang/reflect/Constructor.java index eec4512b632..e06750c98d6 100644 --- a/src/java.base/share/classes/java/lang/reflect/Constructor.java +++ b/src/java.base/share/classes/java/lang/reflect/Constructor.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -33,6 +33,7 @@ import sun.reflect.annotation.TypeAnnotation; import sun.reflect.annotation.TypeAnnotationParser; import sun.reflect.generics.repository.ConstructorRepository; +import sun.reflect.generics.repository.GenericDeclRepository; import sun.reflect.generics.factory.CoreReflectionFactory; import sun.reflect.generics.factory.GenericsFactory; import sun.reflect.generics.scope.ConstructorScope; @@ -241,7 +242,7 @@ public TypeVariable>[] getTypeParameters() { if (getSignature() != null) { return (TypeVariable>[])getGenericInfo().getTypeParameters(); } else - return (TypeVariable>[])new TypeVariable[0]; + return (TypeVariable>[])GenericDeclRepository.EMPTY_TYPE_VARS; } diff --git a/src/java.base/share/classes/java/lang/reflect/Method.java b/src/java.base/share/classes/java/lang/reflect/Method.java index dedf2d5dd91..e471e6e5f07 100644 --- a/src/java.base/share/classes/java/lang/reflect/Method.java +++ b/src/java.base/share/classes/java/lang/reflect/Method.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -34,6 +34,7 @@ import jdk.internal.vm.annotation.Stable; import sun.reflect.annotation.ExceptionProxy; import sun.reflect.annotation.TypeNotPresentExceptionProxy; +import sun.reflect.generics.repository.GenericDeclRepository; import sun.reflect.generics.repository.MethodRepository; import sun.reflect.generics.factory.CoreReflectionFactory; import sun.reflect.generics.factory.GenericsFactory; @@ -253,7 +254,7 @@ public TypeVariable[] getTypeParameters() { if (getGenericSignature() != null) return (TypeVariable[])getGenericInfo().getTypeParameters(); else - return (TypeVariable[])new TypeVariable[0]; + return (TypeVariable[])GenericDeclRepository.EMPTY_TYPE_VARS; } /** diff --git a/src/java.base/share/classes/sun/reflect/generics/repository/GenericDeclRepository.java b/src/java.base/share/classes/sun/reflect/generics/repository/GenericDeclRepository.java index 5a34ab1749a..84152632047 100644 --- a/src/java.base/share/classes/sun/reflect/generics/repository/GenericDeclRepository.java +++ b/src/java.base/share/classes/sun/reflect/generics/repository/GenericDeclRepository.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -42,6 +42,8 @@ public abstract class GenericDeclRepository extends AbstractRepository { + public static final TypeVariable[] EMPTY_TYPE_VARS = new TypeVariable[0]; + /** The formal type parameters. Lazily initialized. */ private volatile TypeVariable[] typeParameters; @@ -77,6 +79,9 @@ private TypeVariable[] computeTypeParameters() { FormalTypeParameter[] ftps = getTree().getFormalTypeParameters(); // create array to store reified subtree(s) int length = ftps.length; + if (length == 0) { + return EMPTY_TYPE_VARS; + } TypeVariable[] typeParameters = new TypeVariable[length]; // reify all subtrees for (int i = 0; i < length; i++) { From 9cf82c9669fd70273d1c5c461253dfef1b817dec Mon Sep 17 00:00:00 2001 From: Andrew Lu Date: Mon, 27 May 2024 09:32:02 +0000 Subject: [PATCH 06/10] 8320681: [macos] Test tools/jpackage/macosx/MacAppStoreJlinkOptionsTest.java timed out on macOS Backport-of: a657aa38a56056211a9d2773b30e8fe1a89c128e --- .../tools/jpackage/macosx/MacAppStoreJlinkOptionsTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/jdk/tools/jpackage/macosx/MacAppStoreJlinkOptionsTest.java b/test/jdk/tools/jpackage/macosx/MacAppStoreJlinkOptionsTest.java index b3cefa7d38f..8b1cec04ab9 100644 --- a/test/jdk/tools/jpackage/macosx/MacAppStoreJlinkOptionsTest.java +++ b/test/jdk/tools/jpackage/macosx/MacAppStoreJlinkOptionsTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -39,7 +39,7 @@ * @build MacAppStoreJLinkOptionsTest * @modules jdk.jpackage/jdk.jpackage.internal * @requires (os.family == "mac") - * @run main/othervm -Xmx512m jdk.jpackage.test.Main + * @run main/othervm/timeout=540 -Xmx512m jdk.jpackage.test.Main * --jpt-run=MacAppStoreJLinkOptionsTest */ public class MacAppStoreJLinkOptionsTest { From 6964d01a7677fbb754a8cb748639c955393f5a45 Mon Sep 17 00:00:00 2001 From: SendaoYan Date: Tue, 28 May 2024 05:17:22 +0000 Subject: [PATCH 07/10] 8314835: gtest wrappers should be marked as flagless Backport-of: 1ea6463fbb95258725ed4a11621ec662859a76e0 --- test/hotspot/jtreg/gtest/AsyncLogGtest.java | 2 ++ test/hotspot/jtreg/gtest/NMTGtests.java | 5 ++++- test/hotspot/jtreg/gtest/NativeHeapTrimmerGtest.java | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/test/hotspot/jtreg/gtest/AsyncLogGtest.java b/test/hotspot/jtreg/gtest/AsyncLogGtest.java index f85f6d5c7e2..302730b1515 100644 --- a/test/hotspot/jtreg/gtest/AsyncLogGtest.java +++ b/test/hotspot/jtreg/gtest/AsyncLogGtest.java @@ -1,5 +1,6 @@ /* * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -33,6 +34,7 @@ * @library /test/lib * @modules java.base/jdk.internal.misc * java.xml + * @requires vm.flagless * @run main/native GTestWrapper --gtest_filter=AsyncLogTest* -Xlog:async * @run main/native GTestWrapper --gtest_filter=Log*Test* -Xlog:async */ diff --git a/test/hotspot/jtreg/gtest/NMTGtests.java b/test/hotspot/jtreg/gtest/NMTGtests.java index 57666680b6b..6516fde8da5 100644 --- a/test/hotspot/jtreg/gtest/NMTGtests.java +++ b/test/hotspot/jtreg/gtest/NMTGtests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2021 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -32,6 +32,7 @@ * @library /test/lib * @modules java.base/jdk.internal.misc * java.xml + * @requires vm.flagless * @run main/native GTestWrapper --gtest_filter=NMT*:os* -XX:NativeMemoryTracking=off */ @@ -40,6 +41,7 @@ * @library /test/lib * @modules java.base/jdk.internal.misc * java.xml + * @requires vm.flagless * @run main/native GTestWrapper --gtest_filter=NMT*:os* -XX:NativeMemoryTracking=summary */ @@ -48,5 +50,6 @@ * @library /test/lib * @modules java.base/jdk.internal.misc * java.xml + * @requires vm.flagless * @run main/native GTestWrapper --gtest_filter=NMT*:os* -XX:NativeMemoryTracking=detail */ diff --git a/test/hotspot/jtreg/gtest/NativeHeapTrimmerGtest.java b/test/hotspot/jtreg/gtest/NativeHeapTrimmerGtest.java index 259ed5b5146..82ee888f653 100644 --- a/test/hotspot/jtreg/gtest/NativeHeapTrimmerGtest.java +++ b/test/hotspot/jtreg/gtest/NativeHeapTrimmerGtest.java @@ -28,5 +28,6 @@ * @library /test/lib * @modules java.base/jdk.internal.misc * java.xml + * @requires vm.flagless * @run main/native GTestWrapper --gtest_filter=os.trim* -Xlog:trimnative -XX:TrimNativeHeapInterval=100 */ From d31a531ead6da5e8baefef0e75bfb6492ded72be Mon Sep 17 00:00:00 2001 From: Rinku Kumar Maurya Date: Tue, 28 May 2024 08:32:14 +0000 Subject: [PATCH 08/10] 8289643: File descriptor leak with ProcessBuilder.startPipeline 8290885: java/lang/ProcessBuilder/PipelineLeaksFD.java fail: More or fewer pipes than expected Reviewed-by: shade Backport-of: 620c8a045f92126c2552347b9f369405ab2d6d36 --- .../classes/java/lang/ProcessBuilder.java | 6 +- .../lang/ProcessBuilder/PipelineLeaksFD.java | 145 ++++++++++++++++++ 2 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 test/jdk/java/lang/ProcessBuilder/PipelineLeaksFD.java diff --git a/src/java.base/share/classes/java/lang/ProcessBuilder.java b/src/java.base/share/classes/java/lang/ProcessBuilder.java index 1bbbec2a847..396b87f4c54 100644 --- a/src/java.base/share/classes/java/lang/ProcessBuilder.java +++ b/src/java.base/share/classes/java/lang/ProcessBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1296,6 +1296,10 @@ public static List startPipeline(List builders) throws redirects[1] = new RedirectPipeImpl(); // placeholder for new output } processes.add(builder.start(redirects)); + if (prevOutput instanceof RedirectPipeImpl redir) { + // Wrap the fd so it can be closed + new Process.PipeInputStream(redir.getFd()).close(); + } prevOutput = redirects[1]; } } catch (Exception ex) { diff --git a/test/jdk/java/lang/ProcessBuilder/PipelineLeaksFD.java b/test/jdk/java/lang/ProcessBuilder/PipelineLeaksFD.java new file mode 100644 index 00000000000..4f572610b9d --- /dev/null +++ b/test/jdk/java/lang/ProcessBuilder/PipelineLeaksFD.java @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/* + * @test + * @bug 8289643 + * @requires (os.family == "linux" & !vm.musl) + * @summary file descriptor leak with ProcessBuilder.startPipeline + * @run testng/othervm PipelineLeaksFD + */ + +@Test +public class PipelineLeaksFD { + @DataProvider + public Object[][] builders() { + return new Object[][]{ + {List.of(new ProcessBuilder("cat"))}, + {List.of(new ProcessBuilder("cat"), + new ProcessBuilder("cat"))}, + {List.of(new ProcessBuilder("cat"), + new ProcessBuilder("cat"), + new ProcessBuilder("cat"), + new ProcessBuilder("cat"), + new ProcessBuilder("cat"))}, + }; + } + + @Test(dataProvider = "builders") + void checkForLeaks(List builders) throws IOException { + + Set pipesBefore = myPipes(); + if (pipesBefore.size() < 3) { + System.out.println(pipesBefore); + Assert.fail("There should be at least 3 pipes before, (0, 1, 2)"); + } + + // Redirect all of the error streams to stdout (except the last) + // so those file descriptors are not left open + for (int i = 0; i < builders.size() - 1; i++) { + builders.get(i).redirectErrorStream(true); + } + + List processes = ProcessBuilder.startPipeline(builders); + + // Write something through the pipeline + try (OutputStream out = processes.get(0).getOutputStream()) { + out.write('a'); + } + + Process last = processes.get(processes.size() - 1); + try (InputStream inputStream = last.getInputStream(); + InputStream errorStream = last.getErrorStream()) { + byte[] bytes = inputStream.readAllBytes(); + Assert.assertEquals(bytes.length, 1, "stdout bytes read"); + byte[] errBytes = errorStream.readAllBytes(); + Assert.assertEquals(errBytes.length, 0, "stderr bytes read"); + } + + processes.forEach(p -> waitForQuiet(p)); + + Set pipesAfter = myPipes(); + if (!pipesBefore.equals(pipesAfter)) { + Set missing = new HashSet<>(pipesBefore); + missing.removeAll(pipesAfter); + printPipes(missing, "Missing from pipesAfter"); + Set extra = new HashSet<>(pipesAfter); + extra.removeAll(pipesBefore); + printPipes(extra, "Extra pipes in pipesAfter"); + Assert.fail("More or fewer pipes than expected"); + } + } + + static void printPipes(Set pipes, String label) { + System.out.printf("%s: [%d]%n", label, pipes.size()); + pipes.forEach(r -> System.out.printf("%-20s: %s%n", r.fd(), r.link())); + } + + static void waitForQuiet(Process p) { + try { + int st = p.waitFor(); + if (st != 0) { + System.out.println("non-zero exit status: " + p); + } + } catch (InterruptedException ie) { + } + } + + /** + * Collect a Set of pairs of /proc fd paths and the symbol links that are pipes. + * @return A set of PipeRecords, possibly empty + */ + static Set myPipes() { + Path path = Path.of("/proc/" + ProcessHandle.current().pid() + "/fd"); + Set pipes = new HashSet<>(); + File[] files = path.toFile().listFiles(f -> Files.isSymbolicLink(f.toPath())); + if (files != null) { + for (File file : files) { + try { + Path link = Files.readSymbolicLink(file.toPath()); + if (link.toString().startsWith("pipe:")) { + pipes.add(new PipeRecord(file.toPath(), link)); + } + } catch (IOException ioe) { + } + } + } + return pipes; + } + + record PipeRecord(Path fd, Path link) { }; +} From 289bb59a456e3be4cc43024ce65a58cf197474b6 Mon Sep 17 00:00:00 2001 From: Martin Doerr Date: Tue, 28 May 2024 20:05:16 +0000 Subject: [PATCH 09/10] 8294699: Launcher causes lingering busy cursor Backport-of: d3df3eb5d7f5537ade917db7a36caba028f94111 --- .../native/applauncher/WinLauncher.cpp | 80 ++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp b/src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp index 9a3cbaaaefb..7522beaf2de 100644 --- a/src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp +++ b/src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -134,6 +134,82 @@ tstring getJvmLibPath(const Jvm& jvm) { } +class RunExecutorWithMsgLoop { +public: + static DWORD apply(const Executor& exec) { + RunExecutorWithMsgLoop instance(exec); + + UniqueHandle threadHandle = UniqueHandle(CreateThread(NULL, 0, worker, + static_cast(&instance), 0, NULL)); + if (threadHandle.get() == NULL) { + JP_THROW(SysError("CreateThread() failed", CreateThread)); + } + + MSG msg; + BOOL bRet; + while((bRet = GetMessage(&msg, instance.hwnd, 0, 0 )) != 0) { + if (bRet == -1) { + JP_THROW(SysError("GetMessage() failed", GetMessage)); + } else { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + + // Wait for worker thread to terminate to guarantee it will not linger + // around after the thread running a message loop terminates. + const DWORD res = ::WaitForSingleObject(threadHandle.get(), INFINITE); + if (WAIT_FAILED == res) { + JP_THROW(SysError("WaitForSingleObject() failed", + WaitForSingleObject)); + } + + LOG_TRACE(tstrings::any() + << "Executor worker thread terminated. Exit code=" + << instance.exitCode); + return instance.exitCode; + } + +private: + RunExecutorWithMsgLoop(const Executor& v): exec(v) { + exitCode = 1; + + // Message-only window. + hwnd = CreateWindowEx(0, _T("STATIC"), _T(""), 0, 0, 0, 0, 0, + HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); + if (!hwnd) { + JP_THROW(SysError("CreateWindowEx() failed", CreateWindowEx)); + } + } + + static DWORD WINAPI worker(LPVOID param) { + static_cast(param)->run(); + return 0; + } + + void run() { + JP_TRY; + exitCode = static_cast(exec.execAndWaitForExit()); + JP_CATCH_ALL; + + JP_TRY; + if (!PostMessage(hwnd, WM_QUIT, 0, 0)) { + JP_THROW(SysError("PostMessage(WM_QUIT) failed", PostMessage)); + } + return; + JP_CATCH_ALL; + + // All went wrong, PostMessage() failed. Just terminate with error code. + exit(1); + } + +private: + const Executor& exec; + DWORD exitCode; + HWND hwnd; +}; + + void launchApp() { // [RT-31061] otherwise UI can be left in back of other windows. ::AllowSetForegroundWindow(ASFW_ANY); @@ -180,7 +256,7 @@ void launchApp() { exec.arg(arg); }); - DWORD exitCode = static_cast(exec.execAndWaitForExit()); + DWORD exitCode = RunExecutorWithMsgLoop::apply(exec); exit(exitCode); return; From d796dcb4d732ec9b96b8a8539dadeca3ee1d365f Mon Sep 17 00:00:00 2001 From: Martin Doerr Date: Wed, 29 May 2024 06:57:35 +0000 Subject: [PATCH 10/10] 8325203: System.exit(0) kills the launched 3rd party application Backport-of: b9ca2532287b02388cd6f9e69b02a86713fc5b88 --- src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp b/src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp index 7522beaf2de..2a8aaf8ad8d 100644 --- a/src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp +++ b/src/jdk.jpackage/windows/native/applauncher/WinLauncher.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -241,7 +241,7 @@ void launchApp() { } JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo = { }; jobInfo.BasicLimitInformation.LimitFlags = - JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK; if (!SetInformationJobObject(jobHandle.get(), JobObjectExtendedLimitInformation, &jobInfo, sizeof(jobInfo))) { JP_THROW(SysError(tstrings::any() <<