1 /*
2 * strncmp test.
3 *
4 * Copyright (c) 2019-2022, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "mte.h"
13 #include "stringlib.h"
14 #include "stringtest.h"
15
16 #define F(x, mte) {#x, x, mte},
17
18 static const struct fun
19 {
20 const char *name;
21 int (*fun) (const char *, const char *, size_t);
22 int test_mte;
23 } funtab[] = {
24 // clang-format off
25 F(strncmp, 0)
26 #if __aarch64__
27 F(__strncmp_aarch64, 1)
28 # if __ARM_FEATURE_SVE
29 F(__strncmp_aarch64_sve, 1)
30 # endif
31 #endif
32 {0, 0, 0}
33 // clang-format on
34 };
35 #undef F
36
37 #define A 32
38 #define LEN 250000
39 static char *s1buf;
40 static char *s2buf;
41
42 static void *
alignup(void * p)43 alignup (void *p)
44 {
45 return (void *) (((uintptr_t) p + A - 1) & -A);
46 }
47
48 static void
test(const struct fun * fun,int s1align,int s2align,int maxlen,int diffpos,int len,int delta)49 test (const struct fun *fun, int s1align, int s2align, int maxlen, int diffpos,
50 int len, int delta)
51 {
52 char *src1 = alignup (s1buf);
53 char *src2 = alignup (s2buf);
54 char *s1 = src1 + s1align;
55 char *s2 = src2 + s2align;
56 int r;
57
58 if (err_count >= ERR_LIMIT)
59 return;
60 if (len > LEN || s1align >= A || s2align >= A)
61 abort ();
62 if (diffpos >= len)
63 abort ();
64 if ((diffpos < 0) != (delta == 0))
65 abort ();
66
67 for (int i = 0; i < len + A; i++)
68 src1[i] = src2[i] = '?';
69 for (int i = 0; i < len; i++)
70 s1[i] = s2[i] = 'a' + i % 23;
71 if (delta)
72 s1[diffpos] += delta;
73 s1[len] = s2[len] = '\0';
74
75 size_t mte_len = maxlen < len + 1 ? maxlen : len + 1;
76 s1 = tag_buffer (s1, mte_len, fun->test_mte);
77 s2 = tag_buffer (s2, mte_len, fun->test_mte);
78 r = fun->fun (s1, s2, maxlen);
79 untag_buffer (s1, mte_len, fun->test_mte);
80 untag_buffer (s2, mte_len, fun->test_mte);
81
82 if (diffpos >= maxlen)
83 {
84 diffpos = -1;
85 delta = 0;
86 }
87 if ((delta == 0 && r != 0) || (delta > 0 && r <= 0) || (delta < 0 && r >= 0))
88 {
89 ERR (
90 "%s(align %d, align %d, %d) (len=%d, diffpos=%d) failed, returned %d\n",
91 fun->name, s1align, s2align, maxlen, len, diffpos, r);
92 quoteat ("src1", src1, len + A, diffpos);
93 quoteat ("src2", src2, len + A, diffpos);
94 }
95 }
96
97 int
main()98 main ()
99 {
100 s1buf = mte_mmap (LEN + 2 * A + 1);
101 s2buf = mte_mmap (LEN + 2 * A + 1);
102 int r = 0;
103 for (int i = 0; funtab[i].name; i++)
104 {
105 err_count = 0;
106 for (int d = 0; d < A; d++)
107 for (int s = 0; s < A; s++)
108 {
109 int n;
110 test (funtab + i, d, s, 0, -1, 0, 0);
111 test (funtab + i, d, s, 1, -1, 0, 0);
112 test (funtab + i, d, s, 0, -1, 1, 0);
113 test (funtab + i, d, s, 1, -1, 1, 0);
114 test (funtab + i, d, s, 2, -1, 1, 0);
115 test (funtab + i, d, s, 1, 0, 1, 1);
116 test (funtab + i, d, s, 1, 0, 1, -1);
117 for (n = 2; n < 100; n++)
118 {
119 test (funtab + i, d, s, n, -1, n, 0);
120 test (funtab + i, d, s, n, n / 2, n, 1);
121 test (funtab + i, d, s, n / 2, -1, n, 0);
122 test (funtab + i, d, s, n / 2, n / 2, n, -1);
123 }
124 for (; n < LEN; n *= 2)
125 {
126 test (funtab + i, d, s, n, -1, n, 0);
127 test (funtab + i, d, s, n, n / 2, n, -1);
128 test (funtab + i, d, s, n / 2, -1, n, 0);
129 test (funtab + i, d, s, n / 2, n / 2, n, 1);
130 }
131 }
132 char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
133 printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
134 if (err_count)
135 r = -1;
136 }
137 return r;
138 }
139