From ddf757e35eb36d684b16273f0ddfe2d387983e8e Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Wed, 17 Oct 2018 15:23:03 -0700 Subject: Properly fail with ESRCH when pthread_killing an exited thread. Previously, we were callign tgkill(pid, 0, signal) instead, which would fail with EINVAL instead. Test: bionic-unit-tests Change-Id: I25b127dcf347e0223274502b0516a950b6c2093e --- libc/bionic/pthread_kill.cpp | 4 +++- tests/pthread_test.cpp | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/libc/bionic/pthread_kill.cpp b/libc/bionic/pthread_kill.cpp index 3761a7589..153157496 100644 --- a/libc/bionic/pthread_kill.cpp +++ b/libc/bionic/pthread_kill.cpp @@ -36,7 +36,9 @@ int pthread_kill(pthread_t t, int sig) { ErrnoRestorer errno_restorer; pid_t tid = pthread_gettid_np(t); - if (tid == -1) return ESRCH; + + // tid gets reset to 0 on thread exit by CLONE_CHILD_CLEARTID. + if (tid == 0 || tid == -1) return ESRCH; return (tgkill(getpid(), tid, sig) == -1) ? errno : 0; } diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp index fc8945ce8..e68f1ff03 100644 --- a/tests/pthread_test.cpp +++ b/tests/pthread_test.cpp @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -539,6 +540,25 @@ TEST(pthread, pthread_kill__in_signal_handler) { ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); } +TEST(pthread, pthread_kill__exited_thread) { + static std::promise tid_promise; + pthread_t thread; + ASSERT_EQ(0, pthread_create(&thread, nullptr, + [](void*) -> void* { + tid_promise.set_value(gettid()); + return nullptr; + }, + nullptr)); + + pid_t tid = tid_promise.get_future().get(); + while (TEMP_FAILURE_RETRY(syscall(__NR_tgkill, getpid(), tid, 0)) != -1) { + continue; + } + ASSERT_EQ(ESRCH, errno); + + ASSERT_EQ(ESRCH, pthread_kill(thread, 0)); +} + TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) { pthread_t dead_thread; MakeDeadThread(dead_thread); -- cgit v1.2.3