From: Jessica Clarke <jrtc27@jrtc27.com>
To: git@vger.kernel.org
Cc: Jessica Clarke <jrtc27@jrtc27.com>
Subject: [PATCH v2] mem-pool: Don't assume uintmax_t is aligned enough for all types
Date: Sun, 23 Jan 2022 15:24:58 +0000 [thread overview]
Message-ID: <20220123152458.72540-1-jrtc27@jrtc27.com> (raw)
In-Reply-To: <20220105132324.6651-1-jrtc27@jrtc27.com>
Currently mem_pool_alloc uses sizeof(uintmax_t) as a proxy for what
should be _Alignof(max_align_t) in C11. On most architectures this is
sufficient (though on m68k it is in fact overly strict, since the
de-facto ABI, which differs from the specified System V ABI, has the
maximum alignment of all types as 2 bytes), but on CHERI, and thus Arm's
Morello prototype, it is insufficient for any type that stores a
pointer, which must be aligned to 128 bits (on 64-bit architectures
extended with CHERI), whilst uintmax_t is a 64-bit integer.
Fix this by introducing our own approximation for max_align_t and a
means to compute _Alignof it without relying on C11. Currently this
union only contains uintmax_t and void *, but more types can be added as
needed.
Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
---
mem-pool.c | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/mem-pool.c b/mem-pool.c
index ccdcad2e3d..b373cadd01 100644
--- a/mem-pool.c
+++ b/mem-pool.c
@@ -7,6 +7,22 @@
#define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block))
+/*
+ * The inner union is an approximation for C11's max_align_t, and the
+ * struct + offsetof computes _Alignof. This can all just be replaced
+ * with _Alignof(max_align_t) if/when C11 is part of the baseline.
+ *
+ * Add more types to the union if the current set is insufficient.
+ */
+struct git_max_alignment {
+ char unalign;
+ union {
+ uintmax_t max_align_uintmax;
+ void *max_align_pointer;
+ } aligned;
+};
+#define GIT_MAX_ALIGNMENT offsetof(struct git_max_alignment, aligned)
+
/*
* Allocate a new mp_block and insert it after the block specified in
* `insert_after`. If `insert_after` is NULL, then insert block at the
@@ -69,9 +85,9 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len)
struct mp_block *p = NULL;
void *r;
- /* round up to a 'uintmax_t' alignment */
- if (len & (sizeof(uintmax_t) - 1))
- len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
+ /* round up to a 'GIT_MAX_ALIGNMENT' alignment */
+ if (len & (GIT_MAX_ALIGNMENT - 1))
+ len += GIT_MAX_ALIGNMENT - (len & (GIT_MAX_ALIGNMENT - 1));
if (pool->mp_block &&
pool->mp_block->end - pool->mp_block->next_free >= len)
--
2.33.1
next prev parent reply other threads:[~2022-01-23 15:25 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-05 13:23 [PATCH] Properly align memory allocations and temporary buffers Jessica Clarke
2022-01-06 21:46 ` Taylor Blau
2022-01-06 21:56 ` Jessica Clarke
2022-01-06 22:27 ` Junio C Hamano
2022-01-06 22:56 ` Jessica Clarke
2022-01-07 0:10 ` Junio C Hamano
2022-01-07 0:22 ` Jessica Clarke
2022-01-07 0:31 ` brian m. carlson
2022-01-07 0:39 ` Jessica Clarke
2022-01-07 1:43 ` brian m. carlson
2022-01-07 2:08 ` Jessica Clarke
2022-01-07 2:11 ` Jessica Clarke
2022-01-07 19:30 ` Junio C Hamano
2022-01-07 19:33 ` Jessica Clarke
2022-01-07 20:56 ` René Scharfe
2022-01-07 21:30 ` Junio C Hamano
2022-01-07 23:30 ` René Scharfe
2022-01-08 0:18 ` Elijah Newren
2022-01-06 23:22 ` brian m. carlson
2022-01-06 23:31 ` Jessica Clarke
2022-01-07 14:57 ` Philip Oakley
2022-01-07 16:08 ` René Scharfe
2022-01-07 16:21 ` Jessica Clarke
2022-01-12 13:58 ` Jessica Clarke
2022-01-12 15:47 ` René Scharfe
2022-01-12 15:49 ` Jessica Clarke
2022-01-23 15:24 ` Jessica Clarke [this message]
2022-01-23 20:17 ` [PATCH v2] mem-pool: Don't assume uintmax_t is aligned enough for all types Junio C Hamano
2022-01-23 20:23 ` Jessica Clarke
2022-01-23 20:28 ` Junio C Hamano
2022-01-23 20:33 ` [PATCH v3] " Jessica Clarke
2022-01-24 17:11 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220123152458.72540-1-jrtc27@jrtc27.com \
--to=jrtc27@jrtc27.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).