public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug string/31055] New: Request: guarantee that memcpy(x, x, n) is well-defined
@ 2023-11-11 17:37 post+sourceware.org at ralfj dot de
  2023-11-11 18:41 ` [Bug string/31055] " sam at gentoo dot org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: post+sourceware.org at ralfj dot de @ 2023-11-11 17:37 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=31055

            Bug ID: 31055
           Summary: Request: guarantee that memcpy(x, x, n) is
                    well-defined
           Product: glibc
           Version: 2.38
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P2
         Component: string
          Assignee: unassigned at sourceware dot org
          Reporter: post+sourceware.org at ralfj dot de
  Target Milestone: ---

The documentation for memcpy states

> The behavior of this function is undefined if the two arrays to and from overlap

However, this contract is suboptimal for several large consumers of this API,
including GCC (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=32667), clang
(https://reviews.llvm.org/D86993), and rustc
(https://doc.rust-lang.org/nightly/core/index.html#how-to-use-the-core-library).
All of these compilers assume that when both pointers are equal, memcpy is
effectively a NOP.

(I am not a compiler backend engineer, so I cannot fully explain why they
consider this contract insufficient and are amending it unilaterally. But there
seems to be a widespread consensus on the compiler side that adding an extra
conditional to guard against the case of both pointers being equal has
significant cost. Obviously compilers should have tried to work with libc
implementations and not just unilaterally assume a different contract than what
is documented; I cannot speak to the history of how we got here, I would just
like to improve the situation here and now.)

Efforts seem to be underway to change the C standard to make this API contract
more useful for these compilers (https://reviews.llvm.org/D86993#4585590), but
that is a slow process. So in the meantime it would be great if glibc could
guarantee that for the special case where to==from, behavior is indeed
well-defined. (Given that in particular the flagship compiler of the GNU
project itself makes this assumption, I hope this is a reasonable request.)

My assumption is that this only requires a change to the documentation, and
that the implementation is already well-defined for to==from. If, however, the
implementation of glibc on some targets is *not* producing well-defined results
for to==from, then that would also be very interesting to know.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-11-28  7:18 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-11 17:37 [Bug string/31055] New: Request: guarantee that memcpy(x, x, n) is well-defined post+sourceware.org at ralfj dot de
2023-11-11 18:41 ` [Bug string/31055] " sam at gentoo dot org
2023-11-13 18:47 ` adhemerval.zanella at linaro dot org
2023-11-18 17:56 ` post+sourceware.org at ralfj dot de
2023-11-21 15:30 ` adhemerval.zanella at linaro dot org
2023-11-23  7:36 ` post+sourceware.org at ralfj dot de
2023-11-23 11:53 ` adhemerval.zanella at linaro dot org
2023-11-23 14:48 ` sam at gentoo dot org
2023-11-23 15:18 ` post+sourceware.org at ralfj dot de
2023-11-24  7:46 ` rguenth at gcc dot gnu.org
2023-11-24  8:52 ` post+sourceware.org at ralfj dot de
2023-11-24  9:37 ` rguenth at gcc dot gnu.org
2023-11-28  7:18 ` sam at gentoo dot org

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 read-only IMAP folder(s) and NNTP newsgroup(s).