Skip to content

Commit b56a0b6

Browse files
committed
build: disable unaligned access under ubsan
Avoids false positives on e.g. x86 platforms.
1 parent c824172 commit b56a0b6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ fi
178178
if test "x$ub_sanitizer" = "xyes"; then
179179
AX_CHECK_COMPILE_FLAG([-fsanitize=undefined], [AM_CFLAGS="$AM_CFLAGS -fsanitize=undefined"])
180180
AX_CHECK_LINK_FLAG([-fsanitize=undefined], [LDFLAGS="$LDFLAGS -fsanitize=undefined"])
181+
# ubsan complains about unaligned reads/writes even though they are legal
182+
# on x86 archs. Force alignment adjustment to avoid false positives.
183+
AX_CHECK_COMPILE_FLAG([-DAVOID_UNALIGNED_ACCESS=1], [AM_CFLAGS="$AM_CFLAGS -DAVOID_UNALIGNED_ACCESS=1"])
181184
fi
182185

183186
# -flax-vector-conversions is needed for our arm assembly

src/ccan_config.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
#define HAVE_BSWAP_64 0
3333
#endif
3434

35-
#if defined(HAVE_UNALIGNED_ACCESS) && defined(__arm__)
36-
/* arm unaligned access is incomplete, in that e.g. byte swap instructions
37-
* can fault on unaligned addresses where a normal load/store would be fine.
38-
* Since the compiler can optimise some of our accesses into operations like
39-
* byte swaps, treat this platform as though it doesn't have unaligned access.
35+
#if defined(AVOID_UNALIGNED_ACCESS) || (defined(HAVE_UNALIGNED_ACCESS) && defined(__arm__))
36+
/* Disable unaligned access when:
37+
* 1) Explicitly asked to via AVOID_UNALIGNED_ACCESS (e.g. for ubsan builds)
38+
* 2) For arm builds where unaligned access is incomplete, in that e.g. byte
39+
* swap instructions can fault on unaligned addresses where a normal load/store
40+
* would be fine. Since the compiler can optimise some of our accesses into
41+
* operations like byte swaps, treat this platform as though it doesn't have
42+
* unaligned access.
4043
*/
4144
#undef HAVE_UNALIGNED_ACCESS
4245
#define HAVE_UNALIGNED_ACCESS 0

0 commit comments

Comments
 (0)