RE: [PATCH v2] x86/insn, tools/x86: Fix some potential undefined behavior.

From: David Laight
Date: Fri Oct 16 2020 - 04:24:46 EST


From: Ian Rogers
> Sent: 15 October 2020 22:47
...
> The decoder is a shared code and using unaligned macros makes life
> hard for the other users of the code. Memcpy is the "standard"
> workaround for this kind of undefined behavior.
> https://lore.kernel.org/lkml/e4269cb2-d8e6-da26-6afd-a9df72d4be36@xxxxxxxxx/

You can't always use memcpy() to copy unaligned data.
If the compiler can find any reason why the misaligned address
should be aligned (eg because it's type prior to some casts
requires alignment) it will use word-sized accesses that will fault.

Now in this specific code the pointers are probably all 'char *'
so have alignment 1 - so memcpy() will DTRT.

But it may generate an expensive function call.

There are 'read unaligned 32bit' (etc) macros out there.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)