Skip to contentSkip to conversation

perf tools: Fix segfault in 'perf top'

PATCH Superseded Wang Nan 4 participants 7 messages Sep 11, 2015 – Sep 16, 2015 perf events

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

PATCH 1/1 perf tools: Fix segfault in 'perf top'

'perf top' segfaults with following operation:

 # perf top -e page-faults -p 15173 # 15173 never generate page-fault

Then on the resulting empty interface, press right key:

 # ./perf top -e page-faults -p 11400
 perf: Segmentation fault
 -------- backtrace --------
 ./perf[0x535428]
 /lib64/libc.so.6(+0x3545f)[0x7f0dd360745f]
 ./perf[0x531d46]
 ./perf(perf_evlist__tui_browse_hists+0x96)[0x5340d6]
 ./perf[0x44ba2f]
 /lib64/libpthread.so.0(+0x81d0)[0x7f0dd49dc1d0]
 /lib64/libc.so.6(clone+0x6c)[0x7f0dd36b90dc]

The bug reside in perf_evsel__hists_browse() that, in the above circumstance browser->selection can be NULL, but code after skip_annotation doesn't consider it.

This patch fix it by checing browser->selection before fetching browser->selection->map and browser->selection->sym.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/ui/browsers/hists.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/ui/browsers/hists.c +6 -2
index e4fd40f..b00fa92 100644
@@ -2017,7 +2017,8 @@ skip_annotation:
 					  &options[nr_options], dso);
 		nr_options += add_map_opt(browser, &actions[nr_options],
 					  &options[nr_options],
-					  browser->selection->map);
+					  browser->selection ?
+					  	browser->selection->map : NULL);
 		nr_options += add_socket_opt(browser, &actions[nr_options],
 					     &options[nr_options],
 					     socket);
@@ -2030,7 +2031,10 @@ skip_annotation:
 			nr_options += add_script_opt(browser,
 						     &actions[nr_options],
 						     &options[nr_options],
-						     NULL, browser->selection->sym);
+						     NULL,
+						     browser->selection ?
+						     	browser->selection->sym :
+							NULL);
 		}
 		nr_options += add_script_opt(browser, &actions[nr_options],
 					     &options[nr_options], NULL, NULL);
Signature · 1 line
1.8.3.4
maintainer
#1 lore raw
Quoted 17 lines · Wang Nan

'perf top' segfaults with following operation:

 # perf top -e page-faults -p 15173 # 15173 never generate page-fault

Then on the resulting empty interface, press right key:

 # ./perf top -e page-faults -p 11400
 perf: Segmentation fault
 -------- backtrace --------
 ./perf[0x535428]
 /lib64/libc.so.6(+0x3545f)[0x7f0dd360745f]
 ./perf[0x531d46]
 ./perf(perf_evlist__tui_browse_hists+0x96)[0x5340d6]
 ./perf[0x44ba2f]
 /lib64/libpthread.so.0(+0x81d0)[0x7f0dd49dc1d0]
 /lib64/libc.so.6(clone+0x6c)[0x7f0dd36b90dc]
Quoted 6 lines

The bug reside in perf_evsel__hists_browse() that, in the above circumstance browser->selection can be NULL, but code after skip_annotation doesn't consider it.

This patch fix it by checing browser->selection before fetching browser->selection->map and browser->selection->sym.

I'm ok with this change, just a nitpick below..

Quoted 23 lines
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/ui/browsers/hists.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/ui/browsers/hists.c +2 -1
index e4fd40f..b00fa92 100644
@@ -2017,7 +2017,8 @@ skip_annotation:
 					  &options[nr_options], dso);
 		nr_options += add_map_opt(browser, &actions[nr_options],
 					  &options[nr_options],
-					  browser->selection->map);
+					  browser->selection ?
+					  	browser->selection->map : NULL);
 		nr_options += add_socket_opt(browser, &actions[nr_options],
 					     &options[nr_options],
 					     socket);

tools/perf/ui/browsers/hists.c skip_annotation: · line 2037

Quoted 8 lines
tools/perf/ui/browsers/hists.c +4 -1
@@ -2030,7 +2031,10 @@ skip_annotation:
 			nr_options += add_script_opt(browser,
 						     &actions[nr_options],
 						     &options[nr_options],
-						     NULL, browser->selection->sym);
+						     NULL,
+						     browser->selection ?
+						     	browser->selection->sym :
+							NULL);

