binutils-gdb/gdbsupport/block-signals.h
Tom de Vries 6418644b0d [gdb] Block SIGTERM in worker threads
With gdb build with gcc-12 and -fsanitize=thread, and test-case
gdb.base/gdb-sigterm.exp, I run into:
...
WARNING: ThreadSanitizer: data race (pid=9722)^M
  Write of size 4 at 0x00000325bc68 by thread T1:^M
  #0 handle_sigterm(int) src/gdb/event-top.c:1211 (gdb+0x8ec01f)^M
  ...
  Previous read of size 4 at 0x00000325bc68 by main thread:^M
    [failed to restore the stack]^M
^M
  Location is global 'sync_quit_force_run' of size 4 at \
  0x00000325bc68 (gdb+0x325bc68)^M
  ...
SUMMARY: ThreadSanitizer: data race gdb/event-top.c:1211 in \
  handle_sigterm(int)^M
...
and 3 more data races involving handle_sigterm and locations:
- active_ext_lang
- quit_flag
- heap block of size 40
  (XNEW (async_signal_handler) in create_async_signal_handler)

This was reported in PR29297.

The testcase executes a "kill -TERM $gdb_pid", which generates a
process-directed signal.

A process-directed signal can be delivered to any thread, and what we see
here is the fallout of the signal being delivered to a worker thread rather
than the main thread.

Fix this by blocking SIGTERM in the worker threads.

[ I have not been able to reproduce this after it occurred for the first time,
so unfortunately I cannot confirm that the patch fixes the problem. ]

Tested on x86_64-linux, with and without -fsanitize=thread.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29297
2022-06-30 13:31:06 +02:00

69 lines
1.7 KiB
C++

/* Block signals used by gdb
Copyright (C) 2019-2022 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef GDBSUPPORT_BLOCK_SIGNALS_H
#define GDBSUPPORT_BLOCK_SIGNALS_H
#include <signal.h>
#include "gdbsupport/gdb-sigmask.h"
namespace gdb
{
/* This is an RAII class that temporarily blocks the signals needed by
gdb. This can be used before starting a new thread to ensure that
this thread starts with the appropriate signals blocked. */
class block_signals
{
public:
block_signals ()
{
#ifdef HAVE_SIGPROCMASK
sigset_t mask;
sigemptyset (&mask);
sigaddset (&mask, SIGINT);
sigaddset (&mask, SIGCHLD);
sigaddset (&mask, SIGALRM);
sigaddset (&mask, SIGWINCH);
sigaddset (&mask, SIGTERM);
gdb_sigmask (SIG_BLOCK, &mask, &m_old_mask);
#endif
}
~block_signals ()
{
#ifdef HAVE_SIGPROCMASK
gdb_sigmask (SIG_SETMASK, &m_old_mask, nullptr);
#endif
}
DISABLE_COPY_AND_ASSIGN (block_signals);
private:
#ifdef HAVE_SIGPROCMASK
sigset_t m_old_mask;
#endif
};
}
#endif /* GDBSUPPORT_BLOCK_SIGNALS_H */