[compiler-rt] [compiler-rt][GWP-ASan] Use no-op backtrace if execinfo.h missing (PR #201266)

Brian Cain via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 27 19:54:03 PDT 2026


https://github.com/androm3da updated https://github.com/llvm/llvm-project/pull/201266

>From a2f9d6212998b633ce0f3857c7230c686c322dee Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Tue, 2 Jun 2026 19:50:25 -0500
Subject: [PATCH 1/2] [compiler-rt][GWP-ASan] Use no-op backtrace if execinfo.h
 missing

backtrace_linux_libc.cpp requires execinfo.h, which is not present in some
C libraries (musl for example).

Add a cmake check_include_file guard: on targets with execinfo.h the existing
RTGwpAsanBacktraceLibc is built unchanged; on targets without it a new
RTGwpAsanBacktraceNone stub is built instead (all three function pointers
return nullptr).  Mirror the same check in scudo/standalone so it links
against whichever library was built.
---
 compiler-rt/lib/gwp_asan/CMakeLists.txt       | 23 +++++++++++++++----
 .../lib/gwp_asan/optional/backtrace_none.cpp  | 22 ++++++++++++++++++
 .../lib/scudo/standalone/CMakeLists.txt       | 11 ++++++++-
 3 files changed, 50 insertions(+), 6 deletions(-)
 create mode 100644 compiler-rt/lib/gwp_asan/optional/backtrace_none.cpp

diff --git a/compiler-rt/lib/gwp_asan/CMakeLists.txt b/compiler-rt/lib/gwp_asan/CMakeLists.txt
index f638adda3abe1..6a7aaf19d05a1 100644
--- a/compiler-rt/lib/gwp_asan/CMakeLists.txt
+++ b/compiler-rt/lib/gwp_asan/CMakeLists.txt
@@ -90,11 +90,24 @@ add_compiler_rt_object_libraries(RTGwpAsanOptionsParser
     CFLAGS ${GWP_ASAN_OPTIONS_PARSER_CFLAGS})
 
 # As above, build the pre-implemented optional backtrace support libraries.
-add_compiler_rt_object_libraries(RTGwpAsanBacktraceLibc
-    ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
-    SOURCES optional/backtrace_linux_libc.cpp
-    ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
-    CFLAGS ${GWP_ASAN_CFLAGS})
+# backtrace_linux_libc.cpp requires execinfo.h.  On targets without it
+# build RTGwpAsanBacktraceNone which provides null stubs so that
+# GWP-ASan still functions as a guard allocator without stack-trace capture.
+include(CheckIncludeFile)
+check_include_file(execinfo.h COMPILER_RT_HAS_EXECINFO_H)
+if(COMPILER_RT_HAS_EXECINFO_H)
+  add_compiler_rt_object_libraries(RTGwpAsanBacktraceLibc
+      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
+      SOURCES optional/backtrace_linux_libc.cpp
+      ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
+      CFLAGS ${GWP_ASAN_CFLAGS})
+else()
+  add_compiler_rt_object_libraries(RTGwpAsanBacktraceNone
+      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
+      SOURCES optional/backtrace_none.cpp
+      ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
+      CFLAGS ${GWP_ASAN_CFLAGS})
+endif()
 add_compiler_rt_object_libraries(RTGwpAsanSegvHandler
     ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
     SOURCES optional/segv_handler_posix.cpp
diff --git a/compiler-rt/lib/gwp_asan/optional/backtrace_none.cpp b/compiler-rt/lib/gwp_asan/optional/backtrace_none.cpp
new file mode 100644
index 0000000000000..0530d2bb0c993
--- /dev/null
+++ b/compiler-rt/lib/gwp_asan/optional/backtrace_none.cpp
@@ -0,0 +1,22 @@
+//===-- backtrace_none.cpp --------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// Stub backtrace implementation for targets where execinfo.h is unavailable
+// (e.g. musl libc).  GWP-ASan still functions as a guard allocator; only
+// the stack-trace capture is absent.
+
+#include "gwp_asan/optional/backtrace.h"
+
+namespace gwp_asan {
+namespace backtrace {
+
+options::Backtrace_t getBacktraceFunction() { return nullptr; }
+PrintBacktrace_t getPrintBacktraceFunction() { return nullptr; }
+SegvBacktrace_t getSegvBacktraceFunction() { return nullptr; }
+
+} // namespace backtrace
+} // namespace gwp_asan
diff --git a/compiler-rt/lib/scudo/standalone/CMakeLists.txt b/compiler-rt/lib/scudo/standalone/CMakeLists.txt
index 20c59b4600613..1718f7e083f61 100644
--- a/compiler-rt/lib/scudo/standalone/CMakeLists.txt
+++ b/compiler-rt/lib/scudo/standalone/CMakeLists.txt
@@ -169,8 +169,17 @@ if (COMPILER_RT_HAS_GWP_ASAN)
   endif()
 
   add_dependencies(scudo_standalone gwp_asan)
+  # RTGwpAsanBacktraceLibc requires execinfo.h (not available on some C
+  # libs. Use RTGwpAsanBacktraceNone which provides null stubs.
+  include(CheckIncludeFile)
+  check_include_file(execinfo.h COMPILER_RT_HAS_EXECINFO_H)
+  if(COMPILER_RT_HAS_EXECINFO_H)
+    set(_gwp_asan_backtrace_lib RTGwpAsanBacktraceLibc)
+  else()
+    set(_gwp_asan_backtrace_lib RTGwpAsanBacktraceNone)
+  endif()
   list(APPEND SCUDO_OBJECT_LIBS
-       RTGwpAsan RTGwpAsanBacktraceLibc RTGwpAsanSegvHandler
+       RTGwpAsan ${_gwp_asan_backtrace_lib} RTGwpAsanSegvHandler
        RTGwpAsanOptionsParser)
 
   append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fno-omit-frame-pointer

>From 8dda8793fa14dd6ab05b1722ceeb97a79ff48cad Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Sat, 27 Jun 2026 19:49:48 -0700
Subject: [PATCH 2/2] fixup! [compiler-rt][GWP-ASan] Use no-op backtrace if
 execinfo.h missing

Use __has_include(<execinfo.h>) in the source instead of CMake-level
check_include_file().  This removes backtrace_none.cpp and the
conditional target selection from both CMakeLists.txt files -- the
preprocessor handles the fallback inline.
---
 compiler-rt/lib/gwp_asan/CMakeLists.txt       | 23 ++++---------------
 .../optional/backtrace_linux_libc.cpp         | 20 +++++++++++++++-
 .../lib/gwp_asan/optional/backtrace_none.cpp  | 22 ------------------
 .../lib/scudo/standalone/CMakeLists.txt       | 11 +--------
 4 files changed, 25 insertions(+), 51 deletions(-)
 delete mode 100644 compiler-rt/lib/gwp_asan/optional/backtrace_none.cpp

diff --git a/compiler-rt/lib/gwp_asan/CMakeLists.txt b/compiler-rt/lib/gwp_asan/CMakeLists.txt
index 6a7aaf19d05a1..f638adda3abe1 100644
--- a/compiler-rt/lib/gwp_asan/CMakeLists.txt
+++ b/compiler-rt/lib/gwp_asan/CMakeLists.txt
@@ -90,24 +90,11 @@ add_compiler_rt_object_libraries(RTGwpAsanOptionsParser
     CFLAGS ${GWP_ASAN_OPTIONS_PARSER_CFLAGS})
 
 # As above, build the pre-implemented optional backtrace support libraries.
-# backtrace_linux_libc.cpp requires execinfo.h.  On targets without it
-# build RTGwpAsanBacktraceNone which provides null stubs so that
-# GWP-ASan still functions as a guard allocator without stack-trace capture.
-include(CheckIncludeFile)
-check_include_file(execinfo.h COMPILER_RT_HAS_EXECINFO_H)
-if(COMPILER_RT_HAS_EXECINFO_H)
-  add_compiler_rt_object_libraries(RTGwpAsanBacktraceLibc
-      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
-      SOURCES optional/backtrace_linux_libc.cpp
-      ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
-      CFLAGS ${GWP_ASAN_CFLAGS})
-else()
-  add_compiler_rt_object_libraries(RTGwpAsanBacktraceNone
-      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
-      SOURCES optional/backtrace_none.cpp
-      ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
-      CFLAGS ${GWP_ASAN_CFLAGS})
-endif()
+add_compiler_rt_object_libraries(RTGwpAsanBacktraceLibc
+    ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
+    SOURCES optional/backtrace_linux_libc.cpp
+    ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
+    CFLAGS ${GWP_ASAN_CFLAGS})
 add_compiler_rt_object_libraries(RTGwpAsanSegvHandler
     ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
     SOURCES optional/segv_handler_posix.cpp
diff --git a/compiler-rt/lib/gwp_asan/optional/backtrace_linux_libc.cpp b/compiler-rt/lib/gwp_asan/optional/backtrace_linux_libc.cpp
index ea8e72be287da..80ba2e2fa5faa 100644
--- a/compiler-rt/lib/gwp_asan/optional/backtrace_linux_libc.cpp
+++ b/compiler-rt/lib/gwp_asan/optional/backtrace_linux_libc.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include <assert.h>
-#include <execinfo.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -18,6 +17,9 @@
 #include "gwp_asan/optional/printf.h"
 #include "gwp_asan/options.h"
 
+#if __has_include(<execinfo.h>)
+#include <execinfo.h>
+
 namespace {
 size_t Backtrace(uintptr_t *TraceBuffer, size_t Size) {
   static_assert(sizeof(uintptr_t) == sizeof(void *), "uintptr_t is not void*");
@@ -65,3 +67,19 @@ SegvBacktrace_t getSegvBacktraceFunction() { return SegvBacktrace; }
 
 } // namespace backtrace
 } // namespace gwp_asan
+
+#else // !__has_include(<execinfo.h>)
+
+// No execinfo.h (e.g. musl libc) -- provide null stubs so GWP-ASan still
+// functions as a guard allocator without stack-trace capture.
+namespace gwp_asan {
+namespace backtrace {
+
+options::Backtrace_t getBacktraceFunction() { return nullptr; }
+PrintBacktrace_t getPrintBacktraceFunction() { return nullptr; }
+SegvBacktrace_t getSegvBacktraceFunction() { return nullptr; }
+
+} // namespace backtrace
+} // namespace gwp_asan
+
+#endif // __has_include(<execinfo.h>)
diff --git a/compiler-rt/lib/gwp_asan/optional/backtrace_none.cpp b/compiler-rt/lib/gwp_asan/optional/backtrace_none.cpp
deleted file mode 100644
index 0530d2bb0c993..0000000000000
--- a/compiler-rt/lib/gwp_asan/optional/backtrace_none.cpp
+++ /dev/null
@@ -1,22 +0,0 @@
-//===-- backtrace_none.cpp --------------------------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-// Stub backtrace implementation for targets where execinfo.h is unavailable
-// (e.g. musl libc).  GWP-ASan still functions as a guard allocator; only
-// the stack-trace capture is absent.
-
-#include "gwp_asan/optional/backtrace.h"
-
-namespace gwp_asan {
-namespace backtrace {
-
-options::Backtrace_t getBacktraceFunction() { return nullptr; }
-PrintBacktrace_t getPrintBacktraceFunction() { return nullptr; }
-SegvBacktrace_t getSegvBacktraceFunction() { return nullptr; }
-
-} // namespace backtrace
-} // namespace gwp_asan
diff --git a/compiler-rt/lib/scudo/standalone/CMakeLists.txt b/compiler-rt/lib/scudo/standalone/CMakeLists.txt
index 1718f7e083f61..20c59b4600613 100644
--- a/compiler-rt/lib/scudo/standalone/CMakeLists.txt
+++ b/compiler-rt/lib/scudo/standalone/CMakeLists.txt
@@ -169,17 +169,8 @@ if (COMPILER_RT_HAS_GWP_ASAN)
   endif()
 
   add_dependencies(scudo_standalone gwp_asan)
-  # RTGwpAsanBacktraceLibc requires execinfo.h (not available on some C
-  # libs. Use RTGwpAsanBacktraceNone which provides null stubs.
-  include(CheckIncludeFile)
-  check_include_file(execinfo.h COMPILER_RT_HAS_EXECINFO_H)
-  if(COMPILER_RT_HAS_EXECINFO_H)
-    set(_gwp_asan_backtrace_lib RTGwpAsanBacktraceLibc)
-  else()
-    set(_gwp_asan_backtrace_lib RTGwpAsanBacktraceNone)
-  endif()
   list(APPEND SCUDO_OBJECT_LIBS
-       RTGwpAsan ${_gwp_asan_backtrace_lib} RTGwpAsanSegvHandler
+       RTGwpAsan RTGwpAsanBacktraceLibc RTGwpAsanSegvHandler
        RTGwpAsanOptionsParser)
 
   append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fno-omit-frame-pointer



More information about the llvm-commits mailing list