Getting rid of alignment faults in userspace

Andy Green andy.green at linaro.org
Sat Jun 18 09:55:42 UTC 2011


On 06/17/2011 11:53 PM, Somebody in the thread at some point said:

Hi -

>> int main(int argc, char * argv[])
>> {
>>
>>        char buf[8];
>>        void *v =&buf[1];
>>        unsigned int *p = (unsigned int *)v;
>
> This does not (reliably) do what you expect.  The compiler need not align buf.

What?  Somebody complaining my code does not blow enough faults and 
exceptions? ^^

If I retry the same test with this, which is definitely proof against 
such doubts -->


#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[])
{
      char buf[8];
      void *v = &buf[1];
      void *v1 = &buf[2];
      unsigned int *p = (unsigned int *)v;
      unsigned int *p1 = (unsigned int *)v1;

      strcpy(buf, "abcdefg");

      printf("0x%08x\n", *p);
      printf("0x%08x\n", *p1);

      return 0;
}

I get

root at linaro:~# echo 2 > /proc/cpu/alignment
root at linaro:~# ./a.out
0x65646362
0x66656463
root at linaro:~# echo 0 > /proc/cpu/alignment
root at linaro:~# ./a.out
0x65646362
0x66656463

ie, it is still always fixed up.

Let's not lose sight of the point of the thread, Dave Martin wants to 
root out remaining alignment faults in userland which is a great idea, I 
was warning him depending on what he tests on, eg, Panda, by default he 
won't be able to see any alignment faults in the first place in the soft 
fixup code that allows him to get a signal and find the bad code in gdb. 
  And this code does prove that to be the case.

-Andy



More information about the ubuntu-devel mailing list