<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Dec 20, 2013 at 4:20 PM, Evgeniy Stepanov <span dir="ltr"><<a href="mailto:eugeni.stepanov@gmail.com" target="_blank">eugeni.stepanov@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: eugenis<br>
Date: Fri Dec 20 06:20:15 2013<br>
New Revision: 197806<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=197806&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=197806&view=rev</a><br>
Log:<br>
[msan] Wrap indirect calls to REAL(x) in interceptors.<br>
<br>
Added:<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h   (with props)<br>
Modified:<br>
    compiler-rt/trunk/lib/interception/interception_linux.h<br>
    compiler-rt/trunk/lib/msan/lit_tests/wrap_indirect_calls_in_rtl.cc<br>
    compiler-rt/trunk/lib/msan/msan_interceptors.cc<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc<br>
<br>
Modified: compiler-rt/trunk/lib/interception/interception_linux.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_linux.h?rev=197806&r1=197805&r2=197806&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_linux.h?rev=197806&r1=197805&r2=197806&view=diff</a><br>

==============================================================================<br>
--- compiler-rt/trunk/lib/interception/interception_linux.h (original)<br>
+++ compiler-rt/trunk/lib/interception/interception_linux.h Fri Dec 20 06:20:15 2013<br>
@@ -28,11 +28,11 @@ bool GetRealFunctionAddress(const char *<br>
 void *GetFuncAddrVer(const char *func_name, const char *ver);<br>
 }  // namespace __interception<br>
<br>
-#define INTERCEPT_FUNCTION_LINUX(func) \<br>
-    ::__interception::GetRealFunctionAddress( \<br>
-          #func, (::__interception::uptr*)&REAL(func), \<br>
-          (::__interception::uptr)&(func), \<br>
-          (::__interception::uptr)&WRAP(func))<br>
+#define INTERCEPT_FUNCTION_LINUX(func)                                     \<br>
+  ::__interception::GetRealFunctionAddress(                                \<br>
+      #func, (::__interception::uptr *)&__interception::PTR_TO_REAL(func), \<br>
+      (::__interception::uptr) & (func),                                   \<br>
+      (::__interception::uptr) & WRAP(func))<br>
<br>
 #if !defined(__ANDROID__)  // android does not have dlvsym<br>
 # define INTERCEPT_FUNCTION_VER_LINUX(func, symver) \<br>
<br>
Modified: compiler-rt/trunk/lib/msan/lit_tests/wrap_indirect_calls_in_rtl.cc<br>
URL: <a href="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" target="_blank">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</a><br>

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

==============================================================================<br>
--- compiler-rt/trunk/lib/msan/msan_interceptors.cc (original)<br>
+++ compiler-rt/trunk/lib/msan/msan_interceptors.cc Fri Dec 20 06:20:15 2013<br>
@@ -15,13 +15,13 @@<br>
 // sanitizer_common/sanitizer_common_interceptors.h<br>
 //===----------------------------------------------------------------------===//<br>
<br>
-#include "interception/interception.h"<br>
 #include "msan.h"<br>
 #include "sanitizer_common/sanitizer_platform_limits_posix.h"<br>
 #include "sanitizer_common/sanitizer_allocator.h"<br>
 #include "sanitizer_common/sanitizer_allocator_internal.h"<br>
 #include "sanitizer_common/sanitizer_atomic.h"<br>
 #include "sanitizer_common/sanitizer_common.h"<br>
+#include "sanitizer_common/sanitizer_interception.h"<br>
 #include "sanitizer_common/sanitizer_stackdepot.h"<br>
 #include "sanitizer_common/sanitizer_libc.h"<br>
 #include "sanitizer_common/sanitizer_linux.h"<br>
<br>
Added: compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h?rev=197806&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h?rev=197806&view=auto</a><br>

==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h (added)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h Fri Dec 20 06:20:15 2013<br>
@@ -0,0 +1,24 @@<br>
+//===-- sanitizer_interception.h --------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// zzz<br></blockquote><div><br></div><div><br></div><div>missing comment?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+//<br>
+//===----------------------------------------------------------------------===//<br>
+#ifndef SANITIZER_INTERCEPTION_H<br>
+#define SANITIZER_INTERCEPTION_H<br>
+<br>
+#include "interception/interception.h"<br>
+#include "sanitizer_common.h"<br>
+<br>
+#if SANITIZER_LINUX && !defined(SANITIZER_GO)<br>
+#undef REAL<br>
+#define REAL(x) IndirectExternCall(__interception::PTR_TO_REAL(x))<br>
+#endif<br>
+<br>
+#endif  // SANITIZER_INTERCEPTION_H<br>
<br>
Propchange: compiler-rt/trunk/lib/sanitizer_common/sanitizer_interception.h<br>
------------------------------------------------------------------------------<br>
    svn:eol-style = LF<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc?rev=197806&r1=197805&r2=197806&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc?rev=197806&r1=197805&r2=197806&view=diff</a><br>

==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc Fri Dec 20 06:20:15 2013<br>
@@ -357,6 +357,8 @@ uptr GetListOfModules(LoadedModule *modu<br>
 uptr indirect_call_wrapper;<br>
<br>
 void SetIndirectCallWrapper(uptr wrapper) {<br>
+  CHECK(!indirect_call_wrapper);<br>
+  CHECK(wrapper);<br>
   indirect_call_wrapper = wrapper;<br>
 }<br>
 #endif<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>