[postgis-devel] Validity flag

Even Rouault even.rouault at spatialys.com
Fri Nov 16 01:00:32 PST 2018


> Just for my own knowledge, I knew the possible performance penalty, but
> what does it mean that RISC does not allow unaligned reads ?
> Say:
> 
> double d = *((double*)0x012345677) /* obviously non-aligned on 8 bytes
> address */
> 
> What will it do ? Raise a processor fault ? return garbage ?

Depends on the CPU and operating system. I know that Sparc Solaris used to SIGBUS on this. 

Interesting post on ARM behaviourS (emphasis on the plural):
https://medium.com/@iLevex/the-curious-case-of-unaligned-access-on-arm-5dd0ebe24965

Even on x86, if you build your code with -fsanitize=undefined, you'll get errors:

$ cat test.c

int main()
{
    double two_doubles[2] = {1, 2};
    char* p = (char*)two_doubles;
    return *(double*)(p + 1) == 0;
}

$ clang -fsanitize=undefined test.c -o test &&  ./test
test.c:5:12: runtime error: load of misaligned address 0x7fff5eaa8481 for type 'double', which requires 8 byte alignment
0x7fff5eaa8481: note: pointer points here
 7f 00 00  00 00 00 00 00 00 f0 3f  00 00 00 00 00 00 00 40  80 85 aa 5e ff 7f 00 00  00 00 00 00 00


Unaligned access is undefined behaviour in C, and the source of real issues due
to compilers being too smart sometimes, assuming your code won't do
unaligned access, and thus using CPU instructions that really assume it.

See http://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-x86.html which is
really enlighting of this.

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the postgis-devel mailing list