Is this really needed? IIUC browser->selection is not NULL if browser->he_selection is not NULL. Did you see a case it's NULL? Also passing a NULL thread and a NULL sym is meaningless here since it's a duplication..

Thanks,
Namhyung

Quoted 6 lines
tools/perf/ui/browsers/hists.c +0 -0
 		}
 		nr_options += add_script_opt(browser, &actions[nr_options],
 					     &options[nr_options], NULL, NULL);
Signature · 1 line
1.8.3.4
#2 lore raw
Quoted 53 lines · Namhyung Kim
Quoted 17 lines · Wang Nan

'perf top' segfaults with following operation:

  # perf top -e page-faults -p 15173 # 15173 never generate page-fault

Then on the resulting empty interface, press right key:

  # ./perf top -e page-faults -p 11400
  perf: Segmentation fault
  -------- backtrace --------
  ./perf[0x535428]
  /lib64/libc.so.6(+0x3545f)[0x7f0dd360745f]
  ./perf[0x531d46]
  ./perf(perf_evlist__tui_browse_hists+0x96)[0x5340d6]
  ./perf[0x44ba2f]
  /lib64/libpthread.so.0(+0x81d0)[0x7f0dd49dc1d0]
  /lib64/libc.so.6(clone+0x6c)[0x7f0dd36b90dc]
Quoted 6 lines

The bug reside in perf_evsel__hists_browse() that, in the above circumstance browser->selection can be NULL, but code after skip_annotation doesn't consider it.

This patch fix it by checing browser->selection before fetching browser->selection->map and browser->selection->sym.

I'm ok with this change, just a nitpick below..

Quoted 26 lines
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Namhyung Kim <[email protected]>
---
  tools/perf/ui/browsers/hists.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/ui/browsers/hists.c +2 -2
index e4fd40f..b00fa92 100644
@@ -2017,7 +2017,8 @@ skip_annotation:
  					  &options[nr_options], dso);
  		nr_options += add_map_opt(browser, &actions[nr_options],
  					  &options[nr_options],
-					  browser->selection->map);
+					  browser->selection ?
+					  	browser->selection->map : NULL);
  		nr_options += add_socket_opt(browser, &actions[nr_options],
  					     &options[nr_options],
  					     socket);
@@ -2030,7 +2031,10 @@ skip_annotation:
  			nr_options += add_script_opt(browser,
  						     &actions[nr_options],
  						     &options[nr_options],
-						     NULL, browser->selection->sym);
Quoted 8 lines

tools/perf/ui/browsers/hists.c skip_annotation: · line 2037

Quoted 4 lines
tools/perf/ui/browsers/hists.c +4 -0
+						     NULL,
+						     browser->selection ?
+						     	browser->selection->sym :
+							NULL);

Is this really needed? IIUC browser->selection is not NULL if browser->he_selection is not NULL. Did you see a case it's NULL? Also passing a NULL thread and a NULL sym is meaningless here since it's a duplication..

Actually I didn't see the case when browser->he_selection is not NULL but browser->selection is NULL. I add the testing only for safety reason because we have

        if (browser->selection == NULL)
            goto skip_annotation;

So I guess we'd better to check browser->selection in the codeblock leading by skip_annotation, so if someone breaks the assumption the code still work.

But yes, we can add the checker back when the assumption *does* be broken. Currently let's add a comment here for code readers.

Please have a look for v2 patch.

Thank you.

Quoted 10 lines

Thanks,
Namhyung

Quoted 6 lines
tools/perf/ui/browsers/hists.c +0 -0
  		}
  		nr_options += add_script_opt(browser, &actions[nr_options],
  					     &options[nr_options], NULL, NULL);
Signature · 1 line
1.8.3.4
author
#3 lore raw

PATCH v2 1/1 perf tools: Fix segfault in 'perf top'

In reply to Namhyung Kim

'perf top' segfaults with following operation:

 # perf top -e page-faults -p 11400 # 11400 never generates page-fault

Then on the resulting empty interface, press right key:

 # ./perf top -e page-faults -p 11400
 perf: Segmentation fault
 -------- backtrace --------
 ./perf[0x535428]
 /lib64/libc.so.6(+0x3545f)[0x7f0dd360745f]
 ./perf[0x531d46]
 ./perf(perf_evlist__tui_browse_hists+0x96)[0x5340d6]
 ./perf[0x44ba2f]
 /lib64/libpthread.so.0(+0x81d0)[0x7f0dd49dc1d0]
 /lib64/libc.so.6(clone+0x6c)[0x7f0dd36b90dc]

The bug reside in perf_evsel__hists_browse() that, in the above circumstance browser->selection can be NULL, but code after skip_annotation doesn't consider it.

This patch fix it by checking browser->selection before fetching browser->selection->map.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/ui/browsers/hists.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/perf/ui/browsers/hists.c +11 -1
index e4fd40f..182b87a 100644
@@ -2017,7 +2017,8 @@ skip_annotation:
 					  &options[nr_options], dso);
 		nr_options += add_map_opt(browser, &actions[nr_options],
 					  &options[nr_options],
-					  browser->selection->map);
+					  browser->selection ?
+					      browser->selection->map : NULL);
 		nr_options += add_socket_opt(browser, &actions[nr_options],
 					     &options[nr_options],
 					     socket);
@@ -2027,6 +2028,15 @@ skip_annotation:
 						     &actions[nr_options],
 						     &options[nr_options],
 						     thread, NULL);
+			/*
+			 * Note that browser->selection != NULL
+			 * when browser->he_selection is not NULL,
+			 * so we don't need to check browser->selection
+			 * before fetching browser->selection->sym like what
+			 * we do before fetching browser->selection->map.
+			 *
+			 * See hist_browser__show_entry.
+			 */
 			nr_options += add_script_opt(browser,
 						     &actions[nr_options],
 						     &options[nr_options],
Signature · 1 line
1.8.3.4
#4 lore raw

Em Mon, Sep 14, 2015 at 10:23:55AM +0000, Wang Nan escreveu:

Quoted 5 lines

'perf top' segfaults with following operation:

 # perf top -e page-faults -p 11400 # 11400 never generates page-fault

Then on the resulting empty interface, press right key:

So, this happens in perf/urgent, so we need to apply it there first, which this patch doesn't.

I am fixing it up this time, thanks for the patch!

- Arnaldo

Quoted 57 lines
 # ./perf top -e page-faults -p 11400
 perf: Segmentation fault
 -------- backtrace --------
 ./perf[0x535428]
 /lib64/libc.so.6(+0x3545f)[0x7f0dd360745f]
 ./perf[0x531d46]
 ./perf(perf_evlist__tui_browse_hists+0x96)[0x5340d6]
 ./perf[0x44ba2f]
 /lib64/libpthread.so.0(+0x81d0)[0x7f0dd49dc1d0]
 /lib64/libc.so.6(clone+0x6c)[0x7f0dd36b90dc]

The bug reside in perf_evsel__hists_browse() that, in the above circumstance browser->selection can be NULL, but code after skip_annotation doesn't consider it.

This patch fix it by checking browser->selection before fetching browser->selection->map.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/ui/browsers/hists.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/perf/ui/browsers/hists.c +11 -1
index e4fd40f..182b87a 100644
@@ -2017,7 +2017,8 @@ skip_annotation:
 					  &options[nr_options], dso);
 		nr_options += add_map_opt(browser, &actions[nr_options],
 					  &options[nr_options],
-					  browser->selection->map);
+					  browser->selection ?
+					      browser->selection->map : NULL);
 		nr_options += add_socket_opt(browser, &actions[nr_options],
 					     &options[nr_options],
 					     socket);
@@ -2027,6 +2028,15 @@ skip_annotation:
 						     &actions[nr_options],
 						     &options[nr_options],
 						     thread, NULL);
+			/*
+			 * Note that browser->selection != NULL
+			 * when browser->he_selection is not NULL,
+			 * so we don't need to check browser->selection
+			 * before fetching browser->selection->sym like what
+			 * we do before fetching browser->selection->map.
+			 *
+			 * See hist_browser__show_entry.
+			 */
 			nr_options += add_script_opt(browser,
 						     &actions[nr_options],
 						     &options[nr_options],
Signature · 1 line
1.8.3.4
#5 lore raw

Em Mon, Sep 14, 2015 at 03:07:14PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 11 lines

Em Mon, Sep 14, 2015 at 10:23:55AM +0000, Wang Nan escreveu:

Quoted 5 lines

'perf top' segfaults with following operation:

 # perf top -e page-faults -p 11400 # 11400 never generates page-fault

Then on the resulting empty interface, press right key:

So, this happens in perf/urgent, so we need to apply it there first, which this patch doesn't.

I am fixing it up this time, thanks for the patch!

For reference, below is the resulting patch, pushed to acme/perf/urgent, now checking if there are more patches to go together with this one...

- Arnaldo

tools/perf/ui/browsers/hists.c +11 -1
index cf86f2d3a5e7..c04c60d4863c 100644
@@ -1968,7 +1968,8 @@ skip_annotation:
 					  &options[nr_options], dso);
 		nr_options += add_map_opt(browser, &actions[nr_options],
 					  &options[nr_options],
-					  browser->selection->map);
+					  browser->selection ?
+						browser->selection->map : NULL);
 
 		/* perf script support */
 		if (browser->he_selection) {
@@ -1976,6 +1977,15 @@ skip_annotation:
 						     &actions[nr_options],
 						     &options[nr_options],
 						     thread, NULL);
+			/*
+			 * Note that browser->selection != NULL
+			 * when browser->he_selection is not NULL,
+			 * so we don't need to check browser->selection
+			 * before fetching browser->selection->sym like what
+			 * we do before fetching browser->selection->map.
+			 *
+			 * See hist_browser__show_entry.
+			 */
 			nr_options += add_script_opt(browser,
 						     &actions[nr_options],
 						     &options[nr_options],
#6 lore raw

[tip:perf/core] perf top: Fix segfault pressing -> with no hist entries

In reply to Wang Nan
perf top: Fix segfault pressing -> with no hist entries
Commit-ID:  bd315aab8a3ab1bc7074774b89a5d8ec7c1ff7ab
Gitweb:     http://git.kernel.org/tip/bd315aab8a3ab1bc7074774b89a5d8ec7c1ff7ab
Author:     Wang Nan <[email protected]>
AuthorDate: Mon, 14 Sep 2015 10:23:55 +0000
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Mon, 14 Sep 2015 15:10:41 -0300

perf top: Fix segfault pressing -> with no hist entries

'perf top' segfaults with following operation:

 # perf top -e page-faults -p 11400 # 11400 never generates page-fault

Then on the resulting empty interface, press right key:

  # ./perf top -e page-faults -p 11400
  perf: Segmentation fault
  -------- backtrace --------
  ./perf[0x535428]
  /lib64/libc.so.6(+0x3545f)[0x7f0dd360745f]
  ./perf[0x531d46]
  ./perf(perf_evlist__tui_browse_hists+0x96)[0x5340d6]
  ./perf[0x44ba2f]
  /lib64/libpthread.so.0(+0x81d0)[0x7f0dd49dc1d0]
  /lib64/libc.so.6(clone+0x6c)[0x7f0dd36b90dc]

The bug resides in perf_evsel__hists_browse() that, in the above circumstance browser->selection can be NULL, but code after skip_annotation doesn't consider it.

This patch fix it by checking browser->selection before fetching browser->selection->map.

---
 tools/perf/ui/browsers/hists.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/perf/ui/browsers/hists.c +11 -1
index cf86f2d..c04c60d 100644
@@ -1968,7 +1968,8 @@ skip_annotation:
 					  &options[nr_options], dso);
 		nr_options += add_map_opt(browser, &actions[nr_options],
 					  &options[nr_options],
-					  browser->selection->map);
+					  browser->selection ?
+						browser->selection->map : NULL);
 
 		/* perf script support */
 		if (browser->he_selection) {
@@ -1976,6 +1977,15 @@ skip_annotation:
 						     &actions[nr_options],
 						     &options[nr_options],
 						     thread, NULL);
+			/*
+			 * Note that browser->selection != NULL
+			 * when browser->he_selection is not NULL,
+			 * so we don't need to check browser->selection
+			 * before fetching browser->selection->sym like what
+			 * we do before fetching browser->selection->map.
+			 *
+			 * See hist_browser__show_entry.
+			 */
 			nr_options += add_script_opt(browser,
 						     &actions[nr_options],
 						     &options[nr_options],