[PATCH v1 6/8] perf stat: Use sig_atomic_t to avoid UB

From: Ian Rogers
Date: Mon Oct 24 2022 - 14:58:28 EST


Use sig_atomic_t for variables written/accessed in signal
handlers. This is undefined behavior as per:
https://wiki.sei.cmu.edu/confluence/display/c/SIG31-C.+Do+not+access+shared+objects+in+signal+handlers

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-stat.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 265b05157972..13bb8508d228 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -83,6 +83,7 @@
#include <inttypes.h>
#include <locale.h>
#include <math.h>
+#include <stdatomic.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
@@ -173,7 +174,7 @@ static struct target target = {

#define METRIC_ONLY_LEN 20

-static volatile pid_t child_pid = -1;
+static volatile sig_atomic_t child_pid = -1;
static int detailed_run = 0;
static bool transaction_run;
static bool topdown_run = false;
@@ -208,7 +209,7 @@ struct perf_stat {
static struct perf_stat perf_stat;
#define STAT_RECORD perf_stat.record

-static volatile int done = 0;
+static volatile sig_atomic_t done = 0;

static struct perf_stat_config stat_config = {
.aggr_mode = AGGR_GLOBAL,
@@ -569,7 +570,7 @@ static void disable_counters(void)
}
}

-static volatile int workload_exec_errno;
+static volatile sig_atomic_t workload_exec_errno;

/*
* evlist__prepare_workload will send a SIGUSR1
@@ -1029,7 +1030,7 @@ static void print_counters(struct timespec *ts, int argc, const char **argv)
evlist__print_counters(evsel_list, &stat_config, &target, ts, argc, argv);
}

-static volatile int signr = -1;
+static volatile sig_atomic_t signr = -1;

static void skip_signal(int signo)
{
--
2.38.0.135.g90850a2211-goog