From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by sourceware.org (Postfix) with ESMTPS id 513F33858437 for ; Thu, 23 Nov 2023 12:14:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 513F33858437 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 513F33858437 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700741648; cv=none; b=hbkoYx5fhoIeSACBvmZs9Gd6FvnouZr2JyA1iolXXKK4OoTuXZm51SXmIKO2Jr+4mP0T2qWJBZPubVHD3LLYMSgjeWM6zjwQS8cSVoUitHM+0rtvFtm4iyhq94ALyN5L+ZCxGNfL6rMmBcewR+rLpupbS0dYAyAvliqudCymZxo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700741648; c=relaxed/simple; bh=XP9phu/GELwKVKUB5PrOZSM23C7Q6c1lSeXqPklde28=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=WHNOrMnbip8sfI0rU7d4lE20k56s+XCTvsDfcxiUhNSlMk0aQ/AK6AyvwNtKaHuySHclGaz9FhKLu70rqXNlD2B5zAfVAhDyBVO7tEpqDw2w0d6CTgLtVmDwmtrgMPUG0bflhBSgw2SIbra2EUYYl9QkB9GHXh3cLDR2M9CdQEM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3b52360cdf0so434672b6e.2 for ; Thu, 23 Nov 2023 04:14:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1700741645; x=1701346445; darn=sourceware.org; h=content-transfer-encoding:cc:organization:subject:from:to :content-language:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=ZttPpev91ciujj3cFtrl6HZYBRKBpEyGrmRKzPWqL/s=; b=CqCFMdutilPwTwgV4CCaN/I/+604XlvCsF2wlanSGsFsb0q9EAUTrdU88E3cPpoJ12 Mf3GpTMWhmmSkJPfdZK3mZDDFs4UnarZsC+qIy+BfTsukxPeIi9lStDjlsoPDRDHW3bh PoIu6a+L38Uy4xGqHVYExsMeoQqQQl3HXXMYwQBiaVUZhZvmq9S7tqAPfL4JaCyMVRUh LbzWTFf/79u4071ILpwct3nVtdkO1PD8RFaVQ6lBwgvXhxZhFil0yWQ4XMsHdLZrdztB Xtu/dGM6omDcM/xKx3gF7JN2ubKH2CPUKoCyXZkD5L/prX+ZQuL3lPjhJmQljU9g2JDU B/dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700741645; x=1701346445; h=content-transfer-encoding:cc:organization:subject:from:to :content-language:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZttPpev91ciujj3cFtrl6HZYBRKBpEyGrmRKzPWqL/s=; b=MgYEEDqX43lRgYpF7qaeLac2QowzULSx/Ib2WMEw3fYZuKs0U24Ynn39X1l+Fsv+ON wWdoBb2k8kaaTOVGRsM4H8PJy729xjPHuk5Bjl8yprrU6YW+cMomJJLCubzEiegTEdrJ VP+W9c/6F03GUykGzTs9hBsxSC328PEIQ9k1Gk85QNq0Uq/jBcA53uduoqqOZsHb7ESr 9HYZRXJo2fw7n8yv2X3nHXrnFn8URe3+/l/9GCkj5r+pxppbBjEEpGIMXlLssOJ3c/Bx ea+S5FBRiPoi0rTYoCw0hCFQ7hznBiqZzv5GOwAnnDvlPmiAEdi1lNOoDmglaStuedBg LrDA== X-Gm-Message-State: AOJu0YwbrTZfTXXwUDn+zph+vOpgIAzGJYVAeN5EoxmBwOr9PGbPAXYi r+xPot0IbScs+4XNs4gmL1iCR9ba/ewW3qBWMSQAgQ== X-Google-Smtp-Source: AGHT+IE880sy4Smg1l/WZdE8hJieR92me+frLvYo6tnRs7W95SMfMuZe30qNY4poXAWA5orC7T1VEA== X-Received: by 2002:a05:6808:3db:b0:3a9:b9eb:998e with SMTP id o27-20020a05680803db00b003a9b9eb998emr5222567oie.51.1700741644837; Thu, 23 Nov 2023 04:14:04 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c2:94e:bdbe:c355:6ac:193b? ([2804:1b3:a7c2:94e:bdbe:c355:6ac:193b]) by smtp.gmail.com with ESMTPSA id t22-20020a634616000000b005c215baacc1sm1212596pga.70.2023.11.23.04.14.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 Nov 2023 04:14:03 -0800 (PST) Message-ID: <1e8beece-f865-4309-a28f-6782135e2a8a@linaro.org> Date: Thu, 23 Nov 2023 09:14:00 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: libc-alpha From: Adhemerval Zanella Netto Subject: Support for memcpy with equal source and destination Organization: Linaro Cc: post+sourceware.org@ralfj.de Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: The BZ#31055 [1] requests a guarantee that memcpy with equal source and destination is well defined on glibc, since both gcc and clang already emits code with this assumption [2] [3]. There is a WIP document [4] to proper extend this requirement for C standard, with some extra requirement (such as allowing NULL inputs). The GCC bug report [5] have further information on why GCC developers thing this is a reasonable assumption, most related to the codgen cost of adding either the extra compare to correctly call mempcy or just change the call to memmmove. >From glibc standpoint, as far I could check there is no implementation that prevents it (although I am not sure for all of them for all input sizes). The memcpy with equal source and destination will still triggers compiler warnings due the restrict issues, but it should not matter to compiler auto-generated libcalls. Another solution would be to provide an alternative symbol, similar to __memcmpeq, meant to be used by the compiler to libcall optimizations. However, __memcmpeq did not have any adoption so far [6] [7]. If we adopt this constraint, I think it would require to add some testing besides the manual documentation. Some arch maintainers would also like to check their implementation to add an early bailout optimization. I also presume that the fortify builtins already handle it correctly. I am not sure about other libc implementation, at least Musl seems unlikely to provide such guarantee. Thoughts? [1] https://sourceware.org/bugzilla/show_bug.cgi?id=31055 [2] https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=7758cb4b53e8a33642709402ce582f769eb9fd18;hp=6ce952188ab39e303e4f63e474b5cba83b5b12fd [3] https://reviews.llvm.org/D86993 [4] https://docs.google.com/document/d/1guH_HgibKrX7t9JfKGfWX2UCPyZOTLsnRfR6UleD1F8/edit [5] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=32667 [6] https://gcc.gnu.org/pipermail/gcc-patches/2022-June/596881.html [7] https://reviews.llvm.org/D127461