From 96bd21d38391467bfb9f2f83a4ccf326fc61d9b9 Mon Sep 17 00:00:00 2001 From: David Holmes Date: Fri, 24 Jan 2025 05:00:27 +0000 Subject: [PATCH] 8290043: serviceability/attach/ConcAttachTest.java failed "guarantee(!CheckJNICalls) failed: Attached JNI thread exited without being detached" Backport-of: 9b1bed0aa416c615a81d429e2f1f33bc4f679109 --- src/hotspot/share/prims/jni.cpp | 6 +++++- src/hotspot/share/runtime/javaThread.cpp | 1 - src/hotspot/share/runtime/thread.cpp | 3 +++ test/hotspot/jtreg/ProblemList.txt | 4 +--- .../jni/terminatedThread/TestTerminatedThread.java | 8 +++++--- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/hotspot/share/prims/jni.cpp b/src/hotspot/share/prims/jni.cpp index 40d3b506258..486aae80874 100644 --- a/src/hotspot/share/prims/jni.cpp +++ b/src/hotspot/share/prims/jni.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2024 Red Hat, Inc. * Copyright (c) 2021, Azul Systems, Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. @@ -3795,6 +3795,7 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae MACOS_AARCH64_ONLY(thread->init_wx()); if (!os::create_attached_thread(thread)) { + thread->unregister_thread_stack_with_NMT(); thread->smr_delete(); return JNI_ERR; } @@ -3839,6 +3840,8 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae if (attach_failed) { // Added missing cleanup thread->cleanup_failed_attach_current_thread(daemon); + thread->unregister_thread_stack_with_NMT(); + thread->smr_delete(); return JNI_ERR; } @@ -3935,6 +3938,7 @@ jint JNICALL jni_DetachCurrentThread(JavaVM *vm) { // (platform-dependent) methods where we do alternate stack // maintenance work?) thread->exit(false, JavaThread::jni_detach); + thread->unregister_thread_stack_with_NMT(); thread->smr_delete(); // Go to the execute mode, the initial state of the thread on creation. diff --git a/src/hotspot/share/runtime/javaThread.cpp b/src/hotspot/share/runtime/javaThread.cpp index 7f332638ee0..fd90cd3dc82 100644 --- a/src/hotspot/share/runtime/javaThread.cpp +++ b/src/hotspot/share/runtime/javaThread.cpp @@ -1050,7 +1050,6 @@ void JavaThread::cleanup_failed_attach_current_thread(bool is_daemon) { } Threads::remove(this, is_daemon); - this->smr_delete(); } JavaThread* JavaThread::active() { diff --git a/src/hotspot/share/runtime/thread.cpp b/src/hotspot/share/runtime/thread.cpp index b02166ce681..2c182ab2a98 100644 --- a/src/hotspot/share/runtime/thread.cpp +++ b/src/hotspot/share/runtime/thread.cpp @@ -247,6 +247,9 @@ void Thread::call_run() { // asynchronously with respect to its termination - that is what _run_state can // be used to check. + // Logically we should do this->unregister_thread_stack_with_NMT() here, but we + // had to move that into post_run() because of the `this` deletion issue. + assert(Thread::current_or_null() == nullptr, "current thread still present"); } diff --git a/test/hotspot/jtreg/ProblemList.txt b/test/hotspot/jtreg/ProblemList.txt index a466b4c9321..a6ff888f030 100644 --- a/test/hotspot/jtreg/ProblemList.txt +++ b/test/hotspot/jtreg/ProblemList.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2016, 2025, 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 @@ -148,8 +148,6 @@ serviceability/sa/ClhsdbPstack.java#core 8267433,8318754 macosx-x64,mac serviceability/sa/TestJmapCore.java 8267433,8318754 macosx-x64,macosx-aarch64 serviceability/sa/TestJmapCoreMetaspace.java 8267433,8318754 macosx-x64,macosx-aarch64 -serviceability/attach/ConcAttachTest.java 8290043 linux-all - serviceability/jvmti/stress/StackTrace/NotSuspended/GetStackTraceNotSuspendedStressTest.java 8315980 linux-all,windows-x64 ############################################################################# diff --git a/test/hotspot/jtreg/runtime/jni/terminatedThread/TestTerminatedThread.java b/test/hotspot/jtreg/runtime/jni/terminatedThread/TestTerminatedThread.java index abc0b176d88..7f56b0cae42 100644 --- a/test/hotspot/jtreg/runtime/jni/terminatedThread/TestTerminatedThread.java +++ b/test/hotspot/jtreg/runtime/jni/terminatedThread/TestTerminatedThread.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, 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 @@ -32,8 +32,10 @@ * @library /testlibrary * @summary Basic test of Thread and ThreadMXBean queries on a natively * attached thread that has failed to detach before terminating. - * @comment The native code only supports POSIX so no windows testing - * @run main/othervm/native TestTerminatedThread + * @comment The native code only supports POSIX so no windows testing. + * @comment Disable -Xcheck:jni else NMT can report a fatal error because + * we did not detach before exiting. + * @run main/othervm/native -XX:-CheckJNICalls TestTerminatedThread */ import jvmti.JVMTIUtils;