Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* Fixing "unaligned trap" errors
@ 2000-05-16 12:13 David Rysdam
  2000-05-19 17:51 ` David Huggins-Daines
  0 siblings, 1 reply; 5+ messages in thread
From: David Rysdam @ 2000-05-16 12:13 UTC (permalink / raw)
  To: axp-list

I've searched the list archives, but I can only find
one post that addresses how to fix "unaligned trap"
messages in one's own program.

What I've been able to figure out so far:

-What file/line the error is in.

What I still need to know:

-What the problem is.
-How to fix it.

Can anyone give any guidance?

__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/


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

* Re: Fixing "unaligned trap" errors
@ 2000-05-19 15:43 William H. Magill
  0 siblings, 0 replies; 5+ messages in thread
From: William H. Magill @ 2000-05-19 15:43 UTC (permalink / raw)
  To: axp-list; +Cc: drysdam

>   Resent-Date: 19 May 2000 14:30:45 -0000
>   MBOX-Line: From axp-list-request@redhat.com  Fri May 19 10:30:43 2000
>   Date: Tue, 16 May 2000 05:13:26 -0700 (PDT)
>   From: David Rysdam <drysdam@yahoo.com>
>
>   I've searched the list archives, but I can only find
>   one post that addresses how to fix "unaligned trap"
>   messages in one's own program.
>
>   What I've been able to figure out so far:
>
>   -What file/line the error is in.
>
>   What I still need to know:
>
>   -What the problem is.
>   -How to fix it.
>
>   Can anyone give any guidance?
>

It looks like the axp-list has been delayed at red-hat all week.
I just received this a few minutes ago

Your problem is not a Linux problem, but a 64 bit architecture problem.

... Check out the Tru64 Unix Alpha porting guides.

http://www.tru64unix.compaq.com/faqs/publications/pub_page/porting.html

The full Tru64 unix docs are on-line at:

http://www.tru64unix.compaq.com/faqs/publications/pub_page/doc_list.html

The Compaq (DEC C) compilers are avaialble at:
http://www.unix.digital.com/linux/compaq_c/index.html
If you haven't tried them instead of GCC, you will experience a 15-30%
performance improvement for most code and possibly a 200% improvement
if you do a lot of floating point.

-- 
                        www.tru64unix.compaq.com
                              www.tru64.org
                             comp.unix.tru64
                        
T.T.F.N.
William H. Magill                          Senior Systems Administrator
Information Services and Computing (ISC)   University of Pennsylvania
Internet: magill@isc.upenn.edu             magill@acm.org
http://www.isc-net.upenn.edu/~magill/


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

* Re: Fixing "unaligned trap" errors
  2000-05-16 12:13 Fixing "unaligned trap" errors David Rysdam
@ 2000-05-19 17:51 ` David Huggins-Daines
  2000-05-20 17:41   ` Michal Jaegermann
  0 siblings, 1 reply; 5+ messages in thread
From: David Huggins-Daines @ 2000-05-19 17:51 UTC (permalink / raw)
  To: axp-list

David Rysdam <drysdam@yahoo.com> writes:

> I've searched the list archives, but I can only find
> one post that addresses how to fix "unaligned trap"
> messages in one's own program.
> 
> What I've been able to figure out so far:
> 
> -What file/line the error is in.

You're doing pretty well, then.  You still need to find out exactly
which variable or pointer is causing the fault.  GDB, 'objdump
--source', or printf() should all serve equally well :-)

> What I still need to know:
> 
> -What the problem is.

Alpha requires data to be "naturally aligned".  In other words, data
of type X must be stored at a memory location Y such that
Y = 0 mod sizeof(X). [1]

Therefore the simplest description of the problem is that you are
accessing a 32-bit or 64-bit number via a pointer that doesn't point
to a 4 or 8-byte boundary.  e.g.:

        char foo[24];
        int * bar = foo + 2; /* BAD:  2 mod 4 == 2 */
        int * baz = foo + 8; /* GOOD: 8 mod 4 == 0 */
        long * quux = foo + 12; /* BAD:  12 mod 8 == 4 */
        long * argh = foo + 16; /* GOOD: 16 mod 8 == 0 */

Lots of legacy binary data formats have unaligned fields, because at
the time, saving a few bytes on the 16-bit machines of the time was a
higher priority than planning for 32 and 64-bit RISC machines of the
future.

For example ... IP headers, DOS partition tables, DWARF2 exception
frame info.

In general whenever you cast from a pointer to a smaller type to a
pointer of a larger type you have a potential alignment problem.  (and
of course, if the smaller type is not char*, you will also have
problems with strict aliasing).  So, for example, if you define a
global variable:  (this was a bug in Linux fdisk)

        char foo[512];

Then later on do:

        uint64_t *bar = (uint64_t *) foo;
        *bar = 42;

You have a good chance of producing an unaligned error.  [2]

Another way to cause this problem is if you concatenate two structs of
different type in a single block of memory (this happens a lot when
reading and writing binary file formats).  For example:

--- start program ---
#include <stdio.h>
#include <stdlib.h>

/* sizeof(struct foo) == 12 */
struct foo {
        int a, b, c;
};
struct bar {
        long d, e, f;
};

int main()
{
        char *quux = malloc(sizeof(struct foo) + sizeof(struct bar));
        struct foo *s1 = (struct foo *) quux;
        struct bar *s2 = (struct bar *) (quux + sizeof(*s1));

        printf("s1 @ %#p, s2 @ %#p\n", s1, s2);
        s2->d = 42;
        return 0;
}
--- end program ---

Results in:

[dhd@curacao]:~$ ./a.out 
s1 @ 0x120100970, s2 @ 0x12010097c
[dhd@curacao]:~$ dmesg | tail -1
a.out(29802): unaligned trap at 00000001200005ec: 000000012010097c 2d 2

> -How to fix it.

Well, if you can at all help it, don't play pointer games that result
in unaligned data.  In particular, if you are designing a binary file
format, make sure that all data in it are naturally aligned.

GCC has an __alignof__ keyword that works like 'sizeof' but evaluates
to the required or optimal alignment for a type or an lvalue, which
might help.  You can also use the 'aligned' attribute to specify a
larger alignment for, e.g., arrays of char:

unsigned char foo[512] __attribute__ ((aligned(__alignof__(long))));

If you absolutely have to access unaligned data (for example, you are
dealing with a legacy file format or data structure that you can't
change), then you have a couple of options.

If you are reading binary data into memory you can sometimes "pad" a
block of memory to move the necessary fields into alignment.  This of
course requires that you know what the alignment of the block of
memory is :-)  For example:

        /* struct partition contains two 32-bit fields that are 2
           bytes out of alignment */
        unsigned char _sect[512 + 2];
        unsigned char *sect = _sect + 2;
        struct partition *part = sect + 0x1be;

But that only works if _sect is already aligned to at least
sizeof(int), which may or may not be the case.

Thus, the other (more portable) way is to write a
get_unaligned_8byte() or get_unaligned_4byte() macro or function to
read and write the value in question.  Unfortunately the only portable
way to do this in C (by reading a byte at a time and shifting/OR-ing
them together) is incredibly wasteful on Alpha, but then again,
unaligned traps are even more wasteful.

If you don't mind being GCC specific you can do something nasty like:

        union unaligned {
          void *p;
          unsigned b4 __attribute__ ((mode (SI)));
          unsigned b8 __attribute__ ((mode (DI)));
        } __attribute__ ((packed));

        static inline unsigned long
        read_8byte (void *p)
        { union unaligned *up = p; return up->b8; }

        static inline void
        write_8byte (void *p, unsigned long val)
        { union unaligned *up = p; up->b8 = val; }

        static inline unsigned int
        read_4byte (void *p)
        { union unaligned *up = p; return up->b4; }

        static inline void
        write_4byte (void *p, unsigned int val)
        { union unaligned *up = p; up->b4 = val; }

But I'm almost 100% certain that this will not work on, for example,
CCC.

[1] Actually for 16-bit integers on non-BWX targets this isn't true
    since they aren't supported in hardware, but you shouldn't rely on
    that :-)

[2] If foo were an auto variable you would not have this problem since
    the stack is always aligned to 8 bytes.

-- 
David Huggins-Daines, Senior GNU/Linux Consultant, Linuxcare, Inc.
613.562.1239 desk, 613.223.0225 mobile
dhd@linuxcare.com, http://www.linuxcare.com/
Linuxcare. Support for the revolution.


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

* Re: Fixing "unaligned trap" errors
  2000-05-19 17:51 ` David Huggins-Daines
@ 2000-05-20 17:41   ` Michal Jaegermann
  2000-05-21 19:11     ` David Huggins-Daines
  0 siblings, 1 reply; 5+ messages in thread
From: Michal Jaegermann @ 2000-05-20 17:41 UTC (permalink / raw)
  To: axp-list

David Huggins-Daines posted a very informative archive on the "unaligned
trap" problem which explains what it is in a much greater detail than
FAQ entry on www.alphalinux.org. :-)

I still have some commments.  First, it looks like that one again it is
a time for a "periodic posting" (it was posted on this list in the past
more than once) of a program which is of a great help if one is hunting
for troublesome spots.  An advantage of it is that you will get a bomb
instead of a trap and a message so you can catch its location in a
debugger like gdb.  Here it goes:

/****
 From: "Christian Groessler" <cpg@aladdin.de>
 To: axp-list@redhat.com
 Date: Thu, 19 Aug 1999 10:52:55 +0100
 Subject: Re: Unaligned traps et al


 The following program creates a subshell and all programs
 started from therein will SIGBUS on unaligned accesses.
***/
/* 28-May-98, chris */


#ifndef __linux__
#include <sys/sysinfo.h>
#else
#include <asm/sysinfo.h>
#include <asm/unistd.h>

static int setsysinfo(unsigned long op, void *buffer, unsigned long size,
                      int *start, void *arg, unsigned long flag)
{
  syscall(__NR_osf_setsysinfo, op, buffer, size, start, arg, flag);
}
#endif

void trap_unaligned(void)
{
  unsigned int buf[2];
  buf[0] = SSIN_UACPROC;
  buf[1] = UAC_SIGBUS | UAC_NOPRINT;
  setsysinfo(SSI_NVPAIRS, buf, 1, 0, 0, 0);
}

#include <stdio.h>

int main()
{
  trap_unaligned();
  printf("creating subshell with SIGBUS setting!\n");
  execl("/bin/bash","/bin/bash",(char *)0);
  return(0); /* not reached */
}

> 
> /* sizeof(struct foo) == 12 */
            ^^^^^^^^^^^^^^^
            Don't bet on it!!!
> struct foo {
>         int a, b, c;
> };


A C compiler, by its definition, is free to allocate a structure with
"holes" so sizeof(struct foo) __may__ turn out to be, say, 24 even if
sum of sizes of this structure elements are 12.  In this particular case
you can, probably, assume that a compiler is relatively "sane" and that
sizeof(struct foo) is indeed 12 even without checking but I have seen
problems caused by similar, if not so blatant, assumptions.  The problem
is compounded by the fact that C does not really provide a good portable
way out of this bind (save, possibly, of making this into an array of
bytes).  gcc has for such occasions , mentioned by David, '__attribute__
((packed))' construct but this is a compiler specific extension.

If you peek inside Linux kernel headers (NOT glibc headers) then
you will find multiple instances where '__attribute__ ((packed))'
was used as a layout of many structures has to be tightly controlled.

A code generated by gcc to access structure elements may not be the
same in an absence or presence of '__attribute__ ((packed))' - even
if a structure layout in memory happens to be identical.  In the later
case you will not see "unaligned access" messages - if your code
was otherwise correct. :-)  If a code is meant to be used with
some other compiler you may use a preprocessor to hide conditionally
such bits (define a macro which expands to __attribute__ ((packed))
or to nothing - depending on a compiler used) but what other compiler
will do with your structures and a code cannot be predicted in advance.

Last, but not least, I encountered cases when "unaligned accesses"
were side-effects of real bugs.  For example, trying to store
long in a space which was allocated for int, or an attempt to
access a wrong location on a stack, __may__ end up with an
unaligned trap.  OTOH it may not, so do not take that for a reliable
indicator, but problems of that sort are worth of an investigation
not only for a performance reasons.

  Michal


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

* Re: Fixing "unaligned trap" errors
  2000-05-20 17:41   ` Michal Jaegermann
@ 2000-05-21 19:11     ` David Huggins-Daines
  0 siblings, 0 replies; 5+ messages in thread
From: David Huggins-Daines @ 2000-05-21 19:11 UTC (permalink / raw)
  To: axp-list

Michal Jaegermann <michal@ellpspace.math.ualberta.ca> writes:

>  From: "Christian Groessler" <cpg@aladdin.de>
>  To: axp-list@redhat.com
>  Date: Thu, 19 Aug 1999 10:52:55 +0100
>  Subject: Re: Unaligned traps et al
> 
> 
>  The following program creates a subshell and all programs
>  started from therein will SIGBUS on unaligned accesses.

Wow nifty!  I have been debugging these problems the hard way
(i.e. with objdump and/or gdb).

> > /* sizeof(struct foo) == 12 */
>             ^^^^^^^^^^^^^^^
>             Don't bet on it!!!

Oops!  Yeah.  It just happens to be so on Linux/Alpha with gcc.

> Last, but not least, I encountered cases when "unaligned accesses"
> were side-effects of real bugs.  For example, trying to store
> long in a space which was allocated for int, or an attempt to

Yup, there has been just such a bug in Emacs on all GNU/Linux
platforms (see http://bugs.debian.org/63993) for a long time now
(since at least ncurses-4.2), but it only showed up on Alpha due to
the unaligned trap messages.

Cheers

-- 
David Huggins-Daines, Senior GNU/Linux Consultant, Linuxcare, Inc.
613.562.1239 desk, 613.223.0225 mobile
dhd@linuxcare.com, http://www.linuxcare.com/
Linuxcare. Support for the revolution.


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

end of thread, other threads:[~2000-05-21 19:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-05-16 12:13 Fixing "unaligned trap" errors David Rysdam
2000-05-19 17:51 ` David Huggins-Daines
2000-05-20 17:41   ` Michal Jaegermann
2000-05-21 19:11     ` David Huggins-Daines
  -- strict thread matches above, loose matches on Subject: below --
2000-05-19 15:43 William H. Magill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox