From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 16512 invoked from network); 16 Jul 2023 21:13:30 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 16 Jul 2023 21:13:30 -0000 Received: (qmail 30634 invoked by uid 550); 16 Jul 2023 21:13:26 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 30599 invoked from network); 16 Jul 2023 21:13:26 -0000 Date: Sun, 16 Jul 2023 23:13:14 +0200 From: Robert Clausecker To: musl@lists.openwall.com Message-ID: References: <20230716174945.qc6234b654k5eebx@gen2.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Subject: Re: [musl] strcmp() guarantees and assumptions Hi Markus, Am Sun, Jul 16, 2023 at 09:33:16PM +0200 schrieb Markus Wichmann: > Am Sun, Jul 16, 2023 at 07:59:57PM +0200 schrieb Robert Clausecker: > > That's good to hear. Any idea on the “what do existing libc > > implementations permit” bit? > > > > So I quickly checked musl, dietlibc, bionic, and glibc, and > unsurprisingly, all of the implementations I looked at allow the strings > to be unterminated if they mismatch before access becomes restricted. > This is, of course, an implementation detail that applications must not > rely on, but it nevertheless is the case. > > The problem in your implementation is that the calls to strlen() will > iterate over both input strings to the end, causing basically a cache > flush for large inputs, only to then iterate over both inputs a second > time. Iterating only once is a major benefit, since it avoids half of > the cache misses. Of course. This was merely a simple example to demonstrate the general point. I of course do not plan to do anything like that. > Also, glibc already has an SSE strcmp implementation you may want to > look at. I'm not going to look at glibc as it's LGPL licensed. I am aware of the Intel implementation, but I don't like that it has to duplicate the code 16 times for each possible misalignment pattern. Without having to ensure that a cacheline of data is only touched once we confirm there is no previous mismatch, it might be possible to write simpler code, but I'm currently not entirely sure how. > Ciao, > Markus Yours, Robert Clausecker -- () ascii ribbon campaign - for an 8-bit clean world /\ - against html email - against proprietary attachments