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