public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Bernd Edlinger <bernd.edlinger@hotmail.de>
To: Andrew Burgess <aburgess@redhat.com>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: Re: [PATCH v2] Fix signal unsafe call inside a signal
Date: Mon, 24 Jun 2024 04:59:53 +0000	[thread overview]
Message-ID: <AS8P193MB1285161EAB0FB5FBDCFB1CEDE4D42@AS8P193MB1285.EURP193.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <87v822491d.fsf@redhat.com>

On 6/21/24 17:20, Andrew Burgess wrote:> 
> Thank you for spotting and fixing this.
> 
> Bernd Edlinger <bernd.edlinger@hotmail.de> writes:
> 
>> As mentioned in
>> https://sourceware.org/bugzilla/show_bug.cgi?id=31713#c9
>> it can easily happen that the signal handler function
>> handle_fatal_signal() uses various signal unsafe functions.
>> Fix that by pre-computing the necessary language specific
>> strings.
>> ---
>>  gdb/bt-utils.c  | 22 +++++++++++--
>>  gdb/bt-utils.h  |  4 +++
>>  gdb/event-top.c | 83 ++++++++++++++++++++++++++++++++++++++++++++-----
>>  3 files changed, 99 insertions(+), 10 deletions(-)
>>
>> v2: moved initalization of language specific string to an init function
>>
>> diff --git a/gdb/bt-utils.c b/gdb/bt-utils.c
>> index f658ce0d4bc..5626662a9c1 100644
>> --- a/gdb/bt-utils.c
>> +++ b/gdb/bt-utils.c
>> @@ -125,6 +125,8 @@ gdb_internal_backtrace_1 ()
>>  
>>  /* See the comment on previous version of this function.  */
>>  
>> +static const char *str_backtrace_incomplete;
>> +
> 
> You've placed this string between the comment and the function that the
> comment applies too.
> 
>>  static void
>>  gdb_internal_backtrace_1 ()
>>  {
>> @@ -139,12 +141,26 @@ gdb_internal_backtrace_1 ()
>>  
>>    backtrace_symbols_fd (buffer, frames, gdb_stderr->fd ());
>>    if (frames == ARRAY_SIZE (buffer))
>> -    sig_write (_("Backtrace might be incomplete.\n"));
>> +    sig_write (str_backtrace_incomplete);
>>  }
>>  
>>  #else
>>  #error "unexpected internal backtrace policy"
>>  #endif
>> +
>> +static const char *str_backtrace;
>> +static const char *str_backtrace_unavailable;
>> +
>> +void
>> +init_str_internal_backtrace ()
> 
> Rather than placing this initialisation into an extern function and then
> calling it from event-top.c I think it would be better to place this
> initialisation into a function like:
> 
>   void _initialize_bt_utils ();
>   void
>   _initialize_bt_utils ()
>   {
>     ...
>   }
> 
> GDB will then take care of calling this automatically for you.  My
> reasoning is that this breaks the dependency between the signal handling
> setup and the backtrace utility function.
> 
Okay, good point. I tried that, but it turns out that the initialization
of these strings is still too late.  It needs to be after setlocale, and
before setting current_ui in main.c as this experiment shows:

diff --git a/gdb/main.c b/gdb/main.c
index 4dd68f3d976..def6bc31d0d 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -676,6 +676,7 @@ captured_main_1 (struct captured_main_args *context)
   /* Note: `error' cannot be called before this point, because the
      caller will crash when trying to print the exception.  */
   main_ui = new ui (stdin, stdout, stderr);
+  internal_error("foo");
   current_ui = main_ui;
 
   gdb_stdtarg = gdb_stderr;

result:
../../binutils-gdb/gdb/main.c:679: internal-error: foo
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Aborted

while

diff --git a/gdb/main.c b/gdb/main.c
index 4dd68f3d976..999782e92bc 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -677,6 +677,7 @@ captured_main_1 (struct captured_main_args *context)
      caller will crash when trying to print the exception.  */
   main_ui = new ui (stdin, stdout, stderr);
   current_ui = main_ui;
+  internal_error("foo");
 
   gdb_stdtarg = gdb_stderr;
   gdb_stdtargin = gdb_stdin;

results in a sigfault:
../../binutils-gdb/gdb/main.c:680: internal-error: foo
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Segmentation fault

instead of the expected Backtrace, because initialize_all_files
is not yet called at this point, therefore I need an explicit
initialization before this line in main.c.


Thanks
Bernd.

      reply	other threads:[~2024-06-24  4:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-21 10:54 Bernd Edlinger
2024-06-21 15:20 ` Andrew Burgess
2024-06-24  4:59   ` Bernd Edlinger [this message]

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=AS8P193MB1285161EAB0FB5FBDCFB1CEDE4D42@AS8P193MB1285.EURP193.PROD.OUTLOOK.COM \
    --to=bernd.edlinger@hotmail.de \
    --cc=aburgess@redhat.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).