From nobody Thu Dec 28 17:20:10 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4T1Fd72Bx4z56C9k; Thu, 28 Dec 2023 17:20:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4T1Fd701Fmz3ZXC; Thu, 28 Dec 2023 17:20:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703784011; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DnNHHXFCkObcIFcT7gFgTs0m6K9U7Wu2Qy6fpEEf7oA=; b=qyr9sq0B0XdUjHtGlCPniSpP9lGvoRZ9ofPpW0pSUWkDYvKfc/DN6bwXECMEPE3Exw+Zcz 1vajkoravuANp5kjHrGwLe+G54FuSg6g9eIhIg6dEiIoNx1I4myhN9PMq5R4NlmFVPD19j cykb48SqhwOedMPkQlqsJsSTHBr2tGdyqCvKSQ4hncf6gSj9PzMZ35i/MiBBZJ1cw3OScZ eQmHSsoIlYweK87TLB7r4KXqKMBmMyonUADgj8e82HczafYay/Mt/mPmEOSsEg1rj8lnck 5w22bA2ZxTcimQDpBQdyG/8mNh5dX2R4dW22fmdsPwS5ou6lctE3iCxsFN7daA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703784011; a=rsa-sha256; cv=none; b=xWQl64VCcl8zEy1eSvMVA2aerh59/ONGfBg6EEEB/pv/1ChVXH7UVJ8I7FBWDjN0NXB4xr gdeYAwxBjOJ4RBQE34sIjvH2i/674H0gDjSD7eHr9C1RQuquZ2+wjEhGs8sAIEfQ2PL+Hd g+Aov2kLrfRANKokJdqVDV4Ffu2YyoWmZCTQwFRu8/GYdJMQPoevu6Zhx7J+uE4OcXtxZd dR/sQIv8vRwXZ5seZCoS6dkrISKI2fvsX5jBZdfv4/FbmO/dx9qfFH+P6El57s9HyoRo3j juPKJH8VuqyxiXqVAEEuP+L4c68mEgDkX7/Npu/7ymNWYgO03qhLBFkPTUaEJA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703784011; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DnNHHXFCkObcIFcT7gFgTs0m6K9U7Wu2Qy6fpEEf7oA=; b=QttoGYgTtNi7EiJXXrE4nMvbTGfHX/0cYkn73hLPlB8i9xtjK99FToDs/C3brW5+OeBwNw IexpigNKzOg0TKomgKLTfE/+IZY+6RYAXHaB6BCbnH8BzoXvs5TcDkIKszcd4u1EUYX/pf rBcGh1OzXZIXRxnic0imJQpDXq7KStaKWLdSZ9EGCz6Kk/8K5U/jkA6AWwYJQi/vAnDcKu Ui5EsPYYM3Yqs76taby7mAzo7PDNQvn9OqhT+MAkvE/k281h8FcteH6inHGNKzL9LTETeg PKk/G0W/iAeCa2Sb+x38+w8mVSWJytZstcEa8ZuYMuioB3/iHU7xKbZehF9vQA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4T1Fd665gYzXF3; Thu, 28 Dec 2023 17:20:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BSHKAGV078051; Thu, 28 Dec 2023 17:20:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BSHKA5S078042; Thu, 28 Dec 2023 17:20:10 GMT (envelope-from git) Date: Thu, 28 Dec 2023 17:20:10 GMT Message-Id: <202312281720.3BSHKA5S078042@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Robert Clausecker Subject: git: c35334b08561 - stable/14 - lib/libc/tests/string: add extended unit tests for strcmp() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: c35334b085616c8ad9a567a04cb1a5cbf6b76a80 Auto-Submitted: auto-generated The branch stable/14 has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=c35334b085616c8ad9a567a04cb1a5cbf6b76a80 commit c35334b085616c8ad9a567a04cb1a5cbf6b76a80 Author: Robert Clausecker AuthorDate: 2023-09-22 20:42:00 +0000 Commit: Robert Clausecker CommitDate: 2023-12-28 17:02:40 +0000 lib/libc/tests/string: add extended unit tests for strcmp() This changeset add a new set of tests that comprehensively test strcmp() on various alignments of the input. This made it easy to smoke out many exciting new bugs in the new SSE strcmp() implementation from D41971. MFC after: 1 week Reviewed by: ngie Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D41970 (cherry picked from commit b49596de2622a51e5673ba2c0b3718cd88a2aa41) --- lib/libc/tests/string/Makefile | 2 + lib/libc/tests/string/strcmp_test.c | 132 ++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/lib/libc/tests/string/Makefile b/lib/libc/tests/string/Makefile index 1994b128ab57..e9b066cacac7 100644 --- a/lib/libc/tests/string/Makefile +++ b/lib/libc/tests/string/Makefile @@ -11,6 +11,7 @@ ATF_TESTS_C+= flsll_test ATF_TESTS_C+= memcmp_test ATF_TESTS_C+= memset_s_test ATF_TESTS_C+= stpncpy_test +ATF_TESTS_C+= strcmp2_test ATF_TESTS_C+= strcspn_test ATF_TESTS_C+= strerror2_test ATF_TESTS_C+= strspn_test @@ -37,6 +38,7 @@ NETBSD_ATF_TESTS_C+= strpbrk_test NETBSD_ATF_TESTS_C+= strrchr_test NETBSD_ATF_TESTS_C+= swab_test +SRCS.strcmp2_test= strcmp_test.c SRCS.strerror2_test= strerror_test.c .include "../Makefile.netbsd-tests" diff --git a/lib/libc/tests/string/strcmp_test.c b/lib/libc/tests/string/strcmp_test.c new file mode 100644 index 000000000000..75ebcbcadda2 --- /dev/null +++ b/lib/libc/tests/string/strcmp_test.c @@ -0,0 +1,132 @@ +/*- + * Copyright (c) 2023 The FreeBSD Foundation + * + * This software was developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE + */ + +#include + +#include +#include +#include + +int (*volatile strcmp_fn)(const char *, const char *); + +ATF_TC(strcmp_alignments); +ATF_TC_HEAD(strcmp_alignments, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test strcmp(3) with various alignments"); +} + +static void +alignment_testcase(char *a, char *b, int want) +{ + int res; + + res = strcmp_fn(a, b); + ATF_CHECK_MSG(want == (res > 0) - (res < 0), + "strcmp(%p \"%s\", %p \"%s\") = %d != %d", + (void *)a, a, (void *)b, b, res, want); +} + +static void +check_strcmp_alignments(char a[], char b[], + size_t a_off, size_t b_off, size_t len, size_t pos) +{ + char *a_str, *b_str, a_orig, b_orig; + + a[a_off] = '\0'; + b[b_off] = '\0'; + + a_str = a + a_off + 1; + b_str = b + b_off + 1; + + a_str[len] = '\0'; + b_str[len] = '\0'; + a_str[len+1] = 'A'; + b_str[len+1] = 'B'; + + a_orig = a_str[pos]; + b_orig = b_str[pos]; + + alignment_testcase(a_str, b_str, 0); + + if (pos < len) { + a_str[pos] = '\0'; + alignment_testcase(a_str, b_str, -1); + a_str[pos] = a_orig; + b_str[pos] = '\0'; + alignment_testcase(a_str, b_str, 1); + b_str[pos] = b_orig; + } + + a_str[pos] = 'X'; + alignment_testcase(a_str, b_str, 1); + a_str[pos] = a_orig; + b_str[pos] = 'X'; + alignment_testcase(a_str, b_str, -1); + b_str[pos] = b_orig; + + a[a_off] = '-'; + b[b_off] = '-'; + a_str[len] = '-'; + b_str[len] = '-'; + a_str[len+1] = '-'; + b_str[len+1] = '-'; +} + +ATF_TC_BODY(strcmp_alignments, tc) +{ + size_t a_off, b_off, len, pos; + /* 16B alignment offset + 64B buffer + sentinel before/after + NUL */ + char a[64+16+3], b[64+16+3]; + + memset(a, '-', sizeof(a)); + memset(b, '-', sizeof(b)); + a[sizeof(a) - 1] = '\0'; + b[sizeof(b) - 1] = '\0'; + + /* check alignment offsets relevant for SSE routines */ + for (a_off = 0; a_off < 16; a_off++) + for (b_off = 0; b_off < 16; b_off++) + /* ensure main loop (@ 32B) is completed at least once */ + for (len = 1; len <= 64; len++) + for (pos = 0; pos <= len; pos++) + check_strcmp_alignments(a, b, a_off, b_off, len, pos); +} + +ATF_TP_ADD_TCS(tp) +{ + void *dl_handle; + + dl_handle = dlopen(NULL, RTLD_LAZY); + strcmp_fn = dlsym(dl_handle, "test_strcmp"); + if (strcmp_fn == NULL) + strcmp_fn = strcmp; + + ATF_TP_ADD_TC(tp, strcmp_alignments); + + return atf_no_error(); +}