[compiler-rt] [XRay] Always register constructor(0) alongside .preinit_array (PR #188788)

Brian Cain via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 27 06:49:18 PDT 2026


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

>From 823c5eb30a90e5bf176058e4fc4d481297cb81bc Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Thu, 26 Mar 2026 09:01:07 -0700
Subject: [PATCH 1/2] [XRay] Always register constructor(0) alongside
 .preinit_array

On musl-based systems the dynamic linker does not process
DT_PREINIT_ARRAY, so the .preinit_array entry alone never calls
__xray_init().  Without initialization, the global XRay Flags struct
is zero-initialized and flags()->xray_mode is NULL.  When the
basic-mode or FDR-mode static initializers run from .init_array and
call internal_strcmp(flags()->xray_mode, ...), they dereference NULL
and crash.

Fix this by always registering a constructor(0) in addition to the
.preinit_array entry.  On glibc where .preinit_array works,
__xray_init() will have already run and the constructor returns
immediately (the function is idempotent).  On musl, the constructor
ensures __xray_init() runs before other .init_array entries that
depend on XRay flags being initialized.

Also add the __xray_init() declaration to xray_interface_internal.h
so it is visible to all compilation units that need it.
---
 compiler-rt/lib/xray/xray_init.cpp             | 14 +++++++++-----
 compiler-rt/lib/xray/xray_interface_internal.h |  6 ++++++
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/lib/xray/xray_init.cpp b/compiler-rt/lib/xray/xray_init.cpp
index 9cc6d5fcc4c1d..4d6ed4eb6fc1b 100644
--- a/compiler-rt/lib/xray/xray_init.cpp
+++ b/compiler-rt/lib/xray/xray_init.cpp
@@ -252,11 +252,15 @@ __xray_deregister_dso(int32_t ObjId) XRAY_NEVER_INSTRUMENT {
 // Only add the preinit array initialization if the sanitizers can.
 __attribute__((section(".preinit_array"),
                used)) void (*__local_xray_preinit)(void) = __xray_init;
-#else
-// If we cannot use the .preinit_array section, we should instead use dynamic
-// initialisation.
-__attribute__ ((constructor (0)))
-static void __local_xray_dyninit() {
+#endif
+
+// Always register a constructor as well.  On platforms where .preinit_array
+// works (glibc), __xray_init will have already run and the constructor returns
+// immediately.  On platforms where .preinit_array is not processed (e.g. musl),
+// the constructor ensures __xray_init runs before other .init_array entries
+// that depend on XRay flags being initialized.
+#if !defined(XRAY_NO_PREINIT)
+__attribute__((constructor(0))) static void __local_xray_dyninit() {
   __xray_init();
 }
 #endif
diff --git a/compiler-rt/lib/xray/xray_interface_internal.h b/compiler-rt/lib/xray/xray_interface_internal.h
index 5dcccfe825cf5..b26914a49559c 100644
--- a/compiler-rt/lib/xray/xray_interface_internal.h
+++ b/compiler-rt/lib/xray/xray_interface_internal.h
@@ -21,6 +21,12 @@
 #include <utility>
 
 extern "C" {
+
+// Ensure XRay is initialized. Safe to call multiple times; the first call
+// performs flag parsing, sled registration, and (if requested) pre-main
+// patching.  Subsequent calls return immediately.
+extern void __xray_init();
+
 // The following functions have to be defined in assembler, on a per-platform
 // basis. See xray_trampoline_*.S files for implementations.
 extern void __xray_FunctionEntry();

>From 13b61a2852942a4d3b7c8ee409551392af75b3e8 Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Fri, 27 Mar 2026 06:48:19 -0700
Subject: [PATCH 2/2] fixup! [XRay] Always register constructor(0) alongside
 .preinit_array

Remove redundant __xray_init() declaration from xray_interface_internal.h

The declaration is already provided by the public header
xray/xray_interface.h, which xray_interface_internal.h includes.
---
 compiler-rt/lib/xray/xray_interface_internal.h | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/compiler-rt/lib/xray/xray_interface_internal.h b/compiler-rt/lib/xray/xray_interface_internal.h
index b26914a49559c..5dcccfe825cf5 100644
--- a/compiler-rt/lib/xray/xray_interface_internal.h
+++ b/compiler-rt/lib/xray/xray_interface_internal.h
@@ -21,12 +21,6 @@
 #include <utility>
 
 extern "C" {
-
-// Ensure XRay is initialized. Safe to call multiple times; the first call
-// performs flag parsing, sled registration, and (if requested) pre-main
-// patching.  Subsequent calls return immediately.
-extern void __xray_init();
-
 // The following functions have to be defined in assembler, on a per-platform
 // basis. See xray_trampoline_*.S files for implementations.
 extern void __xray_FunctionEntry();



More information about the llvm-commits mailing list