From: Michal Jaegermann <michal@ellpspace.math.ualberta.ca>
To: axp-list@redhat.com
Subject: Re: Fixing "unaligned trap" errors
Date: Sat, 20 May 2000 11:41:42 -0600 (MDT) [thread overview]
Message-ID: <200005201741.LAA13710@ellpspace.math.ualberta.ca> (raw)
In-Reply-To: <87g0re2zui.fsf@linuxcare.com> from "David Huggins-Daines" at May 19, 2000 01:51:01 PM
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
next prev parent reply other threads:[~2000-05-20 17:41 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox