perf synthetic-events: Ensure features are aligned
Ian Rogers
reviewerPATCH 1/3 perf synthetic-events: Ensure features are aligned
Features like hostname have arbitrary size and break the assumed 8-byte alignment of perf events. Pad all feature events until 8-byte alignment is restored.
--- tools/perf/util/synthetic-events.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/perf/util/synthetic-events.c +5 -0
@@ -2401,6 +2401,11 @@ int perf_event__synthesize_features(const struct perf_tool *tool, struct perf_sepr_debug ("Error writing feature\n" );continue ; } +while (PERF_ALIGN (ff.offset,sizeof (u64)) != ff.offset) { +char c =0 ; + +do_write (&ff, &c,1 ); + }/* ff.buf may have changed due to realloc in do_write() */ fe = ff.buf;memset (fe,0 ,sizeof (*fe));
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Ian Rogers
reviewerPATCH 2/3 perf machine: Avoid UB by delaying computing branch entries
If the branch_stack is NULL then perf_sample__branch_entries may return NULL+1 which triggers ubsan (undefined behavior sanitizer). Avoid this by making the computation conditional on branch existing.
--- tools/perf/util/machine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/machine.c +1 -1
@@ -2670,7 +2670,6 @@ static int thread__resolve_callchain_sample(struct thread *thread, bool symbols) {struct branch_stack *branch = sample->branch_stack; -struct branch_entry *entries =perf_sample__branch_entries (sample);struct ip_callchain *chain = sample->callchain;int chain_nr =0 ;u8 cpumode =PERF_RECORD_MISC_USER ;
@@ -2712,6 +2711,7 @@ static int thread__resolve_callchain_sample(struct thread *thread,*/ if (branch && callchain_param.branch_callstack) { +struct branch_entry *entries =perf_sample__branch_entries (sample);int nr =min (max_stack, (int )branch->nr);struct branch_entry be[nr];struct iterations iter[nr];
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Ian Rogers
reviewerPATCH 3/3 perf record: Assert synthesized events are 8-byte aligned
In reply to Ian RogersCapture that events are 8-byte aligned and avoid later misaligned event problems.
--- tools/perf/builtin-record.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/builtin-record.c +4 -1
@@ -624,7 +624,10 @@ static int process_synthesized_event(const struct perf_tool *tool, struct machine *machine __maybe_unused) { struct record *rec = container_of(tool, struct record, tool); -return record__write (rec,NULL , event, event->header.size); +size_t size = event->header.size; + +assert (PERF_ALIGN (size,sizeof (u64)) == size); +return record__write (rec,NULL , event, size); }static struct mutex synth_lock;
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Ian Rogers
reviewerQuoted 6 lines · Ian Rogers
Features like hostname have arbitrary size and break the assumed 8-byte alignment of perf events. Pad all feature events until 8-byte alignment is restored.
Signed-off-by Ian Rogers <[email protected]>
This change breaks pipe mode tests for me on ARM. I've not investigated why yet.
Thanks,
Ian
Quoted 23 lines
--- tools/perf/util/synthetic-events.c | 5 +++++ 1 file changed, 5 insertions(+)tools/perf/util/synthetic-events.c +5
-1@@ -2401,6 +2401,11 @@ int perf_event__synthesize_features(const struct perf_tool *tool, struct perf_sepr_debug ("Error writing feature\n" );continue ; } +while (PERF_ALIGN (ff.offset,sizeof (u64)) != ff.offset) { +char c =0 ; + +do_write (&ff, &c,1 ); + }/* ff.buf may have changed due to realloc in do_write() */ fe = ff.buf;memset (fe,0 ,sizeof (*fe)); --2.47.1.613.gc27f4b7a9f-goog