[Spice-devel] [PATCH spice-protocol] stream-device: Force proper packing of protocol structures

Christophe Fergeau cfergeau at redhat.com
Wed Mar 7 15:03:43 UTC 2018


On Wed, Mar 07, 2018 at 11:08:58AM +0000, Frediano Ziglio wrote:
> Assuming the packing of structure can be a portability issue.
> Currently there are no real-world ABI that would break these structure
> however to be safe force a 4 byte packing.

I don't think this is a "4 byte packing", structure is packed, and not
too badly if we ensure that fields are naturally aligned to their
sizeof, and then add some padding so that the structure size is a
multiple of 4 bytes.

Christophe

> 
> Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
> ---
>  spice/Makefile.am     |  2 ++
>  spice/end-packed4.h   | 38 ++++++++++++++++++++++++++++++
>  spice/start-packed4.h | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  spice/stream-device.h |  4 ++++
>  4 files changed, 108 insertions(+)
>  create mode 100644 spice/end-packed4.h
>  create mode 100644 spice/start-packed4.h
> 
> diff --git a/spice/Makefile.am b/spice/Makefile.am
> index 4f9a607..1b48d30 100644
> --- a/spice/Makefile.am
> +++ b/spice/Makefile.am
> @@ -6,6 +6,7 @@ spice_protocol_include_HEADERS =		\
>  	barrier.h				\
>  	controller_prot.h			\
>  	end-packed.h				\
> +	end-packed4.h				\
>  	enums.h					\
>  	error_codes.h				\
>  	foreign_menu_prot.h			\
> @@ -15,6 +16,7 @@ spice_protocol_include_HEADERS =		\
>  	qxl_dev.h				\
>  	qxl_windows.h				\
>  	start-packed.h				\
> +	start-packed4.h				\
>  	stats.h					\
>  	types.h					\
>  	vd_agent.h				\
> diff --git a/spice/end-packed4.h b/spice/end-packed4.h
> new file mode 100644
> index 0000000..648370b
> --- /dev/null
> +++ b/spice/end-packed4.h
> @@ -0,0 +1,38 @@
> +/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
> +/*
> +   Copyright (C) 2009-2018 Red Hat, Inc.
> +
> +   Redistribution and use in source and binary forms, with or without
> +   modification, are permitted provided that the following conditions are
> +   met:
> +
> +       * Redistributions of source code must retain the above copyright
> +         notice, this list of conditions and the following disclaimer.
> +       * Redistributions in binary form must reproduce the above copyright
> +         notice, this list of conditions and the following disclaimer in
> +         the documentation and/or other materials provided with the
> +         distribution.
> +       * Neither the name of the copyright holder nor the names of its
> +         contributors may be used to endorse or promote products derived
> +         from this software without specific prior written permission.
> +
> +   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
> +   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> +   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
> +   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
> +   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
> +   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
> +   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> +   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> +   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> +   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
> +   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> +*/
> +
> +/* See start-packed4.h for details */
> +
> +#undef SPICE_ATTR_PACKED4
> +
> +#if defined(__MINGW32__) || !defined(__GNUC__)
> +#pragma pack(pop)
> +#endif
> diff --git a/spice/start-packed4.h b/spice/start-packed4.h
> new file mode 100644
> index 0000000..a402c64
> --- /dev/null
> +++ b/spice/start-packed4.h
> @@ -0,0 +1,64 @@
> +/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
> +/*
> +   Copyright (C) 2009-2018 Red Hat, Inc.
> +
> +   Redistribution and use in source and binary forms, with or without
> +   modification, are permitted provided that the following conditions are
> +   met:
> +
> +       * Redistributions of source code must retain the above copyright
> +         notice, this list of conditions and the following disclaimer.
> +       * Redistributions in binary form must reproduce the above copyright
> +         notice, this list of conditions and the following disclaimer in
> +         the documentation and/or other materials provided with the
> +         distribution.
> +       * Neither the name of the copyright holder nor the names of its
> +         contributors may be used to endorse or promote products derived
> +         from this software without specific prior written permission.
> +
> +   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
> +   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> +   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
> +   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
> +   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
> +   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
> +   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> +   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> +   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> +   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
> +   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> +*/
> +
> +/* Ideally this should all have been macros in a common headers, but
> + * its not possible to put pragmas into header files, so we have
> + * to use include magic.
> + *
> + * Use it like this:
> + *
> + * #include <spice/start-packed4.h>
> + *
> + * typedef struct SPICE_ATTR_PACKED4 {
> + *    ...
> + * } Type;
> + *
> + * #include <spice/end-packed4.h>
> + *
> + */
> +
> +#ifdef __GNUC__
> +
> +#define SPICE_ATTR_PACKED4 __attribute__ ((__packed__, __aligned__(4)))
> +
> +#ifdef __MINGW32__
> +#pragma pack(push,4)
> +#endif
> +
> +#else
> +
> +#pragma pack(push)
> +#pragma pack(4)
> +#define SPICE_ATTR_PACKED4
> +#pragma warning(disable:4200)
> +#pragma warning(disable:4103)
> +
> +#endif
> diff --git a/spice/stream-device.h b/spice/stream-device.h
> index 2e7c50e..e72a675 100644
> --- a/spice/stream-device.h
> +++ b/spice/stream-device.h
> @@ -43,6 +43,8 @@
>  
>  #include <spice/types.h>
>  
> +#include <spice/start-packed4.h>
> +
>  /*
>   * Structures are all "naturally aligned"
>   * containing integers up to 64 bit.
> @@ -210,4 +212,6 @@ typedef struct StreamMsgCursorMove {
>      int32_t y;
>  } StreamMsgCursorMove;
>  
> +#include <spice/end-packed4.h>
> +
>  #endif /* SPICE_STREAM_DEVICE_H_ */
> -- 
> 2.14.3
> 
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/spice-devel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.freedesktop.org/archives/spice-devel/attachments/20180307/bb49ff55/attachment.sig>


More information about the Spice-devel mailing list