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