Skip to contentSkip to conversation

lib/cobalt: Unbreak sigshadow installation for statically linked applications

PATCH In review Jan Kiszka 2 participants 2 messages May 19, 2025 – May 20, 2025

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH v2 1/1 lib/cobalt: Unbreak sigshadow installation for statically linked applications

From: Jan Kiszka <[email protected]>

When statically linking, we do not have a .eh_frame_hdr section. If we then call backtrace before frame_dummy was invoked during late constructions, we abort. Address that by moving the backtrace warm-up call into a separate constructor that is armed earlier by install_sigshadow, or call it later directly if install_sigshadow is running after the construction phase.

---

Changes in v2:
- use explicit lowest constructor prio to make it clearer that we need to be last

 lib/cobalt/sigshadow.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
lib/cobalt/sigshadow.c +26 -7
index 092cadb08a..10736b113f 100644
@@ -33,6 +33,9 @@ static inline int backtrace(void **buffer, int size)
 
 static struct sigaction sigshadow_action_orig;
 
+static bool request_backtrace_init;
+static bool past_constructors;
+
 /*
  * The following handler is part of the inner user-interface: should
  * remain extern.
@@ -96,19 +99,35 @@ static void sigshadow_handler(int sig, siginfo_t *si, void *ctxt)
 	pthread_sigmask(SIG_SETMASK, &saved_sigset, NULL);
 }
 
-static void install_sigshadow(void)
+/*
+ * Kickstart backtrace() so that it may call malloc() from a safe context right
+ * now, not later on from the sigshadow handler.
+ *
+ * We can only trigger this after main() started or very late during
+ * construction because, when statically linked, frame_dummy in ctrstuff must
+ * have run already.
+ */
+static void __attribute__((constructor(65535))) trigger_backtrace(void)
 {
 	void *dummy[SIGSHADOW_BACKTRACE_DEPTH];
+
+	if (request_backtrace_init)
+		backtrace(dummy, SIGSHADOW_BACKTRACE_DEPTH);
+
+	past_constructors = true;
+	/* ensure visibility of past_constructors after init phase */
+	smp_mb();
+}
+
+static void install_sigshadow(void)
+{
 	struct sigaction new_sigshadow_action;
 	sigset_t saved_sigset;
 	sigset_t mask_sigset;
 
-	/*
-	 * Kickstart backtrace() so that it may call malloc() from a
-	 * safe context right now, not later on from the sigshadow
-	 * handler.
-	 */
-	backtrace(dummy, SIGSHADOW_BACKTRACE_DEPTH);
+	request_backtrace_init = true;
+	if (past_constructors)
+		trigger_backtrace();
 
 	sigemptyset(&mask_sigset);
 	sigaddset(&mask_sigset, SIGSHADOW);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 79 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

When statically linking, we do not have a .eh_frame_hdr section. If we then call backtrace before frame_dummy was invoked during late constructions, we abort. Address that by moving the backtrace warm-up call into a separate constructor that is armed earlier by install_sigshadow, or call it later directly if install_sigshadow is running after the construction phase.

---

Changes in v2:
- use explicit lowest constructor prio to make it clearer that we need to be last

lib/cobalt/sigshadow.c | 33 ++++++++++++++++++++++++++-------

1 file changed, 26 insertions(+), 7 deletions(-)
lib/cobalt/sigshadow.c +26 -7
index 092cadb08a..10736b113f 100644
@@ -33,6 +33,9 @@ static inline int backtrace(void **buffer, int size)
 
 static struct sigaction sigshadow_action_orig;
 
+static bool request_backtrace_init;
+static bool past_constructors;
+
 /*
 * The following handler is part of the inner user-interface: should
 * remain extern.
@@ -96,19 +99,35 @@ static void sigshadow_handler(int sig, siginfo_t *si, void *ctxt)
 	pthread_sigmask(SIG_SETMASK, &saved_sigset, NULL);
 }
 
-static void install_sigshadow(void)
+/*
+ * Kickstart backtrace() so that it may call malloc() from a safe context right
+ * now, not later on from the sigshadow handler.
+ *
+ * We can only trigger this after main() started or very late during
+ * construction because, when statically linked, frame_dummy in ctrstuff must
+ * have run already.
+ */
+static void __attribute__((constructor(65535))) trigger_backtrace(void)
 {
 	void *dummy[SIGSHADOW_BACKTRACE_DEPTH];
+
+	if (request_backtrace_init)
+		backtrace(dummy, SIGSHADOW_BACKTRACE_DEPTH);
+
+	past_constructors = true;
+	/* ensure visibility of past_constructors after init phase */
+	smp_mb();
+}
+
+static void install_sigshadow(void)
+{
 	struct sigaction new_sigshadow_action;
 	sigset_t saved_sigset;
 	sigset_t mask_sigset;
 
-	/*
-	 * Kickstart backtrace() so that it may call malloc() from a
-	 * safe context right now, not later on from the sigshadow
-	 * handler.
-	 */
-	backtrace(dummy, SIGSHADOW_BACKTRACE_DEPTH);
+	request_backtrace_init = true;
+	if (past_constructors)
+		trigger_backtrace();
 
 	sigemptyset(&mask_sigset);
 	sigaddset(&mask_sigset, SIGSHADOW);
Signature · 1 line
2.43.0

With this patch applied to xenomai, rebuild the static app then it runs normally. Thanks!

Haiyang.