public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <andrew.burgess@embecosm.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/5] gdb: register signal handler after setting up event token
Date: Fri,  2 Jul 2021 12:06:05 +0100	[thread overview]
Message-ID: <335a4ee41d39a83a033f1ab5ad2fc0edd0825dba.1625223527.git.andrew.burgess@embecosm.com> (raw)
In-Reply-To: <cover.1625223527.git.andrew.burgess@embecosm.com>

This commit fixes the smallest of small possible bug related to signal
handling.  If we look in async_init_signals we see code like this:

  signal (SIGQUIT, handle_sigquit);
  sigquit_token =
    create_async_signal_handler (async_do_nothing, NULL, "sigquit");

Then if we look in handle_sigquit we see code like this:

  mark_async_signal_handler (sigquit_token);
  signal (sig, handle_sigquit);

Finally, in mark_async_signal_handler we have:

  async_handler_ptr->ready = 1;

Where async_handler_ptr will be sigquit_token.

What this means is that if a SIGQUIT arrive in async_init_signals
after handle_sigquit has been registered, but before sigquit_token has
been initialised, then GDB will most likely crash.

The chance of this happening is tiny, but fixing this is trivial, just
ensure we call create_async_signal_handler before calling signal, so
lets do that.

There are no tests for this.  Trying to land a signal in the right
spot is pretty hit and miss.  I did try changing the current HEAD GDB
like this:

  signal (SIGQUIT, handle_sigquit);
  raise (SIGQUIT);
  sigquit_token =
    create_async_signal_handler (async_do_nothing, NULL, "sigquit");

And confirmed that this did result in a crash, after my change I tried
this:

  sigquit_token =
    create_async_signal_handler (async_do_nothing, NULL, "sigquit");
  signal (SIGQUIT, handle_sigquit);
  raise (SIGQUIT);

And GDB now starts up just fine.

gdb/ChangeLog:

	* event-top.c (async_init_signals): For each signal, call signal
	only after calling create_async_signal_handler.
---
 gdb/ChangeLog   | 5 +++++
 gdb/event-top.c | 8 +++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/gdb/event-top.c b/gdb/event-top.c
index ab5179b7d32..2d3bfa6a9c9 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -918,12 +918,13 @@ async_init_signals (void)
 
   quit_serial_event = make_serial_event ();
 
-  signal (SIGINT, handle_sigint);
   sigint_token =
     create_async_signal_handler (async_request_quit, NULL, "sigint");
-  signal (SIGTERM, handle_sigterm);
+  signal (SIGINT, handle_sigint);
+
   async_sigterm_token
     = create_async_signal_handler (async_sigterm_handler, NULL, "sigterm");
+  signal (SIGTERM, handle_sigterm);
 
   /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
      to the inferior and breakpoints will be ignored.  */
@@ -940,10 +941,11 @@ async_init_signals (void)
      might be in memory, shared between the two).  Since we establish
      a handler for SIGQUIT, when we call exec it will set the signal
      to SIG_DFL for us.  */
-  signal (SIGQUIT, handle_sigquit);
   sigquit_token =
     create_async_signal_handler (async_do_nothing, NULL, "sigquit");
+  signal (SIGQUIT, handle_sigquit);
 #endif
+
 #ifdef SIGHUP
   if (signal (SIGHUP, handle_sighup) != SIG_IGN)
     sighup_token =
-- 
2.25.4


  parent reply	other threads:[~2021-07-02 11:06 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-02 11:06 [PATCH 0/5] GDB Synchronous Signal Handling Andrew Burgess
2021-07-02 11:06 ` [PATCH 1/5] gdb: terminate upon receipt of SIGFPE Andrew Burgess
2021-07-02 12:09   ` Eli Zaretskii
2021-07-02 18:11     ` Tom Tromey
2021-07-02 22:51       ` Pedro Alves
2021-07-03  6:14         ` Eli Zaretskii
2021-07-03 18:02           ` Pedro Alves
2021-07-03 18:23             ` Eli Zaretskii
2021-07-03 22:52               ` Pedro Alves
2021-07-04  4:27                 ` Eli Zaretskii
2021-07-04 14:51                   ` Pedro Alves
2021-07-04 16:31                     ` Eli Zaretskii
2021-07-03 22:58   ` Pedro Alves
2021-07-02 11:06 ` Andrew Burgess [this message]
2021-07-03 23:02   ` [PATCH 2/5] gdb: register signal handler after setting up event token Pedro Alves
2021-07-02 11:06 ` [PATCH 3/5] gdb: rewrite header comment on async_init_signals Andrew Burgess
2021-07-03 23:23   ` Pedro Alves
2021-07-02 11:06 ` [PATCH 4/5] gdb: print backtrace on fatal SIGSEGV Andrew Burgess
2021-07-02 11:47   ` Eli Zaretskii
2021-07-04  0:55     ` Pedro Alves
2021-07-04  4:32       ` Eli Zaretskii
2021-07-04 14:32         ` Pedro Alves
2021-07-04 14:38           ` Eli Zaretskii
2021-07-04 15:03             ` Pedro Alves
2021-07-04 16:34               ` Eli Zaretskii
2021-07-04  0:51   ` Pedro Alves
2021-07-04  0:53   ` Pedro Alves
2021-07-02 11:06 ` [PATCH 5/5] gdb: register SIGBUS, SIGFPE, and SIGABRT handlers Andrew Burgess
2021-07-04  0:58   ` Pedro Alves
2021-07-21 18:08 ` [PATCHv2 0/6] GDB Synchronous Signal Handling Andrew Burgess
2021-07-21 18:08   ` [PATCHv2 1/6] gdb: terminate upon receipt of SIGFPE Andrew Burgess
2021-07-21 18:08   ` [PATCHv2 2/6] gdb: register signal handler after setting up event token Andrew Burgess
2021-07-21 18:08   ` [PATCHv2 3/6] gdb: rename async_init_signals to gdb_init_signals Andrew Burgess
2021-07-21 18:08   ` [PATCHv2 4/6] gdb: print backtrace on fatal SIGSEGV Andrew Burgess
2021-08-10 18:53     ` Pedro Alves
2021-07-21 18:08   ` [PATCHv2 5/6] gdb: register SIGBUS, SIGFPE, and SIGABRT handlers Andrew Burgess
2021-07-21 18:08   ` [PATCHv2 6/6] gdb: don't print backtrace when dumping core after an internal error Andrew Burgess
2021-07-27 18:54   ` [PATCHv2 0/6] GDB Synchronous Signal Handling Tom Tromey
2021-08-10  9:33   ` Andrew Burgess
2021-08-10 18:56     ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=335a4ee41d39a83a033f1ab5ad2fc0edd0825dba.1625223527.git.andrew.burgess@embecosm.com \
    --to=andrew.burgess@embecosm.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).