From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 8456 invoked from network); 20 May 2000 17:41:45 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 20 May 2000 17:41:45 -0000 Received: from ellpspace.math.ualberta.ca (michal@ellpspace.math.ualberta.ca [129.128.88.19]) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id NAA11987 for ; Sat, 20 May 2000 13:41:45 -0400 Received: (from michal@localhost) by ellpspace.math.ualberta.ca (8.9.3/8.9.3) id LAA13710 for axp-list@redhat.com; Sat, 20 May 2000 11:41:42 -0600 From: Michal Jaegermann Message-Id: <200005201741.LAA13710@ellpspace.math.ualberta.ca> Subject: Re: Fixing "unaligned trap" errors To: axp-list@redhat.com Date: Sat, 20 May 2000 11:41:42 -0600 (MDT) In-Reply-To: <87g0re2zui.fsf@linuxcare.com> from "David Huggins-Daines" at May 19, 2000 01:51:01 PM X-Mailer: ELM [version 2.5 PL2] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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" 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 #else #include #include 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 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