2019-07-08 19:27:11

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 0/4] perf: Fix errors detected by Smatch

Since Arnaldo has picked up several patches from patch set v1 and have
left four patches which are needed to be refined based on the feedback.
So this is patch set v2 which contains the rest four patches with
addressed the comments and suggestions.

Changes from v1:
* Added WARN_ON_ONCE(!hbt) in ui/browsers/hists.c (Jiri)
* Removed NULL test for 'session->itrace_synth_opts (Adrian)

Leo Yan (4):
perf hists: Smatch: Fix potential NULL pointer dereference
perf intel-bts: Smatch: Fix potential NULL pointer dereference
perf intel-pt: Smatch: Fix potential NULL pointer dereference
perf cs-etm: Smatch: Fix potential NULL pointer dereference

tools/perf/ui/browsers/hists.c | 15 +++++++++++----
tools/perf/util/cs-etm.c | 2 +-
tools/perf/util/intel-bts.c | 5 ++---
tools/perf/util/intel-pt.c | 13 +++++--------
4 files changed, 19 insertions(+), 16 deletions(-)

--
2.17.1


2019-07-08 19:27:23

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 1/4] perf hists: Smatch: Fix potential NULL pointer dereference

Based on the following report from Smatch, fix the potential
NULL pointer dereference check.

tools/perf/ui/browsers/hists.c:641
hist_browser__run() error: we previously assumed 'hbt' could be
null (see line 625)

tools/perf/ui/browsers/hists.c:3088
perf_evsel__hists_browse() error: we previously assumed
'browser->he_selection' could be null (see line 2902)

tools/perf/ui/browsers/hists.c:3272
perf_evsel_menu__run() error: we previously assumed 'hbt' could be
null (see line 3260)

This patch firstly validating the pointers before access them, so can
fix potential NULL pointer dereference.

Signed-off-by: Leo Yan <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
---
tools/perf/ui/browsers/hists.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c
index 85581cfb9112..a94eb0755e8b 100644
--- a/tools/perf/ui/browsers/hists.c
+++ b/tools/perf/ui/browsers/hists.c
@@ -639,7 +639,11 @@ int hist_browser__run(struct hist_browser *browser, const char *help,
switch (key) {
case K_TIMER: {
u64 nr_entries;
- hbt->timer(hbt->arg);
+
+ WARN_ON_ONCE(!hbt);
+
+ if (hbt)
+ hbt->timer(hbt->arg);

if (hist_browser__has_filter(browser) ||
symbol_conf.report_hierarchy)
@@ -2821,7 +2825,7 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
{
struct hists *hists = evsel__hists(evsel);
struct hist_browser *browser = perf_evsel_browser__new(evsel, hbt, env, annotation_opts);
- struct branch_info *bi;
+ struct branch_info *bi = NULL;
#define MAX_OPTIONS 16
char *options[MAX_OPTIONS];
struct popup_action actions[MAX_OPTIONS];
@@ -3087,7 +3091,9 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
goto skip_annotation;

if (sort__mode == SORT_MODE__BRANCH) {
- bi = browser->he_selection->branch_info;
+
+ if (browser->he_selection)
+ bi = browser->he_selection->branch_info;

if (bi == NULL)
goto skip_annotation;
@@ -3271,7 +3277,8 @@ static int perf_evsel_menu__run(struct perf_evsel_menu *menu,

switch (key) {
case K_TIMER:
- hbt->timer(hbt->arg);
+ if (hbt)
+ hbt->timer(hbt->arg);

if (!menu->lost_events_warned &&
menu->lost_events &&
--
2.17.1

2019-07-08 19:27:30

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 2/4] perf intel-bts: Smatch: Fix potential NULL pointer dereference

Based on the following report from Smatch, fix the potential
NULL pointer dereference check.

tools/perf/util/intel-bts.c:898
intel_bts_process_auxtrace_info() error: we previously assumed
'session->itrace_synth_opts' could be null (see line 894)

tools/perf/util/intel-bts.c:899
intel_bts_process_auxtrace_info() warn: variable dereferenced before
check 'session->itrace_synth_opts' (see line 898)

tools/perf/util/intel-bts.c
894 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
895 bts->synth_opts = *session->itrace_synth_opts;
896 } else {
897 itrace_synth_opts__set_default(&bts->synth_opts,
898 session->itrace_synth_opts->default_no_sample);
^^^^^^^^^^^^^^^^^^^^^^^^^^
899 if (session->itrace_synth_opts)
^^^^^^^^^^^^^^^^^^^^^^^^^^
900 bts->synth_opts.thread_stack =
901 session->itrace_synth_opts->thread_stack;
902 }

'session->itrace_synth_opts' is impossible to be a NULL pointer in
intel_bts_process_auxtrace_info(), thus this patch removes the NULL
test for 'session->itrace_synth_opts'.

Signed-off-by: Leo Yan <[email protected]>
---
tools/perf/util/intel-bts.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c
index 5a21bcdb8ef7..5560e95afdda 100644
--- a/tools/perf/util/intel-bts.c
+++ b/tools/perf/util/intel-bts.c
@@ -891,13 +891,12 @@ int intel_bts_process_auxtrace_info(union perf_event *event,
if (dump_trace)
return 0;

- if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
+ if (session->itrace_synth_opts->set) {
bts->synth_opts = *session->itrace_synth_opts;
} else {
itrace_synth_opts__set_default(&bts->synth_opts,
session->itrace_synth_opts->default_no_sample);
- if (session->itrace_synth_opts)
- bts->synth_opts.thread_stack =
+ bts->synth_opts.thread_stack =
session->itrace_synth_opts->thread_stack;
}

--
2.17.1

2019-07-08 19:27:36

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 3/4] perf intel-pt: Smatch: Fix potential NULL pointer dereference

Based on the following report from Smatch, fix the potential
NULL pointer dereference check.

tools/perf/util/intel-pt.c:3200
intel_pt_process_auxtrace_info() error: we previously assumed
'session->itrace_synth_opts' could be null (see line 3196)

tools/perf/util/intel-pt.c:3206
intel_pt_process_auxtrace_info() warn: variable dereferenced before
check 'session->itrace_synth_opts' (see line 3200)

tools/perf/util/intel-pt.c
3196 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
3197 pt->synth_opts = *session->itrace_synth_opts;
3198 } else {
3199 itrace_synth_opts__set_default(&pt->synth_opts,
3200 session->itrace_synth_opts->default_no_sample);
^^^^^^^^^^^^^^^^^^^^^^^^^^
3201 if (!session->itrace_synth_opts->default_no_sample &&
3202 !session->itrace_synth_opts->inject) {
3203 pt->synth_opts.branches = false;
3204 pt->synth_opts.callchain = true;
3205 }
3206 if (session->itrace_synth_opts)
^^^^^^^^^^^^^^^^^^^^^^^^^^
3207 pt->synth_opts.thread_stack =
3208 session->itrace_synth_opts->thread_stack;
3209 }

'session->itrace_synth_opts' is impossible to be a NULL pointer in
intel_pt_process_auxtrace_info(), thus this patch removes the NULL
test for 'session->itrace_synth_opts'.

Signed-off-by: Leo Yan <[email protected]>
---
tools/perf/util/intel-pt.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index c76a96f777fb..df061599fef4 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -3210,7 +3210,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
goto err_delete_thread;
}

- if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
+ if (session->itrace_synth_opts->set) {
pt->synth_opts = *session->itrace_synth_opts;
} else {
itrace_synth_opts__set_default(&pt->synth_opts,
@@ -3220,8 +3220,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
pt->synth_opts.branches = false;
pt->synth_opts.callchain = true;
}
- if (session->itrace_synth_opts)
- pt->synth_opts.thread_stack =
+ pt->synth_opts.thread_stack =
session->itrace_synth_opts->thread_stack;
}

@@ -3241,11 +3240,9 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
}

- if (session->itrace_synth_opts) {
- err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
- if (err)
- goto err_delete_thread;
- }
+ err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
+ if (err)
+ goto err_delete_thread;

if (pt->synth_opts.calls)
pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
--
2.17.1

2019-07-08 19:27:36

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 4/4] perf cs-etm: Smatch: Fix potential NULL pointer dereference

Based on the following report from Smatch, fix the potential
NULL pointer dereference check.

tools/perf/util/cs-etm.c:2545
cs_etm__process_auxtrace_info() error: we previously assumed
'session->itrace_synth_opts' could be null (see line 2541)

tools/perf/util/cs-etm.c
2541 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2542 etm->synth_opts = *session->itrace_synth_opts;
2543 } else {
2544 itrace_synth_opts__set_default(&etm->synth_opts,
2545 session->itrace_synth_opts->default_no_sample);
^^^^^^^^^^^^^^^^^^^^^^^^^^
2546 etm->synth_opts.callchain = false;
2547 }

'session->itrace_synth_opts' is impossible to be a NULL pointer in
cs_etm__process_auxtrace_info(), thus this patch removes the NULL
test for 'session->itrace_synth_opts'.

Signed-off-by: Leo Yan <[email protected]>
---
tools/perf/util/cs-etm.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index ad43a6e31827..ab578a06a790 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -2537,7 +2537,7 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
return 0;
}

