Skip to contentSkip to conversation

KVM: selftests: Guard execinfo.h inclusion for non-glibc builds

PATCH Superseded Hisam Mehboob 3 participants 9 messages Mar 19 – May 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: selftests: Guard execinfo.h inclusion for non-glibc builds

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Guard the inclusion of execinfo.h under #ifdef __GLIBC__, and wrap all backtrace() usage under the same guard with a fallback message for non-glibc builds indicating that stack traces are not available.

Unlike the approach of adding a weak stub for backtrace(), this explicitly handles the non-glibc case rather than silently providing an empty implementation.

---
 tools/testing/selftests/kvm/lib/assert.c | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/selftests/kvm/lib/assert.c +7 -0
index b49690658c60..3442b80c37c1 100644
@@ -6,7 +6,9 @@
  */
 #include "test_util.h"
 
+#ifdef __GLIBC__
 #include <execinfo.h>
+#endif
 #include <sys/syscall.h>
 
 #include "kselftest.h"
@@ -15,6 +17,7 @@
 static void __attribute__((noinline)) test_dump_stack(void);
 static void test_dump_stack(void)
 {
+#ifdef __GLIBC__
 	/*
 	 * Build and run this command:
 	 *
@@ -56,6 +59,10 @@ static void test_dump_stack(void)
 #pragma GCC diagnostic ignored "-Wunused-result"
 	system(cmd);
 #pragma GCC diagnostic pop
+
+#else /* !__GLIBC__ */
+	fputs("  (stack trace not available: compiled without glibc)\n", stderr);
+#endif
 }
 
 static pid_t _gettid(void)
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 27 lines · Hisam Mehboob

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Guard the inclusion of execinfo.h under #ifdef __GLIBC__, and wrap all backtrace() usage under the same guard with a fallback message for non-glibc builds indicating that stack traces are not available.

Unlike the approach of adding a weak stub for backtrace(), this explicitly handles the non-glibc case rather than silently providing an empty implementation.

---
  tools/testing/selftests/kvm/lib/assert.c | 7 +++++++
  1 file changed, 7 insertions(+)
tools/testing/selftests/kvm/lib/assert.c +0 -0
index b49690658c60..3442b80c37c1 100644

tools/testing/selftests/kvm/lib/assert.c line 11

Quoted 6 lines
tools/testing/selftests/kvm/lib/assert.c +2 -0
@@ -6,7 +6,9 @@
   */
  #include "test_util.h"
 
+#ifdef __GLIBC__
  #include <execinfo.h>
+#endif

Is __GLIBC__ defined in musl-gcc? Looks like that is the case with the error?

Check a couple of things first: did you run "make headers" and do you have the correct packages installed on your system?

thanks,
-- Shuah

maintainer
#2 lore raw
Quoted 23 lines · Shuah Khan
Quoted 22 lines · Hisam Mehboob

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

   lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Guard the inclusion of execinfo.h under #ifdef __GLIBC__, and wrap all backtrace() usage under the same guard with a fallback message for non-glibc builds indicating that stack traces are not available.

Unlike the approach of adding a weak stub for backtrace(), this explicitly handles the non-glibc case rather than silently providing an empty implementation.

---
  tools/testing/selftests/kvm/lib/assert.c | 7 +++++++

  1 file changed, 7 insertions(+)
Quoted 12 lines
Quoted 11 lines
tools/testing/selftests/kvm/lib/assert.c +2 -0
index b49690658c60..3442b80c37c1 100644
@@ -6,7 +6,9 @@
    */
   #include "test_util.h"
+#ifdef __GLIBC__
   #include <execinfo.h>
+#endif

Is __GLIBC__ defined in musl-gcc? Looks like that is the case with the

error?

If __GLIBC__ isn't there you shouldn't see this error because the include is in - this error doesn't make sense if __GLIBC__ isn't defined. What am I missing?

+#ifdef __GLIBC__
#include <execinfo.h>
+#endif

Also check tools/testing/selftests/bpf/test_progs.c - I think backtrace() stub needs be defined only for the !__GLIBC__ case

thanks,
-- Shuah

#3 lore raw
Quoted 37 lines · Shuah Khan
Quoted 23 lines · Shuah Khan
Quoted 22 lines · Hisam Mehboob

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

   lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Guard the inclusion of execinfo.h under #ifdef __GLIBC__, and wrap all backtrace() usage under the same guard with a fallback message for non-glibc builds indicating that stack traces are not available.

Unlike the approach of adding a weak stub for backtrace(), this explicitly handles the non-glibc case rather than silently providing an empty implementation.

---
  tools/testing/selftests/kvm/lib/assert.c | 7 +++++++

  1 file changed, 7 insertions(+)
Quoted 12 lines
Quoted 11 lines
tools/testing/selftests/kvm/lib/assert.c +2 -0
index b49690658c60..3442b80c37c1 100644
@@ -6,7 +6,9 @@
    */
   #include "test_util.h"
+#ifdef __GLIBC__
   #include <execinfo.h>
+#endif

Is __GLIBC__ defined in musl-gcc? Looks like that is the case with the

error?

Quoted 4 lines

If __GLIBC__ isn't there you shouldn't see this error because the include is in - this error doesn't make sense if __GLIBC__ isn't defined. What am I missing?

To clarify the compiler error you mentioned: the error log in the commit message shows the failure that occurs before this patch is applied. Because musl-gcc doesn't define __GLIBC__, the original unconditional <execinfo.h> inclusion causes the build to fail. The #ifdef in my patch was intended to fix that exact failure.

Quoted 7 lines
tools/testing/selftests/kvm/lib/assert.c +2 -0
+#ifdef __GLIBC__
  #include <execinfo.h>
+#endif

Also check tools/testing/selftests/bpf/test_progs.c - I think backtrace() stub needs be defined only for the !__GLIBC__ case

Looking at how bpf/test_progs.c handles it, I agree the weak stub approach is much cleaner. I will implement it so that it still prints an explicit warning message when a trace is unavailable.

If you are okay with this, I will move forward with a v2 patch.

maintainer
#4 lore raw
Quoted 49 lines · Hisam Mehboob
Quoted 37 lines · Shuah Khan
Quoted 23 lines · Shuah Khan
Quoted 22 lines · Hisam Mehboob

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

   lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Guard the inclusion of execinfo.h under #ifdef __GLIBC__, and wrap all backtrace() usage under the same guard with a fallback message for non-glibc builds indicating that stack traces are not available.

Unlike the approach of adding a weak stub for backtrace(), this explicitly handles the non-glibc case rather than silently providing an empty implementation.

---
  tools/testing/selftests/kvm/lib/assert.c | 7 +++++++

  1 file changed, 7 insertions(+)
Quoted 12 lines
Quoted 11 lines
tools/testing/selftests/kvm/lib/assert.c +2 -0
index b49690658c60..3442b80c37c1 100644
@@ -6,7 +6,9 @@
    */
   #include "test_util.h"
+#ifdef __GLIBC__
   #include <execinfo.h>
+#endif

Is __GLIBC__ defined in musl-gcc? Looks like that is the case with the

error?

Quoted 4 lines

If __GLIBC__ isn't there you shouldn't see this error because the include is in - this error doesn't make sense if __GLIBC__ isn't defined. What am I missing?

To clarify the compiler error you mentioned: the error log in the commit message shows the failure that occurs before this patch is applied. Because musl-gcc doesn't define __GLIBC__, the original unconditional <execinfo.h> inclusion causes the build to fail. The #ifdef in my patch was intended to fix that exact failure.

Quoted 4 lines

+#ifdef __GLIBC__
  #include <execinfo.h>
+#endif

Quoted 8 lines
Quoted 3 lines

Also check tools/testing/selftests/bpf/test_progs.c - I think backtrace() stub needs be defined only for the !__GLIBC__ case

Looking at how bpf/test_progs.c handles it, I agree the weak stub approach is much cleaner. I will implement it so that it still prints an explicit warning message when a trace is unavailable.

If you are okay with this, I will move forward with a v2 patch.

Ultimately kvm maintainers make the call - send v2 patch

thanks,
-- Shuah

