From patchwork Thu Aug 15 23:15:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1972984 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=KMa+6E5A; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WlLbH3M1Tz1yYl for ; Fri, 16 Aug 2024 09:16:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 50D393858282 for ; Thu, 15 Aug 2024 23:16:09 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id 2CA9D3858C41 for ; Thu, 15 Aug 2024 23:15:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2CA9D3858C41 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2CA9D3858C41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::633 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723763754; cv=none; b=MxrCFfF0/14Dtq2Vs3L7z1gWLmV15iTt23WyjWhQOPsskgE2E50LplTewmpd891CPgXLecAKp+u5NeO8WoHU0T5uUpoKsa4lPw4Pr+gSLBupY5w5pyMnrtvtWkdzunYM8cW9h+hNbAXt2JuDUiB0odI8NJzNE37o+6Sy3+QCOFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723763754; c=relaxed/simple; bh=nCkAiriIIojlJT0ssb8yiMp+ZxYXeC3iRBJlUgNefP4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=eAv4MuuKvHaI0ahwCRpSDCM9rRfFKDcMHFoSpjpwqIYvQGX/vNw9iJnnEkgcjfUKg4QxuOvhG8EGGqL5plj6a5IbQGY2YRcN+kTXTNZ0td6HLRDzo6cU3zAAQE3oYQDX6S8+XRQkgn7DETL7mZ/leUB57EP28SbR9AUdHxwqSgI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1fd66cddd07so11895105ad.2 for ; Thu, 15 Aug 2024 16:15:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723763752; x=1724368552; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=5+WBgGSJhkxEc2Q4t/cnS8fk2djJgwZ8OHsM0PtxZNo=; b=KMa+6E5Ae3CEn/pzwsKIbYp4wcXBmheKelQpZO1PUswmEn1vHjjv4gKG5+8YjusnNz gKt8PsVY5LSgcTcpJ7DQOoXvNB3DqvPlRkMyt2gQCNQI+Oe5Ltk6DaeWKuE1JsNgiY0k NmFP7H5VYyZ+UGveycwsa47VUUbKitqh32OQJmCGqDaJL6VxYmZ4Ki7Lb1bILiCvdLsj Fz+elVNI6DXINh3fjWm/dmn1BHuA4O6CDW1qN1HROhQNgsQhkp/2DcqEsBR97UAWJ53y GrTBfJunN3wUwKFdAai6qOMLyxruA2VEucd2/GwF7kEZ34IBSmSDHIwPxlyrMtn/eSiT Hy0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723763752; x=1724368552; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5+WBgGSJhkxEc2Q4t/cnS8fk2djJgwZ8OHsM0PtxZNo=; b=r2+6CBd+SixGhphbU0eBVOy8Yo2LSKcvudvXXJ7rxsIUGoPCxTO2eR7oiFv8GZseWY zt2J9z0EP/cH1sagU92hI1YeTN/7wCB7xK15ZN70ZKXLm7L9rHPRaBxgzk0TQCDo+QP1 x5thUWfToCrpJp6yxaern7ZXq+Ifcmx07d6blAoPeGIU6EYzW/163BNIZnhDFKciMSFG 3HX0OouK2ajHgSX6Afs1FGYyAgvIoV4K0rHFwa5AM/aw05jmpRlWYt1Hz5Ic5nfyfL5x kVAb6byeh221Qw2yXrKAs4TmcIiIqyDCHK7fBnBExYjCjGe1bEaaN7t1o88Fvtzs3gDG Ff0g== X-Gm-Message-State: AOJu0Yxp4IjxyhSYODqXF0fWLQUKgCcvbOSYiORoqxyAG6ed34nmS+vo BEhDPv5noy8GBqrGG74LhOsFQAYChPEveMtqcb+TPkarrl7q17CqQaVn+rc6 X-Google-Smtp-Source: AGHT+IFqdPUWPnq1+200sCvYdCHNnHyI1nX/29V/GEZbQGZRET2UJx97LBBd27aSJBXBKZUICxXJmQ== X-Received: by 2002:a17:902:dac5:b0:1fc:3dad:25ed with SMTP id d9443c01a7336-20203e5334amr17565425ad.9.1723763751727; Thu, 15 Aug 2024 16:15:51 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.56.169.70]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f039f074sm15025225ad.251.2024.08.15.16.15.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2024 16:15:51 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 4BF87C0190; Thu, 15 Aug 2024 16:15:50 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: Noah Goldstein Subject: [COMMITTED 2.38/2.37] x86: Fix bug in strchrnul-evex512 [BZ #32078] Date: Thu, 15 Aug 2024 16:15:48 -0700 Message-ID: <20240815231548.155653-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3017.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_SBL_CSS, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org From: Noah Goldstein Issue was we were expecting not matches with CHAR before the start of the string in the page cross case. The check code in the page cross case: ``` and $0xffffffffffffffc0,%rax vmovdqa64 (%rax),%zmm17 vpcmpneqb %zmm17,%zmm16,%k1 vptestmb %zmm17,%zmm17,%k0{%k1} kmovq %k0,%rax inc %rax shr %cl,%rax je L(continue) ``` expects that all characters that neither match null nor CHAR will be 1s in `rax` prior to the `inc`. Then the `inc` will overflow all of the 1s where no relevant match was found. This is incorrect in the page-cross case, as the `vmovdqa64 (%rax),%zmm17` loads from before the start of the input string. If there are matches with CHAR before the start of the string, `rax` won't properly overflow. The fix is quite simple. Just replace: ``` inc %rax shr %cl,%rax ``` With: ``` sar %cl,%rax inc %rax ``` The arithmetic shift will clear any matches prior to the start of the string while maintaining the signbit so the 1s can properly overflow to zero in the case of no matches. Reviewed-by: H.J. Lu (cherry picked from commit 7da08862471dfec6fdae731c2a5f351ad485c71f) --- string/test-strchr.c | 65 ++++++++++++++++++++- sysdeps/x86_64/multiarch/strchr-evex-base.S | 8 +-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/string/test-strchr.c b/string/test-strchr.c index 323c844d21..050da4641e 100644 --- a/string/test-strchr.c +++ b/string/test-strchr.c @@ -225,6 +225,69 @@ check1 (void) check_result (impl, s, c, exp_result); } +static void +check2 (void) +{ + CHAR *s = (CHAR *) (buf1 + getpagesize () - 4 * sizeof (CHAR)); + CHAR *s_begin = (CHAR *) (buf1 + getpagesize () - 64); +#ifndef USE_FOR_STRCHRNUL + CHAR *exp_result = NULL; +#else + CHAR *exp_result = s + 1; +#endif + CHAR val = 0x12; + for (; s_begin != s; ++s_begin) + *s_begin = val; + + s[0] = val + 1; + s[1] = 0; + s[2] = val + 1; + s[3] = val + 1; + + { + FOR_EACH_IMPL (impl, 0) + check_result (impl, s, val, exp_result); + } + s[3] = val; + { + FOR_EACH_IMPL (impl, 0) + check_result (impl, s, val, exp_result); + } + exp_result = s; + s[0] = val; + { + FOR_EACH_IMPL (impl, 0) + check_result (impl, s, val, exp_result); + } + + s[3] = val + 1; + { + FOR_EACH_IMPL (impl, 0) + check_result (impl, s, val, exp_result); + } + + s[0] = val + 1; + s[1] = val + 1; + s[2] = val + 1; + s[3] = val + 1; + s[4] = val; + exp_result = s + 4; + { + FOR_EACH_IMPL (impl, 0) + check_result (impl, s, val, exp_result); + } + s[4] = 0; +#ifndef USE_FOR_STRCHRNUL + exp_result = NULL; +#else + exp_result = s + 4; +#endif + { + FOR_EACH_IMPL (impl, 0) + check_result (impl, s, val, exp_result); + } +} + int test_main (void) { @@ -233,7 +296,7 @@ test_main (void) test_init (); check1 (); - + check2 (); printf ("%20s", ""); FOR_EACH_IMPL (impl, 0) printf ("\t%s", impl->name); diff --git a/sysdeps/x86_64/multiarch/strchr-evex-base.S b/sysdeps/x86_64/multiarch/strchr-evex-base.S index 7209435caf..da6d0eafbb 100644 --- a/sysdeps/x86_64/multiarch/strchr-evex-base.S +++ b/sysdeps/x86_64/multiarch/strchr-evex-base.S @@ -124,13 +124,13 @@ L(page_cross): VPCMPNE %VMM(1), %VMM(0), %k1 VPTEST %VMM(1), %VMM(1), %k0{%k1} KMOV %k0, %VRAX -# ifdef USE_AS_WCSCHR + sar %cl, %VRAX +#ifdef USE_AS_WCSCHR sub $VEC_MATCH_MASK, %VRAX -# else +#else inc %VRAX -# endif +#endif /* Ignore number of character for alignment adjustment. */ - shr %cl, %VRAX jz L(align_more) bsf %VRAX, %VRAX