1 /*
2  * memcmp test.
3  *
4  * Copyright (c) 2019-2020, 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 void *s1, const void *s2, size_t n);
22   int test_mte;
23 } funtab[] = {
24   // clang-format off
25   F(memcmp, 0)
26 #if __aarch64__
27   F(__memcmp_aarch64, 1)
28 # if __ARM_FEATURE_SVE
29   F(__memcmp_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 unsigned char *s1buf;
40 static unsigned char *s2buf;
41 
42 static void *
43 alignup (void *p)
44 {
45   return (void *) (((uintptr_t) p + A - 1) & -A);
46 }
47 
48 static void
49 test (const struct fun *fun, int s1align, int s2align, int len, int diffpos,
50       int delta)
51 {
52   unsigned char *src1 = alignup (s1buf);
53   unsigned char *src2 = alignup (s2buf);
54   unsigned char *s1 = src1 + s1align;
55   unsigned 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 
74   s1 = tag_buffer (s1, len, fun->test_mte);
75   s2 = tag_buffer (s2, len, fun->test_mte);
76   r = fun->fun (s1, s2, len);
77   untag_buffer (s1, len, fun->test_mte);
78   untag_buffer (s2, len, fun->test_mte);
79 
80   if ((delta == 0 && r != 0) || (delta > 0 && r <= 0) || (delta < 0 && r >= 0))
81     {
82       ERR ("%s(align %d, align %d, %d) failed, returned %d\n", fun->name,
83 	   s1align, s2align, len, r);
84       quoteat ("src1", src1, len + A, diffpos);
85       quoteat ("src2", src2, len + A, diffpos);
86     }
87 }
88 
89 int
90 main ()
91 {
92   s1buf = mte_mmap (LEN + 2 * A);
93   s2buf = mte_mmap (LEN + 2 * A);
94   int r = 0;
95   for (int i = 0; funtab[i].name; i++)
96     {
97       err_count = 0;
98       for (int d = 0; d < A; d++)
99 	for (int s = 0; s < A; s++)
100 	  {
101 	    int n;
102 	    test (funtab + i, d, s, 0, -1, 0);
103 	    test (funtab + i, d, s, 1, -1, 0);
104 	    test (funtab + i, d, s, 1, 0, -1);
105 	    test (funtab + i, d, s, 1, 0, 1);
106 	    for (n = 2; n < 100; n++)
107 	      {
108 		test (funtab + i, d, s, n, -1, 0);
109 		test (funtab + i, d, s, n, 0, -1);
110 		test (funtab + i, d, s, n, n - 1, -1);
111 		test (funtab + i, d, s, n, n / 2, 1);
112 	      }
113 	    for (; n < LEN; n *= 2)
114 	      {
115 		test (funtab + i, d, s, n, -1, 0);
116 		test (funtab + i, d, s, n, n / 2, -1);
117 	      }
118 	  }
119       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
120       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
121       if (err_count)
122 	r = -1;
123     }
124   return r;
125 }
126