xref: /freebsd-src/contrib/arm-optimized-routines/string/test/memcpy.c (revision 31914882fca502069810b9e9ddea4bcd8136a4f4)
1*31914882SAlex Richardson /*
2*31914882SAlex Richardson  * memcpy test.
3*31914882SAlex Richardson  *
4*31914882SAlex Richardson  * Copyright (c) 2019-2020, Arm Limited.
5*31914882SAlex Richardson  * SPDX-License-Identifier: MIT
6*31914882SAlex Richardson  */
7*31914882SAlex Richardson 
8*31914882SAlex Richardson #include <stdint.h>
9*31914882SAlex Richardson #include <stdio.h>
10*31914882SAlex Richardson #include <stdlib.h>
11*31914882SAlex Richardson #include <string.h>
12*31914882SAlex Richardson #include "mte.h"
13*31914882SAlex Richardson #include "stringlib.h"
14*31914882SAlex Richardson #include "stringtest.h"
15*31914882SAlex Richardson 
16*31914882SAlex Richardson #define F(x, mte) {#x, x, mte},
17*31914882SAlex Richardson 
18*31914882SAlex Richardson static const struct fun
19*31914882SAlex Richardson {
20*31914882SAlex Richardson   const char *name;
21*31914882SAlex Richardson   void *(*fun) (void *, const void *, size_t);
22*31914882SAlex Richardson   int test_mte;
23*31914882SAlex Richardson } funtab[] = {
24*31914882SAlex Richardson   // clang-format off
25*31914882SAlex Richardson   F(memcpy, 0)
26*31914882SAlex Richardson #if __aarch64__
27*31914882SAlex Richardson   F(__memcpy_aarch64, 1)
28*31914882SAlex Richardson # if __ARM_NEON
29*31914882SAlex Richardson   F(__memcpy_aarch64_simd, 1)
30*31914882SAlex Richardson # endif
31*31914882SAlex Richardson #elif __arm__
32*31914882SAlex Richardson   F(__memcpy_arm, 0)
33*31914882SAlex Richardson #endif
34*31914882SAlex Richardson   {0, 0, 0}
35*31914882SAlex Richardson   // clang-format on
36*31914882SAlex Richardson };
37*31914882SAlex Richardson #undef F
38*31914882SAlex Richardson 
39*31914882SAlex Richardson #define A 32
40*31914882SAlex Richardson #define LEN 250000
41*31914882SAlex Richardson static unsigned char *dbuf;
42*31914882SAlex Richardson static unsigned char *sbuf;
43*31914882SAlex Richardson static unsigned char wbuf[LEN + 2 * A];
44*31914882SAlex Richardson 
45*31914882SAlex Richardson static void *
46*31914882SAlex Richardson alignup (void *p)
47*31914882SAlex Richardson {
48*31914882SAlex Richardson   return (void *) (((uintptr_t) p + A - 1) & -A);
49*31914882SAlex Richardson }
50*31914882SAlex Richardson 
51*31914882SAlex Richardson static void
52*31914882SAlex Richardson test (const struct fun *fun, int dalign, int salign, int len)
53*31914882SAlex Richardson {
54*31914882SAlex Richardson   unsigned char *src = alignup (sbuf);
55*31914882SAlex Richardson   unsigned char *dst = alignup (dbuf);
56*31914882SAlex Richardson   unsigned char *want = wbuf;
57*31914882SAlex Richardson   unsigned char *s = src + salign;
58*31914882SAlex Richardson   unsigned char *d = dst + dalign;
59*31914882SAlex Richardson   unsigned char *w = want + dalign;
60*31914882SAlex Richardson   void *p;
61*31914882SAlex Richardson   int i;
62*31914882SAlex Richardson 
63*31914882SAlex Richardson   if (err_count >= ERR_LIMIT)
64*31914882SAlex Richardson     return;
65*31914882SAlex Richardson   if (len > LEN || dalign >= A || salign >= A)
66*31914882SAlex Richardson     abort ();
67*31914882SAlex Richardson   for (i = 0; i < len + A; i++)
68*31914882SAlex Richardson     {
69*31914882SAlex Richardson       src[i] = '?';
70*31914882SAlex Richardson       want[i] = dst[i] = '*';
71*31914882SAlex Richardson     }
72*31914882SAlex Richardson   for (i = 0; i < len; i++)
73*31914882SAlex Richardson     s[i] = w[i] = 'a' + i % 23;
74*31914882SAlex Richardson 
75*31914882SAlex Richardson   s = tag_buffer (s, len, fun->test_mte);
76*31914882SAlex Richardson   d = tag_buffer (d, len, fun->test_mte);
77*31914882SAlex Richardson   p = fun->fun (d, s, len);
78*31914882SAlex Richardson   untag_buffer (s, len, fun->test_mte);
79*31914882SAlex Richardson   untag_buffer (d, len, fun->test_mte);
80*31914882SAlex Richardson 
81*31914882SAlex Richardson   if (p != d)
82*31914882SAlex Richardson     ERR ("%s(%p,..) returned %p\n", fun->name, d, p);
83*31914882SAlex Richardson   for (i = 0; i < len + A; i++)
84*31914882SAlex Richardson     {
85*31914882SAlex Richardson       if (dst[i] != want[i])
86*31914882SAlex Richardson 	{
87*31914882SAlex Richardson 	  ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign,
88*31914882SAlex Richardson 	       len);
89*31914882SAlex Richardson 	  quoteat ("got", dst, len + A, i);
90*31914882SAlex Richardson 	  quoteat ("want", want, len + A, i);
91*31914882SAlex Richardson 	  break;
92*31914882SAlex Richardson 	}
93*31914882SAlex Richardson     }
94*31914882SAlex Richardson }
95*31914882SAlex Richardson 
96*31914882SAlex Richardson int
97*31914882SAlex Richardson main ()
98*31914882SAlex Richardson {
99*31914882SAlex Richardson   dbuf = mte_mmap (LEN + 2 * A);
100*31914882SAlex Richardson   sbuf = mte_mmap (LEN + 2 * A);
101*31914882SAlex Richardson   int r = 0;
102*31914882SAlex Richardson   for (int i = 0; funtab[i].name; i++)
103*31914882SAlex Richardson     {
104*31914882SAlex Richardson       err_count = 0;
105*31914882SAlex Richardson       for (int d = 0; d < A; d++)
106*31914882SAlex Richardson 	for (int s = 0; s < A; s++)
107*31914882SAlex Richardson 	  {
108*31914882SAlex Richardson 	    int n;
109*31914882SAlex Richardson 	    for (n = 0; n < 100; n++)
110*31914882SAlex Richardson 	      test (funtab + i, d, s, n);
111*31914882SAlex Richardson 	    for (; n < LEN; n *= 2)
112*31914882SAlex Richardson 	      test (funtab + i, d, s, n);
113*31914882SAlex Richardson 	  }
114*31914882SAlex Richardson       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
115*31914882SAlex Richardson       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
116*31914882SAlex Richardson       if (err_count)
117*31914882SAlex Richardson 	r = -1;
118*31914882SAlex Richardson     }
119*31914882SAlex Richardson   return r;
120*31914882SAlex Richardson }
121