diff mbox series

[RFC] libbpf: Change hash_combine parameters from long to __u32

Message ID 20241115103422.55040-1-sidong.yang@furiosa.ai (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [RFC] libbpf: Change hash_combine parameters from long to __u32 | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-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-next-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-next-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-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-next-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-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-next-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-next-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-next-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-next-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-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-next-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-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-next-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-next-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-next-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-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-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-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-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc

Commit Message

Sidong Yang Nov. 15, 2024, 10:34 a.m. UTC
The hash_combine() could be trapped when compiled with sanitizer like "zig cc".
This patch changes parameters to __u32 to fix it.

Signed-off-by: Sidong Yang <sidong.yang@furiosa.ai>
---
 tools/lib/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Andrii Nakryiko Nov. 15, 2024, 7:57 p.m. UTC | #1
On Fri, Nov 15, 2024 at 2:51 AM Sidong Yang <sidong.yang@furiosa.ai> wrote:
>
> The hash_combine() could be trapped when compiled with sanitizer like "zig cc".
> This patch changes parameters to __u32 to fix it.

Can you please elaborate? What exactly are you fixing? "Undefined"
signed integer overflow? I can consider changing long to unsigned
long, but I don't think we should downgrade from long all the way to
32-bit u32. I'd rather keep all those 64 bits for hash.

pw-bot: cr

>
> Signed-off-by: Sidong Yang <sidong.yang@furiosa.ai>
> ---
>  tools/lib/bpf/btf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 8befb8103e32..11ccb5aa4958 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -3548,7 +3548,7 @@ struct btf_dedup {
>         struct strset *strs_set;
>  };
>
> -static long hash_combine(long h, long value)
> +static __u32 hash_combine(__u32 h, __u32 value)
>  {
>         return h * 31 + value;
>  }
> --
> 2.42.0
>
>
Sidong Yang Nov. 16, 2024, 12:36 a.m. UTC | #2
On Fri, Nov 15, 2024 at 11:57:24AM -0800, Andrii Nakryiko wrote:
> On Fri, Nov 15, 2024 at 2:51 AM Sidong Yang <sidong.yang@furiosa.ai> wrote:
> >
> > The hash_combine() could be trapped when compiled with sanitizer like "zig cc".
> > This patch changes parameters to __u32 to fix it.
> 
> Can you please elaborate? What exactly are you fixing? "Undefined"
> signed integer overflow? I can consider changing long to unsigned
> long, but I don't think we should downgrade from long all the way to
> 32-bit u32. I'd rather keep all those 64 bits for hash.

Hi, Andrii.

Actually I'm using libbpf-rs with maturin build that makes python package for
rust. It seems that it uses zig cc for cross compilation. It compiles libbpf
like this command.

CC="zig cc" make CFLAGS="-fsanitize-trap"

And hash_combine's result is like below.

0000000000063860 <hash_combine>:
   63860:       55                      push   %rbp
   63861:       48 89 e5                mov    %rsp,%rbp
   63864:       48 89 7d f8             mov    %rdi,-0x8(%rbp)
   63868:       48 89 75 f0             mov    %rsi,-0x10(%rbp)
   6386c:       b8 1f 00 00 00          mov    $0x1f,%eax
   63871:       48 0f af 45 f8          imul   -0x8(%rbp),%rax
   63876:       48 89 45 e8             mov    %rax,-0x18(%rbp)
   6387a:       0f 90 c0                seto   %al
   6387d:       34 ff                   xor    $0xff,%al
   6387f:       a8 01                   test   $0x1,%al
   63881:       0f 85 05 00 00 00       jne    6388c <hash_combine+0x2c>
-> 63887:       67 0f b9 40 0c          ud1    0xc(%eax),%eax
   6388c:       48 8b 45 e8             mov    -0x18(%rbp),%rax
   63890:       48 03 45 f0             add    -0x10(%rbp),%rax
   63894:       48 89 45 e0             mov    %rax,-0x20(%rbp)
   63898:       0f 90 c0                seto   %al
   6389b:       34 ff                   xor    $0xff,%al
   6389d:       a8 01                   test   $0x1,%al
   6389f:       0f 85 04 00 00 00       jne    638a9 <hash_combine+0x49>
   638a5:       67 0f b9 00             ud1    (%eax),%eax
   638a9:       48 8b 45 e0             mov    -0x20(%rbp),%rax
   638ad:       5d                      pop    %rbp
   638ae:       c3                      ret   
   638af:       90                      nop

When I'm using libbpf-rs, it receives SIGILL for ud1 instruction.
It seems more appropriate to use u64 instead of u32, doesn't it?
I'll work on it.

Thanks,
Sidong
> 
> pw-bot: cr
> 
> >
> > Signed-off-by: Sidong Yang <sidong.yang@furiosa.ai>
> > ---
> >  tools/lib/bpf/btf.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> > index 8befb8103e32..11ccb5aa4958 100644
> > --- a/tools/lib/bpf/btf.c
> > +++ b/tools/lib/bpf/btf.c
> > @@ -3548,7 +3548,7 @@ struct btf_dedup {
> >         struct strset *strs_set;
> >  };
> >
> > -static long hash_combine(long h, long value)
> > +static __u32 hash_combine(__u32 h, __u32 value)
> >  {
> >         return h * 31 + value;
> >  }
> > --
> > 2.42.0
> >
> >
Yonghong Song Nov. 16, 2024, 4:39 a.m. UTC | #3
On 11/15/24 4:36 PM, Sidong Yang wrote:
> On Fri, Nov 15, 2024 at 11:57:24AM -0800, Andrii Nakryiko wrote:
>> On Fri, Nov 15, 2024 at 2:51 AM Sidong Yang <sidong.yang@furiosa.ai> wrote:
>>> The hash_combine() could be trapped when compiled with sanitizer like "zig cc".
>>> This patch changes parameters to __u32 to fix it.
>> Can you please elaborate? What exactly are you fixing? "Undefined"
>> signed integer overflow? I can consider changing long to unsigned
>> long, but I don't think we should downgrade from long all the way to
>> 32-bit u32. I'd rather keep all those 64 bits for hash.
> Hi, Andrii.
>
> Actually I'm using libbpf-rs with maturin build that makes python package for
> rust. It seems that it uses zig cc for cross compilation. It compiles libbpf
> like this command.
>
> CC="zig cc" make CFLAGS="-fsanitize-trap"
>
> And hash_combine's result is like below.
>
> 0000000000063860 <hash_combine>:
>     63860:       55                      push   %rbp
>     63861:       48 89 e5                mov    %rsp,%rbp
>     63864:       48 89 7d f8             mov    %rdi,-0x8(%rbp)
>     63868:       48 89 75 f0             mov    %rsi,-0x10(%rbp)
>     6386c:       b8 1f 00 00 00          mov    $0x1f,%eax
>     63871:       48 0f af 45 f8          imul   -0x8(%rbp),%rax
>     63876:       48 89 45 e8             mov    %rax,-0x18(%rbp)
>     6387a:       0f 90 c0                seto   %al
>     6387d:       34 ff                   xor    $0xff,%al
>     6387f:       a8 01                   test   $0x1,%al
>     63881:       0f 85 05 00 00 00       jne    6388c <hash_combine+0x2c>
> -> 63887:       67 0f b9 40 0c          ud1    0xc(%eax),%eax
>     6388c:       48 8b 45 e8             mov    -0x18(%rbp),%rax
>     63890:       48 03 45 f0             add    -0x10(%rbp),%rax
>     63894:       48 89 45 e0             mov    %rax,-0x20(%rbp)
>     63898:       0f 90 c0                seto   %al
>     6389b:       34 ff                   xor    $0xff,%al
>     6389d:       a8 01                   test   $0x1,%al
>     6389f:       0f 85 04 00 00 00       jne    638a9 <hash_combine+0x49>
>     638a5:       67 0f b9 00             ud1    (%eax),%eax
>     638a9:       48 8b 45 e0             mov    -0x20(%rbp),%rax
>     638ad:       5d                      pop    %rbp
>     638ae:       c3                      ret
>     638af:       90                      nop
>
> When I'm using libbpf-rs, it receives SIGILL for ud1 instruction.
> It seems more appropriate to use u64 instead of u32, doesn't it?
> I'll work on it.

Yes, this is due to potential integer overflow.

I tried with clang with additional flags
    -fsanitize=signed-integer-overflow -fsanitize-trap=all
and disable inlining for hash_combine().
The asm code (the code is compiled with -O2)

0000000000007cb0 <hash_combine>:
     7cb0: 48 6b c7 1f                   imulq   $0x1f, %rdi, %rax
     7cb4: 70 06                         jo      0x7cbc <hash_combine+0xc>
     7cb6: 48 01 f0                      addq    %rsi, %rax
     7cb9: 70 06                         jo      0x7cc1 <hash_combine+0x11>
     7cbb: c3                            retq
     7cbc: 67 0f b9 40 0c                ud1l    0xc(%eax), %eax
     7cc1: 67 0f b9 00                   ud1l    (%eax), %eax
     7cc5: 66 66 2e 0f 1f 84 00 00 00 00 00      nopw    %cs:(%rax,%rax)

Here 'jo' means 'jump if overflow'.
So if overflow happens, 'ud1l' will execute and dump error.

Changing 'long' type to 'unsigned long' should fix the problem.

>
> Thanks,
> Sidong
>> pw-bot: cr
>>
>>> Signed-off-by: Sidong Yang <sidong.yang@furiosa.ai>
>>> ---
>>>   tools/lib/bpf/btf.c | 2 +-
>>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
>>> index 8befb8103e32..11ccb5aa4958 100644
>>> --- a/tools/lib/bpf/btf.c
>>> +++ b/tools/lib/bpf/btf.c
>>> @@ -3548,7 +3548,7 @@ struct btf_dedup {
>>>          struct strset *strs_set;
>>>   };
>>>
>>> -static long hash_combine(long h, long value)
>>> +static __u32 hash_combine(__u32 h, __u32 value)
>>>   {
>>>          return h * 31 + value;
>>>   }
>>> --
>>> 2.42.0
>>>
>>>
Sidong Yang Nov. 16, 2024, 6:49 a.m. UTC | #4
On Fri, Nov 15, 2024 at 08:39:05PM -0800, Yonghong Song wrote:
> 
> 
> 
> On 11/15/24 4:36 PM, Sidong Yang wrote:
> > On Fri, Nov 15, 2024 at 11:57:24AM -0800, Andrii Nakryiko wrote:
> > > On Fri, Nov 15, 2024 at 2:51 AM Sidong Yang <sidong.yang@furiosa.ai> wrote:
> > > > The hash_combine() could be trapped when compiled with sanitizer like "zig cc".
> > > > This patch changes parameters to __u32 to fix it.
> > > Can you please elaborate? What exactly are you fixing? "Undefined"
> > > signed integer overflow? I can consider changing long to unsigned
> > > long, but I don't think we should downgrade from long all the way to
> > > 32-bit u32. I'd rather keep all those 64 bits for hash.
> > Hi, Andrii.
> > 
> > Actually I'm using libbpf-rs with maturin build that makes python package for
> > rust. It seems that it uses zig cc for cross compilation. It compiles libbpf
> > like this command.
> > 
> > CC="zig cc" make CFLAGS="-fsanitize-trap"
> > 
> > And hash_combine's result is like below.
> > 
> > 0000000000063860 <hash_combine>:
> >     63860:       55                      push   %rbp
> >     63861:       48 89 e5                mov    %rsp,%rbp
> >     63864:       48 89 7d f8             mov    %rdi,-0x8(%rbp)
> >     63868:       48 89 75 f0             mov    %rsi,-0x10(%rbp)
> >     6386c:       b8 1f 00 00 00          mov    $0x1f,%eax
> >     63871:       48 0f af 45 f8          imul   -0x8(%rbp),%rax
> >     63876:       48 89 45 e8             mov    %rax,-0x18(%rbp)
> >     6387a:       0f 90 c0                seto   %al
> >     6387d:       34 ff                   xor    $0xff,%al
> >     6387f:       a8 01                   test   $0x1,%al
> >     63881:       0f 85 05 00 00 00       jne    6388c <hash_combine+0x2c>
> > -> 63887:       67 0f b9 40 0c          ud1    0xc(%eax),%eax
> >     6388c:       48 8b 45 e8             mov    -0x18(%rbp),%rax
> >     63890:       48 03 45 f0             add    -0x10(%rbp),%rax
> >     63894:       48 89 45 e0             mov    %rax,-0x20(%rbp)
> >     63898:       0f 90 c0                seto   %al
> >     6389b:       34 ff                   xor    $0xff,%al
> >     6389d:       a8 01                   test   $0x1,%al
> >     6389f:       0f 85 04 00 00 00       jne    638a9 <hash_combine+0x49>
> >     638a5:       67 0f b9 00             ud1    (%eax),%eax
> >     638a9:       48 8b 45 e0             mov    -0x20(%rbp),%rax
> >     638ad:       5d                      pop    %rbp
> >     638ae:       c3                      ret
> >     638af:       90                      nop
> > 
> > When I'm using libbpf-rs, it receives SIGILL for ud1 instruction.
> > It seems more appropriate to use u64 instead of u32, doesn't it?
> > I'll work on it.
> 
> Yes, this is due to potential integer overflow.
> 
> I tried with clang with additional flags
>    -fsanitize=signed-integer-overflow -fsanitize-trap=all
> and disable inlining for hash_combine().
> The asm code (the code is compiled with -O2)
> 
> 0000000000007cb0 <hash_combine>:
>     7cb0: 48 6b c7 1f                   imulq   $0x1f, %rdi, %rax
>     7cb4: 70 06                         jo      0x7cbc <hash_combine+0xc>
>     7cb6: 48 01 f0                      addq    %rsi, %rax
>     7cb9: 70 06                         jo      0x7cc1 <hash_combine+0x11>
>     7cbb: c3                            retq
>     7cbc: 67 0f b9 40 0c                ud1l    0xc(%eax), %eax
>     7cc1: 67 0f b9 00                   ud1l    (%eax), %eax
>     7cc5: 66 66 2e 0f 1f 84 00 00 00 00 00      nopw    %cs:(%rax,%rax)
> 
> Here 'jo' means 'jump if overflow'.
> So if overflow happens, 'ud1l' will execute and dump error.
> 
> Changing 'long' type to 'unsigned long' should fix the problem.

Agree, unsigned long will be good. thanks.

> 
> > 
> > Thanks,
> > Sidong
> > > pw-bot: cr
> > > 
> > > > Signed-off-by: Sidong Yang <sidong.yang@furiosa.ai>
> > > > ---
> > > >   tools/lib/bpf/btf.c | 2 +-
> > > >   1 file changed, 1 insertion(+), 1 deletion(-)
> > > > 
> > > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> > > > index 8befb8103e32..11ccb5aa4958 100644
> > > > --- a/tools/lib/bpf/btf.c
> > > > +++ b/tools/lib/bpf/btf.c
> > > > @@ -3548,7 +3548,7 @@ struct btf_dedup {
> > > >          struct strset *strs_set;
> > > >   };
> > > > 
> > > > -static long hash_combine(long h, long value)
> > > > +static __u32 hash_combine(__u32 h, __u32 value)
> > > >   {
> > > >          return h * 31 + value;
> > > >   }
> > > > --
> > > > 2.42.0
> > > > 
> > > > 
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 8befb8103e32..11ccb5aa4958 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -3548,7 +3548,7 @@  struct btf_dedup {
 	struct strset *strs_set;
 };
 
-static long hash_combine(long h, long value)
+static __u32 hash_combine(__u32 h, __u32 value)
 {
 	return h * 31 + value;
 }