linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf probe: Added protection to avoid endless loop
@ 2021-02-03  2:25 Jianlin Lv
  2021-02-03 13:25 ` Masami Hiramatsu
  0 siblings, 1 reply; 2+ messages in thread
From: Jianlin Lv @ 2021-02-03  2:25 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, mhiramat, srikar, adrian.hunter
  Cc: Jianlin.Lv, linux-kernel

if dwarf_offdie() return NULL, the continue statement forces the next
iteration of the loop without update variable off. It will cause an
endless loop in the process of traversing the compilation unit.
So added exception protection for loop CUs.

Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
---
 tools/perf/util/probe-finder.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 76dd349aa48d..887bffb1cc58 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -1156,7 +1156,7 @@ static int debuginfo__find_probe_location(struct debuginfo *dbg,
 	Dwarf_Die *diep;
 	int ret = 0;
 
-	off = 0;
+	noff = 0;
 	pf->lcache = intlist__new(NULL);
 	if (!pf->lcache)
 		return -ENOMEM;
@@ -1184,7 +1184,7 @@ static int debuginfo__find_probe_location(struct debuginfo *dbg,
 	}
 
 	/* Loop on CUs (Compilation Unit) */
-	while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
+	while (!dwarf_nextcu(dbg->dbg, off = noff, &noff, &cuhl, NULL, NULL, NULL)) {
 		/* Get the DIE(Debugging Information Entry) of this CU */
 		diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
 		if (!diep)
@@ -1208,7 +1208,6 @@ static int debuginfo__find_probe_location(struct debuginfo *dbg,
 			if (ret < 0)
 				break;
 		}
-		off = noff;
 	}
 
 found:
@@ -1919,7 +1918,7 @@ int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
 {
 	struct line_finder lf = {.lr = lr, .found = 0};
 	int ret = 0;
-	Dwarf_Off off = 0, noff;
+	Dwarf_Off off = 0, noff = 0;
 	size_t cuhl;
 	Dwarf_Die *diep;
 	const char *comp_dir;
@@ -1943,6 +1942,7 @@ int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
 
 	/* Loop on CUs (Compilation Unit) */
 	while (!lf.found && ret >= 0) {
+		off = noff;
 		if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
 				 NULL, NULL, NULL) != 0)
 			break;
@@ -1967,7 +1967,6 @@ int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
 				ret = find_line_range_by_line(NULL, &lf);
 			}
 		}
-		off = noff;
 	}
 
 found:
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] perf probe: Added protection to avoid endless loop
  2021-02-03  2:25 [PATCH] perf probe: Added protection to avoid endless loop Jianlin Lv
@ 2021-02-03 13:25 ` Masami Hiramatsu
  0 siblings, 0 replies; 2+ messages in thread
From: Masami Hiramatsu @ 2021-02-03 13:25 UTC (permalink / raw)
  To: Jianlin Lv
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, srikar, adrian.hunter, linux-kernel

On Wed,  3 Feb 2021 10:25:07 +0800
Jianlin Lv <Jianlin.Lv@arm.com> wrote:

> if dwarf_offdie() return NULL, the continue statement forces the next
> iteration of the loop without update variable off. It will cause an
> endless loop in the process of traversing the compilation unit.
> So added exception protection for loop CUs.

Good catch!

> 
> Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
> ---
>  tools/perf/util/probe-finder.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
> index 76dd349aa48d..887bffb1cc58 100644
> --- a/tools/perf/util/probe-finder.c
> +++ b/tools/perf/util/probe-finder.c
> @@ -1156,7 +1156,7 @@ static int debuginfo__find_probe_location(struct debuginfo *dbg,
>  	Dwarf_Die *diep;
>  	int ret = 0;
>  
> -	off = 0;
> +	noff = 0;
>  	pf->lcache = intlist__new(NULL);
>  	if (!pf->lcache)
>  		return -ENOMEM;
> @@ -1184,7 +1184,7 @@ static int debuginfo__find_probe_location(struct debuginfo *dbg,
>  	}
>  
>  	/* Loop on CUs (Compilation Unit) */
> -	while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
> +	while (!dwarf_nextcu(dbg->dbg, off = noff, &noff, &cuhl, NULL, NULL, NULL)) {

I don't like to update variable in function argument, 

I would rather like below code;

>  		/* Get the DIE(Debugging Information Entry) of this CU */
>  		diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die);
  		if (!diep) {
+			off = noff;
			continue;
		}

Or, "goto next;" and

> @@ -1208,7 +1208,6 @@ static int debuginfo__find_probe_location(struct debuginfo *dbg,
>  			if (ret < 0)
>  				break;
>  		}

next:

> -		off = noff;
>  	}
>  
>  found:
> @@ -1919,7 +1918,7 @@ int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
>  {
>  	struct line_finder lf = {.lr = lr, .found = 0};
>  	int ret = 0;
> -	Dwarf_Off off = 0, noff;
> +	Dwarf_Off off = 0, noff = 0;
>  	size_t cuhl;
>  	Dwarf_Die *diep;
>  	const char *comp_dir;
> @@ -1943,6 +1942,7 @@ int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
>  
>  	/* Loop on CUs (Compilation Unit) */
>  	while (!lf.found && ret >= 0) {
> +		off = noff;

Here too.
Can you update it?

Thank you,

>  		if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl,
>  				 NULL, NULL, NULL) != 0)
>  			break;
> @@ -1967,7 +1967,6 @@ int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr)
>  				ret = find_line_range_by_line(NULL, &lf);
>  			}
>  		}
> -		off = noff;
>  	}
>  
>  found:
> -- 
> 2.25.1
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-02-03 13:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-03  2:25 [PATCH] perf probe: Added protection to avoid endless loop Jianlin Lv
2021-02-03 13:25 ` Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).