linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bitops.h: sanitize rotate primitives
@ 2019-04-10 21:19 Rasmus Villemoes
  2019-04-11 17:49 ` Ido Schimmel
  2019-04-16 15:21 ` Will Deacon
  0 siblings, 2 replies; 3+ messages in thread
From: Rasmus Villemoes @ 2019-04-10 21:19 UTC (permalink / raw)
  To: Andrew Morton, Vineet Gupta, Will Deacon, Anthony Yznaga,
	Rasmus Villemoes
  Cc: Andrey Ryabinin, Pavel Machek, Ido Schimmel, Vadim Pasternak,
	linux-kernel

The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time
constant (naturally in that range), but there's an UBSAN report that
these may actually be called with a shift count of 0.

Instead of special-casing that, we can make them DTRT for all values
of shift while also avoiding UB. For some reason, this was already
partly done for rol32 (which was well-defined for [0, 31]). gcc 8
recognizes these patterns as rotates, so for example

__u32 rol32(__u32 word, unsigned int shift)
{
	return (word << (shift & 31)) | (word >> ((-shift) & 31));
}

compiles to

0000000000000020 <rol32>:
  20:   89 f8                   mov    %edi,%eax
  22:   89 f1                   mov    %esi,%ecx
  24:   d3 c0                   rol    %cl,%eax
  26:   c3                      retq

Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.

Due to integer promotions, ro[lr]8 were already well-defined for
shifts in [0, 8], and ro[lr]16 were mostly well-defined for shifts in
[0, 16] (only mostly - u16 gets promoted to _signed_ int, so if bit 15
is set, word << 16 is undefined). For consistency, update those as
well.

