Skip to content

Commit 828c94c

Browse files
committed
[MSAN] Add interceptor for pthread_getaffinity_np.
Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D127185
1 parent 69cd741 commit 828c94c

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4963,6 +4963,27 @@ INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
49634963
#define INIT_PTHREAD_ATTR_GETAFFINITY_NP
49644964
#endif
49654965

4966+
#if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
4967+
INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
4968+
void *cpuset) {
4969+
void *ctx;
4970+
COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
4971+
cpuset);
4972+
// FIXME: under ASan the call below may write to freed memory and corrupt
4973+
// its metadata. See
4974+
// https://github.com/google/sanitizers/issues/321.
4975+
int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
4976+
if (!res && cpusetsize && cpuset)
4977+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
4978+
return res;
4979+
}
4980+
4981+
#define INIT_PTHREAD_GETAFFINITY_NP \
4982+
COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
4983+
#else
4984+
#define INIT_PTHREAD_GETAFFINITY_NP
4985+
#endif
4986+
49664987
#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
49674988
INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
49684989
#define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
@@ -10535,6 +10556,7 @@ static void InitializeCommonInterceptors() {
1053510556
INIT_PTHREAD_ATTR_GET_SCHED;
1053610557
INIT_PTHREAD_ATTR_GETINHERITSCHED;
1053710558
INIT_PTHREAD_ATTR_GETAFFINITY_NP;
10559+
INIT_PTHREAD_GETAFFINITY_NP;
1053810560
INIT_PTHREAD_MUTEXATTR_GETPSHARED;
1053910561
INIT_PTHREAD_MUTEXATTR_GETTYPE;
1054010562
INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@
348348
#define SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED \
349349
(SI_FREEBSD || SI_NETBSD || SI_MAC || SI_LINUX_NOT_ANDROID || SI_SOLARIS)
350350
#define SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP SI_GLIBC
351+
#define SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP SI_LINUX
351352
#define SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED SI_POSIX
352353
#define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED \
353354
(SI_POSIX && !SI_NETBSD)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clangxx_msan -O0 %s -o %t && %run %t
2+
3+
#include <assert.h>
4+
#include <pthread.h>
5+
6+
#include <sanitizer/msan_interface.h>
7+
8+
int main() {
9+
cpu_set_t set_x;
10+
int res = pthread_getaffinity_np(pthread_self(), sizeof(set_x), &set_x);
11+
assert(res == 0);
12+
__msan_check_mem_is_initialized(&set_x, sizeof(set_x));
13+
14+
return 0;
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clangxx -O0 %s -o %t && %run %t
2+
3+
#include <assert.h>
4+
#include <pthread.h>
5+
#include <sys/sysinfo.h>
6+
7+
#include <sanitizer/msan_interface.h>
8+
9+
int main() {
10+
cpu_set_t set_x;
11+
int res = pthread_getaffinity_np(pthread_self(), sizeof(set_x), &set_x);
12+
assert(res == 0);
13+
assert(CPU_COUNT_S(sizeof(set_x), &set_x) == get_nprocs());
14+
15+
return 0;
16+
}

0 commit comments

Comments
 (0)