1bbbf1280Sopenharmony_ci/*
2bbbf1280Sopenharmony_ci * memmove test.
3bbbf1280Sopenharmony_ci *
4bbbf1280Sopenharmony_ci * Copyright (c) 2019-2020, Arm Limited.
5bbbf1280Sopenharmony_ci * SPDX-License-Identifier: MIT
6bbbf1280Sopenharmony_ci */
7bbbf1280Sopenharmony_ci
8bbbf1280Sopenharmony_ci#include <stdint.h>
9bbbf1280Sopenharmony_ci#include <stdio.h>
10bbbf1280Sopenharmony_ci#include <stdlib.h>
11bbbf1280Sopenharmony_ci#include <string.h>
12bbbf1280Sopenharmony_ci#include "mte.h"
13bbbf1280Sopenharmony_ci#include "stringlib.h"
14bbbf1280Sopenharmony_ci#include "stringtest.h"
15bbbf1280Sopenharmony_ci
16bbbf1280Sopenharmony_ci#define F(x, mte) {#x, x, mte},
17bbbf1280Sopenharmony_ci
18bbbf1280Sopenharmony_cistatic const struct fun
19bbbf1280Sopenharmony_ci{
20bbbf1280Sopenharmony_ci  const char *name;
21bbbf1280Sopenharmony_ci  void *(*fun) (void *, const void *, size_t);
22bbbf1280Sopenharmony_ci  int test_mte;
23bbbf1280Sopenharmony_ci} funtab[] = {
24bbbf1280Sopenharmony_ci  // clang-format off
25bbbf1280Sopenharmony_ci  F(memmove, 0)
26bbbf1280Sopenharmony_ci#if __aarch64__
27bbbf1280Sopenharmony_ci  F(__memmove_aarch64, 1)
28bbbf1280Sopenharmony_ci# if __ARM_NEON
29bbbf1280Sopenharmony_ci  F(__memmove_aarch64_simd, 1)
30bbbf1280Sopenharmony_ci# endif
31bbbf1280Sopenharmony_ci#endif
32bbbf1280Sopenharmony_ci  {0, 0, 0}
33bbbf1280Sopenharmony_ci  // clang-format on
34bbbf1280Sopenharmony_ci};
35bbbf1280Sopenharmony_ci#undef F
36bbbf1280Sopenharmony_ci
37bbbf1280Sopenharmony_ci#define A 32
38bbbf1280Sopenharmony_ci#define LEN 250000
39bbbf1280Sopenharmony_cistatic unsigned char *dbuf;
40bbbf1280Sopenharmony_cistatic unsigned char *sbuf;
41bbbf1280Sopenharmony_cistatic unsigned char wbuf[LEN + 2 * A];
42bbbf1280Sopenharmony_ci
43bbbf1280Sopenharmony_cistatic void *
44bbbf1280Sopenharmony_cialignup (void *p)
45bbbf1280Sopenharmony_ci{
46bbbf1280Sopenharmony_ci  return (void *) (((uintptr_t) p + A - 1) & -A);
47bbbf1280Sopenharmony_ci}
48bbbf1280Sopenharmony_ci
49bbbf1280Sopenharmony_cistatic void
50bbbf1280Sopenharmony_citest (const struct fun *fun, int dalign, int salign, int len)
51bbbf1280Sopenharmony_ci{
52bbbf1280Sopenharmony_ci  unsigned char *src = alignup (sbuf);
53bbbf1280Sopenharmony_ci  unsigned char *dst = alignup (dbuf);
54bbbf1280Sopenharmony_ci  unsigned char *want = wbuf;
55bbbf1280Sopenharmony_ci  unsigned char *s = src + salign;
56bbbf1280Sopenharmony_ci  unsigned char *d = dst + dalign;
57bbbf1280Sopenharmony_ci  unsigned char *w = want + dalign;
58bbbf1280Sopenharmony_ci  void *p;
59bbbf1280Sopenharmony_ci  int i;
60bbbf1280Sopenharmony_ci
61bbbf1280Sopenharmony_ci  if (err_count >= ERR_LIMIT)
62bbbf1280Sopenharmony_ci    return;
63bbbf1280Sopenharmony_ci  if (len > LEN || dalign >= A || salign >= A)
64bbbf1280Sopenharmony_ci    abort ();
65bbbf1280Sopenharmony_ci  for (i = 0; i < len + A; i++)
66bbbf1280Sopenharmony_ci    {
67bbbf1280Sopenharmony_ci      src[i] = '?';
68bbbf1280Sopenharmony_ci      want[i] = dst[i] = '*';
69bbbf1280Sopenharmony_ci    }
70bbbf1280Sopenharmony_ci  for (i = 0; i < len; i++)
71bbbf1280Sopenharmony_ci    s[i] = w[i] = 'a' + i % 23;
72bbbf1280Sopenharmony_ci
73bbbf1280Sopenharmony_ci  p = fun->fun (d, s, len);
74bbbf1280Sopenharmony_ci  if (p != d)
75bbbf1280Sopenharmony_ci    ERR ("%s(%p,..) returned %p\n", fun->name, d, p);
76bbbf1280Sopenharmony_ci  for (i = 0; i < len + A; i++)
77bbbf1280Sopenharmony_ci    {
78bbbf1280Sopenharmony_ci      if (dst[i] != want[i])
79bbbf1280Sopenharmony_ci	{
80bbbf1280Sopenharmony_ci	  ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign,
81bbbf1280Sopenharmony_ci	       len);
82bbbf1280Sopenharmony_ci	  quoteat ("got", dst, len + A, i);
83bbbf1280Sopenharmony_ci	  quoteat ("want", want, len + A, i);
84bbbf1280Sopenharmony_ci	  break;
85bbbf1280Sopenharmony_ci	}
86bbbf1280Sopenharmony_ci    }
87bbbf1280Sopenharmony_ci}
88bbbf1280Sopenharmony_ci
89bbbf1280Sopenharmony_cistatic void
90bbbf1280Sopenharmony_citest_overlap (const struct fun *fun, int dalign, int salign, int len)
91bbbf1280Sopenharmony_ci{
92bbbf1280Sopenharmony_ci  unsigned char *src = alignup (sbuf);
93bbbf1280Sopenharmony_ci  unsigned char *dst = src;
94bbbf1280Sopenharmony_ci  unsigned char *want = wbuf;
95bbbf1280Sopenharmony_ci  unsigned char *s = src + salign;
96bbbf1280Sopenharmony_ci  unsigned char *d = dst + dalign;
97bbbf1280Sopenharmony_ci  unsigned char *w = wbuf + dalign;
98bbbf1280Sopenharmony_ci  void *p;
99bbbf1280Sopenharmony_ci
100bbbf1280Sopenharmony_ci  if (err_count >= ERR_LIMIT)
101bbbf1280Sopenharmony_ci    return;
102bbbf1280Sopenharmony_ci  if (len > LEN || dalign >= A || salign >= A)
103bbbf1280Sopenharmony_ci    abort ();
104bbbf1280Sopenharmony_ci
105bbbf1280Sopenharmony_ci  for (int i = 0; i < len + A; i++)
106bbbf1280Sopenharmony_ci    src[i] = want[i] = '?';
107bbbf1280Sopenharmony_ci
108bbbf1280Sopenharmony_ci  for (int i = 0; i < len; i++)
109bbbf1280Sopenharmony_ci    s[i] = want[salign + i] = 'a' + i % 23;
110bbbf1280Sopenharmony_ci  for (int i = 0; i < len; i++)
111bbbf1280Sopenharmony_ci    w[i] = s[i];
112bbbf1280Sopenharmony_ci
113bbbf1280Sopenharmony_ci  s = tag_buffer (s, len, fun->test_mte);
114bbbf1280Sopenharmony_ci  d = tag_buffer (d, len, fun->test_mte);
115bbbf1280Sopenharmony_ci  p = fun->fun (d, s, len);
116bbbf1280Sopenharmony_ci  untag_buffer (s, len, fun->test_mte);
117bbbf1280Sopenharmony_ci  untag_buffer (d, len, fun->test_mte);
118bbbf1280Sopenharmony_ci
119bbbf1280Sopenharmony_ci  if (p != d)
120bbbf1280Sopenharmony_ci    ERR ("%s(%p,..) returned %p\n", fun->name, d, p);
121bbbf1280Sopenharmony_ci  for (int i = 0; i < len + A; i++)
122bbbf1280Sopenharmony_ci    {
123bbbf1280Sopenharmony_ci      if (dst[i] != want[i])
124bbbf1280Sopenharmony_ci	{
125bbbf1280Sopenharmony_ci	  ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign,
126bbbf1280Sopenharmony_ci	       len);
127bbbf1280Sopenharmony_ci	  quoteat ("got", dst, len + A, i);
128bbbf1280Sopenharmony_ci	  quoteat ("want", want, len + A, i);
129bbbf1280Sopenharmony_ci	  break;
130bbbf1280Sopenharmony_ci	}
131bbbf1280Sopenharmony_ci    }
132bbbf1280Sopenharmony_ci}
133bbbf1280Sopenharmony_ci
134bbbf1280Sopenharmony_ciint
135bbbf1280Sopenharmony_cimain ()
136bbbf1280Sopenharmony_ci{
137bbbf1280Sopenharmony_ci  dbuf = mte_mmap (LEN + 2 * A);
138bbbf1280Sopenharmony_ci  sbuf = mte_mmap (LEN + 2 * A);
139bbbf1280Sopenharmony_ci  int r = 0;
140bbbf1280Sopenharmony_ci  for (int i = 0; funtab[i].name; i++)
141bbbf1280Sopenharmony_ci    {
142bbbf1280Sopenharmony_ci      err_count = 0;
143bbbf1280Sopenharmony_ci      for (int d = 0; d < A; d++)
144bbbf1280Sopenharmony_ci	for (int s = 0; s < A; s++)
145bbbf1280Sopenharmony_ci	  {
146bbbf1280Sopenharmony_ci	    int n;
147bbbf1280Sopenharmony_ci	    for (n = 0; n < 100; n++)
148bbbf1280Sopenharmony_ci	      {
149bbbf1280Sopenharmony_ci		test (funtab + i, d, s, n);
150bbbf1280Sopenharmony_ci		test_overlap (funtab + i, d, s, n);
151bbbf1280Sopenharmony_ci	      }
152bbbf1280Sopenharmony_ci	    for (; n < LEN; n *= 2)
153bbbf1280Sopenharmony_ci	      {
154bbbf1280Sopenharmony_ci		test (funtab + i, d, s, n);
155bbbf1280Sopenharmony_ci		test_overlap (funtab + i, d, s, n);
156bbbf1280Sopenharmony_ci	      }
157bbbf1280Sopenharmony_ci	  }
158bbbf1280Sopenharmony_ci      char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
159bbbf1280Sopenharmony_ci      printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
160bbbf1280Sopenharmony_ci      if (err_count)
161bbbf1280Sopenharmony_ci	r = -1;
162bbbf1280Sopenharmony_ci    }
163bbbf1280Sopenharmony_ci  return r;
164bbbf1280Sopenharmony_ci}
165