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

perf tools: Avoid unaligned pointer operations

The sample data is 64-bit aligned basically but raw data starts with
32-bit length field and data follows. In perf_event__synthesize_sample
it treats the sample data as a 64-bit array. And it needs some trick
to update the raw data properly.

But it seems some compilers are not happy with this and the program dies
siliently. I found the sample parsing test failed without any messages
on affected systems.

Let's update the code to use a 32-bit pointer directly and make sure the
result is 64-bit aligned again. No functional changes intended.

Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20241128010325.946897-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
ad5d76ae b1ef2559

+9 -5
+9 -5
tools/perf/util/synthetic-events.c
··· 1686 1686 } 1687 1687 1688 1688 if (type & PERF_SAMPLE_RAW) { 1689 - u.val32[0] = sample->raw_size; 1690 - *array = u.val64; 1691 - array = (void *)array + sizeof(u32); 1689 + u32 *array32 = (void *)array; 1692 1690 1693 - memcpy(array, sample->raw_data, sample->raw_size); 1694 - array = (void *)array + sample->raw_size; 1691 + *array32 = sample->raw_size; 1692 + array32++; 1693 + 1694 + memcpy(array32, sample->raw_data, sample->raw_size); 1695 + array = (void *)(array32 + (sample->raw_size / sizeof(u32))); 1696 + 1697 + /* make sure the array is 64-bit aligned */ 1698 + BUG_ON(((long)array) % sizeof(u64)); 1695 1699 } 1696 1700 1697 1701 if (type & PERF_SAMPLE_BRANCH_STACK) {