From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alexander Korotkov <aekorotkov@gmail.com>
Cc: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Alexander Korotkov <akorotkov@postgresql.org>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: Detecting pointer misalignment (was Re: pgsql: Implementation of subscripting for jsonb)
Date: Mon, 08 Feb 2021 11:49:41 -0500
Message-ID: <628089.1612802981@sss.pgh.pa.us> (raw)
In-Reply-To: <477102.1612747200@sss.pgh.pa.us>
References: <E1l6Jjn-0000kp-75@gemulon.postgresql.org>
<37bb56a0-391d-759d-771b-9c1d8c15a84b@iki.fi>
<3293925.1612163110@sss.pgh.pa.us>
<CAPpHfdsne3=T=fMNU45PtxdhSL_J2PjLTeS8rwKnJzUR4YNd4w@mail.gmail.com>
<CAPpHfdu8Jh0tySa50Zhkyg6gMvi2pQFdHAMiF+UkZNqsDLwTQw@mail.gmail.com>
<475514.1612745257@sss.pgh.pa.us>
<477102.1612747200@sss.pgh.pa.us>
I wrote:
> After digging in gcc's release history, it seems they invented
> "-fsanitize=alignment" in GCC 5, so we can make this work for gcc
> by writing
> #if __GNUC__ >= 5
> (the likely() macro already uses a similar approach). Can't say
> if that's close enough for clang too.
Ugh, no it isn't: even pretty recent clang releases only define
__GNUC__ as 4. It looks like we need a separate test on clang's
version. I looked at their version history and sanitizers seem
to have come in around clang 7, so I propose the attached (where
I worked a bit harder on the comment, too).
regards, tom lane
Attachments:pg_attribute_no_sanitize_alignment-2.patch (text/x-diff, 1.2K) downloaddiff --git a/src/include/c.h b/src/include/c.hindex ae978830da..a86342093e 100644--- a/src/include/c.h+++ b/src/include/c.h@@ -132,6 +132,18 @@
#define pg_nodiscard
#endif
+/*+ * Place this macro before functions that should be allowed to make misaligned+ * accesses. Think twice before using it on non-x86-specific code!+ * Testing can be done with "-fsanitize=alignment -fsanitize-trap=alignment"+ * on clang, or "-fsanitize=alignment -fno-sanitize-recover=alignment" on gcc.+ */+#if __clang_major__ >= 7 || __GNUC__ >= 5+#define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))+#else+#define pg_attribute_no_sanitize_alignment()+#endif+
/*
* Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
* used in assert-enabled builds, to avoid compiler warnings about unused
diff --git a/src/port/pg_crc32c_sse42.c b/src/port/pg_crc32c_sse42.cindex 3b94a7388a..10fc01e1f0 100644--- a/src/port/pg_crc32c_sse42.c+++ b/src/port/pg_crc32c_sse42.c@@ -18,6 +18,7 @@
#include "port/pg_crc32c.h"
+pg_attribute_no_sanitize_alignment()
pg_crc32c
pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len)
{
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us, aekorotkov@gmail.com, hlinnaka@iki.fi, akorotkov@postgresql.org, pgsql-hackers@lists.postgresql.org
Subject: Re: Detecting pointer misalignment (was Re: pgsql: Implementation of subscripting for jsonb)
In-Reply-To: <628089.1612802981@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox