[compiler-rt] r200926 - [sanitizer] Intercept getifaddrs().
Sergey Matveev
earthdok at google.com
Thu Feb 6 09:42:37 PST 2014
Author: smatveev
Date: Thu Feb 6 11:42:36 2014
New Revision: 200926
URL: http://llvm.org/viewvc/llvm-project?rev=200926&view=rev
Log:
[sanitizer] Intercept getifaddrs().
Added:
compiler-rt/trunk/lib/msan/lit_tests/ifaddrs.cc
Modified:
compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.h
Added: compiler-rt/trunk/lib/msan/lit_tests/ifaddrs.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/ifaddrs.cc?rev=200926&view=auto
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/ifaddrs.cc (added)
+++ compiler-rt/trunk/lib/msan/lit_tests/ifaddrs.cc Thu Feb 6 11:42:36 2014
@@ -0,0 +1,41 @@
+// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t %p 2>&1
+// RUN: %clangxx_msan -m64 -O0 -D_FILE_OFFSET_BITS=64 %s -o %t && %t %p 2>&1
+// RUN: %clangxx_msan -m64 -O3 %s -o %t && %t %p 2>&1
+
+#include <assert.h>
+#include <errno.h>
+#include <ifaddrs.h>
+#include <stdio.h>
+
+#include <vector>
+
+#include <sanitizer/msan_interface.h>
+
+int main(int argc, char *argv[]) {
+ struct ifaddrs *ifas;
+
+ assert(0 == __msan_test_shadow(&ifas, sizeof(ifaddrs *)));
+ int res = getifaddrs(&ifas);
+ if (res == -1) {
+ assert(errno == ENOSYS);
+ printf("getifaddrs() is not implemented\n");
+ return 0;
+ }
+ assert(res == 0);
+ assert(-1 == __msan_test_shadow(&ifas, sizeof(ifaddrs *)));
+
+ std::vector<ifaddrs *> ifas_vector;
+ ifaddrs *p = ifas;
+ while (p) {
+ ifas_vector.push_back(p);
+ assert(-1 == __msan_test_shadow(p, sizeof(ifaddrs)));
+ p = p->ifa_next;
+ }
+
+ freeifaddrs(ifas);
+ for (int i = 0; i < ifas_vector.size(); i++) {
+ ifaddrs *p = ifas_vector[i];
+ assert(0 == __msan_test_shadow(p, sizeof(ifaddrs)));
+ }
+ return 0;
+}
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=200926&r1=200925&r2=200926&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Thu Feb 6 11:42:36 2014
@@ -3191,6 +3191,31 @@ INTERCEPTOR(int, getresgid, void *rgid,
#define INIT_GETRESID
#endif
+#if SANITIZER_INTERCEPT_GETIFADDRS
+// As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
+// intercept freeifaddrs(). If that ceases to be the case, we might need to
+// intercept it to poison the memory again.
+INTERCEPTOR(int, getifaddrs, void **ifap) {
+ void *ctx;
+ COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
+ int res = REAL(getifaddrs)(ifap);
+ if (res == 0 && ifap) {
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
+ void *p = *ifap;
+ while (p) {
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, struct_ifaddrs_sz);
+ // Pointer to the next item is stored in the first field.
+ p = *reinterpret_cast<void **>(p);
+ }
+ }
+ return res;
+}
+#define INIT_GETIFADDRS \
+ COMMON_INTERCEPT_FUNCTION(getifaddrs);
+#else
+#define INIT_GETIFADDRS
+#endif
+
#define SANITIZER_COMMON_INTERCEPTORS_INIT \
INIT_TEXTDOMAIN; \
INIT_STRCMP; \
@@ -3309,5 +3334,6 @@ INTERCEPTOR(int, getresgid, void *rgid,
INIT_TLS_GET_ADDR; \
INIT_LISTXATTR; \
INIT_GETXATTR; \
- INIT_GETRESID;
+ INIT_GETRESID; \
+ INIT_GETIFADDRS;
/**/
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=200926&r1=200925&r2=200926&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Thu Feb 6 11:42:36 2014
@@ -182,5 +182,6 @@
#define SANITIZER_INTERCEPT_LISTXATTR SI_LINUX
#define SANITIZER_INTERCEPT_GETXATTR SI_LINUX
#define SANITIZER_INTERCEPT_GETRESID SI_LINUX
+#define SANITIZER_INTERCEPT_GETIFADDRS SI_LINUX | SI_MAC
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.cc?rev=200926&r1=200925&r2=200926&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.cc Thu Feb 6 11:42:36 2014
@@ -23,6 +23,7 @@
#include <dirent.h>
#include <errno.h>
#include <grp.h>
+#include <ifaddrs.h>
#include <limits.h>
#include <net/if.h>
#include <net/if_arp.h>
@@ -145,6 +146,7 @@ namespace __sanitizer {
unsigned struct_sigevent_sz = sizeof(struct sigevent);
unsigned struct_sched_param_sz = sizeof(struct sched_param);
unsigned struct_statfs_sz = sizeof(struct statfs);
+ unsigned struct_ifaddrs_sz = sizeof(struct ifaddrs);
#if SANITIZER_MAC && !SANITIZER_IOS
unsigned struct_statfs64_sz = sizeof(struct statfs64);
@@ -971,4 +973,6 @@ CHECK_SIZE_AND_OFFSET(shmid_ds, shm_natt
CHECK_TYPE_SIZE(clock_t);
+COMPILER_CHECK(0 == offsetof(ifaddrs, ifa_next));
+
#endif // SANITIZER_LINUX || SANITIZER_MAC
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.h?rev=200926&r1=200925&r2=200926&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_posix.h Thu Feb 6 11:42:36 2014
@@ -43,6 +43,7 @@ namespace __sanitizer {
extern unsigned struct_sched_param_sz;
extern unsigned struct_statfs_sz;
extern unsigned struct_statfs64_sz;
+ extern unsigned struct_ifaddrs_sz;
#if !SANITIZER_ANDROID
extern unsigned ucontext_t_sz;
More information about the llvm-commits
mailing list