Skip to contentSkip to conversation

selftests/bpf: fix backtrace printing for selftests crashes

PATCH Applied Eduard Zingerman 5 participants 6 messages Oct 3, 2024 – Oct 8, 2024

Subsystems (3)
lore
Share & tools
lore

Eduard Zingerman

author maintainer
#0 lore raw

PATCH 1/1 selftests/bpf: fix backtrace printing for selftests crashes

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")
---
 tools/testing/selftests/bpf/test_progs.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/test_progs.c +8 -5
index 7846f7f98908..005ff506b527 100644
@@ -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;
 
Signature · 1 line
2.46.1
author maintainer
#1 lore raw

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

[...]

Quoted 4 lines

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

Acked-by on v1 1/1
#4 lore raw
In reply to Eduard Zingerman
Quoted 96 lines · Eduard Zingerman

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")
---
 tools/testing/selftests/bpf/test_progs.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/test_progs.c +8 -5
index 7846f7f98908..005ff506b527 100644
@@ -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;
 
Signature · 1 line
2.46.1
Reviewed-by Tested-by
#2 lore raw
In reply to Eduard Zingerman
Quoted 7 lines · Eduard Zingerman

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

[...]

Quoted 3 lines

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

Quoted 7 lines
Quoted 1 line
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:

Thanks,
Tony

Quoted 3 lines

Thanks,
Eduard

#3 lore raw
Quoted 19 lines · Tony Ambardar
Quoted 7 lines · Eduard Zingerman

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

[...]

Quoted 3 lines

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

Quoted 7 lines
Quoted 1 line
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,

Quoted 8 lines

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.

Quoted 11 lines

Otherwise:

Thanks,
Tony

Quoted 3 lines

Thanks,
Eduard

#5 lore raw
In reply to Eduard Zingerman
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 9 lines · 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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html