Skip to contentSkip to conversation

selftests: kvm: fix shift of 32 bit unsigned int more than 32 bits

PATCH Superseded Colin Ian King 2 participants 3 messages May 23, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: kvm: fix shift of 32 bit unsigned int more than 32 bits

Currrentl a 32 bit 1u value is being shifted more than 32 bits causing overflow and incorrect checking of bits 32-63. Fix this by using the BIT_ULL macro for shifting bits.

Detected by cppcheck:
sev_init2_tests.c:108:34: error: Shifting 32-bit value by 63 bits is undefined behaviour [shiftTooManyBits]

Fixes dfc083a181ba ("selftests: kvm: add tests for KVM_SEV_INIT2")
---
 tools/testing/selftests/kvm/x86_64/sev_init2_tests.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86_64/sev_init2_tests.c +2 -2
index 7a4a61be119b..ea09f7a06aa4 100644
@@ -105,11 +105,11 @@ void test_features(uint32_t vm_type, uint64_t supported_features)
 	int i;
 
 	for (i = 0; i < 64; i++) {
-		if (!(supported_features & (1u << i)))
+		if (!(supported_features & BIT_ULL(i)))
 			test_init2_invalid(vm_type,
 				&(struct kvm_sev_init){ .vmsa_features = BIT_ULL(i) },
 				"unknown feature");
-		else if (KNOWN_FEATURES & (1u << i))
+		else if (KNOWN_FEATURES & BIT_ULL(u))
 			test_init2(vm_type,
 				&(struct kvm_sev_init){ .vmsa_features = BIT_ULL(i) });
 	}
Signature · 1 line
2.39.2
#1 lore raw
Quoted 19 lines · Colin Ian King

Currrentl a 32 bit 1u value is being shifted more than 32 bits causing overflow and incorrect checking of bits 32-63. Fix this by using the BIT_ULL macro for shifting bits.

Detected by cppcheck:
sev_init2_tests.c:108:34: error: Shifting 32-bit value by 63 bits is undefined behaviour [shiftTooManyBits]

Fixes dfc083a181ba ("selftests: kvm: add tests for KVM_SEV_INIT2")
---
 tools/testing/selftests/kvm/x86_64/sev_init2_tests.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86_64/sev_init2_tests.c +0 -0
index 7a4a61be119b..ea09f7a06aa4 100644

tools/testing/selftests/kvm/x86_64/sev_init2_tests.c void test_features(uint32_t vm_type, uint64_t supported_features) · line 112

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_init2_tests.c +2 -2
@@ -105,11 +105,11 @@ void test_features(uint32_t vm_type, uint64_t supported_features)
 	int i;
 
 	for (i = 0; i < 64; i++) {
-		if (!(supported_features & (1u << i)))
+		if (!(supported_features & BIT_ULL(i)))
 			test_init2_invalid(vm_type,
 				&(struct kvm_sev_init){ .vmsa_features = BIT_ULL(i) },
 				"unknown feature");
-		else if (KNOWN_FEATURES & (1u << i))
+		else if (KNOWN_FEATURES & BIT_ULL(u))
                                                  ^

Should be i. How does this build? :P

regards,
dan carpenter

#2 lore raw
Quoted 31 lines · Dan Carpenter
Quoted 19 lines · Colin Ian King

Currrentl a 32 bit 1u value is being shifted more than 32 bits causing overflow and incorrect checking of bits 32-63. Fix this by using the BIT_ULL macro for shifting bits.

Detected by cppcheck:
sev_init2_tests.c:108:34: error: Shifting 32-bit value by 63 bits is undefined behaviour [shiftTooManyBits]

Fixes dfc083a181ba ("selftests: kvm: add tests for KVM_SEV_INIT2")
---
  tools/testing/selftests/kvm/x86_64/sev_init2_tests.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86_64/sev_init2_tests.c +0 -0
index 7a4a61be119b..ea09f7a06aa4 100644

tools/testing/selftests/kvm/x86_64/sev_init2_tests.c void test_features(uint32_t vm_type, uint64_t supported_features) · line 112

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_init2_tests.c +2 -2
@@ -105,11 +105,11 @@ void test_features(uint32_t vm_type, uint64_t supported_features)
  	int i;
 
  	for (i = 0; i < 64; i++) {
-		if (!(supported_features & (1u << i)))
+		if (!(supported_features & BIT_ULL(i)))
  			test_init2_invalid(vm_type,
  				&(struct kvm_sev_init){ .vmsa_features = BIT_ULL(i) },
  				"unknown feature");
-		else if (KNOWN_FEATURES & (1u << i))
+		else if (KNOWN_FEATURES & BIT_ULL(u))
                                                   ^
Quoted 1 line

Should be i. How does this build? :P

good catch, I sent the wrong one :-(

Quoted 4 lines

regards,
dan carpenter