[compiler-rt] r197806 - [msan] Wrap indirect calls to REAL(x) in interceptors.

Evgeniy Stepanov eugeni.stepanov at gmail.com
Fri Dec 20 04:20:16 PST 2013


Author: eugenis
Date: Fri Dec 20 06:20:15 2013
New Revision: 197806

URL: http://llvm.org/viewvc/llvm-project?rev=197806&view=rev
Log:
[msan] Wrap indirect calls to REAL(x) in interceptors.

Added:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h   (with props)
Modified:
    compiler-rt/trunk/lib/interception/interception_linux.h
    compiler-rt/trunk/lib/msan/lit_tests/wrap_indirect_calls_in_rtl.cc
    compiler-rt/trunk/lib/msan/msan_interceptors.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc

Modified: compiler-rt/trunk/lib/interception/interception_linux.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_linux.h?rev=197806&r1=197805&r2=197806&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_linux.h (original)
+++ compiler-rt/trunk/lib/interception/interception_linux.h Fri Dec 20 06:20:15 2013
@@ -28,11 +28,11 @@ bool GetRealFunctionAddress(const char *
 void *GetFuncAddrVer(const char *func_name, const char *ver);
 }  // namespace __interception
 
-#define INTERCEPT_FUNCTION_LINUX(func) \
-    ::__interception::GetRealFunctionAddress( \
-          #func, (::__interception::uptr*)&REAL(func), \
-          (::__interception::uptr)&(func), \
-          (::__interception::uptr)&WRAP(func))
+#define INTERCEPT_FUNCTION_LINUX(func)                                     \
+  ::__interception::GetRealFunctionAddress(                                \
+      #func, (::__interception::uptr *)&__interception::PTR_TO_REAL(func), \
+      (::__interception::uptr) & (func),                                   \
+      (::__interception::uptr) & WRAP(func))
 
 #if !defined(__ANDROID__)  // android does not have dlvsym
 # define INTERCEPT_FUNCTION_VER_LINUX(func, symver) \

Modified: compiler-rt/trunk/lib/msan/lit_tests/wrap_indirect_calls_in_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/wrap_indirect_calls_in_rtl.cc?rev=197806&r1=197805&r2=197806&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/wrap_indirect_calls_in_rtl.cc (original)
+++ compiler-rt/trunk/lib/msan/lit_tests/wrap_indirect_calls_in_rtl.cc Fri Dec 20 06:20:15 2013
@@ -3,13 +3,15 @@
 // RUN: %clangxx_msan -O0 -g -rdynamic %s -o %t && %t
 
 #include <assert.h>
+#include <math.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <sys/time.h>
 
 extern "C" void __msan_set_indirect_call_wrapper(uintptr_t);
 
-bool done;
+bool pthread_create_done;
 
 void *ThreadFn(void *) {
   printf("bad threadfn\n");
@@ -18,24 +20,61 @@ void *ThreadFn(void *) {
 
 void *ThreadFn2(void *) {
   printf("good threadfn\n");
-  done = true;
+  pthread_create_done = true;
   return 0;
 }
 
-// ThreadFn is called indirectly from a wrapper function in MSan rtl and
-// is subject to indirect call wrapping (it could be an native-to-translated
-// edge).
+bool in_gettimeofday;
+bool in_lgamma;
+
+int my_gettimeofday(struct timeval *p, void *q) {
+  p->tv_sec = 1;
+  p->tv_usec = 2;
+  return 42;
+}
+
+double my_lgamma(double x) {
+  printf("zzz\n");
+  return x;
+}
+
 extern "C" uintptr_t my_wrapper(uintptr_t f) {
   if (f == (uintptr_t)ThreadFn)
     return (uintptr_t)&ThreadFn2;
+  if (in_gettimeofday)
+    return (uintptr_t)my_gettimeofday;
+  if (in_lgamma)
+    return (uintptr_t)my_lgamma;
   return f;
 }
 
 int main(void) {
   __msan_set_indirect_call_wrapper((uintptr_t)my_wrapper);
+
+  // ThreadFn is called indirectly from a wrapper function in MSan rtl and
+  // is subject to indirect call wrapping (it could be an native-to-translated
+  // edge).
   pthread_t t;
   pthread_create(&t, 0, ThreadFn, 0);
   pthread_join(t, 0);
-  assert(done);
+  assert(pthread_create_done);
+
+  // gettimeofday is intercepted in msan_interceptors.cc and the real one (from
+  // libc) is called indirectly.
+  struct timeval tv;
+  in_gettimeofday = true;
+  int res = gettimeofday(&tv, NULL);
+  in_gettimeofday = false;
+  assert(tv.tv_sec == 1);
+  assert(tv.tv_usec == 2);
+  assert(res == 42);
+
+  // lgamma is intercepted in sanitizer_common_interceptors.inc and is also
+  // called indirectly.
+  in_lgamma = true;
+  double dres = lgamma(1.1);
+  in_lgamma = false;
+  assert(dres == 1.1);
+  
   return 0;
 }

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=197806&r1=197805&r2=197806&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/msan/msan_interceptors.cc Fri Dec 20 06:20:15 2013
@@ -15,13 +15,13 @@
 // sanitizer_common/sanitizer_common_interceptors.h
 //===----------------------------------------------------------------------===//
 
-#include "interception/interception.h"
 #include "msan.h"
 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
 #include "sanitizer_common/sanitizer_allocator.h"
 #include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_interception.h"
 #include "sanitizer_common/sanitizer_stackdepot.h"
 #include "sanitizer_common/sanitizer_libc.h"
 #include "sanitizer_common/sanitizer_linux.h"

Added: compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h?rev=197806&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h (added)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h Fri Dec 20 06:20:15 2013
@@ -0,0 +1,24 @@
+//===-- sanitizer_interception.h --------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// zzz
+//
+//===----------------------------------------------------------------------===//
+#ifndef SANITIZER_INTERCEPTION_H
+#define SANITIZER_INTERCEPTION_H
+
+#include "interception/interception.h"
+#include "sanitizer_common.h"
+
+#if SANITIZER_LINUX && !defined(SANITIZER_GO)
+#undef REAL
+#define REAL(x) IndirectExternCall(__interception::PTR_TO_REAL(x))
+#endif
+
+#endif  // SANITIZER_INTERCEPTION_H

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

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc?rev=197806&r1=197805&r2=197806&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc Fri Dec 20 06:20:15 2013
@@ -357,6 +357,8 @@ uptr GetListOfModules(LoadedModule *modu
 uptr indirect_call_wrapper;
 
 void SetIndirectCallWrapper(uptr wrapper) {
+  CHECK(!indirect_call_wrapper);
+  CHECK(wrapper);
   indirect_call_wrapper = wrapper;
 }
 #endif





More information about the llvm-commits mailing list