byteorder.h and strict aliasing

Andreas Schneider asn at samba.org
Thu Nov 14 10:42:33 MST 2013


Hello,

if you compile Samba with optimization on i686 you get 342 warnings like this:

lib/util/genrand.c:294:2: warning: dereferencing type-punned pointer will 
break strict-aliasing rules [-Wstrict-aliasing]

These warnings normally mean that the compiler can't optimize the code. For 
the long answer read [1].

The reason is that we have a define CAREFUL_ALIGNMENT in byteorder.h. There is 
a comment which states:

--------------
The distinction between 386 and other architectures is only there as
an optimisation. You can take it out completely and it will make no
difference. The routines (macros) in byteorder.h are totally byteorder
independent. The 386 optimsation just takes advantage of the fact that
the x86 processors don't care about alignment, so we don't have to
align ints on int boundaries etc. If there are other processors out
there that aren't alignment sensitive then you could also define
CAREFUL_ALIGNMENT=0 on those processors as well.
--------------

Well, to show that this is not an optimization but breaks the compiler 
optimization I wrote a test with it:

----- schnipp -----
asn at samba32:~/workspace/projects/test> cat g.c

#include <stdint.h>
#include <stdio.h>

#include "byteorder.h"

uint32_t generate_random(void)
{
    uint8_t v[4];

    v[0] = 1;
    v[1] = 2;
    v[2] = 3;
    v[3] = 4;

    return IVAL(v, 0);
}

int main(void)
{
    uint32_t i;

    i = generate_random();

    printf("i=%u", i);

    return 0;
}
----- schnapp -----

You need lib/util/byteorder.h from Samba and then compile it:

----- schnipp -----
asn at samba32:~/workspace/projects/test> gcc -O3 -Wall -S g.c -o g
g.c: In function ‘generate_random’:
g.c:15:5: warning: dereferencing type-punned pointer will break strict-
aliasing rules [-Wstrict-aliasing]
     return IVAL(v, 0);
     ^
----- schnapp -----

So the next step was to compile an assembler file

----- schnipp -----
asn at samba32:~/workspace/projects/test> gcc -O3 -Wall -S g.c -o 
g_without_careful_alignment.s 
g.c: In function ‘generate_random’:
g.c:15:5: warning: dereferencing type-punned pointer will break strict-
aliasing rules [-Wstrict-aliasing]
     return IVAL(v, 0);
     ^
asn at samba32:~/workspace/projects/test> vi byteorder.h
asn at samba32:~/workspace/projects/test> gcc -O3 -Wall -S g.c -o 
g_with_careful_alignment.s
----- schnapp -----

If you create a diff, you can see that if we use the careful alignment 
functions, we don't have any strict aliasing problems and the compiler can 
optimize the source code. generate_random() is reduced by 4 instructions.

----- schnipp -----
asn at samba32:~/workspace/projects/test> diff -u g_without_careful_alignment.s 
g_with_careful_alignment.s
--- g_without_careful_alignment.s       2013-11-14 18:31:40.609949754 +0100
+++ g_with_careful_alignment.s  2013-11-14 18:32:16.371947782 +0100
@@ -6,11 +6,7 @@
 generate_random:
 .LFB11:
        .cfi_startproc
-       subl    $16, %esp
-       .cfi_def_cfa_offset 20
        movl    $67305985, %eax
-       addl    $16, %esp
-       .cfi_def_cfa_offset 4
        ret
        .cfi_endproc
 .LFE11:
@@ -31,10 +27,9 @@
        movl    %esp, %ebp
        .cfi_def_cfa_register 5
        andl    $-16, %esp
-       subl    $32, %esp
+       subl    $16, %esp
        movl    $67305985, 4(%esp)
        movl    $.LC0, (%esp)
-       movl    $67305985, 28(%esp)
        call    printf
        xorl    %eax, %eax
        leave
----- schnapp -----



[1] http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html


-- 
Andreas Schneider                   GPG-ID: CC014E3D
Samba Team                             asn at samba.org
www.samba.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-util-Remove-32bit-macros-breaking-strict-aliasing.patch
Type: text/x-patch
Size: 4284 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20131114/dc6bff5b/attachment.bin>


More information about the samba-technical mailing list