Fix: procname context async-signal safety
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 14 Feb 2019 21:14:42 +0000 (16:14 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 14 Feb 2019 21:14:42 +0000 (16:14 -0500)
Ensure an instrumented signal handler interrupting the procname
context code will not write a partial process name string into the
trace context.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust/lttng-context-procname.c

index b6e69501d81c08bc79c5f82d9ff5b85d1d22230a..95d5f28dbe20b9a2e21decd18e7b2db04ccb0efa 100644 (file)
@@ -28,6 +28,9 @@
 #include <assert.h>
 #include "compat.h"
 
+/* Maximum number of nesting levels for the procname cache. */
+#define PROCNAME_NESTING_MAX   2
+
 /*
  * We cache the result to ensure we don't trigger a system call for
  * each event.
  * be set for a thread before the first event is logged within this
  * thread.
  */
-typedef char procname_array[17];
+typedef char procname_array[PROCNAME_NESTING_MAX][17];
+
 static DEFINE_URCU_TLS(procname_array, cached_procname);
 
+static DEFINE_URCU_TLS(int, procname_nesting);
+
 static inline
 char *wrapper_getprocname(void)
 {
-       if (caa_unlikely(!URCU_TLS(cached_procname)[0])) {
-               lttng_ust_getprocname(URCU_TLS(cached_procname));
-               URCU_TLS(cached_procname)[LTTNG_UST_PROCNAME_LEN - 1] = '\0';
+       int nesting = URCU_TLS(procname_nesting);
+
+       if (caa_unlikely(nesting >= PROCNAME_NESTING_MAX))
+               return "<unknown>";
+       if (caa_unlikely(!URCU_TLS(cached_procname)[nesting][0])) {
+               CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1);
+               lttng_ust_getprocname(URCU_TLS(cached_procname)[nesting]);
+               URCU_TLS(cached_procname)[nesting][LTTNG_UST_PROCNAME_LEN - 1] = '\0';
+               CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
        }
-       return URCU_TLS(cached_procname);
+       return URCU_TLS(cached_procname)[nesting];
 }
 
 void lttng_context_procname_reset(void)
 {
-       URCU_TLS(cached_procname)[0] = '\0';
+       URCU_TLS(cached_procname)[1][0] = '\0';
+       CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1);
+       URCU_TLS(cached_procname)[0][0] = '\0';
+       CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0);
 }
 
 static
This page took 0.027058 seconds and 4 git commands to generate.