8205878: pthread_getcpuclockid is expected to return 0 code
Reviewed-by: cjplummer, amenkov, coleenp
--- a/make/test/JtregNativeHotspot.gmk Mon Jul 09 14:21:33 2018 -0700
+++ b/make/test/JtregNativeHotspot.gmk Mon Jul 09 20:17:32 2018 -0400
@@ -836,6 +836,10 @@
################################################################################
+ifeq ($(TOOLCHAIN_TYPE), solstudio)
+ BUILD_HOTSPOT_JTREG_LIBRARIES_CFLAGS_libji06t001 += -erroff=E_END_OF_LOOP_CODE_NOT_REACHED
+endif
+
# Platform specific setup
ifneq ($(OPENJDK_TARGET_OS)-$(OPENJDK_TARGET_CPU_ARCH), solaris-sparc)
BUILD_HOTSPOT_JTREG_EXCLUDE += liboverflow.c exeThreadSignalMask.c
@@ -858,9 +862,13 @@
BUILD_HOTSPOT_JTREG_EXECUTABLES_LIBS_exesigtest := -ljvm
+ifeq ($(OPENJDK_TARGET_OS), solaris)
+ BUILD_HOTSPOT_JTREG_EXCLUDE += libterminatedThread.c
+endif
+
ifeq ($(OPENJDK_TARGET_OS), windows)
BUILD_HOTSPOT_JTREG_EXECUTABLES_CFLAGS_exeFPRegs := -MT
- BUILD_HOTSPOT_JTREG_EXCLUDE += exesigtest.c
+ BUILD_HOTSPOT_JTREG_EXCLUDE += exesigtest.c libterminatedThread.c
else
BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libbootclssearch_agent += -lpthread
@@ -1494,6 +1502,7 @@
BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libvmdeath001 += -lpthread
BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libgetphase001 += -lpthread
BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libgetphase002 += -lpthread
+ BUILD_HOTSPOT_JTREG_LIBRARIES_LIBS_libterminatedThread += -lpthread
endif
$(eval $(call SetupTestFilesCompilation, BUILD_HOTSPOT_JTREG_LIBRARIES, \
--- a/src/hotspot/os/linux/os_linux.cpp Mon Jul 09 14:21:33 2018 -0700
+++ b/src/hotspot/os/linux/os_linux.cpp Mon Jul 09 20:17:32 2018 -0400
@@ -5572,14 +5572,18 @@
static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);
-static clockid_t thread_cpu_clockid(Thread* thread) {
- pthread_t tid = thread->osthread()->pthread_id();
- clockid_t clockid;
-
- // Get thread clockid
- int rc = os::Linux::pthread_getcpuclockid(tid, &clockid);
- assert(rc == 0, "pthread_getcpuclockid is expected to return 0 code");
- return clockid;
+static jlong fast_cpu_time(Thread *thread) {
+ clockid_t clockid;
+ int rc = os::Linux::pthread_getcpuclockid(thread->osthread()->pthread_id(),
+ &clockid);
+ if (rc == 0) {
+ return os::Linux::fast_thread_cpu_time(clockid);
+ } else {
+ // It's possible to encounter a terminated native thread that failed
+ // to detach itself from the VM - which should result in ESRCH.
+ assert_status(rc == ESRCH, rc, "pthread_getcpuclockid failed");
+ return -1;
+ }
}
// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
@@ -5601,7 +5605,7 @@
jlong os::thread_cpu_time(Thread* thread) {
// consistent with what current_thread_cpu_time() returns
if (os::Linux::supports_fast_thread_cpu_time()) {
- return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
+ return fast_cpu_time(thread);
} else {
return slow_thread_cpu_time(thread, true /* user + sys */);
}
@@ -5617,7 +5621,7 @@
jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
- return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
+ return fast_cpu_time(thread);
} else {
return slow_thread_cpu_time(thread, user_sys_cpu_time);
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/jni/terminatedThread/TestTerminatedThread.java Mon Jul 09 20:17:32 2018 -0400
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2018, 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 java.lang.management.*;
+
+/*
+ * @test
+ * @bug 8205878
+ * @requires os.family != "windows" & os.family != "solaris"
+ * @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; also
+ * we have to skip solaris as a terminating thread that fails to
+ * detach will hit an infinite loop due to TLS destructor issues - see
+ * comments in JDK-8156708
+ * @run main/native TestTerminatedThread
+ */
+
+public class TestTerminatedThread {
+
+ static native Thread createTerminatedThread();
+
+ static {
+ System.loadLibrary("terminatedThread");
+ }
+
+ private static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
+
+ public static void main(String[] args) throws Throwable {
+
+ Thread t = createTerminatedThread();
+
+ if (!t.isAlive())
+ throw new Error("Thread is only supposed to terminate at native layer!");
+
+ // Now invoke the various functions on this thread to
+ // make sure the VM handles it okay. The focus is on
+ // functions with an underlying native OS implementation.
+ // Generally as long as we don't crash or throw unexpected
+ // exceptions then the test passes. In some cases we know exactly
+ // what a function should return and so can check that.
+
+ System.out.println("Working with thread: " + t +
+ ", in state: " + t.getState());
+
+ System.out.println("Calling suspend ...");
+ t.suspend();
+ System.out.println("Calling resume ...");
+ t.resume();
+ System.out.println("Calling getStackTrace ...");
+ StackTraceElement[] stack = t.getStackTrace();
+ System.out.println(java.util.Arrays.toString(stack));
+ if (stack.length != 0)
+ throw new Error("Terminated thread should have empty java stack trace");
+ System.out.println("Calling setName(\"NewName\") ...");
+ t.setName("NewName");
+ System.out.println("Calling interrupt ...");
+ t.interrupt();
+ System.out.println("Calling stop ...");
+ t.stop();
+
+ // Now the ThreadMXBean functions
+
+ if (mbean.isThreadCpuTimeSupported() &&
+ mbean.isThreadCpuTimeEnabled() ) {
+ System.out.println("Calling getThreadCpuTime ...");
+ long t1 = mbean.getThreadCpuTime(t.getId());
+ if (t1 != -1) {
+ throw new RuntimeException("Invalid ThreadCpuTime returned = " +
+ t1 + " expected = -1");
+ }
+ System.out.println("Okay: getThreadCpuTime() reported -1 as expected");
+ } else {
+ System.out.println("Skipping Thread CPU time test as it's not supported");
+ }
+
+ System.out.println("Calling getThreadUserTime ...");
+ long t1 = mbean.getThreadUserTime(t.getId());
+ if (t1 != -1) {
+ throw new RuntimeException("Invalid ThreadUserTime returned = " +
+ t1 + " expected = -1");
+ }
+ System.out.println("Okay: getThreadUserTime() reported -1 as expected");
+
+ System.out.println("Calling getThreadInfo ...");
+ ThreadInfo info = mbean.getThreadInfo(t.getId());
+ System.out.println(info);
+
+ System.out.println("Calling getThreadInfo with stack ...");
+ info = mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE);
+ System.out.println(info);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/jni/terminatedThread/libterminatedThread.c Mon Jul 09 20:17:32 2018 -0400
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2018, 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pthread.h>
+#include <string.h>
+
+#include "jni.h"
+#include "jni_util.h"
+
+
+JavaVM* jvm;
+jobject nativeThread;
+
+static void * thread_start(void* unused) {
+ JNIEnv *env;
+ jclass class_id;
+ jmethodID method_id;
+ int res;
+
+ printf("Native thread is running and attaching as daemon ...\n");
+
+ res = (*jvm)->AttachCurrentThreadAsDaemon(jvm, (void **)&env, NULL);
+ if (res != JNI_OK) {
+ fprintf(stderr, "Test ERROR. Can't attach current thread: %d\n", res);
+ exit(1);
+ }
+
+ class_id = (*env)->FindClass (env, "java/lang/Thread");
+ if (class_id == NULL) {
+ fprintf(stderr, "Test ERROR. Can't load class Thread\n");
+ exit(1);
+ }
+
+ method_id = (*env)->GetStaticMethodID(env, class_id, "currentThread",
+ "()Ljava/lang/Thread;");
+ if (method_id == NULL) {
+ fprintf(stderr, "Test ERROR. Can't find method currentThread\n");
+ exit(1);
+ }
+
+ nativeThread = (*env)->CallStaticObjectMethod(env, class_id, method_id, NULL);
+
+ if ((*env)->ExceptionOccurred(env) != NULL) {
+ (*env)->ExceptionDescribe(env);
+ exit(1);
+ }
+ printf("Native thread terminating\n");
+
+ return NULL;
+}
+
+JNIEXPORT jobject JNICALL
+Java_TestTerminatedThread_createTerminatedThread
+(JNIEnv *env, jclass cls) {
+ pthread_t thread;
+ int res = (*env)->GetJavaVM(env, &jvm);
+ if (res != JNI_OK) {
+ fprintf(stderr, "Test ERROR. Can't extract JavaVM: %d\n", res);
+ exit(1);
+ }
+
+ if ((res = pthread_create(&thread, NULL, thread_start, NULL)) != 0) {
+ fprintf(stderr, "TEST ERROR: pthread_create failed: %s (%d)\n", strerror(res), res);
+ exit(1);
+ }
+
+ if ((res = pthread_join(thread, NULL)) != 0) {
+ fprintf(stderr, "TEST ERROR: pthread_join failed: %s (%d)\n", strerror(res), res);
+ exit(1);
+ }
+
+ return nativeThread;
+}
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI05/ji05t001/ji05t001.c Mon Jul 09 14:21:33 2018 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI05/ji05t001/ji05t001.c Mon Jul 09 20:17:32 2018 -0400
@@ -333,8 +333,11 @@
checkIntercept(1, 0, 1); /* expected interceptions: 1 */
NSK_DISPLAY0("\n<<< TEST CASE #4) done\n");
- NSK_DISPLAY1("\nagent A: returning exit code %d\n",
+ NSK_DISPLAY1("\nagent A: detaching and returning exit code %d\n",
exitCode);
+ if ((res = JNI_ENV_PTR(vm)->DetachCurrentThread(JNI_ENV_ARG1(vm))) != 0) {
+ NSK_COMPLAIN1("TEST WARNING: agent A: DetachCurrentThread() returns: %d\n", res);
+ }
return exitCode;
}
@@ -393,8 +396,11 @@
redir[1] = 1;
- NSK_DISPLAY1("\nagent B: returning exit code %d\n",
+ NSK_DISPLAY1("\nagent B: detaching and returning exit code %d\n",
exitCode);
+ if ((res = JNI_ENV_PTR(vm)->DetachCurrentThread(JNI_ENV_ARG1(vm))) != 0) {
+ NSK_COMPLAIN1("TEST WARNING: agent B: DetachCurrentThread() returns: %d\n", res);
+ }
return exitCode;
}
/*********************/
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI06/ji06t001/ji06t001.c Mon Jul 09 14:21:33 2018 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI06/ji06t001/ji06t001.c Mon Jul 09 20:17:32 2018 -0400
@@ -62,6 +62,18 @@
#define TRIES 30
#define MAX_THREADS 5
+// Helper for thread detach and terminate
+#define THREAD_return(status) \
+ do { \
+ int res = JNI_ENV_PTR(vm)->DetachCurrentThread(JNI_ENV_ARG1(vm)); \
+ if (res != 0) \
+ NSK_COMPLAIN1("TEST WARNING: DetachCurrentThread() returns: %d\n", res); \
+ else \
+ NSK_DISPLAY0("Detaching thread ...\n"); \
+ return status; \
+ } while (0)
+
+
static const char *javaField = "_ji06t001a";
static const char *classSig =
"Lnsk/jvmti/scenarios/jni_interception/JI06/ji06t001a;";
@@ -225,16 +237,16 @@
thrStarted[indx-1] = 1; /* the thread is started */
if (enterMonitor(env, "waitingThread") == STATUS_FAILED)
- return STATUS_FAILED;
+ THREAD_return(STATUS_FAILED);
if (verbose)
printf("waitingThread: thread #%d entered the monitor\n",
indx);
if (exitMonitor(env, "waitingThread") == STATUS_FAILED)
- return STATUS_FAILED;
+ THREAD_return(STATUS_FAILED);
NSK_DISPLAY2("waitingThread: thread #%d exits the monitor\n\treturning %d\n",
indx, exitCode);
- return exitCode;
+ THREAD_return(exitCode);
}
static int ownerThread(void *context) {
@@ -254,7 +266,7 @@
NSK_DISPLAY0("ownerThread: trying to enter the monitor ...\n");
if (enterMonitor(env, "ownerThread") == STATUS_FAILED)
- return STATUS_FAILED;
+ THREAD_return(STATUS_FAILED);
monEntered = 1; /* the monitor has been entered */
NSK_DISPLAY1("ownerThread: entered the monitor: monEntered=%d\n\
@@ -272,12 +284,12 @@
} while(releaseMon != 1);
if (exitMonitor(env, "ownerThread") == STATUS_FAILED)
- return STATUS_FAILED;
+ THREAD_return(STATUS_FAILED);
NSK_DISPLAY1("ownerThread: exits the monitor\n\treturning %d\n",
exitCode);
- return exitCode;
+ THREAD_return(exitCode);
}
static int redirectorThread(void *context) {
@@ -301,7 +313,7 @@
NSK_DISPLAY1("redirectorThread: the MonitorEnter() redirected\n\treturning %d\n",
exitCode);
- return exitCode;
+ THREAD_return(exitCode);
}
/*********************/