Reported-by: Ido Schimmel <idosch@mellanox.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 include/linux/bitops.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index 602af23b98c7..cf074bce3eb3 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -60,7 +60,7 @@ static __always_inline unsigned long hweight_long(unsigned long w)
  */
 static inline __u64 rol64(__u64 word, unsigned int shift)
 {
-	return (word << shift) | (word >> (64 - shift));
+	return (word << (shift & 63)) | (word >> ((-shift) & 63));
 }
 
 /**
@@ -70,7 +70,7 @@ static inline __u64 rol64(__u64 word, unsigned int shift)
  */
 static inline __u64 ror64(__u64 word, unsigned int shift)
 {
-	return (word >> shift) | (word << (64 - shift));
+	return (word >> (shift & 63)) | (word << ((-shift) & 63));
 }
 
 /**
@@ -80,7 +80,7 @@ static inline __u64 ror64(__u64 word, unsigned int shift)
  */
 static inline __u32 rol32(__u32 word, unsigned int shift)
 {
-	return (word << shift) | (word >> ((-shift) & 31));
+	return (word << (shift & 31)) | (word >> ((-shift) & 31));
 }
 
 /**
@@ -90,7 +90,7 @@ static inline __u32 rol32(__u32 word, unsigned int shift)
  */
 static inline __u32 ror32(__u32 word, unsigned int shift)
 {
-	return (word >> shift) | (word << (32 - shift));
+	return (word >> (shift & 31)) | (word << ((-shift) & 31));
 }
 
 /**
@@ -100,7 +100,7 @@ static inline __u32 ror32(__u32 word, unsigned int shift)
  */
 static inline __u16 rol16(__u16 word, unsigned int shift)
 {
-	return (word << shift) | (word >> (16 - shift));
+	return (word << (shift & 15)) | (word >> ((-shift) & 15));
 }
 
 /**
@@ -110,7 +110,7 @@ static inline __u16 rol16(__u16 word, unsigned int shift)
  */
 static inline __u16 ror16(__u16 word, unsigned int shift)
 {
-	return (word >> shift) | (word << (16 - shift));
+	return (word >> (shift & 15)) | (word << ((-shift) & 15));
 }
 
 /**
@@ -120,7 +120,7 @@ static inline __u16 ror16(__u16 word, unsigned int shift)
  */
 static inline __u8 rol8(__u8 word, unsigned int shift)
 {
-	return (word << shift) | (word >> (8 - shift));
+	return (word << (shift & 7)) | (word >> ((-shift) & 7));
 }
 
 /**
@@ -130,7 +130,7 @@ static inline __u8 rol8(__u8 word, unsigned int shift)
  */
 static inline __u8 ror8(__u8 word, unsigned int shift)
 {
-	return (word >> shift) | (word << (8 - shift));
+	return (word >> (shift & 7)) | (word << ((-shift) & 7));
 }
 
 /**
-- 
2.20.1


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

* Re: [PATCH] bitops.h: sanitize rotate primitives
  2019-04-10 21:19 [PATCH] bitops.h: sanitize rotate primitives Rasmus Villemoes
@ 2019-04-11 17:49 ` Ido Schimmel
  2019-04-16 15:21 ` Will Deacon
  1 sibling, 0 replies; 3+ messages in thread
From: Ido Schimmel @ 2019-04-11 17:49 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Andrew Morton, Vineet Gupta, Will Deacon, Anthony Yznaga,
	Andrey Ryabinin, Pavel Machek, Vadim Pasternak, linux-kernel

On Wed, Apr 10, 2019 at 11:19:06PM +0200, Rasmus Villemoes wrote:
> The ror32 implementation (word >> shift) | (word << (32 - shift) has
> undefined behaviour if shift is outside the [1, 31] range. Similarly
> for the 64 bit variants. Most callers pass a compile-time
> constant (naturally in that range), but there's an UBSAN report that
> these may actually be called with a shift count of 0.
> 
> Instead of special-casing that, we can make them DTRT for all values
> of shift while also avoiding UB. For some reason, this was already
> partly done for rol32 (which was well-defined for [0, 31]). gcc 8
> recognizes these patterns as rotates, so for example
> 
> __u32 rol32(__u32 word, unsigned int shift)
> {
> 	return (word << (shift & 31)) | (word >> ((-shift) & 31));
> }
> 
> compiles to
> 
> 0000000000000020 <rol32>:
>   20:   89 f8                   mov    %edi,%eax
>   22:   89 f1                   mov    %esi,%ecx
>   24:   d3 c0                   rol    %cl,%eax
>   26:   c3                      retq
> 
> Older compilers unfortunately do not do as well, but this only affects
> the small minority of users that don't pass constants.
> 
> Due to integer promotions, ro[lr]8 were already well-defined for
> shifts in [0, 8], and ro[lr]16 were mostly well-defined for shifts in
> [0, 16] (only mostly - u16 gets promoted to _signed_ int, so if bit 15
> is set, word << 16 is undefined). For consistency, update those as
> well.
> 
> Reported-by: Ido Schimmel <idosch@mellanox.com>
> Cc: Vadim Pasternak <vadimp@mellanox.com>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>

Tested-by: Ido Schimmel <idosch@mellanox.com>

Thanks!

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

* Re: [PATCH] bitops.h: sanitize rotate primitives
  2019-04-10 21:19 [PATCH] bitops.h: sanitize rotate primitives Rasmus Villemoes
  2019-04-11 17:49 ` Ido Schimmel
@ 2019-04-16 15:21 ` Will Deacon
  1 sibling, 0 replies; 3+ messages in thread
From: Will Deacon @ 2019-04-16 15:21 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Andrew Morton, Vineet Gupta, Anthony Yznaga, Andrey Ryabinin,
	Pavel Machek, Ido Schimmel, Vadim Pasternak, linux-kernel

On Wed, Apr 10, 2019 at 11:19:06PM +0200, Rasmus Villemoes wrote:
> The ror32 implementation (word >> shift) | (word << (32 - shift) has
> undefined behaviour if shift is outside the [1, 31] range. Similarly
> for the 64 bit variants. Most callers pass a compile-time
> constant (naturally in that range), but there's an UBSAN report that
> these may actually be called with a shift count of 0.
> 
> Instead of special-casing that, we can make them DTRT for all values
> of shift while also avoiding UB. For some reason, this was already
> partly done for rol32 (which was well-defined for [0, 31]). gcc 8
> recognizes these patterns as rotates, so for example
> 
> __u32 rol32(__u32 word, unsigned int shift)
> {
> 	return (word << (shift & 31)) | (word >> ((-shift) & 31));
> }
> 
> compiles to
> 
> 0000000000000020 <rol32>:
>   20:   89 f8                   mov    %edi,%eax
>   22:   89 f1                   mov    %esi,%ecx
>   24:   d3 c0                   rol    %cl,%eax
>   26:   c3                      retq
> 
> Older compilers unfortunately do not do as well, but this only affects
> the small minority of users that don't pass constants.
> 
> Due to integer promotions, ro[lr]8 were already well-defined for
> shifts in [0, 8], and ro[lr]16 were mostly well-defined for shifts in
> [0, 16] (only mostly - u16 gets promoted to _signed_ int, so if bit 15
> is set, word << 16 is undefined). For consistency, update those as
> well.
> 
> Reported-by: Ido Schimmel <idosch@mellanox.com>
> Cc: Vadim Pasternak <vadimp@mellanox.com>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  include/linux/bitops.h | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)

Reviewed-by: Will Deacon <will.deacon@arm.com>

I guess it would be possible to roll some of this up into macros using
sizeof, but perhaps that would make things even more difficult for the
compiler.

Will

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

end of thread, other threads:[~2019-04-16 15:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-10 21:19 [PATCH] bitops.h: sanitize rotate primitives Rasmus Villemoes
2019-04-11 17:49 ` Ido Schimmel
2019-04-16 15:21 ` Will Deacon

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).