]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use proper padding instead of using alignas()
authorOndřej Surý <ondrej@isc.org>
Mon, 4 Dec 2023 11:21:33 +0000 (12:21 +0100)
committerOndřej Surý <ondrej@isc.org>
Thu, 8 Feb 2024 09:54:35 +0000 (10:54 +0100)
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.

configure.ac
lib/isc/Makefile.am
lib/isc/include/isc/align.h [deleted file]
lib/isc/include/isc/quota.h
lib/isc/include/isc/rwlock.h
lib/isc/job_p.h
lib/isc/mem.c

index a27b22543c26669a4bc1d462aa3b3194c6325dfc..de7c9dab643918564f369090bc7dcabce8979467 100644 (file)
@@ -388,7 +388,7 @@ AC_COMPILE_IFELSE(
   ],
   [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.])])
 
index c16de9d83d490b79cfd8dbba677d4eadfe7f4a2d..f6343136efb2f8b479b6733cdfd16b21ce237eb7 100644 (file)
@@ -4,7 +4,6 @@ lib_LTLIBRARIES = libisc.la
 
 libisc_ladir = $(includedir)/isc
 libisc_la_HEADERS =                    \
-       include/isc/align.h             \
        include/isc/ascii.h             \
        include/isc/assertions.h        \
        include/isc/async.h             \
diff --git a/lib/isc/include/isc/align.h b/lib/isc/include/isc/align.h
deleted file mode 100644 (file)
index 7b72e9d..0000000
+++ /dev/null
@@ -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 */
index 571cd5b09b772c4abb4e0a4921fe191b132c50f2..5597b9fda12af7522e6abb74962456c72b720450 100644 (file)
@@ -30,7 +30,6 @@
  *** Imports.
  ***/
 
-#include <isc/align.h>
 #include <isc/atomic.h>
 #include <isc/job.h>
 #include <isc/lang.h>
@@ -57,14 +56,19 @@ ISC_LANG_BEGINDECLS
  * synchronization between multiple threads (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)");
 struct isc_quota {
        int                  magic;
        atomic_uint_fast32_t max;
        atomic_uint_fast32_t used;
        atomic_uint_fast32_t soft;
        struct {
-               alignas(ISC_OS_CACHELINE_SIZE) struct cds_wfcq_head head;
-               alignas(ISC_OS_CACHELINE_SIZE) struct cds_wfcq_tail tail;
+               struct cds_wfcq_head head;
+               uint8_t              __padding[ISC_OS_CACHELINE_SIZE -
+                                      sizeof(struct __cds_wfcq_head)];
+               struct cds_wfcq_tail tail;
        } jobs;
        ISC_LINK(isc_quota_t) link;
 };
index c5b95182b4b1ae1800feb5a5212b544de1ef1713..3ae93d6a3a6702edaa01944cc48d643ce91d64d3 100644 (file)
@@ -161,15 +161,23 @@ typedef pthread_rwlock_t isc__rwlock_t;
 
 #else /* USE_PTHREAD_RWLOCK */
 
-#include <isc/align.h>
 #include <isc/atomic.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 {
-       alignas(ISC_OS_CACHELINE_SIZE) atomic_uint_fast32_t readers_ingress;
-       alignas(ISC_OS_CACHELINE_SIZE) atomic_uint_fast32_t readers_egress;
-       alignas(ISC_OS_CACHELINE_SIZE) atomic_int_fast32_t writers_barrier;
-       alignas(ISC_OS_CACHELINE_SIZE) atomic_bool writers_lock;
+       atomic_uint_fast32_t readers_ingress;
+       uint8_t __padding1[ISC_OS_CACHELINE_SIZE - sizeof(atomic_uint_fast32_t)];
+       atomic_uint_fast32_t readers_egress;
+       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;
index 06c7bc319ec8ca8c12e357023b03b5e9445f9843..385ef02b348a5f592954d714db4490013af5bb5a 100644 (file)
@@ -13,7 +13,6 @@
 
 #pragma once
 
-#include <isc/align.h>
 #include <isc/job.h>
 #include <isc/loop.h>
 #include <isc/os.h>
  * mutex, because we are only using enqueue and splice, and those don't need
  * 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 {
-       alignas(ISC_OS_CACHELINE_SIZE) struct __cds_wfcq_head head;
-       alignas(ISC_OS_CACHELINE_SIZE) struct cds_wfcq_tail tail;
+       struct __cds_wfcq_head head;
+       uint8_t __padding[ISC_OS_CACHELINE_SIZE -
+                         sizeof(struct __cds_wfcq_head)];
+       struct cds_wfcq_tail tail;
 } isc_jobqueue_t;
 
 typedef ISC_LIST(isc_job_t) isc_joblist_t;
index d117decfe3afbdc51a8b9178af408d54d11fc85d..55debffe55191fb0fa3dc940e0cba247a1db2921 100644 (file)
@@ -21,7 +21,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include <isc/align.h>
 #include <isc/hash.h>
 #include <isc/magic.h>
 #include <isc/mem.h>