Skip to content

Commit 3db1d81

Browse files
ccotteryuxuanchen1997
authored andcommitted
[msan] Support prctl PR_GET_NAME call (#98951)
Summary: Per the man page, PR_GET_NAME stores a null terminated string into the input `char name[16]`. This also adds prctl support in ASAN to detect freed memory being passed to `prctl(PR_GET_NAME, ...)`: Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251534
1 parent cc440b1 commit 3db1d81

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
12511251
void *ctx;
12521252
COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
12531253
static const int PR_SET_NAME = 15;
1254+
static const int PR_GET_NAME = 16;
12541255
static const int PR_SET_VMA = 0x53564d41;
12551256
static const int PR_SCHED_CORE = 62;
12561257
static const int PR_SCHED_CORE_GET = 0;
@@ -1264,7 +1265,11 @@ INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
12641265
internal_strncpy(buff, (char *)arg2, 15);
12651266
buff[15] = 0;
12661267
COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
1267-
} else if (res != -1 && option == PR_SCHED_CORE && arg2 == PR_SCHED_CORE_GET) {
1268+
} else if (res == 0 && option == PR_GET_NAME) {
1269+
char *name = (char *)arg2;
1270+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
1271+
} else if (res != -1 && option == PR_SCHED_CORE &&
1272+
arg2 == PR_SCHED_CORE_GET) {
12681273
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64*)(arg5), sizeof(u64));
12691274
}
12701275
return res;

compiler-rt/test/msan/Linux/prctl.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clangxx_msan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2+
3+
#include <linux/prctl.h>
4+
#include <sys/prctl.h>
5+
6+
int main(void) {
7+
prctl(PR_SET_NAME, "tname");
8+
char name[16];
9+
prctl(PR_GET_NAME, name);
10+
11+
if (name[0] == 'A') {
12+
return 0;
13+
}
14+
if (name[5] != '\0') {
15+
return 0;
16+
}
17+
if (name[6] != '\0') {
18+
return 0;
19+
}
20+
// CHECK: SUMMARY: MemorySanitizer: use-of-uninitialized-value {{.*prctl.cpp}}:[[@LINE-3]]
21+
22+
return 0;
23+
}

compiler-rt/test/sanitizer_common/TestCases/Linux/prctl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <assert.h>
44
#include <errno.h>
55
#include <stdint.h>
6+
#include <string.h>
67
#include <sys/mman.h>
78
#include <sys/prctl.h>
89

@@ -60,5 +61,14 @@ int main() {
6061
}
6162
munmap(p, 128);
6263

64+
res = prctl(PR_SET_NAME, "tname");
65+
if (res == 0) {
66+
char name[16];
67+
res = prctl(PR_GET_NAME, name);
68+
if (res == 0) {
69+
assert(!strcmp(name, "tname"));
70+
}
71+
}
72+
6373
return 0;
6474
}

0 commit comments

Comments
 (0)