From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id 105E438582B0; Sun, 12 Feb 2023 01:37:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 105E438582B0 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-x234.google.com with SMTP id v15so7621751oie.9; Sat, 11 Feb 2023 17:37:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=bveE7Qcj0G5lSGIJiBam9HVVntrWLGGC8mOyUPG+gdw=; b=NnMDM68l+ahYUt94UbmbixJKSJchk54yfILDdMvnGohhhNXWMd6adsLOPDW1/SwdZ8 dopZ39rMUjAYlFxEYgueOC8WfdhBAiYjyIAVmbE0Bj0IoBW5zd2Rj29C3RIyKZdVbCmi fH/MU8YUItDFNaH1qICqIMLAZJdCm5/vZYvavcbgWlkADjrf8Iw7jJ4sR5KFXVPqEqGL pzTaQu+Tzw1x16uTw1qmvem44qhKcy+x/ZKXVa54wqdJ29pk/u2HF50g7cLVpNecSabJ tRHwGtWARewraVSgJmMkA4faTORGBd9HqaKEyGaDGHpWcOnrfKjrLJid/8DUNg94Svi0 5gxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bveE7Qcj0G5lSGIJiBam9HVVntrWLGGC8mOyUPG+gdw=; b=1PG9M9E2IP4LCRNXKptkCDWvngV0DBIGgZuwfSIUL305EMKn2EgEgmKwO95UhFmIsT 6nr02TbESmAOyC/TyCz44CEMJPxWTw0Tcjg/hi1re8upZcHjLWCBDgVrXjLfzzJfzxPO TOWJPJcTzRJWlYi9OZbdnKH2FwmtNxC1CQBzcZrAK3nWCdz9s8nbANHjguNdatkpmKDu 4mhreiY3IaGp3cyjwagJ+4Y0l71RerWPGbD+CY8rVzDHXjTkKG9u1zxevi0yvknK2vg+ 7fRIUDLxm9fCxK+DH0IWTgnRkpNjXQFXr3oRbTfhgV2I3kVOZHgw8HBGo2ZKi0BmPt2r YCtg== X-Gm-Message-State: AO0yUKUR8ZdzYyn2AASD6IS/GLMVq4bcWvXJJ7iX6seAWjBD/JwJ1kVZ yOLctLBn/b+UfdFlBfA4pWTnbYrwBwxO+WQm9nc= X-Google-Smtp-Source: AK7set/yy9g4y2/x/XXsTBD3nmjv4tTtlxXknszLiYCAL48eF6hiZ0QmA6Xhvw0l7Ta18080Cnw0DenqquFnhldV9b0= X-Received: by 2002:a05:6808:1316:b0:35b:c6c4:7a33 with SMTP id y22-20020a056808131600b0035bc6c47a33mr1823735oiv.266.1676165834261; Sat, 11 Feb 2023 17:37:14 -0800 (PST) MIME-Version: 1.0 References: <20230131213655.4033602-1-goldstein.w.n@gmail.com> <20230131234656.2175991-1-goldstein.w.n@gmail.com> In-Reply-To: From: "H.J. Lu" Date: Sat, 11 Feb 2023 17:36:38 -0800 Message-ID: Subject: Re: [PATCH v2] x86: Fix strncat-avx2.S reading past length [BZ #30065] To: Sunil Pandey Cc: "Carlos O'Donell" , Libc-stable Mailing List , Noah Goldstein , libc-alpha@sourceware.org, carlos@systemhalted.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3022.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,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 Sat, Feb 11, 2023 at 5:00 PM Sunil Pandey wrote: > > On Tue, Jan 31, 2023 at 7:10 PM Carlos O'Donell via Libc-alpha > wrote: > > > > On 1/31/23 18:46, Noah Goldstein via Libc-alpha wrote: > > > Occurs when `src` has no null-term. > > > > This has been pushed as b2c474f8de4c92bfe7435853a96805ec32d68dfa. > > > > We are now in a hard freeze as I prepare to cut the release. > > > > Please do not commit anything further. > > > > If we find other issues we can backport to the release branch after testing. > > > > I'm re-running testing with this patch included for x86_64 and i686. > > > > > Two cases: > > > > > > 1) Zero-length check is doing: > > > ``` > > > test %rdx, %rdx > > > jl L(zero_len) > > > ``` > > > which doesn't actually check zero (was at some point `decq` and the > > > flag never got updated). > > > > > > The fix is just make the flag `jle` i.e: > > > ``` > > > test %rdx, %rdx > > > jle L(zero_len) > > > ``` > > > > > > 2) Length check in page-cross case checking if we should continue is > > > doing: > > > ``` > > > cmpq %r8, %rdx > > > jb L(page_cross_small) > > > ``` > > > which means we will continue searching for null-term if length ends at > > > the end of a page and there was no null-term in `src`. > > > > > > The fix is to make the flag: > > > ``` > > > cmpq %r8, %rdx > > > jbe L(page_cross_small) > > > ``` > > > --- > > > string/test-strncat.c | 25 ++++++++++++++++++++++++- > > > sysdeps/x86_64/multiarch/strncat-avx2.S | 4 ++-- > > > 2 files changed, 26 insertions(+), 3 deletions(-) > > > > > > diff --git a/string/test-strncat.c b/string/test-strncat.c > > > index e03d329e1c..c0cde206ee 100644 > > > --- a/string/test-strncat.c > > > +++ b/string/test-strncat.c > > > @@ -28,6 +28,7 @@ > > > # define CHAR char > > > # define UCHAR unsigned char > > > # define SIMPLE_STRNCAT simple_strncat > > > +# define STRNLEN strnlen > > > # define STRLEN strlen > > > # define MEMSET memset > > > # define MEMCPY memcpy > > > @@ -40,6 +41,7 @@ > > > # define CHAR wchar_t > > > # define UCHAR wchar_t > > > # define SIMPLE_STRNCAT simple_wcsncat > > > +# define STRNLEN wcsnlen > > > # define STRLEN wcslen > > > # define MEMSET wmemset > > > # define MEMCPY wmemcpy > > > @@ -78,7 +80,7 @@ do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t n) > > > return; > > > } > > > > > > - size_t len = STRLEN (src); > > > + size_t len = STRNLEN (src, n); > > > if (MEMCMP (dst + k, src, len + 1 > n ? n : len + 1) != 0) > > > { > > > error (0, 0, "Incorrect concatenation in function %s", > > > @@ -95,6 +97,26 @@ do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t n) > > > } > > > } > > > > > > +static void > > > +do_test_src_no_nullterm_bz30065 (void) > > > +{ > > > + /* NB: "src does not need to be null-terminated if it contains n or more > > > + * bytes." */ > > > + CHAR *s1, *s2; > > > + size_t bound = page_size / sizeof (CHAR); > > > + s1 = (CHAR *) (buf1 + BUF1PAGES * page_size); > > > + s2 = (CHAR *) buf2; > > > + MEMSET (s1 - bound, -1, bound); > > > + for (size_t n = 0; n < bound; ++n) > > > + { > > > + FOR_EACH_IMPL (impl, 0) > > > + { > > > + s2[0] = '\0'; > > > + do_one_test (impl, s2, s1 - n, n); > > > + } > > > + } > > > +} > > > + > > > static void > > > do_test (size_t align1, size_t align2, size_t len1, size_t len2, > > > size_t n, int max_char) > > > @@ -372,6 +394,7 @@ test_main (void) > > > > > > do_random_tests (); > > > do_overflow_tests (); > > > + do_test_src_no_nullterm_bz30065 (); > > > return ret; > > > } > > > > > > diff --git a/sysdeps/x86_64/multiarch/strncat-avx2.S b/sysdeps/x86_64/multiarch/strncat-avx2.S > > > index b380e8e11c..c2ff202238 100644 > > > --- a/sysdeps/x86_64/multiarch/strncat-avx2.S > > > +++ b/sysdeps/x86_64/multiarch/strncat-avx2.S > > > @@ -66,7 +66,7 @@ ENTRY(STRNCAT) > > > salq $2, %rdx > > > # else > > > test %rdx, %rdx > > > - jl L(zero_len) > > > + jle L(zero_len) > > > # endif > > > vpxor %VZERO_128, %VZERO_128, %VZERO_128 > > > > > > @@ -387,7 +387,7 @@ L(page_cross): > > > subl %esi, %r8d > > > andl $(VEC_SIZE - 1), %r8d > > > cmpq %r8, %rdx > > > - jb L(page_cross_small) > > > + jbe L(page_cross_small) > > > > > > /* Optimizing more aggressively for space as this is very cold > > > code. This saves 2x cache lines. */ > > > > -- > > Cheers, > > Carlos. > > > > I would like to backport this patch to release branches. > Any comments or objections? > > --Sunil OK. Thanks. -- H.J.