diff mbox series

Makefile: fix unaligned loads in sha1dc with UBSan

Message ID 20190312210626.GA5157@sigill.intra.peff.net (mailing list archive)
State New, archived
Headers show
Series Makefile: fix unaligned loads in sha1dc with UBSan | expand

Commit Message

Jeff King March 12, 2019, 9:06 p.m. UTC
On Sun, Mar 10, 2019 at 11:37:55PM -0400, Jeff King wrote:

> Unfortunately, I don't think sha1dc currently supports #defines in that
> direction. The only logic is "if we are on intel, do unaligned loads"
> and "even if we are not on intel, do it anyway". There is no "even if we
> are on intel, do not do unaligned loads".
> 
> I think you'd need something like this:
> [...]

The sha1dc folks gave us a very nice and quick turnaround on this.
Thanks to them, and to Jeffrey for opening an issue there.

Here's a commit which updates Git to use the new feature. I've tested it
with both the in-tree and submodule builds like:

  make DC_SHA1_SUBMODULE=Yes SANITIZE=undefined && (cd t && ./t0001-*)
  make DC_SHA1_SUBMODULE=    SANITIZE=undefined && (cd t && ./t0001-*)

both of which fail without this patch and succeed without it.

-- >8 --
Subject: [PATCH] Makefile: fix unaligned loads in sha1dc with UBSan

The sha1dc library uses unaligned loads on platforms that support them.
This is normally what you'd want for performance, but it does cause
UBSan to complain when we compile with SANITIZE=undefined. Just like we
set -DNO_UNALIGNED_LOADS for our own code in that case, we should set
-DSHA1DC_FORCE_ALIGNED_ACCESS.

Of course that does nothing without pulling in the patches from sha1dc
to respect that define. So let's do that, too, updating both the
submodule link and our in-tree copy (from the same commit).

Signed-off-by: Jeff King <peff@peff.net>
---
 Makefile               | 1 +
 sha1collisiondetection | 2 +-
 sha1dc/sha1.c          | 5 +++--
 3 files changed, 5 insertions(+), 3 deletions(-)

Comments

Ævar Arnfjörð Bjarmason March 12, 2019, 9:17 p.m. UTC | #1
On Tue, Mar 12 2019, Jeff King wrote:

> On Sun, Mar 10, 2019 at 11:37:55PM -0400, Jeff King wrote:
>
>> Unfortunately, I don't think sha1dc currently supports #defines in that
>> direction. The only logic is "if we are on intel, do unaligned loads"
>> and "even if we are not on intel, do it anyway". There is no "even if we
>> are on intel, do not do unaligned loads".
>>
>> I think you'd need something like this:
>> [...]
>
> The sha1dc folks gave us a very nice and quick turnaround on this.
> Thanks to them, and to Jeffrey for opening an issue there.

Thanks. Good to have it resolved this way.

> Here's a commit which updates Git to use the new feature. I've tested it
> with both the in-tree and submodule builds like:
>
>   make DC_SHA1_SUBMODULE=Yes SANITIZE=undefined && (cd t && ./t0001-*)
>   make DC_SHA1_SUBMODULE=    SANITIZE=undefined && (cd t && ./t0001-*)
>
> both of which fail without this patch and succeed without it.

FWIW I've reproduced this testing and found the same thing. Looks good
to me.
Jeff King March 12, 2019, 9:19 p.m. UTC | #2
On Tue, Mar 12, 2019 at 10:17:56PM +0100, Ævar Arnfjörð Bjarmason wrote:

> > Here's a commit which updates Git to use the new feature. I've tested it
> > with both the in-tree and submodule builds like:
> >
> >   make DC_SHA1_SUBMODULE=Yes SANITIZE=undefined && (cd t && ./t0001-*)
> >   make DC_SHA1_SUBMODULE=    SANITIZE=undefined && (cd t && ./t0001-*)
> >
> > both of which fail without this patch and succeed without it.
> 
> FWIW I've reproduced this testing and found the same thing. Looks good
> to me.

Er, that second "without" should be "with", but hopefully you figured
that out during your testing. :)

-Peff
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 537493822b..593c2c729a 100644
--- a/Makefile
+++ b/Makefile
@@ -1195,6 +1195,7 @@  BASIC_CFLAGS += -fsanitize=$(SANITIZE) -fno-sanitize-recover=$(SANITIZE)
 BASIC_CFLAGS += -fno-omit-frame-pointer
 ifneq ($(filter undefined,$(SANITIZERS)),)
 BASIC_CFLAGS += -DNO_UNALIGNED_LOADS
+BASIC_CFLAGS += -DSHA1DC_FORCE_ALIGNED_ACCESS
 endif
 ifneq ($(filter leak,$(SANITIZERS)),)
 BASIC_CFLAGS += -DSUPPRESS_ANNOTATED_LEAKS
diff --git a/sha1collisiondetection b/sha1collisiondetection
index 232357eb2e..16033998da 160000
--- a/sha1collisiondetection
+++ b/sha1collisiondetection
@@ -1 +1 @@ 
-Subproject commit 232357eb2ea0397388254a4b188333a227bf5b10
+Subproject commit 16033998da4b273aebd92c84b1e1b12e4aaf7009
diff --git a/sha1dc/sha1.c b/sha1dc/sha1.c
index df0630bc6d..5931cf25d5 100644
--- a/sha1dc/sha1.c
+++ b/sha1dc/sha1.c
@@ -124,10 +124,11 @@ 
 #endif
 /*ENDIANNESS SELECTION*/
 
+#ifndef SHA1DC_FORCE_ALIGNED_ACCESS
 #if defined(SHA1DC_FORCE_UNALIGNED_ACCESS) || defined(SHA1DC_ON_INTEL_LIKE_PROCESSOR)
 #define SHA1DC_ALLOW_UNALIGNED_ACCESS
-#endif /*UNALIGNMENT DETECTION*/
-
+#endif /*UNALIGNED ACCESS DETECTION*/
+#endif /*FORCE ALIGNED ACCESS*/
 
 #define rotate_right(x,n) (((x)>>(n))|((x)<<(32-(n))))
 #define rotate_left(x,n)  (((x)<<(n))|((x)>>(32-(n))))