This is the mail archive of the
mailing list for the libstdc++ project.
Re: [PATCH] Use __builtin_memmove for trivially copy assignable types
- From: Jeff Law <law at redhat dot com>
- To: Jonathan Wakely <jwakely at redhat dot com>, Glen Fernandes <glen dot fernandes at gmail dot com>
- Cc: ville dot voutilainen at gmail dot com, gcc-patches at gcc dot gnu dot org, libstdc++ at gcc dot gnu dot org
- Date: Thu, 19 Jul 2018 09:35:52 -0600
- Subject: Re: [PATCH] Use __builtin_memmove for trivially copy assignable types
- References: <CAHVPgz=BsNLPv-Q=uvbF2L8qw1hG3mhJFpU1uudYURddxpxP-Q@mail.gmail.com> <alpine.DEB.email@example.com> <CAFk2RUbAjawC2uAYJig=t9CmZ8DyF3-r-8D8M3YUWBDdrH70QA@mail.gmail.com> <CAHVPgz=j8jVWsK2RcioWTR0b40tyEiiEYDT=fqE7Bfea00oDoQ@mail.gmail.com> <CAHVPgzkOWEpKpS8EDELB2AjKWa=bbZiGs0XOyC-jWAPL_dJevQ@mail.gmail.com> <20180719132522.GK14057@redhat.com> <CAHVPgzkCTsPBL6-_np47E2q1gziXYxEPfEMLP5r4ZA9y4D3Fng@mail.gmail.com> <20180719143949.GL14057@redhat.com>
On 07/19/2018 08:39 AM, Jonathan Wakely wrote:
> On 19/07/18 10:01 -0400, Glen Fernandes wrote:
>> On Thu, Jul 19, 2018 at 9:25 AM Jonathan Wakely <firstname.lastname@example.org>
>>> Sorry for the delay in reviewing this properly, as I've only just
>>> realised that this introduces undefined behaviour, doesn't it?
>>> It's undefined to use memmove for a type that is not trivially
>>> copyable. All trivial types are trivially copyable, so __is_trivial
>>> was too conservative, but safe (IIRC we used it because there was no
>>> __is_trivially_copyable trait at the time, so __is_trivial was the
>>> best we had).
>>> There are types which are trivially assignable but not trivially
>>> copyable, and it's undefined to use memmove for such types.
>> I was still unclear about that, but I forwarded you an e-mail from
>> Marshall with his answer when I asked whether libc++'s use of
>> TriviallyCopyAssignable here was incorrect. Let me know if it applies
>> here, and if not (and that interpretation of the standard is
>> incorrect), I'll update the patch to do as you suggest and run the
>> tests again.
> While I sympathise with Marshall's position (that std::copy only cares
> about assignment not copying) that doesn't make it OK to use memmove
> Using memmove for a non-trivially copyable type is undefined. Period.
> The fact GCC warns that it's undefined also means GCC might start
> optimising based on the assumption that undefined behaviour isn't
> reached at runtime. So it could (for example) assume that the input
> range must be empty and remove the entire call to std::copy.
Right. In fact we have a pass which searches for a very small subset of
undefined behavior (null pointer dereferences, division by zero) and
when it finds them it replaces the offending operation with a trap and
does the obvious CFG cleanups.
While neither of those would affect this specific issue and we're pretty
conservative about adding more cases to this pass, certainly the right
thing to do is avoid undefined behavior :-) So I'm in total agreement
with Jon here.