#5 lore raw
Quoted 54 lines · Shuah Khan
Quoted 53 lines · Hisam Mehboob
Quoted 37 lines · Shuah Khan
Quoted 23 lines · Shuah Khan
Quoted 22 lines · Hisam Mehboob

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

   lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Guard the inclusion of execinfo.h under #ifdef __GLIBC__, and wrap all backtrace() usage under the same guard with a fallback message for non-glibc builds indicating that stack traces are not available.

Unlike the approach of adding a weak stub for backtrace(), this explicitly handles the non-glibc case rather than silently providing an empty implementation.

---
  tools/testing/selftests/kvm/lib/assert.c | 7 +++++++

  1 file changed, 7 insertions(+)
Quoted 12 lines
Quoted 11 lines
tools/testing/selftests/kvm/lib/assert.c +2 -0
index b49690658c60..3442b80c37c1 100644
@@ -6,7 +6,9 @@
    */
   #include "test_util.h"
+#ifdef __GLIBC__
   #include <execinfo.h>
+#endif

Is __GLIBC__ defined in musl-gcc? Looks like that is the case with the

error?

Quoted 4 lines

If __GLIBC__ isn't there you shouldn't see this error because the include is in - this error doesn't make sense if __GLIBC__ isn't defined. What am I missing?

To clarify the compiler error you mentioned: the error log in the commit message shows the failure that occurs before this patch is applied. Because musl-gcc doesn't define __GLIBC__, the original unconditional <execinfo.h> inclusion causes the build to fail. The #ifdef in my patch was intended to fix that exact failure.

Quoted 4 lines

+#ifdef __GLIBC__
  #include <execinfo.h>
+#endif

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

Also check tools/testing/selftests/bpf/test_progs.c - I think backtrace() stub needs be defined only for the !__GLIBC__ case

Looking at how bpf/test_progs.c handles it, I agree the weak stub approach is much cleaner. I will implement it so that it still prints an explicit warning message when a trace is unavailable.

I disagree. _If_ we didn't need the __GLIBC__ #ifdef, then I would be in favor of __weak, but since the #ifdeffery is needed, using an #ifdef and a __weak symbol is double the ugliness.

IMO, the way to make this less ugly is to using a single #ifdef and a local stub.

tools/testing/selftests/kvm/lib/assert.c +6 -1
index b49690658c60..315175ca49f1 100644
@@ -6,11 +6,13 @@
  */
 #include "test_util.h"
 
-#include <execinfo.h>
 #include <sys/syscall.h>
 
 #include "kselftest.h"
 
+#ifdef __GLIBC__
+#include <execinfo.h>
+
 /* Dumps the current stack trace to stderr. */
 static void __attribute__((noinline)) test_dump_stack(void);
 static void test_dump_stack(void)
@@ -57,6 +59,9 @@ static void test_dump_stack(void)
        system(cmd);
 #pragma GCC diagnostic pop
 }
