pgcrypto/des tests fail on riscv64 due to clang's code generation anomaly

Started by Alexander Lakhinabout 1 month ago5 messageshackers
Jump to latest
#1Alexander Lakhin
exclusion@gmail.com

Hello hackers,

As recent failures from greenfly, e.g. [1], show:
--- /home/gburd/build/HEAD/pgsql/contrib/pgcrypto/expected/crypt-des.out 2026-03-13 18:06:10.669824704 +0000
+++ /home/gburd/build/HEAD/pgsql.build/testrun/pgcrypto/regress/results/crypt-des.out 2026-03-13 18:38:39.830105127 +0000
@@ -4,13 +4,13 @@
  SELECT crypt('', 'NB');
       crypt
  ---------------
- NBPx/38Y48kHg
+ NB3TsPvodZDgs
  (1 row)
adding -march=rv64gcv to CFLAGS makes clang generate different (wrong?)
code for crypt-des.c. Namely, having reproduced the diff locally, with
this debugging addition:
--- a/contrib/pgcrypto/crypt-des.c
+++ b/contrib/pgcrypto/crypt-des.c
@@ -355,6 +355,11 @@ des_init(void)
      for (i = 0; i < 32; i++)
          un_pbox[pbox[i] - 1] = i;
+fprintf(stderr, "!!!des_init| un_pbox:");
+    for (i = 0; i < 32; i++)
+        fprintf(stderr, " %d", un_pbox[i]);
+fprintf(stderr, "\n");
+
      for (b = 0; b < 4; b++)

I can see the following during a normal execution of:
CREATE EXTENSION pgcrypto; SELECT crypt('', 'NB');
     crypt
---------------
 NBPx/38Y48kHg

!!!des_init| un_pbox: 8 16 22 30 12 27 1 17 23 15 29 5 25 19 9 0 7 13 24 2 3 28 10 18 31 11 21 6 4 26 14 20

vs:
     crypt
---------------
 NB3TsPvodZDgs

!!!des_init| un_pbox: 15 6 19 20 28 11 27 16 0 14 22 25 4 17 30 9 1 7 23 13 31 26 2 8 18 12 29 5 21 10 3 24

when the code compiled with CC=clang-20 CPPFLAGS="-march=rv64gcv".

I don't observe this issue with gcc-14, nor with this modification (using
clang-20, -march=rv64gcv):
        for (i = 0; i < 32; i++)
+{
                un_pbox[pbox[i] - 1] = i;
+pg_compiler_barrier();
+}

[1]: https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=greenfly&amp;dt=2026-03-13%2018%3A06%3A09

Best regards,
Alexander

#2Michael Paquier
michael@paquier.xyz
In reply to: Alexander Lakhin (#1)
Re: pgcrypto/des tests fail on riscv64 due to clang's code generation anomaly

On Sun, Mar 15, 2026 at 02:00:00PM +0200, Alexander Lakhin wrote:

adding -march=rv64gcv to CFLAGS makes clang generate different (wrong?)
code for crypt-des.c. Namely, having reproduced the diff locally, with
this debugging addition:

when the code compiled with CC=clang-20 CPPFLAGS="-march=rv64gcv".

Thanks for the detailed investigation. I was puzzled by it myself for
a couple of hours, and did not see something else than a broken
compiler here..
--
Michael

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paquier (#2)
Re: pgcrypto/des tests fail on riscv64 due to clang's code generation anomaly

Michael Paquier <michael@paquier.xyz> writes:

On Sun, Mar 15, 2026 at 02:00:00PM +0200, Alexander Lakhin wrote:

adding -march=rv64gcv to CFLAGS makes clang generate different (wrong?)
code for crypt-des.c. Namely, having reproduced the diff locally, with
this debugging addition:
when the code compiled with CC=clang-20 CPPFLAGS="-march=rv64gcv".

Thanks for the detailed investigation. I was puzzled by it myself for
a couple of hours, and did not see something else than a broken
compiler here..

Seems like this might be worth a bug report to the clang people.

regards, tom lane

#4Greg Burd
greg@burd.me
In reply to: Tom Lane (#3)
Re: pgcrypto/des tests fail on riscv64 due to clang's code generation anomaly

On Sun, Mar 15, 2026, at 9:40 PM, Tom Lane wrote:

Michael Paquier <michael@paquier.xyz> writes:

On Sun, Mar 15, 2026 at 02:00:00PM +0200, Alexander Lakhin wrote:

adding -march=rv64gcv to CFLAGS makes clang generate different (wrong?)
code for crypt-des.c. Namely, having reproduced the diff locally, with
this debugging addition:
when the code compiled with CC=clang-20 CPPFLAGS="-march=rv64gcv".

Thanks for the detailed investigation. I was puzzled by it myself for
a couple of hours, and did not see something else than a broken
compiler here..

Seems like this might be worth a bug report to the clang people.

Thanks Alexander for digging and a solid report of the issue, and Michael for spending some time on this, and Tom for chiming in as well. I agree this looks like a compiler issue. I know that Thomas (added/CC'ed) you've posted bugs against clang before, any thoughts on this one?

regards, tom lane

best.

-greg

#5Alexander Lakhin
exclusion@gmail.com
In reply to: Greg Burd (#4)
Re: pgcrypto/des tests fail on riscv64 due to clang's code generation anomaly

Hello,

16.03.2026 14:13, Greg Burd wrote:

Seems like this might be worth a bug report to the clang people.

Thanks Alexander for digging and a solid report of the issue, and Michael for spending some time on this, and Tom for chiming in as well. I agree this looks like a compiler issue. I know that Thomas (added/CC'ed) you've posted bugs against clang before, any thoughts on this one?

Please find attached the reduced test code, which works for me as follows:
$ clang-20 -O2 -march=rv64gcv crypt-des-test.c -o crypt-des-test && ./crypt-des-test
!!!des_init| un_pbox: 15 6 19 20 28 11 27 16 0 14 22 25 4 17 30 9 1 7 23 13 31 26 2 8 18 12 29 5 21 10 3 24

$ clang-20 -O2 -march=rv64gc crypt-des-test.c -o crypt-des-test && ./crypt-des-test
!!!des_init| un_pbox: 8 16 22 30 12 27 1 17 23 15 29 5 25 19 9 0 7 13 24 2 3 28 10 18 31 11 21 6 4 26 14 20

$ clang-20 -O1 -march=rv64gcv crypt-des-test.c -o crypt-des-test && ./crypt-des-test
!!!des_init| un_pbox: 8 16 22 30 12 27 1 17 23 15 29 5 25 19 9 0 7 13 24 2 3 28 10 18 31 11 21 6 4 26 14 20

$ clang-20 --version
Ubuntu clang version 20.1.2 (0ubuntu1~24.04.2)

Best regards,
Alexander

Attachments:

crypt-des-test.ctext/x-csrc; charset=UTF-8; name=crypt-des-test.cDownload