Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/gpl3/gcc/lib Add NetBSD LSan support



details:   https://anonhg.NetBSD.org/src/rev/e4172be6a9ea
branches:  trunk
changeset: 938546:e4172be6a9ea
user:      kamil <kamil%NetBSD.org@localhost>
date:      Fri Sep 11 01:05:28 2020 +0000

description:
Add NetBSD LSan support

Cherry-pick and adapt:

commit 983d7ddd0b278b45d815cbac9197205b39c4860a
Author: Kamil Rytarowski <n54%gmx.com@localhost>
Date:   Thu Jul 11 06:22:35 2019 +0000

    Add NetBSD LSan support

    Summary:
    Combine few relatively small changes into one:

     - implement internal_ptrace() and internal_clone() for NetBSD
     - add support for stoptheworld based on the ptrace(2) API
     - define COMPILER_RT_HAS_LSAN for NetBSD
     - enable tests for NetBSD/amd64

    Inspired by the original implementation by Christos Zoulas in netbsd/src for GCC.

    The implementation is in theory CPU independent through well defined macros
    across all NetBSD ports, however only the x86_64 version was tested.

    Reviewers: mgorny, dvyukov, vitalybuka, joerg, jfb

    Reviewed By: vitalybuka

    Subscribers: dexonsmith, jfb, srhines, kubamracek, llvm-commits, christos

    Tags: #llvm

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

    llvm-svn: 365735

diffstat:

 external/gpl3/gcc/dist/libsanitizer/lsan/lsan_common.h                                        |    6 +-
 external/gpl3/gcc/dist/libsanitizer/lsan/lsan_linux.cc                                        |    6 +-
 external/gpl3/gcc/dist/libsanitizer/sanitizer_common/Makefile.am                              |    2 +-
 external/gpl3/gcc/dist/libsanitizer/sanitizer_common/Makefile.in                              |    1 +
 external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_netbsd.cc                      |   22 +-
 external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_posix.h                        |    4 +
 external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cc |  356 ++++++++++
 external/gpl3/gcc/lib/Makefile.sanitizer                                                      |    3 +-
 8 files changed, 384 insertions(+), 16 deletions(-)

diffs (truncated from 506 to 300 lines):

diff -r dc53e00f12da -r e4172be6a9ea external/gpl3/gcc/dist/libsanitizer/lsan/lsan_common.h
--- a/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_common.h    Fri Sep 11 01:04:33 2020 +0000
+++ b/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_common.h    Fri Sep 11 01:05:28 2020 +0000
@@ -20,8 +20,8 @@
 #include "sanitizer_common/sanitizer_stoptheworld.h"
 #include "sanitizer_common/sanitizer_symbolizer.h"
 
-// LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) thus
-// supported for Linux only. Also, LSan doesn't like 32 bit architectures
+// LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) on Linux.
+// Also, LSan doesn't like 32 bit architectures
 // because of "small" (4 bytes) pointer size that leads to high false negative
 // ratio on large leaks. But we still want to have it for some 32 bit arches
 // (e.g. x86), see https://github.com/google/sanitizers/issues/403.
@@ -39,6 +39,8 @@
 #elif defined(__arm__) && \
     ((SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_NETBSD)
 #define CAN_SANITIZE_LEAKS 1
+#elif SANITIZER_NETBSD
+#define CAN_SANITIZE_LEAKS 1
 #else
 #define CAN_SANITIZE_LEAKS 0
 #endif
diff -r dc53e00f12da -r e4172be6a9ea external/gpl3/gcc/dist/libsanitizer/lsan/lsan_linux.cc
--- a/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_linux.cc    Fri Sep 11 01:04:33 2020 +0000
+++ b/external/gpl3/gcc/dist/libsanitizer/lsan/lsan_linux.cc    Fri Sep 11 01:05:28 2020 +0000
@@ -5,13 +5,13 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file is a part of LeakSanitizer. Linux-specific code.
+// This file is a part of LeakSanitizer. Linux/NetBSD-specific code.
 //
 //===----------------------------------------------------------------------===//
 
 #include "sanitizer_common/sanitizer_platform.h"
 
-#if SANITIZER_LINUX
+#if SANITIZER_LINUX || SANITIZER_NETBSD
 
 #include "lsan_allocator.h"
 
@@ -28,4 +28,4 @@
 
 } // namespace __lsan
 
-#endif // SANITIZER_LINUX
+#endif  // SANITIZER_LINUX || SANITIZER_NETBSD
diff -r dc53e00f12da -r e4172be6a9ea external/gpl3/gcc/dist/libsanitizer/sanitizer_common/Makefile.am
--- a/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/Makefile.am  Fri Sep 11 01:04:33 2020 +0000
+++ b/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/Makefile.am  Fri Sep 11 01:05:28 2020 +0000
@@ -66,6 +66,7 @@
        sanitizer_symbolizer_report.cc \
        sanitizer_stacktrace_printer.cc \
        sanitizer_stoptheworld_linux_libcdep.cc \
