From e5540b5a3a545ed4e3f3df97be131ec81a155032 Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Sun, 12 Sep 2021 23:52:40 +0200
Subject: [PATCH] Fix struct offset testing in ABI test case

The intention of the (severely incomplete) ABI test case is to detect
changes to the ABI of libsquashfs. Currently it tries to blurt out if
the layout of some structure is changed unintentionally.

Unfortunately, the test uses some unportable assumptions. Among other
things, it was assumed that a 64 bit field will always require 64 bit
alignment. This is apparently no the case on 32 bit x86.

This patch makes the check work on 32 bit and 64 bit x86, by adding
an additional runtime check that relies on the __alignof__ extension
offered by gcc and clang (the only 2 compilers that are really
supported at the moment).

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 tests/libsqfs/abi.c | 71 +++++++++++++++++++++++++++++++++------------
 1 file changed, 52 insertions(+), 19 deletions(-)

diff --git a/tests/libsqfs/abi.c b/tests/libsqfs/abi.c
index 04bbc30..d409f67 100644
--- a/tests/libsqfs/abi.c
+++ b/tests/libsqfs/abi.c
@@ -35,8 +35,14 @@ static void test_compressor_opt_struct(void)
 		      sizeof(sqfs_u32));
 	TEST_EQUAL_UI(offsetof(sqfs_compressor_config_t, level),
 		      (2 * sizeof(sqfs_u32)));
-	TEST_EQUAL_UI(offsetof(sqfs_compressor_config_t, opt),
-		      (4 * sizeof(sqfs_u32)));
+
+	if (__alignof__(sqfs_compressor_config_t) == __alignof__(sqfs_u32)) {
+		TEST_EQUAL_UI(offsetof(sqfs_compressor_config_t, opt),
+			      (3 * sizeof(sqfs_u32)));
+	} else if (__alignof__(sqfs_compressor_config_t) == __alignof__(sqfs_u64)) {
+		TEST_EQUAL_UI(offsetof(sqfs_compressor_config_t, opt),
+			      (4 * sizeof(sqfs_u32)));
+	}
 }
 
 static const char *names[] = {
@@ -65,33 +71,60 @@ static void test_compressor_names(void)
 static void test_blockproc_stats(void)
 {
 	sqfs_block_processor_stats_t stats;
+	size_t off;
+
+	TEST_EQUAL_UI(sizeof(stats.size), sizeof(size_t));
+	TEST_EQUAL_UI(sizeof(stats.input_bytes_read), sizeof(sqfs_u64));
+	TEST_EQUAL_UI(sizeof(stats.output_bytes_generated), sizeof(sqfs_u64));
+	TEST_EQUAL_UI(sizeof(stats.data_block_count), sizeof(sqfs_u64));
+	TEST_EQUAL_UI(sizeof(stats.frag_block_count), sizeof(sqfs_u64));
+	TEST_EQUAL_UI(sizeof(stats.sparse_block_count), sizeof(sqfs_u64));
+	TEST_EQUAL_UI(sizeof(stats.total_frag_count), sizeof(sqfs_u64));
+	TEST_EQUAL_UI(sizeof(stats.actual_frag_count), sizeof(sqfs_u64));
 
-	TEST_ASSERT(sizeof(stats) >= (8 * sizeof(sqfs_u64)));
+	if (__alignof__(stats) == __alignof__(sqfs_u32)) {
+		TEST_ASSERT(sizeof(stats) >=
+			    (sizeof(sqfs_u32) + 7 * sizeof(sqfs_u64)));
+	} else if (__alignof__(stats) == __alignof__(sqfs_u64)) {
+		TEST_ASSERT(sizeof(stats) >= (8 * sizeof(sqfs_u64)));
+	}
 
 	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t, size), 0);
+
+	if (sizeof(size_t) < sizeof(sqfs_u64) &&
+	    (__alignof__(sqfs_block_processor_stats_t) ==
+	     __alignof__(sqfs_u64))) {
+		off = sizeof(sqfs_u64);
+	} else {
+		off = sizeof(stats.size);
+	}
+
 	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t,
-			       input_bytes_read), sizeof(sqfs_u64));
-	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t,
-			       output_bytes_generated), 2 * sizeof(sqfs_u64));
+			       input_bytes_read), off);
+	off += sizeof(sqfs_u64);
+
 	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t,
-			       data_block_count), 3 * sizeof(sqfs_u64));
+			       output_bytes_generated), off);
+	off += sizeof(sqfs_u64);
+
 	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t,
-			       frag_block_count), 4 * sizeof(sqfs_u64));
+			       data_block_count), off);
+	off += sizeof(sqfs_u64);
+
 	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t,
-			       sparse_block_count), 5 * sizeof(sqfs_u64));
+			       frag_block_count), off);
+	off += sizeof(sqfs_u64);
+
 	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t,
-			       total_frag_count), 6 * sizeof(sqfs_u64));
+			       sparse_block_count), off);
+	off += sizeof(sqfs_u64);
+
 	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t,
-			       actual_frag_count), 7 * sizeof(sqfs_u64));
+			       total_frag_count), off);
+	off += sizeof(sqfs_u64);
 
-	TEST_EQUAL_UI(sizeof(stats.size), sizeof(size_t));
-	TEST_EQUAL_UI(sizeof(stats.input_bytes_read), sizeof(sqfs_u64));
-	TEST_EQUAL_UI(sizeof(stats.output_bytes_generated), sizeof(sqfs_u64));
-	TEST_EQUAL_UI(sizeof(stats.data_block_count), sizeof(sqfs_u64));
-	TEST_EQUAL_UI(sizeof(stats.frag_block_count), sizeof(sqfs_u64));
-	TEST_EQUAL_UI(sizeof(stats.sparse_block_count), sizeof(sqfs_u64));
-	TEST_EQUAL_UI(sizeof(stats.total_frag_count), sizeof(sqfs_u64));
-	TEST_EQUAL_UI(sizeof(stats.actual_frag_count), sizeof(sqfs_u64));
+	TEST_EQUAL_UI(offsetof(sqfs_block_processor_stats_t,
+			       actual_frag_count), off);
 }
 
 static void test_blockproc_desc(void)
-- 
2.31.1

