Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l8uiZ-0000Nt-90 for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Feb 2021 00:47:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l8uiY-0003aO-54 for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Feb 2021 00:47:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l8uiX-0003aH-US for pgsql-hackers@lists.postgresql.org; Mon, 08 Feb 2021 00:47:53 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l8uiV-0005qz-Qm; Mon, 08 Feb 2021 00:47:52 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 1180lbTE475515; Sun, 7 Feb 2021 19:47:37 -0500 From: Tom Lane To: Alexander Korotkov cc: Heikki Linnakangas , Alexander Korotkov , pgsql-hackers@lists.postgresql.org Subject: Detecting pointer misalignment (was Re: pgsql: Implementation of subscripting for jsonb) In-reply-to: References: <37bb56a0-391d-759d-771b-9c1d8c15a84b@iki.fi> <3293925.1612163110@sss.pgh.pa.us> Comments: In-reply-to Alexander Korotkov message dated "Mon, 01 Feb 2021 16:00:21 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <475513.1612745257.1@sss.pgh.pa.us> Date: Sun, 07 Feb 2021 19:47:37 -0500 Message-ID: <475514.1612745257@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk [ redirecting to -hackers ] Alexander Korotkov writes: >> BTW, I managed to reproduce the issue by compiling with CFLAGS="-O0 >> -fsanitize=alignment -fsanitize-trap=alignment" and the patch >> attached. >> I can propose the following to catch such issues earlier. We could >> finish (wrap attribute with macro and apply it to other places with >> misalignment access if any) and apply the attached patch and make >> commitfest.cputube.org check patches with CFLAGS="-O0 >> -fsanitize=alignment -fsanitize-trap=alignment". What do you think? > The revised patch is attached. The attribute is wrapped into > pg_attribute_no_sanitize_alignment() macro. I've checked it works for > me with gcc-10 and clang-11. I found some time to experiment with this today. It is really nice to be able to detect these problems without using obsolete hardware. However, I have a few issues: * Why do you recommend -O0? Seems to me we want to test the code as we'd normally use it, ie typically -O2. * -fsanitize-trap=alignment seems to be a clang-ism; gcc won't take it. However, after some experimenting I found that "-fno-sanitize-recover=all" (or "-fno-sanitize-recover=alignment" if you prefer) produces roughly equivalent results on gcc. * Both clang and gcc seem to be happy with the same spelling of the function attribute, which is fortunate. However, I seriously doubt that bare "#ifdef __GNUC__" is going to be good enough. At the very least there's going to need to be a compiler version test in there, and we might end up needing to get the configure script involved. * I think the right place to run such a check is in some buildfarm animals. The cfbot only sees portions of what goes into our tree. regards, tom lane