From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by sourceware.org (Postfix) with ESMTPS id 3DDC53844053 for ; Tue, 21 Feb 2023 12:24:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3DDC53844053 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-172334d5c8aso1266499fac.8 for ; Tue, 21 Feb 2023 04:24:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=5j2oqlbWnRD/WwZbHKZfWBrW3QdhjKTPE3swxVQwWq8=; b=NFviZkM3oWleriUn63qUY0qqZFm3Tbjjx05xUfEzVLHetNgt8d8vCq1k1NSuXNeJBb zYf4FKEJZyUgCYO/FWOgHxrlXYoqd8ENelwOPLkNWEmLHXO0Yxdgc4xWwyFi//eOapju cYxnwrWmGAy0NS2RNsLiGNJ9pzwlcpdDCibsNGLirGKdGl9isd2QS2FLgCG92yZus2DB jIgZutCVqmhThtZ8NwUxSGJTfFOGwqwY0zRomX+gYKx0xQ1VGqjsQiym8YgDO56Qjkxi zyTOtcq0EavVsO2SGjTMI4jmQNPcvIu53ApsSO4FZRBUZn6Uwy9Sbm2WVqUrNa0aVE22 g4tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5j2oqlbWnRD/WwZbHKZfWBrW3QdhjKTPE3swxVQwWq8=; b=elOhQwmI0m/3tEDKhVpgQKlvh8m1qI6ZDRf3Rvqd2AEt+1yvAzdI8h8rQAOa0XdeBI WMaPbomSdYs0n5Z41K/Xi7wa7POf0suK6FyQdS55ApGwxMDSCmeKSno0676k+EUvYcAB iwOB/ALQnxVbDx8MRGznEwkyrncu8G1+kvbGo5HuG7n2I4p6AR4OjQC5vrHQp1CH0ziF LiH2s0ZkhztFI8zvIclA/YL2JX7dxQh7mkuibdgmzrJJCTq0vbB1oIZU7NO4uQuxGbXL 1Cl7UnPAhdnbj0zhP7owWy9oY5VGxXKCpMUI9Ddowf9oRXmIhyrIQ1FcrRafW3cBnrSE 35kQ== X-Gm-Message-State: AO0yUKXs7TEFWDUVnX0Y92+3qnTqO2w2MxP6s7d+NiiPrdqYblwgpUP2 o7BvbZx3/ArhCoJIvPMf3QO79dRHFsKjIK6joCQ= X-Google-Smtp-Source: AK7set+uJyf9LvlyIpmZOCQdo7YmvpluwSRDXI8LxCn4VataNPtbanDFkSL/K0oXbqDCCpxNcyAaNA== X-Received: by 2002:a05:6871:60c:b0:15f:e044:23b2 with SMTP id w12-20020a056871060c00b0015fe04423b2mr8007871oan.7.1676982244432; Tue, 21 Feb 2023 04:24:04 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c3:3a5:a0:9596:f951:7d30? ([2804:1b3:a7c3:3a5:a0:9596:f951:7d30]) by smtp.gmail.com with ESMTPSA id e15-20020a4a91cf000000b004f9cd1e42d3sm1018226ooh.26.2023.02.21.04.24.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Feb 2023 04:24:03 -0800 (PST) Message-ID: <3ee7f4ab-3317-a0e7-99e1-1c6d2deae312@linaro.org> Date: Tue, 21 Feb 2023 09:24:00 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.7.2 Subject: Re: [PATCH v12 08/31] string: Improve generic strncmp Content-Language: en-US To: Szabolcs Nagy Cc: libc-alpha@sourceware.org, Richard Henderson , Jeff Law , Xi Ruoyao , Noah Goldstein References: <20230202181149.2181553-1-adhemerval.zanella@linaro.org> <20230202181149.2181553-9-adhemerval.zanella@linaro.org> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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: On 21/02/23 06:28, Szabolcs Nagy wrote: > The 02/02/2023 15:11, Adhemerval Zanella via Libc-alpha wrote: >> It follows the strategy: >> >> - Align the first input to word boundary using byte operations. >> >> - If second input is also word aligned, read a word per time, check >> for null (using has_zero), and check final words using byte >> operation. >> >> - If second input is not word aligned, loop by aligning the source, >> and merge the result of two reads. Similar to aligned case, check >> for null with has_zero, and check final words using byte operation. >> >> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu, >> and powerpc-linux-gnu by removing the arch-specific assembly >> implementation and disabling multi-arch (it covers both LE and BE >> for 64 and 32 bits). > > on arm i see > > FAIL: crypt/badsalttest > > Program received signal SIGSEGV, Segmentation fault. > strncmp_unaligned_loop (n=3, ofs=, w1=2371876, x2=0xf7feb000, x1=0xf7f6563c) at strncmp.c:85 > 85 w2b = *x2++; > > this strncmp does out of bounds read: > > Breakpoint 2, __GI_strncmp (p1=0xf7f65638 "$1$", p2=p2@entry=0xf7feafff "*", n=n@entry=3) at strncmp.c:115 > > 0xf7feb000 is mapped PROT_NONE. > >> +strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs, >> + size_t n) >> +{ >> + op_t w2a = *x2++; >> + uintptr_t sh_1 = ofs * CHAR_BIT; >> + uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1; >> + >> + op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2); >> + if (!has_zero (w2) && n > (sizeof (op_t) - ofs)) >> { >> - c1 = (unsigned char) *s1++; >> - c2 = (unsigned char) *s2++; >> - if (c1 == '\0' || c1 != c2) >> - return c1 - c2; >> - n--; >> + op_t w2b; >> + >> + /* Unaligned loop. The invariant is that W2B, which is "ahead" of W1, >> + does not contain end-of-string. Therefore it is safe (and necessary) >> + to read another word from each while we do not have a difference. */ >> + while (1) >> + { >> + w2b = *x2++; > ^^^^^^^^^^^^^^^^^^^^^^ > > reading ahead is wrong if w1 and w2 already mismatches. Right, I will take a look. > >> + w2 = MERGE (w2a, sh_1, w2b, sh_2); >> + if (n <= sizeof (op_t) || w1 != w2) >> + return final_cmp (w1, w2, n); >> + n -= sizeof(op_t); >> + if (has_zero (w2b) || n <= (sizeof (op_t) - ofs)) >> + break; >> + w1 = *x1++; >> + w2a = w2b; >> + } >> + >> + /* Zero found in the second partial of P2. If we had EOS in the aligned >> + word, we have equality. */ >> + if (has_zero (w1)) >> + return 0; >> + >> + /* Load the final word of P1 and align the final partial of P2. */ >> + w1 = *x1++; >> + w2 = MERGE (w2b, sh_1, 0, sh_2); >> } >> >> - return c1 - c2; >> + return final_cmp (w1, w2, n);