[sanitizer] Intercept scandir/scandir64.

llvm-svn: 187982
This commit is contained in:
Evgeniy Stepanov 2013-08-08 13:57:15 +00:00
parent e2c05bbefe
commit 564215d949
9 changed files with 206 additions and 1 deletions

View File

@ -0,0 +1,56 @@
// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t %p
// RUN: %clangxx_msan -m64 -O0 -D_FILE_OFFSET_BITS=64 %s -o %t && %t %p
// RUN: %clangxx_msan -m64 -O3 %s -o %t && %t %p
#include <assert.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <sanitizer/msan_interface.h>
static int my_filter(const struct dirent *a) {
assert(__msan_test_shadow(&a, sizeof(a)) == (size_t)-1);
printf("%s\n", a->d_name);
__msan_print_shadow(a, a->d_reclen);
assert(__msan_test_shadow(a, a->d_reclen) == (size_t)-1);
printf("%s\n", a->d_name);
return strlen(a->d_name) == 3 && a->d_name[2] == 'b';
}
static int my_compar(const struct dirent **a, const struct dirent **b) {
assert(__msan_test_shadow(a, sizeof(*a)) == (size_t)-1);
assert(__msan_test_shadow(*a, (*a)->d_reclen) == (size_t)-1);
assert(__msan_test_shadow(b, sizeof(*b)) == (size_t)-1);
assert(__msan_test_shadow(*b, (*b)->d_reclen) == (size_t)-1);
if ((*a)->d_name[1] == (*b)->d_name[1])
return 0;
return ((*a)->d_name[1] < (*b)->d_name[1]) ? 1 : -1;
}
int main(int argc, char *argv[]) {
assert(argc == 2);
char buf[1024];
snprintf(buf, sizeof(buf), "%s/%s", argv[1], "scandir_test_root/");
struct dirent **d;
int res = scandir(buf, &d, my_filter, my_compar);
assert(res == 2);
assert(__msan_test_shadow(&d, sizeof(*d)) == (size_t)-1);
for (int i = 0; i < res; ++i) {
assert(__msan_test_shadow(&d[i], sizeof(d[i])) == (size_t)-1);
assert(__msan_test_shadow(d[i], d[i]->d_reclen) == (size_t)-1);
}
assert(strcmp(d[0]->d_name, "bbb") == 0);
assert(strcmp(d[1]->d_name, "aab") == 0);
return 0;
}

View File

@ -0,0 +1,34 @@
// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t %p
// RUN: %clangxx_msan -m64 -O0 -D_FILE_OFFSET_BITS=64 %s -o %t && %t %p
// RUN: %clangxx_msan -m64 -O3 %s -o %t && %t %p
#include <assert.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <sanitizer/msan_interface.h>
int main(int argc, char *argv[]) {
assert(argc == 2);
char buf[1024];
snprintf(buf, sizeof(buf), "%s/%s", argv[1], "scandir_test_root/");
struct dirent **d;
int res = scandir(buf, &d, NULL, NULL);
assert(res >= 3);
assert(__msan_test_shadow(&d, sizeof(*d)) == (size_t)-1);
for (int i = 0; i < res; ++i) {
assert(__msan_test_shadow(&d[i], sizeof(d[i])) == (size_t)-1);
assert(__msan_test_shadow(d[i], d[i]->d_reclen) == (size_t)-1);
}
return 0;
}

View File

@ -1816,6 +1816,113 @@ INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
#define INIT_STRERROR_R
#endif
#if SANITIZER_INTERCEPT_SCANDIR
typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
const struct __sanitizer_dirent **);
static THREADLOCAL void *scandir_ctx;
static THREADLOCAL scandir_filter_f scandir_filter;
static THREADLOCAL scandir_compar_f scandir_compar;
static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir_ctx, 1);
COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, dir, dir->d_reclen);
return scandir_filter(dir);
}
static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
const struct __sanitizer_dirent **b) {
COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir_ctx, 2);
COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, a, sizeof(*a));
COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, *a, (*a)->d_reclen);
COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, b, sizeof(*b));
COMMON_INTERCEPTOR_WRITE_RANGE(scandir_ctx, *b, (*b)->d_reclen);
return scandir_compar(a, b);
}
INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
scandir_filter_f filter, scandir_compar_f compar) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
CHECK(scandir_ctx == 0);
scandir_ctx = ctx;
scandir_filter = filter;
scandir_compar = compar;
int res = REAL(scandir)(dirp, namelist, filter ? wrapped_scandir_filter : 0,
compar ? wrapped_scandir_compar : 0);
scandir_ctx = 0;
scandir_filter = 0;
scandir_compar = 0;
if (namelist && res > 0) {
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
for (int i = 0; i < res; ++i)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
(*namelist)[i]->d_reclen);
}
return res;
}
#define INIT_SCANDIR INTERCEPT_FUNCTION(scandir);
#else
#define INIT_SCANDIR
#endif
#if SANITIZER_INTERCEPT_SCANDIR64
typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
const struct __sanitizer_dirent64 **);
static THREADLOCAL void *scandir64_ctx;
static THREADLOCAL scandir64_filter_f scandir64_filter;
static THREADLOCAL scandir64_compar_f scandir64_compar;
static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir64_ctx, 1);
COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, dir, dir->d_reclen);
return scandir64_filter(dir);
}
static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
const struct __sanitizer_dirent64 **b) {
COMMON_INTERCEPTOR_UNPOISON_PARAM(scandir64_ctx, 2);
COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, a, sizeof(*a));
COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, *a, (*a)->d_reclen);
COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, b, sizeof(*b));
COMMON_INTERCEPTOR_WRITE_RANGE(scandir64_ctx, *b, (*b)->d_reclen);
return scandir64_compar(a, b);
}
INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
scandir64_filter_f filter, scandir64_compar_f compar) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
CHECK(scandir64_ctx == 0);
scandir64_ctx = ctx;
scandir64_filter = filter;
scandir64_compar = compar;
int res =
REAL(scandir64)(dirp, namelist, filter ? wrapped_scandir64_filter : 0,
compar ? wrapped_scandir64_compar : 0);
scandir64_ctx = 0;
scandir64_filter = 0;
scandir64_compar = 0;
if (namelist && res > 0) {
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
for (int i = 0; i < res; ++i)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
(*namelist)[i]->d_reclen);
}
return res;
}
#define INIT_SCANDIR64 INTERCEPT_FUNCTION(scandir64);
#else
#define INIT_SCANDIR64
#endif
#define SANITIZER_COMMON_INTERCEPTORS_INIT \
INIT_STRCMP; \
INIT_STRNCMP; \
@ -1878,4 +1985,6 @@ INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
INIT_CONFSTR; \
INIT_SCHED_GETAFFINITY; \
INIT_STRERROR; \
INIT_STRERROR_R;
INIT_STRERROR_R; \
INIT_SCANDIR; \
INIT_SCANDIR64;

View File

@ -113,5 +113,7 @@
# define SANITIZER_INTERCEPT_SCHED_GETAFFINITY SI_LINUX_NOT_ANDROID
# define SANITIZER_INTERCEPT_STRERROR SI_NOT_WINDOWS
# define SANITIZER_INTERCEPT_STRERROR_R SI_NOT_WINDOWS
# define SANITIZER_INTERCEPT_SCANDIR SI_NOT_WINDOWS
# define SANITIZER_INTERCEPT_SCANDIR64 SI_LINUX_NOT_ANDROID
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

View File

@ -356,6 +356,8 @@ void StatOutput(u64 *stat) {
name[StatInt_sched_getaffinity] = " sched_getaffinity ";
name[StatInt_strerror] = " strerror ";
name[StatInt_strerror_r] = " strerror_r ";
name[StatInt_scandir] = " scandir ";
name[StatInt_scandir64] = " scandir64 ";
name[StatAnnotation] = "Dynamic annotations ";
name[StatAnnotateHappensBefore] = " HappensBefore ";

View File

@ -351,6 +351,8 @@ enum StatType {
StatInt_sched_getaffinity,
StatInt_strerror,
StatInt_strerror_r,
StatInt_scandir,
StatInt_scandir64,
// Dynamic annotations.
StatAnnotation,