summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Kitching <kitching@google.com>2019-11-12 21:47:27 +0800
committerCommit Bot <commit-bot@chromium.org>2019-11-25 13:38:20 +0000
commitc7d266e0d6e66a23adfd7681efa39e08f6ccf51b (patch)
tree27f9e3fe8ccd7f976d9a4e638dc5968222739746
parent30481361d88718fa6eead75508c552cc70c728dc (diff)
downloadvboot-c7d266e0d6e66a23adfd7681efa39e08f6ccf51b.tar.gz
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>
-rw-r--r--firmware/2lib/include/2constants.h6
-rw-r--r--tests/vb2_common_tests.c52
2 files changed, 31 insertions, 27 deletions
diff --git a/firmware/2lib/include/2constants.h b/firmware/2lib/include/2constants.h
index 8560d0de..b2abd4ab 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 11c97635..11d183cf 100644
--- a/tests/vb2_common_tests.c
+++ b/tests/vb2_common_tests.c
@@ -152,46 +152,50 @@ static void test_workbuf(void)
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");
}
/**