perf machine: Avoid out of bounds LBR memory read
Ian Rogers
reviewerPATCH 1/3 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 e2b23483eb1d ("perf machine: Factor out lbr_callchain_add_lbr_ip()")
Signed-off-by Ian Rogers <[email protected]>
--- tools/perf/util/machine.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
tools/perf/util/machine.c +12 -10
@@ -2622,16 +2622,18 @@ static int lbr_callchain_add_lbr_ip(struct thread *thread,save_lbr_cursor_node (thread, cursor, i); } -/* Add LBR ip from first entries.to */ - ip = entries[0 ].to; - flags = &entries[0 ].flags; - *branch_from = entries[0 ].from; - err =add_callchain_ip (thread, cursor, parent, - root_al, &cpumode, ip, - true, flags,NULL , - *branch_from); -if (err) -return err; +if (lbr_nr >0 ) { +/* Add LBR ip from first entries.to */ + ip = entries[0 ].to; + flags = &entries[0 ].flags; + *branch_from = entries[0 ].from; + err =add_callchain_ip (thread, cursor, parent, + root_al, &cpumode, ip, + true, flags,NULL , + *branch_from); +if (err) +return err; + }return 0 ; }
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH 2/3 libperf rc_check: Make implicit enabling work for GCC
Make the implicit REFCOUNT_CHECKING robust to when building with GCC.
Fixes 9be6ab181b7b ("libperf rc_check: Enable implicitly with sanitizers")
Signed-off-by Ian Rogers <[email protected]>
--- tools/lib/perf/include/internal/rc_check.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/lib/perf/include/internal/rc_check.h +5 -1
@@ -9,8 +9,12 @@* Enable reference count checking implicitly with leak checking, which is * integrated into address sanitizer. */ -#if defined(LEAK_SANITIZER ) || defined(ADDRESS_SANITIZER ) +#if defined(__SANITIZE_ADDRESS__) || defined(LEAK_SANITIZER ) || defined(ADDRESS_SANITIZER )#define REFCNT_CHECKING 1 +#elif defined(__has_feature) +#if __has_feature (address_sanitizer) ||__has_feature (leak_sanitizer) +#define REFCNT_CHECKING 1 +#endif #endif /*
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH 3/3 perf hist: Add missing puts to hist__account_cycles
In reply to Ian RogersCaught using reference count checking on perf top with "--call-graph=lbr". After this no memory leaks were detected.
Fixes 57849998e2cd ("perf report: Add processing for cycle histograms")
Signed-off-by Ian Rogers <[email protected]>
--- tools/perf/util/hist.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
tools/perf/util/hist.c +7 -3
@@ -2676,8 +2676,6 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,/* If we have branch cycles always annotate them. */ if (bs && bs->nr && entries[0 ].flags.cycles) { -int i; - bi =sample__resolve_bstack (sample, al);if (bi) {struct addr_map_symbol *prev =NULL ;
@@ -2692,7 +2690,7 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,* Note that perf stores branches reversed from * program order! */ -for (i = bs->nr -1 ; i >=0 ; i--) { +for (int i = bs->nr -1 ; i >=0 ; i--) {addr_map_symbol__account_cycles (&bi[i].from, nonany_branch_mode ?NULL : prev, bi[i].flags.cycles);
@@ -2701,6 +2699,12 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,if (total_cycles) *total_cycles += bi[i].flags.cycles; } +for (unsigned int i =0 ; i < bs->nr; i++) { +map__put (bi[i].to.ms.map); +maps__put (bi[i].to.ms.maps); +map__put (bi[i].from.ms.map); +maps__put (bi[i].from.ms.maps); + }free (bi); } }
Signature · 1 line
2.42.0.609.gbb76f46606-goog