Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] mem: Align heap allocations for pointers too



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