perf tools: Avoid unaligned pointer operations
Namhyung Kim
maintainerPATCH v2 1/1 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.
---
v2) use '%' instead of '/' to check alignment
tools/perf/util/synthetic-events.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
tools/perf/util/synthetic-events.c +9 -5
@@ -1686,12 +1686,16 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_fo }if (type &PERF_SAMPLE_RAW ) { - u.val32[0 ] = sample->raw_size; - *array = u.val64; - array = (void *)array +sizeof (u32); +u32 *array32 = (void *)array; + + *array32 = sample->raw_size; + array32++; + +memcpy (array32, sample->raw_data, sample->raw_size); + array = (void *)(array32 + (sample->raw_size /sizeof (u32))); -memcpy (array, sample->raw_data, sample->raw_size); - array = (void *)array + sample->raw_size; +/* make sure the array is 64-bit aligned */ +BUG_ON (((long )array) %sizeof (u64)); }if (type &PERF_SAMPLE_BRANCH_STACK ) {
Signature · 1 line
2.47.0.338.g60cca15819-goog
Quoted 9 lines · Namhyung Kim
The sample data is 64-bit aligned basically but raw data starts with 32-bit length field and data follows. In
perf_event__synthesize_sampleit 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.
Quoted 5 lines
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 <[email protected]>Signed-off-by Namhyung Kim <[email protected]>
Thanks,
Kan
Quoted 32 lines
---
v2) use '%' instead of '/' to check alignmenttools/perf/util/synthetic-events.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)tools/perf/util/synthetic-events.c +9
-5@@ -1686,12 +1686,16 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_fo }if (type &PERF_SAMPLE_RAW ) { - u.val32[0 ] = sample->raw_size; - *array = u.val64; - array = (void *)array +sizeof (u32); +u32 *array32 = (void *)array; + + *array32 = sample->raw_size; + array32++; + +memcpy (array32, sample->raw_data, sample->raw_size); + array = (void *)(array32 + (sample->raw_size /sizeof (u32))); -memcpy (array, sample->raw_data, sample->raw_size); - array = (void *)array + sample->raw_size; +/* make sure the array is 64-bit aligned */ +BUG_ON (((long )array) %sizeof (u64)); }if (type &PERF_SAMPLE_BRANCH_STACK ) {
Ian Rogers
reviewerQuoted 11 lines · Kan
Quoted 9 lines · Namhyung Kim
The sample data is 64-bit aligned basically but raw data starts with 32-bit length field and data follows. In
perf_event__synthesize_sampleit 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.
Quoted 7 lines
Quoted 5 lines
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 <[email protected]>Signed-off-by Namhyung Kim <[email protected]>Reviewed-by Kan Liang <[email protected]>
nit: The commit message subject is somewhat generic. Perhaps "perf
synthetic-events: Avoid unaligned accesses for raw samples".
Thanks,
Ian