Module Name:    src
Committed By:   kamil
Date:           Fri Sep 11 01:04:34 UTC 2020

Modified Files:
        src/external/gpl3/gcc/dist/libsanitizer/asan: asan_interceptors.h
        src/external/gpl3/gcc/dist/libsanitizer/lsan: lsan_interceptors.cc
        src/external/gpl3/gcc/dist/libsanitizer/sanitizer_common:
            sanitizer_platform_interceptors.h

Log Message:
Stop tracking atexit/__cxa_atexit/pthread_atfork allocations in LSan/NetBSD

Cherry-pick and adapt:

commit 8827047551570b7ed7088765c3de2a8cce6823b8
Author: Kamil Rytarowski <[email protected]>
Date:   Sat Sep 21 07:30:42 2019 +0000

    Stop tracking atexit/__cxa_atexit/pthread_atfork allocations in LSan/NetBSD

    Summary:
    The atexit(3) and __cxa_atexit() calls allocate internally memory and free 
on exit,
    after executing all callback. This causes false positives as DoLeakCheck() 
is called
    from the atexit handler. In the LSan/ASan tests there are strict checks 
triggering
    false positives here.

    Intercept all atexit(3) and __cxa_atexit() calls and disable LSan when 
calling the
    real functions.

    Stop tracing allocations in pthread_atfork(3) funtions, as there are 
performed
    internal allocations that are not freed for the time of running 
StopTheWorld()
    code. This avoids false-positives.

    The same changes have to be replicated in the ASan and LSan runtime.

    Non-NetBSD OSs are not tested and this code is restricted to NetBSD only.

    Reviewers: dvyukov, joerg, mgorny, vitalybuka, eugenis

    Reviewed By: vitalybuka

    Subscribers: jfb, llvm-commits, #sanitizers

    Tags: #sanitizers, #llvm

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

    llvm-svn: 372459


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 \
    src/external/gpl3/gcc/dist/libsanitizer/asan/asan_interceptors.h
cvs rdiff -u -r1.6 -r1.7 \
    src/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_interceptors.cc
cvs rdiff -u -r1.8 -r1.9 \
    
src/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_platform_interceptors.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/external/gpl3/gcc/dist/libsanitizer/asan/asan_interceptors.h
diff -u src/external/gpl3/gcc/dist/libsanitizer/asan/asan_interceptors.h:1.6 src/external/gpl3/gcc/dist/libsanitizer/asan/asan_interceptors.h:1.7
--- src/external/gpl3/gcc/dist/libsanitizer/asan/asan_interceptors.h:1.6	Sat Sep  5 09:12:31 2020
+++ src/external/gpl3/gcc/dist/libsanitizer/asan/asan_interceptors.h	Fri Sep 11 01:04:33 2020
@@ -103,12 +103,24 @@ void InitializePlatformInterceptors();
 # define ASAN_INTERCEPT___CXA_ATEXIT 0
 #endif
 
+#if SANITIZER_NETBSD
+# define ASAN_INTERCEPT_ATEXIT 1
+#else
+# define ASAN_INTERCEPT_ATEXIT 0
+#endif
+
 #if SANITIZER_LINUX && !SANITIZER_ANDROID
 # define ASAN_INTERCEPT___STRDUP 1
 #else
 # define ASAN_INTERCEPT___STRDUP 0
 #endif
 
+#if SANITIZER_NETBSD
+# define ASAN_INTERCEPT_PTHREAD_ATFORK 1
+#else
+# define ASAN_INTERCEPT_PTHREAD_ATFORK 0
+#endif
+
 DECLARE_REAL(int, memcmp, const void *a1, const void *a2, uptr size)
 DECLARE_REAL(char*, strchr, const char *str, int c)
 DECLARE_REAL(SIZE_T, strlen, const char *s)

Index: src/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_interceptors.cc
diff -u src/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_interceptors.cc:1.6 src/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_interceptors.cc:1.7
--- src/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_interceptors.cc:1.6	Fri Sep 11 01:03:31 2020
+++ src/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_interceptors.cc	Fri Sep 11 01:04:34 2020
@@ -348,6 +348,44 @@ INTERCEPTOR(char *, strerror, int errnum
 #define LSAN_MAYBE_INTERCEPT_STRERROR
 #endif
 
+#if SANITIZER_INTERCEPT___CXA_ATEXIT
+INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
+            void *dso_handle) {
+  __lsan::ScopedInterceptorDisabler disabler;
+  return REAL(__cxa_atexit)(func, arg, dso_handle);
+}
+#define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT INTERCEPT_FUNCTION(__cxa_atexit)
+#else
+#define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT
+#endif
+
+#if SANITIZER_INTERCEPT_ATEXIT
+INTERCEPTOR(int, atexit, void (*f)()) {
+  __lsan::ScopedInterceptorDisabler disabler;
+  return REAL(__cxa_atexit)((void (*)(void *a))f, 0, 0);
+}
+#define LSAN_MAYBE_INTERCEPT_ATEXIT INTERCEPT_FUNCTION(atexit)
+#else
+#define LSAN_MAYBE_INTERCEPT_ATEXIT
+#endif
+
+#if SANITIZER_INTERCEPT_PTHREAD_ATFORK
+extern "C" {
+extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
+                           void (*child)());
+};
+
+INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
+            void (*child)()) {
+  __lsan::ScopedInterceptorDisabler disabler;
+  // REAL(pthread_atfork) cannot be called due to symbol indirections at least on NetBSD
+  return _pthread_atfork(prepare, parent, child);
+}
+#define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK INTERCEPT_FUNCTION(pthread_atfork)
+#else
+#define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
+#endif
+
 struct ThreadParam {
   void *(*callback)(void *arg);
   void *param;
@@ -459,6 +497,10 @@ void InitializeInterceptors() {
 
   LSAN_MAYBE_INTERCEPT_STRERROR;
 
+  LSAN_MAYBE_INTERCEPT___CXA_ATEXIT;
+  LSAN_MAYBE_INTERCEPT_ATEXIT;
+  LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK;
+
 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
   if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
     Report("LeakSanitizer: failed to create thread key.\n");

Index: src/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_platform_interceptors.h
diff -u src/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_platform_interceptors.h:1.8 src/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_platform_interceptors.h:1.9
--- src/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_platform_interceptors.h:1.8	Sat Sep  5 09:12:32 2020
+++ src/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_platform_interceptors.h	Fri Sep 11 01:04:34 2020
@@ -508,5 +508,8 @@
 #define SANITIZER_INTERCEPT_TTYENT SI_NETBSD
 #define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD
 #define SANITIZER_INTERCEPT_NETENT SI_NETBSD
+#define SANITIZER_INTERCEPT___CXA_ATEXIT SI_NETBSD
+#define SANITIZER_INTERCEPT_ATEXIT SI_NETBSD
+#define SANITIZER_INTERCEPT_PTHREAD_ATFORK SI_NETBSD
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

Reply via email to