xref: /freebsd/lib/libc/tests/string/memccpy_test.c (revision 1edb7116)
1 /*-
2  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3  * Copyright (c) 2023 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Robert Clausecker
7  * <fuz@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 #include <assert.h>
35 #include <dlfcn.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include <atf-c.h>
42 
43 void *(*memccpy_fn)(void *restrict, const void *restrict, int, size_t);
44 
45 static char *
46 makebuf(size_t len, int guard_at_end)
47 {
48 	char *buf;
49 	size_t alloc_size, page_size;
50 
51 	page_size = getpagesize();
52 	alloc_size = roundup2(len, page_size) + page_size;
53 
54 	buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
55 	assert(buf);
56 	if (guard_at_end) {
57 		assert(munmap(buf + alloc_size - page_size, page_size) == 0);
58 		return (buf + alloc_size - page_size - len);
59 	} else {
60 		assert(munmap(buf, page_size) == 0);
61 		return (buf + page_size);
62 	}
63 }
64 
65 static void
66 test_memccpy(const char *s)
67 {
68 	char *src, *dst, *expected;
69 	size_t size, bufsize, x;
70 	int i, j;
71 
72 	size = strlen(s) + 1;
73 	for (i = 0; i <= 1; i++) {
74 		for (j = 0; j <= 1; j++) {
75 			for (bufsize = 0; bufsize <= size + 10; bufsize++) {
76 				src = makebuf(size, i);
77 				memcpy(src, s, size);
78 				dst = makebuf(bufsize, j);
79 				memset(dst, 'X', bufsize);
80 				expected = bufsize >= size ? dst + size : NULL;
81 				assert(memccpy_fn(dst, src, src[size-1], bufsize) == expected);
82 				assert(bufsize == 0 || strncmp(src, dst, bufsize - 1) == 0);
83 				for (x = size; x < bufsize; x++)
84 					assert(dst[x] == 'X');
85 			}
86 		}
87 	}
88 }
89 
90 static void
91 test_sentinel(char *dest, char *src, size_t destlen, size_t srclen)
92 {
93 	size_t i, effective_len;
94 	void *res, *wantres;
95 	const char *fail = NULL;
96 	char terminator;
97 
98 	for (i = 0; i < srclen; i++)
99 		/* src will never include (){} */
100 		src[i] = '0' + i;
101 
102 	/* source sentinels: not to be copied */
103 	src[-1] = '(';
104 	src[srclen] = ')';
105 
106 	memset(dest, '\xee', destlen);
107 
108 	/* destination sentinels: not to be touched */
109 	dest[-1] = '{';
110 	dest[destlen] = '}';
111 
112 	effective_len = srclen < destlen ? srclen : destlen;
113 	wantres = srclen <= destlen ? dest + srclen : NULL;
114 	terminator = src[srclen-1];
115 	res = memccpy_fn(dest, src, terminator, destlen);
116 
117 	if (dest[-1] != '{')
118 		fail = "start sentinel overwritten";
119 	else if (dest[destlen] != '}')
120 		fail = "end sentinel overwritten";
121 	else if (res != wantres)
122 		fail = "incorrect return value";
123 	else if (destlen > 0 && memcmp(src, dest, effective_len) != 0)
124 		fail = "string not copied correctly";
125 	else for (i = srclen; i < destlen; i++)
126 		if (dest[i] != '\xee') {
127 			fail = "buffer mutilated behind string";
128 			break;
129 		}
130 
131 	if (fail)
132 		atf_tc_fail_nonfatal("%s\n"
133 		    "memccpy(%p \"%s\", %p \"%s\", %u '%c', %zu) = %p (want %p)\n",
134 		    fail, dest, dest, src, src, terminator, terminator, destlen, res, wantres);
135 }
136 
137 ATF_TC_WITHOUT_HEAD(null);
138 ATF_TC_BODY(null, tc)
139 {
140 	ATF_CHECK_EQ(memccpy_fn(NULL, "foo", 42, 0), NULL);
141 }
142 
143 ATF_TC(zero_extension);
144 ATF_TC_HEAD(zero_extension, tc)
145 {
146 	atf_tc_set_md_var(tc, "descr",
147 	    "Ensure the upper bits of the terminator are ignored");
148 }
149 ATF_TC_BODY(zero_extension, tc)
150 {
151 	int mask = -1 & ~UCHAR_MAX;
152 	char buf[16];
153 
154 	memset(buf, 0xcc, sizeof(buf));
155 	ATF_CHECK_EQ(memccpy(buf, "foobar", 'r', sizeof(buf)), buf + sizeof("foobar") - 1);
156 	ATF_CHECK_EQ(memcmp(buf, "foobar", sizeof("foobar") - 1), 0);
157 
158 	memset(buf, 0xcc, sizeof(buf));
159 	ATF_CHECK_EQ(memccpy(buf, "foobar", mask | 'r', sizeof(buf)), buf + sizeof("foobar") - 1);
160 	ATF_CHECK_EQ(memcmp(buf, "foobar", sizeof("foobar") - 1), 0);
161 }
162 
163 ATF_TC_WITHOUT_HEAD(bounds);
164 ATF_TC_BODY(bounds, tc)
165 {
166 	size_t i;
167 	char buf[64];
168 
169 	for (i = 0; i < sizeof(buf) - 1; i++) {
170 		buf[i] = ' ' + i;
171 		test_memccpy(buf);
172 	}
173 }
174 
175 ATF_TC_WITHOUT_HEAD(alignments);
176 ATF_TC_BODY(alignments, tc)
177 {
178 	size_t srcalign, destalign, srclen, destlen;
179 	char src[15+2+64]; /* 15 offsets + 64 max length + sentinels */
180 	char dest[15+2+64]; /* 15 offsets + 64 max length + sentinels */
181 
182 	for (srcalign = 0; srcalign < 16; srcalign++)
183 		for (destalign = 0; destalign < 16; destalign++)
184 			for (srclen = 1; srclen < 64; srclen++)
185 				for (destlen = 0; destlen < 64; destlen++)
186 					test_sentinel(dest+destalign+1,
187 					    src+srcalign+1, destlen, srclen);
188 }
189 
190 ATF_TP_ADD_TCS(tp)
191 {
192 	void *dl_handle;
193 
194 	dl_handle = dlopen(NULL, RTLD_LAZY);
195 	memccpy_fn = dlsym(dl_handle, "test_memccpy");
196 	if (memccpy_fn == NULL)
197 		memccpy_fn = memccpy;
198 
199 	ATF_TP_ADD_TC(tp, null);
200 	ATF_TP_ADD_TC(tp, zero_extension);
201 	ATF_TP_ADD_TC(tp, bounds);
202 	ATF_TP_ADD_TC(tp, alignments);
203 
204 	return (atf_no_error());
205 }
206