summaryrefslogtreecommitdiff
path: root/usr/src/test/libc-tests/tests/memchr.c
blob: 9ec3c6e0ef95689388ed69d709bd56180cc117fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2021 Oxide Computer Company
 */

/*
 * Various tests for memchr() and memrchr(). Note, this test assumes that the
 * system is either ILP32 or LP64 with an 8-bit unsigned char due to the tests
 * that are explicitly looking at making sure memchr() and memrchr() truncate
 * correctly.
 */

#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <err.h>
#include <stdlib.h>

/*
 * memchr_buf is a page sized buffer surrounded by two PROT_NONE pages which are
 * meant to try and catch us walking over the edge of the buffer.
 */
static uint8_t *memchr_buf;
static size_t memchr_buflen;

static void
memchr_setup(void)
{
	size_t pgsz = getpagesize();
	void *addr;

	if (pgsz <= 0) {
		err(EXIT_FAILURE, "failed to get system page size");
	}

	addr = mmap(NULL, 3 * pgsz, PROT_READ | PROT_WRITE,
	    MAP_PRIVATE | MAP_ANON, -1, 0);
	if (addr == MAP_FAILED) {
		err(EXIT_FAILURE, "failed to mmap %zu bytes", 3 * pgsz);
	}

	memchr_buf = (uint8_t *)addr + pgsz;
	memchr_buflen = pgsz;

	if (mprotect(addr, pgsz, PROT_NONE) != 0) {
		err(EXIT_FAILURE, "failed to protect leading PROT_NONE guard "
		    "at %p", addr);
	}

	addr = (uint8_t *)addr + 2 * pgsz;
	if (mprotect(addr, pgsz, PROT_NONE) != 0) {
		err(EXIT_FAILURE, "failed to protect trailing PROT_NONE guard "
		    "at %p", addr);
	}
}

static boolean_t
memchr_basic(void)
{
	boolean_t ret = B_TRUE;
	const void *targ;
	const void *found;

	(void) memset(memchr_buf, 0, memchr_buflen);
	memchr_buf[0] = 'r';

	if ((found = memchr(memchr_buf, 'r', memchr_buflen)) != memchr_buf) {
		warnx("TEST FAILED: memchr failed to find 'r' (1), found %p, "
		    "expected %p", found, memchr_buf);
		ret = B_FALSE;
	}

	if ((found = memrchr(memchr_buf, 'r', memchr_buflen)) != memchr_buf) {
		warnx("TEST FAILED: memrchr failed to find 'r' (1), found %p, "
		    "expected %p", found, memchr_buf);
		ret = B_FALSE;
	}

	memchr_buf[memchr_buflen - 1] = 'r';
	targ = &memchr_buf[memchr_buflen - 1];

	if ((found = memchr(memchr_buf, 'r', memchr_buflen)) != memchr_buf) {
		warnx("TEST FAILED: memchr failed to find 'r' (2), found %p, "
		    "expected %p", found, memchr_buf);
		ret = B_FALSE;
	}

	if ((found = memrchr(memchr_buf, 'r', memchr_buflen)) != targ) {
		warnx("TEST FAILED: memrchr failed to find 'r' (2), found %p, "
		    "expected %p", found, targ);
		warnx("TEST FAILED: memchr failed to find 'r'");
		ret = B_FALSE;
	}

	memchr_buf[0] = 0;

	if ((found = memchr(memchr_buf, 'r', memchr_buflen)) != targ) {
		warnx("TEST FAILED: memchr failed to find 'r' (3), found %p, "
		    "expected %p", found, targ);
		ret = B_FALSE;
	}

	if ((found = memrchr(memchr_buf, 'r', memchr_buflen)) != targ) {
		warnx("TEST FAILED: memrchr failed to find 'r' (3), found %p, "
		    "expected %p", found, targ);
		ret = B_FALSE;
	}

	if (ret) {
		(void) printf("TEST PASSED: basic memchr() and memrchr()\n");
	}
	return (ret);
}

