From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4F0783858D32; Sun, 8 Oct 2023 00:19:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F0783858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1696724395; bh=N/Wn4vFVZwxoNyfZ1MEQ6+//B340jn36joOBdFHPVRo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=NIIxTMftDtr2uerbeGKPCLjWr9UEwEflSDc0stG6pnX5pDHogqwoRQ+ffayRFcVuw 3XG5AerFRkedjwhoq7I1iim8/wby+MsOYbIUri2vsz9HoSMIsQ9cK40tpgz+2BCU7b ypaPU3CsTHT6pZzuT6sXVzT0db+m2Oi8uPGF6f7Y= From: "zfigura at codeweavers dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/111722] manually defined memcpy() and memmove() incorrectly handle overlap with -O2 -m32 -march=bdver2 Date: Sun, 08 Oct 2023 00:19:54 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 13.2.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: zfigura at codeweavers dot com X-Bugzilla-Status: WAITING X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: version keywords cf_gcctarget component short_desc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D111722 Zeb Figura changed: What |Removed |Added ---------------------------------------------------------------------------- Version|unknown |13.2.0 Keywords| |wrong-code Target| |i686-linux-gnu Component|c |target Summary|gcc generates wrong code |manually defined memcpy() |with |and memmove() incorrectly | |handle overlap with -O2 | |-m32 -march=3Dbdver2 --- Comment #2 from Zeb Figura --- Really sorry about that, I managed to accidentally hit the Enter key halfway through writing the title. Here is the actual bug description: -- Wine provides freestanding libraries, including manual definitions of memcp= y() and memmove() [1]. Those are defined in C, and while our definitions are *technically* non-compliant C (violating the requirement that the pointers must point to = the same object), they should be fine for our targets, and anyway, the case I'm running into is failure to handle overlap where the pointers *do* in fact p= oint into the same object. I can't find fault with the definitions themselves, although I may be missing something. We also, contrary to standards, give memcpy() the semantics of memmove(), because some Windows programs are buggy and make that assumption. We do thi= s by copy-pasting the definition (I'm not sure why we do this rather than just calling one function from the other, but it is what it is). I recently started compiling with -march=3Dnative, and found that gcc was f= ailing to correctly handle overlap in memmove. Further investigation revealed that, somehow, memmove() was being incorrectly optimized to *not* check for overl= ap, while memcpy() remained in its unoptimized form. I ran into this originally with the i686-w64-mingw32 target, but I've adjus= ted the target to i686-linux-gnu since it happens there too. It does *not* happ= en on x86_64. [1] https://source.winehq.org/git/wine.git/blob/HEAD:/dlls/ntdll/string.c#l= 98=