Files
clang-p2996/compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_join.cpp
goussepi 5e406615fe [sanitizer] Fix asserts in asan and tsan in pthread interceptors. (#75394)
Calling one of pthread join/detach interceptor on an already
joined/detached thread causes asserts such as:

AddressSanitizer: CHECK failed: sanitizer_thread_arg_retval.cpp:56
"((t)) != (0)" (0x0, 0x0) (tid=1236094)
#0 0x555555634f8b in __asan::CheckUnwind()
compiler-rt/lib/asan/asan_rtl.cpp:69:3
#1 0x55555564e06e in __sanitizer::CheckFailed(char const*, int, char
const*, unsigned long long, unsigned long long)
compiler-rt/lib/sanitizer_common/sanitizer_termination.cpp:86:24
#2 0x5555556491df in __sanitizer::ThreadArgRetval::BeforeJoin(unsigned
long) const
compiler-rt/lib/sanitizer_common/sanitizer_thread_arg_retval.cpp:56:3
#3 0x5555556198ed in Join<___interceptor_pthread_tryjoin_np(void*,
void**)::<lambda()> >
compiler-rt/lib/asan/../sanitizer_common/sanitizer_thread_arg_retval.h:74:26
#4 0x5555556198ed in pthread_tryjoin_np
compiler-rt/lib/asan/asan_interceptors.cpp:311:29

The assert are replaced by error codes.
2024-01-11 12:23:33 +00:00

70 lines
1.8 KiB
C++

// RUN: %clangxx -pthread %s -o %t
// RUN: %env_tool_opts=detect_invalid_join=false %run %t 0
// FIXME: Crashes on some bots in pthread_exit.
// RUN: %env_tool_opts=detect_invalid_join=false %run %t %if tsan %{ 0 %} %else %{ 1 %}
// REQUIRES: glibc
#include <assert.h>
#include <ctime>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
bool use_exit;
static void *fn(void *args) {
sleep(1);
auto ret = (void *)(~(uintptr_t)args);
if (use_exit)
pthread_exit(ret);
return ret;
}
int main(int argc, char **argv) {
use_exit = atoi(argv[1]);
bool check_invalid_join = !use_exit;
pthread_t thread[5];
assert(!pthread_create(&thread[0], nullptr, fn, (void *)1000));
assert(!pthread_create(&thread[1], nullptr, fn, (void *)1001));
assert(!pthread_create(&thread[2], nullptr, fn, (void *)1002));
assert(!pthread_create(&thread[3], nullptr, fn, (void *)1003));
pthread_attr_t attr;
assert(!pthread_attr_init(&attr));
assert(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
assert(!pthread_create(&thread[4], &attr, fn, (void *)1004));
assert(!pthread_attr_destroy(&attr));
assert(!pthread_detach(thread[0]));
{
void *res;
while (pthread_tryjoin_np(thread[1], &res))
sleep(1);
assert(~(uintptr_t)res == 1001);
assert(check_invalid_join ||
(pthread_tryjoin_np(thread[1], &res) == EBUSY));
}
{
void *res;
timespec tm = {0, 1};
while (pthread_timedjoin_np(thread[2], &res, &tm))
sleep(1);
assert(~(uintptr_t)res == 1002);
assert(check_invalid_join ||
(pthread_timedjoin_np(thread[2], &res, &tm) == ESRCH));
}
{
void *res;
assert(!pthread_join(thread[3], &res));
assert(~(uintptr_t)res == 1003);
assert(check_invalid_join || (pthread_join(thread[3], &res) == ESRCH));
}
return 0;
}