1 /*
2 * memset test.
3 *
4 * Copyright (c) 2019-2023, 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 void *(*fun) (void *s, int c, size_t n);
22 int test_mte;
23 } funtab[] = {
24 // clang-format off
25 F(memset, 0)
26 #if __aarch64__
27 F(__memset_aarch64, 1)
28 # if __ARM_FEATURE_SVE
29 F(__memset_aarch64_sve, 1)
30 # endif
31 # if WANT_MOPS
32 F(__memset_aarch64_mops, 1)
33 # endif
34 #elif __arm__
35 F(__memset_arm, 0)
36 #endif
37 {0, 0, 0}
38 // clang-format on
39 };
40 #undef F
41
42 #define A 32
43 #define LEN 250000
44 static unsigned char *sbuf;
45
46 static void *
alignup(void * p)47 alignup (void *p)
48 {
49 return (void *) (((uintptr_t) p + A - 1) & -A);
50 }
51
52 static void
test(const struct fun * fun,int salign,int c,int len)53 test (const struct fun *fun, int salign, int c, int len)
54 {
55 unsigned char *src = alignup (sbuf);
56 unsigned char *s = src + salign;
57 void *p;
58 int i;
59
60 if (err_count >= ERR_LIMIT)
61 return;
62 if (len > LEN || salign >= A)
63 abort ();
64 for (i = 0; i < len + A; i++)
65 src[i] = '?';
66 for (i = 0; i < len; i++)
67 s[i] = 'a' + i % 23;
68
69 s = tag_buffer (s, len, fun->test_mte);
70 p = fun->fun (s, c, len);
71 untag_buffer (s, len, fun->test_mte);
72
73 if (p != s)
74 ERR ("%s(%p,..) returned %p\n", fun->name, s, p);
75
76 for (i = 0; i < salign; i++)
77 {
78 if (src[i] != '?')
79 {
80 ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);
81 quoteat ("got", src, len + A, i);
82 return;
83 }
84 }
85 for (; i < salign + len; i++)
86 {
87 if (src[i] != (unsigned char) c)
88 {
89 ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);
90 quoteat ("got", src, len + A, i);
91 return;
92 }
93 }
94 for (; i < len + A; i++)
95 {
96 if (src[i] != '?')
97 {
98 ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);
99 quoteat ("got", src, len + A, i);
100 return;
101 }
102 }
103 }
104
105 int
main()106 main ()
107 {
108 sbuf = mte_mmap (LEN + 2 * A);
109 int r = 0;
110 for (int i = 0; funtab[i].name; i++)
111 {
112 err_count = 0;
113 for (int s = 0; s < A; s++)
114 {
115 int n;
116 for (n = 0; n < 100; n++)
117 {
118 test (funtab + i, s, 0, n);
119 test (funtab + i, s, 0x25, n);
120 test (funtab + i, s, 0xaa25, n);
121 }
122 for (; n < LEN; n *= 2)
123 {
124 test (funtab + i, s, 0, n);
125 test (funtab + i, s, 0x25, n);
126 test (funtab + i, s, 0xaa25, n);
127 }
128 }
129 char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
130 printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
131 if (err_count)
132 r = -1;
133 }
134 return r;
135 }
136