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