Re: [kvm-unit-tests PATCH 5/7] svm: convert neg shift to unsigned shift

Paolo Bonzini
2020-02-28T11:13:00Z

On 26/02/20 10:44, Bill Wendling wrote:
> Shifting a negative signed value is undefined. Use a shift of an
> unsigned value instead.
>
> Signed-off-by: Bill Wendling <morbo@google.com>

In practice only UBSAN complains; GCC even documents that it does not
treat it as undefined (which would be insane, especially if the
multiplication by 2 does not overflow as in this case):

GCC does not use the latitude given in C99 only to treat certain
aspects of signed '<<' as undefined, but this is subject to change.

But in the interest of purity we should instead compile with -fwrapv.

Paolo

> ---
> x86/svm.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/x86/svm.c b/x86/svm.c
> index ae85194..17be4b0 100644
> --- a/x86/svm.c
> +++ b/x86/svm.c
> @@ -1148,7 +1148,7 @@ static bool npt_rw_l1mmio_check(struct test *test)
> }
>
> #define TSC_ADJUST_VALUE (1ll << 32)
> -#define TSC_OFFSET_VALUE (-1ll << 48)
> +#define TSC_OFFSET_VALUE (~0ull << 48)
> static bool ok;
>
> static void tsc_adjust_prepare(struct test *test)
>