[llvm-commits] [compiler-rt] r172805 - in /compiler-rt/trunk/lib: asan/asan_interceptors.cc interception/interception.h msan/msan_interceptors.cc msan/tests/msan_test.cc sanitizer_common/CMakeLists.txt sanitizer_common/sanitizer_common_interceptors.h sanitizer_common/sanitizer_common_interceptors_scanf.h sanitizer_common/sanitizer_platform_interceptors.h sanitizer_common/tests/CMakeLists.txt sanitizer_common/tests/sanitizer_scanf_interceptor_test.cc tsan/rtl/tsan_interceptors.cc tsan/rtl/tsan_stat.cc tsan/rtl/tsan_stat.h

Evgeniy Stepanov eugeni.stepanov at gmail.com
Fri Jan 18 03:17:24 PST 2013


Author: eugenis
Date: Fri Jan 18 05:17:23 2013
New Revision: 172805

URL: http://llvm.org/viewvc/llvm-project?rev=172805&view=rev
Log:
[sanitizer] Common *scanf interceptors.

Added:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_scanf.h   (with props)
    compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_scanf_interceptor_test.cc   (with props)
Modified:
    compiler-rt/trunk/lib/asan/asan_interceptors.cc
    compiler-rt/trunk/lib/interception/interception.h
    compiler-rt/trunk/lib/msan/msan_interceptors.cc
    compiler-rt/trunk/lib/msan/tests/msan_test.cc
    compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
    compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt
    compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h

Modified: compiler-rt/trunk/lib/asan/asan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.cc?rev=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_interceptors.cc Fri Jan 18 05:17:23 2013
@@ -86,12 +86,12 @@
 // ---------------------- Wrappers ---------------- {{{1
 using namespace __asan;  // NOLINT
 
-#define COMMON_INTERCEPTOR_WRITE_RANGE(ptr, size) ASAN_WRITE_RANGE(ptr, size)
-#define COMMON_INTERCEPTOR_READ_RANGE(ptr, size) ASAN_READ_RANGE(ptr, size)
-#define COMMON_INTERCEPTOR_ENTER(func, ...) ENSURE_ASAN_INITED()
-#define COMMON_INTERCEPTOR_FD_ACQUIRE(fd) do { } while (false)
-#define COMMON_INTERCEPTOR_FD_RELEASE(fd) do { } while (false)
-#define COMMON_INTERCEPTOR_SET_THREAD_NAME(name) SetThreadName(name)
+#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) ASAN_WRITE_RANGE(ptr, size)
+#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size)
+#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) (void)ctx; ENSURE_ASAN_INITED()
+#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) do { } while (false)
+#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) do { } while (false)
+#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
 #include "sanitizer_common/sanitizer_common_interceptors.h"
 
 static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {

Modified: compiler-rt/trunk/lib/interception/interception.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception.h?rev=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception.h (original)
+++ compiler-rt/trunk/lib/interception/interception.h Fri Jan 18 05:17:23 2013
@@ -25,6 +25,8 @@
 // the standard system types (e.g. SSIZE_T instead of ssize_t)
 typedef __sanitizer::uptr SIZE_T;
 typedef __sanitizer::sptr SSIZE_T;
+typedef __sanitizer::sptr PTRDIFF_T;
+typedef __sanitizer::s64  INTMAX_T;
 typedef __sanitizer::u64  OFF_T;
 typedef __sanitizer::u64  OFF64_T;
 

Modified: compiler-rt/trunk/lib/msan/msan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan_interceptors.cc?rev=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/msan/msan_interceptors.cc Fri Jan 18 05:17:23 2013
@@ -735,13 +735,13 @@
   return res;
 }
 
-#define COMMON_INTERCEPTOR_WRITE_RANGE(ptr, size)  \
+#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
     __msan_unpoison(ptr, size)
-#define COMMON_INTERCEPTOR_READ_RANGE(ptr, size) do { } while (false)
-#define COMMON_INTERCEPTOR_ENTER(func, ...) ENSURE_MSAN_INITED()
-#define COMMON_INTERCEPTOR_FD_ACQUIRE(fd) do { } while (false)
-#define COMMON_INTERCEPTOR_FD_RELEASE(fd) do { } while (false)
-#define COMMON_INTERCEPTOR_SET_THREAD_NAME(name) do { } while (false)  // FIXME
+#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) do { } while (false)
+#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) (void)ctx; ENSURE_MSAN_INITED()
+#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) do { } while (false)
+#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) do { } while (false)
+#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) do { } while (false)  // FIXME
 #include "sanitizer_common/sanitizer_common_interceptors.h"
 
 // static

Modified: compiler-rt/trunk/lib/msan/tests/msan_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/tests/msan_test.cc?rev=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/tests/msan_test.cc (original)
+++ compiler-rt/trunk/lib/msan/tests/msan_test.cc Fri Jan 18 05:17:23 2013
@@ -1246,6 +1246,25 @@
   v_u8 = (unsigned long)info.dli_saddr;
 }
 
+TEST(MemorySanitizer, scanf) {
+  const char *input = "42 hello";
+  int* d = new int;
+  char* s = new char[7];
+  int res = sscanf(input, "%d %5s", d, s);
+  printf("res %d\n", res);
+  assert(res == 2);
+  v_s4 = *d;
+  v_u1 = s[0];
+  v_u1 = s[1];
+  v_u1 = s[2];
+  v_u1 = s[3];
+  v_u1 = s[4];
+  v_u1 = s[5];
+  EXPECT_POISONED(v_u1 = s[6]);
+  delete s;
+  delete d;
+}
+
 static void* SimpleThread_threadfn(void* data) {
   return new int;
 }

Modified: compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt?rev=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt Fri Jan 18 05:17:23 2013
@@ -30,6 +30,7 @@
   sanitizer_atomic.h
   sanitizer_common.h
   sanitizer_common_interceptors.h
+  sanitizer_common_interceptors_scanf.h
   sanitizer_flags.h
   sanitizer_internal_defs.h
   sanitizer_lfstack.h

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.h?rev=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.h Fri Jan 18 05:17:23 2013
@@ -25,14 +25,17 @@
 #include "interception/interception.h"
 #include "sanitizer_platform_interceptors.h"
 
+#include <stdarg.h>
+
 #if SANITIZER_INTERCEPT_READ
 INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
-  COMMON_INTERCEPTOR_ENTER(read, fd, ptr, count);
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
   SSIZE_T res = REAL(read)(fd, ptr, count);
   if (res > 0)
-    COMMON_INTERCEPTOR_WRITE_RANGE(ptr, res);
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
   if (res >= 0 && fd >= 0)
-    COMMON_INTERCEPTOR_FD_ACQUIRE(fd);
+    COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
   return res;
 }
 # define INIT_READ INTERCEPT_FUNCTION(read)
@@ -42,12 +45,13 @@
 
 #if SANITIZER_INTERCEPT_PREAD
 INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
-  COMMON_INTERCEPTOR_ENTER(pread, fd, ptr, count, offset);
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
   SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
   if (res > 0)
-    COMMON_INTERCEPTOR_WRITE_RANGE(ptr, res);
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
   if (res >= 0 && fd >= 0)
-    COMMON_INTERCEPTOR_FD_ACQUIRE(fd);
+    COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
   return res;
 }
 # define INIT_PREAD INTERCEPT_FUNCTION(pread)
@@ -57,12 +61,13 @@
 
 #if SANITIZER_INTERCEPT_PREAD64
 INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
-  COMMON_INTERCEPTOR_ENTER(pread64, fd, ptr, count, offset);
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
   SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
   if (res > 0)
-    COMMON_INTERCEPTOR_WRITE_RANGE(ptr, res);
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
   if (res >= 0 && fd >= 0)
-    COMMON_INTERCEPTOR_FD_ACQUIRE(fd);
+    COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
   return res;
 }
 # define INIT_PREAD64 INTERCEPT_FUNCTION(pread64)
@@ -72,12 +77,13 @@
 
 #if SANITIZER_INTERCEPT_WRITE
 INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
-  COMMON_INTERCEPTOR_ENTER(write, fd, ptr, count);
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
   if (fd >= 0)
-    COMMON_INTERCEPTOR_FD_RELEASE(fd);
+    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
   SSIZE_T res = REAL(write)(fd, ptr, count);
   if (res > 0)
-    COMMON_INTERCEPTOR_READ_RANGE(ptr, res);
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
   return res;
 }
 # define INIT_WRITE INTERCEPT_FUNCTION(write)
