Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
58a60614 241f21be

+4 -15
+4 -15
tools/perf/builtin-sched.c
··· 1532 1532 return 0; 1533 1533 } 1534 1534 1535 - union map_priv { 1536 - void *ptr; 1537 - bool color; 1538 - }; 1539 - 1540 1535 static bool thread__has_color(struct thread *thread) 1541 1536 { 1542 - union map_priv priv = { 1543 - .ptr = thread__priv(thread), 1544 - }; 1545 - 1546 - return priv.color; 1537 + return thread__priv(thread) != NULL; 1547 1538 } 1548 1539 1549 1540 static struct thread* 1550 1541 map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid) 1551 1542 { 1552 1543 struct thread *thread = machine__findnew_thread(machine, pid, tid); 1553 - union map_priv priv = { 1554 - .color = false, 1555 - }; 1544 + bool color = false; 1556 1545 1557 1546 if (!sched->map.color_pids || !thread || thread__priv(thread)) 1558 1547 return thread; 1559 1548 1560 1549 if (thread_map__has(sched->map.color_pids, tid)) 1561 - priv.color = true; 1550 + color = true; 1562 1551 1563 - thread__set_priv(thread, priv.ptr); 1552 + thread__set_priv(thread, color ? ((void*)1) : NULL); 1564 1553 return thread; 1565 1554 } 1566 1555