vboot: workbuf alignment should always use 8

Rather than depending on the architecture and environment to
provide the correct memory alignment (__BIGGEST_ALIGNMENT__),
hardcode to 8, which should be sufficient for all cases.

(Previously, by using __BIGGEST_ALIGNMENT__, this is set to
16 in all known cases, which is unnecessarily large.)

Update vb2_workbuf tests to be more flexible according to
VB2_WORKBUF_ALIGN value.

BUG=b:124141368
TEST=make clean && make runtests
TEST=Try values of VB2_WORKBUF_ALIGN=2,4,8,16,32,64
BRANCH=none

Change-Id: I819586119fa3102fa423a01e0737e6864c05d752
Signed-off-by: Joel Kitching <kitching@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/1911921
Reviewed-by: Joel Kitching <kitching@chromium.org>
Commit-Queue: Joel Kitching <kitching@chromium.org>
Tested-by: Joel Kitching <kitching@chromium.org>
diff --git a/firmware/2lib/include/2constants.h b/firmware/2lib/include/2constants.h
index 8560d0d..b2abd4a 100644
--- a/firmware/2lib/include/2constants.h
+++ b/firmware/2lib/include/2constants.h
@@ -66,9 +66,9 @@
  *        wb.size = sizeof(buf);
  */
 
-/* We might get away with using __alignof__(void *), but since GCC defines a
- * macro for us we'll be safe and use that. */
-#define VB2_WORKBUF_ALIGN __BIGGEST_ALIGNMENT__
+/* We want consistent alignment across all architectures.
+   8-byte should work for all of them. */
+#define VB2_WORKBUF_ALIGN 8
 
 /* Maximum length of a HWID in bytes, counting terminating null. */
 #define VB2_GBB_HWID_MAX_SIZE 256
diff --git a/tests/vb2_common_tests.c b/tests/vb2_common_tests.c
index 11c9763..11d183c 100644
--- a/tests/vb2_common_tests.c
+++ b/tests/vb2_common_tests.c
@@ -152,46 +152,50 @@
 	uint8_t *p0 = (uint8_t *)buf, *ptr;
 	struct vb2_workbuf wb;
 
-	/* NOTE: There are several magic numbers below which assume that
-	 * VB2_WORKBUF_ALIGN == 16 */
-
 	/* Init */
-	vb2_workbuf_init(&wb, p0, 64);
+	vb2_workbuf_init(&wb, p0, VB2_WORKBUF_ALIGN * 2);
 	TEST_EQ(vb2_offset_of(p0, wb.buf), 0, "Workbuf init aligned");
-	TEST_EQ(wb.size, 64, "  size");
+	TEST_EQ(wb.size, VB2_WORKBUF_ALIGN * 2, "  size");
 
-	vb2_workbuf_init(&wb, p0 + 4, 64);
+	/* Unaligned init */
+	vb2_workbuf_init(&wb, p0 + 1, VB2_WORKBUF_ALIGN * 2);
 	TEST_EQ(vb2_offset_of(p0, wb.buf), VB2_WORKBUF_ALIGN,
 		"Workbuf init unaligned");
-	TEST_EQ(wb.size, 64 - VB2_WORKBUF_ALIGN + 4, "  size");
+	TEST_EQ(wb.size, VB2_WORKBUF_ALIGN + 1, "  size");
 
-	vb2_workbuf_init(&wb, p0 + 2, 5);
-	TEST_EQ(wb.size, 0, "Workbuf init tiny unaligned size");
+	/* No size left after align */
+	vb2_workbuf_init(&wb, p0 + 1, VB2_WORKBUF_ALIGN - 1);
+	TEST_EQ(wb.size, 0, "Workbuf init size=0 after align");
+	vb2_workbuf_init(&wb, p0 + 1, VB2_WORKBUF_ALIGN - 2);
+	TEST_EQ(wb.size, 0, "Workbuf init size=-1 after align");
 
 	/* Alloc rounds up */
-	vb2_workbuf_init(&wb, p0, 64);
-	ptr = vb2_workbuf_alloc(&wb, 22);
+	vb2_workbuf_init(&wb, p0, VB2_WORKBUF_ALIGN * 2);
+	ptr = vb2_workbuf_alloc(&wb, VB2_WORKBUF_ALIGN - 1);
 	TEST_EQ(vb2_offset_of(p0, ptr), 0, "Workbuf alloc");
-	TEST_EQ(vb2_offset_of(p0, wb.buf), 32, "  buf");
-	TEST_EQ(wb.size, 32, "  size");
+	TEST_EQ(vb2_offset_of(p0, wb.buf), VB2_WORKBUF_ALIGN, "  buf");
+	TEST_EQ(wb.size, VB2_WORKBUF_ALIGN, "  size");
 
-	vb2_workbuf_init(&wb, p0, 32);
-	TEST_PTR_EQ(vb2_workbuf_alloc(&wb, 33), NULL, "Workbuf alloc too big");
+	/* Alloc doesn't fit */
+	vb2_workbuf_init(&wb, p0, VB2_WORKBUF_ALIGN);
+	TEST_PTR_EQ(vb2_workbuf_alloc(&wb, VB2_WORKBUF_ALIGN + 1), NULL,
+		    "Workbuf alloc too big");
 
 	/* Free reverses alloc */
-	vb2_workbuf_init(&wb, p0, 32);
-	vb2_workbuf_alloc(&wb, 22);
-	vb2_workbuf_free(&wb, 22);
+	vb2_workbuf_init(&wb, p0, VB2_WORKBUF_ALIGN * 2);
+	vb2_workbuf_alloc(&wb, VB2_WORKBUF_ALIGN + 1);
+	vb2_workbuf_free(&wb, VB2_WORKBUF_ALIGN + 1);
 	TEST_EQ(vb2_offset_of(p0, wb.buf), 0, "Workbuf free buf");
-	TEST_EQ(wb.size, 32, "  size");
+	TEST_EQ(wb.size, VB2_WORKBUF_ALIGN * 2, "  size");
 
 	/* Realloc keeps same pointer as alloc */
-	vb2_workbuf_init(&wb, p0, 64);
-	vb2_workbuf_alloc(&wb, 6);
-	ptr = vb2_workbuf_realloc(&wb, 6, 21);
+	vb2_workbuf_init(&wb, p0, VB2_WORKBUF_ALIGN * 3);
+	vb2_workbuf_alloc(&wb, VB2_WORKBUF_ALIGN - 1);
+	ptr = vb2_workbuf_realloc(&wb, VB2_WORKBUF_ALIGN - 1,
+				  VB2_WORKBUF_ALIGN + 1);
 	TEST_EQ(vb2_offset_of(p0, ptr), 0, "Workbuf realloc");
-	TEST_EQ(vb2_offset_of(p0, wb.buf), 32, "  buf");
-	TEST_EQ(wb.size, 32, "  size");
+	TEST_EQ(vb2_offset_of(p0, wb.buf), VB2_WORKBUF_ALIGN * 2, "  buf");
+	TEST_EQ(wb.size, VB2_WORKBUF_ALIGN, "  size");
 }
 
 /**