From 4a03fa4f602012d71430ff3a1d66c8e6202b5021 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 6 Feb 2014 20:39:33 +0000 Subject: [PATCH] [sanitizer] Intercept if_indextoname() and if_nametoindex(). llvm-svn: 200945 --- .../lib/msan/lit_tests/if_indextoname.cc | 23 ++++++++++++++++ .../sanitizer_common_interceptors.inc | 26 ++++++++++++++++++- .../sanitizer_platform_interceptors.h | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 compiler-rt/lib/msan/lit_tests/if_indextoname.cc diff --git a/compiler-rt/lib/msan/lit_tests/if_indextoname.cc b/compiler-rt/lib/msan/lit_tests/if_indextoname.cc new file mode 100644 index 000000000000..7a5ba359953a --- /dev/null +++ b/compiler-rt/lib/msan/lit_tests/if_indextoname.cc @@ -0,0 +1,23 @@ +// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t 2>&1 +// RUN: %clangxx_msan -m64 -O0 -D_FILE_OFFSET_BITS=64 %s -o %t && %t 2>&1 +// RUN: %clangxx_msan -m64 -O3 %s -o %t && %t 2>&1 + +#include +#include +#include +#include +#include + +#include + +int main(int argc, char *argv[]) { + char ifname[IF_NAMESIZE + 1]; + assert(0 == __msan_test_shadow(ifname, sizeof(ifname))); + if (!if_indextoname(1, ifname)) { + assert(errno == ENXIO); + printf("No network interfaces found.\n"); + return 0; + } + assert(strlen(ifname) + 1 == __msan_test_shadow(ifname, sizeof(ifname))); + return 0; +} diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index a87494c21fd0..d7af140b2099 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -3227,6 +3227,29 @@ INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) { #define INIT_GETIFADDRS #endif +#if SANITIZER_INTERCEPT_IF_INDEXTONAME +INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname); + char *res = REAL(if_indextoname)(ifindex, ifname); + if (res && ifname) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1); + return res; +} +INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname); + if (ifname) + COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1); + return REAL(if_nametoindex)(ifname); +} +#define INIT_IF_INDEXTONAME \ + COMMON_INTERCEPT_FUNCTION(if_indextoname); \ + COMMON_INTERCEPT_FUNCTION(if_nametoindex); +#else +#define INIT_IF_INDEXTONAME +#endif + #define SANITIZER_COMMON_INTERCEPTORS_INIT \ INIT_TEXTDOMAIN; \ INIT_STRCMP; \ @@ -3346,5 +3369,6 @@ INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) { INIT_LISTXATTR; \ INIT_GETXATTR; \ INIT_GETRESID; \ - INIT_GETIFADDRS; + INIT_GETIFADDRS; \ + INIT_IF_INDEXTONAME; /**/ diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index 154b5d2f5e7e..ef514dd0171e 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -183,5 +183,6 @@ #define SANITIZER_INTERCEPT_GETXATTR SI_LINUX #define SANITIZER_INTERCEPT_GETRESID SI_LINUX #define SANITIZER_INTERCEPT_GETIFADDRS SI_LINUX_NOT_ANDROID | SI_MAC +#define SANITIZER_INTERCEPT_IF_INDEXTONAME SI_LINUX_NOT_ANDROID | SI_MAC #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H