Skip to content

Commit ab8ce15

Browse files
captain5050namhyung
authored andcommitted
perf machine: Avoid out of bounds LBR memory read
Running perf top with address sanitizer and "--call-graph=lbr" fails due to reading sample 0 when no samples exist. Add a guard to prevent this. Fixes: e2b2348 ("perf machine: Factor out lbr_callchain_add_lbr_ip()") Signed-off-by: Ian Rogers <irogers@google.com> Cc: K Prateek Nayak <kprateek.nayak@amd.com> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Sandipan Das <sandipan.das@amd.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: German Gomez <german.gomez@arm.com> Cc: James Clark <james.clark@arm.com> Cc: Nick Terrell <terrelln@fb.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Changbin Du <changbin.du@huawei.com> Cc: liuwenyu <liuwenyu7@huawei.com> Cc: Yang Jihong <yangjihong1@huawei.com> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Song Liu <song@kernel.org> Cc: Leo Yan <leo.yan@linaro.org> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Cc: Yanteng Si <siyanteng@loongson.cn> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Link: https://lore.kernel.org/r/20231024222353.3024098-3-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 7a8f349 commit ab8ce15

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

tools/perf/util/machine.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,16 +2622,18 @@ static int lbr_callchain_add_lbr_ip(struct thread *thread,
26222622
save_lbr_cursor_node(thread, cursor, i);
26232623
}
26242624

2625-
/* Add LBR ip from first entries.to */
2626-
ip = entries[0].to;
2627-
flags = &entries[0].flags;
2628-
*branch_from = entries[0].from;
2629-
err = add_callchain_ip(thread, cursor, parent,
2630-
root_al, &cpumode, ip,
2631-
true, flags, NULL,
2632-
*branch_from);
2633-
if (err)
2634-
return err;
2625+
if (lbr_nr > 0) {
2626+
/* Add LBR ip from first entries.to */
2627+
ip = entries[0].to;
2628+
flags = &entries[0].flags;
2629+
*branch_from = entries[0].from;
2630+
err = add_callchain_ip(thread, cursor, parent,
2631+
root_al, &cpumode, ip,
2632+
true, flags, NULL,
2633+
*branch_from);
2634+
if (err)
2635+
return err;
2636+
}
26352637

26362638
return 0;
26372639
}

0 commit comments

Comments
 (0)