Skip to content

Commit 7475bd5

Browse files
committed
[Msan] Add ptsname, ptsname_r interceptors
Reviewed By: eugenis, MaskRay Differential Revision: https://reviews.llvm.org/D88547
1 parent c694588 commit 7475bd5

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4867,6 +4867,34 @@ INTERCEPTOR(char *, tmpnam_r, char *s) {
48674867
#define INIT_TMPNAM_R
48684868
#endif
48694869

4870+
#if SANITIZER_INTERCEPT_PTSNAME
4871+
INTERCEPTOR(char *, ptsname, int fd) {
4872+
void *ctx;
4873+
COMMON_INTERCEPTOR_ENTER(ctx, ptsname, fd);
4874+
char *res = REAL(ptsname)(fd);
4875+
if (res != nullptr)
4876+
COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
4877+
return res;
4878+
}
4879+
#define INIT_PTSNAME COMMON_INTERCEPT_FUNCTION(ptsname);
4880+
#else
4881+
#define INIT_PTSNAME
4882+
#endif
4883+
4884+
#if SANITIZER_INTERCEPT_PTSNAME_R
4885+
INTERCEPTOR(int, ptsname_r, int fd, char *name, SIZE_T namesize) {
4886+
void *ctx;
4887+
COMMON_INTERCEPTOR_ENTER(ctx, ptsname_r, fd, name, namesize);
4888+
int res = REAL(ptsname_r)(fd, name, namesize);
4889+
if (res == 0)
4890+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, REAL(strlen)(name) + 1);
4891+
return res;
4892+
}
4893+
#define INIT_PTSNAME_R COMMON_INTERCEPT_FUNCTION(ptsname_r);
4894+
#else
4895+
#define INIT_PTSNAME_R
4896+
#endif
4897+
48704898
#if SANITIZER_INTERCEPT_TTYNAME
48714899
INTERCEPTOR(char *, ttyname, int fd) {
48724900
void *ctx;
@@ -10166,6 +10194,8 @@ static void InitializeCommonInterceptors() {
1016610194
INIT_PTHREAD_BARRIERATTR_GETPSHARED;
1016710195
INIT_TMPNAM;
1016810196
INIT_TMPNAM_R;
10197+
INIT_PTSNAME;
10198+
INIT_PTSNAME_R;
1016910199
INIT_TTYNAME;
1017010200
INIT_TTYNAME_R;
1017110201
INIT_TEMPNAM;

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@
384384
#define SANITIZER_INTERCEPT_THR_EXIT SI_FREEBSD
385385
#define SANITIZER_INTERCEPT_TMPNAM SI_POSIX
386386
#define SANITIZER_INTERCEPT_TMPNAM_R SI_LINUX_NOT_ANDROID || SI_SOLARIS
387+
#define SANITIZER_INTERCEPT_PTSNAME SI_LINUX
388+
#define SANITIZER_INTERCEPT_PTSNAME_R SI_LINUX
387389
#define SANITIZER_INTERCEPT_TTYNAME SI_POSIX
388390
#define SANITIZER_INTERCEPT_TTYNAME_R SI_POSIX
389391
#define SANITIZER_INTERCEPT_TEMPNAM SI_POSIX
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang %s -o %t && %run %t
2+
3+
#define _GNU_SOURCE
4+
#define _XOPEN_SOURCE 600
5+
6+
#include <assert.h>
7+
#include <fcntl.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
#include <unistd.h>
11+
12+
int main() {
13+
int pt = posix_openpt(O_NOCTTY);
14+
if (pt == -1)
15+
return 0;
16+
char *s = ptsname(pt);
17+
assert(s);
18+
assert(strstr(s, "/dev"));
19+
20+
char buff[1000] = {};
21+
int r = ptsname_r(pt, buff, sizeof(buff));
22+
assert(!r);
23+
assert(strstr(buff, "/dev"));
24+
25+
close(pt);
26+
return 0;
27+
}

0 commit comments

Comments
 (0)