To send and receive messages with ancillary data through Unix sockets, Zig provides wrappers over msghdr, those being Io.net.IncomingMessage and Io.net.OutgoingMessage.
Currently, both structs define the control buffer simply as:
control:[]u8
To properly access and process ancillary data, the POSIX standard requires using
CMSG_* macros on C(which don't have Zig equivalents yet). Macros like CMSG_FIRSTHDR strictly assume that the control field is aligned to a cmsghdr struct. Some examples:
musl
glibc
FreeBSD
Because []u8 only guarantees an alignment of 1 byte, casting or accessing this
buffer as a cmsghdr can lead to unaligned pointer dereferences and can cause direct crashes on architectures with strict alignment requirements like ARM.
The control field type in both structs should be explicitly aligned to cmsghdr
instead of a simple byte slice:
control:[]align(@alignOf(std.c.cmsghdr))u8,
To send and receive messages with ancillary data through Unix sockets, Zig provides wrappers over `msghdr`, those being `Io.net.IncomingMessage` and `Io.net.OutgoingMessage`.
Currently, both structs define the `control` buffer simply as:
```zig
control: []u8
```
To properly access and process ancillary data, the [POSIX standard](https://www.open-std.org/jtc1/sc22/open/n4217.pdf#page=414&zoom=auto,-177,731) requires using
CMSG_* macros on C(which [don't have Zig equivalents yet](https://codeberg.org/ziglang/zig/issues/30629)). Macros like `CMSG_FIRSTHDR` strictly assume that the control field is aligned to a `cmsghdr` struct. Some examples:
[musl](https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n360)
[glibc](https://sourceware.org/git/?p=glibc.git;a=blob;f=bits/socket.h;h=cbd9ae188b92f3093ce97744a87568748276b611;hb=HEAD#l234)
[FreeBSD](https://github.com/freebsd/freebsd-src/blob/815dc35b7b5ed694fc1986d054149e375b8a17bd/sys/sys/socket.h#L579)
Because `[]u8` only guarantees an alignment of 1 byte, casting or accessing this
buffer as a `cmsghdr` can lead to unaligned pointer dereferences and can cause direct crashes on architectures with strict alignment requirements like ARM.
The control field type in both structs should be explicitly aligned to `cmsghdr`
instead of a simple byte slice:
```zig
control: []align(@alignOf(std.c.cmsghdr)) u8,
```