@@ -87,12 +93,13 @@
 
 #if SANITIZER_INTERCEPT_PWRITE
 INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count) {
-  COMMON_INTERCEPTOR_ENTER(pwrite, fd, ptr, count);
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count);
   if (fd >= 0)
-    COMMON_INTERCEPTOR_FD_RELEASE(fd);
+    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
   SSIZE_T res = REAL(pwrite)(fd, ptr, count);
   if (res > 0)
-    COMMON_INTERCEPTOR_READ_RANGE(ptr, res);
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
   return res;
 }
 # define INIT_PWRITE INTERCEPT_FUNCTION(pwrite)
@@ -102,12 +109,13 @@
 
 #if SANITIZER_INTERCEPT_PWRITE64
 INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count) {
-  COMMON_INTERCEPTOR_ENTER(pwrite64, fd, ptr, count);
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count);
   if (fd >= 0)
-    COMMON_INTERCEPTOR_FD_RELEASE(fd);
+    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
   SSIZE_T res = REAL(pwrite64)(fd, ptr, count);
   if (res > 0)
-    COMMON_INTERCEPTOR_READ_RANGE(ptr, res);
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
   return res;
 }
 # define INIT_PWRITE64 INTERCEPT_FUNCTION(pwrite64)
@@ -119,14 +127,15 @@
 INTERCEPTOR(int, prctl, int option,
             unsigned long arg2, unsigned long arg3,  // NOLINT
             unsigned long arg4, unsigned long arg5) {  // NOLINT
-  COMMON_INTERCEPTOR_ENTER(prctl, option, arg2, arg3, arg4, arg5);
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
   static const int PR_SET_NAME = 15;
   int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
   if (option == PR_SET_NAME) {
     char buff[16];
     internal_strncpy(buff, (char*)arg2, 15);
     buff[15] = 0;
-    COMMON_INTERCEPTOR_SET_THREAD_NAME(buff);
+    COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
   }
   return res;
 }
@@ -135,11 +144,86 @@
 # define INIT_PRCTL
 #endif  // SANITIZER_INTERCEPT_PRCTL
 
+
+#if SANITIZER_INTERCEPT_SCANF
+
+#include "sanitizer_common_interceptors_scanf.h"
+
+INTERCEPTOR(int, vscanf, const char *format, va_list ap) {  // NOLINT
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, vscanf, format, ap);
+  scanf_common(ctx, format, ap);
+  int res = REAL(vscanf)(format, ap);  // NOLINT
+  return res;
+}
+
+INTERCEPTOR(int, vsscanf, const char *str, const char *format,  // NOLINT
+    va_list ap) {
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, vsscanf, str, format, ap);
+  scanf_common(ctx, format, ap);
+  int res = REAL(vsscanf)(str, format, ap);  // NOLINT
+  // FIXME: read of str
+  return res;
+}
+
+INTERCEPTOR(int, vfscanf, void *stream, const char *format,  // NOLINT
+    va_list ap) {
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, vfscanf, stream, format, ap);
+  scanf_common(ctx, format, ap);
+  int res = REAL(vfscanf)(stream, format, ap);  // NOLINT
+  return res;
+}
+
+INTERCEPTOR(int, scanf, const char *format, ...) {  // NOLINT
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, scanf, format);
+  va_list ap;
+  va_start(ap, format);
+  int res = vscanf(format, ap);  // NOLINT
+  va_end(ap);
+  return res;
+}
+
+INTERCEPTOR(int, fscanf, void* stream, const char *format, ...) {  // NOLINT
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, fscanf, stream, format);
+  va_list ap;
+  va_start(ap, format);
+  int res = vfscanf(stream, format, ap);  // NOLINT
+  va_end(ap);
+  return res;
+}
+
+INTERCEPTOR(int, sscanf, const char *str, const char *format, ...) {  // NOLINT
+  void* ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, sscanf, str, format);
+  va_list ap;
+  va_start(ap, format);
+  int res = vsscanf(str, format, ap);  // NOLINT
+  va_end(ap);
+  return res;
+}
+
+#define INIT_SCANF \
+  INTERCEPT_FUNCTION(scanf); \
+  INTERCEPT_FUNCTION(sscanf); \
+  INTERCEPT_FUNCTION(fscanf); \
+  INTERCEPT_FUNCTION(vscanf); \
+  INTERCEPT_FUNCTION(vsscanf); \
+  INTERCEPT_FUNCTION(vfscanf)
+
+#else
+#define INIT_SCANF
+#endif
+
 #define SANITIZER_COMMON_INTERCEPTORS_INIT \
   INIT_READ;                               \
   INIT_PREAD;                              \
   INIT_PREAD64;                            \
   INIT_PRCTL;                              \
   INIT_WRITE;                              \
