summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Moyer <jmoyer@redhat.com>2021-09-29 15:28:45 -0400
committerJeff Moyer <jmoyer@redhat.com>2021-09-29 15:28:45 -0400
commit4d0c04ad730dc51ff68f2665de0687bf3404d115 (patch)
tree6f245531a9a40c2f12e1f9f298719b7b63923df4
parent4fd9dcf7508947f9ee1d187a5edc758eef8f9524 (diff)
downloadlibaio-4d0c04ad730dc51ff68f2665de0687bf3404d115.tar.gz
Verify structure padding is correct at build time
Padding for the various structures in the iocb.u union should be the same. This patch verifies they are at build time. Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
-rw-r--r--src/Makefile1
-rw-r--r--src/struct_offsets.c23
2 files changed, 24 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile
index 37ae219..b6d0499 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -55,6 +55,7 @@ libaio.a: $(libaio_objs)
$(RANLIB) libaio.a
$(libname): $(libaio_sobjs) libaio.map
+ $(CC) $(CFLAGS) -c struct_offsets.c
$(CC) $(SO_CFLAGS) -Wl,--version-script=libaio.map -Wl,-soname=$(soname) -o $@ $(libaio_sobjs) $(LINK_FLAGS)
install: $(all_targets)
diff --git a/src/struct_offsets.c b/src/struct_offsets.c
new file mode 100644
index 0000000..4dc6fcc
--- /dev/null
+++ b/src/struct_offsets.c
@@ -0,0 +1,23 @@
+/*
+ * Ensure that data structure offsets in the iocb.u union match.
+ * Note that this code does not end up in the compiled object files.
+ * Its sole purpose is to abort the build if the structure padding
+ * is incorrect.
+ */
+#include <stddef.h>
+#include <assert.h>
+#include <libaio.h>
+
+void
+offset_check(void)
+{
+ static_assert(offsetof(struct iocb, u.v.nr) ==
+ offsetof(struct iocb, u.c.nbytes),
+ "Error: iocb.u.v.nr does not match the offset of iocb.u.c.nbytes.");
+ static_assert(offsetof(struct iocb, u.v.offset) ==
+ offsetof(struct iocb, u.c.offset),
+ "Error: iocb.u.v.offset does not match the offset of iocb.u.c.offset");
+ static_assert(offsetof(struct iocb, u.saddr.len) ==
+ offsetof(struct iocb, u.c.nbytes),
+ "Error: iocb.u.saddr.len does not match the offset of iocb.u.c.nbytes");
+}