xref: /freebsd/lib/libc/tests/string/strcmp_test.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * Copyright (c) 2023 The FreeBSD Foundation
3  *
4  * This software was developed by Robert Clausecker <fuz@FreeBSD.org>
5  * under sponsorship from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE
27  */
28 
29 #include <sys/cdefs.h>
30 
31 #include <atf-c.h>
32 #include <dlfcn.h>
33 #include <string.h>
34 
35 int (*volatile strcmp_fn)(const char *, const char *);
36 
37 ATF_TC(strcmp_alignments);
38 ATF_TC_HEAD(strcmp_alignments, tc)
39 {
40 	atf_tc_set_md_var(tc, "descr", "Test strcmp(3) with various alignments");
41 }
42 
43 static void
44 alignment_testcase(char *a, char *b, int want)
45 {
46 	int res;
47 
48 	res = strcmp_fn(a, b);
49 	ATF_CHECK_MSG(want == (res > 0) - (res < 0),
50 	    "strcmp(%p \"%s\", %p \"%s\") = %d != %d",
51 	    (void *)a, a, (void *)b, b, res, want);
52 }
53 
54 static void
55 check_strcmp_alignments(char a[], char b[],
56     size_t a_off, size_t b_off, size_t len, size_t pos)
57 {
58 	char *a_str, *b_str, a_orig, b_orig;
59 
60 	a[a_off] = '\0';
61 	b[b_off] = '\0';
62 
63 	a_str = a + a_off + 1;
64 	b_str = b + b_off + 1;
65 
66 	a_str[len] = '\0';
67 	b_str[len] = '\0';
68 	a_str[len+1] = 'A';
69 	b_str[len+1] = 'B';
70 
71 	a_orig = a_str[pos];
72 	b_orig = b_str[pos];
73 
74 	alignment_testcase(a_str, b_str, 0);
75 
76 	if (pos < len) {
77 		a_str[pos] = '\0';
78 		alignment_testcase(a_str, b_str, -1);
79 		a_str[pos] = a_orig;
80 		b_str[pos] = '\0';
81 		alignment_testcase(a_str, b_str, 1);
82 		b_str[pos] = b_orig;
83 	}
84 
85 	a_str[pos] = 'X';
86 	alignment_testcase(a_str, b_str, 1);
87 	a_str[pos] = a_orig;
88 	b_str[pos] = 'X';
89 	alignment_testcase(a_str, b_str, -1);
90 	b_str[pos] = b_orig;
91 
92 	a[a_off] = '-';
93 	b[b_off] = '-';
94 	a_str[len] = '-';
95 	b_str[len] = '-';
96 	a_str[len+1] = '-';
97 	b_str[len+1] = '-';
98 }
99 
100 ATF_TC_BODY(strcmp_alignments, tc)
101 {
102 	size_t a_off, b_off, len, pos;
103 	/* 16B alignment offset + 64B buffer + sentinel before/after + NUL */
104 	char a[64+16+3], b[64+16+3];
105 
106 	memset(a, '-', sizeof(a));
107 	memset(b, '-', sizeof(b));
108 	a[sizeof(a) - 1] = '\0';
109 	b[sizeof(b) - 1] = '\0';
110 
111 	/* check alignment offsets relevant for SSE routines */
112 	for (a_off = 0; a_off < 16; a_off++)
113 		for (b_off = 0; b_off < 16; b_off++)
114 			/* ensure main loop (@ 32B) is completed at least once */
115 			for (len = 1; len <= 64; len++)
116 				for (pos = 0; pos <= len; pos++)
117 					check_strcmp_alignments(a, b, a_off, b_off, len, pos);
118 }
119 
120 ATF_TP_ADD_TCS(tp)
121 {
122 	void *dl_handle;
123 
124 	dl_handle = dlopen(NULL, RTLD_LAZY);
125 	strcmp_fn = dlsym(dl_handle, "test_strcmp");
126 	if (strcmp_fn == NULL)
127 		strcmp_fn = strcmp;
128 
129 	ATF_TP_ADD_TC(tp, strcmp_alignments);
130 
131 	return atf_no_error();
132 }
133