build: disable unaligned access under ubsan
What changed, and why it matters
This commit changes the build configuration so that when the Undefined Behavior Sanitizer (UBSan) is enabled, the project pretends the CPU cannot do unaligned memory reads/writes. This is done to silence UBSan warnings that are technically false alarms on x86 CPUs. It is a build/test-hygiene change, not a fix for an exploitable bug.
No security action required. Treat as ordinary build-system maintenance. If auditing, confirm that AVOID_UNALIGNED_ACCESS only affects sanitizer builds and does not introduce performance regressions in benchmarked paths.
Security signals we found
Build-only change gated by ubsan flag
No functional code change in non-ubsan builds
Commit message explicitly describes false-positive avoidance
Evidence from the diff
The patch adds -DAVOID_UNALIGNED_ACCESS=1 to CFLAGS when –enable-ubsan is used, and updates src/ccan_config.h so that AVOID_UNALIGNED_ACCESS forces HAVE_UNALIGNED_ACCESS to 0. The existing ARM-specific unaligned-access avoidance is preserved. The stated goal is to avoid UBSan ‘unaligned reads/writes’ reports that are legal on x86. No runtime code path is changed for normal builds.
Changed components
configure.acsrc/ccan_config.hInspect captured patch +11 / −5
diff --git a/configure.ac b/configure.ac
index d74da95..57caee5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -178,6 +178,9 @@ fi
if test "x$ub_sanitizer" = "xyes"; then
AX_CHECK_COMPILE_FLAG([-fsanitize=undefined], [AM_CFLAGS="$AM_CFLAGS -fsanitize=undefined"])
AX_CHECK_LINK_FLAG([-fsanitize=undefined], [LDFLAGS="$LDFLAGS -fsanitize=undefined"])
+ # ubsan complains about unaligned reads/writes even though they are legal
+ # on x86 archs. Force alignment adjustment to avoid false positives.
+ AX_CHECK_COMPILE_FLAG([-DAVOID_UNALIGNED_ACCESS=1], [AM_CFLAGS="$AM_CFLAGS -DAVOID_UNALIGNED_ACCESS=1"])
fi
# -flax-vector-conversions is needed for our arm assembly
diff --git a/src/ccan_config.h b/src/ccan_config.h
index 87ea136..c660937 100644
--- a/src/ccan_config.h
+++ b/src/ccan_config.h
@@ -32,11 +32,14 @@
#define HAVE_BSWAP_64 0
#endif
-#if defined(HAVE_UNALIGNED_ACCESS) && defined(__arm__)
-/* arm unaligned access is incomplete, in that e.g. byte swap instructions
- * can fault on unaligned addresses where a normal load/store would be fine.
- * Since the compiler can optimise some of our accesses into operations like
- * byte swaps, treat this platform as though it doesn't have unaligned access.
+#if defined(AVOID_UNALIGNED_ACCESS) || (defined(HAVE_UNALIGNED_ACCESS) && defined(__arm__))
+/* Disable unaligned access when:
+ * 1) Explicitly asked to via AVOID_UNALIGNED_ACCESS (e.g. for ubsan builds)
+ * 2) For arm builds where unaligned access is incomplete, in that e.g. byte
+ * swap instructions can fault on unaligned addresses where a normal load/store
+ * would be fine. Since the compiler can optimise some of our accesses into
+ * operations like byte swaps, treat this platform as though it doesn't have
+ * unaligned access.
*/
#undef HAVE_UNALIGNED_ACCESS
#define HAVE_UNALIGNED_ACCESS 0
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.