https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65149
Bug ID: 65149 Summary: unaligned atomic object causes Bus Error on SPARC Product: gcc Version: 4.9.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: alexey.lapshin at oracle dot com Following test case caused Bus Error on SPARC : $ cat bus_error.cpp #include <atomic> #include <stdio.h> typedef struct { char c[8]; } struct8; typedef struct { char c[1]; std::atomic< struct8 > as; } obj_t; obj_t obj; int main( void ) { printf("\n sizeof(obj.as) %d alignof(obj.as) %d", sizeof(obj.as), alignof(obj.as) ); printf("\n\n atomic_is_lock_free(&obj.as) %d __atomic_always_lock_free(sizeof(obj.as), &(obj.as)) %d", atomic_is_lock_free(&obj.as), __atomic_always_lock_free(sizeof(obj.as), &(obj.as)), atomic_is_lock_free(&obj.as) ); printf("\n\n"); return obj.as.load().c[0]; } $ g++ version is - 4.9.2 (GCC) $ g++ bus_error.cpp -latomic -m32 -std=c++11 $ ./a.out sizeof(obj.as) 8 alignof(obj.as) 1 atomic_is_lock_free(&obj.as) 1 __atomic_always_lock_free(sizeof(obj.as), &(obj.as)) 0 Bus Error (core dumped) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< The reason for the failure is that __atomic_always_lock_free() reported 0, but compiler inserts lock-free implementation : _ZNKSt6atomicI7struct8E4loadESt12memory_order: .LLFB359: save %sp, -104, %sp .LLCFI4: ld [%fp+64], %i5 st %i0, [%fp+68] st %i1, [%fp+72] ld [%fp+68], %g1 membar 2 ldd [%g1], %g2 std %g2, [%fp-8] mov %i5, %g3 add %fp, -8, %g2 mov 8, %g1 mov %g3, %o0 mov %g2, %o1 mov %g1, %o2 call memcpy, 0 nop nop mov %i5, %i0 return %i7+12 nop .LLFE359: This lock-free implementation could not work correctly with unaligned data. The bug was found on Solaris SPARC, but it could exist on other platforms.