[v2] Fix tsan warning: signal handler spoils errno

Message ID 20241210142136.10002-1-tdevries@suse.de
State Committed
Headers
Series [v2] Fix tsan warning: signal handler spoils errno |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Tom de Vries Dec. 10, 2024, 2:21 p.m. UTC
  When building gdb with -fsanitize=thread and running test-case
gdb.base/bg-exec-sigint-bp-cond.exp, I run into:
...
==================^M
WARNING: ThreadSanitizer: signal handler spoils errno (pid=25422)^M
    #0 handler_wrapper gdb/posix-hdep.c:66^M
    #1 decltype ({parm#2}({parm#3}...)) gdb::handle_eintr<>() \
         gdbsupport/eintr.h:67^M
    #2 gdb::waitpid(int, int*, int) gdbsupport/eintr.h:78^M
    #3 run_under_shell gdb/cli/cli-cmds.c:926^M
...

Likewise in:
- tui_sigwinch_handler with test-case gdb.python/tui-window.exp, and
- handle_sighup with test-case gdb.base/quit-live.exp.

Fix this by saving the original errno, and restoring it before returning [1].

Tested on x86_64-linux.

[1] https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html
---
 gdb/event-top.c   | 1 +
 gdb/posix-hdep.c  | 1 +
 gdb/tui/tui-win.c | 1 +
 3 files changed, 3 insertions(+)


base-commit: c79fb66c7837cbc3b5f3915d8c23bb5747235c34
  

Comments

Tom Tromey Dec. 13, 2024, 4:36 p.m. UTC | #1
>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> When building gdb with -fsanitize=thread and running test-case
Tom> gdb.base/bg-exec-sigint-bp-cond.exp, I run into:
Tom> ...
Tom> ==================^M
Tom> WARNING: ThreadSanitizer: signal handler spoils errno (pid=25422)^M
Tom>     #0 handler_wrapper gdb/posix-hdep.c:66^M
Tom>     #1 decltype ({parm#2}({parm#3}...)) gdb::handle_eintr<>() \
Tom>          gdbsupport/eintr.h:67^M
Tom>     #2 gdb::waitpid(int, int*, int) gdbsupport/eintr.h:78^M
Tom>     #3 run_under_shell gdb/cli/cli-cmds.c:926^M
Tom> ...

Looks good, thank you.
Approved-By: Tom Tromey <tom@tromey.com>

Tom
  

Patch

diff --git a/gdb/event-top.c b/gdb/event-top.c
index 45ad7b990fa..102304cb4ec 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -1480,6 +1480,7 @@  async_do_nothing (gdb_client_data arg)
 static void
 handle_sighup (int sig)
 {
+  scoped_restore restore_errno = make_scoped_restore (&errno);
   mark_async_signal_handler (sighup_token);
   signal (sig, handle_sighup);
 }
diff --git a/gdb/posix-hdep.c b/gdb/posix-hdep.c
index a0d5c585ebc..d7d17e6b2f4 100644
--- a/gdb/posix-hdep.c
+++ b/gdb/posix-hdep.c
@@ -64,6 +64,7 @@  static c_c_handler_ftype *current_handler;
 static void
 handler_wrapper (int num)
 {
+  scoped_restore restore_errno = make_scoped_restore (&errno);
   signal (num, handler_wrapper);
   if (current_handler != SIG_IGN)
     current_handler (num);
diff --git a/gdb/tui/tui-win.c b/gdb/tui/tui-win.c
index e9a8e4651ea..414d4699bea 100644
--- a/gdb/tui/tui-win.c
+++ b/gdb/tui/tui-win.c
@@ -527,6 +527,7 @@  static struct async_signal_handler *tui_sigwinch_token;
 static void
 tui_sigwinch_handler (int signal)
 {
+  scoped_restore restore_errno = make_scoped_restore (&errno);
   mark_async_signal_handler (tui_sigwinch_token);
   tui_set_win_resized_to (true);
 }