bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Subject: sigsegv tests: Avoid a crash on NetBSD 10.0/i386
Date: Sun, 07 Apr 2024 16:59:46 +0200	[thread overview]
Message-ID: <13165582.Meur8eHHLF@nimes> (raw)

On NetBSD 10.0/i386, one of the 'sigsegv' tests crashes:

FAIL: test-sigsegv-catch-stackoverflow1
=======================================

FAIL test-sigsegv-catch-stackoverflow1 (exit status: 139)

More precisely, it crashes inside _rtld_bind, when
stackoverflow_handler_continuation invokes longjmp.

The crash is reproducible in a testdir of all gnulib modules,
but not in a testdir of the 'sigsegv' module alone.

I've found two possible workarounds:
  - Align the stack pointer before invoking longjmp.
  - Ensure that resolving the 'longjmp' symbol is already done
    in main(), through

      if (setjmp (mainloop) == 0)
        longjmp (mainloop, 1);

The former is the more proper fix. So, that is what I'm applying:


2024-04-07  Bruno Haible  <bruno@clisp.org>

	sigsegv tests: Avoid a crash on NetBSD 10.0/i386.
	* tests/test-sigsegv-catch-stackoverflow1.c
	(stackoverflow_handler_continuation): On NetBSD/i386, align the stack
	pointer before calling longjmp.

diff --git a/tests/test-sigsegv-catch-stackoverflow1.c b/tests/test-sigsegv-catch-stackoverflow1.c
index 20915adfd5..46b120686c 100644
--- a/tests/test-sigsegv-catch-stackoverflow1.c
+++ b/tests/test-sigsegv-catch-stackoverflow1.c
@@ -55,7 +55,26 @@ static volatile char *stack_upper_bound;
 static void
 stackoverflow_handler_continuation (void *arg1, void *arg2, void *arg3)
 {
+#if defined __NetBSD__ && defined __i386__
+  /* On NetBSD 10.0/i386, when built as part of a testdir-all (but not as part
+     of a testdir for just the module 'sigsegv'!) this program crashes.  The
+     cause is that:
+       - The alternate stack is not aligned (which is intentional, see
+         altstack-util.h) and NetBSD does not align the stack pointer while
+         switching to the alternate stack.
+       - When %esp is not aligned, the dynamic linker crashes in function
+         _rtld_bind while resolving the symbol 'longjmp'.
+     We would around this by aligning the stack pointer, to a multiple of 8.  */
+  int *argp;
+  __asm__ __volatile__ ("movl %1,%0" : "=r" (argp) : "r" (&arg1));
+  unsigned long sp;
+  __asm__ __volatile__ ("movl %%esp,%0" : "=r" (sp));
+  sp &= ~7UL;
+  __asm__ __volatile__ ("movl %0,%%esp" : : "r" (sp));
+  int arg = *argp;
+#else
   int arg = (int) (long) arg1;
+#endif
   longjmp (mainloop, arg);
 }
 





                 reply	other threads:[~2024-04-07 15:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

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

  git send-email \
    --in-reply-to=13165582.Meur8eHHLF@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.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).