[compiler-rt] r319906 - [Sanitizers] Use SANITIZER_* macros in lib/interception

Kamil Rytarowski via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 6 09:02:00 PST 2017


Author: kamil
Date: Wed Dec  6 09:02:00 2017
New Revision: 319906

URL: http://llvm.org/viewvc/llvm-project?rev=319906&view=rev
Log:
[Sanitizers] Use SANITIZER_* macros in lib/interception

Summary:
Unlike the rest of the sanitizer code, lib/interception uses native macros like __linux__
to check for specific targets instead of the common ones like SANITIZER_LINUX.

When working on the Solaris port of the sanitizers, the current style was found to not
only be inconsistent, but clumsy to use because the canonical way to check for Solaris
is to check for __sun__ && __svr4__ which is a mouthful.

Therefore, this patch switches to use SANITIZER_* macros instead.

Tested on x86_64-pc-linux-gnu.

Reviewers: kcc, vitalybuka

Reviewed By: vitalybuka

Subscribers: #sanitizers, srhines, krytarowski, llvm-commits, fedor.sergeev

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D39798

Modified:
    compiler-rt/trunk/lib/interception/interception.h
    compiler-rt/trunk/lib/interception/interception_linux.cc
    compiler-rt/trunk/lib/interception/interception_linux.h
    compiler-rt/trunk/lib/interception/interception_mac.cc
    compiler-rt/trunk/lib/interception/interception_mac.h
    compiler-rt/trunk/lib/interception/interception_type_test.cc
    compiler-rt/trunk/lib/interception/interception_win.cc
    compiler-rt/trunk/lib/interception/interception_win.h

Modified: compiler-rt/trunk/lib/interception/interception.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception.h?rev=319906&r1=319905&r2=319906&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception.h (original)
+++ compiler-rt/trunk/lib/interception/interception.h Wed Dec  6 09:02:00 2017
@@ -15,13 +15,13 @@
 #ifndef INTERCEPTION_H
 #define INTERCEPTION_H
 
-#if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__APPLE__) && \
-    !defined(__NetBSD__) && !defined(_WIN32) && !defined(__Fuchsia__)
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+#if !SANITIZER_LINUX && !SANITIZER_FREEBSD && !SANITIZER_MAC && \
+    !SANITIZER_NETBSD && !SANITIZER_WINDOWS && !SANITIZER_FUCHSIA
 # error "Interception doesn't work on this operating system."
 #endif
 
-#include "sanitizer_common/sanitizer_internal_defs.h"
-
 // These typedefs should be used only in the interceptor definitions to replace
 // the standard system types (e.g. SSIZE_T instead of ssize_t)
 typedef __sanitizer::uptr    SIZE_T;
@@ -87,7 +87,7 @@ typedef __sanitizer::OFF64_T OFF64_T;
 // As it's decided at compile time which functions are to be intercepted on Mac,
 // INTERCEPT_FUNCTION() is effectively a no-op on this system.
 
-#if defined(__APPLE__)
+#if SANITIZER_MAC
 #include <sys/cdefs.h>  // For __DARWIN_ALIAS_C().
 
 // Just a pair of pointers.
@@ -121,7 +121,7 @@ const interpose_substitution substitutio
 # define INTERCEPTOR_ATTRIBUTE
 # define DECLARE_WRAPPER(ret_type, func, ...)
 
-#elif defined(_WIN32)
+#elif SANITIZER_WINDOWS
 # define WRAP(x) __asan_wrap_##x
 # define WRAPPER_NAME(x) "__asan_wrap_"#x
 # define INTERCEPTOR_ATTRIBUTE __declspec(dllexport)
@@ -129,7 +129,7 @@ const interpose_substitution substitutio
     extern "C" ret_type func(__VA_ARGS__);
 # define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \
     extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
-#elif defined(__FreeBSD__) || defined(__NetBSD__)
+#elif SANITIZER_FREEBSD || SANITIZER_NETBSD
 # define WRAP(x) __interceptor_ ## x
 # define WRAPPER_NAME(x) "__interceptor_" #x
 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
@@ -139,7 +139,7 @@ const interpose_substitution substitutio
 # define DECLARE_WRAPPER(ret_type, func, ...) \
      extern "C" ret_type func(__VA_ARGS__) \
      __attribute__((alias("__interceptor_" #func), visibility("default")));
-#elif !defined(__Fuchsia__)
+#elif !SANITIZER_FUCHSIA
 # define WRAP(x) __interceptor_ ## x
 # define WRAPPER_NAME(x) "__interceptor_" #x
 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
@@ -148,7 +148,7 @@ const interpose_substitution substitutio
     __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
 #endif
 
-#if defined(__Fuchsia__)
+#if SANITIZER_FUCHSIA
 // There is no general interception at all on Fuchsia.
 // Sanitizer runtimes just define functions directly to preempt them,
 // and have bespoke ways to access the underlying libc functions.
@@ -156,7 +156,7 @@ const interpose_substitution substitutio
 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
 # define REAL(x) __unsanitized_##x
 # define DECLARE_REAL(ret_type, func, ...)
-#elif !defined(__APPLE__)
+#elif !SANITIZER_MAC
 # define PTR_TO_REAL(x) real_##x
 # define REAL(x) __interception::PTR_TO_REAL(x)
 # define FUNC_TYPE(x) x##_f
@@ -167,14 +167,14 @@ const interpose_substitution substitutio
       extern FUNC_TYPE(func) PTR_TO_REAL(func); \
     }
 # define ASSIGN_REAL(dst, src) REAL(dst) = REAL(src)
-#else  // __APPLE__
+#else  // SANITIZER_MAC
 # define REAL(x) x
 # define DECLARE_REAL(ret_type, func, ...) \
     extern "C" ret_type func(__VA_ARGS__);
 # define ASSIGN_REAL(x, y)
-#endif  // __APPLE__
+#endif  // SANITIZER_MAC
 
-#if !defined(__Fuchsia__)
+#if !SANITIZER_FUCHSIA
 #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
   DECLARE_REAL(ret_type, func, __VA_ARGS__) \
   extern "C" ret_type WRAP(func)(__VA_ARGS__);
@@ -186,7 +186,7 @@ const interpose_substitution substitutio
 // macros does its job. In exceptional cases you may need to call REAL(foo)
 // without defining INTERCEPTOR(..., foo, ...). For example, if you override
 // foo with an interceptor for other function.
-#if !defined(__APPLE__) && !defined(__Fuchsia__)
+#if !SANITIZER_MAC && !SANITIZER_FUCHSIA
 # define DEFINE_REAL(ret_type, func, ...) \
     typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
     namespace __interception { \
@@ -196,7 +196,7 @@ const interpose_substitution substitutio
 # define DEFINE_REAL(ret_type, func, ...)
 #endif
 
-#if defined(__Fuchsia__)
+#if SANITIZER_FUCHSIA
 
 // We need to define the __interceptor_func name just to get
 // sanitizer_common/scripts/gen_dynamic_list.py to export func.
@@ -206,7 +206,7 @@ const interpose_substitution substitutio
       __interceptor_##func(__VA_ARGS__);                                \
   extern "C" INTERCEPTOR_ATTRIBUTE ret_type func(__VA_ARGS__)
 
-#elif !defined(__APPLE__)
+#elif !SANITIZER_MAC
 
 #define INTERCEPTOR(ret_type, func, ...) \
   DEFINE_REAL(ret_type, func, __VA_ARGS__) \
@@ -219,7 +219,7 @@ const interpose_substitution substitutio
 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
   INTERCEPTOR(ret_type, func, __VA_ARGS__)
 
-#else  // __APPLE__
+#else  // SANITIZER_MAC
 
 #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \
   extern "C" ret_type func(__VA_ARGS__) suffix; \
@@ -238,7 +238,7 @@ const interpose_substitution substitutio
   INTERPOSER_2(overridee, WRAP(overrider))
 #endif
 
-#if defined(_WIN32)
+#if SANITIZER_WINDOWS
 # define INTERCEPTOR_WINAPI(ret_type, func, ...) \
     typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
     namespace __interception { \
@@ -264,17 +264,17 @@ typedef unsigned long uptr;  // NOLINT
 
 #define INCLUDED_FROM_INTERCEPTION_LIB
 
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
+#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD
 # include "interception_linux.h"
 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
 # define INTERCEPT_FUNCTION_VER(func, symver) \
     INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver)
-#elif defined(__APPLE__)
+#elif SANITIZER_MAC
 # include "interception_mac.h"
 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
 # define INTERCEPT_FUNCTION_VER(func, symver) \
     INTERCEPT_FUNCTION_VER_MAC(func, symver)
-#elif defined(_WIN32)
+#elif SANITIZER_WINDOWS
 # include "interception_win.h"
 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
 # define INTERCEPT_FUNCTION_VER(func, symver) \

Modified: compiler-rt/trunk/lib/interception/interception_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_linux.cc?rev=319906&r1=319905&r2=319906&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_linux.cc (original)
+++ compiler-rt/trunk/lib/interception/interception_linux.cc Wed Dec  6 09:02:00 2017
@@ -12,19 +12,20 @@
 // Linux-specific interception methods.
 //===----------------------------------------------------------------------===//
 
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
 #include "interception.h"
 
+#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD
+
 #include <dlfcn.h>   // for dlsym() and dlvsym()
 
-#ifdef __NetBSD__
+#if SANITIZER_NETBSD
 #include "sanitizer_common/sanitizer_libc.h"
 #endif
 
 namespace __interception {
 bool GetRealFunctionAddress(const char *func_name, uptr *func_addr,
     uptr real, uptr wrapper) {
-#ifdef __NetBSD__
+#if SANITIZER_NETBSD
   // XXX: Find a better way to handle renames
   if (internal_strcmp(func_name, "sigaction") == 0) func_name = "__sigaction14";
 #endif
@@ -40,12 +41,12 @@ bool GetRealFunctionAddress(const char *
   return real == wrapper;
 }
 
-#if !defined(__ANDROID__)  // android does not have dlvsym
+#if !SANITIZER_ANDROID  // android does not have dlvsym
 void *GetFuncAddrVer(const char *func_name, const char *ver) {
   return dlvsym(RTLD_NEXT, func_name, ver);
 }
-#endif  // !defined(__ANDROID__)
+#endif  // !SANITIZER_ANDROID
 
 }  // namespace __interception
 
-#endif  // __linux__ || __FreeBSD__ || __NetBSD__
+#endif  // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD

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=319906&r1=319905&r2=319906&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_linux.h (original)
+++ compiler-rt/trunk/lib/interception/interception_linux.h Wed Dec  6 09:02:00 2017
@@ -12,7 +12,7 @@
 // Linux-specific interception methods.
 //===----------------------------------------------------------------------===//
 
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
+#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD
 
 #if !defined(INCLUDED_FROM_INTERCEPTION_LIB)
 # error "interception_linux.h should be included from interception library only"
@@ -34,14 +34,14 @@ void *GetFuncAddrVer(const char *func_na
       (::__interception::uptr) & (func),                                   \
       (::__interception::uptr) & WRAP(func))
 
-#if !defined(__ANDROID__)  // android does not have dlvsym
+#if !SANITIZER_ANDROID  // android does not have dlvsym
 #define INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver) \
   (::__interception::real_##func = (func##_f)(                \
        unsigned long)::__interception::GetFuncAddrVer(#func, symver))
 #else
 #define INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver) \
   INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
-#endif  // !defined(__ANDROID__)
+#endif  // !SANITIZER_ANDROID
 
 #endif  // INTERCEPTION_LINUX_H
-#endif  // __linux__ || __FreeBSD__ || __NetBSD__
+#endif  // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD

Modified: compiler-rt/trunk/lib/interception/interception_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_mac.cc?rev=319906&r1=319905&r2=319906&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_mac.cc (original)
+++ compiler-rt/trunk/lib/interception/interception_mac.cc Wed Dec  6 09:02:00 2017
@@ -12,9 +12,8 @@
 // Mac-specific interception methods.
 //===----------------------------------------------------------------------===//
 
-#ifdef __APPLE__
-
 #include "interception.h"
 
+#if SANITIZER_MAC
 
-#endif  // __APPLE__
+#endif  // SANITIZER_MAC

Modified: compiler-rt/trunk/lib/interception/interception_mac.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_mac.h?rev=319906&r1=319905&r2=319906&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_mac.h (original)
+++ compiler-rt/trunk/lib/interception/interception_mac.h Wed Dec  6 09:02:00 2017
@@ -12,7 +12,7 @@
 // Mac-specific interception methods.
 //===----------------------------------------------------------------------===//
 
-#ifdef __APPLE__
+#if SANITIZER_MAC
 
 #if !defined(INCLUDED_FROM_INTERCEPTION_LIB)
 # error "interception_mac.h should be included from interception.h only"
@@ -25,4 +25,4 @@
 #define INTERCEPT_FUNCTION_VER_MAC(func, symver)
 
 #endif  // INTERCEPTION_MAC_H
-#endif  // __APPLE__
+#endif  // SANITIZER_MAC

Modified: compiler-rt/trunk/lib/interception/interception_type_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_type_test.cc?rev=319906&r1=319905&r2=319906&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_type_test.cc (original)
+++ compiler-rt/trunk/lib/interception/interception_type_test.cc Wed Dec  6 09:02:00 2017
@@ -12,9 +12,10 @@
 // Compile-time tests of the internal type definitions.
 //===----------------------------------------------------------------------===//
 
-#if defined(__linux__) || defined(__APPLE__)
-
 #include "interception.h"
+
+#if SANITIZER_LINUX || SANITIZER_MAC
+
 #include <sys/types.h>
 #include <stddef.h>
 #include <stdint.h>
@@ -24,14 +25,14 @@ COMPILER_CHECK(sizeof(::SSIZE_T) == size
 COMPILER_CHECK(sizeof(::PTRDIFF_T) == sizeof(ptrdiff_t));
 COMPILER_CHECK(sizeof(::INTMAX_T) == sizeof(intmax_t));
 
-#ifndef __APPLE__
+#if !SANITIZER_MAC
 COMPILER_CHECK(sizeof(::OFF64_T) == sizeof(off64_t));
 #endif
 
 // The following are the cases when pread (and friends) is used instead of
 // pread64. In those cases we need OFF_T to match off_t. We don't care about the
 // rest (they depend on _FILE_OFFSET_BITS setting when building an application).
-# if defined(__ANDROID__) || !defined _FILE_OFFSET_BITS || \
+# if SANITIZER_ANDROID || !defined _FILE_OFFSET_BITS || \
   _FILE_OFFSET_BITS != 64
 COMPILER_CHECK(sizeof(::OFF_T) == sizeof(off_t));
 # endif

Modified: compiler-rt/trunk/lib/interception/interception_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_win.cc?rev=319906&r1=319905&r2=319906&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_win.cc (original)
+++ compiler-rt/trunk/lib/interception/interception_win.cc Wed Dec  6 09:02:00 2017
@@ -125,9 +125,9 @@
 //                                      addr2:  .bytes <body>
 //===----------------------------------------------------------------------===//
 
-#ifdef _WIN32
-
 #include "interception.h"
+
+#if SANITIZER_WINDOWS
 #include "sanitizer_common/sanitizer_platform.h"
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
@@ -1013,4 +1013,4 @@ bool OverrideImportedFunction(const char
 
 }  // namespace __interception
 
-#endif  // _WIN32
+#endif  // SANITIZER_MAC

Modified: compiler-rt/trunk/lib/interception/interception_win.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_win.h?rev=319906&r1=319905&r2=319906&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_win.h (original)
+++ compiler-rt/trunk/lib/interception/interception_win.h Wed Dec  6 09:02:00 2017
@@ -12,7 +12,7 @@
 // Windows-specific interception methods.
 //===----------------------------------------------------------------------===//
 
-#ifdef _WIN32
+#if SANITIZER_WINDOWS
 
 #if !defined(INCLUDED_FROM_INTERCEPTION_LIB)
 # error "interception_win.h should be included from interception library only"
@@ -81,4 +81,4 @@ void TestOnlyReleaseTrampolineRegions();
       (::__interception::uptr *)&REAL(func))
 
 #endif  // INTERCEPTION_WIN_H
-#endif  // _WIN32
+#endif  // SANITIZER_WINDOWS




More information about the llvm-commits mailing list