Skip to contentSkip to conversation

tracing/probes: Support $argN in return probe (kprobe and fprobe)

bug report In review Dan Carpenter 3 participants 9 messages Mar 19, 2024 – Mar 20, 2024 Tracing

lore
Share & tools
lore
#0 lore raw

Hello Masami Hiramatsu (Google),

Commit 25f00e40ce79 ("tracing/probes: Support $argN in return probe (kprobe and fprobe)") from Mar 4, 2024 (linux-next), leads to the following Smatch static checker warning:

kernel/trace/trace_probe.c:856 store_trace_entry_data()
error: uninitialized symbol 'val'.

kernel/trace/trace_probe.c

    846                 return;
    847 
    848         for (i = 0; i < earg->size; i++) {
    849                 struct fetch_insn *code = &earg->code[i];
    850 
    851                 switch (code->op) {
    852                 case FETCH_OP_ARG:
    853                         val = regs_get_kernel_argument(regs, code->param);
    854                         break;
    855                 case FETCH_OP_ST_EDATA:

--> 856 *(unsigned long *)((unsigned long)edata + code->offset) = val;

Probably the earg->code[i] always has FETCH_OP_ARG before FETCH_OP_ST_EDATA but Smatch isn't smart enough to figure that out...

    857                         break;
    858                 case FETCH_OP_END:
    859                         goto end;
    860                 default:
    861                         break;
    862                 }
    863         }
    864 end:
    865         return;
    866 }

regards,
dan carpenter

maintainer
#1 lore raw
Quoted 20 lines · On Tue, 19 Mar 2024 10:19:09 +0300 Dan Carpenter <[email protected]>…

Hello Masami Hiramatsu (Google),

Commit 25f00e40ce79 ("tracing/probes: Support $argN in return probe (kprobe and fprobe)") from Mar 4, 2024 (linux-next), leads to the following Smatch static checker warning:

kernel/trace/trace_probe.c:856 store_trace_entry_data()
error: uninitialized symbol 'val'.

kernel/trace/trace_probe.c

    846                 return;
    847 
    848         for (i = 0; i < earg->size; i++) {
    849                 struct fetch_insn *code = &earg->code[i];
    850 
    851                 switch (code->op) {
    852                 case FETCH_OP_ARG:
    853                         val = regs_get_kernel_argument(regs, code->param);
    854                         break;
    855                 case FETCH_OP_ST_EDATA:
Quoted 4 lines

--> 856 *(unsigned long *)((unsigned long)edata + code->offset) = val;

Probably the earg->code[i] always has FETCH_OP_ARG before FETCH_OP_ST_EDATA but Smatch isn't smart enough to figure that out...

Looks that way:

		case FETCH_OP_END:
			earg->code[i].op = FETCH_OP_ARG;
			earg->code[i].param = argnum;
			earg->code[i + 1].op = FETCH_OP_ST_EDATA;
			earg->code[i + 1].offset = offset;
			return offset;

But probably should still initialize val to zero or have a WARN_ON() if that doesn't happen.

-- Steve

Quoted 14 lines
    857                         break;
    858                 case FETCH_OP_END:
    859                         goto end;
    860                 default:
    861                         break;
    862                 }
    863         }
    864 end:
    865         return;
    866 }

regards,
dan carpenter

#2 lore raw
Quoted 37 lines · Steven Rostedt
Quoted 20 lines · On Tue, 19 Mar 2024 10:19:09 +0300 Dan Carpenter <[email protected]>…

Hello Masami Hiramatsu (Google),

Commit 25f00e40ce79 ("tracing/probes: Support $argN in return probe (kprobe and fprobe)") from Mar 4, 2024 (linux-next), leads to the following Smatch static checker warning:

kernel/trace/trace_probe.c:856 store_trace_entry_data()
error: uninitialized symbol 'val'.