+  INIT_SCANF;                              \
 
 #endif  // SANITIZER_COMMON_INTERCEPTORS_H

Added: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_scanf.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_scanf.h?rev=172805&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_scanf.h (added)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_scanf.h Fri Jan 18 05:17:23 2013
@@ -0,0 +1,133 @@
+#ifndef SANITIZER_COMMON_INTERCEPTORS_SCANF_H
+#define SANITIZER_COMMON_INTERCEPTORS_SCANF_H
+
+struct ScanfSpec {
+  char c;
+  unsigned size;
+};
+
+// One-letter specs.
+static const ScanfSpec scanf_specs[] = {
+  {'p', sizeof(void *)},
+  {'e', sizeof(float)},
+  {'E', sizeof(float)},
+  {'a', sizeof(float)},
+  {'f', sizeof(float)},
+  {'g', sizeof(float)},
+  {'d', sizeof(int)},
+  {'i', sizeof(int)},
+  {'o', sizeof(int)},
+  {'u', sizeof(int)},
+  {'x', sizeof(int)},
+  {'X', sizeof(int)},
+  {'n', sizeof(int)},
+  {'t', sizeof(PTRDIFF_T)},
+  {'z', sizeof(SIZE_T)},
+  {'j', sizeof(INTMAX_T)},
+  {'h', sizeof(short)}
+};
+
+static const unsigned scanf_specs_cnt =
+  sizeof(scanf_specs) / sizeof(scanf_specs[0]);
+
+// %ll?, %L?, %q? specs
+static const ScanfSpec scanf_llspecs[] = {
+  {'e', sizeof(long double)},
+  {'f', sizeof(long double)},
+  {'g', sizeof(long double)},
+  {'d', sizeof(long long)},
+  {'i', sizeof(long long)},
+  {'o', sizeof(long long)},
+  {'u', sizeof(long long)},
+  {'x', sizeof(long long)}
+};
+
+static const unsigned scanf_llspecs_cnt =
+  sizeof(scanf_llspecs) / sizeof(scanf_llspecs[0]);
+
+// %l? specs
+static const ScanfSpec scanf_lspecs[] = {
+  {'e', sizeof(double)},
+  {'f', sizeof(double)},
+  {'g', sizeof(double)},
+  {'d', sizeof(long)},
+  {'i', sizeof(long)},
+  {'o', sizeof(long)},
+  {'u', sizeof(long)},
+  {'x', sizeof(long)},
+  {'X', sizeof(long)},
+};
+
+static const unsigned scanf_lspecs_cnt =
+  sizeof(scanf_lspecs) / sizeof(scanf_lspecs[0]);
+
+static unsigned match_spec(const struct ScanfSpec *spec, unsigned n, char c) {
+  for (unsigned i = 0; i < n; ++i)
+    if (spec[i].c == c)
+      return spec[i].size;
+  return 0;
+}
+
+static void scanf_common(void *ctx, const char *format, va_list ap_const) {
+  va_list aq;
+  va_copy(aq, ap_const);
+
+  const char *p = format;
+  unsigned size;
+
+  while (*p) {
+    if (*p != '%') {
+      ++p;
+      continue;
+    }
+    ++p;
+    if (*p == '*' || *p == '%' || *p == 0) {
+      ++p;
+      continue;
+    }
+    if (*p == '0' || (*p >= '1' && *p <= '9')) {
+      size = internal_atoll(p);
+      // +1 for the \0 at the end
+      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, va_arg(aq, void *), size + 1);
+      ++p;
+      continue;
+    }
+    
+    if (*p == 'L' || *p == 'q') {
+      ++p;
+      size = match_spec(scanf_llspecs, scanf_llspecs_cnt, *p);
+      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, va_arg(aq, void *), size);
+      continue;
+    }
+
+    if (*p == 'l') {
+      ++p;
+      if (*p == 'l') {
+        ++p;
+        size = match_spec(scanf_llspecs, scanf_llspecs_cnt, *p);
+        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, va_arg(aq, void *), size);
+        continue;
+      } else {
+        size = match_spec(scanf_lspecs, scanf_lspecs_cnt, *p);
+        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, va_arg(aq, void *), size);
+        continue;
+      }
+    }
+
+    if (*p == 'h' && *(p + 1) == 'h') {
+      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, va_arg(aq, void *), sizeof(char));
+      p += 2;
+      continue;
+    }
+
+    size = match_spec(scanf_specs, scanf_specs_cnt, *p);
+    if (size) {
+      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, va_arg(aq, void *), size);
+      ++p;
+      continue;
+    }
+  }
+  va_end(aq);
+}
+
+#endif  // SANITIZER_COMMON_INTERCEPTORS_SCANF_H

