Skip to contentSkip to conversation

perf sched: Avoid union type punning undefined behavior

PATCH Applied Ian Rogers 2 participants 3 messages Sep 14, 2025 – Oct 1, 2025 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf sched: Avoid union type punning undefined behavior

A union is used to set the priv value in thread (a void*) to a boolean value through type punning. Undefined behavior sanitizer fails on this.

---
 tools/perf/builtin-sched.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
tools/perf/builtin-sched.c +4 -15
index f166d6cbc083..eca3b1c58c4b 100644
@@ -1532,35 +1532,24 @@ static int process_sched_wakeup_ignore(const struct perf_tool *tool __maybe_unus
 	return 0;
 }
 
-union map_priv {
-	void	*ptr;
-	bool	 color;
-};
-
 static bool thread__has_color(struct thread *thread)
 {
-	union map_priv priv = {
-		.ptr = thread__priv(thread),
-	};
-
-	return priv.color;
+	return thread__priv(thread) != NULL;
 }
 
 static struct thread*
 map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
 {
 	struct thread *thread = machine__findnew_thread(machine, pid, tid);
-	union map_priv priv = {
-		.color = false,
-	};
+	bool color = false;
 
 	if (!sched->map.color_pids || !thread || thread__priv(thread))
 		return thread;
 
 	if (thread_map__has(sched->map.color_pids, tid))
-		priv.color = true;
+		color = true;
 
-	thread__set_priv(thread, priv.ptr);
+	thread__set_priv(thread, color ? ((void*)1) : NULL);
 	return thread;
 }
 
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
author reviewer
#1 lore raw
Quoted 5 lines · Ian Rogers

A union is used to set the priv value in thread (a void*) to a boolean value through type punning. Undefined behavior sanitizer fails on this.

Ping.

Thanks,
Ian

Quoted 51 lines
---
 tools/perf/builtin-sched.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
tools/perf/builtin-sched.c +4 -16
index f166d6cbc083..eca3b1c58c4b 100644
@@ -1532,35 +1532,24 @@ static int process_sched_wakeup_ignore(const struct perf_tool *tool __maybe_unus
        return 0;
 }
 
-union map_priv {
-       void    *ptr;
-       bool     color;
-};
-
 static bool thread__has_color(struct thread *thread)
 {
-       union map_priv priv = {
-               .ptr = thread__priv(thread),
-       };
-
-       return priv.color;
+       return thread__priv(thread) != NULL;
 }
 
 static struct thread*
 map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
 {
        struct thread *thread = machine__findnew_thread(machine, pid, tid);
-       union map_priv priv = {
-               .color = false,
-       };
+       bool color = false;
 
        if (!sched->map.color_pids || !thread || thread__priv(thread))
                return thread;
 
        if (thread_map__has(sched->map.color_pids, tid))
-               priv.color = true;
+               color = true;
 
-       thread__set_priv(thread, priv.ptr);
+       thread__set_priv(thread, color ? ((void*)1) : NULL);
        return thread;
 }
 
--

2.51.0.384.g4c02a37b29-goog

maintainer Applied
#2 lore raw
Quoted 8 lines · Ian Rogers
Quoted 5 lines · Ian Rogers

A union is used to set the priv value in thread (a void*) to a boolean value through type punning. Undefined behavior sanitizer fails on this.

Ping.

Thanks, applied to perf-tools-next,

- Arnaldo