Re: [PATCH] ubsan: Implement __ubsan_handle_alignment_assumption

From: Nick Desaulniers
Date: Tue Jan 12 2021 - 16:54:33 EST


On Tue, Jan 12, 2021 at 1:37 PM Nathan Chancellor
<natechancellor@xxxxxxxxx> wrote:
>
> > if real_ptr is an unsigned long, do we want to use `__ffs(real_ptr) +
> > 1` here rather than ffs which takes an int? It seems the kernel is
> > missing a definition of ffsl. :(
>
> Why the + 1? I think if we use __ffs (which it seems like we should), I
> think that needs to become

This came up recently in an internal code review; ffs and __ffs differ
in output by one. See also the definition of ffs for alpha in
arch/alpha/include/asm/bitops.h.

Also, I just confirmed that:
```
#include <stdio.h>

// include/asm-generic/bitops/ffs.h
static inline int ffs(int x)
{
int r = 1;

if (!x)
return 0;
if (!(x & 0xffff)) {
x >>= 16;
r += 16;
}
if (!(x & 0xff)) {
x >>= 8;
r += 8;
}
if (!(x & 0xf)) {
x >>= 4;
r += 4;
}
if (!(x & 3)) {
x >>= 2;
r += 2;
}
if (!(x & 1)) {
x >>= 1;
r += 1;
}
return r;
}

// include/asm-generic/bitops/__ffs.h
static __always_inline unsigned long __ffs(unsigned long word)
{
int num = 0;

if ((word & 0xffffffff) == 0) {
num += 32;
word >>= 32;
}
if ((word & 0xffff) == 0) {
num += 16;
word >>= 16;
}
if ((word & 0xff) == 0) {
num += 8;
word >>= 8;
}
if ((word & 0xf) == 0) {
num += 4;
word >>= 4;
}
if ((word & 0x3) == 0) {
num += 2;
word >>= 2;
}
if ((word & 0x1) == 0)
num += 1;
return num;
}

int main() {
int x = 3;
unsigned long y = 3;
printf("%d\n%lu\n", ffs(x), __ffs(y));
return 0;
}
```
will print:
1
0
--
Thanks,
~Nick Desaulniers