]> www.infradead.org Git - users/jedix/linux-maple.git/commit
perf trace: Fix unaligned access for augmented args
authorNamhyung Kim <namhyung@kernel.org>
Thu, 2 Jan 2025 20:12:47 +0000 (12:12 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 10 Jan 2025 13:59:42 +0000 (10:59 -0300)
commit4f90ed0ae36ac2c223d312a2cd767714196b1455
tree0e62dce23a3996ba56f082b139147d7b21ffac5a
parent0ba2022410fc1acf67f958474d666c0d35addcc7
perf trace: Fix unaligned access for augmented args

Some version of compilers reported unaligned accesses in perf trace when
undefined-behavior sanitizer is on.  I found that it uses raw data in
the sample directly and assuming it's properly aligned.

Unlike other sample fields, the raw data is not 8-byte aligned because
there's a size field (u32) before the actual data.  So I added a static
buffer in syscall__augmented_args() and return it instead.  This is not
ideal but should work well as perf trace is single-threaded.

A better approach would be aligning the raw data by adding a 4-byte data
before the augmented args but I'm afraid it'd break the backward
compatibility.

Committer testing:

To build with the undefined behaviour sanitizer:

 $ make CC=clang EXTRA_CFLAGS=-fsanitize=undefined -C tools/perf

Checking if the resulting binary is instrumented:

  root@number:~# nm ~/bin/perf | grep ubsan | wc -l
  113
  root@number:~# nm ~/bin/perf | grep ubsan | tail -5
  000000000043d5b0 t _ZN7__ubsanL19UBsanOnDeadlySignalEiPvS0_
  000000000043ce50 T _ZNK7__ubsan5Value12getSIntValueEv
  000000000043cf40 T _ZNK7__ubsan5Value12getUIntValueEv
  000000000043d140 T _ZNK7__ubsan5Value13getFloatValueEv
  000000000043cfd0 T _ZNK7__ubsan5Value19getPositiveIntValueEv
  root@number:~#

Now running something that will access timespec, as reported in the
Closes URL:

  root@number:~# perf trace --max-events=1 -e *nano* sleep 1.1
  trace/beauty/timespec.c:10:64: runtime error: member access within misaligned address 0x7fc583cfb2a4 for type 'struct augmented_arg', which requires 8 byte alignment
  0x7fc583cfb2a4: note: pointer points here
    99 99 11 00 10 00 00 00  00 00 00 00 01 00 00 00  00 00 00 00 01 e1 f5 05  00 00 00 00 00 00 00 00
                ^
  SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior trace/beauty/timespec.c:10:64
  <SNIP>

As Namhyung said we need to make the raw_data to be 64-bit aligned,
probably we need to add a PERF_SAMPLE_ALIGNED_RAW with a 64-bit raw_size
instead of the current u32 done at kernel/events/core.c,
perf_output_sample(), that perf_output_put(handle, raw->size) where
raw->size is an u32 and then the raw_data is always 64-bit unaligned...

After the patch:

  root@number:~# perf trace -e *nano* sleep 1.1
       0.000 (1100.064 ms): sleep/1984224 clock_nanosleep(rqtp: { .tv_sec: 1, .tv_nsec: 100000001 }, rmtp: 0x7fff5b3fe970) = 0
  root@number:~#

Closes: https://lore.kernel.org/r/Z2STgyD1p456Qqhg@google.com
Reviewed-by: Howard Chu <howardchu95@gmail.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250102201248.790841-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-trace.c