Unaligned access across babl source provokes crashes on sparc*
Submitted by John Paul Adrian Glaubitz
We're currently shaping up Debian's sparc64 port to make it fit for release after Linux sparc64 development has been picked up by Oracle as upstream.
While working through packages which are failing to build on sparc64, I stumbled over gegl which fails to build from source with a bus error . This is an indicator for unaligned access which is not allowed one some architectures like sparc64.
After some quick debugging with gdb, I was able to localize the problem in babl :
Program received signal SIGBUS, Bus error.
0xfff80001009d5338 in conv_rgb8_rgba8 (
<incomplete sequence \363>...,
dst=0x22ec14 "", samples=128) at gggl.c:758
758 *(unsigned int ) dst = ((unsigned int *) src) | (255 << 24);
As you can see, this particular kind of pointer arithmetics provokes unaligned access. This is because both src and dst are actually declared as a pointer of char, then later cast into pointer of unsigned int.
According to the C99 specification, declarations which refer to the same object or function must have compatible types, otherwise the behavior is undefined  (chapter 6.2.7, paragraph 2).
So, either the declarations have to be fixed or the direct assignments have to be replaced with memcpy which guarantees aligned access.
With that in mind, the above example:
*(unsigned int *) dst = (*(unsigned int *) src) | (255 << 24);
unsigned int tmp; memcpy(&tmp, src, sizeof(unsigned int)); tmp = tmp | (255 << 24); memcpy(dst, &tmp, sizeof(unsigned int));
For some reason, lt-babl_fish_path_dhtml will then hang after making this change, so I'm not a 100% sure whether this particular change has other ramifications.
Either way, the pointer arithmetics should be replaced with memcpy to guarantee aligned access. On x86, memcpy will not produce any additional overhead while, on sparc*, it will actually fix the unaligned access.
 https://buildd.debian.org/status/fetch.php?pkg=gegl&arch=sparc64&ver=0.3.6-4&stamp=1464963616  http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=806208  http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf