From patchwork Tue Aug 13 15:29:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1972008 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=R+UN9GG5; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WjwKx6Bjhz1yZl for ; Wed, 14 Aug 2024 01:29:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1B77C3858420 for ; Tue, 13 Aug 2024 15:29:40 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id 14FF83858403 for ; Tue, 13 Aug 2024 15:29:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 14FF83858403 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 14FF83858403 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::531 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723562965; cv=none; b=loTS4airAos5gvwf9ngktMQNebdEn5MRBy/zUU5g7V9jdUbAsES2b/P6F27xiqaZP4pLskB9hX9JZ1aTuNkoOMUWOwA8H1pJH9c8TA9RIKh7ibKaiAIe2NgYbLh7xux+AdPwa962n0vJFbux8SzZ0PVrCv7SjjfajdV4FAphcDs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723562965; c=relaxed/simple; bh=D2dKk2/M6xxS7nqxnviYgF0LWv9IiaP5XvvE5eBN4rQ=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=am0FelJORSU/lEJQ1kT4wjQRRjKZl14mI4OZTlDQrCjl9JQBmSL9ealHi0q63T4OBs24832mmt8CX2oH17GU1Mc8ky6HT/YFBbqcrJMaY8A84YHyKXz1kf/Qd9Jdmr9tUV66h7IiO7PbZ6WP/CZEZQw8BK8HwQbUYXCT/83FelA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-5af51684d52so6088570a12.1 for ; Tue, 13 Aug 2024 08:29:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723562961; x=1724167761; 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=sDVLMAfQdacp9v+BR1V22UgOAiuOy6t1vv5P8HUNp3o=; b=R+UN9GG52vI1ziO8sqKEGhqR3zNhJ7uK4Y7jEii93A/9E1lwpUgbxUNFJXzHwyg7qh 1YdaQJ6gyzhGMQwNZtH7u1DuHG/hFM2wvdZ7cHmHE/SpuSs0JdbmOiwdD8cRdGu7rCf0 233RDrTGnmWjq22Y+Xokw/8+ELRn4ugkejCwxpASxEQJSmnhtVQkvF2PVRzIBKDDgRfq yPaPD7tvfninyvSbAxdaeMUbz1iduiVKlQaOOX4WzKw1JqQRF+S/DWb7YQuuPC6Y163n SBzn0M7cKvVansQTr8iDhrZXCnJKRZfvUcy2logy62NlKt67MBtX3jl8vuB5btbHM3ni wwIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723562961; x=1724167761; 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=sDVLMAfQdacp9v+BR1V22UgOAiuOy6t1vv5P8HUNp3o=; b=NWdXK27mNcXJVW1HWHQ7BvBC9/xLhF+A0PlS6nx2I3exiUKji4Pf4vqeuOC7lyOC51 Qv1i75AZM51qxelcf02WpsrbPeK2GI8wXZbX6Lx8PoSnnEu50g2niDrRzTgJZkgef8NB qV/67syyNt9TPHuJa2q6l5G7k8WlntFtoBof+6OepgglWDSNrOCwwhg+Q/sq15R8J2NF JlBxCGhR1JL9RL7y+1Ys3u43jG4eoX5fxWGAOrN8igUtATyBDgT4VyFqIoxpIqJXMaCZ ykWmK9q3q3G0z9KUvgg3ocVBKADw5O7JOlfpf3fNL+9FwWcHG9m1PAZ2Pd1/JMhQhMwR o2yA== X-Gm-Message-State: AOJu0YynotJzh2XasinLDtVBz9vSKNehqp8fBPfTSzs80Ck0wuPL9Mgh LLGIIuJjJxBprCdD88+lY3eNJ2AP/Gtq4TNvyo2Pl+6vgB/YYpx8zcoOl5T6 X-Google-Smtp-Source: AGHT+IHjlTFoH90fYZhLpd7CD1rNrD1FMoXfy5c8GmoabwDLtJAe0MLyrtlCkytSa2t+/K2/MsMXAA== X-Received: by 2002:a05:6402:3587:b0:5a1:8984:977e with SMTP id 4fb4d7f45d1cf-5bd44c0cd51mr2842933a12.8.1723562960923; Tue, 13 Aug 2024 08:29:20 -0700 (PDT) Received: from noahgold-desk.. ([185.213.154.220]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5bd196a7d41sm2984537a12.57.2024.08.13.08.29.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 08:29:20 -0700 (PDT) From: Noah Goldstein To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com, hjl.tools@gmail.com Subject: [PATCH v1] x86: Fix bug in strchrnul-evex512 [BZ #32078] Date: Tue, 13 Aug 2024 23:29:14 +0800 Message-Id: <20240813152914.4183543-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 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, 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 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 --- 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 c795eac6fa..72b17af687 100644 --- a/string/test-strchr.c +++ b/string/test-strchr.c @@ -255,6 +255,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) { @@ -263,7 +326,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 04e2c0e79e..3a0b7c9d64 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