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