Re: Data race in interfaces/libpq/fe-exec.c - Mailing list pgsql-hackers

From Mark Charsley
Subject Re: Data race in interfaces/libpq/fe-exec.c
Date
Msg-id CAAf4L0cUWq+qD2GCc5BxsYzVx9b5GW_d9PT3mA1_AZV+Mws63w@mail.gmail.com
Whole thread Raw
In response to Re: Data race in interfaces/libpq/fe-exec.c  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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@sss.pgh.pa.us> wrote:
Mark Charsley <mcharsley@google.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

pgsql-hackers by date:

Previous
From: Bernd Helmle
Date:
Subject: Re: [Patch] Make pg_checksums skip foreign tablespace directories
Next
From: vignesh C
Date:
Subject: Re: pg_restore crash when there is a failure before all child processis created