netlink: Align allocations on __max_align_t, not uint64_t.

uint64_t is not sufficient alignment for allocators on all platforms.
On a CHERI platform pointers require 16 byte alignment, but also if a
type contained a uint128_t or long double it would not be aligned
correctly either.  C11 added max_align_t precisely to provide a
portable type for allocators to use.

Reviewed by:	melifaro
Obtained from:	CheriBSD
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D41301
This commit is contained in:
John Baldwin 2023-08-10 11:12:52 -07:00
parent 2bd446d7f1
commit 9795f14ec4
2 changed files with 5 additions and 4 deletions

View File

@ -41,12 +41,12 @@ struct linear_buffer {
char *base; /* Base allocated memory pointer */
uint32_t offset; /* Currently used offset */
uint32_t size; /* Total buffer size */
};
} __aligned(_Alignof(__max_align_t));
static inline void *
lb_alloc(struct linear_buffer *lb, int len)
{
len = roundup2(len, sizeof(uint64_t));
len = roundup2(len, _Alignof(__max_align_t));
if (lb->offset + len > lb->size)
return (NULL);
void *data = (void *)(lb->base + lb->offset);

View File

@ -33,6 +33,7 @@
#include <assert.h>
#include <errno.h>
#include <stdalign.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
@ -74,7 +75,7 @@ struct linear_buffer {
uint32_t offset; /* Currently used offset */
uint32_t size; /* Total buffer size */
struct linear_buffer *next; /* Buffer chaining */
};
} __aligned(alignof(__max_align_t));
static inline struct linear_buffer *
lb_init(uint32_t size)
@ -98,7 +99,7 @@ lb_free(struct linear_buffer *lb)
static inline char *
lb_allocz(struct linear_buffer *lb, int len)
{
len = roundup2(len, sizeof(uint64_t));
len = roundup2(len, alignof(__max_align_t));
if (lb->offset + len > lb->size)
return (NULL);
void *data = (void *)(lb->base + lb->offset);