From db4ec2ffce3549225619cae51d828224a11a165f Mon Sep 17 00:00:00 2001 From: Andres Freund <andres@anarazel.de> Date: Sun, 11 Jan 2015 01:06:37 +0100 Subject: [PATCH] Fix alignment of pg_atomic_uint64 variables on some 32bit platforms. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I failed to recognize that pg_atomic_uint64 wasn't guaranteed to be 8 byte aligned on some 32bit platforms - which it has to be on some platforms to guarantee the desired atomicity and which we assert. As this is all compiler specific code anyway we can just rely on compiler specific tricks to enforce alignment. I've been unable to find concrete documentation about the version that introduce the sunpro alignment support, so that might need additional guards. I've verified that this works with gcc x86 32bit, but I don't have access to any other 32bit environment. Discussion: op.xpsjdkil0sbe7t@vld-kuci Per report from Vladimir Koković. --- src/include/port/atomics/arch-x86.h | 1 + src/include/port/atomics/generic-acc.h | 6 ++++++ src/include/port/atomics/generic-gcc.h | 2 +- src/include/port/atomics/generic-msvc.h | 2 +- src/include/port/atomics/generic-sunpro.h | 8 +++++++- src/include/port/atomics/generic-xlc.h | 2 +- 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/include/port/atomics/arch-x86.h b/src/include/port/atomics/arch-x86.h index fb5623d2dd1..168a49c7934 100644 --- a/src/include/port/atomics/arch-x86.h +++ b/src/include/port/atomics/arch-x86.h @@ -73,6 +73,7 @@ typedef struct pg_atomic_uint32 #define PG_HAVE_ATOMIC_U64_SUPPORT typedef struct pg_atomic_uint64 { + /* alignment guaranteed due to being on a 64bit platform */ volatile uint64 value; } pg_atomic_uint64; #endif diff --git a/src/include/port/atomics/generic-acc.h b/src/include/port/atomics/generic-acc.h index e16e282597b..c5639aadda0 100644 --- a/src/include/port/atomics/generic-acc.h +++ b/src/include/port/atomics/generic-acc.h @@ -40,6 +40,12 @@ typedef struct pg_atomic_uint32 #define PG_HAVE_ATOMIC_U64_SUPPORT typedef struct pg_atomic_uint64 { + /* + * Alignment is guaranteed to be 64bit. Search for "Well-behaved + * application restrictions" => "Data alignment and data sharing" on HP's + * website. Unfortunately the URL doesn't seem to stable enough to + * include. + */ volatile uint64 value; } pg_atomic_uint64; diff --git a/src/include/port/atomics/generic-gcc.h b/src/include/port/atomics/generic-gcc.h index f19ad34cc00..fea1cb5e135 100644 --- a/src/include/port/atomics/generic-gcc.h +++ b/src/include/port/atomics/generic-gcc.h @@ -98,7 +98,7 @@ typedef struct pg_atomic_uint32 typedef struct pg_atomic_uint64 { - volatile uint64 value; + volatile uint64 value __attribute__((aligned(8))); } pg_atomic_uint64; #endif /* defined(HAVE_GCC__ATOMIC_INT64_CAS) || defined(HAVE_GCC__SYNC_INT64_CAS) */ diff --git a/src/include/port/atomics/generic-msvc.h b/src/include/port/atomics/generic-msvc.h index 1d763ab78c3..d259d6f51d0 100644 --- a/src/include/port/atomics/generic-msvc.h +++ b/src/include/port/atomics/generic-msvc.h @@ -41,7 +41,7 @@ typedef struct pg_atomic_uint32 } pg_atomic_uint32; #define PG_HAVE_ATOMIC_U64_SUPPORT -typedef struct pg_atomic_uint64 +typedef struct __declspec(align(8)) pg_atomic_uint64 { volatile uint64 value; } pg_atomic_uint64; diff --git a/src/include/port/atomics/generic-sunpro.h b/src/include/port/atomics/generic-sunpro.h index b756fb979ce..7a3028ec3d0 100644 --- a/src/include/port/atomics/generic-sunpro.h +++ b/src/include/port/atomics/generic-sunpro.h @@ -55,7 +55,13 @@ typedef struct pg_atomic_uint32 #define PG_HAVE_ATOMIC_U64_SUPPORT typedef struct pg_atomic_uint64 { - volatile uint64 value; + /* + * Syntax to enforce variable alignment should be supported by versions + * supporting atomic.h, but it's hard to find accurate documentation. If + * it proves to be a problem, we'll have to add more version checks for 64 + * bit support. + */ + volatile uint64 value __attribute__((__aligned__(8))); } pg_atomic_uint64; #endif /* HAVE_ATOMIC_H */ diff --git a/src/include/port/atomics/generic-xlc.h b/src/include/port/atomics/generic-xlc.h index 92bba6f04fd..7a4c12ae6ca 100644 --- a/src/include/port/atomics/generic-xlc.h +++ b/src/include/port/atomics/generic-xlc.h @@ -32,7 +32,7 @@ typedef struct pg_atomic_uint32 #define PG_HAVE_ATOMIC_U64_SUPPORT typedef struct pg_atomic_uint64 { - volatile uint64 value; + volatile uint64 value __attribute__((__aligned__(8))); } pg_atomic_uint64; #endif /* __64BIT__ */ -- GitLab