2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00

Use proper padding instead of using alignas()

As it was pointed out, the alignas() can't be used on objects larger
than `max_align_t` otherwise the compiler might miscompile the code to
use auto-vectorization on unaligned memory.

As we were only using alignas() as a way to prevent false memory
sharing, we can use manual padding in the affected structures.
This commit is contained in:
Ondřej Surý 2023-12-04 12:21:33 +01:00
parent 05e60a0af6
commit 2463e5232d
No known key found for this signature in database
GPG Key ID: 2820F37E873DEA41
7 changed files with 29 additions and 34 deletions

View File

@ -388,7 +388,7 @@ AC_COMPILE_IFELSE(
], ],
[AC_MSG_FAILURE([stdatomic.h header found, but compilation failed, please fix your toolchain.])]) [AC_MSG_FAILURE([stdatomic.h header found, but compilation failed, please fix your toolchain.])])
AC_CHECK_HEADERS([stdalign.h stdnoreturn.h], AC_CHECK_HEADERS([stdnoreturn.h],
[], [],
[AC_MSG_ERROR([C11 standard headers not found, update your toolchain.])]) [AC_MSG_ERROR([C11 standard headers not found, update your toolchain.])])

View File

@ -4,7 +4,6 @@ lib_LTLIBRARIES = libisc.la
libisc_ladir = $(includedir)/isc libisc_ladir = $(includedir)/isc
libisc_la_HEADERS = \ libisc_la_HEADERS = \
include/isc/align.h \
include/isc/ascii.h \ include/isc/ascii.h \
include/isc/assertions.h \ include/isc/assertions.h \
include/isc/async.h \ include/isc/async.h \

View File

@ -1,20 +0,0 @@
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#pragma once
#ifdef HAVE_STDALIGN_H
#include <stdalign.h>
#else /* ifdef HAVE_STDALIGN_H */
#define alignas(x) __attribute__((__aligned__(x)))
#endif /* ifdef HAVE_STDALIGN_H */

View File

@ -30,7 +30,6 @@
*** Imports. *** Imports.
***/ ***/
#include <isc/align.h>
#include <isc/atomic.h> #include <isc/atomic.h>
#include <isc/job.h> #include <isc/job.h>
#include <isc/lang.h> #include <isc/lang.h>
@ -57,14 +56,19 @@ ISC_LANG_BEGINDECLS
* synchronization between multiple threads (see urcu/wfcqueue.h for * synchronization between multiple threads (see urcu/wfcqueue.h for
* detailed description). * detailed description).
*/ */
STATIC_ASSERT(ISC_OS_CACHELINE_SIZE >= sizeof(struct __cds_wfcq_head),
"ISC_OS_CACHELINE_SIZE smaller than "
"sizeof(struct __cds_wfcq_head)");
struct isc_quota { struct isc_quota {
int magic; int magic;
atomic_uint_fast32_t max; atomic_uint_fast32_t max;
atomic_uint_fast32_t used; atomic_uint_fast32_t used;
atomic_uint_fast32_t soft; atomic_uint_fast32_t soft;
struct { struct {
alignas(ISC_OS_CACHELINE_SIZE) struct cds_wfcq_head head; struct cds_wfcq_head head;
alignas(ISC_OS_CACHELINE_SIZE) struct cds_wfcq_tail tail; uint8_t __padding[ISC_OS_CACHELINE_SIZE -
sizeof(struct __cds_wfcq_head)];
struct cds_wfcq_tail tail;
} jobs; } jobs;
ISC_LINK(isc_quota_t) link; ISC_LINK(isc_quota_t) link;
}; };

View File

@ -161,15 +161,23 @@ typedef pthread_rwlock_t isc__rwlock_t;
#else /* USE_PTHREAD_RWLOCK */ #else /* USE_PTHREAD_RWLOCK */
#include <isc/align.h>
#include <isc/atomic.h> #include <isc/atomic.h>
#include <isc/os.h> #include <isc/os.h>
STATIC_ASSERT(ISC_OS_CACHELINE_SIZE >= sizeof(atomic_uint_fast32_t),
"ISC_OS_CACHELINE_SIZE smaller than "
"sizeof(atomic_uint_fast32_t)");
STATIC_ASSERT(ISC_OS_CACHELINE_SIZE >= sizeof(atomic_int_fast32_t),
"ISC_OS_CACHELINE_SIZE smaller than sizeof(atomic_int_fast32_t)");
struct isc_rwlock { struct isc_rwlock {
alignas(ISC_OS_CACHELINE_SIZE) atomic_uint_fast32_t readers_ingress; atomic_uint_fast32_t readers_ingress;
alignas(ISC_OS_CACHELINE_SIZE) atomic_uint_fast32_t readers_egress; uint8_t __padding1[ISC_OS_CACHELINE_SIZE - sizeof(atomic_uint_fast32_t)];
alignas(ISC_OS_CACHELINE_SIZE) atomic_int_fast32_t writers_barrier; atomic_uint_fast32_t readers_egress;
alignas(ISC_OS_CACHELINE_SIZE) atomic_bool writers_lock; uint8_t __padding2[ISC_OS_CACHELINE_SIZE - sizeof(atomic_uint_fast32_t)];
atomic_int_fast32_t writers_barrier;
uint8_t __padding3[ISC_OS_CACHELINE_SIZE - sizeof(atomic_int_fast32_t)];
atomic_bool writers_lock;
}; };
typedef struct isc_rwlock isc_rwlock_t; typedef struct isc_rwlock isc_rwlock_t;

View File

@ -13,7 +13,6 @@
#pragma once #pragma once
#include <isc/align.h>
#include <isc/job.h> #include <isc/job.h>
#include <isc/loop.h> #include <isc/loop.h>
#include <isc/os.h> #include <isc/os.h>
@ -24,9 +23,15 @@
* mutex, because we are only using enqueue and splice, and those don't need * mutex, because we are only using enqueue and splice, and those don't need
* any synchronization (see urcu/wfcqueue.h for detailed description). * any synchronization (see urcu/wfcqueue.h for detailed description).
*/ */
STATIC_ASSERT(ISC_OS_CACHELINE_SIZE >= sizeof(struct __cds_wfcq_head),
"ISC_OS_CACHELINE_SIZE smaller than "
"sizeof(struct __cds_wfcq_head)");
typedef struct isc_jobqueue { typedef struct isc_jobqueue {
alignas(ISC_OS_CACHELINE_SIZE) struct __cds_wfcq_head head; struct __cds_wfcq_head head;
alignas(ISC_OS_CACHELINE_SIZE) struct cds_wfcq_tail tail; uint8_t __padding[ISC_OS_CACHELINE_SIZE -
sizeof(struct __cds_wfcq_head)];
struct cds_wfcq_tail tail;
} isc_jobqueue_t; } isc_jobqueue_t;
typedef ISC_LIST(isc_job_t) isc_joblist_t; typedef ISC_LIST(isc_job_t) isc_joblist_t;

View File

@ -21,7 +21,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <isc/align.h>
#include <isc/hash.h> #include <isc/hash.h>
#include <isc/magic.h> #include <isc/magic.h>
#include <isc/mem.h> #include <isc/mem.h>