git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Martin Ågren" <martin.agren@gmail.com>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: Andreas Schwab <schwab@suse.de>,
	Junio C Hamano <gitster@pobox.com>,
	Lars Schneider <larsxschneider@gmail.com>,
	Git Mailing List <git@vger.kernel.org>,
	Marc Stevens <marc@marc-stevens.nl>
Subject: Re: Unaligned accesses in sha1dc
Date: Thu, 1 Jun 2017 13:53:00 +0200	[thread overview]
Message-ID: <CAN0heSrZcW3b6Osa8XNs0ghg2RE0ZS6FdPq8oPpwLcJjXAtLHg@mail.gmail.com> (raw)
In-Reply-To: <CACBZZX6H9EZTLVnunoH2641fw6QmQL=hO9isinK07-dHnuxyFQ@mail.gmail.com>

On 1 June 2017 at 12:33, Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
> On Thu, Jun 1, 2017 at 12:26 PM, Martin Ågren <martin.agren@gmail.com> wrote:
>> On 1 June 2017 at 12:08, Andreas Schwab <schwab@suse.de> wrote:
>>> Even if the architecture implements unaligned accesses in hardware, it
>>> is still undefined behaviour, and the compiler will (eventually) take
>>> advantage of it.
>>
>> I tried to optically follow the macros and ended up on line 87/89 in
>> lib/sha1.c of the sha1dc-library, where there is undefined behavior if
>> the address is unaligned, which it seems it could be. Maybe Git uses
>> some particular combination of macro-definitions and I went down the
>> wrong path... There might also be other spots; I haven't thrown UBSan
>> at the code.
>>
>> Using memcpy on those lines should not be a performance problem on
>> platforms where unaligned access is ok, of course assuming the
>> compiler sees the opportunity.
>
> This is what the upstream version of sha1dc now in the next branch
> does, i.e. just does a memcpy() on platforms which aren't on a
> whitelist of CPUs that allow unaligned access.

Ok, now I get it. Undefined behavior can occur on line 1772 in
sha1dc/sha1.c on "next", but only if SHA1DC_ALLOW_UNALIGNED_ACCESS is
defined. I don't think the macro does what its name suggests, though.
To me, it behaves more like
SHA1DC_RELY_ON_UNDEFINED_BEHAVIOR_TO_DO_THE_RIGHT_THING_BY_CHANCE...

So it seems the call chain of commands and macros could be redesigned
to work with "char*" instead of "uint32_t*"... Then the lines I
mentioned earlier could be converted to memcpy and "should" be
compiled to efficient loads where possible. Yes, I know, patches speak
for themselves, and no, this mail is not a patch.

  reply	other threads:[~2017-06-01 11:53 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-01  8:28 Unaligned accesses in sha1dc Andreas Schwab
     [not found] ` <CDB32E2C-48AF-4636-B921-4C45B614FD35@marc-stevens.nl>
2017-06-01  9:05   ` Andreas Schwab
2017-06-01  9:15 ` Junio C Hamano
2017-06-01  9:15   ` Andreas Schwab
2017-06-01  9:34     ` Junio C Hamano
2017-06-01  9:18 ` brian m. carlson
2017-06-01  9:32   ` Andreas Schwab
2017-06-01  9:21 ` Lars Schneider
2017-06-01  9:53   ` Junio C Hamano
2017-06-01 10:08     ` Andreas Schwab
2017-06-01 10:26       ` Martin Ågren
2017-06-01 10:33         ` Ævar Arnfjörð Bjarmason
2017-06-01 11:53           ` Martin Ågren [this message]
2017-06-01 15:57             ` Martin Ågren
2017-06-02  0:15               ` Junio C Hamano
2017-06-02  8:51                 ` Ævar Arnfjörð Bjarmason
2017-06-02  9:49                   ` Martin Ågren
2017-06-02 19:32                     ` Ævar Arnfjörð Bjarmason
2017-06-02 20:11                       ` Martin Ågren
2017-06-02 20:14                         ` Ævar Arnfjörð Bjarmason
2017-06-02 20:25                           ` demerphq
2017-06-02 20:17                       ` demerphq
2017-06-02 20:38                         ` Ævar Arnfjörð Bjarmason
2017-06-02 21:53                         ` Linus Torvalds
2017-06-03  0:13                           ` Junio C Hamano
2017-06-02 14:46                   ` Liam R. Howlett
2017-06-02 16:53                     ` Ævar Arnfjörð Bjarmason
2017-06-03  0:15                       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAN0heSrZcW3b6Osa8XNs0ghg2RE0ZS6FdPq8oPpwLcJjXAtLHg@mail.gmail.com \
    --to=martin.agren@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=larsxschneider@gmail.com \
    --cc=marc@marc-stevens.nl \
    --cc=schwab@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).