Skip to contentSkip to conversation

KVM: s390: selftest: memop: Fix undefined behavior

PATCH Superseded Nina Schoetterl-Glausch 2 participants 4 messages Dec 15, 2023 – Dec 18, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: s390: selftest: memop: Fix undefined behavior

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")
---
 tools/testing/selftests/kvm/s390x/memop.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/s390x/memop.c +5 -3
index bb3ca9a5d731..2eba9575828e 100644
@@ -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

Signature · 1 line
2.40.1
#1 lore raw
Quoted 4 lines · On Fri, 15 Dec 2023 17:11:25 +0100 Nina Schoetterl-Glausch <[email protected]>…

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

Quoted 12 lines
Fixes 0dd714bfd200 ("KVM: s390: selftest: memop: Add cmpxchg tests")
---
 tools/testing/selftests/kvm/s390x/memop.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/s390x/memop.c +0 -0
index bb3ca9a5d731..2eba9575828e 100644

tools/testing/selftests/kvm/s390x/memop.c static bool popcount_eq(__uint128_t a, __uint128_t b) · line 489

Quoted 6 lines
tools/testing/selftests/kvm/s390x/memop.c +1 -1
@@ -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;

?

Quoted 12 lines
tools/testing/selftests/kvm/s390x/memop.c +4 -2
-	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 lore raw
Quoted 31 lines · Claudio Imbrenda
Quoted 4 lines · On Fri, 15 Dec 2023 17:11:25 +0100 Nina Schoetterl-Glausch <[email protected]>…

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

Quoted 12 lines
Fixes 0dd714bfd200 ("KVM: s390: selftest: memop: Add cmpxchg tests")
---
 tools/testing/selftests/kvm/s390x/memop.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/s390x/memop.c +0 -0
index bb3ca9a5d731..2eba9575828e 100644

tools/testing/selftests/kvm/s390x/memop.c static bool popcount_eq(__uint128_t a, __uint128_t b) · line 489

Quoted 6 lines
tools/testing/selftests/kvm/s390x/memop.c +1 -1
@@ -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:

Quoted 4 lines

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.

Quoted 14 lines
Quoted 13 lines
tools/testing/selftests/kvm/s390x/memop.c +4 -2
-	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

#3 lore raw

On Mon, 18 Dec 2023 13:18:14 +0100
Nina Schoetterl-Glausch <[email protected]> wrote:

[...]

Quoted 14 lines
Quoted 14 lines

tools/testing/selftests/kvm/s390x/memop.c static bool popcount_eq(__uint128_t a, __uint128_t b) · line 489

Quoted 11 lines
tools/testing/selftests/kvm/s390x/memop.c +1 -1
index bb3ca9a5d731..2eba9575828e 100644
@@ -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:

Quoted 6 lines
Quoted 4 lines

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

Quoted 3 lines

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

Quoted 8 lines
Quoted 8 lines

tools/testing/selftests/kvm/s390x/memop.c static bool popcount_eq(__uint128_t a, __uint128_t b) · line 492

Quoted 8 lines
tools/testing/selftests/kvm/s390x/memop.c +3 -2
-	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

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
tools/testing/selftests/kvm/s390x/memop.c +1 -0
+	return (val << left) | (val >> right);
 }
 
 const unsigned int max_block = 16;

base-commit: 305230142ae0637213bf6e04f6d9f10bbcb74af8