BPF List
 help / color / mirror / Atom feed
* [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes
@ 2024-10-03 21:03 Eduard Zingerman
  2024-10-03 21:07 ` Eduard Zingerman
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Eduard Zingerman @ 2024-10-03 21:03 UTC (permalink / raw)
  To: bpf, ast
  Cc: andrii, daniel, martin.lau, kernel-team, yonghong.song,
	Eduard Zingerman, Tony Ambardar

test_progs uses glibc specific functions backtrace() and
backtrace_symbols_fd() to print backtrace in case of SIGSEGV.

Recent commit (see fixes) updated test_progs.c to define stub versions
of the same functions with attriubte "weak" in order to allow linking
test_progs against musl libc. Unfortunately this broke the backtrace
handling for glibc builds.

As it turns out, glibc defines backtrace() and backtrace_symbols_fd()
as weak:

  $ llvm-readelf --symbols /lib64/libc.so.6 \
     | grep -P '( backtrace_symbols_fd| backtrace)$'
  4910: 0000000000126b40   161 FUNC    WEAK   DEFAULT    16 backtrace
  6843: 0000000000126f90   852 FUNC    WEAK   DEFAULT    16 backtrace_symbols_fd

So does test_progs:

 $ llvm-readelf --symbols test_progs \
    | grep -P '( backtrace_symbols_fd| backtrace)$'
  2891: 00000000006ad190    15 FUNC    WEAK   DEFAULT    13 backtrace
 11215: 00000000006ad1a0    41 FUNC    WEAK   DEFAULT    13 backtrace_symbols_fd

In such situation dynamic linker is not obliged to favour glibc
implementation over the one defined in test_progs.

Compiling with the following simple modification to test_progs.c
demonstrates the issue:

  $ git diff
  ...
  \--- a/tools/testing/selftests/bpf/test_progs.c
  \+++ b/tools/testing/selftests/bpf/test_progs.c
  \@@ -1817,6 +1817,7 @@ int main(int argc, char **argv)
          if (err)
                  return err;

  +       *(int *)0xdeadbeef  = 42;
          err = cd_flavor_subdir(argv[0]);
          if (err)
                  return err;

  $ ./test_progs
  [0]: Caught signal #11!
  Stack trace:
  <backtrace not supported>
  Segmentation fault (core dumped)

Resolve this by hiding stub definitions behind __GLIBC__ macro check
instead of using "weak" attribute.

Fixes: c9a83e76b5a9 ("selftests/bpf: Fix compile if backtrace support missing in libc")

CC: Tony Ambardar <tony.ambardar@gmail.com>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
 tools/testing/selftests/bpf/test_progs.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 7846f7f98908..005ff506b527 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -20,20 +20,23 @@
 
 #include "network_helpers.h"
 
+/* backtrace() and backtrace_symbols_fd() are glibc specific,
+ * use header file when glibc is available and provide stub
+ * implementations when another libc implementation is used.
+ */
 #ifdef __GLIBC__
 #include <execinfo.h> /* backtrace */
-#endif
-
-/* Default backtrace funcs if missing at link */
-__weak int backtrace(void **buffer, int size)
+#else
+int backtrace(void **buffer, int size)
 {
 	return 0;
 }
 
-__weak void backtrace_symbols_fd(void *const *buffer, int size, int fd)
+void backtrace_symbols_fd(void *const *buffer, int size, int fd)
 {
 	dprintf(fd, "<backtrace not supported>\n");
 }
+#endif /*__GLIBC__ */
 
 int env_verbosity = 0;
 
-- 
2.46.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes
  2024-10-03 21:03 [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes Eduard Zingerman
@ 2024-10-03 21:07 ` Eduard Zingerman
  2024-10-06  8:12   ` Tony Ambardar
  2024-10-04 17:21 ` Daniel Xu
  2024-10-08  3:40 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 6+ messages in thread
From: Eduard Zingerman @ 2024-10-03 21:07 UTC (permalink / raw)
  To: Tony Ambardar
  Cc: andrii, daniel, martin.lau, kernel-team, yonghong.song, bpf,
	Alexei Starovoitov

On Thu, 2024-10-03 at 14:03 -0700, Eduard Zingerman wrote:

[...]

> Resolve this by hiding stub definitions behind __GLIBC__ macro check
> instead of using "weak" attribute.
> 
> Fixes: c9a83e76b5a9 ("selftests/bpf: Fix compile if backtrace support missing in libc")

Hi Tony,

could you please double-check if your musl setup behaves as expected
after these changes?

Thanks,
Eduard


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes
  2024-10-03 21:03 [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes Eduard Zingerman
  2024-10-03 21:07 ` Eduard Zingerman
@ 2024-10-04 17:21 ` Daniel Xu
  2024-10-08  3:40 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 6+ messages in thread
From: Daniel Xu @ 2024-10-04 17:21 UTC (permalink / raw)
  To: Eduard Zingerman
  Cc: bpf, ast, andrii, daniel, martin.lau, kernel-team, yonghong.song,
	Tony Ambardar

On Thu, Oct 03, 2024 at 02:03:07PM GMT, Eduard Zingerman wrote:
> test_progs uses glibc specific functions backtrace() and
> backtrace_symbols_fd() to print backtrace in case of SIGSEGV.
> 
> Recent commit (see fixes) updated test_progs.c to define stub versions
> of the same functions with attriubte "weak" in order to allow linking
> test_progs against musl libc. Unfortunately this broke the backtrace
> handling for glibc builds.
> 
> As it turns out, glibc defines backtrace() and backtrace_symbols_fd()
> as weak:
> 
>   $ llvm-readelf --symbols /lib64/libc.so.6 \
>      | grep -P '( backtrace_symbols_fd| backtrace)$'
>   4910: 0000000000126b40   161 FUNC    WEAK   DEFAULT    16 backtrace
>   6843: 0000000000126f90   852 FUNC    WEAK   DEFAULT    16 backtrace_symbols_fd
> 
> So does test_progs:
> 
>  $ llvm-readelf --symbols test_progs \
>     | grep -P '( backtrace_symbols_fd| backtrace)$'
>   2891: 00000000006ad190    15 FUNC    WEAK   DEFAULT    13 backtrace
>  11215: 00000000006ad1a0    41 FUNC    WEAK   DEFAULT    13 backtrace_symbols_fd
> 
> In such situation dynamic linker is not obliged to favour glibc
> implementation over the one defined in test_progs.
> 
> Compiling with the following simple modification to test_progs.c
> demonstrates the issue:
> 
>   $ git diff
>   ...
>   \--- a/tools/testing/selftests/bpf/test_progs.c
>   \+++ b/tools/testing/selftests/bpf/test_progs.c
>   \@@ -1817,6 +1817,7 @@ int main(int argc, char **argv)
>           if (err)
>                   return err;
> 
>   +       *(int *)0xdeadbeef  = 42;
>           err = cd_flavor_subdir(argv[0]);
>           if (err)
>                   return err;
> 
>   $ ./test_progs
>   [0]: Caught signal #11!
>   Stack trace:
>   <backtrace not supported>
>   Segmentation fault (core dumped)
> 
> Resolve this by hiding stub definitions behind __GLIBC__ macro check
> instead of using "weak" attribute.
> 
> Fixes: c9a83e76b5a9 ("selftests/bpf: Fix compile if backtrace support missing in libc")
> 
> CC: Tony Ambardar <tony.ambardar@gmail.com>
> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> ---
>  tools/testing/selftests/bpf/test_progs.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
> index 7846f7f98908..005ff506b527 100644
> --- a/tools/testing/selftests/bpf/test_progs.c
> +++ b/tools/testing/selftests/bpf/test_progs.c
> @@ -20,20 +20,23 @@
>  
>  #include "network_helpers.h"
>  
> +/* backtrace() and backtrace_symbols_fd() are glibc specific,
> + * use header file when glibc is available and provide stub
> + * implementations when another libc implementation is used.
> + */
>  #ifdef __GLIBC__
>  #include <execinfo.h> /* backtrace */
> -#endif
> -
> -/* Default backtrace funcs if missing at link */
> -__weak int backtrace(void **buffer, int size)
> +#else
> +int backtrace(void **buffer, int size)
>  {
>  	return 0;
>  }
>  
> -__weak void backtrace_symbols_fd(void *const *buffer, int size, int fd)
> +void backtrace_symbols_fd(void *const *buffer, int size, int fd)
>  {
>  	dprintf(fd, "<backtrace not supported>\n");
>  }
> +#endif /*__GLIBC__ */
>  
>  int env_verbosity = 0;
>  
> -- 
> 2.46.1
> 
> 

Acked-by: Daniel Xu <dxu@dxuuu.xyz>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes
  2024-10-03 21:07 ` Eduard Zingerman
@ 2024-10-06  8:12   ` Tony Ambardar
  2024-10-08  3:39     ` Andrii Nakryiko
  0 siblings, 1 reply; 6+ messages in thread
From: Tony Ambardar @ 2024-10-06  8:12 UTC (permalink / raw)
  To: Eduard Zingerman
  Cc: andrii, daniel, martin.lau, kernel-team, yonghong.song, bpf,
	Alexei Starovoitov

On Thu, Oct 03, 2024 at 02:07:23PM -0700, Eduard Zingerman wrote:
> On Thu, 2024-10-03 at 14:03 -0700, Eduard Zingerman wrote:
> 
> [...]
> 
> > Resolve this by hiding stub definitions behind __GLIBC__ macro check
> > instead of using "weak" attribute.
> > 
> > Fixes: c9a83e76b5a9 ("selftests/bpf: Fix compile if backtrace support missing in libc")
> 
> Hi Tony,
> 
> could you please double-check if your musl setup behaves as expected
> after these changes?
> 

Hi Eduard,

I discovered building for musl has broken over the last month or so, and
it took some time to find fixes and workarounds before I could retest.

Since glibc execinfo.h also defines its functions as weak, and given the
linking issues that can cause, I think changing the #ifdef as you did is
the right approach. But could you leave the fallback stub functions as
"__weak" like before to simplify overriding in the non-GLIBC case?

Otherwise:

Reviewed-by: Tony Ambardar <tony.ambardar@gmail.com>
Tested-by: Tony Ambardar <tony.ambardar@gmail.com>

Thanks,
Tony

> Thanks,
> Eduard
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes
  2024-10-06  8:12   ` Tony Ambardar
@ 2024-10-08  3:39     ` Andrii Nakryiko
  0 siblings, 0 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2024-10-08  3:39 UTC (permalink / raw)
  To: Tony Ambardar
  Cc: Eduard Zingerman, andrii, daniel, martin.lau, kernel-team,
	yonghong.song, bpf, Alexei Starovoitov

On Sun, Oct 6, 2024 at 1:12 AM Tony Ambardar <tony.ambardar@gmail.com> wrote:
>
> On Thu, Oct 03, 2024 at 02:07:23PM -0700, Eduard Zingerman wrote:
> > On Thu, 2024-10-03 at 14:03 -0700, Eduard Zingerman wrote:
> >
> > [...]
> >
> > > Resolve this by hiding stub definitions behind __GLIBC__ macro check
> > > instead of using "weak" attribute.
> > >
> > > Fixes: c9a83e76b5a9 ("selftests/bpf: Fix compile if backtrace support missing in libc")
> >
> > Hi Tony,
> >
> > could you please double-check if your musl setup behaves as expected
> > after these changes?
> >
>
> Hi Eduard,
>
> I discovered building for musl has broken over the last month or so, and
> it took some time to find fixes and workarounds before I could retest.
>
> Since glibc execinfo.h also defines its functions as weak, and given the
> linking issues that can cause, I think changing the #ifdef as you did is
> the right approach. But could you leave the fallback stub functions as
> "__weak" like before to simplify overriding in the non-GLIBC case?
>

I added __weak back while applying.

> Otherwise:
>
> Reviewed-by: Tony Ambardar <tony.ambardar@gmail.com>
> Tested-by: Tony Ambardar <tony.ambardar@gmail.com>
>
> Thanks,
> Tony
>
> > Thanks,
> > Eduard
> >

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes
  2024-10-03 21:03 [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes Eduard Zingerman
  2024-10-03 21:07 ` Eduard Zingerman
  2024-10-04 17:21 ` Daniel Xu
@ 2024-10-08  3:40 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-10-08  3:40 UTC (permalink / raw)
  To: Eduard Zingerman
  Cc: bpf, ast, andrii, daniel, martin.lau, kernel-team, yonghong.song,
	tony.ambardar

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu,  3 Oct 2024 14:03:07 -0700 you wrote:
> test_progs uses glibc specific functions backtrace() and
> backtrace_symbols_fd() to print backtrace in case of SIGSEGV.
> 
> Recent commit (see fixes) updated test_progs.c to define stub versions
> of the same functions with attriubte "weak" in order to allow linking
> test_progs against musl libc. Unfortunately this broke the backtrace
> handling for glibc builds.
> 
> [...]

Here is the summary with links:
  - [bpf] selftests/bpf: fix backtrace printing for selftests crashes
    https://git.kernel.org/bpf/bpf-next/c/5bf1557e3d6a

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-10-08  3:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-03 21:03 [PATCH bpf] selftests/bpf: fix backtrace printing for selftests crashes Eduard Zingerman
2024-10-03 21:07 ` Eduard Zingerman
2024-10-06  8:12   ` Tony Ambardar
2024-10-08  3:39     ` Andrii Nakryiko
2024-10-04 17:21 ` Daniel Xu
2024-10-08  3:40 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox