[compiler-rt] r200295 - [ASan] Move the signal handling-related flags to sanitizer_common.

Alexey Samsonov samsonov at google.com
Tue Jan 28 06:07:44 PST 2014


On Tue, Jan 28, 2014 at 1:28 PM, Alexander Potapenko <glider at google.com>wrote:

> Author: glider
> Date: Tue Jan 28 03:28:57 2014
> New Revision: 200295
>
> URL: http://llvm.org/viewvc/llvm-project?rev=200295&view=rev
> Log:
> [ASan] Move the signal handling-related flags to sanitizer_common.
> This change is a part of refactoring intended to have common signal
> handling behavior in all tools.
>
> Modified:
>     compiler-rt/trunk/lib/asan/CMakeLists.txt
>     compiler-rt/trunk/lib/asan/asan_flags.h
>     compiler-rt/trunk/lib/asan/asan_interceptors.cc
>     compiler-rt/trunk/lib/asan/asan_internal.h
>     compiler-rt/trunk/lib/asan/asan_linux.cc
>     compiler-rt/trunk/lib/asan/asan_mac.cc
>     compiler-rt/trunk/lib/asan/asan_posix.cc
>     compiler-rt/trunk/lib/asan/asan_rtl.cc
>     compiler-rt/trunk/lib/asan/asan_thread.cc
>     compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
>
> Modified: compiler-rt/trunk/lib/asan/CMakeLists.txt
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/CMakeLists.txt?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/CMakeLists.txt (original)
> +++ compiler-rt/trunk/lib/asan/CMakeLists.txt Tue Jan 28 03:28:57 2014
> @@ -37,18 +37,6 @@ endif()
>  set(ASAN_COMMON_DEFINITIONS
>    ASAN_HAS_EXCEPTIONS=1)
>
> -if(ANDROID)
> -  list(APPEND ASAN_COMMON_DEFINITIONS
> -    ASAN_NEEDS_SEGV=0
> -    ASAN_LOW_MEMORY=1)
> -elseif(MSVC)
> -  list(APPEND ASAN_COMMON_DEFINITIONS
> -    ASAN_NEEDS_SEGV=0)
> -else()
> -  list(APPEND ASAN_COMMON_DEFINITIONS
> -    ASAN_NEEDS_SEGV=1)
> -endif()
> -
>  # Architectures supported by ASan.
>  filter_available_targets(ASAN_SUPPORTED_ARCH
>    x86_64 i386 powerpc64)
>
> Modified: compiler-rt/trunk/lib/asan/asan_flags.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_flags.h?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_flags.h (original)
> +++ compiler-rt/trunk/lib/asan/asan_flags.h Tue Jan 28 03:28:57 2014
> @@ -70,12 +70,6 @@ struct Flags {
>    // terminating application. Useful for debug purposes (when one needs
>    // to attach gdb, for example).
>    int  sleep_before_dying;
> -  // If set, registers ASan custom segv handler.
> -  bool handle_segv;
> -  // If set, allows user register segv handler even if ASan registers one.
> -  bool allow_user_segv_handler;
> -  // If set, uses alternate stack for signal handling.
> -  bool use_sigaltstack;
>    // Allow the users to work around the bug in Nvidia drivers prior to
> 295.*.
>    bool check_malloc_usable_size;
>    // If set, explicitly unmaps (huge) shadow at exit.
>
> 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=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_interceptors.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_interceptors.cc Tue Jan 28 03:28:57
> 2014
> @@ -187,7 +187,8 @@ INTERCEPTOR(int, pthread_create, void *t
>
>  #if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
>  INTERCEPTOR(void*, signal, int signum, void *handler) {
> -  if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) {
> +  if (!AsanInterceptsSignal(signum) ||
> +      common_flags()->allow_user_segv_handler) {
>      return REAL(signal)(signum, handler);
>    }
>    return 0;
> @@ -195,7 +196,8 @@ INTERCEPTOR(void*, signal, int signum, v
>
>  INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act,
>                              struct sigaction *oldact) {
> -  if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) {
> +  if (!AsanInterceptsSignal(signum) ||
> +      common_flags()->allow_user_segv_handler) {
>      return REAL(sigaction)(signum, act, oldact);
>    }
>    return 0;
>
> Modified: compiler-rt/trunk/lib/asan/asan_internal.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_internal.h?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_internal.h (original)
> +++ compiler-rt/trunk/lib/asan/asan_internal.h Tue Jan 28 03:28:57 2014
> @@ -30,15 +30,6 @@
>
>  // Build-time configuration options.
>
> -// If set, asan will install its own SEGV signal handler.
> -#ifndef ASAN_NEEDS_SEGV
> -# if SANITIZER_ANDROID == 1
> -#  define ASAN_NEEDS_SEGV 0
> -# else
> -#  define ASAN_NEEDS_SEGV 1
> -# endif
> -#endif
> -
>  // If set, asan will intercept C++ exception api call(s).
>  #ifndef ASAN_HAS_EXCEPTIONS
>  # define ASAN_HAS_EXCEPTIONS 1
>
> Modified: compiler-rt/trunk/lib/asan/asan_linux.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_linux.cc?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_linux.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_linux.cc Tue Jan 28 03:28:57 2014
> @@ -99,7 +99,7 @@ void GetPcSpBp(void *context, uptr *pc,
>  }
>
>  bool AsanInterceptsSignal(int signum) {
> -  return signum == SIGSEGV && flags()->handle_segv;
> +  return signum == SIGSEGV && common_flags()->handle_segv;
>  }
>
>  void AsanPlatformThreadInit() {
>
> Modified: compiler-rt/trunk/lib/asan/asan_mac.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.cc?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_mac.cc Tue Jan 28 03:28:57 2014
> @@ -237,7 +237,8 @@ void *AsanDoesNotSupportStaticLinkage()
>  }
>
>  bool AsanInterceptsSignal(int signum) {
> -  return (signum == SIGSEGV || signum == SIGBUS) && flags()->handle_segv;
> +  return (signum == SIGSEGV || signum == SIGBUS) &&
> +         common_flags()->handle_segv;
>  }
>
>  void AsanPlatformThreadInit() {
>
> Modified: compiler-rt/trunk/lib/asan/asan_posix.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_posix.cc?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_posix.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_posix.cc Tue Jan 28 03:28:57 2014
> @@ -42,7 +42,7 @@ static void MaybeInstallSigaction(int si
>    REAL(memset)(&sigact, 0, sizeof(sigact));
>    sigact.sa_sigaction = handler;
>    sigact.sa_flags = SA_SIGINFO;
> -  if (flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
> +  if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
>    CHECK_EQ(0, REAL(sigaction)(signum, &sigact, 0));
>    VReport(1, "Installed the sigaction for signal %d\n", signum);
>  }
> @@ -87,7 +87,7 @@ void InstallSignalHandlers() {
>    // Set the alternate signal stack for the main thread.
>    // This will cause SetAlternateSignalStack to be called twice, but the
> stack
>    // will be actually set only once.
> -  if (flags()->use_sigaltstack) SetAlternateSignalStack();
> +  if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
>    MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
>    MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
>  }
>
> Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_rtl.cc Tue Jan 28 03:28:57 2014
> @@ -121,9 +121,6 @@ static void ParseFlagsFromString(Flags *
>    ParseFlag(str, &f->exitcode, "exitcode");
>    ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning");
>    ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying");
> -  ParseFlag(str, &f->handle_segv, "handle_segv");
> -  ParseFlag(str, &f->allow_user_segv_handler, "allow_user_segv_handler");
> -  ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack");
>    ParseFlag(str, &f->check_malloc_usable_size,
> "check_malloc_usable_size");
>    ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit");
>    ParseFlag(str, &f->abort_on_error, "abort_on_error");
> @@ -167,9 +164,6 @@ void InitializeFlags(Flags *f, const cha
>    f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
>    f->allow_user_poisoning = true;
>    f->sleep_before_dying = 0;
> -  f->handle_segv = ASAN_NEEDS_SEGV;
> -  f->allow_user_segv_handler = false;
> -  f->use_sigaltstack = false;
>    f->check_malloc_usable_size = true;
>    f->unmap_shadow_on_exit = false;
>    f->abort_on_error = false;
>
> Modified: compiler-rt/trunk/lib/asan/asan_thread.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.cc?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_thread.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_thread.cc Tue Jan 28 03:28:57 2014
> @@ -97,7 +97,7 @@ void AsanThread::Destroy() {
>    VReport(1, "T%d exited\n", tid);
>
>    malloc_storage().CommitBack();
> -  if (flags()->use_sigaltstack) UnsetAlternateSignalStack();
> +  if (common_flags()->use_sigaltstack) UnsetAlternateSignalStack();
>    asanThreadRegistry().FinishThread(tid);
>    FlushToDeadThreadStats(&stats_);
>    // We also clear the shadow on thread destruction because
> @@ -154,7 +154,7 @@ void AsanThread::Init() {
>  thread_return_t AsanThread::ThreadStart(uptr os_id) {
>    Init();
>    asanThreadRegistry().StartThread(tid(), os_id, 0);
> -  if (flags()->use_sigaltstack) SetAlternateSignalStack();
> +  if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
>
>    if (!start_routine_) {
>      // start_routine_ == 0 if we're on the main thread or on one of the
>
> 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=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt Tue Jan 28
> 03:28:57 2014
> @@ -73,6 +73,20 @@ set(SANITIZER_HEADERS
>    sanitizer_syscall_linux_x86_64.inc
>    sanitizer_thread_registry.h)
>
> +set(SANITIZER_COMMON_DEFINITIONS)
> +
> +if(ANDROID)
> +  list(APPEND SANITIZER_COMMON_DEFINITIONS
> +    SANITIZER_NEEDS_SEGV=0
> +    SANITIZER_LOW_MEMORY=1)
> +elseif(MSVC)
> +  list(APPEND SANITIZER_COMMON_DEFINITIONS
> +    SANITIZER_NEEDS_SEGV=0)
> +else()
> +  list(APPEND SANITIZER_COMMON_DEFINITIONS
> +    SANITIZER_NEEDS_SEGV=1)
> +endif()
> +
>  if (NOT MSVC)
>    set(SANITIZER_CFLAGS
>      ${SANITIZER_COMMON_CFLAGS}
> @@ -94,7 +108,8 @@ if(APPLE)
>      add_compiler_rt_darwin_object_library(RTSanitizerCommon ${os}
>        ARCH ${SANITIZER_COMMON_SUPPORTED_ARCH}
>        SOURCES ${SANITIZER_SOURCES} ${SANITIZER_LIBCDEP_SOURCES}
> -      CFLAGS ${SANITIZER_CFLAGS})
> +      CFLAGS ${SANITIZER_CFLAGS}
> +      DEFS ${SANITIZER_COMMON_DEFINITIONS})
>      list(APPEND SANITIZER_RUNTIME_LIBRARIES RTSanitizerCommon.${os})
>    endforeach()
>  elseif(ANDROID)
> @@ -102,18 +117,23 @@ elseif(ANDROID)
>      ${SANITIZER_SOURCES} ${SANITIZER_LIBCDEP_SOURCES})
>    set_target_compile_flags(RTSanitizerCommon.arm.android
>      ${SANITIZER_CFLAGS})
> +  set_property(TARGET RTSanitizerCommon.arm.android APPEND PROPERTY
> +    COMPILE_DEFINITIONS ${SANITIZER_COMMON_DEFINITIONS})
>    list(APPEND SANITIZER_RUNTIME_LIBRARIES RTSanitizerCommon.arm.android)
>  else()
>    # Otherwise, build separate libraries for each target.
>    foreach(arch ${SANITIZER_COMMON_SUPPORTED_ARCH})
>      add_compiler_rt_object_library(RTSanitizerCommon ${arch}
> -      SOURCES ${SANITIZER_SOURCES} CFLAGS ${SANITIZER_CFLAGS})
> +      SOURCES ${SANITIZER_SOURCES} CFLAGS ${SANITIZER_CFLAGS}
> +      DEFS ${SANITIZER_COMMON_DEFINITIONS})
>      add_compiler_rt_object_library(RTSanitizerCommonLibc ${arch}
> -      SOURCES ${SANITIZER_LIBCDEP_SOURCES} CFLAGS ${SANITIZER_CFLAGS})
> +      SOURCES ${SANITIZER_LIBCDEP_SOURCES} CFLAGS ${SANITIZER_CFLAGS}
> +      DEFS ${SANITIZER_COMMON_DEFINITIONS})
>      add_compiler_rt_static_runtime(clang_rt.san-${arch} ${arch}
>        SOURCES $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
>                $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
> -      CFLAGS ${SANITIZER_CFLAGS})
> +      CFLAGS ${SANITIZER_CFLAGS}
> +      DEFS ${SANITIZER_COMMON_DEFINITIONS})
>      list(APPEND SANITIZER_RUNTIME_LIBRARIES RTSanitizerCommon.${arch}
>                                              RTSanitizerCommonLibc.${arch})
>    endforeach()
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc Tue Jan 28
> 03:28:57 2014
> @@ -33,6 +33,10 @@ void SetCommonFlagsDefaults(CommonFlags
>    f->allocator_may_return_null = false;
>    f->print_summary = true;
>    f->check_printf = false;
> +  // TODO(glider): tools may want to set different defaults for
> handle_segv.
> +  f->handle_segv = SANITIZER_NEEDS_SEGV;
> +  f->allow_user_segv_handler = false;
> +  f->use_sigaltstack = false;
>  }
>
>  void ParseCommonFlagsFromString(CommonFlags *f, const char *str) {
> @@ -50,6 +54,9 @@ void ParseCommonFlagsFromString(CommonFl
>    ParseFlag(str, &f->allocator_may_return_null,
> "allocator_may_return_null");
>    ParseFlag(str, &f->print_summary, "print_summary");
>    ParseFlag(str, &f->check_printf, "check_printf");
> +  ParseFlag(str, &f->handle_segv, "handle_segv");
> +  ParseFlag(str, &f->allow_user_segv_handler, "allow_user_segv_handler");
> +  ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack");
>
>    // Do a sanity check for certain flags.
>    if (f->malloc_context_size < 1)
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h Tue Jan 28
> 03:28:57 2014
> @@ -58,6 +58,14 @@ struct CommonFlags {
>    bool print_summary;
>    // Check printf arguments.
>    bool check_printf;
> +  // If set, registers the tool's custom SEGV handler (both SIGBUS and
> SIGSEGV
> +  // on OSX).
> +  bool handle_segv;
> +  // If set, allows user to register a SEGV handler even if the tool
> registers
> +  // one.
> +  bool allow_user_segv_handler;
> +  // If set, uses alternate stack for signal handling.
> +  bool use_sigaltstack;
>  };
>
>  inline CommonFlags *common_flags() {
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h?rev=200295&r1=200294&r2=200295&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h Tue
> Jan 28 03:28:57 2014
> @@ -34,6 +34,15 @@
>  # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
>  #endif
>
> +// If set, the tool will install its own SEGV signal handler.
> +#ifndef SANITIZER_NEEDS_SEGV
> +# if SANITIZER_ANDROID == 1
> +#  define SANITIZER_NEEDS_SEGV 0
> +# else
> +#  define SANITIZER_NEEDS_SEGV 1
> +# endif
> +#endif
>

what about SANITIZER_LOW_MEMORY ?
Who provides a definition for it, and is ASAN_LOW_MEMORY replaced with it?


> +
>  // GCC does not understand __has_feature
>  #if !defined(__has_feature)
>  # define __has_feature(x) 0
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>



-- 
Alexey Samsonov, MSK
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140128/3e2b2df3/attachment.html>


More information about the llvm-commits mailing list