Propchange: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_scanf.h
------------------------------------------------------------------------------
    svn:eol-style = LF

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=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Fri Jan 18 05:17:23 2013
@@ -35,4 +35,4 @@
 # define SANITIZER_INTERCEPT_PWRITE64 SI_LINUX_NOT_ANDROID
 # define SANITIZER_INTERCEPT_PRCTL   SI_LINUX_NOT_ANDROID
 
-
+# define SANITIZER_INTERCEPT_SCANF 1

Modified: compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt?rev=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/CMakeLists.txt Fri Jan 18 05:17:23 2013
@@ -9,6 +9,7 @@
   sanitizer_list_test.cc
   sanitizer_mutex_test.cc
   sanitizer_printf_test.cc
+  sanitizer_scanf_interceptor_test.cc
   sanitizer_stackdepot_test.cc
   sanitizer_test_main.cc
   )

Added: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_scanf_interceptor_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_scanf_interceptor_test.cc?rev=172805&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_scanf_interceptor_test.cc (added)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_scanf_interceptor_test.cc Fri Jan 18 05:17:23 2013
@@ -0,0 +1,74 @@
+#include <vector>
+
+#include "interception/interception.h"
+#include "sanitizer_test_utils.h"
+#include "sanitizer_common/sanitizer_libc.h"
+#include "gtest/gtest.h"
+
+using namespace __sanitizer;
+
+#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
+  ((std::vector<unsigned> *)ctx)->push_back(size)
+
+#include "sanitizer_common/sanitizer_common_interceptors_scanf.h"
+
+static void testScanf2(void *ctx, const char *format, ...) {
+  va_list ap;
+  va_start(ap, format);
+  scanf_common(ctx, format, ap);
+  va_end(ap);
+}
+
+static void testScanf(const char *format, unsigned n, ...) {
+  std::vector<unsigned> scanf_sizes;
+  // 16 args should be enough.
+  testScanf2((void *)&scanf_sizes, format,
+      (void*)0, (void*)0, (void*)0, (void*)0,
+      (void*)0, (void*)0, (void*)0, (void*)0,
+      (void*)0, (void*)0, (void*)0, (void*)0,
+      (void*)0, (void*)0, (void*)0, (void*)0);
+  ASSERT_EQ(n, scanf_sizes.size()) <<
+    "Unexpected number of format arguments: '" << format << "'";
+  va_list ap;
+  va_start(ap, n);
+  for (unsigned i = 0; i < n; ++i)
+    EXPECT_EQ(va_arg(ap, unsigned), scanf_sizes[i]) <<
+      "Unexpect write size for argument " << i << ", format string '" <<
+      format << "'";
+  va_end(ap);
+}
+
+TEST(SanitizerCommonInterceptors, Scanf) {
+  const unsigned I = sizeof(int);
+  const unsigned L = sizeof(long);
+  const unsigned LL = sizeof(long long);
+  const unsigned S = sizeof(short);
+  const unsigned C = sizeof(char);
+  const unsigned D = sizeof(double);
+  const unsigned F = sizeof(float);
+  const unsigned LD = sizeof(long double);
+
+  testScanf("%d", 1, I);
+  testScanf("%d%d%d", 3, I, I, I);
+  testScanf("ab%u%dc", 2, I, I);
+  testScanf("%ld", 1, L);
+  testScanf("%llu", 1, LL);
+  testScanf("a %hd%hhx", 2, S, C);
+
+  testScanf("%%", 0);
+  testScanf("a%%", 0);
+  testScanf("a%%b", 0);
+  testScanf("a%%%%b", 0);
+  testScanf("a%%b%%", 0);
+  testScanf("a%%%%%%b", 0);
+  testScanf("a%%%%%b", 0);
+  testScanf("a%%%%%f", 1, F);
+  testScanf("a%%%lxb", 1, L);
+  testScanf("a%lf%%%lxb", 2, D, L);
+  testScanf("%nf", 1, I);
+
+  testScanf("%10s", 1, 11);
+  testScanf("%%10s", 0);
+  testScanf("%*10s", 0);
+  testScanf("%*d", 0);
+}

