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 55300
- From: "Daniel Shahaf" <d.s@xxxxxxxxxxxxxxxxxx>
- To: "Florian Schmaus" <florian.schmaus@xxxxxxxxxxx>, zsh-workers@xxxxxxx
- Subject: Re: [PATCH] mem: Align heap allocations for pointers too
- Date: Thu, 26 Nov 2026 23:02:48 +0000
- Archived-at: <https://zsh.org/workers/55300>
- Dkim2-signature: i=1; m=1; t=1795734188; d=daniel.shahaf.name; mf=PGQuc0BkYW5pZWwuc2hhaGFmLm5hbWU+; rt=PHpzaC13b3JrZXJzQHpzaC5vcmc+; s=fm2:rsa-sha256:qjB7EweXg93ZSRrQJwyDm6v8AABCWi2mnt4CUGXQlu+I52q Vl/gwq/QdRUZJ4DhMfS8poVPjJflljU2f8MOM6QRKDRnOi00I+2lpp15LKnJYnPK SlptB4ZZcLquKITjZ8zpE0xXeZuR5u+v0mUBQ7YE6oGdLKNrXa+bQYhZNOLs+bJ6 lx4Q0Ili3/6RFyiLrcfO27rFD3Lje1XQMmIExbFt1LbA98/oh1Qetcdh3q1Hj4PM 6cLDUdDdrnvMVSQs3udIrSES47lFvrnr68726V8Ohe6EqATkSFREaEqo16cpZZtU LwlpLDumwR88O7NRT27Sl+/Aw5PQhyx5zc6F6Ug==;
- Feedback-id: i425e4195:Fastmail
- In-reply-to: <bb4be1f9-e10e-418c-b9e1-18f6ed6eaf5c@codasip.com>
- List-id: <zsh-workers.zsh.org>
- Message-instance: m=1; h=sha256:5ftlfwqYUJnJ8D3V6GDJ6feo/4eler1slDH+KEXgClQ=:Cat8Cgvd4K/W7A3c8Spxt9WsWK0dR9gn1+6DLGpCdiM=;
- References: <20261123155137.108879-1-florian.schmaus@codasip.com> <887fdf66-5e75-4a19-8ddd-65b41b3bdcfe@app.fastmail.com> <bb4be1f9-e10e-418c-b9e1-18f6ed6eaf5c@codasip.com>
Florian Schmaus wrote on Tue, 24 Nov 2026 10:29 +00:00:
> On 23/11/2026 20.07, Daniel Shahaf wrote:
>> 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.
⋮
>> 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.
>
Thus, if we ever use zhalloc() to allocate a «long double» or an
«int128_t», that could result in a crash on some platforms only.
Sounds like we have a bit of a technical debt to pay off.
> 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.
>
To be clear, this, too, falls under "Not by the C standard, but in
practice, yes": the C standard allows a compiler to insert trailing
padding bytes into any struct type. (I figure you know this, but
spelling it out for the benefit of lurkers on the list.)
>
>> 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.
>
Sounds reasonable enough.
>
>> 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.
>
Thank you :-)
I could be missing something, but I think v2 1/2 and v2 2/2 are both
ready to be merged. (And then the _Alignof(max_align_t) issue can be
pursued, or at least recorded in Etc/BUGS.)
> Only slightly related: I wonder if it is not time to remove ZSH_MEM from
> zsh…
>
No strong opinion here.
Apologies for my lateness; I was dealing with matters on private lists.
Cheers,
Daniel
>>> 1: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
>>> 2: https://codasip.com/solutions/riscv-processor-safety-security/cheri/
Messages sorted by:
Reverse Date,
Date,
Thread,
Author