summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2023-02-13 13:13:05 -0500
committerBen Gamari <ben@smart-cactus.org>2023-03-02 10:33:54 -0500
commit0a0e22f50550ab5ba2b789239cfdd7410662e120 (patch)
treea3a1448286a1f961a080b1a9f396a864a5a599a0
parentb73b70bfa976eb57f1c4ec0c0db97a5fab2bf9ff (diff)
downloadhaskell-0a0e22f50550ab5ba2b789239cfdd7410662e120.tar.gz
rts: Statically assert alignment of Capability
In #22965 we noticed that changes in the size of `Capability` can result in unsound behavior due to the `align` pragma claiming an alignment which we don't in practice observe. Avoid this by statically asserting that the size is a multiple of the alignment. (cherry picked from commit 485ccddacff5ed8892348905754c02452ac8f523)
-rw-r--r--rts/Capability.h24
1 files changed, 16 insertions, 8 deletions
diff --git a/rts/Capability.h b/rts/Capability.h
index 2910c3faf0..0c4848c08d 100644
--- a/rts/Capability.h
+++ b/rts/Capability.h
@@ -28,6 +28,16 @@
#include "BeginPrivate.h"
+// We never want a Capability to overlap a cache line with
+// anything else, so round it up to a cache line size:
+#if defined(s390x_HOST_ARCH)
+#define CAPABILITY_ALIGNMENT 256
+#elif !defined(mingw32_HOST_OS)
+#define CAPABILITY_ALIGNMENT 64
+#else
+#define CAPABILITY_ALIGNMENT 1
+#endif
+
/* N.B. This must be consistent with CapabilityPublic in RtsAPI.h */
struct Capability_ {
// State required by the STG virtual machine when running Haskell
@@ -169,14 +179,12 @@ struct Capability_ {
StgTRecHeader *free_trec_headers;
uint32_t transaction_tokens;
} // typedef Capability is defined in RtsAPI.h
- // We never want a Capability to overlap a cache line with anything
- // else, so round it up to a cache line size:
-#if defined(s390x_HOST_ARCH)
- ATTRIBUTE_ALIGNED(256)
-#elif !defined(mingw32_HOST_OS)
- ATTRIBUTE_ALIGNED(64)
-#endif
- ;
+ ATTRIBUTE_ALIGNED(CAPABILITY_ALIGNMENT)
+;
+
+// We allocate arrays of Capabilities therefore we must ensure that the size is
+// a multiple of the claimed alignment
+GHC_STATIC_ASSERT(sizeof(struct Capability_) % CAPABILITY_ALIGNMENT == 0, "Capability size does not match cache size");
#if defined(THREADED_RTS)
#define ASSERT_TASK_ID(task) ASSERT(task->id == osThreadId())