From nobody Mon Dec 25 14:25:46 2023 X-Original-To: dev-commits-src-main@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 4SzKvH45zsz54mvf; Mon, 25 Dec 2023 14:25:47 +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 4SzKvH184yz3Mpg; Mon, 25 Dec 2023 14:25:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703514347; 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=mUaNz7au34G+L7QdsvH+6LxMuTsrf6PaxIfwu3i8IMA=; b=jb/x9Ndltb+fckWBWktfE8OIiZRvJ9Q98qeDe8IdRCySypjVV7SRGV3rjEB+RpnXNVo1Rt MgUQg2OUOO0NWsxCmXkXt4nmHcuIQevscaDrPPrVd9+bWCx8lwgFHRSLTdyb1jWij53iWp 6JOlPtDv8W9hCMuZpPd4lc2C9ltOFkHFFAivdvRnIIsQfJO+aOdgiPgEXIkgLc3gkT9rOI GBkbDyJITYrULOPJeCEFtksT0UqHAhljwcXLBjIBsWU8CR+Li6nqAmX+kv6CWzizLJWWWF L6M0OKGqbgxF13lr4qUvX8hDvii21Ow7JIgmJAshu+r4NoqizeoH8vzf/jE8Kg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703514347; a=rsa-sha256; cv=none; b=npyHyu6cQD8yDISxOCVIkWua3DxygMD85W5kiYZdpw+J7qY1JlzsJzDeLB6XEjG7scawKP 6c+xnpcBomJcRram9EorG9pMpZg4C0zp4TGCOJu8Z8fApcOxSTzoCjfG4P4C4v5daBB7fA IxBqE6J+bHyhTkx7pWBntHn9uJeTzP5AQaivWKH8maWCvfRDFKyifeboy4M6BTz+O+gl/e h3PgJnOcd4U3RlzAoSodVE7Dp7W0Fa76KWEZAfCfiyuECp+ZwOMWyhcnCOgD4alYNrwgGa XWv9/7aMmJhxoaqoduMHBVRCEw+k3y5TfDaLAHiN52/HjJ7Oqd4qa6yeFcCfmQ== 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=1703514347; 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=mUaNz7au34G+L7QdsvH+6LxMuTsrf6PaxIfwu3i8IMA=; b=WA51KATq0Jv5bEJ+KCwXDxzdfnWfyzOMPnIz15ukB2Ma44sUWMmpBx/QHE5oRrIFtdQhxc myJMGg8od5P2udcGxfXac3tm/Sz9zELPc4rr0fkBQ+rCBfLUohYieuZpAKqQlWg1vcrxW8 7AMb6lbe8Pi5qIDDhqD1kv37DhhvoDRtbC4+Ik70I2EK7Hu9cX2xyA55aRLCKCz91WSjiJ uQh5UieAnzV1KXgjqOhCTA3gew9raPEcE4WFC2qQWSwXML5gDAIJKPPxIFWhEG8L+my9y/ BZ4Wr7SbK47fkEb3q6yHu7qUgYLED5ffAl8QCXUymg+WIPrIilvwMN47G1J1aQ== 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 4SzKvH0C8lzJx; Mon, 25 Dec 2023 14:25:47 +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 3BPEPktI023228; Mon, 25 Dec 2023 14:25:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BPEPkgB023225; Mon, 25 Dec 2023 14:25:46 GMT (envelope-from git) Date: Mon, 25 Dec 2023 14:25:46 GMT Message-Id: <202312251425.3BPEPkgB023225@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: 6fa9e7d87375 - main - lib/libc/tests/string/stpncpy_test.c: extend for upcoming SSE implementation List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 6fa9e7d8737548ef93c573387ce62402c368d486 Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=6fa9e7d8737548ef93c573387ce62402c368d486 commit 6fa9e7d8737548ef93c573387ce62402c368d486 Author: Robert Clausecker AuthorDate: 2023-11-05 04:02:00 +0000 Commit: Robert Clausecker CommitDate: 2023-12-25 13:55:37 +0000 lib/libc/tests/string/stpncpy_test.c: extend for upcoming SSE implementation This adds additional unit tests validating the function for All possible alignment offsets of source and destination. Also extend the test to allow testing of an external stpncpy implementation, which greatly simplifies the development of custom implementations. Sponsored by: The FreeBSD Foundation Tested by: developers@, exp-run Approved by: mjg MFC after: 1 month MFC to: stable/14 PR: 275785 Differential Revision: https://reviews.freebsd.org/D42519 --- lib/libc/tests/string/stpncpy_test.c | 99 +++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 14 deletions(-) diff --git a/lib/libc/tests/string/stpncpy_test.c b/lib/libc/tests/string/stpncpy_test.c index 8154237eb8c2..8574b2d591be 100644 --- a/lib/libc/tests/string/stpncpy_test.c +++ b/lib/libc/tests/string/stpncpy_test.c @@ -1,7 +1,11 @@ /*- * Copyright (c) 2009 David Schultz + * Copyright (c) 2023 The FreeBSD Foundation * All rights reserved. * + * Portions of this software were 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: @@ -27,12 +31,15 @@ #include #include #include +#include #include #include #include #include +static char *(*stpncpy_fn)(char *restrict, const char *restrict, size_t); + static char * makebuf(size_t len, int guard_at_end) { @@ -69,7 +76,7 @@ test_stpncpy(const char *s) dst = makebuf(bufsize, j); memset(dst, 'X', bufsize); len = (bufsize < size) ? bufsize : size - 1; - assert(stpncpy(dst, src, bufsize) == dst+len); + assert(stpncpy_fn(dst, src, bufsize) == dst+len); assert(memcmp(src, dst, len) == 0); for (x = len; x < bufsize; x++) assert(dst[x] == '\0'); @@ -78,33 +85,97 @@ test_stpncpy(const char *s) } } -ATF_TC_WITHOUT_HEAD(nul); -ATF_TC_BODY(nul, tc) +static void +test_sentinel(char *dest, char *src, size_t destlen, size_t srclen) { + size_t i; + const char *res, *wantres; + const char *fail = NULL; + + for (i = 0; i < srclen; i++) + /* src will never include (){} */ + src[i] = '0' + i; + src[srclen] = '\0'; + + /* source sentinels: not to be copied */ + src[-1] = '('; + src[srclen+1] = ')'; + + memset(dest, 0xee, destlen); + + /* destination sentinels: not to be touched */ + dest[-1] = '{'; + dest[destlen] = '}'; + + wantres = dest + (srclen > destlen ? destlen : srclen); + res = stpncpy_fn(dest, src, destlen); + + if (dest[-1] != '{') + fail = "start sentinel overwritten"; + else if (dest[destlen] != '}') + fail = "end sentinel overwritten"; + else if (strncmp(src, dest, destlen) != 0) + fail = "string not copied correctly"; + else if (res != wantres) + fail = "incorrect return value"; + else for (i = srclen; i < destlen; i++) + if (dest[i] != '\0') { + fail = "incomplete NUL padding"; + break; + } - test_stpncpy(""); + if (fail) + atf_tc_fail_nonfatal("%s\n" + "stpncpy(%p \"%s\", %p \"%s\", %zu) = %p (want %p)\n", + fail, dest, dest, src, src, destlen, res, wantres); } -ATF_TC_WITHOUT_HEAD(foo); -ATF_TC_BODY(foo, tc) +ATF_TC_WITHOUT_HEAD(null); +ATF_TC_BODY(null, tc) { - - test_stpncpy("foo"); + ATF_CHECK_EQ(stpncpy_fn(NULL, NULL, 0), NULL); } -ATF_TC_WITHOUT_HEAD(glorp); -ATF_TC_BODY(glorp, tc) +ATF_TC_WITHOUT_HEAD(bounds); +ATF_TC_BODY(bounds, tc) { + size_t i; + char buf[64+1]; - test_stpncpy("glorp"); + for (i = 0; i < sizeof(buf) - 1; i++) { + buf[i] = ' ' + i; + buf[i+1] = '\0'; + test_stpncpy(buf); + } +} + +ATF_TC_WITHOUT_HEAD(alignments); +ATF_TC_BODY(alignments, tc) +{ + size_t srcalign, destalign, srclen, destlen; + char src[15+3+64]; /* 15 offsets + 64 max length + NUL + sentinels */ + char dest[15+2+64]; /* 15 offsets + 64 max length + sentinels */ + + for (srcalign = 0; srcalign < 16; srcalign++) + for (destalign = 0; destalign < 16; destalign++) + for (srclen = 0; srclen < 64; srclen++) + for (destlen = 0; destlen < 64; destlen++) + test_sentinel(dest+destalign+1, + src+srcalign+1, destlen, srclen); } ATF_TP_ADD_TCS(tp) { + void *dl_handle; + + dl_handle = dlopen(NULL, RTLD_LAZY); + stpncpy_fn = dlsym(dl_handle, "test_stpncpy"); + if (stpncpy_fn == NULL) + stpncpy_fn = stpncpy; - ATF_TP_ADD_TC(tp, nul); - ATF_TP_ADD_TC(tp, foo); - ATF_TP_ADD_TC(tp, glorp); + ATF_TP_ADD_TC(tp, null); + ATF_TP_ADD_TC(tp, bounds); + ATF_TP_ADD_TC(tp, alignments); return (atf_no_error()); }