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 1lxmP2-0006W9-Jc for pgsql-hackers@arkaria.postgresql.org; Mon, 28 Jun 2021 08:14:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lxmOV-0003t0-W4 for pgsql-hackers@arkaria.postgresql.org; Mon, 28 Jun 2021 08:13:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lxmOV-0003ss-OS for pgsql-hackers@lists.postgresql.org; Mon, 28 Jun 2021 08:13:27 +0000 Received: from sfr-1.cri.mines-paristech.fr ([77.158.180.225] helo=smtp.cri.ensmp.fr) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lxmOP-00028r-U7 for pgsql-hackers@lists.postgresql.org; Mon, 28 Jun 2021 08:13:27 +0000 Received: from pseudo (nat-7.mines-paristech.fr [77.158.173.14]) by smtp.cri.ensmp.fr (Postfix) with ESMTPSA id 57B7EC01B5; Mon, 28 Jun 2021 10:15:18 +0200 (CEST) Date: Mon, 28 Jun 2021 10:13:11 +0200 (CEST) From: Fabien COELHO X-X-Sender: fabien@pseudo To: Tom Lane cc: PostgreSQL Developers Subject: Re: Overflow hazard in pgbench In-Reply-To: <73927.1624815543@sss.pgh.pa.us> Message-ID: References: <73927.1624815543@sss.pgh.pa.us> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2056645051-2073406906-1624868000=:1401908" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --2056645051-2073406906-1624868000=:1401908 Content-Type: text/plain; format=flowed; charset=US-ASCII Hello Tom, > moonjelly just reported an interesting failure [1]. I noticed. I was planning to have a look at it, thanks for digging! > It seems that with the latest bleeding-edge gcc, this code is > misoptimized: > else if (imax - imin < 0 || (imax - imin) + 1 < 0) > { > /* prevent int overflows in random functions */ > pg_log_error("random range is too large"); > return false; > } > > such that the second if-test doesn't fire. Now, according to the C99 > spec this code is broken, because the compiler is allowed to assume > that signed integer overflow doesn't happen, Hmmm, so it is not possible to detect these with standard arithmetic as done by this code. Note that the code was written in 2016, ISTM pre C99 Postgres. I'm unsure about what a C compiler could assume on the previous standard wrt integer arithmetic. > whereupon the second if-block is provably unreachable. Indeed. > The failure still represents a gcc bug, because we're using -fwrapv > which should disable that assumption. Ok, I'll report it. I also see a good point with pgbench tests exercising edge cases. > However, not all compilers have that switch, so it'd be better to code > this in a spec-compliant way. Ok. > I suggest applying the attached in branches that have the required > functions. The latest gcc, recompiled yesterday, is still wrong, as shown by moonjelly current status. The proposed patch does fix the issue in pgbench TAP test. I'd suggest to add unlikely() on all these conditions, as already done elsewhere. See attached version. I confirm that check-world passed with gcc head and its broken -fwrapv. I also recompiled after removing manually -fwrapv: Make check, pgbench TAP tests and check-world all passed. I'm not sure that edge case are well enough tested in postgres to be sure that all is ok just from these runs though. -- Fabien. --2056645051-2073406906-1624868000=:1401908 Content-Type: text/x-diff; name=avoid-pgbench-overflow-hazard-2.patch Content-Transfer-Encoding: BASE64 Content-ID: Content-Description: Content-Disposition: attachment; filename=avoid-pgbench-overflow-hazard-2.patch ZGlmZiAtLWdpdCBhL3NyYy9iaW4vcGdiZW5jaC9wZ2JlbmNoLmMgYi9zcmMv YmluL3BnYmVuY2gvcGdiZW5jaC5jDQppbmRleCBlNjEwNTViNmI3Li44ZDBj YWJkYWIyIDEwMDY0NA0KLS0tIGEvc3JjL2Jpbi9wZ2JlbmNoL3BnYmVuY2gu Yw0KKysrIGIvc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