From 175d2b8620a8a6ea409f04b2a1188b2c29ae0bd8 Mon Sep 17 00:00:00 2001 From: Kamil Rytarowski Date: Tue, 4 Dec 2018 01:45:52 +0000 Subject: [PATCH] Add interceptors for the fts(3) API family from NetBSD Summary: fts(3) is API to traverse a file hierarchy. Cover this interface with interceptors. Add a test to validate the interface reading the number of regular files in /etc. Based on original work by Yang Zheng. Reviewers: joerg, vitalybuka Reviewed By: vitalybuka Subscribers: tomsun.0.7, kubamracek, llvm-commits, mgorny, #sanitizers Tags: #sanitizers Differential Revision: https://reviews.llvm.org/D54247 llvm-svn: 348227 --- .../sanitizer_common_interceptors.inc | 75 ++++++++++++++++++++++ .../sanitizer_platform_interceptors.h | 1 + .../sanitizer_platform_limits_netbsd.cc | 2 + .../sanitizer_platform_limits_netbsd.h | 2 + .../test/sanitizer_common/TestCases/NetBSD/fts.cc | 40 ++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 compiler-rt/test/sanitizer_common/TestCases/NetBSD/fts.cc diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index ab025c4..d1857cf 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -77,6 +77,11 @@ #define ctime __ctime50 #define ctime_r __ctime_r50 #define devname __devname50 +#define fts_children __fts_children60 +#define fts_close __fts_close60 +#define fts_open __fts_open60 +#define fts_read __fts_read60 +#define fts_set __fts_set60 #define getitimer __getitimer50 #define getmntinfo __getmntinfo13 #define getpwent __getpwent50 @@ -7449,6 +7454,75 @@ INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub, #define INIT_REGEX #endif +#if SANITIZER_INTERCEPT_FTS +INTERCEPTOR(void *, fts_open, char *const *path_argv, int options, + int (*compar)(void **, void **)) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar); + if (path_argv) { + for (char *const *pa = path_argv; ; ++pa) { + COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **)); + if (!*pa) + break; + COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, REAL(strlen)(*pa) + 1); + } + } + // TODO(kamil): handle compar callback + void *fts = REAL(fts_open)(path_argv, options, compar); + if (fts) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz); + return fts; +} + +INTERCEPTOR(void *, fts_read, void *ftsp) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp); + if (ftsp) + COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz); + void *ftsent = REAL(fts_read)(ftsp); + if (ftsent) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz); + return ftsent; +} + +INTERCEPTOR(void *, fts_children, void *ftsp, int options) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options); + if (ftsp) + COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz); + void *ftsent = REAL(fts_children)(ftsp, options); + if (ftsent) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz); + return ftsent; +} + +INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options); + if (ftsp) + COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz); + if (f) + COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz); + return REAL(fts_set)(ftsp, f, options); +} + +INTERCEPTOR(int, fts_close, void *ftsp) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp); + if (ftsp) + COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz); + return REAL(fts_close)(ftsp); +} +#define INIT_FTS \ + COMMON_INTERCEPT_FUNCTION(fts_open); \ + COMMON_INTERCEPT_FUNCTION(fts_read); \ + COMMON_INTERCEPT_FUNCTION(fts_children); \ + COMMON_INTERCEPT_FUNCTION(fts_set); \ + COMMON_INTERCEPT_FUNCTION(fts_close); +#else +#define INIT_FTS +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap(); @@ -7707,6 +7781,7 @@ static void InitializeCommonInterceptors() { INIT_SETVBUF; INIT_GETVFSSTAT; INIT_REGEX; + INIT_FTS; INIT___PRINTF_CHK; } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index 2ffeb7e..40d3c5f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -522,5 +522,6 @@ #define SANITIZER_INTERCEPT_MI_VECTOR_HASH SI_NETBSD #define SANITIZER_INTERCEPT_GETVFSSTAT SI_NETBSD #define SANITIZER_INTERCEPT_REGEX SI_NETBSD +#define SANITIZER_INTERCEPT_FTS SI_NETBSD #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc index b20dc5f..aca2ca0 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc @@ -253,6 +253,8 @@ unsigned struct_rlimit_sz = sizeof(struct rlimit); unsigned struct_timespec_sz = sizeof(struct timespec); unsigned struct_sembuf_sz = sizeof(struct sembuf); unsigned struct_kevent_sz = sizeof(struct kevent); +unsigned struct_FTS_sz = sizeof(FTS); +unsigned struct_FTSENT_sz = sizeof(FTSENT); unsigned struct_regex_sz = sizeof(regex_t); unsigned struct_regmatch_sz = sizeof(regmatch_t); unsigned struct_utimbuf_sz = sizeof(struct utimbuf); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h index f39c575..54d0b84 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h @@ -60,6 +60,8 @@ extern unsigned struct_timespec_sz; extern unsigned struct_sembuf_sz; extern unsigned struct_kevent_sz; +extern unsigned struct_FTS_sz; +extern unsigned struct_FTSENT_sz; extern unsigned struct_regex_sz; extern unsigned struct_regmatch_sz; diff --git a/compiler-rt/test/sanitizer_common/TestCases/NetBSD/fts.cc b/compiler-rt/test/sanitizer_common/TestCases/NetBSD/fts.cc new file mode 100644 index 0000000..1461005 --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/NetBSD/fts.cc @@ -0,0 +1,40 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include +#include + +#include + +#include +#include +#include +#include + +int main() { + char *const paths[] = {(char *)"/etc", 0}; + FTS *ftsp = fts_open(paths, FTS_LOGICAL, NULL); + assert(ftsp); + + FTSENT *chp = fts_children(ftsp, 0); + assert(chp); + + size_t n = 0; + for (FTSENT *p = fts_read(ftsp); p; p = fts_read(ftsp)) { + /* Skip recursively subdirectories */ + if (p->fts_info == FTS_D && p->fts_level != FTS_ROOTLEVEL) /* pre-order */ + fts_set(ftsp, p, FTS_SKIP); + else if (p->fts_info == FTS_DP) /* post-order */ + continue; + else if (p->fts_info == FTS_F) /* regular file */ + n++; + } + + int rv = fts_close(ftsp); + assert(!rv); + + printf("Number of files in /etc: '%zu'\n", n); + + return EXIT_SUCCESS; + + // CHECK: Number of files in /etc: '{{.*}}' +} -- 2.7.4