131914882SAlex Richardson /*
231914882SAlex Richardson * strcpy test.
331914882SAlex Richardson *
4*072a4ba8SAndrew Turner * Copyright (c) 2019-2022, Arm Limited.
5*072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson */
731914882SAlex Richardson
831914882SAlex Richardson #include <stdint.h>
931914882SAlex Richardson #include <stdio.h>
1031914882SAlex Richardson #include <stdlib.h>
1131914882SAlex Richardson #include <string.h>
1231914882SAlex Richardson #include "mte.h"
1331914882SAlex Richardson #include "stringlib.h"
1431914882SAlex Richardson #include "stringtest.h"
1531914882SAlex Richardson
1631914882SAlex Richardson #define F(x, mte) {#x, x, mte},
1731914882SAlex Richardson
1831914882SAlex Richardson static const struct fun
1931914882SAlex Richardson {
2031914882SAlex Richardson const char *name;
2131914882SAlex Richardson char *(*fun) (char *dest, const char *src);
2231914882SAlex Richardson int test_mte;
2331914882SAlex Richardson } funtab[] = {
2431914882SAlex Richardson // clang-format off
2531914882SAlex Richardson F(strcpy, 0)
2631914882SAlex Richardson #if __aarch64__
27d49ad206SAndrew Turner F(__strcpy_aarch64, 1)
2831914882SAlex Richardson # if __ARM_FEATURE_SVE
2931914882SAlex Richardson F(__strcpy_aarch64_sve, 1)
3031914882SAlex Richardson # endif
3131914882SAlex Richardson #elif __arm__ && defined (__thumb2__) && !defined (__thumb__)
3231914882SAlex Richardson F(__strcpy_arm, 0)
3331914882SAlex Richardson #endif
3431914882SAlex Richardson {0, 0, 0}
3531914882SAlex Richardson // clang-format on
3631914882SAlex Richardson };
3731914882SAlex Richardson #undef F
3831914882SAlex Richardson
3931914882SAlex Richardson #define ALIGN 32
4031914882SAlex Richardson #define LEN 512
4131914882SAlex Richardson static char *dbuf;
4231914882SAlex Richardson static char *sbuf;
4331914882SAlex Richardson static char wbuf[LEN + 3 * ALIGN];
4431914882SAlex Richardson
4531914882SAlex Richardson static void *
alignup(void * p)4631914882SAlex Richardson alignup (void *p)
4731914882SAlex Richardson {
4831914882SAlex Richardson return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);
4931914882SAlex Richardson }
5031914882SAlex Richardson
5131914882SAlex Richardson static void
test(const struct fun * fun,int dalign,int salign,int len)5231914882SAlex Richardson test (const struct fun *fun, int dalign, int salign, int len)
5331914882SAlex Richardson {
5431914882SAlex Richardson char *src = alignup (sbuf);
5531914882SAlex Richardson char *dst = alignup (dbuf);
5631914882SAlex Richardson char *want = wbuf;
5731914882SAlex Richardson char *s = src + salign;
5831914882SAlex Richardson char *d = dst + dalign;
5931914882SAlex Richardson char *w = want + dalign;
6031914882SAlex Richardson void *p;
6131914882SAlex Richardson int i;
6231914882SAlex Richardson
6331914882SAlex Richardson if (err_count >= ERR_LIMIT)
6431914882SAlex Richardson return;
6531914882SAlex Richardson if (len > LEN || dalign >= ALIGN || salign >= ALIGN)
6631914882SAlex Richardson abort ();
6731914882SAlex Richardson for (i = 0; i < len + ALIGN; i++)
6831914882SAlex Richardson {
6931914882SAlex Richardson src[i] = '?';
7031914882SAlex Richardson want[i] = dst[i] = '*';
7131914882SAlex Richardson }
7231914882SAlex Richardson for (int i = 0; src + i < s; i++)
7331914882SAlex Richardson src[i] = 0;
7431914882SAlex Richardson for (int i = 1; i <= ALIGN; i++)
7531914882SAlex Richardson s[len + i] = (len + salign) & 1 ? 1 : 0;
7631914882SAlex Richardson for (i = 0; i < len; i++)
7731914882SAlex Richardson s[i] = w[i] = 'a' + (i & 31);
7831914882SAlex Richardson s[len] = w[len] = '\0';
7931914882SAlex Richardson
8031914882SAlex Richardson s = tag_buffer (s, len + 1, fun->test_mte);
8131914882SAlex Richardson d = tag_buffer (d, len + 1, fun->test_mte);
8231914882SAlex Richardson p = fun->fun (d, s);
8331914882SAlex Richardson untag_buffer (s, len + 1, fun->test_mte);
8431914882SAlex Richardson untag_buffer (d, len + 1, fun->test_mte);
8531914882SAlex Richardson
8631914882SAlex Richardson if (p != d)
8731914882SAlex Richardson ERR ("%s (%p,..) returned %p\n", fun->name, d, p);
8831914882SAlex Richardson
8931914882SAlex Richardson for (i = 0; i < len + ALIGN; i++)
9031914882SAlex Richardson {
9131914882SAlex Richardson if (dst[i] != want[i])
9231914882SAlex Richardson {
9331914882SAlex Richardson ERR ("%s (align %d, align %d, %d) failed\n",
9431914882SAlex Richardson fun->name, dalign, salign, len);
9531914882SAlex Richardson quoteat ("got", dst, len + ALIGN, i);
9631914882SAlex Richardson quoteat ("want", want, len + ALIGN, i);
9731914882SAlex Richardson break;
9831914882SAlex Richardson }
9931914882SAlex Richardson }
10031914882SAlex Richardson }
10131914882SAlex Richardson
10231914882SAlex Richardson int
main(void)10331914882SAlex Richardson main (void)
10431914882SAlex Richardson {
10531914882SAlex Richardson sbuf = mte_mmap (LEN + 3 * ALIGN);
10631914882SAlex Richardson dbuf = mte_mmap (LEN + 3 * ALIGN);
10731914882SAlex Richardson int r = 0;
10831914882SAlex Richardson for (int i = 0; funtab[i].name; i++)
10931914882SAlex Richardson {
11031914882SAlex Richardson err_count = 0;
11131914882SAlex Richardson for (int d = 0; d < ALIGN; d++)
11231914882SAlex Richardson for (int s = 0; s < ALIGN; s++)
11331914882SAlex Richardson for (int n = 0; n < LEN; n++)
11431914882SAlex Richardson test (funtab + i, d, s, n);
11531914882SAlex Richardson
11631914882SAlex Richardson char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
11731914882SAlex Richardson printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
11831914882SAlex Richardson if (err_count)
11931914882SAlex Richardson r = -1;
12031914882SAlex Richardson }
12131914882SAlex Richardson return r;
12231914882SAlex Richardson }
123