From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 17B3E385842E; Thu, 5 Jan 2023 09:19:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17B3E385842E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672910393; bh=D+um9nWbI27AitcZrbgVnqbuQTPQYonoW0tIvOSm1ZY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=iiIepMmuJA+m45ohxxMAS5qkSZSNCXKZzAj4NYe9pIfa/2iZ9LSbF0Bxj99Art48l LvBcVCowxgmFq778AR3FL5jS2jTNCUs7KB6IMMs/a0y99/603dl/AEzgg/PH8Zilfn kSfi/qYCuP+lTdFrO7TC9Nmxs/B55LJu7NJSN4TE= From: "nyh at math dot technion.ac.il" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/32667] block copy with exact overlap is expanded as memcpy Date: Thu, 05 Jan 2023 09:19:49 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 4.2.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: nyh at math dot technion.ac.il X-Bugzilla-Status: NEW 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: 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=3D32667 --- Comment #21 from Nadav Har'El --- This old problem has become a real problem in gcc 12 with a real effect on incorrect code generation, where code that copies an object was incorrectly "optimized" to use __builtin_memcpy() instead of __builtin_memmove() even though the source and destination objects may overlap - and it turns out th= at __builtin_memcpy() may produce incorrect results for overlapping addresses. This bug was discovered in the OSv project https://github.com/cloudius-systems/osv/issues/1212 with code that doesn't (obviously) call __builtin_memcpy() directly, but rather had a 27-character type being copied and the compiler implemented this copy with a call to __builtin_memcpy(). Here is an example of code which generates the wrong results (note the missing "c" in the result and unexpectedly doubled "g"): #include int main(){ char buf[128] =3D "0123456789abcdefghijklmnopqrstuvwxyz"; struct [[gnu::packed]] data { char x[27]; }; void *p0 =3D buf; void *p1 =3D &buf[1]; *static_cast(p0) =3D *static_cast(p1); printf("%s", buf); }=