All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] packed structures and unaligned accesses (sparc)
@ 2017-03-27 17:34 Peter Maydell
  2017-03-27 21:43 ` Richard Henderson
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Peter Maydell @ 2017-03-27 17:34 UTC (permalink / raw)
  To: QEMU Developers, Richard Henderson; +Cc: Knut Omang, John Paul Adrian Glaubitz

At the moment the 9p QEMU tests fail on SPARC. This turns out to
be because the test case itself gets a SIGBUS. Looking at the
code I guess it makes sense, but I don't understand why the
code didn't at least generate a warning. Here's a cutdown testcase:

pm215@stadler:~$ cat packed.c
#include <stdio.h>
#include <inttypes.h>

typedef struct {
  uint32_t size;
  uint8_t id;
  uint16_t tag;
} __attribute__((packed)) hdr;

uint32_t getval(uint32_t *p) {
  return *p;
}

uint32_t foo(void) {
  hdr h;
  h.size = 42;

  return getval(&h.size);
}

int main(void) {
  printf("got 0x%x\n", foo());
  return 0;
}
pm215@stadler:~$ gcc -Wall -o packed packed.c
pm215@stadler:~$ ./packed
Bus error

(This is with gcc (Debian 6.3.0-10) 6.3.0 20170321.)

The bus error happens because:
 * the compiler decides to put the local 'hdr h' in foo() at
an unaligned address (which is allowed although I'm not
quite sure why it chooses to do so here given it's the only
local in the function)
 * getval() is compiled to code that assumes the pointer is
aligned
 * the address of h.size isn't aligned, so the call to getval()
blows up

That all makes sense in isolation, but shouldn't something have
at least warned that "&h.size" isn't actually a uint32_t* in
the sense of being something you can validly pass to a
function that takes a uint32_t* ?

thanks
-- PMM

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-03-28 10:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-27 17:34 [Qemu-devel] packed structures and unaligned accesses (sparc) Peter Maydell
2017-03-27 21:43 ` Richard Henderson
2017-03-27 22:13 ` John Paul Adrian Glaubitz
2017-03-28 10:19 ` John Paul Adrian Glaubitz
2017-03-28 10:23   ` Peter Maydell

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.