* [PATCH] KVM: s390: selftest: memop: Fix undefined behavior
@ 2023-12-15 16:11 Nina Schoetterl-Glausch
2023-12-15 17:02 ` Claudio Imbrenda
0 siblings, 1 reply; 4+ messages in thread
From: Nina Schoetterl-Glausch @ 2023-12-15 16:11 UTC (permalink / raw)
To: Claudio Imbrenda, Paolo Bonzini, Shuah Khan, Janosch Frank,
Christian Borntraeger
Cc: Nina Schoetterl-Glausch, kvm, linux-kselftest, David Hildenbrand,
linux-kernel
If an integer's type has x bits, shifting the integer left by x or more
is undefined behavior.
This can happen in the rotate function when attempting to do a rotation
of the whole value by 0.
Fixes: 0dd714bfd200 ("KVM: s390: selftest: memop: Add cmpxchg tests")
Signed-off-by: Nina Schoetterl-Glausch <nsg@linux.ibm.com>
---
tools/testing/selftests/kvm/s390x/memop.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/kvm/s390x/memop.c b/tools/testing/selftests/kvm/s390x/memop.c
index bb3ca9a5d731..2eba9575828e 100644
--- a/tools/testing/selftests/kvm/s390x/memop.c
+++ b/tools/testing/selftests/kvm/s390x/memop.c
@@ -485,11 +485,13 @@ static bool popcount_eq(__uint128_t a, __uint128_t b)
static __uint128_t rotate(int size, __uint128_t val, int amount)
{
- unsigned int bits = size * 8;
+ unsigned int left, right, bits = size * 8;
- amount = (amount + bits) % bits;
+ right = (amount + bits) % bits;
+ /* % 128 prevents left shift UB if size == 16 && right == 0 */
+ left = (bits - right) % 128;
val = cut_to_size(size, val);
- return (val << (bits - amount)) | (val >> amount);
+ return (val << left) | (val >> right);
}
const unsigned int max_block = 16;
base-commit: 305230142ae0637213bf6e04f6d9f10bbcb74af8
--
2.40.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] KVM: s390: selftest: memop: Fix undefined behavior
2023-12-15 16:11 [PATCH] KVM: s390: selftest: memop: Fix undefined behavior Nina Schoetterl-Glausch
@ 2023-12-15 17:02 ` Claudio Imbrenda
2023-12-18 12:18 ` Nina Schoetterl-Glausch
0 siblings, 1 reply; 4+ messages in thread
From: Claudio Imbrenda @ 2023-12-15 17:02 UTC (permalink / raw)
To: Nina Schoetterl-Glausch
Cc: Paolo Bonzini, Shuah Khan, Janosch Frank, Christian Borntraeger,
kvm, linux-kselftest, David Hildenbrand, linux-kernel
On Fri, 15 Dec 2023 17:11:25 +0100
Nina Schoetterl-Glausch <nsg@linux.ibm.com> wrote:
> If an integer's type has x bits, shifting the integer left by x or more
> is undefined behavior.
> This can happen in the rotate function when attempting to do a rotation
> of the whole value by 0.
is 0 the only problematic value? because in that case...
>
> Fixes: 0dd714bfd200 ("KVM: s390: selftest: memop: Add cmpxchg tests")
> Signed-off-by: Nina Schoetterl-Glausch <nsg@linux.ibm.com>
> ---
> tools/testing/selftests/kvm/s390x/memop.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/s390x/memop.c b/tools/testing/selftests/kvm/s390x/memop.c
> index bb3ca9a5d731..2eba9575828e 100644
> --- a/tools/testing/selftests/kvm/s390x/memop.c
> +++ b/tools/testing/selftests/kvm/s390x/memop.c
> @@ -485,11 +485,13 @@ static bool popcount_eq(__uint128_t a, __uint128_t b)
>
> static __uint128_t rotate(int size, __uint128_t val, int amount)
> {
> - unsigned int bits = size * 8;
> + unsigned int left, right, bits = size * 8;
>
...why not just:
if (!amount)
return val;
?
> - amount = (amount + bits) % bits;
> + right = (amount + bits) % bits;
> + /* % 128 prevents left shift UB if size == 16 && right == 0 */
> + left = (bits - right) % 128;
> val = cut_to_size(size, val);
> - return (val << (bits - amount)) | (val >> amount);
> + return (val << left) | (val >> right);
> }
>
> const unsigned int max_block = 16;
>
> base-commit: 305230142ae0637213bf6e04f6d9f10bbcb74af8
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] KVM: s390: selftest: memop: Fix undefined behavior
2023-12-15 17:02 ` Claudio Imbrenda
@ 2023-12-18 12:18 ` Nina Schoetterl-Glausch
2023-12-18 14:58 ` Claudio Imbrenda
0 siblings, 1 reply; 4+ messages in thread
From: Nina Schoetterl-Glausch @ 2023-12-18 12:18 UTC (permalink / raw)
To: Claudio Imbrenda
Cc: Paolo Bonzini, Shuah Khan, Janosch Frank, Christian Borntraeger,
kvm, linux-kselftest, David Hildenbrand, linux-kernel
On Fri, 2023-12-15 at 18:02 +0100, Claudio Imbrenda wrote:
> On Fri, 15 Dec 2023 17:11:25 +0100
> Nina Schoetterl-Glausch <nsg@linux.ibm.com> wrote:
>
> > If an integer's type has x bits, shifting the integer left by x or more
> > is undefined behavior.
> > This can happen in the rotate function when attempting to do a rotation
> > of the whole value by 0.
>
> is 0 the only problematic value? because in that case...
>
> >
> > Fixes: 0dd714bfd200 ("KVM: s390: selftest: memop: Add cmpxchg tests")
> > Signed-off-by: Nina Schoetterl-Glausch <nsg@linux.ibm.com>
> > ---
> > tools/testing/selftests/kvm/s390x/memop.c | 8 +++++---
> > 1 file changed, 5 insertions(+), 3 deletions(-)
> >
> > diff --git a/tools/testing/selftests/kvm/s390x/memop.c b/tools/testing/selftests/kvm/s390x/memop.c
> > index bb3ca9a5d731..2eba9575828e 100644
> > --- a/tools/testing/selftests/kvm/s390x/memop.c
> > +++ b/tools/testing/selftests/kvm/s390x/memop.c
> > @@ -485,11 +485,13 @@ static bool popcount_eq(__uint128_t a, __uint128_t b)
> >
> > static __uint128_t rotate(int size, __uint128_t val, int amount)
> > {
> > - unsigned int bits = size * 8;
> > + unsigned int left, right, bits = size * 8;
> >
>
> ...why not just:
>
> if (!amount)
> return val;
>
> ?
That works if you move it one statement down (128 would also trigger UB).
% 128 does the trick, is branchless and there is a bit of a symmetry going
on between right and left.
But I can use an early return if you want.
>
> > - amount = (amount + bits) % bits;
> > + right = (amount + bits) % bits;
> > + /* % 128 prevents left shift UB if size == 16 && right == 0 */
> > + left = (bits - right) % 128;
> > val = cut_to_size(size, val);
> > - return (val << (bits - amount)) | (val >> amount);
> > + return (val << left) | (val >> right);
> > }
> >
> > const unsigned int max_block = 16;
> >
> > base-commit: 305230142ae0637213bf6e04f6d9f10bbcb74af8
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] KVM: s390: selftest: memop: Fix undefined behavior
2023-12-18 12:18 ` Nina Schoetterl-Glausch
@ 2023-12-18 14:58 ` Claudio Imbrenda
0 siblings, 0 replies; 4+ messages in thread
From: Claudio Imbrenda @ 2023-12-18 14:58 UTC (permalink / raw)
To: Nina Schoetterl-Glausch
Cc: Paolo Bonzini, Shuah Khan, Janosch Frank, Christian Borntraeger,
kvm, linux-kselftest, David Hildenbrand, linux-kernel
On Mon, 18 Dec 2023 13:18:14 +0100
Nina Schoetterl-Glausch <nsg@linux.ibm.com> wrote:
[...]
> > > diff --git a/tools/testing/selftests/kvm/s390x/memop.c b/tools/testing/selftests/kvm/s390x/memop.c
> > > index bb3ca9a5d731..2eba9575828e 100644
> > > --- a/tools/testing/selftests/kvm/s390x/memop.c
> > > +++ b/tools/testing/selftests/kvm/s390x/memop.c
> > > @@ -485,11 +485,13 @@ static bool popcount_eq(__uint128_t a, __uint128_t b)
> > >
> > > static __uint128_t rotate(int size, __uint128_t val, int amount)
> > > {
> > > - unsigned int bits = size * 8;
> > > + unsigned int left, right, bits = size * 8;
> > >
> >
> > ...why not just:
> >
> > if (!amount)
> > return val;
> >
> > ?
>
> That works if you move it one statement down (128 would also trigger UB).
oops, yes it has to be after
> % 128 does the trick, is branchless and there is a bit of a symmetry going
> on between right and left.
> But I can use an early return if you want.
I think it's more readable, and furthermore...
>
> >
> > > - amount = (amount + bits) % bits;
> > > + right = (amount + bits) % bits;
> > > + /* % 128 prevents left shift UB if size == 16 && right == 0 */
> > > + left = (bits - right) % 128;
> > > val = cut_to_size(size, val);
> > > - return (val << (bits - amount)) | (val >> amount);
...this is a more idiomatic syntax for a rotate operation
> > > + return (val << left) | (val >> right);
> > > }
> > >
> > > const unsigned int max_block = 16;
> > >
> > > base-commit: 305230142ae0637213bf6e04f6d9f10bbcb74af8
> >
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-12-18 14:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-15 16:11 [PATCH] KVM: s390: selftest: memop: Fix undefined behavior Nina Schoetterl-Glausch
2023-12-15 17:02 ` Claudio Imbrenda
2023-12-18 12:18 ` Nina Schoetterl-Glausch
2023-12-18 14:58 ` Claudio Imbrenda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®