Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / lib / libc / string / strlen / strlen_test.c
blob577f4b2156a13db1e3f9ae8696e431e636265761
1 /*
2 * Written by J.T. Conklin <jtc@acorntoolworks.com>
3 * Public domain.
4 */
6 /*
7 * str*() regression suite
9 * Trivial str*() implementations can be audited by hand. Optimized
10 * versions that unroll loops, use naturally-aligned memory acesses,
11 * and "magic" arithmetic sequences to detect zero-bytes, written in
12 * assembler are much harder to validate. This program attempts to
13 * catch the corner cases.
15 * BUGS:
16 * Misssing checks for strncpy, strncat, strncmp, etc.
18 * TODO:
19 * Use mmap/mprotect to ensure the functions don't access memory
20 * across page boundaries.
22 * Consider generating tables programmatically. It would reduce
23 * the size, but it's also one more thing to go wrong.
25 * Share tables between strlen, strcpy, and strcat?
26 * Share tables between strchr and strrchr?
29 #include <assert.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <dlfcn.h>
35 void check_strlen(void);
37 size_t (*volatile strlen_fn)(const char *s);
39 static void
40 write_num(int val)
42 char buf[20];
43 int i;
44 for (i = sizeof buf; --i >= 0;) {
45 buf[i] = '0' + val % 10;
46 val /= 10;
47 if (val == 0) {
48 write(2, buf + i, sizeof buf - i);
49 return;
52 write(2, "overflow", 8);
55 void
56 check_strlen(void)
58 /* try to trick the compiler */
60 int a;
61 int t;
62 size_t len;
63 char buf[64];
65 struct tab {
66 const char* val;
67 size_t len;
70 const struct tab tab[] = {
72 * patterns that check for all combinations of leading and
73 * trailing unaligned characters (on a 64 bit processor)
76 { "", 0 },
77 { "a", 1 },
78 { "ab", 2 },
79 { "abc", 3 },
80 { "abcd", 4 },
81 { "abcde", 5 },
82 { "abcdef", 6 },
83 { "abcdefg", 7 },
84 { "abcdefgh", 8 },
85 { "abcdefghi", 9 },
86 { "abcdefghij", 10 },
87 { "abcdefghijk", 11 },
88 { "abcdefghijkl", 12 },
89 { "abcdefghijklm", 13 },
90 { "abcdefghijklmn", 14 },
91 { "abcdefghijklmno", 15 },
92 { "abcdefghijklmnop", 16 },
93 { "abcdefghijklmnopq", 17 },
94 { "abcdefghijklmnopqr", 18 },
95 { "abcdefghijklmnopqrs", 19 },
96 { "abcdefghijklmnopqrst", 20 },
97 { "abcdefghijklmnopqrstu", 21 },
98 { "abcdefghijklmnopqrstuv", 22 },
99 { "abcdefghijklmnopqrstuvw", 23 },
102 * patterns that check for the cases where the expression:
104 * ((word - 0x7f7f..7f) & 0x8080..80)
106 * returns non-zero even though there are no zero bytes in the
107 * word.
110 { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
111 { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
112 { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
113 { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
114 { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
115 { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
116 { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
117 { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
118 { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
121 for (a = 0; a < sizeof(long); ++a) {
122 for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
124 memcpy(&buf[a], tab[t].val, tab[t].len + 1);
125 len = strlen_fn(&buf[a]);
127 if (len != tab[t].len) {
128 /* Write error without using printf (and strlen) */
129 write(2, "alignment ", 10);
130 write_num(a);
131 write(2, ", test ", 7);
132 write_num(t);
133 write(2, ", got len ", 10);
134 write_num(len);
135 write(2, ", not ", 6);
136 write_num(tab[t].len);
137 write(2, ", for '", 7);
138 write(2, tab[t].val, tab[t].len);
139 write(2, "'\n", 2);
140 exit(1);
147 main(void)
149 /* During testing it is useful have the rest of the program
150 * use a known good version! */
151 strlen_fn = dlsym(dlopen(NULL, RTLD_LAZY), "test_strlen");
152 if (!strlen_fn)
153 strlen_fn = strlen;
154 check_strlen();
155 return 0;