From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id 632D03858403 for ; Tue, 26 Oct 2021 22:47:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 632D03858403 Received: by mail-pl1-x636.google.com with SMTP id n18so629312plc.2 for ; Tue, 26 Oct 2021 15:47:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=+QO+MZcu+OBIMxYOkuG6z8/jygJhVnUTzj6m+Sv+f6Q=; b=IbqoJiNOrFA0csQOpnGcjirzneUrWk2af9Eannds1dR59dSPZ0ltEKern3xc7K6aUb tZFJ03GHugtVbDkME1gjfv6zZS03SJAy0fRIelGQZJhawwq6DOorVt21KB4iFWCb/soz ki9StKM14fzvwLK834uO8pdomA2uXZi7bdfknDyukunly7eHRR24diR+f1Cim/++iS+F gADB1CE9sf29qFo7puiUW1xpw33JP7z2tifjQVuutIOv4PuVfbdBvmAUW4pRVWwg+Xha LgHNv5sdyciZLUQX4pkV2RrXxusPwvT1bQX/xzHtjVuVAcICjhacnADiLBy9rH4NgLvN RBhw== X-Gm-Message-State: AOAM531z1wb6z5cL+zPQY4GgZ8WnWI72wFlSJAEtWEaOzOE0jXCPJY5D F82/BazgvE7CIvvHlsKPN6iok2XudUcLZFm7qdNORD+W6Zw= X-Google-Smtp-Source: ABdhPJxXWQ51McyIMkkUDmLfTFQm4dMSvJbuqJu42bokTLgohruRL2uACb1DvCvxiiFCNpVvnFqjfuavExl1t99fzzY= X-Received: by 2002:a17:902:f551:b0:13e:fb56:f519 with SMTP id h17-20020a170902f55100b0013efb56f519mr24654825plf.0.1635288447583; Tue, 26 Oct 2021 15:47:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Noah Goldstein Date: Tue, 26 Oct 2021 17:47:16 -0500 Message-ID: Subject: Re: Add new ABI '__memcmpeq()' to libc To: libc-coord@lists.openwall.com Cc: GNU C Library , Richard Biener via Gcc Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Oct 2021 22:47:30 -0000 On Thu, Sep 16, 2021 at 12:02 PM Noah Goldstein wrote: > > Hi All, > > This is a proposal for a new interface to be supported by libc. > > The new interface is the same as the old 'bcmp()' routine. Essentially > the goal of this proposal is to add a reserved namespace for a new > function, '__memcmpeq()', which shares the same behavior as the old > 'bcmp()'. > > #### Interface #### > > int __memcmpeq(void const * s1, const void * s2, size_t n) > > > #### Description #### > > The '__memcmpeq()' function would compare the two byte sequences 's1' > and 's2', each of length 'n'. If the two byte sequences are equal, the > return would be zero. Otherwise it would return some non-zero > value. 'memcmp()' is a valid implementation of '__memcmpeq()'. > > > #### Use Case #### > > 1. The goal is that '__memcmpeq()' will be usable as an optimization > by compilers if a program uses the return value of 'memcmp()' as a > boolean. For example: > > > void foo(const void* s1, const void* s2, size_t n) > { > if (!memcmp(s1, s2, n)) { > printf("memcmp can be optimized to __memcmpeq in this use case\n"); > } > } > > > - In the above case '__memcmpeq()' could be used instead. Due to the > simpler constraints on the return value of '__memcmpeq()', it will > be able to be implemented more optimally for this case than > 'memcmp()'. If there is no separately optimized version of > '__memcmpeq()' can alias 'memcmp()' and thus be at least equally as > fast. > > 2. Possibly use cases in security as the runtime of the function will > be *more* oblivious to the byte sequences being compared. > > > #### Argument Specifications #### > > 1. 's1' > - All 'n' bytes in the byte sequence starting at 's1' and ending > at, but not including, 's1 + n' must be accessible memory. There > are no guarantees about the order the sequence will be > traversed. > 2. 's2' > - All 'n' bytes in the byte sequence starting at 's2' and ending > at, but not including, 's2 + n' must be accessible memory. There > are no guarantees about the order the sequence will be > traversed. > 3. 'n' > - 'n' may be any value that does not violate the specifications on > 's1' and 's2'. > > If any of the argument specifications are violated there are no > guarantees about the behavior of the interface. > > > #### Return Value Specification #### > > If the byte sequences starting at 's1' and 's2' are equals the > function will return zero. Otherwise the function will return a > non-zero value. > > Equality between the byte sequences starting at 's1' and 's2' is > defined as follows: > > 1. If 'n' is zero the two sequences are zero. > 2. If 'n' is non-zero then for all 'i' in range [0, n) the byte at > offset 'i' of 's1' equals the byte at offset 'i' in 's2'. > > For a simple C implementation of '__memcmpeq()' could be as follows: > > > int __memcmpeq(const void* s1, const void* s2, size_t n) > { > int ret; > size_t i; > const char *s1c, *s2c; > s1c = (const char*)s1; > s2c = (const char*)s2; > for (i = 0, ret = 0; ret == 0 && i < n; ++i) { > ret = s1c[i] - s2c[i] > } > return ret; > } > > > #### Notes #### > > This interface is essentially old 'bcmp()' and 'memcmp()' will always > be a valid implementation of '__memcmpeq()'. > > > #### ABI vs API #### > > This proposal is for '__memcmpeq()' as a new ABI. As an ABI > '__memcmpeq()' will have value, as using the return value of > 'memcmp()' is quite idiomatic in C code. > > It is, however, possible that this would also be useful as a new API > as well. Especially if there are likely use cases where the compiler > would be unable to prove that '__memcmpeq()' would be a valid > replacement for 'memcmp()'. > > > #### Further Options #### > > If this proposal is received positively, libc could also add > interfaces for '__streq()', '__strneq()', '__wcseq()' and '__wcsneq()' > which similarly would loosen return value restrictions on 'strcmp()', > 'strncmp()', 'wcscmp()' and 'wcsncmp()' respectively. > > Best, > Noah ABI support for '__memcmpeq' have been pushed for GLIBC with: commit 44829b3ddb64e99e37343a0f25b2c082387d31a5