How can I debug memory alignment problems (AVX) when they don't reproduce in a minimal context?

Viewed 358

I run into the following problem all the time recently:

  1. I have a large-ish codebase which uses Eigen and PCL (which also uses Eigen)
  2. I need to use the almost current Eigen master branch and can't use a stable release due to a dependency requiring it.
  3. Both PCL and other dependencies in my project make use of vector instructions, so I compile with -march=native.
  4. When running some things that involve Eigen types (Affine transforms, Point cloud viewing), I sometimes get segmentation faults or other crashes in seemingly nonsensical locations, such as in the Eigen::Quaternionf constructor. In other case, the EIGEN_UNALIGNED_ASSERT is triggered, which leads one to this page, whose contents I observe everywhere in my code (so far as I know)

I want to be a good citizen so I try to reproduce the problem on a minimal example to post on stackoverflow or in the related issue trackers. However I invariably fail at reproducing the crash when extracting just the bit triggering the problem. My guess is that whether or not memory is aligned (and no special care is taken) is also dependent on what was allocated before the problem location, so the surrounding code is instrumental in triggering the issue.

Question: What techniques and tools can I use to effectively determine the source of memory-alignment-related crashes in C+14 on Linux and/or macOS?

1 Answers

I would highly recommend AddressSanitizer. On an embedded application I once had a small stack overflow on a buffer which had side effects only when the stack was nearly full .I finaly found the stack overflow with including this command options on my cmake (works with gcc).Note the fsanitize=alignment finds runtime bugs related with alignment ,gcc documentation about ASan says:

This option enables checking of alignment of pointers when they are dereferenced, or when a reference is bound to insufficiently aligned target, or when a method or constructor is invoked on insufficiently aligned object.

And those are the most probable bugs when you are dealing with custom alignment

add_compile_options(
    $<$<CONFIG:Debug>:-Og>
    -fdiagnostics-color=always
    -Wuninitialized
    -fno-omit-frame-pointer
    $<$<CONFIG:Debug>:-fsanitize=undefined>
    $<$<CONFIG:Debug>:-fsanitize=address>
    $<$<CONFIG:Debug>:-fsanitize=alignment>
    $<$<CONFIG:Debug>:-fsanitize=bounds>
    -fno-sanitize-recover
)
add_link_options(
    $<$<CONFIG:Debug>:-fsanitize=undefined>
    $<$<CONFIG:Debug>:-fsanitize=address>
    $<$<CONFIG:Debug>:-fsanitize=alignment>
    $<$<CONFIG:Debug>:-fsanitize=bounds>
)


Related