131914882SAlex Richardson /*
231914882SAlex Richardson * strcmp 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 int (*fun) (const char *s1, const char *s2);
2231914882SAlex Richardson int test_mte;
2331914882SAlex Richardson } funtab[] = {
2431914882SAlex Richardson // clang-format off
2531914882SAlex Richardson F(strcmp, 0)
2631914882SAlex Richardson #if __aarch64__
27d49ad206SAndrew Turner F(__strcmp_aarch64, 1)
2831914882SAlex Richardson # if __ARM_FEATURE_SVE
2931914882SAlex Richardson F(__strcmp_aarch64_sve, 1)
3031914882SAlex Richardson # endif
3131914882SAlex Richardson #elif __arm__
3231914882SAlex Richardson # if __ARM_ARCH >= 7 && __ARM_ARCH_ISA_ARM >= 1
3331914882SAlex Richardson F(__strcmp_arm, 0)
3431914882SAlex Richardson # elif __ARM_ARCH == 6 && __ARM_ARCH_6M__ >= 1
3531914882SAlex Richardson F(__strcmp_armv6m, 0)
3631914882SAlex Richardson # endif
3731914882SAlex Richardson #endif
3831914882SAlex Richardson {0, 0, 0}
3931914882SAlex Richardson // clang-format on
4031914882SAlex Richardson };
4131914882SAlex Richardson #undef F
4231914882SAlex Richardson
4331914882SAlex Richardson #define A 32
4431914882SAlex Richardson #define LEN 250000
4531914882SAlex Richardson static char *s1buf;
4631914882SAlex Richardson static char *s2buf;
4731914882SAlex Richardson
4831914882SAlex Richardson static void *
alignup(void * p)4931914882SAlex Richardson alignup (void *p)
5031914882SAlex Richardson {
5131914882SAlex Richardson return (void *) (((uintptr_t) p + A - 1) & -A);
5231914882SAlex Richardson }
5331914882SAlex Richardson
5431914882SAlex Richardson static void
test(const struct fun * fun,int s1align,int s2align,int len,int diffpos,int delta)5531914882SAlex Richardson test (const struct fun *fun, int s1align, int s2align, int len, int diffpos,
5631914882SAlex Richardson int delta)
5731914882SAlex Richardson {
5831914882SAlex Richardson char *src1 = alignup (s1buf);
5931914882SAlex Richardson char *src2 = alignup (s2buf);
6031914882SAlex Richardson char *s1 = src1 + s1align;
6131914882SAlex Richardson char *s2 = src2 + s2align;
6231914882SAlex Richardson int r;
6331914882SAlex Richardson
6431914882SAlex Richardson if (err_count >= ERR_LIMIT)
6531914882SAlex Richardson return;
6631914882SAlex Richardson if (len > LEN || s1align >= A || s2align >= A)
6731914882SAlex Richardson abort ();
6831914882SAlex Richardson if (diffpos >= len)
6931914882SAlex Richardson abort ();
7031914882SAlex Richardson if ((diffpos < 0) != (delta == 0))
7131914882SAlex Richardson abort ();
7231914882SAlex Richardson
7331914882SAlex Richardson for (int i = 0; i < len + A; i++)
7431914882SAlex Richardson src1[i] = src2[i] = '?';
7531914882SAlex Richardson for (int i = 0; i < len; i++)
7631914882SAlex Richardson s1[i] = s2[i] = 'a' + i % 23;
7731914882SAlex Richardson if (delta)
7831914882SAlex Richardson s1[diffpos] += delta;
7931914882SAlex Richardson s1[len] = s2[len] = '\0';
8031914882SAlex Richardson
8131914882SAlex Richardson s1 = tag_buffer (s1, len + 1, fun->test_mte);
8231914882SAlex Richardson s2 = tag_buffer (s2, len + 1, fun->test_mte);
8331914882SAlex Richardson r = fun->fun (s1, s2);
8431914882SAlex Richardson untag_buffer (s1, len + 1, fun->test_mte);
8531914882SAlex Richardson untag_buffer (s2, len + 1, fun->test_mte);
8631914882SAlex Richardson
8731914882SAlex Richardson if ((delta == 0 && r != 0) || (delta > 0 && r <= 0) || (delta < 0 && r >= 0))
8831914882SAlex Richardson {
8931914882SAlex Richardson ERR ("%s(align %d, align %d, %d) failed, returned %d\n", fun->name,
9031914882SAlex Richardson s1align, s2align, len, r);
9131914882SAlex Richardson quoteat ("src1", src1, len + A, diffpos);
9231914882SAlex Richardson quoteat ("src2", src2, len + A, diffpos);
9331914882SAlex Richardson }
9431914882SAlex Richardson }
9531914882SAlex Richardson
9631914882SAlex Richardson int
main()9731914882SAlex Richardson main ()
9831914882SAlex Richardson {
9931914882SAlex Richardson s1buf = mte_mmap (LEN + 2 * A + 1);
10031914882SAlex Richardson s2buf = mte_mmap (LEN + 2 * A + 1);
10131914882SAlex Richardson int r = 0;
10231914882SAlex Richardson for (int i = 0; funtab[i].name; i++)
10331914882SAlex Richardson {
10431914882SAlex Richardson err_count = 0;
10531914882SAlex Richardson for (int d = 0; d < A; d++)
10631914882SAlex Richardson for (int s = 0; s < A; s++)
10731914882SAlex Richardson {
10831914882SAlex Richardson int n;
10931914882SAlex Richardson test (funtab + i, d, s, 0, -1, 0);
11031914882SAlex Richardson test (funtab + i, d, s, 1, -1, 0);
11131914882SAlex Richardson test (funtab + i, d, s, 1, 0, 1);
11231914882SAlex Richardson test (funtab + i, d, s, 1, 0, -1);
11331914882SAlex Richardson for (n = 2; n < 100; n++)
11431914882SAlex Richardson {
11531914882SAlex Richardson test (funtab + i, d, s, n, -1, 0);
11631914882SAlex Richardson test (funtab + i, d, s, n, n - 1, -1);
11731914882SAlex Richardson test (funtab + i, d, s, n, n / 2, 1);
11831914882SAlex Richardson }
11931914882SAlex Richardson for (; n < LEN; n *= 2)
12031914882SAlex Richardson {
12131914882SAlex Richardson test (funtab + i, d, s, n, -1, 0);
12231914882SAlex Richardson test (funtab + i, d, s, n, n / 2, -1);
12331914882SAlex Richardson }
12431914882SAlex Richardson }
12531914882SAlex Richardson char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
12631914882SAlex Richardson printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
12731914882SAlex Richardson if (err_count)
12831914882SAlex Richardson r = -1;
12931914882SAlex Richardson }
13031914882SAlex Richardson return r;
13131914882SAlex Richardson }
132