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