diff mbox series

[v2] libbpf: Initialize st_ops->tname with strdup()

Message ID 20240821112344.54299-3-soma.nakata01@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [v2] libbpf: Initialize st_ops->tname with strdup() | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-net-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-net-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-net-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-net-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-net-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-net-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-net-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-net-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-net-VM_Test-13 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-net-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-net-VM_Test-15 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-net-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-net-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-net-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-22 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-26 fail Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-net-VM_Test-29 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-net-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-net-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-net-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-net-PR fail PR summary
bpf/vmtest-bpf-net-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc

Commit Message

Soma Nakata Aug. 21, 2024, 11:23 a.m. UTC
`tname` is returned by `btf__name_by_offset()` as well as `var_name`,
and these addresses point to strings in the btf. Since their locations
may change while loading the bpf program, using `strdup()` ensures
`tname` is safely stored.

Signed-off-by: Soma Nakata <soma.nakata01@gmail.com>
---
 tools/lib/bpf/libbpf.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Christophe JAILLET Aug. 21, 2024, 12:15 p.m. UTC | #1
Le 21/08/2024 à 13:23, Soma Nakata a écrit :
> `tname` is returned by `btf__name_by_offset()` as well as `var_name`,
> and these addresses point to strings in the btf. Since their locations
> may change while loading the bpf program, using `strdup()` ensures
> `tname` is safely stored.
> 
> Signed-off-by: Soma Nakata <soma.nakata01@gmail.com>
> ---
>   tools/lib/bpf/libbpf.c | 7 +++++--
>   1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index a3be6f8fac09..f4ad1b993ec5 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -496,7 +496,7 @@ struct bpf_program {
>   };
>   
>   struct bpf_struct_ops {
> -	const char *tname;
> +	char *tname;
>   	const struct btf_type *type;
>   	struct bpf_program **progs;
>   	__u32 *kern_func_off;
> @@ -1423,7 +1423,9 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
>   		memcpy(st_ops->data,
>   		       data->d_buf + vsi->offset,
>   		       type->size);
> -		st_ops->tname = tname;
> +		st_ops->tname = strdup(tname);
> +		if (!st_ops->tname)
> +			return -ENOMEM;

Certainly a matter of taste, but I would personally move it just after 
"st_ops->kern_func_off = malloc()" and add the NULL check with the 
existing ones.

BTW, there are some memory leaks if 1 or more allocations fail in this 
function.
Not sure if it is an issue or not, and what should be done in this case.

CJ


>   		st_ops->type = type;
>   		st_ops->type_id = type_id;
>   
> @@ -8984,6 +8986,7 @@ static void bpf_map__destroy(struct bpf_map *map)
>   	map->mmaped = NULL;
>   
>   	if (map->st_ops) {
> +		zfree(&map->st_ops->tname);
>   		zfree(&map->st_ops->data);
>   		zfree(&map->st_ops->progs);
>   		zfree(&map->st_ops->kern_func_off);
Soma Nakata Aug. 21, 2024, 1:30 p.m. UTC | #2
On Wed, Aug 21, 2024 at 9:16 PM Christophe JAILLET
<christophe.jaillet@wanadoo.fr> wrote:
>
> Le 21/08/2024 à 13:23, Soma Nakata a écrit :
> > `tname` is returned by `btf__name_by_offset()` as well as `var_name`,
> > and these addresses point to strings in the btf. Since their locations
> > may change while loading the bpf program, using `strdup()` ensures
> > `tname` is safely stored.
> >
> > Signed-off-by: Soma Nakata <soma.nakata01@gmail.com>
> > ---
> >   tools/lib/bpf/libbpf.c | 7 +++++--
> >   1 file changed, 5 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index a3be6f8fac09..f4ad1b993ec5 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -496,7 +496,7 @@ struct bpf_program {
> >   };
> >
> >   struct bpf_struct_ops {
> > -     const char *tname;
> > +     char *tname;
> >       const struct btf_type *type;
> >       struct bpf_program **progs;
> >       __u32 *kern_func_off;
> > @@ -1423,7 +1423,9 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
> >               memcpy(st_ops->data,
> >                      data->d_buf + vsi->offset,
> >                      type->size);
> > -             st_ops->tname = tname;
> > +             st_ops->tname = strdup(tname);
> > +             if (!st_ops->tname)
> > +                     return -ENOMEM;
>
> Certainly a matter of taste, but I would personally move it just after
> "st_ops->kern_func_off = malloc()" and add the NULL check with the
> existing ones.
>
> BTW, there are some memory leaks if 1 or more allocations fail in this
> function.
> Not sure if it is an issue or not, and what should be done in this case.

You mean the line below?
if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
seems it says the size of them are in descending order or something.
But regardless, this looks like a memory leak.
I will send another patch on this.

thanks,

>
> CJ
>
>
> >               st_ops->type = type;
> >               st_ops->type_id = type_id;
> >
> > @@ -8984,6 +8986,7 @@ static void bpf_map__destroy(struct bpf_map *map)
> >       map->mmaped = NULL;
> >
> >       if (map->st_ops) {
> > +             zfree(&map->st_ops->tname);
> >               zfree(&map->st_ops->data);
> >               zfree(&map->st_ops->progs);
> >               zfree(&map->st_ops->kern_func_off);
>
Christophe JAILLET Aug. 21, 2024, 1:41 p.m. UTC | #3
Le 21/08/2024 à 15:30, Soma Nakata a écrit :
> On Wed, Aug 21, 2024 at 9:16 PM Christophe JAILLET
> <christophe.jaillet@wanadoo.fr> wrote:
>>
>> Le 21/08/2024 à 13:23, Soma Nakata a écrit :
>>> `tname` is returned by `btf__name_by_offset()` as well as `var_name`,
>>> and these addresses point to strings in the btf. Since their locations
>>> may change while loading the bpf program, using `strdup()` ensures
>>> `tname` is safely stored.
>>>
>>> Signed-off-by: Soma Nakata <soma.nakata01@gmail.com>
>>> ---
>>>    tools/lib/bpf/libbpf.c | 7 +++++--
>>>    1 file changed, 5 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>>> index a3be6f8fac09..f4ad1b993ec5 100644
>>> --- a/tools/lib/bpf/libbpf.c
>>> +++ b/tools/lib/bpf/libbpf.c
>>> @@ -496,7 +496,7 @@ struct bpf_program {
>>>    };
>>>
>>>    struct bpf_struct_ops {
>>> -     const char *tname;
>>> +     char *tname;
>>>        const struct btf_type *type;
>>>        struct bpf_program **progs;
>>>        __u32 *kern_func_off;
>>> @@ -1423,7 +1423,9 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
>>>                memcpy(st_ops->data,
>>>                       data->d_buf + vsi->offset,
>>>                       type->size);
>>> -             st_ops->tname = tname;
>>> +             st_ops->tname = strdup(tname);
>>> +             if (!st_ops->tname)
>>> +                     return -ENOMEM;
>>
>> Certainly a matter of taste, but I would personally move it just after
>> "st_ops->kern_func_off = malloc()" and add the NULL check with the
>> existing ones.
>>
>> BTW, there are some memory leaks if 1 or more allocations fail in this
>> function.
>> Not sure if it is an issue or not, and what should be done in this case.
> 
> You mean the line below?
> if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)

Yes.

> seems it says the size of them are in descending order or something.
> But regardless, this looks like a memory leak.
> I will send another patch on this.
> 
> thanks,
> 
>>
>> CJ
>>
>>
>>>                st_ops->type = type;
>>>                st_ops->type_id = type_id;
>>>
>>> @@ -8984,6 +8986,7 @@ static void bpf_map__destroy(struct bpf_map *map)
>>>        map->mmaped = NULL;
>>>
>>>        if (map->st_ops) {
>>> +             zfree(&map->st_ops->tname);
>>>                zfree(&map->st_ops->data);
>>>                zfree(&map->st_ops->progs);
>>>                zfree(&map->st_ops->kern_func_off);
>>
> 
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index a3be6f8fac09..f4ad1b993ec5 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -496,7 +496,7 @@  struct bpf_program {
 };
 
 struct bpf_struct_ops {
-	const char *tname;
+	char *tname;
 	const struct btf_type *type;
 	struct bpf_program **progs;
 	__u32 *kern_func_off;
@@ -1423,7 +1423,9 @@  static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
 		memcpy(st_ops->data,
 		       data->d_buf + vsi->offset,
 		       type->size);
-		st_ops->tname = tname;
+		st_ops->tname = strdup(tname);
+		if (!st_ops->tname)
+			return -ENOMEM;
 		st_ops->type = type;
 		st_ops->type_id = type_id;
 
@@ -8984,6 +8986,7 @@  static void bpf_map__destroy(struct bpf_map *map)
 	map->mmaped = NULL;
 
 	if (map->st_ops) {
+		zfree(&map->st_ops->tname);
 		zfree(&map->st_ops->data);
 		zfree(&map->st_ops->progs);
 		zfree(&map->st_ops->kern_func_off);