Re: [PATCH spice-gtk v7 2/4] Avoid clang warnings on casts with stricter alignment requirements

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jun 08, 2017 at 06:17:31AM -0400, Frediano Ziglio wrote:
> > 
> > From: Christophe de Dinechin <dinechin@xxxxxxxxxx>
> > 
> > For example, something like this:
> >     uint8_t  *p8;
> >     uint32_t *p32 = (uint32_t *) p8;
> > 
> > generates a warning like this:
> >   spice-channel.c:1350:10: error: cast from 'uint8_t *' (aka 'unsigned char
> >   *') to
> >       'uint32_t *' (aka 'unsigned int *') increases required alignment from 1
> >       to
> >       4 [-Werror,-Wcast-align]
> > 
> > The warning indicates that we end up with a pointer to data that
> > should be 4-byte aligned, but its value may be misaligned. On x86,
> > this does not make much of a difference, except a relatively minor
> > performance penalty. However, on platforms such as older ARM, misaligned
> > accesses are emulated by the kernel, and support for them is optional.
> > So we may end up with a fault.
> > 
> > The intent of the fix here is to make it easy to identify and rework
> > places where actual mis-alignment occurs. Wherever casts raise the warning,
> > they are replaced with a macro:
> > 
> > - SPICE_ALIGNED_CAST(type, value) casts value to type, and indicates that
> >   we believe the resulting pointer is aligned. If it is not, a runtime
> >   warning will be issued.
> > 
> > - SPICE_UNALIGNED_CAST(type, value) casts value to type, and indicates that
> >   we believe the resulting pointer is not always aligned
> > 
> > Any code using SPICE_UNALIGNED_CAST may need to be revisited in order
> > to improve performance, e.g. by using memcpy.
> > 
> > There are normally no warnings for SPICE_UNALIGNED_CAST, but it is possible
> > to emit debug messages for mis-alignment in SPICE_UNALIGNED_CAST.
> > Set environment variable SPICE_DEBUG_ALIGNMENT to activate this check.
> > 
> > Runtime warnings are enabled by the --enable-alignment-checks configure
> > option (or #define SPICE_DEBUG_ALIGNMENT).
> > 
> > Signed-off-by: Christophe de Dinechin <dinechin@xxxxxxxxxx>
> 
> Honestly I would start nacking this patch.
> 
> Or you disable the warning or you fix it where possible and leave
> the warning for a time when we'll really need to fix it.

This patch adds annotations indicating whether the data is known to be
aligned or not, this is better than just disabling the warning.
Then you can ask the question of what should the macro implementation
do, as it could just be a no-op, and we remove -Waligned-cast.
Personally I'd keep the macro which does the appropriate cast to silence
the warning, and have optional runtime checks if needed.

Christophe

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel

[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]