[compiler-rt] r179091 - [msan] Intercept glob() with tests.

Evgeniy Stepanov eugeni.stepanov at gmail.com
Tue Apr 9 04:35:14 PDT 2013


Author: eugenis
Date: Tue Apr  9 06:35:13 2013
New Revision: 179091

URL: http://llvm.org/viewvc/llvm-project?rev=179091&view=rev
Log:
[msan] Intercept glob() with tests.

Added:
    compiler-rt/trunk/lib/asan/lit_tests/Linux/glob.cc   (with props)
    compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/
    compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/aa
    compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/ab
    compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/ba
    compiler-rt/trunk/lib/msan/lit_tests/Linux/
    compiler-rt/trunk/lib/msan/lit_tests/Linux/glob.cc   (with props)
    compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/
    compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/aa
    compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/ab
    compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/ba
    compiler-rt/trunk/lib/msan/lit_tests/Linux/lit.local.cfg
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/tsan/rtl/tsan_stat.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h

Added: compiler-rt/trunk/lib/asan/lit_tests/Linux/glob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/Linux/glob.cc?rev=179091&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/Linux/glob.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/Linux/glob.cc Tue Apr  9 06:35:13 2013
@@ -0,0 +1,30 @@
+// RUN: %clangxx_asan -m64 -O0 %s -o %t && %t %p 2>&1 | FileCheck %s
+// RUN: %clangxx_asan -m64 -O3 %s -o %t && %t %p 2>&1 | FileCheck %s
+// RUN: %clangxx_asan -m32 -O0 %s -o %t && %t %p 2>&1 | FileCheck %s
+// RUN: %clangxx_asan -m32 -O3 %s -o %t && %t %p 2>&1 | FileCheck %s
+
+#include <assert.h>
+#include <glob.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <string>
+
+
+int main(int argc, char *argv[]) {
+  std::string path = argv[1];
+  std::string pattern = path + "/glob_test_root/*a";
+  printf("pattern: %s\n", pattern.c_str());
+
+  glob_t globbuf;
+  int res = glob(pattern.c_str(), 0, 0, &globbuf);
+
+  printf("%d %s\n", errno, strerror(errno));
+  assert(res == 0);
+  assert(globbuf.gl_pathc == 2);
+  printf("%zu\n", strlen(globbuf.gl_pathv[0]));
+  printf("%zu\n", strlen(globbuf.gl_pathv[1]));
+  printf("PASS\n");
+  // CHECK: PASS
+  return 0;
+}

Propchange: compiler-rt/trunk/lib/asan/lit_tests/Linux/glob.cc
------------------------------------------------------------------------------
    svn:eol-style = LF

Added: compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/aa
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/aa?rev=179091&view=auto
==============================================================================
    (empty)

Added: compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/ab
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/ab?rev=179091&view=auto
==============================================================================
    (empty)

Added: compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/ba
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/Linux/glob_test_root/ba?rev=179091&view=auto
==============================================================================
    (empty)

Added: compiler-rt/trunk/lib/msan/lit_tests/Linux/glob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/Linux/glob.cc?rev=179091&view=auto
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/Linux/glob.cc (added)
+++ compiler-rt/trunk/lib/msan/lit_tests/Linux/glob.cc Tue Apr  9 06:35:13 2013
@@ -0,0 +1,26 @@
+// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t %p 2>&1 | FileCheck %s
+// RUN: %clangxx_msan -m64 -O3 %s -o %t && %t %p 2>&1 | FileCheck %s
+
+#include <assert.h>
+#include <glob.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+int main(int argc, char *argv[]) {
+  assert(argc == 2);
+  char buf[1024];
+  snprintf(buf, sizeof(buf), "%s/%s", argv[1], "glob_test_root/*a");
+
+  glob_t globbuf;
+  int res = glob(buf, 0, 0, &globbuf);
+
+  printf("%d %s\n", errno, strerror(errno));
+  assert(res == 0);
+  assert(globbuf.gl_pathc == 2);
+  printf("%zu\n", strlen(globbuf.gl_pathv[0]));
+  printf("%zu\n", strlen(globbuf.gl_pathv[1]));
+  printf("PASS\n");
+  // CHECK: PASS
+  return 0;
+}

Propchange: compiler-rt/trunk/lib/msan/lit_tests/Linux/glob.cc
------------------------------------------------------------------------------
    svn:eol-style = LF

Added: compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/aa
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/aa?rev=179091&view=auto
==============================================================================
    (empty)

Added: compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/ab
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/ab?rev=179091&view=auto
==============================================================================
    (empty)

Added: compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/ba
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/Linux/glob_test_root/ba?rev=179091&view=auto
==============================================================================
    (empty)

Added: compiler-rt/trunk/lib/msan/lit_tests/Linux/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/Linux/lit.local.cfg?rev=179091&view=auto
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/Linux/lit.local.cfg (added)
+++ compiler-rt/trunk/lib/msan/lit_tests/Linux/lit.local.cfg Tue Apr  9 06:35:13 2013
@@ -0,0 +1,9 @@
+def getRoot(config):
+  if not config.parent:
+    return config
+  return getRoot(config.parent)
+
+root = getRoot(config)
+
+if root.host_os not in ['Linux']:
+  config.unsupported = True

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=179091&r1=179090&r2=179091&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Tue Apr  9 06:35:13 2013
@@ -549,6 +549,52 @@ INTERCEPTOR(int, setitimer, int which, c
 #endif
 
 
+#if SANITIZER_INTERCEPT_GLOB
+struct sanitizer_glob_t {
+  SIZE_T gl_pathc;
+  char **gl_pathv;
+};
+
+static void unpoison_glob_t(void *ctx, sanitizer_glob_t *pglob) {
+  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
+  // +1 for NULL pointer at the end.
+  COMMON_INTERCEPTOR_WRITE_RANGE(
+      ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
+  for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
+    char *p = pglob->gl_pathv[i];
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
+  }
+}
+
+INTERCEPTOR(int, glob, const char *pattern, int flags,
+            int (*errfunc)(const char *epath, int eerrno),
+            sanitizer_glob_t *pglob) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
+  int res = REAL(glob)(pattern, flags, errfunc, pglob);
+  if (res == 0)
+    unpoison_glob_t(ctx, pglob);
+  return res;
+}
+
+INTERCEPTOR(int, glob64, const char *pattern, int flags,
+            int (*errfunc)(const char *epath, int eerrno),
+            sanitizer_glob_t *pglob) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
+  int res = REAL(glob64)(pattern, flags, errfunc, pglob);
+  if (res == 0)
+    unpoison_glob_t(ctx, pglob);
+  return res;
+}
+#define INIT_GLOB                               \
+  INTERCEPT_FUNCTION(glob);                     \
+  INTERCEPT_FUNCTION(glob64);
+#else // SANITIZER_INTERCEPT_GLOB
+#define INIT_GLOB
+#endif // SANITIZER_INTERCEPT_GLOB
+
+
 #define SANITIZER_COMMON_INTERCEPTORS_INIT                                     \
   INIT_STRCASECMP;                                                             \
   INIT_STRNCASECMP;                                                            \
@@ -567,4 +613,5 @@ INTERCEPTOR(int, setitimer, int which, c
   INIT_GETPWNAM_R_GETPWUID_R;                                                  \
   INIT_CLOCK_GETTIME;                                                          \
   INIT_GETITIMER;                                                              \
-  INIT_TIME;
+  INIT_TIME;                                                                   \
+  INIT_GLOB;

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=179091&r1=179090&r2=179091&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Tue Apr  9 06:35:13 2013
@@ -64,3 +64,4 @@
 # define SANITIZER_INTERCEPT_CLOCK_GETTIME SI_LINUX
 # define SANITIZER_INTERCEPT_GETITIMER SI_NOT_WINDOWS
 # define SANITIZER_INTERCEPT_TIME SI_NOT_WINDOWS
+# define SANITIZER_INTERCEPT_GLOB SI_LINUX_NOT_ANDROID

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc?rev=179091&r1=179090&r2=179091&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc Tue Apr  9 06:35:13 2013
@@ -297,6 +297,8 @@ void StatOutput(u64 *stat) {
   name[StatInt_getitimer]                = "  getitimer                       ";
   name[StatInt_setitimer]                = "  setitimer                       ";
   name[StatInt_time]                     = "  time                            ";
+  name[StatInt_glob]                     = "  glob                            ";
+  name[StatInt_glob64]                   = "  glob64                          ";
 
   name[StatAnnotation]                   = "Dynamic annotations               ";
   name[StatAnnotateHappensBefore]        = "  HappensBefore                   ";

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h?rev=179091&r1=179090&r2=179091&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h Tue Apr  9 06:35:13 2013
@@ -292,6 +292,8 @@ enum StatType {
   StatInt_getitimer,
   StatInt_setitimer,
   StatInt_time,
+  StatInt_glob,
+  StatInt_glob64,
 
   // Dynamic annotations.
   StatAnnotation,





More information about the llvm-commits mailing list