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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
|
/* $NetBSD: t_string.c,v 1.1 2010/12/25 21:10:24 pgoyette Exp $ */
/*
* Written by J.T. Conklin <jtc@acorntoolworks.com>
* Public domain.
*/
/*
* str*() regression suite
*
* Trivial str*() implementations can be audited by hand. Optimized
* versions that unroll loops, use naturally-aligned memory acesses,
* and "magic" arithmetic sequences to detect zero-bytes, written in
* assembler are much harder to validate. This program attempts to
* catch the corner cases.
*
* BUGS:
* Misssing checks for strncpy, strncat, strncmp, etc.
*
* TODO:
* Use mmap/mprotect to ensure the functions don't access memory
* across page boundaries.
*
* Consider generating tables programmatically. It would reduce
* the size, but it's also one more thing to go wrong.
*
* Share tables between strlen, strcpy, and strcat?
* Share tables between strchr and strrchr?
*/
#include <atf-c.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
/* for strchr */
char * (*volatile strchr_fn)(const char *, int);
ATF_TC(check_memchr);
ATF_TC_HEAD(check_memchr, tc)
{
atf_tc_set_md_var(tc, "descr", "Test memchr results");
}
ATF_TC_BODY(check_memchr, tc)
{
/* try to trick the compiler */
void * (*f)(const void *, int, size_t) = memchr;
unsigned int a, t;
void *off, *off2;
char buf[32];
struct tab {
const char *val;
size_t len;
char match;
ssize_t off;
};
const struct tab tab[] = {
{ "", 0, 0, 0 },
{ "/", 0, 0, 0 },
{ "/", 1, 1, 0 },
{ "/a", 2, 1, 0 },
{ "/ab", 3, 1, 0 },
{ "/abc", 4, 1, 0 },
{ "/abcd", 5, 1, 0 },
{ "/abcde", 6, 1, 0 },
{ "/abcdef", 7, 1, 0 },
{ "/abcdefg", 8, 1, 0 },
{ "a/", 1, 0, 0 },
{ "a/", 2, 1, 1 },
{ "a/b", 3, 1, 1 },
{ "a/bc", 4, 1, 1 },
{ "a/bcd", 5, 1, 1 },
{ "a/bcde", 6, 1, 1 },
{ "a/bcdef", 7, 1, 1 },
{ "a/bcdefg", 8, 1, 1 },
{ "ab/", 2, 0, 0 },
{ "ab/", 3, 1, 2 },
{ "ab/c", 4, 1, 2 },
{ "ab/cd", 5, 1, 2 },
{ "ab/cde", 6, 1, 2 },
{ "ab/cdef", 7, 1, 2 },
{ "ab/cdefg", 8, 1, 2 },
{ "abc/", 3, 0, 0 },
{ "abc/", 4, 1, 3 },
{ "abc/d", 5, 1, 3 },
{ "abc/de", 6, 1, 3 },
{ "abc/def", 7, 1, 3 },
{ "abc/defg", 8, 1, 3 },
{ "abcd/", 4, 0, 0 },
{ "abcd/", 5, 1, 4 },
{ "abcd/e", 6, 1, 4 },
{ "abcd/ef", 7, 1, 4 },
{ "abcd/efg", 8, 1, 4 },
{ "abcde/", 5, 0, 0 },
{ "abcde/", 6, 1, 5 },
{ "abcde/f", 7, 1, 5 },
{ "abcde/fg", 8, 1, 5 },
{ "abcdef/", 6, 0, 0 },
{ "abcdef/", 7, 1, 6 },
{ "abcdef/g", 8, 1, 6 },
{ "abcdefg/", 7, 0, 0 },
{ "abcdefg/", 8, 1, 7 },
{ "\xff\xff\xff\xff" "efg/", 8, 1, 7 },
{ "a" "\xff\xff\xff\xff" "fg/", 8, 1, 7 },
{ "ab" "\xff\xff\xff\xff" "g/", 8, 1, 7 },
{ "abc" "\xff\xff\xff\xff" "/", 8, 1, 7 },
};
for (a = 1; a < 1 + sizeof(long); ++a) {
for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
buf[a-1] = '/';
strcpy(&buf[a], tab[t].val);
off = f(&buf[a], '/', tab[t].len);
if (tab[t].match == 0) {
if (off != 0) {
fprintf(stderr, "a = %d, t = %d\n",
a, t);
atf_tc_fail("should not have found "
" char past len");
}
} else if (tab[t].match == 1) {
if (tab[t].off != ((char*)off - &buf[a])) {
fprintf(stderr, "a = %d, t = %d\n",
a, t);
atf_tc_fail("char not found at "
"correct offset");
}
} else {
fprintf(stderr, "a = %d, t = %d\n", a, t);
atf_tc_fail("Corrupt test case data");
}
/* check zero extension of char arg */
off2 = f(&buf[a], 0xffffff00 | '/', tab[t].len);
if (off2 != off)
atf_tc_fail("zero extension of char arg "
"failed");
}
}
}
ATF_TC(check_strcat);
ATF_TC_HEAD(check_strcat, tc)
{
atf_tc_set_md_var(tc, "descr", "Test strcat results");
}
ATF_TC_BODY(check_strcat, tc)
{
/* try to trick the compiler */
char * (*f)(char *, const char *s) = strcat;
unsigned int a0, a1, t0, t1;
char buf0[64];
char buf1[64];
char *ret;
struct tab {
const char* val;
size_t len;
};
const struct tab tab[] = {
/*
* patterns that check for all combinations of leading and
* trailing unaligned characters (on a 64 bit processor)
*/
{ "", 0 },
{ "a", 1 },
{ "ab", 2 },
{ "abc", 3 },
{ "abcd", 4 },
{ "abcde", 5 },
{ "abcdef", 6 },
{ "abcdefg", 7 },
{ "abcdefgh", 8 },
{ "abcdefghi", 9 },
{ "abcdefghij", 10 },
{ "abcdefghijk", 11 },
{ "abcdefghijkl", 12 },
{ "abcdefghijklm", 13 },
{ "abcdefghijklmn", 14 },
{ "abcdefghijklmno", 15 },
{ "abcdefghijklmnop", 16 },
{ "abcdefghijklmnopq", 17 },
{ "abcdefghijklmnopqr", 18 },
{ "abcdefghijklmnopqrs", 19 },
{ "abcdefghijklmnopqrst", 20 },
{ "abcdefghijklmnopqrstu", 21 },
{ "abcdefghijklmnopqrstuv", 22 },
{ "abcdefghijklmnopqrstuvw", 23 },
/*
* patterns that check for the cases where the expression:
*
* ((word - 0x7f7f..7f) & 0x8080..80)
*
* returns non-zero even though there are no zero bytes in
* the word.
*/
{ "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
{ "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
{ "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
{ "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
{ "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
{ "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
{ "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
{ "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
{ "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
};
for (a0 = 0; a0 < sizeof(long); ++a0) {
for (a1 = 0; a1 < sizeof(long); ++a1) {
for (t0 = 0; t0 < __arraycount(tab); ++t0) {
for (t1 = 0; t1 < __arraycount(tab); ++t1) {
memcpy(&buf0[a0], tab[t0].val,
tab[t0].len + 1);
memcpy(&buf1[a1], tab[t1].val,
tab[t1].len + 1);
ret = f(&buf0[a0], &buf1[a1]);
/*
* verify strcat returns address
* of first parameter
*/
if (&buf0[a0] != ret) {
fprintf(stderr, "a0 %d, a1 %d, "
"t0 %d, t1 %d\n",
a0, a1, t0, t1);
atf_tc_fail("strcat did not "
"return its first arg");
}
/* verify string copied correctly */
if (memcmp(&buf0[a0] + tab[t0].len,
&buf1[a1],
tab[t1].len + 1) != 0) {
fprintf(stderr, "a0 %d, a1 %d, "
"t0 %d, t1 %d\n",
a0, a1, t0, t1);
atf_tc_fail("string not copied "
"correctly");
}
}
}
}
}
}
static char *
slow_strchr(char *buf, int ch)
{
unsigned char c = 1;
ch &= 0xff;
for (; c != 0; buf++) {
c = *buf;
if (c == ch)
return buf;
}
return 0;
}
static void
verify_strchr(char *buf, int ch, unsigned int t, unsigned int a)
{
const char *off, *ok_off;
off = strchr_fn(buf, ch);
ok_off = slow_strchr(buf, ch);
if (off == ok_off)
return;
fprintf(stderr, "test_strchr(\"%s\", %#x) gave %zd not %zd (test %d, "
"alignment %d)\n",
buf, ch, off ? off - buf : -1, ok_off ? ok_off - buf : -1, t, a);
atf_tc_fail("Check stderr for details");
}
ATF_TC(check_strchr);
ATF_TC_HEAD(check_strchr, tc)
{
atf_tc_set_md_var(tc, "descr", "Test strchr results");
}
ATF_TC_BODY(check_strchr, tc)
{
unsigned int t, a;
char *off;
char buf[32];
const char *tab[] = {
"",
"a",
"aa",
"abc",
"abcd",
"abcde",
"abcdef",
"abcdefg",
"abcdefgh",
"/",
"//",
"/a",
"/a/",
"/ab",
"/ab/",
"/abc",
"/abc/",
"/abcd",
"/abcd/",
"/abcde",
"/abcde/",
"/abcdef",
"/abcdef/",
"/abcdefg",
"/abcdefg/",
"/abcdefgh",
"/abcdefgh/",
"a/",
"a//",
"a/a",
"a/a/",
"a/ab",
"a/ab/",
"a/abc",
"a/abc/",
"a/abcd",
"a/abcd/",
"a/abcde",
"a/abcde/",
"a/abcdef",
"a/abcdef/",
"a/abcdefg",
"a/abcdefg/",
"a/abcdefgh",
"a/abcdefgh/",
"ab/",
"ab//",
"ab/a",
"ab/a/",
"ab/ab",
"ab/ab/",
"ab/abc",
"ab/abc/",
"ab/abcd",
"ab/abcd/",
"ab/abcde",
"ab/abcde/",
"ab/abcdef",
"ab/abcdef/",
"ab/abcdefg",
"ab/abcdefg/",
"ab/abcdefgh",
"ab/abcdefgh/",
"abc/",
"abc//",
"abc/a",
"abc/a/",
"abc/ab",
"abc/ab/",
"abc/abc",
"abc/abc/",
"abc/abcd",
"abc/abcd/",
"abc/abcde",
"abc/abcde/",
"abc/abcdef",
"abc/abcdef/",
"abc/abcdefg",
"abc/abcdefg/",
"abc/abcdefgh",
"abc/abcdefgh/",
"abcd/",
"abcd//",
"abcd/a",
"abcd/a/",
"abcd/ab",
"abcd/ab/",
"abcd/abc",
"abcd/abc/",
"abcd/abcd",
"abcd/abcd/",
"abcd/abcde",
"abcd/abcde/",
"abcd/abcdef",
"abcd/abcdef/",
"abcd/abcdefg",
"abcd/abcdefg/",
"abcd/abcdefgh",
"abcd/abcdefgh/",
"abcde/",
"abcde//",
"abcde/a",
"abcde/a/",
"abcde/ab",
"abcde/ab/",
"abcde/abc",
"abcde/abc/",
"abcde/abcd",
"abcde/abcd/",
"abcde/abcde",
"abcde/abcde/",
"abcde/abcdef",
"abcde/abcdef/",
"abcde/abcdefg",
"abcde/abcdefg/",
"abcde/abcdefgh",
"abcde/abcdefgh/",
"abcdef/",
"abcdef//",
"abcdef/a",
"abcdef/a/",
"abcdef/ab",
"abcdef/ab/",
"abcdef/abc",
"abcdef/abc/",
"abcdef/abcd",
"abcdef/abcd/",
"abcdef/abcde",
"abcdef/abcde/",
"abcdef/abcdef",
"abcdef/abcdef/",
"abcdef/abcdefg",
"abcdef/abcdefg/",
"abcdef/abcdefgh",
"abcdef/abcdefgh/",
"abcdefg/",
"abcdefg//",
"abcdefg/a",
"abcdefg/a/",
"abcdefg/ab",
"abcdefg/ab/",
"abcdefg/abc",
"abcdefg/abc/",
"abcdefg/abcd",
"abcdefg/abcd/",
"abcdefg/abcde",
"abcdefg/abcde/",
"abcdefg/abcdef",
"abcdefg/abcdef/",
"abcdefg/abcdefg",
"abcdefg/abcdefg/",
"abcdefg/abcdefgh",
"abcdefg/abcdefgh/",
"abcdefgh/",
"abcdefgh//",
"abcdefgh/a",
"abcdefgh/a/",
"abcdefgh/ab",
"abcdefgh/ab/",
"abcdefgh/abc",
"abcdefgh/abc/",
"abcdefgh/abcd",
"abcdefgh/abcd/",
"abcdefgh/abcde",
"abcdefgh/abcde/",
"abcdefgh/abcdef",
"abcdefgh/abcdef/",
"abcdefgh/abcdefg",
"abcdefgh/abcdefg/",
"abcdefgh/abcdefgh",
"abcdefgh/abcdefgh/",
};
strchr_fn = dlsym(dlopen(0, RTLD_LAZY), "test_strchr");
if (!strchr_fn)
strchr_fn = strchr;
for (a = 3; a < 3 + sizeof(long); ++a) {
/* Put char and a \0 before the buffer */
buf[a-1] = '/';
buf[a-2] = '0';
buf[a-3] = 0xff;
for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
int len = strlen(tab[t]) + 1;
memcpy(&buf[a], tab[t], len);
/* Put the char we are looking for after the \0 */
buf[a + len] = '/';
/* Check search for NUL at end of string */
verify_strchr(buf + a, 0, t, a);
/* Then for the '/' in the strings */
verify_strchr(buf + a, '/', t, a);
/* check zero extension of char arg */
verify_strchr(buf + a, 0xffffff00 | '/', t, a);
/* Replace all the '/' with 0xff */
while ((off = slow_strchr(buf + a, '/')) != NULL)
*off = 0xff;
buf[a + len] = 0xff;
/* Check we can search for 0xff as well as '/' */
verify_strchr(buf + a, 0xff, t, a);
}
}
}
ATF_TC(check_strcmp);
ATF_TC_HEAD(check_strcmp, tc)
{
atf_tc_set_md_var(tc, "descr", "Test strcmp results");
}
ATF_TC_BODY(check_strcmp, tc)
{
/* try to trick the compiler */
int (*f)(const char *, const char *s) = strcmp;
unsigned int a0, a1, t;
char buf0[64];
char buf1[64];
int ret;
struct tab {
const char* val0;
const char* val1;
int ret;
};
const struct tab tab[] = {
{ "", "", 0 },
{ "a", "a", 0 },
{ "a", "b", -1 },
{ "b", "a", +1 },
{ "", "a", -1 },
{ "a", "", +1 },
{ "aa", "aa", 0 },
{ "aa", "ab", -1 },
{ "ab", "aa", +1 },
{ "a", "aa", -1 },
{ "aa", "a", +1 },
{ "aaa", "aaa", 0 },
{ "aaa", "aab", -1 },
{ "aab", "aaa", +1 },
{ "aa", "aaa", -1 },
{ "aaa", "aa", +1 },
{ "aaaa", "aaaa", 0 },
{ "aaaa", "aaab", -1 },
{ "aaab", "aaaa", +1 },
{ "aaa", "aaaa", -1 },
{ "aaaa", "aaa", +1 },
{ "aaaaa", "aaaaa", 0 },
{ "aaaaa", "aaaab", -1 },
{ "aaaab", "aaaaa", +1 },
{ "aaaa", "aaaaa", -1 },
{ "aaaaa", "aaaa", +1 },
{ "aaaaaa", "aaaaaa", 0 },
{ "aaaaaa", "aaaaab", -1 },
{ "aaaaab", "aaaaaa", +1 },
{ "aaaaa", "aaaaaa", -1 },
{ "aaaaaa", "aaaaa", +1 },
};
for (a0 = 0; a0 < sizeof(long); ++a0) {
for (a1 = 0; a1 < sizeof(long); ++a1) {
for (t = 0; t < __arraycount(tab); ++t) {
memcpy(&buf0[a0], tab[t].val0,
strlen(tab[t].val0) + 1);
memcpy(&buf1[a1], tab[t].val1,
strlen(tab[t].val1) + 1);
ret = f(&buf0[a0], &buf1[a1]);
if ((ret == 0 && tab[t].ret != 0) ||
(ret < 0 && tab[t].ret >= 0) ||
(ret > 0 && tab[t].ret <= 0)) {
fprintf(stderr, "a0 %d, a1 %d, t %d\n",
a0, a1, t);
fprintf(stderr, "\"%s\" \"%s\" %d\n",
&buf0[a0], &buf1[a1], ret);
atf_tc_fail("Check stderr for details");
}
}
}
}
}
ATF_TC(check_strcpy);
ATF_TC_HEAD(check_strcpy, tc)
{
atf_tc_set_md_var(tc, "descr", "Test strcpy results");
}
ATF_TC_BODY(check_strcpy, tc)
{
/* try to trick the compiler */
char * (*f)(char *, const char *s) = strcpy;
unsigned int a0, a1, t;
char buf0[64];
char buf1[64];
char *ret;
struct tab {
const char* val;
size_t len;
};
const struct tab tab[] = {
/*
* patterns that check for all combinations of leading and
* trailing unaligned characters (on a 64 bit processor)
*/
{ "", 0 },
{ "a", 1 },
{ "ab", 2 },
{ "abc", 3 },
{ "abcd", 4 },
{ "abcde", 5 },
{ "abcdef", 6 },
{ "abcdefg", 7 },
{ "abcdefgh", 8 },
{ "abcdefghi", 9 },
{ "abcdefghij", 10 },
{ "abcdefghijk", 11 },
{ "abcdefghijkl", 12 },
{ "abcdefghijklm", 13 },
{ "abcdefghijklmn", 14 },
{ "abcdefghijklmno", 15 },
{ "abcdefghijklmnop", 16 },
{ "abcdefghijklmnopq", 17 },
{ "abcdefghijklmnopqr", 18 },
{ "abcdefghijklmnopqrs", 19 },
{ "abcdefghijklmnopqrst", 20 },
{ "abcdefghijklmnopqrstu", 21 },
{ "abcdefghijklmnopqrstuv", 22 },
{ "abcdefghijklmnopqrstuvw", 23 },
/*
* patterns that check for the cases where the expression:
*
* ((word - 0x7f7f..7f) & 0x8080..80)
*
* returns non-zero even though there are no zero bytes in
* the word.
*/
{ "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
{ "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
{ "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
{ "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
{ "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
{ "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
{ "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
{ "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
{ "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
};
for (a0 = 0; a0 < sizeof(long); ++a0) {
for (a1 = 0; a1 < sizeof(long); ++a1) {
for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
memcpy(&buf1[a1], tab[t].val, tab[t].len + 1);
ret = f(&buf0[a0], &buf1[a1]);
/*
* verify strcpy returns address of
* first parameter
*/
if (&buf0[a0] != ret) {
fprintf(stderr, "a0 %d, a1 %d, t %d\n",
a0, a1, t);
atf_tc_fail("strcpy did not return "
"its first arg");
}
/*
* verify string was copied correctly
*/
if (memcmp(&buf0[a0], &buf1[a1],
tab[t].len + 1) != 0) {
fprintf(stderr, "a0 %d, a1 %d, t %d\n",
a0, a1, t);
atf_tc_fail("not correctly copied");
}
}
}
}
}
static void
write_num(int val)
{
char buf[20];
int i;
for (i = sizeof buf; --i >= 0;) {
buf[i] = '0' + val % 10;
val /= 10;
if (val == 0) {
write(2, buf + i, sizeof buf - i);
return;
}
}
write(2, "overflow", 8);
}
ATF_TC(check_strlen);
ATF_TC_HEAD(check_strlen, tc)
{
atf_tc_set_md_var(tc, "descr", "Test strlen results");
}
ATF_TC_BODY(check_strlen, tc)
{
/* try to trick the compiler */
size_t (*strlen_fn)(const char *);
unsigned int a, t;
size_t len;
char buf[64];
struct tab {
const char* val;
size_t len;
};
const struct tab tab[] = {
/*
* patterns that check for all combinations of leading and
* trailing unaligned characters (on a 64 bit processor)
*/
{ "", 0 },
{ "a", 1 },
{ "ab", 2 },
{ "abc", 3 },
{ "abcd", 4 },
{ "abcde", 5 },
{ "abcdef", 6 },
{ "abcdefg", 7 },
{ "abcdefgh", 8 },
{ "abcdefghi", 9 },
{ "abcdefghij", 10 },
{ "abcdefghijk", 11 },
{ "abcdefghijkl", 12 },
{ "abcdefghijklm", 13 },
{ "abcdefghijklmn", 14 },
{ "abcdefghijklmno", 15 },
{ "abcdefghijklmnop", 16 },
{ "abcdefghijklmnopq", 17 },
{ "abcdefghijklmnopqr", 18 },
{ "abcdefghijklmnopqrs", 19 },
{ "abcdefghijklmnopqrst", 20 },
{ "abcdefghijklmnopqrstu", 21 },
{ "abcdefghijklmnopqrstuv", 22 },
{ "abcdefghijklmnopqrstuvw", 23 },
/*
* patterns that check for the cases where the expression:
*
* ((word - 0x7f7f..7f) & 0x8080..80)
*
* returns non-zero even though there are no zero bytes in
* the word.
*/
{ "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
{ "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
{ "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
{ "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
{ "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
{ "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
{ "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
{ "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
{ "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
};
/*
* During testing it is useful have the rest of the program
* use a known good version!
*/
strlen_fn = dlsym(dlopen(NULL, RTLD_LAZY), "test_strlen");
if (!strlen_fn)
strlen_fn = strlen;
for (a = 0; a < sizeof(long); ++a) {
for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
memcpy(&buf[a], tab[t].val, tab[t].len + 1);
len = strlen_fn(&buf[a]);
if (len != tab[t].len) {
/* Write error without using printf / strlen */
write(2, "alignment ", 10);
write_num(a);
write(2, ", test ", 7);
write_num(t);
write(2, ", got len ", 10);
write_num(len);
write(2, ", not ", 6);
write_num(tab[t].len);
write(2, ", for '", 7);
write(2, tab[t].val, tab[t].len);
write(2, "'\n", 2);
atf_tc_fail("See stderr for details");
}
}
}
}
ATF_TC(check_strrchr);
ATF_TC_HEAD(check_strrchr, tc)
{
atf_tc_set_md_var(tc, "descr", "Test strrchr results");
}
ATF_TC_BODY(check_strrchr, tc)
{
/* try to trick the compiler */
char * (*f)(const char *, int) = strrchr;
unsigned int a, t;
char *off, *off2;
char buf[32];
struct tab {
const char* val;
char match;
ssize_t f_off; /* offset of first match */
ssize_t l_off; /* offset of last match */
};
const struct tab tab[] = {
{ "", 0, 0, 0 },
{ "a", 0, 0, 0 },
{ "aa", 0, 0, 0 },
{ "abc", 0, 0, 0 },
{ "abcd", 0, 0, 0 },
{ "abcde", 0, 0, 0 },
{ "abcdef", 0, 0, 0 },
{ "abcdefg", 0, 0, 0 },
{ "abcdefgh", 0, 0, 0 },
{ "/", 1, 0, 0 },
{ "//", 1, 0, 1 },
{ "/a", 1, 0, 0 },
{ "/a/", 1, 0, 2 },
{ "/ab", 1, 0, 0 },
{ "/ab/", 1, 0, 3 },
{ "/abc", 1, 0, 0 },
{ "/abc/", 1, 0, 4 },
{ "/abcd", 1, 0, 0 },
{ "/abcd/", 1, 0, 5 },
{ "/abcde", 1, 0, 0 },
{ "/abcde/", 1, 0, 6 },
{ "/abcdef", 1, 0, 0 },
{ "/abcdef/", 1, 0, 7 },
{ "/abcdefg", 1, 0, 0 },
{ "/abcdefg/", 1, 0, 8 },
{ "/abcdefgh", 1, 0, 0 },
{ "/abcdefgh/", 1, 0, 9 },
{ "a/", 1, 1, 1 },
{ "a//", 1, 1, 2 },
{ "a/a", 1, 1, 1 },
{ "a/a/", 1, 1, 3 },
{ "a/ab", 1, 1, 1 },
{ "a/ab/", 1, 1, 4 },
{ "a/abc", 1, 1, 1 },
{ "a/abc/", 1, 1, 5 },
{ "a/abcd", 1, 1, 1 },
{ "a/abcd/", 1, 1, 6 },
{ "a/abcde", 1, 1, 1 },
{ "a/abcde/", 1, 1, 7 },
{ "a/abcdef", 1, 1, 1 },
{ "a/abcdef/", 1, 1, 8 },
{ "a/abcdefg", 1, 1, 1 },
{ "a/abcdefg/", 1, 1, 9 },
{ "a/abcdefgh", 1, 1, 1 },
{ "a/abcdefgh/", 1, 1, 10 },
{ "ab/", 1, 2, 2 },
{ "ab//", 1, 2, 3 },
{ "ab/a", 1, 2, 2 },
{ "ab/a/", 1, 2, 4 },
{ "ab/ab", 1, 2, 2 },
{ "ab/ab/", 1, 2, 5 },
{ "ab/abc", 1, 2, 2 },
{ "ab/abc/", 1, 2, 6 },
{ "ab/abcd", 1, 2, 2 },
{ "ab/abcd/", 1, 2, 7 },
{ "ab/abcde", 1, 2, 2 },
{ "ab/abcde/", 1, 2, 8 },
{ "ab/abcdef", 1, 2, 2 },
{ "ab/abcdef/", 1, 2, 9 },
{ "ab/abcdefg", 1, 2, 2 },
{ "ab/abcdefg/", 1, 2, 10 },
{ "ab/abcdefgh", 1, 2, 2 },
{ "ab/abcdefgh/", 1, 2, 11 },
{ "abc/", 1, 3, 3 },
{ "abc//", 1, 3, 4 },
{ "abc/a", 1, 3, 3 },
{ "abc/a/", 1, 3, 5 },
{ "abc/ab", 1, 3, 3 },
{ "abc/ab/", 1, 3, 6 },
{ "abc/abc", 1, 3, 3 },
{ "abc/abc/", 1, 3, 7 },
{ "abc/abcd", 1, 3, 3 },
{ "abc/abcd/", 1, 3, 8 },
{ "abc/abcde", 1, 3, 3 },
{ "abc/abcde/", 1, 3, 9 },
{ "abc/abcdef", 1, 3, 3 },
{ "abc/abcdef/", 1, 3, 10 },
{ "abc/abcdefg", 1, 3, 3 },
{ "abc/abcdefg/", 1, 3, 11 },
{ "abc/abcdefgh", 1, 3, 3 },
{ "abc/abcdefgh/", 1, 3, 12 },
{ "abcd/", 1, 4, 4 },
{ "abcd//", 1, 4, 5 },
{ "abcd/a", 1, 4, 4 },
{ "abcd/a/", 1, 4, 6 },
{ "abcd/ab", 1, 4, 4 },
{ "abcd/ab/", 1, 4, 7 },
{ "abcd/abc", 1, 4, 4 },
{ "abcd/abc/", 1, 4, 8 },
{ "abcd/abcd", 1, 4, 4 },
{ "abcd/abcd/", 1, 4, 9 },
{ "abcd/abcde", 1, 4, 4 },
{ "abcd/abcde/", 1, 4, 10 },
{ "abcd/abcdef", 1, 4, 4 },
{ "abcd/abcdef/", 1, 4, 11 },
{ "abcd/abcdefg", 1, 4, 4 },
{ "abcd/abcdefg/", 1, 4, 12 },
{ "abcd/abcdefgh", 1, 4, 4 },
{ "abcd/abcdefgh/", 1, 4, 13 },
{ "abcde/", 1, 5, 5 },
{ "abcde//", 1, 5, 6 },
{ "abcde/a", 1, 5, 5 },
{ "abcde/a/", 1, 5, 7 },
{ "abcde/ab", 1, 5, 5 },
{ "abcde/ab/", 1, 5, 8 },
{ "abcde/abc", 1, 5, 5 },
{ "abcde/abc/", 1, 5, 9 },
{ "abcde/abcd", 1, 5, 5 },
{ "abcde/abcd/", 1, 5, 10 },
{ "abcde/abcde", 1, 5, 5 },
{ "abcde/abcde/", 1, 5, 11 },
{ "abcde/abcdef", 1, 5, 5 },
{ "abcde/abcdef/", 1, 5, 12 },
{ "abcde/abcdefg", 1, 5, 5 },
{ "abcde/abcdefg/", 1, 5, 13 },
{ "abcde/abcdefgh", 1, 5, 5 },
{ "abcde/abcdefgh/", 1, 5, 14 },
{ "abcdef/", 1, 6, 6 },
{ "abcdef//", 1, 6, 7 },
{ "abcdef/a", 1, 6, 6 },
{ "abcdef/a/", 1, 6, 8 },
{ "abcdef/ab", 1, 6, 6 },
{ "abcdef/ab/", 1, 6, 9 },
{ "abcdef/abc", 1, 6, 6 },
{ "abcdef/abc/", 1, 6, 10 },
{ "abcdef/abcd", 1, 6, 6 },
{ "abcdef/abcd/", 1, 6, 11 },
{ "abcdef/abcde", 1, 6, 6 },
{ "abcdef/abcde/", 1, 6, 12 },
{ "abcdef/abcdef", 1, 6, 6 },
{ "abcdef/abcdef/", 1, 6, 13 },
{ "abcdef/abcdefg", 1, 6, 6 },
{ "abcdef/abcdefg/", 1, 6, 14 },
{ "abcdef/abcdefgh", 1, 6, 6 },
{ "abcdef/abcdefgh/", 1, 6, 15 },
{ "abcdefg/", 1, 7, 7 },
{ "abcdefg//", 1, 7, 8 },
{ "abcdefg/a", 1, 7, 7 },
{ "abcdefg/a/", 1, 7, 9 },
{ "abcdefg/ab", 1, 7, 7 },
{ "abcdefg/ab/", 1, 7, 10 },
{ "abcdefg/abc", 1, 7, 7 },
{ "abcdefg/abc/", 1, 7, 11 },
{ "abcdefg/abcd", 1, 7, 7 },
{ "abcdefg/abcd/", 1, 7, 12 },
{ "abcdefg/abcde", 1, 7, 7 },
{ "abcdefg/abcde/", 1, 7, 13 },
{ "abcdefg/abcdef", 1, 7, 7 },
{ "abcdefg/abcdef/", 1, 7, 14 },
{ "abcdefg/abcdefg", 1, 7, 7 },
{ "abcdefg/abcdefg/", 1, 7, 15 },
{ "abcdefg/abcdefgh", 1, 7, 7 },
{ "abcdefg/abcdefgh/", 1, 7, 16 },
{ "abcdefgh/", 1, 8, 8 },
{ "abcdefgh//", 1, 8, 9 },
{ "abcdefgh/a", 1, 8, 8 },
{ "abcdefgh/a/", 1, 8, 10 },
{ "abcdefgh/ab", 1, 8, 8 },
{ "abcdefgh/ab/", 1, 8, 11 },
{ "abcdefgh/abc", 1, 8, 8 },
{ "abcdefgh/abc/", 1, 8, 12 },
{ "abcdefgh/abcd", 1, 8, 8 },
{ "abcdefgh/abcd/", 1, 8, 13 },
{ "abcdefgh/abcde", 1, 8, 8 },
{ "abcdefgh/abcde/", 1, 8, 14 },
{ "abcdefgh/abcdef", 1, 8, 8 },
{ "abcdefgh/abcdef/", 1, 8, 15 },
{ "abcdefgh/abcdefg", 1, 8, 8 },
{ "abcdefgh/abcdefg/", 1, 8, 16 },
{ "abcdefgh/abcdefgh", 1, 8, 8 },
{ "abcdefgh/abcdefgh/", 1, 8, 17 },
};
for (a = 0; a < sizeof(long); ++a) {
for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
strcpy(&buf[a], tab[t].val);
off = f(&buf[a], '/');
if (tab[t].match == 0) {
if (off != 0) {
fprintf(stderr, "a %d, t %d\n", a, t);
atf_tc_fail("strrchr should not have "
"found the character");
}
} else if (tab[t].match == 1) {
if (tab[t].l_off != (off - &buf[a])) {
fprintf(stderr, "a %d, t %d\n", a, t);
atf_tc_fail("strrchr returns wrong "
"offset");
}
} else {
fprintf(stderr, "a %d, t %d\n", a, t);
atf_tc_fail("bad test case data");
}
/* check zero extension of char arg */
off2 = f(&buf[a], 0xffffff00 | '/');
if (off != off2) {
fprintf(stderr, "a %d, t %d\n", a, t);
atf_tc_fail("zero extension of char arg fails");
}
}
}
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, check_memchr);
ATF_TP_ADD_TC(tp, check_strcat);
ATF_TP_ADD_TC(tp, check_strchr);
ATF_TP_ADD_TC(tp, check_strcmp);
ATF_TP_ADD_TC(tp, check_strcpy);
ATF_TP_ADD_TC(tp, check_strlen);
ATF_TP_ADD_TC(tp, check_strrchr);
return atf_no_error();
}
|