Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH][next] selftests: kvm: fix shift of 32 bit unsigned int more than 32 bits
@ 2024-05-23 11:38 Colin Ian King
  2024-05-23 15:35 ` Dan Carpenter
  0 siblings, 1 reply; 3+ messages in thread
From: Colin Ian King @ 2024-05-23 11:38 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Shuah Khan, kvm,
	linux-kselftest
  Cc: kernel-janitors, linux-kernel

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")
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
---
 tools/testing/selftests/kvm/x86_64/sev_init2_tests.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
index 7a4a61be119b..ea09f7a06aa4 100644
--- a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
+++ b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
@@ -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) });
 	}
-- 
2.39.2


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

* Re: [PATCH][next] selftests: kvm: fix shift of 32 bit unsigned int more than 32 bits
  2024-05-23 11:38 [PATCH][next] selftests: kvm: fix shift of 32 bit unsigned int more than 32 bits Colin Ian King
@ 2024-05-23 15:35 ` Dan Carpenter
  2024-05-23 15:37   ` Colin King (gmail)
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2024-05-23 15:35 UTC (permalink / raw)
  To: Colin Ian King
  Cc: Sean Christopherson, Paolo Bonzini, Shuah Khan, kvm,
	linux-kselftest, kernel-janitors, linux-kernel

On Thu, May 23, 2024 at 12:38:02PM +0100, Colin Ian King wrote:
> 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")
> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
> ---
>  tools/testing/selftests/kvm/x86_64/sev_init2_tests.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
> index 7a4a61be119b..ea09f7a06aa4 100644
> --- a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
> +++ b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
> @@ -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


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

* Re: [PATCH][next] selftests: kvm: fix shift of 32 bit unsigned int more than 32 bits
  2024-05-23 15:35 ` Dan Carpenter
@ 2024-05-23 15:37   ` Colin King (gmail)
  0 siblings, 0 replies; 3+ messages in thread
From: Colin King (gmail) @ 2024-05-23 15:37 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Sean Christopherson, Paolo Bonzini, Shuah Khan, kvm,
	linux-kselftest, kernel-janitors, linux-kernel

On 23/05/2024 16:35, Dan Carpenter wrote:
> On Thu, May 23, 2024 at 12:38:02PM +0100, Colin Ian King wrote:
>> 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")
>> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
>> ---
>>   tools/testing/selftests/kvm/x86_64/sev_init2_tests.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
>> index 7a4a61be119b..ea09f7a06aa4 100644
>> --- a/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
>> +++ b/tools/testing/selftests/kvm/x86_64/sev_init2_tests.c
>> @@ -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

good catch, I sent the wrong one :-(
> 
> regards,
> dan carpenter
> 


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

end of thread, other threads:[~2024-05-23 15:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-23 11:38 [PATCH][next] selftests: kvm: fix shift of 32 bit unsigned int more than 32 bits Colin Ian King
2024-05-23 15:35 ` Dan Carpenter
2024-05-23 15:37   ` Colin King (gmail)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox