[Msan] Intercept stat() and fstatat() on FreeBSD

Differential Revision: http://reviews.llvm.org/D7051

llvm-svn: 226461
This commit is contained in:
Viktor Kutuzov 2015-01-19 13:22:33 +00:00
parent 62975f55c8
commit 68f150f3d4
2 changed files with 29 additions and 12 deletions

View File

@ -703,7 +703,15 @@ INTERCEPTOR(int, __fxstat64, int magic, int fd, void *buf) {
#define MSAN_MAYBE_INTERCEPT___FXSTAT64 #define MSAN_MAYBE_INTERCEPT___FXSTAT64
#endif #endif
#if !SANITIZER_FREEBSD #if SANITIZER_FREEBSD
INTERCEPTOR(int, fstatat, int fd, char *pathname, void *buf, int flags) {
ENSURE_MSAN_INITED();
int res = REAL(fstatat)(fd, pathname, buf, flags);
if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
return res;
}
# define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(fstatat)
#else
INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf, INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
int flags) { int flags) {
ENSURE_MSAN_INITED(); ENSURE_MSAN_INITED();
@ -711,9 +719,7 @@ INTERCEPTOR(int, __fxstatat, int magic, int fd, char *pathname, void *buf,
if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz); if (!res) __msan_unpoison(buf, __sanitizer::struct_stat_sz);
return res; return res;
} }
#define MSAN_MAYBE_INTERCEPT___FXSTATAT INTERCEPT_FUNCTION(__fxstatat) # define MSAN_INTERCEPT_FSTATAT INTERCEPT_FUNCTION(__fxstatat)
#else
#define MSAN_MAYBE_INTERCEPT___FXSTATAT
#endif #endif
#if !SANITIZER_FREEBSD #if !SANITIZER_FREEBSD
@ -729,7 +735,16 @@ INTERCEPTOR(int, __fxstatat64, int magic, int fd, char *pathname, void *buf,
#define MSAN_MAYBE_INTERCEPT___FXSTATAT64 #define MSAN_MAYBE_INTERCEPT___FXSTATAT64
#endif #endif
#if !SANITIZER_FREEBSD #if SANITIZER_FREEBSD
INTERCEPTOR(int, stat, char *path, void *buf) {
ENSURE_MSAN_INITED();
int res = REAL(stat)(path, buf);
if (!res)
__msan_unpoison(buf, __sanitizer::struct_stat_sz);
return res;
}
# define MSAN_INTERCEPT_STAT INTERCEPT_FUNCTION(stat)
#else
INTERCEPTOR(int, __xstat, int magic, char *path, void *buf) { INTERCEPTOR(int, __xstat, int magic, char *path, void *buf) {
ENSURE_MSAN_INITED(); ENSURE_MSAN_INITED();
int res = REAL(__xstat)(magic, path, buf); int res = REAL(__xstat)(magic, path, buf);
@ -737,9 +752,7 @@ INTERCEPTOR(int, __xstat, int magic, char *path, void *buf) {
__msan_unpoison(buf, __sanitizer::struct_stat_sz); __msan_unpoison(buf, __sanitizer::struct_stat_sz);
return res; return res;
} }
#define MSAN_MAYBE_INTERCEPT___XSTAT INTERCEPT_FUNCTION(__xstat) # define MSAN_INTERCEPT_STAT INTERCEPT_FUNCTION(__xstat)
#else
#define MSAN_MAYBE_INTERCEPT___XSTAT
#endif #endif
#if !SANITIZER_FREEBSD #if !SANITIZER_FREEBSD
@ -1634,8 +1647,8 @@ void InitializeInterceptors() {
INTERCEPT_FUNCTION(gettimeofday); INTERCEPT_FUNCTION(gettimeofday);
INTERCEPT_FUNCTION(fcvt); INTERCEPT_FUNCTION(fcvt);
MSAN_MAYBE_INTERCEPT___FXSTAT; MSAN_MAYBE_INTERCEPT___FXSTAT;
MSAN_MAYBE_INTERCEPT___FXSTATAT; MSAN_INTERCEPT_FSTATAT;
MSAN_MAYBE_INTERCEPT___XSTAT; MSAN_INTERCEPT_STAT;
MSAN_MAYBE_INTERCEPT___LXSTAT; MSAN_MAYBE_INTERCEPT___LXSTAT;
MSAN_MAYBE_INTERCEPT___FXSTAT64; MSAN_MAYBE_INTERCEPT___FXSTAT64;
MSAN_MAYBE_INTERCEPT___FXSTATAT64; MSAN_MAYBE_INTERCEPT___FXSTATAT64;

View File

@ -76,8 +76,12 @@
// On FreeBSD procfs is not enabled by default. // On FreeBSD procfs is not enabled by default.
#if defined(__FreeBSD__) #if defined(__FreeBSD__)
# define FILE_TO_READ "/bin/cat" # define FILE_TO_READ "/bin/cat"
# define DIR_TO_READ "/bin"
# define SUBFILE_TO_READ "cat"
#else #else
# define FILE_TO_READ "/proc/self/stat" # define FILE_TO_READ "/proc/self/stat"
# define DIR_TO_READ "/proc/self"
# define SUBFILE_TO_READ "stat"
#endif #endif
static const size_t kPageSize = 4096; static const size_t kPageSize = 4096;
@ -680,9 +684,9 @@ TEST(MemorySanitizer, stat) {
TEST(MemorySanitizer, fstatat) { TEST(MemorySanitizer, fstatat) {
struct stat* st = new struct stat; struct stat* st = new struct stat;
int dirfd = open("/proc/self", O_RDONLY); int dirfd = open(DIR_TO_READ, O_RDONLY);
ASSERT_GT(dirfd, 0); ASSERT_GT(dirfd, 0);
int res = fstatat(dirfd, "stat", st, 0); int res = fstatat(dirfd, SUBFILE_TO_READ, st, 0);
ASSERT_EQ(0, res); ASSERT_EQ(0, res);
EXPECT_NOT_POISONED(st->st_dev); EXPECT_NOT_POISONED(st->st_dev);
EXPECT_NOT_POISONED(st->st_mode); EXPECT_NOT_POISONED(st->st_mode);