- if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
+ if (session->itrace_synth_opts->set) {
etm->synth_opts = *session->itrace_synth_opts;
} else {
itrace_synth_opts__set_default(&etm->synth_opts,
--
2.17.1

2019-07-08 22:44:56

by Mathieu Poirier

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] perf cs-etm: Smatch: Fix potential NULL pointer dereference

On Mon, 8 Jul 2019 at 08:40, Leo Yan <[email protected]> wrote:
>
> Based on the following report from Smatch, fix the potential
> NULL pointer dereference check.
>
> tools/perf/util/cs-etm.c:2545
> cs_etm__process_auxtrace_info() error: we previously assumed
> 'session->itrace_synth_opts' could be null (see line 2541)
>
> tools/perf/util/cs-etm.c
> 2541 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
> 2542 etm->synth_opts = *session->itrace_synth_opts;
> 2543 } else {
> 2544 itrace_synth_opts__set_default(&etm->synth_opts,
> 2545 session->itrace_synth_opts->default_no_sample);
> ^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2546 etm->synth_opts.callchain = false;
> 2547 }
>
> 'session->itrace_synth_opts' is impossible to be a NULL pointer in
> cs_etm__process_auxtrace_info(), thus this patch removes the NULL
> test for 'session->itrace_synth_opts'.
>
> Signed-off-by: Leo Yan <[email protected]>
> ---
> tools/perf/util/cs-etm.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
> index ad43a6e31827..ab578a06a790 100644
> --- a/tools/perf/util/cs-etm.c
> +++ b/tools/perf/util/cs-etm.c
> @@ -2537,7 +2537,7 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
> return 0;
> }
>
> - if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
> + if (session->itrace_synth_opts->set) {
> etm->synth_opts = *session->itrace_synth_opts;
> } else {
> itrace_synth_opts__set_default(&etm->synth_opts,

This is in accordance with what was previously discussed.

Reviewed-by: Mathieu Poirier <[email protected]>

> --
> 2.17.1
>

2019-07-08 22:52:50

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] perf cs-etm: Smatch: Fix potential NULL pointer dereference

Em Mon, Jul 08, 2019 at 11:38:48AM -0600, Mathieu Poirier escreveu:
> On Mon, 8 Jul 2019 at 08:40, Leo Yan <[email protected]> wrote:
> >
> > Based on the following report from Smatch, fix the potential
> > NULL pointer dereference check.
> >
> > tools/perf/util/cs-etm.c:2545
> > cs_etm__process_auxtrace_info() error: we previously assumed
> > 'session->itrace_synth_opts' could be null (see line 2541)
> >
> > tools/perf/util/cs-etm.c
> > 2541 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
> > 2542 etm->synth_opts = *session->itrace_synth_opts;
> > 2543 } else {
> > 2544 itrace_synth_opts__set_default(&etm->synth_opts,
> > 2545 session->itrace_synth_opts->default_no_sample);
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^
> > 2546 etm->synth_opts.callchain = false;
> > 2547 }
> >
> > 'session->itrace_synth_opts' is impossible to be a NULL pointer in
> > cs_etm__process_auxtrace_info(), thus this patch removes the NULL
> > test for 'session->itrace_synth_opts'.
> >
> > Signed-off-by: Leo Yan <[email protected]>
> > ---
> > tools/perf/util/cs-etm.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
> > index ad43a6e31827..ab578a06a790 100644
> > --- a/tools/perf/util/cs-etm.c
> > +++ b/tools/perf/util/cs-etm.c
> > @@ -2537,7 +2537,7 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
> > return 0;
> > }
> >
> > - if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
> > + if (session->itrace_synth_opts->set) {
> > etm->synth_opts = *session->itrace_synth_opts;
> > } else {
> > itrace_synth_opts__set_default(&etm->synth_opts,
>
> This is in accordance with what was previously discussed.
>
> Reviewed-by: Mathieu Poirier <[email protected]>

Thanks, applied.

- Arnaldo

2019-07-08 22:54:18

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] perf intel-pt: Smatch: Fix potential NULL pointer dereference

Em Mon, Jul 08, 2019 at 10:39:36PM +0800, Leo Yan escreveu:
> Based on the following report from Smatch, fix the potential
> NULL pointer dereference check.

Adrian, are you ok now with these two for pt and bts? Can I have your
acked-by?

- Arnaldo

> tools/perf/util/intel-pt.c:3200
> intel_pt_process_auxtrace_info() error: we previously assumed
> 'session->itrace_synth_opts' could be null (see line 3196)
>
> tools/perf/util/intel-pt.c:3206
> intel_pt_process_auxtrace_info() warn: variable dereferenced before
> check 'session->itrace_synth_opts' (see line 3200)
>
> tools/perf/util/intel-pt.c
> 3196 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
> 3197 pt->synth_opts = *session->itrace_synth_opts;
> 3198 } else {
> 3199 itrace_synth_opts__set_default(&pt->synth_opts,
> 3200 session->itrace_synth_opts->default_no_sample);
> ^^^^^^^^^^^^^^^^^^^^^^^^^^
> 3201 if (!session->itrace_synth_opts->default_no_sample &&
> 3202 !session->itrace_synth_opts->inject) {
> 3203 pt->synth_opts.branches = false;
> 3204 pt->synth_opts.callchain = true;
> 3205 }
> 3206 if (session->itrace_synth_opts)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^
> 3207 pt->synth_opts.thread_stack =
> 3208 session->itrace_synth_opts->thread_stack;
> 3209 }
>
> 'session->itrace_synth_opts' is impossible to be a NULL pointer in
> intel_pt_process_auxtrace_info(), thus this patch removes the NULL
> test for 'session->itrace_synth_opts'.
>
> Signed-off-by: Leo Yan <[email protected]>
> ---
> tools/perf/util/intel-pt.c | 13 +++++--------
> 1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
> index c76a96f777fb..df061599fef4 100644
> --- a/tools/perf/util/intel-pt.c
> +++ b/tools/perf/util/intel-pt.c
> @@ -3210,7 +3210,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
> goto err_delete_thread;
> }
>
> - if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
> + if (session->itrace_synth_opts->set) {
> pt->synth_opts = *session->itrace_synth_opts;
> } else {
> itrace_synth_opts__set_default(&pt->synth_opts,
> @@ -3220,8 +3220,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
> pt->synth_opts.branches = false;
> pt->synth_opts.callchain = true;
> }
> - if (session->itrace_synth_opts)
> - pt->synth_opts.thread_stack =
> + pt->synth_opts.thread_stack =
> session->itrace_synth_opts->thread_stack;
> }
>
> @@ -3241,11 +3240,9 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
> pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
> }
>
> - if (session->itrace_synth_opts) {
> - err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
> - if (err)
> - goto err_delete_thread;
> - }
> + err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
> + if (err)
> + goto err_delete_thread;
>
> if (pt->synth_opts.calls)
> pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
> --
> 2.17.1

--

- Arnaldo

2019-07-09 11:39:06

by Adrian Hunter

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] perf intel-pt: Smatch: Fix potential NULL pointer dereference

On 9/07/19 12:59 AM, Arnaldo Carvalho de Melo wrote:
> Em Mon, Jul 08, 2019 at 10:39:36PM +0800, Leo Yan escreveu:
>> Based on the following report from Smatch, fix the potential
>> NULL pointer dereference check.
>
> Adrian, are you ok now with these two for pt and bts? Can I have your
> acked-by?

Yes they are good. For both:

Acked-by: Adrian Hunter <[email protected]>

>
> - Arnaldo
>
>> tools/perf/util/intel-pt.c:3200
>> intel_pt_process_auxtrace_info() error: we previously assumed
>> 'session->itrace_synth_opts' could be null (see line 3196)
>>
>> tools/perf/util/intel-pt.c:3206
>> intel_pt_process_auxtrace_info() warn: variable dereferenced before
>> check 'session->itrace_synth_opts' (see line 3200)
>>
>> tools/perf/util/intel-pt.c
>> 3196 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
>> 3197 pt->synth_opts = *session->itrace_synth_opts;
>> 3198 } else {
>> 3199 itrace_synth_opts__set_default(&pt->synth_opts,
>> 3200 session->itrace_synth_opts->default_no_sample);
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^
>> 3201 if (!session->itrace_synth_opts->default_no_sample &&
>> 3202 !session->itrace_synth_opts->inject) {
>> 3203 pt->synth_opts.branches = false;
>> 3204 pt->synth_opts.callchain = true;
>> 3205 }
>> 3206 if (session->itrace_synth_opts)
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^
>> 3207 pt->synth_opts.thread_stack =
>> 3208 session->itrace_synth_opts->thread_stack;
>> 3209 }
>>
>> 'session->itrace_synth_opts' is impossible to be a NULL pointer in
>> intel_pt_process_auxtrace_info(), thus this patch removes the NULL
>> test for 'session->itrace_synth_opts'.
>>
>> Signed-off-by: Leo Yan <[email protected]>
>> ---
>> tools/perf/util/intel-pt.c | 13 +++++--------
>> 1 file changed, 5 insertions(+), 8 deletions(-)
>>
>> diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
>> index c76a96f777fb..df061599fef4 100644
>> --- a/tools/perf/util/intel-pt.c
>> +++ b/tools/perf/util/intel-pt.c
>> @@ -3210,7 +3210,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
>> goto err_delete_thread;
>> }
>>
>> - if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
>> + if (session->itrace_synth_opts->set) {
>> pt->synth_opts = *session->itrace_synth_opts;
>> } else {
>> itrace_synth_opts__set_default(&pt->synth_opts,
>> @@ -3220,8 +3220,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
>> pt->synth_opts.branches = false;
>> pt->synth_opts.callchain = true;
>> }
>> - if (session->itrace_synth_opts)
>> - pt->synth_opts.thread_stack =
>> + pt->synth_opts.thread_stack =
>> session->itrace_synth_opts->thread_stack;
>> }
>>
>> @@ -3241,11 +3240,9 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
>> pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
>> }
>>
>> - if (session->itrace_synth_opts) {
>> - err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
>> - if (err)
>> - goto err_delete_thread;
>> - }
>> + err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
>> + if (err)
>> + goto err_delete_thread;
>>
>> if (pt->synth_opts.calls)
>> pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
>> --
>> 2.17.1
>

Subject: [tip:perf/urgent] perf cs-etm: Fix potential NULL pointer dereference found by the smatch tool

Commit-ID: 0702f23c983b8a921853c33a9f59b9cf0975d36e
Gitweb: https://git.kernel.org/tip/0702f23c983b8a921853c33a9f59b9cf0975d36e
Author: Leo Yan <[email protected]>
AuthorDate: Mon, 8 Jul 2019 22:39:37 +0800
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 9 Jul 2019 10:13:27 -0300

perf cs-etm: Fix potential NULL pointer dereference found by the smatch tool

Based on the following report from Smatch, fix the potential NULL
pointer dereference check.

tools/perf/util/cs-etm.c:2545
cs_etm__process_auxtrace_info() error: we previously assumed
'session->itrace_synth_opts' could be null (see line 2541)

tools/perf/util/cs-etm.c
2541 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2542 etm->synth_opts = *session->itrace_synth_opts;
2543 } else {
2544 itrace_synth_opts__set_default(&etm->synth_opts,
2545 session->itrace_synth_opts->default_no_sample);
^^^^^^^^^^^^^^^^^^^^^^^^^^
2546 etm->synth_opts.callchain = false;
2547 }

'session->itrace_synth_opts' is impossible to be a NULL pointer in
cs_etm__process_auxtrace_info(), thus this patch removes the NULL
test for 'session->itrace_synth_opts'.

Signed-off-by: Leo Yan <[email protected]>
Reviewed-by: Mathieu Poirier <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Suzuki Poulouse <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/cs-etm.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index 508e4a3ddc8c..67b88b599a53 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -2538,7 +2538,7 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
return 0;
}