kernel/trace/trace_probe.c

    846                 return;
    847 
    848         for (i = 0; i < earg->size; i++) {
    849                 struct fetch_insn *code = &earg->code[i];
    850 
    851                 switch (code->op) {
    852                 case FETCH_OP_ARG:
    853                         val = regs_get_kernel_argument(regs, code->param);
    854                         break;
    855                 case FETCH_OP_ST_EDATA:
Quoted 4 lines

--> 856 *(unsigned long *)((unsigned long)edata + code->offset) = val;

Probably the earg->code[i] always has FETCH_OP_ARG before FETCH_OP_ST_EDATA but Smatch isn't smart enough to figure that out...

Looks that way:

		case FETCH_OP_END:
			earg->code[i].op = FETCH_OP_ARG;
			earg->code[i].param = argnum;
			earg->code[i + 1].op = FETCH_OP_ST_EDATA;
			earg->code[i + 1].offset = offset;
			return offset;
Quoted 3 lines

But probably should still initialize val to zero or have a WARN_ON() if that doesn't happen.

Most people use the GCC extension to initialize everything to zero so initializing to zero really has zero cost. I always recomend people to do it.

regards,
dan carpenter

#3 lore raw
In reply to Steven Rostedt
Quoted 37 lines · On Tue, 19 Mar 2024 10:10:00 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 20 lines · On Tue, 19 Mar 2024 10:19:09 +0300 Dan Carpenter <[email protected]>…

Hello Masami Hiramatsu (Google),

Commit 25f00e40ce79 ("tracing/probes: Support $argN in return probe (kprobe and fprobe)") from Mar 4, 2024 (linux-next), leads to the following Smatch static checker warning:

kernel/trace/trace_probe.c:856 store_trace_entry_data()
error: uninitialized symbol 'val'.

kernel/trace/trace_probe.c

    846                 return;
    847 
    848         for (i = 0; i < earg->size; i++) {
    849                 struct fetch_insn *code = &earg->code[i];
    850 
    851                 switch (code->op) {
    852                 case FETCH_OP_ARG:
    853                         val = regs_get_kernel_argument(regs, code->param);
    854                         break;
    855                 case FETCH_OP_ST_EDATA:
Quoted 4 lines

--> 856 *(unsigned long *)((unsigned long)edata + code->offset) = val;

Probably the earg->code[i] always has FETCH_OP_ARG before FETCH_OP_ST_EDATA but Smatch isn't smart enough to figure that out...

Looks that way:

		case FETCH_OP_END:
			earg->code[i].op = FETCH_OP_ARG;
			earg->code[i].param = argnum;
			earg->code[i + 1].op = FETCH_OP_ST_EDATA;
			earg->code[i + 1].offset = offset;
			return offset;
Quoted 2 lines

But probably should still initialize val to zero or have a WARN_ON() if that doesn't happen.

OK, let's val = 0 in the store_trace_entry_data(), but WARN_ON() in this loop is a bit strange. I think we should have a verifiler.

Thank you,

Quoted 19 lines

-- Steve

Quoted 14 lines
    857                         break;
    858                 case FETCH_OP_END:
    859                         goto end;
    860                 default:
    861                         break;
    862                 }
    863         }
    864 end:
    865         return;
    866 }

regards,
dan carpenter

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#4 lore raw
Quoted 25 lines · On Wed, 20 Mar 2024 12:44:23 +0900 Masami Hiramatsu (Google)…
Quoted 25 lines
Quoted 11 lines

kernel/trace/trace_probe.c

    846                 return;
    847 
    848         for (i = 0; i < earg->size; i++) {
    849                 struct fetch_insn *code = &earg->code[i];
    850 
    851                 switch (code->op) {
    852                 case FETCH_OP_ARG:
    853                         val = regs_get_kernel_argument(regs, code->param);
    854                         break;
    855                 case FETCH_OP_ST_EDATA:  
Quoted 4 lines

--> 856 *(unsigned long *)((unsigned long)edata + code->offset) = val;

Probably the earg->code[i] always has FETCH_OP_ARG before FETCH_OP_ST_EDATA but Smatch isn't smart enough to figure that out...

Looks that way:

		case FETCH_OP_END:
			earg->code[i].op = FETCH_OP_ARG;
			earg->code[i].param = argnum;
			earg->code[i + 1].op = FETCH_OP_ST_EDATA;
			earg->code[i + 1].offset = offset;
			return offset;
Quoted 5 lines
Quoted 2 lines

But probably should still initialize val to zero or have a WARN_ON() if that doesn't happen.

OK, let's val = 0 in the store_trace_entry_data(), but WARN_ON() in this loop is a bit strange. I think we should have a verifiler.

Initializing to zero is fine.

-- Steve

#5 lore raw

PATCH 1/1 tracing: probes: Fix to zero initialize a local variable

In reply to Dan Carpenter

From: Masami Hiramatsu (Google) <[email protected]>

Fix to initialize 'val' local variable with zero.
Dan reported that Smatch static code checker reports an error that a local 'val' variable needs to be initialized. Actually, the 'val' is expected to be initialized by FETCH_OP_ARG in the same loop, but it is not obvious. So initialize it with zero.

Fixes 25f00e40ce79 ("tracing/probes: Support $argN in return probe (kprobe and fprobe)")
---
 kernel/trace/trace_probe.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_probe.c +1 -1
index 217169de0920..dfe3ee6035ec 100644
@@ -839,7 +839,7 @@ int traceprobe_get_entry_data_size(struct trace_probe *tp)
 void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs)
 {
 	struct probe_entry_arg *earg = tp->entry_arg;
-	unsigned long val;
+	unsigned long val = 0;
 	int i;
 
 	if (!earg)
maintainer Reviewed-by on v1 1/1
#6 lore raw

Re: [PATCH] tracing: probes: Fix to zero initialize a local variable

Quoted 8 lines · On Wed, 20 Mar 2024 17:10:38 +0900 "Masami Hiramatsu (Google)"…

From: Masami Hiramatsu (Google) <[email protected]>

Fix to initialize 'val' local variable with zero.
Dan reported that Smatch static code checker reports an error that a local 'val' variable needs to be initialized. Actually, the 'val' is expected to be initialized by FETCH_OP_ARG in the same loop, but it is not obvious. So initialize it with zero.

Quoted 5 lines
Fixes 25f00e40ce79 ("tracing/probes: Support $argN in return probe (kprobe and fprobe)")

---

-- Steve

Quoted 16 lines
 kernel/trace/trace_probe.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_probe.c +1 -1
index 217169de0920..dfe3ee6035ec 100644
@@ -839,7 +839,7 @@ int traceprobe_get_entry_data_size(struct trace_probe *tp)
 void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs)
 {
 	struct probe_entry_arg *earg = tp->entry_arg;
-	unsigned long val;
+	unsigned long val = 0;
 	int i;
 
 	if (!earg)
maintainer
#7 lore raw

Re: [PATCH] tracing: probes: Fix to zero initialize a local variable

In reply to Masami Hiramatsu (Google)
Quoted 7 lines · On Wed, 20 Mar 2024 17:10:38 +0900 "Masami Hiramatsu (Google)"…

From: Masami Hiramatsu (Google) <[email protected]>

Fix to initialize 'val' local variable with zero.
Dan reported that Smatch static code checker reports an error that a local 'val' variable needs to be initialized. Actually, the 'val' is expected to be initialized by FETCH_OP_ARG in the same loop, but it is not obvious. So initialize it with zero.

BTW, that loop should really have a comment stating that FETCH_OP_ARG is expected to happen before FETCH_OP_ST_EDATA.

-- Steve

#8 lore raw

Re: [PATCH] tracing: probes: Fix to zero initialize a local variable

Quoted 5 lines · On Wed, 20 Mar 2024 09:27:49 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 2 lines · On Wed, 20 Mar 2024 17:10:38 +0900 "Masami Hiramatsu (Google)"…

From: Masami Hiramatsu (Google) <[email protected]>

Quoted 8 lines
Quoted 5 lines

Fix to initialize 'val' local variable with zero.
Dan reported that Smatch static code checker reports an error that a local 'val' variable needs to be initialized. Actually, the 'val' is expected to be initialized by FETCH_OP_ARG in the same loop, but it is not obvious. So initialize it with zero.

BTW, that loop should really have a comment stating that FETCH_OP_ARG is expected to happen before FETCH_OP_ST_EDATA.

Indeed, OK, let me add it.

Thank you,

Quoted 2 lines

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>