1 /*
2 * strlen test.
3 *
4 * Copyright (c) 2019-2022, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <limits.h>
13 #include "mte.h"
14 #include "stringlib.h"
15 #include "stringtest.h"
16
17 #define F(x, mte) {#x, x, mte},
18
19 static const struct fun
20 {
21 const char *name;
22 size_t (*fun) (const char *s);
23 int test_mte;
24 } funtab[] = {
25 // clang-format off
26 F(strlen, 0)
27 #if __aarch64__
28 F(__strlen_aarch64, 0)
29 F(__strlen_aarch64_mte, 1)
30 # if __ARM_FEATURE_SVE
31 F(__strlen_aarch64_sve, 1)
32 # endif
33 #elif __arm__
34 # if __ARM_ARCH >= 6 && __ARM_ARCH_ISA_THUMB == 2
35 F(__strlen_armv6t2, 0)
36 # endif
37 #endif
38 {0, 0, 0}
39 // clang-format on
40 };
41 #undef F
42
43 #define ALIGN 32
44 #define LEN 512
45 static char *sbuf;
46
47 static void *
alignup(void * p)48 alignup (void *p)
49 {
50 return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);
51 }
52
53 static void
test(const struct fun * fun,int align,int len)54 test (const struct fun *fun, int align, int len)
55 {
56 char *src = alignup (sbuf);
57 char *s = src + align;
58 size_t r;
59
60 if (err_count >= ERR_LIMIT)
61 return;
62 if (len > LEN || align >= ALIGN)
63 abort ();
64
65 for (int i = 0; src + i < s; i++)
66 src[i] = 0;
67 for (int i = 1; i <= ALIGN; i++)
68 s[len + i] = (len + align) & 1 ? 1 : 0;
69 for (int i = 0; i < len; i++)
70 s[i] = 'a' + (i & 31);
71 s[len] = '\0';
72
73 s = tag_buffer (s, len + 1, fun->test_mte);
74 r = fun->fun (s);
75 untag_buffer (s, len + 1, fun->test_mte);
76
77 if (r != len)
78 {
79 ERR ("%s (%p) returned %zu expected %d\n", fun->name, s, r, len);
80 quote ("input", src, len);
81 }
82 }
83
84 int
main(void)85 main (void)
86 {
87 sbuf = mte_mmap (LEN + 3 * ALIGN);
88 int r = 0;
89 for (int i = 0; funtab[i].name; i++)
90 {
91 err_count = 0;
92 for (int a = 0; a < ALIGN; a++)
93 for (int n = 0; n < LEN; n++)
94 test (funtab + i, a, n);
95
96 char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
97 printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
98 if (err_count)
99 r = -1;
100 }
101 return r;
102 }
103