+       sanitizer_stoptheworld_netbsd_libcdep.cc \
        sanitizer_stoptheworld_mac.cc \
        sanitizer_suppressions.cc \
        sanitizer_symbolizer.cc \
@@ -127,4 +128,3 @@
 MAKEOVERRIDES=
 
 ## ################################################################
-
diff -r dc53e00f12da -r e4172be6a9ea external/gpl3/gcc/dist/libsanitizer/sanitizer_common/Makefile.in
--- a/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/Makefile.in  Fri Sep 11 01:04:33 2020 +0000
+++ b/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/Makefile.in  Fri Sep 11 01:05:28 2020 +0000
@@ -451,6 +451,7 @@
        sanitizer_symbolizer_report.cc \
        sanitizer_stacktrace_printer.cc \
        sanitizer_stoptheworld_linux_libcdep.cc \
+       sanitizer_stoptheworld_netbsd_libcdep.cc \
        sanitizer_stoptheworld_mac.cc \
        sanitizer_suppressions.cc \
        sanitizer_symbolizer.cc \
diff -r dc53e00f12da -r e4172be6a9ea external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_netbsd.cc
--- a/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_netbsd.cc  Fri Sep 11 01:04:33 2020 +0000
+++ b/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_netbsd.cc  Fri Sep 11 01:05:28 2020 +0000
@@ -239,10 +239,9 @@
   return _REAL(__clock_gettime50, clk_id, tp);
 }
 
-uptr internal_ptrace(int request, int pid, void *addr, void *data) {
-  Printf("internal_ptrace not implemented for NetBSD");
-  Die();
-  return 0;
+uptr internal_ptrace(int request, int pid, void *addr, int data) {
+  DEFINE__REAL(int, ptrace, int a, int b, void *c, int d);
+  return _REAL(ptrace, request, pid, addr, data);
 }
 
 uptr internal_waitpid(int pid, int *status, int options) {
@@ -316,11 +315,16 @@
   (void)_REAL(__sigemptyset14, set);
 }
 
-uptr intrnal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
-                   int *parent_tidptr, void *newtls, int *child_tidptr) {
-  Printf("internal_clone not implemented for NetBSD");
-  Die();
-  return 0;
+void internal_sigdelset(__sanitizer_sigset_t *set, int signo) {
+  DEFINE__REAL(int, __sigdelset14, const void *a, int b);
+  (void)_REAL(__sigdelset14, set, signo);
+}
+
+uptr internal_clone(int (*fn)(void *), void *child_stack, int flags,
+                    void *arg) {
+  DEFINE__REAL(int, clone, int (*a)(void *b), void *c, int d, void *e);
+
+  return _REAL(clone, fn, child_stack, flags, arg);
 }
 
 }  // namespace __sanitizer
diff -r dc53e00f12da -r e4172be6a9ea external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_posix.h
--- a/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_posix.h    Fri Sep 11 01:04:33 2020 +0000
+++ b/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_posix.h    Fri Sep 11 01:05:28 2020 +0000
@@ -52,7 +52,11 @@
 uptr internal_rename(const char *oldpath, const char *newpath);
 uptr internal_lseek(fd_t fd, OFF_T offset, int whence);
 
+#if SANITIZER_NETBSD
+uptr internal_ptrace(int request, int pid, void *addr, int data);
+#else
 uptr internal_ptrace(int request, int pid, void *addr, void *data);
+#endif
 uptr internal_waitpid(int pid, int *status, int options);
 
 int internal_fork();
