2 * Written by J.T. Conklin <jtc@acorntoolworks.com>
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.
16 * Misssing checks for strncpy, strncat, strncmp, etc.
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?
35 void check_strlen(void);
37 size_t (*volatile strlen_fn
)(const char *s
);
44 for (i
= sizeof buf
; --i
>= 0;) {
45 buf
[i
] = '0' + val
% 10;
48 write(2, buf
+ i
, sizeof buf
- i
);
52 write(2, "overflow", 8);
58 /* try to trick the compiler */
70 const struct tab tab
[] = {
72 * patterns that check for all combinations of leading and
73 * trailing unaligned characters (on a 64 bit processor)
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
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);
131 write(2, ", test ", 7);
133 write(2, ", got len ", 10);
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
);
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");