diff --git a/src/java.base/macosx/native/libjava/ProcessHandleImpl_macosx.c b/src/java.base/macosx/native/libjava/ProcessHandleImpl_macosx.c index 2db64ef37b5d9..06bf394601dc5 100644 --- a/src/java.base/macosx/native/libjava/ProcessHandleImpl_macosx.c +++ b/src/java.base/macosx/native/libjava/ProcessHandleImpl_macosx.c @@ -203,6 +203,11 @@ pid_t os_getParentPidAndTimings(JNIEnv *env, pid_t jpid, struct kinfo_proc kp; size_t bufSize = sizeof kp; + if (jpid == 0) { + // macos would return a process 0. It's not a real process + return -1; + } + // Read the process info for the specific pid int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; diff --git a/test/jdk/java/lang/ProcessHandle/PidZero.java b/test/jdk/java/lang/ProcessHandle/PidZero.java new file mode 100644 index 0000000000000..168b7df45d7b6 --- /dev/null +++ b/test/jdk/java/lang/ProcessHandle/PidZero.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2026, 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.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/* + * @test + * @bug 8381567 + * @summary ProcessHandle.descendants fails with AIOOE for process 0 + * @requires os.family == "mac" + * @run junit PidZero + */ +public class PidZero { + /** + * Test of ProcessHandle.descendants for pid 0 + * Only Macos allows a ProcessHandle to be instantiated for pid 0 + * which includes all processes on the system, but the implementation + * throws ArrayIndexOutOfBoundException without this fix. + */ + @Test + public void test() { + assertTrue(ProcessHandle.of(0).isEmpty()); + } +}