static boolean_t
memchr_notfound(void)
{
	boolean_t ret = B_TRUE;
	const void *found;

	(void) memset(memchr_buf, 0x23, memchr_buflen);

	if ((found = memchr(memchr_buf, 0, memchr_buflen)) != NULL) {
		warnx("TEST FAILED: memchr unexpectedly found value (1), "
		    "found %p, expected %p", found, NULL);
		ret = B_FALSE;
	}

	if (memrchr(memchr_buf, 0, memchr_buflen) != NULL) {
		warnx("TEST FAILED: memrchr unexpectedly found value (1), "
		    "found %p, expected %p", found, NULL);
		ret = B_FALSE;
	}

	if (memchr(memchr_buf, 0x24, memchr_buflen) != NULL) {
		warnx("TEST FAILED: memchr unexpectedly found value (2), "
		    "found %p, expected %p", found, NULL);
		ret = B_FALSE;
	}

	if (memrchr(memchr_buf, 0x24, memchr_buflen) != NULL) {
		warnx("TEST FAILED: memrchr unexpectedly found value (2), "
		    "found %p, expected %p", found, NULL);
		ret = B_FALSE;
	}

	memchr_buf[1] = 0x24;

	if (memchr(memchr_buf, 0x24, 1) != NULL) {
		warnx("TEST FAILED: memchr unexpectedly found value (3), "
		    "found %p, expected %p", found, NULL);
		ret = B_FALSE;
	}

	if (memrchr(memchr_buf, 0x24, 1) != NULL) {
		warnx("TEST FAILED: memrchr unexpectedly found value (3), "
		    "found %p, expected %p", found, NULL);
		ret = B_FALSE;
	}

	memchr_buf[1] = 0x24;

	if (memchr(memchr_buf + 1, 0x23, 1) != NULL) {
		warnx("TEST FAILED: memchr unexpectedly found value (4), "
		    "found %p, expected %p", found, NULL);
		ret = B_FALSE;
	}

	if (memrchr(memchr_buf + 1, 0x23, 1) != NULL) {
		warnx("TEST FAILED: memrchr unexpectedly found value (4), "
		    "found %p, expected %p", found, NULL);
		ret = B_FALSE;
	}

	if (ret) {
		(void) printf("TEST PASSED: memchr() and memrchr() on "
		    "missing values\n");
	}

	return (ret);
}

static boolean_t
memchr_truncation(void)
{
	boolean_t ret = B_TRUE;
	const void *found;
	const void *targ;

	(void) memset(memchr_buf, 0x42, memchr_buflen);

	if ((found = memchr(memchr_buf, 0x42, memchr_buflen)) != memchr_buf) {
		warnx("TEST FAILED: memchr failed to find 0x42, found %p, "
		    "expected %p", found, memchr_buf);
		ret = B_FALSE;
	}

	targ = &memchr_buf[memchr_buflen - 1];

	if ((found = memrchr(memchr_buf, 0x42, memchr_buflen)) != targ) {
		warnx("TEST FAILED: memrchr failed to find 0x42, found %p, "
		    "expected %p", found, targ);
		ret = B_FALSE;
	}

	if ((found = memchr(memchr_buf, 0x430042, memchr_buflen)) !=
	    memchr_buf) {
		warnx("TEST FAILED: memchr failed to find 0x42 with 0x430042, "
		    "found %p, expected %p", found, memchr_buf);
		ret = B_FALSE;
	}

	if ((found = memrchr(memchr_buf, 0x430042, memchr_buflen)) != targ) {
		warnx("TEST FAILED: memrchr failed to find 0x42 with 0x430042, "
		    "found %p, expected %p", found, targ);
		ret = B_FALSE;
	}

	/*
	 * -190 is -0xbe, which when cast to an unsigned char will be 0x42.
	 */
	if ((found = memchr(memchr_buf, -190, memchr_buflen)) != memchr_buf) {
		warnx("TEST FAILED: memchr failed to find 0x42 with -190, "
		    "found %p, expected %p", found, memchr_buf);
		ret = B_FALSE;
	}

	if ((found = memrchr(memchr_buf, -190, memchr_buflen)) != targ) {
		warnx("TEST FAILED: memrchr failed to find 0x42 with -190, "
		    "found %p, expected %p", found, targ);
		ret = B_FALSE;
	}

	if ((found = memchr(memchr_buf, -190, memchr_buflen)) != memchr_buf) {
		warnx("TEST FAILED: memchr failed to find 0x42 with -190, "
		    "found %p, expected %p", found, memchr_buf);
		ret = B_FALSE;
	}

	if ((found = memrchr(memchr_buf, -190, memchr_buflen)) != targ) {
		warnx("TEST FAILED: memrchr failed to find 0x42 with -190, "
		    "found %p, expected %p", found, targ);
		ret = B_FALSE;
	}

	if ((found = memchr(memchr_buf, 0x42424200, memchr_buflen)) != NULL) {
		warnx("TEST FAILED: memchr somehow found 0x42 with "
		    "0x42424200, found %p, expected NULL", found);
		ret = B_FALSE;
	}

	if ((found = memrchr(memchr_buf, 0x42424200, memchr_buflen)) != NULL) {
		warnx("TEST FAILED: memrchr somehow found 0x42 with "
		    "0x42424200, found %p, expected NULL", found);
		ret = B_FALSE;
	}

	if (ret) {
		(void) printf("TEST PASSED: truncated values\n");
	}

	return (B_TRUE);
}

int
main(void)
{
	int ret = EXIT_SUCCESS;

	memchr_setup();

	if (!memchr_basic())
		ret = EXIT_FAILURE;
	if (!memchr_notfound())
		ret = EXIT_FAILURE;
	if (!memchr_truncation())
		ret = EXIT_FAILURE;

	if (ret == EXIT_SUCCESS) {
		(void) printf("All tests passed successfully\n");
	}

	return (ret);
}