[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20190411174926.GA18767@splinter>
Date: Thu, 11 Apr 2019 17:49:29 +0000
From: Ido Schimmel <idosch@...lanox.com>
To: Rasmus Villemoes <linux@...musvillemoes.dk>
CC: Andrew Morton <akpm@...ux-foundation.org>,
Vineet Gupta <vineet.gupta1@...opsys.com>,
Will Deacon <will.deacon@....com>,
Anthony Yznaga <anthony.yznaga@...cle.com>,
Andrey Ryabinin <aryabinin@...tuozzo.com>,
Pavel Machek <pavel@....cz>,
Vadim Pasternak <vadimp@...lanox.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] bitops.h: sanitize rotate primitives
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@...lanox.com>
> Cc: Vadim Pasternak <vadimp@...lanox.com>
> Signed-off-by: Rasmus Villemoes <linux@...musvillemoes.dk>
Tested-by: Ido Schimmel <idosch@...lanox.com>
Thanks!
Powered by blists - more mailing lists