This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v2 1/2] Refactor sigcontextinfo.h
On 15/08/2019 08:59, Florian Weimer wrote:
> I did this:
>
> -#define GET_PC(ctx) ((intptr_t) (((ucontext_t *) \
> +#define GET_PC(ctx) (1 + (intptr_t) (((ucontext_t *) \
> (ctx))->uc_mcontext.gregs[REG_RIP]))
>
> And none of the existing x86-64 tests failed. 8-(
I would expect so, since GET_PC is only used internally on libSegFault and profiler.
Both will just dump bogus values and we currently don't have tests to check it .
>
> So I strongly urge you to add a test like the one below. I can commit
> it separately after your patch. I still need to verify that it builds
> on all architectures.
I will integrate your tests if you are ok with that, however it requires some
changes. I will double check it on some different architectures also.
>
> Thanks,
> Florian
>
>
> p
>
> diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
> index 1ab6bcbfc8..df960aa7b4 100644
> --- a/sysdeps/unix/sysv/linux/Makefile
> +++ b/sysdeps/unix/sysv/linux/Makefile
> @@ -55,7 +55,7 @@ tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
> test-errno-linux tst-memfd_create tst-mlock2 tst-pkey \
> tst-rlimit-infinity tst-ofdlocks tst-gettid tst-gettid-kill \
> tst-tgkill
> -tests-internal += tst-ofdlocks-compat
> +tests-internal += tst-ofdlocks-compat tst-sigcontextinfo-get_pc
>
> # Generate the list of SYS_* macros for the system calls (__NR_*
> # macros). The file syscall-names.list contains all possible system
> diff --git a/sysdeps/unix/sysv/linux/tst-sigcontextinfo-get_pc.c b/sysdeps/unix/sysv/linux/tst-sigcontextinfo-get_pc.c
> new file mode 100644
> index 0000000000..f16b30250e
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/tst-sigcontextinfo-get_pc.c
> @@ -0,0 +1,84 @@
> +/* Test that the GET_PC macro is consistent with the unwinder.
> + Copyright (C) 2019 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public License as
> + published by the Free Software Foundation; either version 2.1 of the
> + License, or (at your option) any later version.
> +
> + The GNU C Library 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
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; see the file COPYING.LIB. If
> + not, see <http://www.gnu.org/licenses/>. */
> +
> +/* This test searches for the value of the GET_PC macro in the
> + addresses obtained from the backtrace function. */
> +
> +#include <array_length.h>
> +#include <execinfo.h>
> +#include <inttypes.h>
> +#include <signal.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <support/check.h>
> +#include <support/xsignal.h>
> +
> +/* This file defines macros to access the content of the sigcontext element
> + passed up by the signal handler. */
> +#include <sigcontextinfo.h>
> +
> +#ifdef SA_SIGINFO
> +# define SIGCONTEXT siginfo_t *info, void *
> +#endif
We can assume SA_SIGINFO for Linux, currently the only system that does
not define is Hurd.
> +
> +static bool handler_called;
> +
> +static void
> +handler (int signal, SIGCONTEXT ctx)
> +{
> + TEST_COMPARE (signal, SIGUSR1);
> +
> + uintptr_t pc = GET_PC (ctx);
> + printf ("info: address in signal handler: 0x%" PRIxPTR "\n", pc);
> +
> + void *callstack[10];
> + int callstack_count = backtrace (callstack, array_length (callstack));
> + TEST_VERIFY_EXIT (callstack_count > 0);
> + TEST_VERIFY_EXIT (callstack_count <= array_length (callstack));
> + bool found = false;
> + for (int i = 0; i < callstack_count; ++i)
> + {
> + const char *marker;
> + if ((uintptr_t) callstack[i] == pc)
> + {
> + found = true;
> + marker = " *";
> + }
> + else
> + marker = "";
> + printf ("info: call stack entry %d: 0x%" PRIxPTR "%s\n",
> + i, (uintptr_t) callstack[i], marker);
> + }
> + TEST_VERIFY (found);
> + handler_called = true;
> +}
> +
> +static int
> +do_test (void)
> +{
> + struct sigaction sa =
> + {
> + .sa_sigaction = &handler,
We need a '.sa_flags = SA_SIGINFO' here.
> + };
> + xsigaction (SIGUSR1, &sa, NULL);
> + raise (SIGUSR1);
> + TEST_VERIFY (handler_called);
> + return 0;
> +}
> +
> +#include <support/test-driver.c>
>