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

[PATCH] mem: Align heap allocations for pointers too



zhalloc() rounds the size of each allocation up to a multiple of
sizeof(union mem_align). As the union only holds a zlong and a double,
this is 8 bytes on common platforms. Allocations are carved
consecutively out of an arena, so their start addresses are only
guaranteed to be 8-byte aligned.

On CHERI [1, 2], pointers are capabilities, which on 64-bit systems are
16 bytes in size and must be 16-byte aligned. Loading or storing a
pointer in a heap allocation that is only 8-byte aligned therefore
faults.

Add a void * member to union mem_align, so that heap allocations are
suitably aligned for pointers too. This also raises the block alignment
(M_ALIGN) of the optional ZSH_MEM allocator accordingly. On platforms
where pointers are no larger than zlong and double, the size of the
union, and thus the heap layout, remains unchanged.

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