+#else
+static void test_dump_stack(void) {}
+#endif
 
 static pid_t _gettid(void)
 {
#6 lore raw
Quoted 89 lines · Sean Christopherson
Quoted 54 lines · Shuah Khan
Quoted 53 lines · Hisam Mehboob
Quoted 37 lines · Shuah Khan
Quoted 23 lines · Shuah Khan
Quoted 22 lines · Hisam Mehboob

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

   lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Guard the inclusion of execinfo.h under #ifdef __GLIBC__, and wrap all backtrace() usage under the same guard with a fallback message for non-glibc builds indicating that stack traces are not available.

Unlike the approach of adding a weak stub for backtrace(), this explicitly handles the non-glibc case rather than silently providing an empty implementation.

---
   tools/testing/selftests/kvm/lib/assert.c | 7 +++++++
   1 file changed, 7 insertions(+)
Quoted 12 lines
Quoted 11 lines
tools/testing/selftests/kvm/lib/assert.c +2 -0
index b49690658c60..3442b80c37c1 100644
@@ -6,7 +6,9 @@
    */
   #include "test_util.h"
+#ifdef __GLIBC__
   #include <execinfo.h>
+#endif

Is __GLIBC__ defined in musl-gcc? Looks like that is the case with the

error?

Quoted 4 lines

If __GLIBC__ isn't there you shouldn't see this error because the include is in - this error doesn't make sense if __GLIBC__ isn't defined. What am I missing?

To clarify the compiler error you mentioned: the error log in the commit message shows the failure that occurs before this patch is applied. Because musl-gcc doesn't define __GLIBC__, the original unconditional <execinfo.h> inclusion causes the build to fail. The #ifdef in my patch was intended to fix that exact failure.

Quoted 4 lines
(quoted diff) +2 -0
+#ifdef __GLIBC__
   #include <execinfo.h>
+#endif
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

Also check tools/testing/selftests/bpf/test_progs.c - I think backtrace() stub needs be defined only for the !__GLIBC__ case

Looking at how bpf/test_progs.c handles it, I agree the weak stub approach is much cleaner. I will implement it so that it still prints an explicit warning message when a trace is unavailable.

I disagree. _If_ we didn't need the __GLIBC__ #ifdef, then I would be in favor of __weak, but since the #ifdeffery is needed, using an #ifdef and a __weak symbol is double the ugliness.

IMO, the way to make this less ugly is to using a single #ifdef and a local stub.

tools/testing/selftests/kvm/lib/assert.c +3 -1
index b49690658c60..315175ca49f1 100644
@@ -6,11 +6,13 @@
   */
  #include "test_util.h"
 
-#include <execinfo.h>
  #include <sys/syscall.h>
 
  #include "kselftest.h"
 
+#ifdef __GLIBC__
+#include <execinfo.h>
+
  /* Dumps the current stack trace to stderr. */
  static void __attribute__((noinline)) test_dump_stack(void);
  static void test_dump_stack(void)

tools/testing/selftests/kvm/lib/assert.c static void test_dump_stack(void) · line 67

Quoted 9 lines
tools/testing/selftests/kvm/lib/assert.c +3 -0
@@ -57,6 +59,9 @@ static void test_dump_stack(void)
         system(cmd);
  #pragma GCC diagnostic pop
  }
+#else
+static void test_dump_stack(void) {}
+#endif
 
  static pid_t _gettid(void)
  {

Thanks for the suggestion. I will send a v2 implementing this approach.

#7 lore raw

PATCH v2 1/1 KVM: selftests: Guard execinfo.h inclusion for non-glibc builds

In reply to Hisam Mehboob

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Fix this by guarding the inclusion of execinfo.h and the stack dumping logic under #ifdef __GLIBC__. For non-glibc builds, provide a local stub for test_dump_stack().

---
Changes in v2:
- Replaced the __weak stub approach with a single #ifdef __GLIBC__ and a local stub inside assert.c as suggested by Sean Christopherson.

 tools/testing/selftests/kvm/lib/assert.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/assert.c +7 -1
index b49690658c60..8be0d09ecf0f 100644
@@ -6,11 +6,14 @@
  */
 #include "test_util.h"
 
-#include <execinfo.h>
+
 #include <sys/syscall.h>
 
 #include "kselftest.h"
 
+#ifdef __GLIBC__
+#include <execinfo.h>
+
 /* Dumps the current stack trace to stderr. */
 static void __attribute__((noinline)) test_dump_stack(void);
 static void test_dump_stack(void)
@@ -57,6 +60,9 @@ static void test_dump_stack(void)
 	system(cmd);
 #pragma GCC diagnostic pop
 }
+#else
+static void test_dump_stack(void) {}
+#endif
 
 static pid_t _gettid(void)
 {
Signature · 1 line
2.51.0
reviewer Applied
#8 lore raw
Quoted 11 lines · Hisam Mehboob

The backtrace() function and execinfo.h are GNU extensions available in glibc but not in non-glibc C libraries such as musl. Building KVM selftests with musl-gcc fails with:

lib/assert.c:9:10: fatal error: execinfo.h: No such file or directory

Fix this by guarding the inclusion of execinfo.h and the stack dumping logic under #ifdef __GLIBC__. For non-glibc builds, provide a local stub for test_dump_stack().

[...]

Applied to kvm-x86 fixes, thanks!

[1/1] KVM: selftests: Guard execinfo.h inclusion for non-glibc builds

      https://github.com/kvm-x86/linux/commit/34065a5f3cf9

--
https://github.com/kvm-x86/linux/tree/next