Propchange: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_scanf_interceptor_test.cc
------------------------------------------------------------------------------
    svn:eol-style = LF

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc?rev=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc Fri Jan 18 05:17:23 2013
@@ -1594,15 +1594,30 @@
   return pid;
 }
 
-#define COMMON_INTERCEPTOR_WRITE_RANGE(ptr, size)  \
-    MemoryAccessRange(thr, pc, (uptr)ptr, size, true)
-#define COMMON_INTERCEPTOR_READ_RANGE(ptr, size)  \
-    MemoryAccessRange(thr, pc, (uptr)ptr, size, false)
-#define COMMON_INTERCEPTOR_ENTER(func, ...) \
-  SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__)
-#define COMMON_INTERCEPTOR_FD_ACQUIRE(fd) FdAcquire(thr, pc, fd)
-#define COMMON_INTERCEPTOR_FD_RELEASE(fd) FdRelease(thr, pc, fd)
-#define COMMON_INTERCEPTOR_SET_THREAD_NAME(name) ThreadSetName(thr, name)
+struct TsanInterceptorContext {
+  ThreadState *thr;
+  const uptr caller_pc;
+  const uptr pc;
+};
+
+#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
+    MemoryAccessRange(((TsanInterceptorContext*)ctx)->thr,  \
+                      ((TsanInterceptorContext*)ctx)->pc,   \
+                      (uptr)ptr, size, true)
+#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size)       \
+    MemoryAccessRange(((TsanInterceptorContext*)ctx)->thr,  \
+                      ((TsanInterceptorContext*)ctx)->pc,   \
+                      (uptr)ptr, size, false)
+#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
+    SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__) \
+    TsanInterceptorContext _ctx = {thr, caller_pc, pc}; \
+    ctx = (void*)&_ctx; (void)ctx;
+#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
+    FdAcquire(((TsanInterceptorContext*)ctx)->thr, pc, fd)
+#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
+    FdRelease(((TsanInterceptorContext*)ctx)->thr, pc, fd)
+#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
+    ThreadSetName(((TsanInterceptorContext*)ctx)->thr, name)
 #include "sanitizer_common/sanitizer_common_interceptors.h"
 
 namespace __tsan {

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=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc Fri Jan 18 05:17:23 2013
@@ -236,6 +236,12 @@
   name[StatInt_nanosleep]                = "  nanosleep                       ";
   name[StatInt_gettimeofday]             = "  gettimeofday                    ";
   name[StatInt_fork]                     = "  fork                            ";
+  name[StatInt_vscanf]                   = "  vscanf                          ";
+  name[StatInt_vsscanf]                  = "  vsscanf                         ";
+  name[StatInt_vfscanf]                  = "  vfscanf                         ";
+  name[StatInt_scanf]                    = "  scanf                           ";
+  name[StatInt_sscanf]                   = "  sscanf                          ";
+  name[StatInt_fscanf]                   = "  fscanf                          ";
 
   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=172805&r1=172804&r2=172805&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h Fri Jan 18 05:17:23 2013
@@ -235,6 +235,12 @@
   StatInt_nanosleep,
   StatInt_gettimeofday,
   StatInt_fork,
+  StatInt_vscanf,
+  StatInt_vsscanf,
+  StatInt_vfscanf,
+  StatInt_scanf,
+  StatInt_sscanf,
+  StatInt_fscanf,
 
   // Dynamic annotations.
   StatAnnotation,





More information about the llvm-commits mailing list