- if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
+ if (session->itrace_synth_opts->set) {
etm->synth_opts = *session->itrace_synth_opts;
} else {
itrace_synth_opts__set_default(&etm->synth_opts,

Subject: [tip:perf/urgent] perf hists browser: Fix potential NULL pointer dereference found by the smatch tool

Commit-ID: ceb75476db1617a88cc29b09839acacb69aa076e
Gitweb: https://git.kernel.org/tip/ceb75476db1617a88cc29b09839acacb69aa076e
Author: Leo Yan <[email protected]>
AuthorDate: Mon, 8 Jul 2019 22:39:34 +0800
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 9 Jul 2019 10:13:27 -0300

perf hists browser: Fix potential NULL pointer dereference found by the smatch tool

Based on the following report from Smatch, fix the potential
NULL pointer dereference check.

tools/perf/ui/browsers/hists.c:641
hist_browser__run() error: we previously assumed 'hbt' could be
null (see line 625)

tools/perf/ui/browsers/hists.c:3088
perf_evsel__hists_browse() error: we previously assumed
'browser->he_selection' could be null (see line 2902)

tools/perf/ui/browsers/hists.c:3272
perf_evsel_menu__run() error: we previously assumed 'hbt' could be
null (see line 3260)

This patch firstly validating the pointers before access them, so can
fix potential NULL pointer dereference.

Signed-off-by: Leo Yan <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Mathieu Poirier <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Suzuki Poulouse <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/ui/browsers/hists.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c
index 85581cfb9112..a94eb0755e8b 100644
--- a/tools/perf/ui/browsers/hists.c
+++ b/tools/perf/ui/browsers/hists.c
@@ -639,7 +639,11 @@ int hist_browser__run(struct hist_browser *browser, const char *help,
switch (key) {
case K_TIMER: {
u64 nr_entries;
- hbt->timer(hbt->arg);
+
+ WARN_ON_ONCE(!hbt);
+
+ if (hbt)
+ hbt->timer(hbt->arg);

if (hist_browser__has_filter(browser) ||
symbol_conf.report_hierarchy)
@@ -2821,7 +2825,7 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
{
struct hists *hists = evsel__hists(evsel);
struct hist_browser *browser = perf_evsel_browser__new(evsel, hbt, env, annotation_opts);
- struct branch_info *bi;
+ struct branch_info *bi = NULL;
#define MAX_OPTIONS 16
char *options[MAX_OPTIONS];
struct popup_action actions[MAX_OPTIONS];
@@ -3087,7 +3091,9 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
goto skip_annotation;

if (sort__mode == SORT_MODE__BRANCH) {
- bi = browser->he_selection->branch_info;
+
+ if (browser->he_selection)
+ bi = browser->he_selection->branch_info;

if (bi == NULL)
goto skip_annotation;
@@ -3271,7 +3277,8 @@ static int perf_evsel_menu__run(struct perf_evsel_menu *menu,

switch (key) {
case K_TIMER:
- hbt->timer(hbt->arg);
+ if (hbt)
+ hbt->timer(hbt->arg);

if (!menu->lost_events_warned &&
menu->lost_events &&

Subject: [tip:perf/urgent] perf intel-pt: Fix potential NULL pointer dereference found by the smatch tool

Commit-ID: 323fd749821daab0f327ec86d707c4542963cdb0
Gitweb: https://git.kernel.org/tip/323fd749821daab0f327ec86d707c4542963cdb0
Author: Leo Yan <[email protected]>
AuthorDate: Mon, 8 Jul 2019 22:39:36 +0800
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 9 Jul 2019 10:13:28 -0300

perf intel-pt: Fix potential NULL pointer dereference found by the smatch tool

Based on the following report from Smatch, fix the potential NULL
pointer dereference check.

tools/perf/util/intel-pt.c:3200
intel_pt_process_auxtrace_info() error: we previously assumed
'session->itrace_synth_opts' could be null (see line 3196)

tools/perf/util/intel-pt.c:3206
intel_pt_process_auxtrace_info() warn: variable dereferenced before
check 'session->itrace_synth_opts' (see line 3200)

tools/perf/util/intel-pt.c
3196 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
3197 pt->synth_opts = *session->itrace_synth_opts;
3198 } else {
3199 itrace_synth_opts__set_default(&pt->synth_opts,
3200 session->itrace_synth_opts->default_no_sample);
^^^^^^^^^^^^^^^^^^^^^^^^^^
3201 if (!session->itrace_synth_opts->default_no_sample &&
3202 !session->itrace_synth_opts->inject) {
3203 pt->synth_opts.branches = false;
3204 pt->synth_opts.callchain = true;
3205 }
3206 if (session->itrace_synth_opts)
^^^^^^^^^^^^^^^^^^^^^^^^^^
3207 pt->synth_opts.thread_stack =
3208 session->itrace_synth_opts->thread_stack;
3209 }

'session->itrace_synth_opts' is impossible to be a NULL pointer in
intel_pt_process_auxtrace_info(), thus this patch removes the NULL test
for 'session->itrace_synth_opts'.

Signed-off-by: Leo Yan <[email protected]>
Acked-by: Adrian Hunter <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Mathieu Poirier <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Suzuki Poulouse <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/intel-pt.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index c76a96f777fb..df061599fef4 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -3210,7 +3210,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
goto err_delete_thread;
}

- if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
+ if (session->itrace_synth_opts->set) {
pt->synth_opts = *session->itrace_synth_opts;
} else {
itrace_synth_opts__set_default(&pt->synth_opts,
@@ -3220,8 +3220,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
pt->synth_opts.branches = false;
pt->synth_opts.callchain = true;
}
- if (session->itrace_synth_opts)
- pt->synth_opts.thread_stack =
+ pt->synth_opts.thread_stack =
session->itrace_synth_opts->thread_stack;
}

@@ -3241,11 +3240,9 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
}

- if (session->itrace_synth_opts) {
- err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
- if (err)
- goto err_delete_thread;
- }
+ err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
+ if (err)
+ goto err_delete_thread;

if (pt->synth_opts.calls)
pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |

Subject: [tip:perf/urgent] perf intel-bts: Fix potential NULL pointer dereference found by the smatch tool

Commit-ID: 1d481458816d9424c8a05833ce0ebe72194a350e
Gitweb: https://git.kernel.org/tip/1d481458816d9424c8a05833ce0ebe72194a350e
Author: Leo Yan <[email protected]>
AuthorDate: Mon, 8 Jul 2019 22:39:35 +0800
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 9 Jul 2019 10:13:28 -0300

perf intel-bts: Fix potential NULL pointer dereference found by the smatch tool

Based on the following report from Smatch, fix the potential NULL
pointer dereference check.

tools/perf/util/intel-bts.c:898
intel_bts_process_auxtrace_info() error: we previously assumed
'session->itrace_synth_opts' could be null (see line 894)

tools/perf/util/intel-bts.c:899
intel_bts_process_auxtrace_info() warn: variable dereferenced before
check 'session->itrace_synth_opts' (see line 898)

tools/perf/util/intel-bts.c
894 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
895 bts->synth_opts = *session->itrace_synth_opts;
896 } else {
897 itrace_synth_opts__set_default(&bts->synth_opts,
898 session->itrace_synth_opts->default_no_sample);
^^^^^^^^^^^^^^^^^^^^^^^^^^
899 if (session->itrace_synth_opts)
^^^^^^^^^^^^^^^^^^^^^^^^^^
900 bts->synth_opts.thread_stack =
901 session->itrace_synth_opts->thread_stack;
902 }

'session->itrace_synth_opts' is impossible to be a NULL pointer in
intel_bts_process_auxtrace_info(), thus this patch removes the NULL test
for 'session->itrace_synth_opts'.

Signed-off-by: Leo Yan <[email protected]>
Acked-by: Adrian Hunter <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Mathieu Poirier <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Suzuki Poulouse <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/intel-bts.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c
index 5a21bcdb8ef7..5560e95afdda 100644
--- a/tools/perf/util/intel-bts.c
+++ b/tools/perf/util/intel-bts.c
@@ -891,13 +891,12 @@ int intel_bts_process_auxtrace_info(union perf_event *event,
if (dump_trace)
return 0;

- if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
+ if (session->itrace_synth_opts->set) {
bts->synth_opts = *session->itrace_synth_opts;
} else {
itrace_synth_opts__set_default(&bts->synth_opts,
session->itrace_synth_opts->default_no_sample);
- if (session->itrace_synth_opts)
- bts->synth_opts.thread_stack =
+ bts->synth_opts.thread_stack =
session->itrace_synth_opts->thread_stack;
}