Detecting pointer misalignment (was Re: pgsql: Implementation of subscripting for jsonb)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Alexander Korotkov <akorotkov(at)postgresql(dot)org>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Detecting pointer misalignment (was Re: pgsql: Implementation of subscripting for jsonb)
Date: 2021-02-08 00:47:37
Message-ID: 475514.1612745257@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

[ redirecting to -hackers ]

Alexander Korotkov <aekorotkov(at)gmail(dot)com> 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

In response to

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2021-02-08 01:20:00 Re: Detecting pointer misalignment (was Re: pgsql: Implementation of subscripting for jsonb)
Previous Message Tom Lane 2021-02-07 20:47:03 pgsql: Release notes for 13.2, 12.6, 11.11, 10.16, 9.6.21, 9.5.25.

Browse pgsql-hackers by date

  From Date Subject
Next Message Yugo NAGATA 2021-02-08 01:06:46 Re: Is Recovery actually paused?
Previous Message Peter Smith 2021-02-08 00:42:29 Re: Single transaction in the tablesync worker?