Re: Data race in interfaces/libpq/fe-exec.c

From: Mark Charsley <mcharsley(at)google(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Data race in interfaces/libpq/fe-exec.c
Date: 2020-01-31 10:47:24
Message-ID: CAAf4L0cUWq+qD2GCc5BxsYzVx9b5GW_d9PT3mA1_AZV+Mws63w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

According to folks significantly cleverer than me, this can be a problem:
See section 2.4 in
https://www.usenix.org/legacy/events/hotpar11/tech/final_files/Boehm.pdf

tl;dr in a self-fulfilling prophecy kind of way, there are no benign
data-races. So the compiler can assume no-one would write a data race.
Therefore it can make aggressive optimisations that render what would
otherwise have been a benign race actively dangerous.

Granted the danger here is mainly theoretical, and the main problem for me
is that turning off ThreadSanitizer because of this issue means that other
more dangerous issues in my code (rather than the postgres client code)
won't be found. But the above is the reason why ThreadSanitizer folks don't
want to put in any "you can ignore this race, it's benign" functionality,
and told me that the right thing to do was to contact you folks and get a
fix in upstream...

Mark

On Thu, Jan 30, 2020 at 4:46 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Mark Charsley <mcharsley(at)google(dot)com> writes:
> > This line
> >
> https://github.com/postgres/postgres/blob/30012a04a6c8127397a8ab71e160d9c7e7fbe874/src/interfaces/libpq/fe-exec.c#L1073
> > triggers data race errors when run under ThreadSanitizer (*)
>
> > As far as I can tell, the static variable in question is a hack to allow
> a
> > couple of deprecated functions that are already unsafe to use
> > (PQescapeString and PQescapeBytea) to be fractionally less unsafe to use.
>
> Yup.
>
> > Would there be any interest in a patch changing the type of
> > static_client_coding
> > and static_std_strings
> > <
> https://github.com/postgres/postgres/blob/30012a04a6c8127397a8ab71e160d9c7e7fbe874/src/interfaces/libpq/fe-exec.c#L49
> >
> > to
> > some atomic equivalent, so the data race goes away?
>
> I don't see that making those be some other datatype would improve anything
> usefully. (1) On just about every platform known to man, int and bool are
> going to be atomic anyway. (2) The *actual* hazards here, as opposed to
> theoretical ones, are that you're using more than one connection with
> different settings for these values, whereupon it's not clear whether
> those deprecated functions will see the appropriate settings when they're
> used. A different data type won't help that.
>
> In short: this warning you're getting from ThreadSanitizer is entirely
> off-point, so contorting the code to suppress it seems useless.
>
> regards, tom lane
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message vignesh C 2020-01-31 11:13:07 Re: pg_restore crash when there is a failure before all child process is created
Previous Message Bernd Helmle 2020-01-31 10:33:34 Re: [Patch] Make pg_checksums skip foreign tablespace directories