Port-amd64 archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

unaligned memory access & SIGBUS


While trying to chase some unaligned memory access originally noticed
on alpha, i wanted to have a way to detect this on amd64 ...

Unfortunately, the attached sample code does not fail with SIGBUS on
NetBSD/amd64 unlike other OSes i checked (Linux and FreeBSD). It
simply tries to set the Alignment Check bit from RFLAGS register and
then trigger an unaligned memory access.

Any specific reason for such a behaviour ?


njoly@lanfeust [tmp/malign]> uname -a
NetBSD lanfeust.sis.pasteur.fr 6.99.4 NetBSD 6.99.4 (LANFEUST) #2: Mon Apr  9 
23:30:03 CEST 2012  
njoly@lanfeust [tmp/malign]> cc -o malign malign.c 
njoly@lanfeust [tmp/malign]> ./malign 

njoly@kiri-adm1 [~]> uname -a
Linux kiri-adm1.cluster.pasteur.fr 2.6.18-274.12.1.el5 #1 SMP Tue Nov 29 
13:37:46 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
njoly@kiri-adm1 [~]> cc -o malign malign.c 
njoly@kiri-adm1 [~]> ./malign 
zsh: bus error (core dumped)  ./malign

njoly@livarot [~]> uname -a
FreeBSD livarot.sis.pasteur.fr 8.1-RELEASE-p5 FreeBSD 8.1-RELEASE-p5 #0: Tue 
Sep 27 16:49:00 UTC 2011     
root%amd64-builder.daemonology.net@localhost:/usr/obj/usr/src/sys/GENERIC  amd64
njoly@livarot [~]> cc -o malign malign.c 
njoly@livarot [~]> ./malign 
zsh: bus error (core dumped)  ./malign

Nicolas Joly

Projects and Developments in Bioinformatics
Institut Pasteur, Paris.
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
int main(void)  
    char *p;  
    __asm__("pushf; orl $0x40000, (%rsp); popf");  
    p = malloc(sizeof(int) + 1);  
    memset(p, 0, sizeof(int) + 1);  
    printf("%d\n", *(int *)p);  
    return 0;  

Home | Main Index | Thread Index | Old Index