Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [PATCH] mem: Align heap allocations for pointers too
- X-seq: zsh-workers 55297
- From: Florian Schmaus <florian.schmaus@xxxxxxxxxxx>
- To: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>, zsh-workers@xxxxxxx
- Subject: Re: [PATCH] mem: Align heap allocations for pointers too
- Date: Tue, 24 Nov 2026 11:29:21 +0100
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=codasip.com; dmarc=pass action=none header.from=codasip.com; dkim=pass header.d=codasip.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=rsfqy2DdXdQeEN55FDRaNdDkCuUSPtlF+R2VtQI3cR4=; b=Hc85G70JN0yPmrps7xXYn+qVTfvTolcgvGpbXsmi54kAFPKcZs6yfMklrS4azopsUcOr7D+Bnt9K5iuh+JYE5PusRVI7swwjI8WLafT4CD/ghDUQk4dN0fyQABXu6jRMEbS/vd1VfRAGt9roL/V2T9LKVOGM7L0qh2BtBOGAsjah0j7wcg1JwCmw3YKZNaXpeKJhoHbWKeJDj5kujzPYLIfZLz7xqQcJcNQZGw6jtFoDT5Wfo2KByk9FRq4WMnsBjlo6bWZFjvfJdHSUuPoL4xvhpRxGeLH7oSDJJloorKiNO5bJssphyH25Pi5eHizlHgWWKr/08xX4A8GWOGwVSA==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=QGV6EXxkeGwakIDRAG8VELDMo4NZ3HNrubAyVCqXZaOJPLm9lwjsgpu3/o7jznheTXjs03ioi6L897LJOvaNZB6UmLCfOwf78V4dJ95DEiFnWBtPu+pbQSOPn5lnzSeANKaFF7bVoJnE8C3oLl+I2wxJOeVqhSYmVcduE1yEZAmedgJaJ7xBb6S32HyBNoZZpugyb2vbibJ5ptN87Qtnz2BLdqs9+t447M8EpkwGsMlP/EUw8mQV5SmHDL3CjzkaKYWQq8RCW28If50hYl7HIJUaC9Tbb5QNvDnF8NrGGeBPPaw22bf9QAbfwKh+2Zc78AFUxFJ2vY3CoDXWrgcX4g==
- Archived-at: <https://zsh.org/workers/55297>
- In-reply-to: <887fdf66-5e75-4a19-8ddd-65b41b3bdcfe@app.fastmail.com>
- List-id: <zsh-workers.zsh.org>
- References: <20261123155137.108879-1-florian.schmaus@codasip.com> <887fdf66-5e75-4a19-8ddd-65b41b3bdcfe@app.fastmail.com>
On 23/11/2026 20.07, Daniel Shahaf wrote:
[You don't often get email from d.s@xxxxxxxxxxxxxxxxxx. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Florian Schmaus wrote on Mon, 23 Nov 2026 15:51 +00:00:
Add a void * member to union mem_align, so that heap allocations are
suitably aligned for pointers too.
First, the comment on line 1030 will need to be updated:
Done in v2.
Secondly, if I understand correctly, mem_align is what determines the
alignment of zhalloc()'s return value. If so, are we guaranteed that
alignment that's sufficient for zlong, for double, and for a pointer
would be sufficient, on all platforms, for any type we might use?
Not by the C standard, but in practice, yes. Unlike malloc(), zhalloc()
is not a general-purpose allocator: it only hands out memory for zsh's
own data, i.e., strings, pointer arrays, and structs made of integers,
zlong, double, and pointers. As far as I can tell, zsh uses no type
with a stricter alignment requirement, such as long double. The only
explicit alignment, ALIGN64 in glob.c, asks for 8 bytes.
The other half of zhalloc()'s alignment is where the arena starts,
i.e., sizeof(struct heap) bytes into the heap block, which itself comes
from mmap() or malloc(). Since struct heap contains pointers, its size
is a multiple of the pointer alignment, so the arena start is suitably
aligned for pointers, too.
Should we align to max_align_t as well (conditionally on being supported
by the compiler/libc, of course)?
We could use _Alignof(max_align_t) instead, with the current union as
fallback for pre-C11 compilers. However, that would change the heap
layout on common platforms such as x86_64, where _Alignof(max_align_t)
is 16 rather than 8. It would also require padding the arena start, as
sizeof(struct heap) is not always a multiple of 16 (e.g., it is 40 with
ZSH_HEAP_DEBUG on x86_64). I'd rather keep this patch minimal and
consider that separately.
If I'm not mistaken, the incumbent code assumes that M_ALIGN is a power
of 2 (when it does «(size + M_ALIGN - 1) & ~(M_ALIGN - 1)» — that's
"round up to the next multiple of M_ALIGN" when M_ALIGN is a power
of 2), but nothing guarantees that to be the case.
>
This /is/ a preëxisting issue, but it's not unrelated to the patch,
unfortunately: it does bear on whether the patch might introduce
a regression on some platforms.
Right, and the same goes for H_ISIZE, which zhalloc() and hrealloc()
round with the same mask.
The patch only changes the size of the union on platforms where
pointers are larger than both zlong and double, e.g., on 64-bit CHERI,
where it becomes 16, a power of two. Everywhere else, the generated
code stays the same: on x86_64, the disassembly of mem.o is identical
with and without the patch.
On a hypothetical platform where the size is not a power of two (say,
with 12-byte pointers), zhalloc() would still return suitably aligned
memory. Only ZSH_MEM's free() could reject valid pointers, as its
"(long)p & (M_ALIGN - 1)" check would misfire. However ZSH_MEM is
disabled by default and marked as deprecated in configure. Still, to
rule this out, v2 adds a preparatory patch with a compile-time check
that sizeof(union mem_align) is a power of two.
Only slightly related: I wonder if it is not time to remove ZSH_MEM from
zsh…
Cheers,
Daniel
Best
Florian
1: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
2: https://codasip.com/solutions/riscv-processor-safety-security/cheri/
---
Src/mem.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/Src/mem.c b/Src/mem.c
index 0b6f76e46b2b..5d9ba7d280a5 100644
--- a/Src/mem.c
+++ b/Src/mem.c
@@ -113,6 +113,7 @@ static int h_m[1025], h_push, h_pop, h_free;
union mem_align {
zlong l;
double d;
+ void *p;
};
#define H_ISIZE sizeof(union mem_align)
--
2.56.1
Messages sorted by:
Reverse Date,
Date,
Thread,
Author