Nicolas Joly | 10 Apr 2012 11:21
Picon
Picon
Favicon

unaligned memory access & SIGBUS


Hi,

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 ?

Thanks.

njoly <at> 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 <at> lanfeust.sis.pasteur.fr:/local/src/NetBSD/obj.amd64/sys/arch/amd64/compile/LANFEUST amd64
njoly <at> lanfeust [tmp/malign]> cc -o malign malign.c 
njoly <at> lanfeust [tmp/malign]> ./malign 
0

njoly <at> 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 <at> kiri-adm1 [~]> cc -o malign malign.c 
njoly <at> kiri-adm1 [~]> ./malign 
zsh: bus error (core dumped)  ./malign

njoly <at> livarot [~]> uname -a
(Continue reading)

Christos Zoulas | 11 Apr 2012 04:25

Re: unaligned memory access & SIGBUS

In article <20120410092122.GA26870 <at> lynche.sis.pasteur.fr>,
Nicolas Joly  <njoly <at> pasteur.fr> wrote:
>-=-=-=-=-=-
>
>
>Hi,
>
>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 ?
>
>Thanks.
>
>njoly <at> 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 <at> lanfeust.sis.pasteur.fr:/local/src/NetBSD/obj.amd64/sys/arch/amd64/compile/LANFEUST amd64
>njoly <at> lanfeust [tmp/malign]> cc -o malign malign.c 
>njoly <at> lanfeust [tmp/malign]> ./malign 
>0
>
>njoly <at> 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
(Continue reading)

Nicolas Joly | 11 Apr 2012 17:42
Picon
Picon
Favicon

Re: unaligned memory access & SIGBUS

On Wed, Apr 11, 2012 at 02:25:51AM +0000, Christos Zoulas wrote:
> In article <20120410092122.GA26870 <at> lynche.sis.pasteur.fr>,
> Nicolas Joly  <njoly <at> pasteur.fr> wrote:
> >-=-=-=-=-=-
> >
> >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 ?
> >
> >Thanks.
> >
> >njoly <at> 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 <at> lanfeust.sis.pasteur.fr:/local/src/NetBSD/obj.amd64/sys/arch/amd64/compile/LANFEUST amd64
> >njoly <at> lanfeust [tmp/malign]> cc -o malign malign.c 
> >njoly <at> lanfeust [tmp/malign]> ./malign 
> >0
[...]
> PSL_AC needs to be part of PSL_USER?

Thanks for the tip, but simply adding it to PSL_USER do not change
anything; still no SIGBUS signal.
(Continue reading)

Jean-Yves Migeon | 18 Apr 2012 20:37
Picon
Favicon

Re: unaligned memory access & SIGBUS

Le 11/04/12 17:42, Nicolas Joly a écrit :
>>> njoly <at> 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 <at> lanfeust.sis.pasteur.fr:/local/src/NetBSD/obj.amd64/sys/arch/amd64/compile/LANFEUST amd64
>>> njoly <at> lanfeust [tmp/malign]>  cc -o malign malign.c
>>> njoly <at> lanfeust [tmp/malign]>  ./malign
>>> 0
> [...]
>> PSL_AC needs to be part of PSL_USER?
>
> Thanks for the tip, but simply adding it to PSL_USER do not change
> anything; still no SIGBUS signal.
>
> Unless someone beats me ;) i'll need to find some spare to understand
> that part of code hoping to see what's going on.

FWIW I talked with Nicolas about this privately, and the fix is fairly 
straightforward once you know it: alignment checks only trap when the 
CR0_AM bit is set in %cr0. Our code does not set it, so the CPU does not 
trap AE.

I have a patch ready for it, will commit it after I convert its malign.c 
test to an ATF one.

I will ask for a pull-up, the patch is rather harmless.

--

-- 
jym <at> 
(Continue reading)


Gmane