selftests/bpf: fix backtrace printing for selftests crashes
Subsystems (3)
Eduard Zingerman
maintainerPATCH 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_fdSo 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_fdIn 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.
--- tools/testing/selftests/bpf/test_progs.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/test_progs.c +8 -5
@@ -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
Eduard Zingerman
maintainerOn 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
Quoted 96 lines · Eduard Zingerman
test_progsuses glibc specific functionsbacktrace()andbacktrace_symbols_fd()to print backtrace in case of SIGSEGV.Recent commit (see fixes) updated
test_progs.cto define stub versions of the same functions with attriubte "weak" in order to allow linkingtest_progsagainst musl libc. Unfortunately this broke the backtrace handling for glibc builds.As it turns out, glibc defines
backtrace()andbacktrace_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_fdSo 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_fdIn 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.cdemonstrates 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 <[email protected]>Signed-off-by Eduard Zingerman <[email protected]>--- tools/testing/selftests/bpf/test_progs.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)tools/testing/selftests/bpf/test_progs.c +8
-5@@ -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
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
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.halso 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:
Reviewed-by Tony Ambardar <[email protected]>Tested-by Tony Ambardar <[email protected]>Thanks,
TonyQuoted 3 lines
Thanks,
Eduard
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_progsuses glibc specific functionsbacktrace()andbacktrace_symbols_fd()to print backtrace in case of SIGSEGV.Recent commit (see fixes) updated
test_progs.cto define stub versions of the same functions with attriubte "weak" in order to allow linkingtest_progsagainst 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