Skip to content

Commit

Permalink
[MSAN] Add interceptor for pthread_getaffinity_np.
Browse files Browse the repository at this point in the history
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D127185
  • Loading branch information
kda committed Jun 7, 2022
1 parent 69cd741 commit 828c94c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
Expand Up @@ -4963,6 +4963,27 @@ INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
#define INIT_PTHREAD_ATTR_GETAFFINITY_NP
#endif

#if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
void *cpuset) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
cpuset);
// FIXME: under ASan the call below may write to freed memory and corrupt
// its metadata. See
// https://github.com/google/sanitizers/issues/321.
int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
if (!res && cpusetsize && cpuset)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
return res;
}

#define INIT_PTHREAD_GETAFFINITY_NP \
COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
#else
#define INIT_PTHREAD_GETAFFINITY_NP
#endif

#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
#define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
Expand Down Expand Up @@ -10535,6 +10556,7 @@ static void InitializeCommonInterceptors() {
INIT_PTHREAD_ATTR_GET_SCHED;
INIT_PTHREAD_ATTR_GETINHERITSCHED;
INIT_PTHREAD_ATTR_GETAFFINITY_NP;
INIT_PTHREAD_GETAFFINITY_NP;
INIT_PTHREAD_MUTEXATTR_GETPSHARED;
INIT_PTHREAD_MUTEXATTR_GETTYPE;
INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
Expand Down
Expand Up @@ -348,6 +348,7 @@
#define SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED \
(SI_FREEBSD || SI_NETBSD || SI_MAC || SI_LINUX_NOT_ANDROID || SI_SOLARIS)
#define SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP SI_GLIBC
#define SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP SI_LINUX
#define SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED SI_POSIX
#define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED \
(SI_POSIX && !SI_NETBSD)
Expand Down
15 changes: 15 additions & 0 deletions compiler-rt/test/msan/Linux/pthread_getaffinity_np.cpp
@@ -0,0 +1,15 @@
// RUN: %clangxx_msan -O0 %s -o %t && %run %t

#include <assert.h>
#include <pthread.h>

#include <sanitizer/msan_interface.h>

int main() {
cpu_set_t set_x;
int res = pthread_getaffinity_np(pthread_self(), sizeof(set_x), &set_x);
assert(res == 0);
__msan_check_mem_is_initialized(&set_x, sizeof(set_x));

return 0;
}
@@ -0,0 +1,16 @@
// RUN: %clangxx -O0 %s -o %t && %run %t

#include <assert.h>
#include <pthread.h>
#include <sys/sysinfo.h>

#include <sanitizer/msan_interface.h>

int main() {
cpu_set_t set_x;
int res = pthread_getaffinity_np(pthread_self(), sizeof(set_x), &set_x);
assert(res == 0);
assert(CPU_COUNT_S(sizeof(set_x), &set_x) == get_nprocs());

return 0;
}

0 comments on commit 828c94c

Please sign in to comment.