[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: pkgtools/digest sha256 aliasing bug with gcc 11
On Tue, 8 Feb 2022 at 00:51:39 -0800, Michael Forney wrote:
I recently rebuilt my pkgtools/digest package for the blake2s update,
and noticed that sha256 sums are now incorrect. It turns out that
this is because I had updated gcc to 11.2 in the mean time, which
appears to be stricter about aliasing violations and is completely
eliminating the code that stores the bit length into the buffer
(see https://godbolt.org/z/3doTrbse9 for a minimal reproducer).
I'm not sure if this is a regression in gcc or an intended code
transformation, but regardless, I think we should make it use memcpy
instead to avoid this issue. There are still a number of questionable
casts in this file, but the others don't seem to cause a problem
Does this patch look good to apply?
I can't comment on the compiler internals aspect of this, but it seems
reasonable to me, and I can confirm it fixes the issue for me, which I
reproduced and tested the patch with GCC 11.2.1 on Fedora 35.
-- >8 --
Subject: [PATCH] digest: fix aliasing bug with gcc 11
gcc 11 with -O2 optimizes away the store of the bit length into the
last 8 bytes of the context buffer due to an aliasing violation
(stored through uint64_t, retrieved through uint32_t).
To fix this, import the NetBSD patch from christos which makes
it use memcpy instead.
pkgtools/digest/files/configure | 4 ++--
pkgtools/digest/files/sha2.c | 3 ++-
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/pkgtools/digest/files/configure
index f833c50c7067..a10ad8606bd3 100755
@@ -580,8 +580,8 @@ MAKEFLAGS=
# Identity of this package.
diff --git a/pkgtools/digest/files/sha2.c
index 972a24fbbd36..f2cc98487191 100644
@@ -548,7 +548,8 @@ void SHA256_Final(sha2_byte digest, SHA256_CTX*
*context->buffer = 0x80;
/* Set the bit count: */
- *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] =
+ &context->bitcount, sizeof(context->bitcount));
/* Final transform: */
Main Index |
Thread Index |