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?
32 void check_strcpy(void);
37 /* try to trick the compiler */
38 char * (*f
)(char *, const char *s
) = strcpy
;
51 const struct tab tab
[] = {
53 * patterns that check for all combinations of leading and
54 * trailing unaligned characters (on a 64 bit processor)
68 { "abcdefghijk", 11 },
69 { "abcdefghijkl", 12 },
70 { "abcdefghijklm", 13 },
71 { "abcdefghijklmn", 14 },
72 { "abcdefghijklmno", 15 },
73 { "abcdefghijklmnop", 16 },
74 { "abcdefghijklmnopq", 17 },
75 { "abcdefghijklmnopqr", 18 },
76 { "abcdefghijklmnopqrs", 19 },
77 { "abcdefghijklmnopqrst", 20 },
78 { "abcdefghijklmnopqrstu", 21 },
79 { "abcdefghijklmnopqrstuv", 22 },
80 { "abcdefghijklmnopqrstuvw", 23 },
83 * patterns that check for the cases where the expression:
85 * ((word - 0x7f7f..7f) & 0x8080..80)
87 * returns non-zero even though there are no zero bytes in the
91 { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
92 { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
93 { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
94 { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
95 { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
96 { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
97 { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
98 { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
99 { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
102 for (a0
= 0; a0
< sizeof(long); ++a0
) {
103 for (a1
= 0; a1
< sizeof(long); ++a1
) {
104 for (t
= 0; t
< (sizeof(tab
) / sizeof(tab
[0])); ++t
) {
106 memcpy(&buf1
[a1
], tab
[t
].val
, tab
[t
].len
+ 1);
107 ret
= f(&buf0
[a0
], &buf1
[a1
]);
109 // verify strcpy returns address of first parameter
110 assert(&buf0
[a0
] == ret
);
112 // verify string was copied correctly
113 assert(memcmp(&buf0
[a0
], &buf1
[a1
], tab
[t
].len
+ 1) == 0);