x86_64 peculiarities
Integer overflows
When multiplying integers, be careful:
// Allocate 8GiB
void *buf = malloc(8 * 1024 * 1024 * 1024);
// Access the memory
char *buf_bptr = (char *)buf;
buf_bptr[1 * 1024 * 1024 * 1024] = 0;
// *surprise*, segfault
Do this instead:
// Allocate 8GiB
void *buf = malloc(8UL * 1024 * 1024 * 1024);
// Access the memory
char *buf_bptr = (char *)buf;
buf_bptr[1 * 1024 * 1024 * 1024] = 0;
// *surprise*, no segfault
Vector stores are not aligned
Vector stores that are not aligned to the boundary of their word size result in a segmentation fault.
For example, the movnt
instruction takes a destination address and a source register (ymm
):
_mm256_stream_si256(__m256i *dest, __m256i *ymm0);
In case dest
is not aligned at the instruction’s word boundary the processor
would generate a general protection fault and kernel will kill your application
with a segfault.
Compiler/Build System
Your-fancy-build-system is broken
The build system didn’t recompile binaries that depend on object files/shared files that were recompiled. e.g., struct layout is updated in a header file for a shared object, but the binary was never recompiled.