Skip to content

Instantly share code, notes, and snippets.

@padenot
Last active April 8, 2016 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save padenot/8536411 to your computer and use it in GitHub Desktop.
Save padenot/8536411 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef __SANITIZE_ADDRESS__
#define ALLOC_STACK(ptr) __asan_describe_address(ptr)
#else
#define ALLOC_STACK(ptr) fprintf(stderr, \
"Do an ASAN build to get allocation stacks.\n");
#endif
#define DUMP_IF_NOT_ALIGNED(ptr, align) \
if ((((uintptr_t)ptr + align - 1) & ~(align - 1)) != (uintptr_t)ptr) { \
fprintf(stderr, "%s does not satisfy alignment request of %d.\n", \
#ptr, align); \
ALLOC_STACK(ptr); \
}
int main() {
char * meh = malloc(128);
DUMP_IF_NOT_ALIGNED(meh, 16);
DUMP_IF_NOT_ALIGNED(&(meh[7]), 16);
DUMP_IF_NOT_ALIGNED(&(meh[16]), 16);
free(meh);
return 0;
}
repositories/test-valgrind » gcc -fsanitize=address -fno-omit-frame-pointer -g align-check.c
repositories/test-valgrind » ./a.out
&(meh[7]) does not satisfy alignment request of 16.
0x60180000bf87 is located 7 bytes inside of 128-byte region [0x60180000bf80,0x60180000c000)
allocated by thread T0 here:
#0 0x7fb030bbd45a (/usr/lib/x86_64-linux-gnu/libasan.so.0.0.0+0x1545a)
#1 0x4008de (/home/padenot/src/repositories/test-valgrind/a.out+0x4008de)
#2 0x7fb030801de4 (/lib/x86_64-linux-gnu/libc-2.17.so+0x21de4)
repositories/test-valgrind » addr2line ./a.out 0x4008de
??:0
/home/padenot/src/repositories/test-valgrind/align-check.c:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment