All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Henderson <rth@twiddle.net>
To: "Michael S. Tsirkin" <mst@redhat.com>, qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Anthony Liguori <aliguori@amazon.com>
Subject: Re: [Qemu-devel] [PATCH 2/2] acpi-build: don't access unaligned addresses
Date: Wed, 12 Mar 2014 15:24:23 -0700	[thread overview]
Message-ID: <5320DE97.9030300@twiddle.net> (raw)
In-Reply-To: <1394480575-3698-2-git-send-email-mst@redhat.com>

On 03/10/2014 12:43 PM, Michael S. Tsirkin wrote:
> casting an unaligned address to e.g.
> uint32_t can trigger undefined behaviour in C.
> Replace cast + assignment with memcpy.
> 
> Reported-by: Peter Maydell <peter.maydell@linaro.org>
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>  hw/i386/acpi-build.c | 31 ++++++++++++++++---------------
>  1 file changed, 16 insertions(+), 15 deletions(-)
> 
> diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
> index b667d31..e08f514 100644
> --- a/hw/i386/acpi-build.c
> +++ b/hw/i386/acpi-build.c
> @@ -466,9 +466,15 @@ static void acpi_align_size(GArray *blob, unsigned align)
>      g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
>  }
>  
> -/* Get pointer within table in a safe manner */
> -#define ACPI_BUILD_PTR(table, size, off, type) \
> -    ((type *)(acpi_data_get_ptr(table, size, off, sizeof(type))))
> +/* Set a value within table in a safe manner */
> +#define ACPI_BUILD_SET_LE(table, size, off, bits, val) \
> +    do { \
> +        uint64_t ACPI_BUILD_SET_LE_val = le_bswap(val, bits); \
> +        memcpy(acpi_data_get_ptr(table, size, off, \
> +                                 (bits) / BITS_PER_BYTE), \
> +               &ACPI_BUILD_SET_LE_val, \
> +               (bits) / BITS_PER_BYTE); \
> +    } while (0)
>  
>  static inline void *acpi_data_get_ptr(uint8_t *table_data, unsigned table_size,
>                                        unsigned off, unsigned size)
> @@ -974,22 +980,17 @@ static void build_pci_bus_end(PCIBus *bus, void *bus_state)
>  
>  static void patch_pci_windows(PcPciInfo *pci, uint8_t *start, unsigned size)
>  {
> -    *ACPI_BUILD_PTR(start, size, acpi_pci32_start[0], uint32_t) =
> -        cpu_to_le32(pci->w32.begin);
> +    ACPI_BUILD_SET_LE(start, size, acpi_pci32_start[0], 32, pci->w32.begin);

You're working too hard at this because you're using the wrong bswap interface.

Also, you missed one.

I'll follow up with a patch I wrote just recently, but hadn't yet gotten around
to posting.


r~

  reply	other threads:[~2014-03-12 22:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-10 19:43 [Qemu-devel] [PATCH 1/2] bswap.h: export bswap_le Michael S. Tsirkin
2014-03-10 19:43 ` [Qemu-devel] [PATCH 2/2] acpi-build: don't access unaligned addresses Michael S. Tsirkin
2014-03-12 22:24   ` Richard Henderson [this message]
2014-03-12 22:25   ` [Qemu-devel] [PATCH] hw/i386: Use unaligned store functions building acpi tables Richard Henderson
2014-03-12 23:26     ` Peter Maydell
2014-03-13 13:30       ` Richard Henderson
2014-03-12 22:21 ` [Qemu-devel] [PATCH 1/2] bswap.h: export bswap_le Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5320DE97.9030300@twiddle.net \
    --to=rth@twiddle.net \
    --cc=aliguori@amazon.com \
    --cc=mst@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.