Lasse Collin wrote: > About sanitizers: They can be annoying with SIMD code. If a function is > passed an unaligned buffer, it would be fine to round the address down > to an aligned value, do an aligned read, and ignore the out-of-bounds > bytes. One can do it in assembly because sanitizers don't see it. In > contrast to sanitizers, Valgrind is happy if the extra bytes are thrown > away.
Valgrind was a tool without replacement, for many years, when sanitizers did not exist. Nowadays, however, I generally prefer testing with sanitizers than with valgrind because there are some bugs that ASAN finds and valgrind doesn't [1]. For example, when you have a struct with two adjacent arrays, ASAN can find buffer overruns of the first array, while valgrind can't. Just this week, sanitizers have found a real bug in coreutils [2]. Regarding your trick to do an aligned read on (addr & -alignment) instead of an unaligned read on (addr): I find it good that ASAN catches this, because this trick amounts to exploiting a coincidental property of current hardware. Similarly to accessing (addr + (1 << n)) for 48 < n < 64: some hardware allows this, but it's an ISO C violation nevertheless. Bruno [1] https://lists.gnu.org/archive/html/bug-gnulib/2023-11/msg00112.html [2] https://lists.gnu.org/archive/html/bug-coreutils/2025-01/msg00022.html