diff -r dc53e00f12da -r e4172be6a9ea external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cc
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/external/gpl3/gcc/dist/libsanitizer/sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cc     Fri Sep 11 01:05:28 2020 +0000
@@ -0,0 +1,356 @@
+//===-- sanitizer_stoptheworld_netbsd_libcdep.cc --------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// See sanitizer_stoptheworld.h for details.
+// This implementation was inspired by Markus Gutschke's linuxthreads.cc.
+//
+// This is a NetBSD variation of Linux stoptheworld implementation
+// See sanitizer_stoptheworld_linux_libcdep.cc for code comments.
+//
+//===----------------------------------------------------------------------===//
+
+#include "sanitizer_platform.h"
+
+#if SANITIZER_NETBSD
+
+#include "sanitizer_stoptheworld.h"
+
+#include "sanitizer_atomic.h"
+#include "sanitizer_platform_limits_posix.h"
+
+#include <sys/types.h>
+
+#include <sys/ptrace.h>
+#include <sys/uio.h>
+#include <sys/wait.h>
+
+#include <machine/reg.h>
+
+#include <elf.h>
+#include <errno.h>
+#include <sched.h>
+#include <signal.h>
+#include <stddef.h>
+
+#define internal_sigaction_norestorer internal_sigaction
+
+#include "sanitizer_common.h"
+#include "sanitizer_flags.h"
+#include "sanitizer_libc.h"
+#include "sanitizer_linux.h"
+#include "sanitizer_mutex.h"
+#include "sanitizer_placement_new.h"
+
+namespace __sanitizer {
+
+class SuspendedThreadsListNetBSD : public SuspendedThreadsList {
+ public:
+  SuspendedThreadsListNetBSD() { thread_ids_.reserve(1024); }
+
+  tid_t GetThreadID(uptr index) const;
+  uptr ThreadCount() const;
+  bool ContainsTid(tid_t thread_id) const;
+  void Append(tid_t tid);
+
+  PtraceRegistersStatus GetRegistersAndSP(uptr index, uptr *buffer,
+                                          uptr *sp) const;
+  uptr RegisterCount() const;
+
+ private:
+  InternalMmapVector<tid_t> thread_ids_;
+};
+
+struct TracerThreadArgument {
+  StopTheWorldCallback callback;
+  void *callback_argument;
+  BlockingMutex mutex;
+  atomic_uintptr_t done;
+  uptr parent_pid;
+};
+
+class ThreadSuspender {
+ public:
+  explicit ThreadSuspender(pid_t pid, TracerThreadArgument *arg)
+      : arg(arg), pid_(pid) {
+    CHECK_GE(pid, 0);
+  }
+  bool SuspendAllThreads();
+  void ResumeAllThreads();
+  void KillAllThreads();
+  SuspendedThreadsListNetBSD &suspended_threads_list() {
+    return suspended_threads_list_;
+  }
+  TracerThreadArgument *arg;
+
+ private:
+  SuspendedThreadsListNetBSD suspended_threads_list_;
+  pid_t pid_;
+};
+
+void ThreadSuspender::ResumeAllThreads() {
+  int pterrno;
+  if (!internal_iserror(internal_ptrace(PT_DETACH, pid_, (void *)(uptr)1, 0),
+                        &pterrno)) {
+    VReport(2, "Detached from process %d.\n", pid_);
+  } else {
+    VReport(1, "Could not detach from process %d (errno %d).\n", pid_, pterrno);
+  }
+}
+
+void ThreadSuspender::KillAllThreads() {
+  internal_ptrace(PT_KILL, pid_, nullptr, 0);
+}
+
+bool ThreadSuspender::SuspendAllThreads() {
+  int pterrno;
+  if (internal_iserror(internal_ptrace(PT_ATTACH, pid_, nullptr, 0),
+                       &pterrno)) {
+    Printf("Could not attach to process %d (errno %d).\n", pid_, pterrno);
+    return false;
+  }
+
+  int status;
+  uptr waitpid_status;
+  HANDLE_EINTR(waitpid_status, internal_waitpid(pid_, &status, 0));
+
+  VReport(2, "Attached to process %d.\n", pid_);
+
+  struct ptrace_lwpinfo pl;
+  int val;
+  pl.pl_lwpid = 0;
+  while ((val = ptrace(PT_LWPINFO, pid_, (void *)&pl, sizeof(pl))) != -1 &&
+         pl.pl_lwpid != 0) {
+    suspended_threads_list_.Append(pl.pl_lwpid);
+    VReport(2, "Appended thread %d in process %d.\n", pl.pl_lwpid, pid_);
+  }
+  return true;
+}
+
+// Pointer to the ThreadSuspender instance for use in signal handler.
+static ThreadSuspender *thread_suspender_instance = nullptr;
+
+// Synchronous signals that should not be blocked.
+static const int kSyncSignals[] = {SIGABRT, SIGILL,  SIGFPE, SIGSEGV,
+                                   SIGBUS,  SIGXCPU, SIGXFSZ};
+
+static void TracerThreadDieCallback() {
+  ThreadSuspender *inst = thread_suspender_instance;
+  if (inst && stoptheworld_tracer_pid == internal_getpid()) {
+    inst->KillAllThreads();
+    thread_suspender_instance = nullptr;
+  }
+}
+
+// Signal handler to wake up suspended threads when the tracer thread dies.
+static void TracerThreadSignalHandler(int signum, __sanitizer_siginfo *siginfo,
+                                      void *uctx) {
+  SignalContext ctx(siginfo, uctx);
+  Printf("Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n", signum,
+         ctx.addr, ctx.pc, ctx.sp);
+  ThreadSuspender *inst = thread_suspender_instance;
+  if (inst) {
+    if (signum == SIGABRT)
+      inst->KillAllThreads();
+    else
+      inst->ResumeAllThreads();
+    RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback));
+    thread_suspender_instance = nullptr;
+    atomic_store(&inst->arg->done, 1, memory_order_relaxed);
+  }
+  internal__exit((signum == SIGABRT) ? 1 : 2);
+}
+



Home | Main Index | Thread Index | Old Index